-
Notifications
You must be signed in to change notification settings - Fork 5
2015 002 Addition of Either module
Author: John Reppy
Last revised: August 17, 2015
Status: proposed
Discussion: issue #2
signature EITHER
structure Either :> EITHERThe Either structure provides a polymorphic disjoint-sum type with associated
operations.
datatype ('left, 'right) either = INL of 'left | INR of 'right
val isLeft : ('left, 'right) either -> bool
val isRight : ('left, 'right) either -> bool
val asLeft : ('left, 'right) either -> 'left option
val asRight : ('left, 'right) either -> 'right option
val map : ('ldom -> 'lrng) * ('rdom -> 'rrng)
-> ('ldom, 'rdom) either
-> ('lrng, 'rrng) either
val mapLeft : ('ldom -> 'lrng) -> ('ldom, 'rdom) either -> ('lrng, 'rdom) either
val mapRight : ('rdom -> 'rrng) -> ('ldom, 'rdom) either -> ('ldom, 'rrng) either
val app : ('left -> unit) * ('right -> unit)
-> ('left, 'right) either
-> unit
val appLeft : ('left -> unit) -> ('left, 'right) either -> unit
val appRight : ('right -> unit) -> ('left, 'right) either -> unit
val fold : ('left * 'b -> 'b) * ('right * 'b -> 'b)
-> 'b -> ('left, 'right) either -> 'b
val proj : ('a, 'a) either -> 'a
val partition : (('left, 'right) either) list -> ('left list * 'right list)-
datatype ('left, 'right) either = INL of 'left | INR of 'right
defines a generic union type. We say that a valueINL(x)is a left value withxas its contents. Likewise,INR(x)is a right value. -
isLeft sm
returns true ifsmis a left value. -
isRight sm
returns true ifsmis a right value. -
asLeft sm
returnsSOME(x)ifsmis a left value with contentsx, otherwise it returnsNONE. -
asRight sm
returnsSOME(x)ifsmis a right value with contentsx, otherwise it returnsNONE. -
map (fl, fr) sm
mapsflover the contents of left values andfrover the contents of right values. -
mapLeft f sm
maps the functionfover the contents of left values and acts as the identity on right values. -
mapRight f sm
maps the functionfover the contents of right values and acts as the identity on left values. -
app (fl, fr) sm
appliesflto the contents of left values andfrto the contents of right values. -
appLeft f sm
appliesfto the contents of left values and ignores right values. -
appRight f sm
appliesfto the contents of right values and ignores left values. -
fold (fl, fr) init sm
computesfx (v, init), wherevis the contents ofsmandfxis eitherfl(ifsmis a left value) orfr(ifsmis a right value). -
proj sm
projects out the contents ofsm. -
partition sms
partitions the list of sum values into a list of left values and a list of right values.
There are a number of possible names for this type and its constructors. We have selected
either for the type name (as opposed to sum), since that matches the name in Haskell.
On the other hand, we use the shorter constructor names INL and INR (instead of Left
and Right), because we believe that it will make patterns easier to read.
As with the list and option datatypes, it may also be useful to promote the either
datatype to the pervasive environment, but we should allow experience to inform
that decision.
Adopting this proposal should not affect existing programs.
This module is a natural candidate for inclusion in the Basis Library. Similar structures have been found to be useful in other functional languages.
-
[2016-08-10] Added
mapLeft,mapRight,appLeft, andappRightfunctions, which were suggested by Andreas Rossberg. -
[2015-08-17] Added
foldandprojfunctions, which were suggested by Matthew Fluet. -
[2015-08-03] Proposed
