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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
type 'a t = 'a option
let pure x = Some x
module Foldable = Preface_make.Foldable.Via_fold_right (struct
type nonrec 'a t = 'a t
let fold_right f x acc = (match x with None -> acc | Some v -> f v acc)
end)
module Functor = Preface_make.Functor.Via_map (struct
type nonrec 'a t = 'a t
let map f = function Some x -> Some (f x) | None -> None
end)
module Alternative = Preface_make.Alternative.Via_apply (struct
type nonrec 'a t = 'a t
let pure = pure
let apply fa xa =
(match (fa, xa) with (Some f, Some x) -> Some (f x) | _ -> None)
;;
let neutral = None
let combine l r = (match (l, r) with (None, x) -> x | (x, _) -> x)
end)
let traverse_aux pure map f = function
| None -> pure None
| Some x -> map Stdlib.Option.some (f x)
;;
module Applicative_traversable (A : Preface_specs.APPLICATIVE) =
Preface_make.Traversable.Over_applicative
(A)
(struct
type 'a t = 'a A.t
type 'a iter = 'a option
let traverse f x = traverse_aux A.pure A.map f x
end)
module Applicative =
Preface_make.Traversable.Join_with_applicative
(Alternative)
(Applicative_traversable)
module Monad_internal = Preface_make.Monad.Via_bind (struct
type nonrec 'a t = 'a t
let return x = Some x
let bind f = function Some x -> f x | None -> None
end)
module Monad_traversable (M : Preface_specs.MONAD) =
Preface_make.Traversable.Over_monad
(M)
(struct
type 'a t = 'a M.t
type 'a iter = 'a option
let traverse f x = traverse_aux M.return M.map f x
end)
module Monad =
Preface_make.Traversable.Join_with_monad (Monad_internal) (Monad_traversable)
module Monad_plus =
Preface_make.Monad_plus.Over_monad_and_alternative (Monad) (Alternative)
module Monoid (M : Preface_specs.SEMIGROUP) =
Preface_make.Monoid.Via_combine_and_neutral (struct
type nonrec t = M.t t
let neutral = None
let combine x y =
match (x, y) with
| (None, result) | (result, None) -> result
| (Some a, Some b) -> Some (M.combine a b)
;;
end)
let equal f left right =
match (left, right) with
| (None, None) -> true
| (Some x, Some y) -> f x y
| _ -> false
;;
let pp pp' formater = function
| None -> Format.fprintf formater "None"
| Some x -> Format.fprintf formater "Some (%a)" pp' x
;;