| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
end |
| 35 |
|
| 36 |
module type ListS = sig |
| 37 |
|
| 38 |
type 'a t |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
val map : ('a -> 'b t) -> 'a list -> 'b list t |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
val map_right : ('a -> 'b t) -> 'a list -> 'b list t |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
val fold_right : ('a -> 'b -> 'b t) -> 'a list -> 'b -> 'b t |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
val fold_left : ('a -> 'b -> 'a t) -> 'a -> 'b list -> 'a t |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
val iter : ('a -> unit t) -> 'a list -> unit t |
| 68 |
|
| 69 |
|
| 70 |
val map_filter : ('a -> 'b option t) -> 'a list -> 'b list t |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|