| 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 | |
| 37 | (** List combinators *) |
| 38 | module type ListS = sig |
| 39 | |
| 40 | type 'a t |
| 41 | |
| 42 | (** [List.map f l] maps [f] on the elements of [l] in left to right |
| 43 | order. *) |
| 44 | val map : ('a -> 'b t) -> 'a list -> 'b list t |
| 45 | |
| 46 | (** [List.map f l] maps [f] on the elements of [l] in right to left |
| 47 | order. *) |
| 48 | val map_right : ('a -> 'b t) -> 'a list -> 'b list t |
| 49 | |
| 50 | (** Like the regular [List.fold_right]. The monadic effects are |
| 51 | threaded right to left. |
| 52 | |
| 53 | Note: many monads behave poorly with right-to-left order. For |
| 54 | instance a failure monad would still have to traverse the |
| 55 | whole list in order to fail and failure needs to be propagated |
| 56 | through the rest of the list in binds which are now |
| 57 | spurious. It is also the worst case for substitution monads |
| 58 | (aka free monads), exposing the quadratic behaviour.*) |
| 59 | val fold_right : ('a -> 'b -> 'b t) -> 'a list -> 'b -> 'b t |
| 60 | |
| 61 | (** Like the regular [List.fold_left]. The monadic effects are |
| 62 | threaded left to right. It is tail-recursive if the [(>>=)] |
| 63 | operator calls its second argument in a tail position. *) |
| 64 | val fold_left : ('a -> 'b -> 'a t) -> 'a -> 'b list -> 'a t |
| 65 | |
| 66 | (** Like the regular [List.iter]. The monadic effects are threaded |
| 67 | left to right. It is tail-recurisve if the [>>] operator calls |
| 68 | its second argument in a tail position. *) |
| 69 | val iter : ('a -> unit t) -> 'a list -> unit t |
| 70 | |
| 71 | (** Like the regular {!CList.map_filter}. The monadic effects are |
| 72 | threaded left to right. *) |
| 73 | val map_filter : ('a -> 'b option t) -> 'a list -> 'b list t |
| 74 | |
| 75 | |
| 76 | (** {6 Two-list iterators} *) |
| 77 | |
| 78 | (** [fold_left2 r f s l1 l2] behaves like {!fold_left} but acts |
| 79 | simultaneously on two lists. Runs [r] (presumably an |
| 80 | exception-raising computation) if both lists do not have the |
| 81 | same length. *) |
| 82 | val fold_left2 : 'a t -> |
| 83 | ('a -> 'b -> 'c -> 'a t) -> 'a -> 'b list -> 'c list -> 'a t |
| 84 | |
| 85 | end |
| 86 | |
| 87 | module type S = sig |
| 88 | |
| 89 | include Def |
| 90 | |
| 91 | module List : ListS with type 'a t := 'a t |
| 92 | |
| 93 | end |
| 94 | |
| 95 | (** Expands the monadic definition to extra combinators. *) |
| 96 | module Make (M:Def) : S with type +'a t = 'a M.t |