Module Frama_c_kernel.Option

Extension of OCaml's Stdlib.Option module. Be wary that the parameters order of the bind function are reversed compared to the standard library and that get takes an optional exn argument. @see https://frama-c.com/download/frama-c-plugin-development-guide.pdf

include Monad.S_with_product with type 'a t = 'a option
val return : 'a -> 'a option
val flatten : 'a option option -> 'a option
module Operators : sig ... end
include module type of Option

Options

type 'a t = 'a option =
  1. | None
  2. | Some of 'a

The type for option values. Either None or a value Some v.

val none : 'a option

none is None.

val some : 'a -> 'a option

some v is Some v.

val value : 'a option -> default:'a -> 'a

value o ~default is v if o is Some v and default otherwise.

val join : 'a option option -> 'a option

join oo is Some v if oo is Some (Some v) and None otherwise.

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

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

val product : 'a option -> 'b option -> ('a * 'b) option

product o0 o1 is Some (v0, v1) if o0 is Some v0 and o1 is Some v1 and None otherwise.

  • since 5.5
val fold : none:'a -> some:('b -> 'a) -> 'b option -> 'a

fold ~none ~some o is none if o is None and some v if o is Some v.

val iter : ('a -> unit) -> 'a option -> unit

iter f o is f v if o is Some v and () otherwise.

val blend : ('a -> 'a -> 'a) -> 'a option -> 'a option -> 'a option

blend f o1 o2 is o1 if o2 is None, o2 if o1 is None, and Some (f v1 v2) if o1 is Some v1 and o2 is Some v2.

  • since 5.5
val for_all : ('a -> bool) -> 'a option -> bool

for_all p behaves like List.for_all p on a list of zero or one element:

  • for_all p None is true,
  • for_all p (Some v) is p v.
  • since 5.5
val exists : ('a -> bool) -> 'a option -> bool

exists p behaves like List.exists p on a list of zero or one element:

  • exists p None is false,
  • exists p (Some v) is p v.
  • since 5.5

Predicates and comparisons

val is_none : 'a option -> bool

is_none o is true if and only if o is None.

val is_some : 'a option -> bool

is_some o is true if and only if o is Some o.

val equal : ('a -> 'a -> bool) -> 'a option -> 'a option -> bool

equal eq o0 o1 is true if and only if o0 and o1 are both None or if they are Some v0 and Some v1 and eq v0 v1 is true.

val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int

compare cmp o0 o1 is a total order on options using cmp to compare values wrapped by Some _. None is smaller than Some _ values.

Converting

val to_result : none:'e -> 'a option -> ('a, 'e) result

to_result ~none o is Ok v if o is Some v and Error none otherwise.

val to_list : 'a option -> 'a list

to_list o is [] if o is None and [v] if o is Some v.

val to_seq : 'a option -> 'a Seq.t

to_seq o is o as a sequence. None is the empty sequence and Some v is the singleton sequence containing v.

Syntax

module Syntax : sig ... end

Binding operators. See manual section 12.23 for details.

module Make_monadic_iterators (M : Monad.S) : Monad.Iterators with type 'a iterable := 'a option and type 'a monad := 'a M.t

Make iterators to handle options of monadic elements and monadic options.

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

The call opt <? default is equivalent to value ~default opt.

  • since 33.0-Arsenic
val bind : ('a -> 'b t) -> 'a t -> 'b t

Reverse Stdlib.Option.bind parameters for monad compatibility. bind f o is f v if o is Some v and None if o is None.

val get : ?exn:exn -> 'a option -> 'a

Redefines Stdlib.Option.get with a exn parameter.

  • raises Exn

    if the value is None and exn is specified.

  • returns

    v if the value is Some v.

  • since 33.0-Arsenic
val value_or_else : none:(unit -> 'a) -> 'a option -> 'a

value_or_else ~none o is similar to value but uses a function to compute the default value.

  • since 33.0-Arsenic
val hash : ('a -> int) -> 'a option -> int

Compute a hash for the option given a hash for the element.

  • since 33.0-Arsenic
val merge : ('a -> 'a -> 'a) -> 'a option -> 'a option -> 'a option

Merges two options such that

  • merge None None = None
  • merge (Some a) None = Some a
  • merge None (Some b) = Some b
  • merge (Some a) (Some b) = Some (f a b) See also product and map2 for other ways to combine options.
  • since 33.0-Arsenic
val map2 : ('a -> 'b -> 'c) -> 'a option -> 'b option -> 'c option

Maps two options such that

  • map2 None None = None
  • map2 (Some a) None = None
  • map2 None (Some b) = None
  • map2 (Some a) (Some b) = Some (f a b) See also product and merge for other ways to combine options.
  • since 33.0-Arsenic
val map_no_copy : ('a -> 'a) -> 'a option -> 'a option

Same as Stdlib.Option.map but avoid creating a copy of the option if the mapped function returns its argument (tested through physical equality).

  • since 33.0-Arsenic
val filter : ('a -> bool) -> 'a option -> 'a option

filter f (Some a) applies f to a and returns Some a if f a is true or None if f a is false. filter f None always returns None.

  • since 33.0-Arsenic