-
Notifications
You must be signed in to change notification settings - Fork 0
/
monade_pack.ml
executable file
·59 lines (52 loc) · 1.3 KB
/
monade_pack.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module OPTION : Monade.Interface with type 'a m = 'a option = struct
type 'a m = 'a option
let return x = Some x
let (>>=) m f =
match m with
Some x -> f x
| None -> None
end
module Option =
struct
include Monade.Make(OPTION)
let safe f x = try Some (f x) with _ -> None
end
module LISTE : (Monade.Interface with type 'a m = 'a list) = struct
type 'a m = 'a list
let return x = [x]
let (>>=) m f =
match m with
[] -> []
| liste -> List.concat (List.map f liste)
end
module Liste = Monade.Make(LISTE)
module WRITER_MAKE (M : Monoide.Interface) =
struct
type 'a m = ('a * M.m)
let return x = (x, M.neutre)
let (>>=) (x, log) f =
let (x2, log2) = f x
in (x2, M.combine log log2)
end
module Writer_make (M : Monoide.Interface) = struct
include Monade.Make(WRITER_MAKE(M))
let tell msg = ((),msg)
end
module State =
struct
module type TYPE = sig
type state
end
module MAKE (M : TYPE) = struct
type 'a m = M.state -> 'a * M.state
let return x = fun s -> (x,s)
let (>>=) h f = fun s -> let (value, new_state) = h s
in let g = f value
in g new_state
end
module Make (M : TYPE) = struct
include Monade.Make(MAKE(M))
let get_val (v,_) = v
let get_state (_,s) = s
end
end