1
(************************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
4
(* \VV/ **************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(************************************************************************)
9
(* $Id: option.mli 11576 2008-11-10 19:13:15Z msozeau $ *)
11
(** Module implementing basic combinators for OCaml option type.
12
It tries follow closely the style of OCaml standard library.
14
Actually, some operations have the same name as [List] ones:
15
they actually are similar considering ['a option] as a type
16
of lists with at most one element. *)
18
(** [has_some x] is [true] if [x] is of the form [Some y] and [false]
20
val has_some : 'a option -> bool
24
(** [get x] returns [y] where [x] is [Some y]. It raises IsNone
25
if [x] equals [None]. *)
26
val get : 'a option -> 'a
28
(** [make x] returns [Some x]. *)
29
val make : 'a -> 'a option
31
(** [init b x] returns [Some x] if [b] is [true] and [None] otherwise. *)
32
val init : bool -> 'a -> 'a option
34
(** [flatten x] is [Some y] if [x] is [Some (Some y)] and [None] otherwise. *)
35
val flatten : 'a option option -> 'a option
38
(** {6 "Iterators"} ***)
40
(** [iter f x] executes [f y] if [x] equals [Some y]. It does nothing
42
val iter : ('a -> unit) -> 'a option -> unit
44
exception Heterogeneous
46
(** [iter2 f x y] executes [f z w] if [x] equals [Some z] and [y] equals
47
[Some w]. It does nothing if both [x] and [y] are [None]. And raises
48
[Heterogeneous] otherwise. *)
49
val iter2 : ('a -> 'b -> unit) -> 'a option -> 'b option -> unit
51
(** [map f x] is [None] if [x] is [None] and [Some (f y)] if [x] is [Some y]. *)
52
val map : ('a -> 'b) -> 'a option -> 'b option
54
(** [smartmap f x] does the same as [map f x] except that it tries to share
56
val smartmap : ('a -> 'a) -> 'a option -> 'a option
58
(** [fold_left f a x] is [f a y] if [x] is [Some y], and [a] otherwise. *)
59
val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a option -> 'b
61
(** [fold_left2 f a x y] is [f z w] if [x] is [Some z] and [y] is [Some w].
62
It is [a] if both [x] and [y] are [None]. Otherwise it raises
64
val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b option -> 'c option -> 'a
66
(** [fold_right f x a] is [f y a] if [x] is [Some y], and [a] otherwise. *)
67
val fold_right : ('a -> 'b -> 'b) -> 'a option -> 'b -> 'b
69
(** [cata e f x] is [e] if [x] is [None] and [f a] if [x] is [Some a] *)
70
val cata : ('a -> 'b) -> 'b -> 'a option -> 'b
72
(** {6 More Specific Operations} ***)
74
(** [default a x] is [y] if [x] is [Some y] and [a] otherwise. *)
75
val default : 'a -> 'a option -> 'a
77
(** [lift] is the same as {!map}. *)
78
val lift : ('a -> 'b) -> 'a option -> 'b option
80
(** [lift_right f a x] is [Some (f a y)] if [x] is [Some y], and
82
val lift_right : ('a -> 'b -> 'c) -> 'a -> 'b option -> 'c option
84
(** [lift_left f x a] is [Some (f y a)] if [x] is [Some y], and
86
val lift_left : ('a -> 'b -> 'c) -> 'a option -> 'b -> 'c option
88
(** [lift2 f x y] is [Some (f z w)] if [x] equals [Some z] and [y] equals
89
[Some w]. It is [None] otherwise. *)
90
val lift2 : ('a -> 'b -> 'c) -> 'a option -> 'b option -> 'c option
93
(** {6 Operations with Lists} *)
96
(** [List.cons x l] equals [y::l] if [x] is [Some y] and [l] otherwise. *)
97
val cons : 'a option -> 'a list -> 'a list
99
(** [List.flatten l] is the list of all the [y]s such that [l] contains
100
[Some y] (in the same order). *)
101
val flatten : 'a option list -> 'a list
105
(** {6 Miscelaneous Primitives} *)
108
(** [Misc.compare f x y] lifts the equality predicate [f] to
109
option types. That is, if both [x] and [y] are [None] then
110
it returns [true], if they are bothe [Some _] then
111
[f] is called. Otherwise it returns [false]. *)
112
val compare : ('a -> 'a -> bool) -> 'a option -> 'a option -> bool