Module Support.Option

include module type of Stdlib.Option
type !'a t = 'a option =
  1. | None
  2. | Some of 'a
val none : 'a option
val some : 'a -> 'a option
val value : 'a option -> default:'a -> 'a
val get : 'a option -> 'a
val join : 'a option option -> 'a option
val fold : none:'a -> some:('b -> 'a) -> 'b option -> 'a
val iter : ('a -> unit) -> 'a option -> unit
val is_none : 'a option -> bool
val is_some : 'a option -> bool
val equal : ('a -> 'a -> bool) -> 'a option -> 'a option -> bool
val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int
val to_result : none:'e -> 'a option -> ('a, 'e) Stdlib.result
val to_list : 'a option -> 'a list
val to_seq : 'a option -> 'a Stdlib.Seq.t
val eliminate : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b

eliminate none_handler some_handler o is some_handler v if o is Some v, and none_handler () otherwise.

val get_or_else : (unit -> 'a) -> 'a t -> 'a

get_or_else f o is f () if o is None and v if o is Some v.

val of_bool : bool -> unit t

of_bool b is Some () if b is true, and None otherwise.

When used with other monadic operations, this is (a specialized) guard function from Haskell, which allows to abort a monadic computation on account of a boolean check.

val from_predicate : ('a -> bool) -> 'a -> 'a t

from_predicate p a is Some a if p a, and None otherwise.

val lazy_alt : 'a t Stdlib.Lazy.t -> 'a t Stdlib.Lazy.t -> 'a t Stdlib.Lazy.t

lazy_alt a1 a2 is a1 if it is not lazy None, and a2 otherwise.

val (<||>) : 'a t Stdlib.Lazy.t -> 'a t Stdlib.Lazy.t -> 'a t Stdlib.Lazy.t

Infix operator alias of lazy_alt.

val void : 'a t -> unit t
val when_some : 'a t -> ('a -> unit) -> unit

Specialized effectful eliminator for option types.

val print : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit

Prints an option by doing nothing if it is None; else it uses the given printer.

val pp : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit

Prints an option by showing "None" or "Some (X)" where "X" is generated by the given formatting function.

Instances

include Monad.MONAD with type 'a t := 'a t
val return : 'a -> 'a t

return a injects a into the monadic type.

val bind : ('a -> 'b t) -> 'a t -> 'b t

bind f a is the sequential composition of two actions, passing any value produced by a as argument to f.

val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

Operator alias of bind.

val compose : ('b -> 'c t) -> ('a -> 'b t) -> 'a -> 'c t

compose g f is the Kleisli composition of f and g, passing the input to f, then binding the output to g.

val (>=>) : ('a -> 'b t) -> ('b -> 'c t) -> 'a -> 'c t

Operator alias of compose.

val (let*) : 'a t -> ('a -> 'b t) -> 'b t

( let* ) ma f is bind f ma. This is a binding operator, and it is used as let* a = ma in f a.

val (and*) : 'a t -> 'b t -> ('a * 'b) t

( and* ) ma mb is let* a = ma in let* b = mb in return (a, b). This is a binding operator, and it is used as let* a = ma and* b = mb in ....

include Functor.FUNCTOR with type 'a t := 'a t

Combinators

val map : ('a -> 'b) -> 'a t -> 'b t

map f is the function that maps values of t by f. The order of arguments is for use in function pipelines as fb = fa |> map (fun a -> (* ... *)).

val ($>) : 'a t -> ('a -> 'b) -> 'b t

( $> ) is an infix synonym for map.

val (let+) : 'a t -> ('a -> 'b) -> 'b t

( let+ ) ma f is map f a. This is a binding operator, and is used as let+ a = ma in f a

val (and+) : 'a t -> 'b t -> ('a * 'b) t

( and+ ) ma mb is let+ a = ma in let+ b = mb in return (a, b). This is a binding operator, and it is used as let+ a = ma and+ b = mb in ....

include Apply.APPLY with type 'a t := 'a t
val ap : 'a t -> ('a -> 'b) t -> 'b t

ap fa fab applies argument fa to fab under the abstract datatype t.

val (<&>) : ('a -> 'b) t -> 'a t -> 'b t

( <&> ) is an infix synonym of ap.

Combinators

val ap_first : 'b t -> 'a t -> 'a t

ap_first second first combines actions first and second but keeps only first. That is, ap_first second first = first. The order of arguments is for use in function pipelines as first = first |> ap_first second.

val (<&) : 'a t -> 'b t -> 'a t

( <& ) is an infix synonym for ap_first.

val ap_second : 'b t -> 'a t -> 'b t

ap_second second first combines actions first and second but keeps only second. That is, ap_second second first = second. The order of arguments is for use in function pipelines as second = first |> ap_second second.

val (&>) : 'a t -> 'b t -> 'b t

( &> ) is an infix synonym for ap_second.

val seq2 : 'a1 t -> 'a2 t -> ('a1 * 'a2) t

seq2 fa1 fa2 sequentially executes actions fa1 and fa2, and keeps their outputs under the abstract datatype t.

val seq3 : 'a1 t -> 'a2 t -> 'a3 t -> ('a1 * 'a2 * 'a3) t

seq3 fa1 fa2 fa3 sequentially executes actions fa1, fa2 and fa3, and keeps their outputs under the abstract datatype t.

val seq4 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> ('a1 * 'a2 * 'a3 * 'a4) t

seq4 fa1 fa2 fa3 fa4 sequentially executes actions fa1, fa2, fa3 and fa4, and keeps their outputs under the abstract datatype t.

val seq5 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'a5 t -> ('a1 * 'a2 * 'a3 * 'a4 * 'a5) t

seq5 fa1 fa2 fa3 fa4 fa5 sequentially executes actions fa1, fa2, fa3, fa4 and fa5, and keeps their outputs under the abstract datatype t.

val lift2 : ('a1 -> 'a2 -> 'r) -> 'a1 t -> 'a2 t -> 'r t

lift2 f ma1 ma2 sequentially executes actions ma1, ma2 and passes their outputs to f.

val lift3 : ('a1 -> 'a2 -> 'a3 -> 'r) -> 'a1 t -> 'a2 t -> 'a3 t -> 'r t

lift3 f ma1 ma2 ma3 sequentially executes actions ma1, ma2, ma3 and passes their outputs to f.

val lift4 : ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'r) -> 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'r t

lift4 f ma1 ma2 ma3 ma4 sequentially executes actions ma1, ma2, ma3, ma4 and passes their outputs to f.

val lift5 : ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'r) -> 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'a5 t -> 'r t

lift5 f ma1 ma2 ma3 ma4 ma5 sequentially executes actions ma1, ma2, ma3, ma4, ma5 and passes their outputs to f.

val replicate : int -> 'a t -> 'a list t

replicate n a sequentially runs n times a.

  • raises Invalid_argument

    If n < 0.

include Alternative.ALTERNATIVE with type 'a t := 'a t
val empty : 'a t

The identity element of (<|>).

val alt : 'a t -> 'a t -> 'a t

alt a1 a2 is a1 if it is not empty, and a2 otherwise.

val (<|>) : 'a t -> 'a t -> 'a t

Infix synonym of alt.

module MakeEq (E : Eq.EQ) : Eq.EQ with type t = E.t t

Functor for an instance of Eq.EQ over option values using equal.

module MakeOrd (O : Ord.ORD) : Ord.ORD with type t = O.t t

Functor for an instance of Ord.ORD over option values using compare.

module MakeShow (S : Show.SHOW) : Show.SHOW with type t = S.t t

Functor for an instance of Show.SHOW over option values using pp.