Either structure

May 20, 2026 ยท View on GitHub

Up to index

The Either structure provides a polymorphic disjoint-sum type ('left, 'right) either whose values are either INL v (left) or INR v (right), along with operations for examining and transforming such values.

Synopsis

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

is the type of disjoint-sum values; INL v represents a left value and INR v represents a right value.

isLeft

isLeft sm (or sm.isLeft ()) returns true if sm is a left value.

isRight

isRight sm (or sm.isRight ()) returns true if sm is a right value.

asLeft

asLeft sm (or sm.asLeft ()) returns SOME (x) if sm is a left value with contents x, otherwise it returns NONE.

asRight

asRight sm (or sm.asRight ()) returns SOME (x) if sm is a right value with contents x, otherwise it returns NONE.

map

map (fl, fr) sm maps fl over the contents of left values and fr over the contents of right values.

mapLeft

mapLeft f sm maps the function f over the contents of left values and acts as the identity on right values.

mapRight

mapRight f sm maps the function f over the contents of right values and acts as the identity on left values.

app

app (fl, fr) sm applies fl to the contents of left values and fr to the contents of right values.

appLeft

appLeft f sm applies f to the contents of left values and ignores right values.

appRight

appRight f sm applies f to the contents of right values and ignores left values.

fold

fold (fl, fr) init sm computes fx (v, init), where v is the contents of sm and fx is either fl (if sm is a left value) or fr (if sm is a right value).

proj

proj sm (or sm.proj ()) projects out the contents of sm.

partition

partition sms partitions the list of sum values into a list of left values and a list of right values.