Option structureThe Option structure defines the option type, used for handling partial functions and optional values, and provides a collection of common combinators.
The type, the Option exception and the functions getOpt, valOf and isSome are available in the top-level environment.
signature OPTION
structure Option : OPTION
datatype 'a option = NONE | SOME of 'a
exception Option
val getOpt : ('a option * 'a) -> 'a
val isSome : 'a option -> bool
val valOf : 'a option -> 'a
val filter : ('a -> bool) -> 'a -> 'a option
val join : 'a option option -> 'a option
val map : ('a -> 'b) -> 'a option -> 'b option
val mapPartial : ('a -> 'b option) -> 'a option -> 'b option
val compose : (('a -> 'b) * ('c -> 'a option)) -> 'c -> 'b option
val composePartial : (('a -> 'b option) * ('c -> 'a option)) -> 'c -> 'b option
datatype 'a option
exception Option
getOpt (opt, a)
v if opt is SOME v; otherwise returns a.
isSome opt
true if opt is SOME v; otherwise returns false.
valOf opt
v if opt is SOME v; otherwise raises Option.
filter f a
SOME a if f a is true and NONE otherwise.
join opt
NONE to NONE and SOME v to v.
map f opt
NONE to NONE and SOME v to SOME (f v).
mapPartial f opt
NONE to NONE and SOME v to f v. The expression mapPartial f is equivalent to join o (map f).
compose (f, g) a
NONE if g a is NONE; otherwise, if g a is SOME v, it returns SOME (f v). Thus, the compose function composes f with the partial function g, to produce another partial function. The expression compose (f, g) is equivalent to (map f) o g.
composePartial (f, g) a
NONE if g a is NONE; otherwise, if g a is SOME v, it returns f v. Thus, the composePartial function composes the two partial functions f and g, to produce another partial function. The expression composePartial (f, g) is equivalent to (mapPartial f) o g.
Note that adding val unit = SOME to the functions map and join makes the type constructor option into a monad ([CITE]Wadler/, Section 7.2).
Last Modified January 22, 1997
Comments to John Reppy.
Copyright © 1997 Bell Labs, Lucent Technologies