| 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 | let has_some = function |
| 21 | | None -> false |
| 22 | | _ -> true |
| 23 | |
| 24 | let is_empty = function |
| 25 | | None -> true |
| 26 | | Some _ -> false |
| 27 | |
| 28 | (** Lifting equality onto option types. *) |
| 29 | let equal f x y = match x, y with |
| 30 | | None, None -> true |
| 31 | | Some x, Some y -> f x y |
| 32 | | _, _ -> false |
| 33 | |
| 34 | let compare f x y = match x, y with |
| 35 | | None, None -> 0 |
| 36 | | Some x, Some y -> f x y |
| 37 | | None, Some _ -> -1 |
| 38 | | Some _, None -> 1 |
| 39 | |
| 40 | let hash f = function |
| 41 | | None -> 0 |
| 42 | | Some x -> f x |
| 43 | |
| 44 | (** [get x] returns [y] where [x] is [Some y] and asserts false otherwise. *) |
| 45 | let get = function |
| 46 | | Some y -> y |
| 47 | | None -> assert false |
| 48 | |
| 49 | (** [make x] returns [Some x]. *) |
| 50 | let make x = Some x |
| 51 | |
| 52 | (** [bind x f] is [f y] if [x] is [Some y] and [None] otherwise *) |
| 53 | let bind x f = match x with Some y -> f y | None -> None |
| 54 | |
| 55 | let filter f x = bind x (fun v -> if f v then x else None) |
| 56 | |
| 57 | (** [init b x] returns [Some x] if [b] is [true] and [None] otherwise. *) |
| 58 | let init b x = |
| 59 | if b then |
| 60 | Some x |
| 61 | else |
| 62 | None |
| 63 | |
| 64 | (** [flatten x] is [Some y] if [x] is [Some (Some y)] and [None] otherwise. *) |
| 65 | let flatten = function |
| 66 | | Some (Some y) -> Some y |
| 67 | | _ -> None |
| 68 | |
| 69 | (** [append x y] is the first element of the concatenation of [x] and |
| 70 | [y] seen as lists. *) |
| 71 | let append o1 o2 = |
| 72 | match o1 with |
| 73 | | Some _ -> o1 |
| 74 | | None -> o2 |
| 75 | |
| 76 | |
| 77 | (** {6 "Iterators"} ***) |
| 78 | |
| 79 | (** [iter f x] executes [f y] if [x] equals [Some y]. It does nothing |
| 80 | otherwise. *) |
| 81 | let iter f = function |
| 82 | | Some y -> f y |
| 83 | | _ -> () |
| 84 | |
| 85 | |
| 86 | exception Heterogeneous |
| 87 | |
| 88 | (** [iter2 f x y] executes [f z w] if [x] equals [Some z] and [y] equals |
| 89 | [Some w]. It does nothing if both [x] and [y] are [None]. And raises |
| 90 | [Heterogeneous] otherwise. *) |
| 91 | let iter2 f x y = |
| 92 | match x,y with |
| 93 | | Some z, Some w -> f z w |
| 94 | | None,None -> () |
| 95 | | _,_ -> raise Heterogeneous |
| 96 | |
| 97 | (** [map f x] is [None] if [x] is [None] and [Some (f y)] if [x] is [Some y]. *) |
| 98 | let map f = function |
| 99 | | Some y -> Some (f y) |
| 100 | | _ -> None |
| 101 | |
| 102 | (** [fold_left f a x] is [f a y] if [x] is [Some y], and [a] otherwise. *) |
| 103 | let fold_left f a = function |
| 104 | | Some y -> f a y |
| 105 | | _ -> a |
| 106 | |
| 107 | (** [fold_left2 f a x y] is [f z w] if [x] is [Some z] and [y] is [Some w]. |
| 108 | It is [a] if both [x] and [y] are [None]. Otherwise it raises |
| 109 | [Heterogeneous]. *) |
| 110 | let fold_left2 f a x y = |
| 111 | match x,y with |
| 112 | | Some x, Some y -> f a x y |
| 113 | | None, None -> a |
| 114 | | _ -> raise Heterogeneous |
| 115 | |
| 116 | (** [fold_right f x a] is [f y a] if [x] is [Some y], and [a] otherwise. *) |
| 117 | let fold_right f x a = |
| 118 | match x with |
| 119 | | Some y -> f y a |
| 120 | | _ -> a |
| 121 | |
| 122 | (** [fold_left_map f a x] is [a, f y] if [x] is [Some y], and [a] otherwise. *) |
| 123 | let fold_left_map f a x = |
| 124 | match x with |
| 125 | | Some y -> let a, z = f a y in a, Some z |
| 126 | | _ -> a, None |
| 127 | |
| 128 | let fold_right_map f x a = |
| 129 | match x with |
| 130 | | Some y -> let z, a = f y a in Some z, a |
| 131 | | _ -> None, a |
| 132 | |
| 133 | (** [cata f a x] is [a] if [x] is [None] and [f y] if [x] is [Some y]. *) |
| 134 | let cata f a = function |
| 135 | | Some c -> f c |
| 136 | | None -> a |
| 137 | |
| 138 | |
| 139 | (** {6 More Specific operations} ***) |
| 140 | |
| 141 | (** [default a x] is [y] if [x] is [Some y] and [a] otherwise. *) |
| 142 | let default a = function |
| 143 | | Some y -> y |
| 144 | | _ -> a |
| 145 | |
| 146 | (** {6 Smart operations} *) |
| 147 | |
| 148 | module Smart = |
| 149 | struct |
| 150 | |
| 151 | (** [Smart.map f x] does the same as [map f x] except that it tries to share |
| 152 | some memory. *) |
| 153 | let map f = function |
| 154 | | Some y as x -> let y' = f y in if y' == y then x else Some y' |
| 155 | | _ -> None |
| 156 | |
| 157 | end |
| 158 | |
| 159 | (** {6 Operations with Lists} *) |
| 160 | |
| 161 | module List = |
| 162 | struct |
| 163 | (** [List.cons x l] equals [y::l] if [x] is [Some y] and [l] otherwise. *) |
| 164 | let cons x l = |
| 165 | match x with |
| 166 | | Some y -> y::l |
| 167 | | _ -> l |
| 168 | |
| 169 | (** [List.flatten l] is the list of all the [y]s such that [l] contains |
| 170 | [Some y] (in the same order). *) |
| 171 | let rec flatten = function |
| 172 | | x::l -> cons x (flatten l) |
| 173 | | [] -> [] |
| 174 | |
| 175 | let map f l = |
| 176 | let rec aux f l = match l with |
| 177 | | [] -> [] |
| 178 | | x :: l -> |
| 179 | match f x with |
| 180 | | None -> raise_notrace Exit |
| 181 | | Some y -> y :: aux f l |
| 182 | in |
| 183 | try Some (aux f l) with Exit -> None |
| 184 | |
| 185 | end |