Debian Codegraph @

clib/monad.ml

raw symbols SCIP
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
12 (** Combinators on monadic computations. *)
13
14
15 (** A definition of monads, each of the combinators is used in the
16 [Make] functor. *)
17 module type Def = sig
18
19 type +'a t
20 val return : 'a -> 'a t
21 val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
22 val (>>) : unit t -> 'a t -> 'a t
23 val map : ('a -> 'b) -> 'a t -> 'b t
24
25 (** The monadic laws must hold:
26 - [(x>>=f)>>=g] = [x>>=fun x' -> (f x'>>=g)]
27 - [return a >>= f] = [f a]
28 - [x>>=return] = [x]
29
30 As well as the following identities:
31 - [x >> y] = [x >>= fun () -> y]
32 - [map f x] = [x >>= fun x' -> f x'] *)
33
34 end
35
36 module type ListS = sig
37
38 type 'a t
39
40 (** [List.map f l] maps [f] on the elements of [l] in left to right
41 order. *)
42 val map : ('a -> 'b t) -> 'a list -> 'b list t
43
44 (** [List.map f l] maps [f] on the elements of [l] in right to left
45 order. *)
46 val map_right : ('a -> 'b t) -> 'a list -> 'b list t
47
48 (** Like the regular [List.fold_right]. The monadic effects are
49 threaded right to left.
50
51 Note: many monads behave poorly with right-to-left order. For
52 instance a failure monad would still have to traverse the
53 whole list in order to fail and failure needs to be propagated
54 through the rest of the list in binds which are now
55 spurious. It is also the worst case for substitution monads
56 (aka free monads), exposing the quadratic behaviour.*)
57 val fold_right : ('a -> 'b -> 'b t) -> 'a list -> 'b -> 'b t
58
59 (** Like the regular [List.fold_left]. The monadic effects are
60 threaded left to right. It is tail-recursive if the [(>>=)]
61 operator calls its second argument in a tail position. *)
62 val fold_left : ('a -> 'b -> 'a t) -> 'a -> 'b list -> 'a t
63
64 (** Like the regular [List.iter]. The monadic effects are threaded
65 left to right. It is tail-recurisve if the [>>] operator calls
66 its second argument in a tail position. *)
67 val iter : ('a -> unit t) -> 'a list -> unit t
68
69 (** Like the regular {!CList.map_filter}. The monadic effects are threaded left*)
70 val map_filter : ('a -> 'b option t) -> 'a list -> 'b list t
71
72
73 (** {6 Two-list iterators} *)
74
75 (** [fold_left2 r f s l1 l2] behaves like {!fold_left} but acts
76 simultaneously on two lists. Runs [r] (presumably an
77 exception-raising computation) if both lists do not have the
78 same length. *)
79 val fold_left2 : 'a t ->
80 ('a -> 'b -> 'c -> 'a t) -> 'a -> 'b list -> 'c list -> 'a t
81
82 end
83
84 module type S = sig
85
86 include Def
87
88 (** List combinators *)
89 module List : ListS with type 'a t := 'a t
90
91 end
92
93
94 module Make (M:Def) : S with type +'a t = 'a M.t = struct
95
96 include M
97
98 module List = struct
99
100 (* The combinators are loop-unrolled to spare a some monadic binds
101 (it is a common optimisation to treat the last of a list of
102 bind specially) and hopefully gain some efficiency using fewer
103 jump. *)
104
105 let rec map f = function
106 | [] -> return []
107 | [a] ->
108 M.map (fun a' -> [a']) (f a)
109 | a::b::l ->
110 f a >>= fun a' ->
111 f b >>= fun b' ->
112 M.map (fun l' -> a'::b'::l') (map f l)
113
114 let rec map_right f = function
115 | [] -> return []
116 | [a] ->
117 M.map (fun a' -> [a']) (f a)
118 | a::b::l ->
119 map_right f l >>= fun l' ->
120 f b >>= fun b' ->
121 M.map (fun a' -> a'::b'::l') (f a)
122
123 let rec fold_right f l x =
124 match l with
125 | [] -> return x
126 | [a] -> f a x
127 | a::b::l ->
128 fold_right f l x >>= fun acc ->
129 f b acc >>= fun acc ->
130 f a acc
131
132 let rec fold_left f x = function
133 | [] -> return x
134 | [a] -> f x a
135 | a::b::l ->
136 f x a >>= fun x' ->
137 f x' b >>= fun x'' ->
138 fold_left f x'' l
139
140 let rec iter f = function
141 | [] -> return ()
142 | [a] -> f a
143 | a::b::l -> f a >> f b >> iter f l
144
145
146 let rec map_filter f = function
147 | [] -> return []
148 | a::l ->
149 f a >>= function
150 | None -> map_filter f l
151 | Some b ->
152 map_filter f l >>= fun filtered ->
153 return (b::filtered)
154
155 let rec fold_left2 r f x l1 l2 =
156 match l1,l2 with
157 | [] , [] -> return x
158 | [a] , [b] -> f x a b
159 | a1::a2::l1 , b1::b2::l2 ->
160 f x a1 b1 >>= fun x' ->
161 f x' a2 b2 >>= fun x'' ->
162 fold_left2 r f x'' l1 l2
163 | _ , _ -> r
164
165 end
166
167 end
168
169
170