Option structure

May 20, 2026 ยท View on GitHub

Up to index

The Option structure provides the option type 'a option whose values are either NONE (absent) or SOME v (present), along with operations for creating, examining, and transforming optional values.

Specified by the Standard ML Basis Library.

Synopsis

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 app : ('a -> unit) -> 'a option -> unit
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

The type option provides a distinction between some value and no value, and is often used for representing the result of partially defined functions. It can be viewed as a typed version of the C convention of returning a NULL pointer to indicate no value.

exception Option

is raised by valOf when applied to NONE.

getOpt

getOpt (opt, a) (or opt.getOpt a) returns v if opt is SOME (v); otherwise returns a.

isSome

isSome opt (or opt.isSome ()) returns true if opt is SOME v; otherwise returns false.

valOf

valOf opt (or opt.valOf ()) returns v if opt is SOME v, otherwise raises Option.

filter

filter f a returns SOME a if f(a) is true, NONE otherwise.

join

join opt maps NONE to NONE and SOME v to v.

app

app f opt applies the function f to the value v if opt is SOME v, and otherwise does nothing.

map

map f opt maps NONE to NONE and SOME v to SOME (f v).

mapPartial

mapPartial f opt maps NONE to NONE and SOME v to f(v).

compose

compose (f, g) a returns NONE if g(a) is NONE; otherwise, if g(a) is SOME v, it returns SOME (f v).

composePartial

composePartial (f, g) a returns NONE if g(a) is NONE; otherwise, if g(a) is SOME v, returns f(v).