| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** Module implementing basic combinators for OCaml option type. |
| 12 | It tries follow closely the style of OCaml standard library. |
| 13 | |
| 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. *) |
| 17 | |
| 18 | (** [has_some x] is [true] if [x] is of the form [Some y] and [false] |
| 19 | otherwise. *) |
| 20 | val has_some : 'a option -> bool |
| 21 | |
| 22 | (** Negation of [has_some] *) |
| 23 | val is_empty : 'a option -> bool |
| 24 | |
| 25 | (** [equal f x y] lifts the equality predicate [f] to |
| 26 | option types. That is, if both [x] and [y] are [None] then |
| 27 | it returns [true], if they are both [Some _] then |
| 28 | [f] is called. Otherwise it returns [false]. *) |
| 29 | val equal : ('a -> 'a -> bool) -> 'a option -> 'a option -> bool |
| 30 | |
| 31 | (** Same as [equal], but with comparison. *) |
| 32 | val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int |
| 33 | |
| 34 | (** Lift a hash to option types. *) |
| 35 | val hash : ('a -> int) -> 'a option -> int |
| 36 | |
| 37 | (** [get x] returns [y] where [x] is [Some y], and asserts false otherwise. *) |
| 38 | val get : 'a option -> 'a |
| 39 | |
| 40 | (** [make x] returns [Some x]. *) |
| 41 | val make : 'a -> 'a option |
| 42 | |
| 43 | (** [bind x f] is [f y] if [x] is [Some y] and [None] otherwise *) |
| 44 | val bind : 'a option -> ('a -> 'b option) -> 'b option |
| 45 | |
| 46 | (** [filter f x] is [x] if [x] [Some y] and [f y] is true, [None] otherwise *) |
| 47 | val filter : ('a -> bool) -> 'a option -> 'a option |
| 48 | |
| 49 | (** [init b x] returns [Some x] if [b] is [true] and [None] otherwise. *) |
| 50 | val init : bool -> 'a -> 'a option |
| 51 | |
| 52 | (** [flatten x] is [Some y] if [x] is [Some (Some y)] and [None] otherwise. *) |
| 53 | val flatten : 'a option option -> 'a option |
| 54 | |
| 55 | (** [append x y] is the first element of the concatenation of [x] and |
| 56 | [y] seen as lists. In other words, [append (Some a) y] is [Some |
| 57 | a], [append None (Some b)] is [Some b], and [append None None] is |
| 58 | [None]. *) |
| 59 | val append : 'a option -> 'a option -> 'a option |
| 60 | |
| 61 | |
| 62 | (** {6 "Iterators"} *) |
| 63 | |
| 64 | (** [iter f x] executes [f y] if [x] equals [Some y]. It does nothing |
| 65 | otherwise. *) |
| 66 | val iter : ('a -> unit) -> 'a option -> unit |
| 67 | |
| 68 | exception Heterogeneous |
| 69 | |
| 70 | (** [iter2 f x y] executes [f z w] if [x] equals [Some z] and [y] equals |
| 71 | [Some w]. It does nothing if both [x] and [y] are [None]. |
| 72 | @raise Heterogeneous otherwise. *) |
| 73 | val iter2 : ('a -> 'b -> unit) -> 'a option -> 'b option -> unit |
| 74 | |
| 75 | (** [map f x] is [None] if [x] is [None] and [Some (f y)] if [x] is [Some y]. *) |
| 76 | val map : ('a -> 'b) -> 'a option -> 'b option |
| 77 | |
| 78 | (** [fold_left f a x] is [f a y] if [x] is [Some y], and [a] otherwise. *) |
| 79 | val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a option -> 'b |
| 80 | |
| 81 | (** [fold_left2 f a x y] is [f z w] if [x] is [Some z] and [y] is [Some w]. |
| 82 | It is [a] if both [x] and [y] are [None]. |
| 83 | @raise Heterogeneous otherwise. *) |
| 84 | val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b option -> 'c option -> 'a |
| 85 | |
| 86 | (** [fold_right f x a] is [f y a] if [x] is [Some y], and [a] otherwise. *) |
| 87 | val fold_right : ('a -> 'b -> 'b) -> 'a option -> 'b -> 'b |
| 88 | |
| 89 | (** [fold_left_map f a x] is [a, f y] if [x] is [Some y], and [a] otherwise. *) |
| 90 | val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b option -> 'a * 'c option |
| 91 | |
| 92 | (** Same as [fold_left_map] on the right *) |
| 93 | val fold_right_map : ('b -> 'a -> 'c * 'a) -> 'b option -> 'a -> 'c option * 'a |
| 94 | |
| 95 | (** [cata f e x] is [e] if [x] is [None] and [f a] if [x] is [Some a] *) |
| 96 | val cata : ('a -> 'b) -> 'b -> 'a option -> 'b |
| 97 | |
| 98 | (** {6 More Specific Operations} *) |
| 99 | |
| 100 | (** [default a x] is [y] if [x] is [Some y] and [a] otherwise. *) |
| 101 | val default : 'a -> 'a option -> 'a |
| 102 | |
| 103 | (** {6 Smart operations} *) |
| 104 | |
| 105 | module Smart : |
| 106 | sig |
| 107 | |
| 108 | (** [Smart.map f x] does the same as [map f x] except that it tries to share |
| 109 | some memory. *) |
| 110 | val map : ('a -> 'a) -> 'a option -> 'a option |
| 111 | |
| 112 | end |
| 113 | |
| 114 | (** {6 Operations with Lists} *) |
| 115 | |
| 116 | module List : sig |
| 117 | (** [List.cons x l] equals [y::l] if [x] is [Some y] and [l] otherwise. *) |
| 118 | val cons : 'a option -> 'a list -> 'a list |
| 119 | |
| 120 | (** [List.flatten l] is the list of all the [y]s such that [l] contains |
| 121 | [Some y] (in the same order). *) |
| 122 | val flatten : 'a option list -> 'a list |
| 123 | |
| 124 | (** [List.map f [a1;...;an]] is the list [Some [b1;...;bn]] if |
| 125 | for all i, there is a [bi] such that [f ai] is [Some bi]; it is |
| 126 | [None] if, for at least one i, [f ai] is [None]. *) |
| 127 | val map : ('a -> 'b option) -> 'a list -> 'b list option |
| 128 | |
| 129 | end |