/
monads.ml
285 lines (252 loc) · 8.98 KB
/
monads.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
(* Use the compiler
* echo monads.ml | entr -c bash -c 'ocamlc ./monads.ml && ./a.out'
*)
(* Or use the top-level interactive mode
* echo monads.ml | entr -c ocaml /_
*)
let add_a a b c =
match a with
| None -> None
| Some x -> (
match b with
| None -> None
| Some y -> (
match c with
| None -> None
| Some z -> Some (x + y + z)))
;;
let add_b a b c =
Option.bind a (fun x ->
Option.bind b (fun y ->
Option.bind c (fun z ->
Some (x + y + z)
)
)
)
[@@ocamlformat "disable"]
let add_c a b c =
match (a, b, c) with
| Some x, Some y, Some z -> Some (x + y + z)
| _ -> None
;;
let product o1 o2 =
match (o1, o2) with
| Some x, Some y -> Some (x, y)
| _ -> None
;;
let ( let* ) = Option.bind
let ( let+ ) o f = Option.map f o
let ( and+ ) = product
let add_d a b c =
let* x = a in
let* y = b in
let* z = c in
Some (x + y + z)
;;
let ( >>= ) opt f =
match opt with
| Some x -> f x
| None -> None
;;
let add_e a b c =
a >>= fun x ->
b >>= fun y ->
c >>= fun z ->
Some(x + y +z )
[@@ocamlformat "disable"]
(*
See the Elm blog post at: https://thoughtbot.com/blog/running-out-of-maps
Rather than using `and_map`, I'll use the function name `apply` (it seems fit?)
*)
let apply opt_a opt_b =
match (opt_a, opt_b) with
| Some value, Some fn -> Some (fn value)
| _, _ -> None
;;
let ( <*> ) a b = apply b a
let pure x = Some x
let add_f a b c =
Some (fun x y z -> x + y + z)
|> apply a
|> apply b
|> apply c
[@@ocamlformat "disable"]
let apply res_a res_b =
match (res_a, res_b) with
| Ok value, Ok fn -> Ok (fn value)
| Error err, _ -> Error err
| _, Error err -> Error err
;;
let add_g a b c =
Ok (fun x y z -> x + y + z)
|> apply a
|> apply b
|> apply c
[@@ocamlformat "disable"]
(* See again the Elm examples *)
let apply' res_a res_b =
Result.bind res_b (fun partial -> Result.map partial res_a)
;;
let add_h a b c =
Ok (fun x y z -> x + y + z)
|> apply' a
|> apply' b
|> apply' c
[@@ocamlformat "disable"]
let and_then a b = Result.bind b a
let add_i a b c =
Ok (fun x y z -> x + y + z)
|> and_then (fun partial -> Result.map partial a)
|> and_then (fun partial -> Result.map partial b)
|> and_then (fun partial -> Result.map partial c)
[@@ocamlformat "disable"]
(* https://jobjo.github.io/2019/04/24/ocaml-has-some-new-shiny-syntax.html *)
let add_j a b c =
let+ x = a
and+ y = b
and+ z = c in
x + y + z
[@@ocamlformat "disable"]
(* https://jobjo.github.io/2019/04/24/ocaml-has-some-new-shiny-syntax.html *)
let add_k a b c =
pure (fun x y z -> x + y + z)
<*> a
<*> b
<*> c
[@@ocamlformat "disable"]
(* MAIN *)
let printRes header fn =
match fn with
| None -> Printf.printf "%s => None\n" header
| Some n -> Printf.printf "%s => Some %d\n" header n
;;
let printRes2 header fn =
match fn with
| Error s -> Printf.printf "%s => Error \"%s\"\n" header s
| Ok n -> Printf.printf "%s => Ok %d\n" header n
;;
let _ =
(* add_a: verbose! *)
printRes "add_a (Some 1) (Some 2) (Some 3)"
@@ add_a (Some 1) (Some 2) (Some 3)
; printRes "add_a None (Some 2) (Some 3) " @@ add_a None (Some 2) (Some 3)
; printRes "add_a (Some 1) None (Some 3) " @@ add_a (Some 1) None (Some 3)
; printRes "add_a (Some 1) None None " @@ add_a (Some 1) None (Some 3)
; print_endline ""
; (* add_b: fn chain *)
printRes "add_b (Some 1) (Some 2) (Some 3)"
@@ add_b (Some 1) (Some 2) (Some 3)
; printRes "add_b None (Some 2) (Some 3) " @@ add_b None (Some 2) (Some 3)
; printRes "add_b (Some 1) None (Some 3) " @@ add_b (Some 1) None (Some 3)
; printRes "add_b (Some 1) None None " @@ add_b (Some 1) None (Some 3)
; print_endline ""
; (* add_c: use pattern matching *)
printRes "add_c (Some 1) (Some 2) (Some 3)"
@@ add_c (Some 1) (Some 2) (Some 3)
; printRes "add_c None (Some 2) (Some 3) " @@ add_c None (Some 2) (Some 3)
; printRes "add_c (Some 1) None (Some 3) " @@ add_c (Some 1) None (Some 3)
; printRes "add_c (Some 1) None None " @@ add_c (Some 1) None (Some 3)
; print_endline ""
; (* add_d: use let bind *)
printRes "add_d (Some 1) (Some 2) (Some 3)"
@@ add_d (Some 1) (Some 2) (Some 3)
; printRes "add_d None (Some 2) (Some 3) " @@ add_d None (Some 2) (Some 3)
; printRes "add_d (Some 1) None (Some 3) " @@ add_d (Some 1) None (Some 3)
; printRes "add_d (Some 1) None None " @@ add_d (Some 1) None (Some 3)
; print_endline ""
; (* add_e: use bind operator, Haskell style *)
printRes "add_e (Some 1) (Some 2) (Some 3)"
@@ add_e (Some 1) (Some 2) (Some 3)
; printRes "add_e None (Some 2) (Some 3) " @@ add_e None (Some 2) (Some 3)
; printRes "add_e (Some 1) None (Some 3) " @@ add_e (Some 1) None (Some 3)
; printRes "add_e (Some 1) None None " @@ add_e (Some 1) None (Some 3)
; print_endline ""
; (* add_f: Elm style *)
printRes "add_f (Some 1) (Some 2) (Some 3)"
@@ add_f (Some 1) (Some 2) (Some 3)
; printRes "add_f None (Some 2) (Some 3) " @@ add_f None (Some 2) (Some 3)
; printRes "add_f (Some 1) None (Some 3) " @@ add_f (Some 1) None (Some 3)
; printRes "add_f (Some 1) None None " @@ add_f (Some 1) None (Some 3)
; print_endline ""
; (* add_g: Elm style. NOTE: as with Elm, the arguments are processed in reverse. *)
printRes2 "add_g (Ok 1) (Ok 2) (Ok 3) " @@ add_g (Ok 1) (Ok 2) (Ok 3)
; printRes2 "add_g (Error 'Oops1') (Ok 2) (Ok 3) " @@ add_g (Error "Oops1") (Ok 2) (Ok 3)
; printRes2 "add_g (Ok 1) (Error 'Oops2') (Ok 3) " @@ add_g (Ok 1) (Error "Oops2") (Ok 3)
; printRes2 "add_g (Ok 1) (Error 'Oops2') (Error 'Oops3')" @@ add_g (Ok 1) (Error "Oops2") (Error "Oops3")
; print_endline ""
; (* add_h: Elm style again. This fixes the arguments being processed in reverse order. *)
printRes2 "add_h (Ok 1) (Ok 2) (Ok 3) " @@ add_h (Ok 1) (Ok 2) (Ok 3)
; printRes2 "add_h (Error 'Oops1') (Ok 2) (Ok 3) " @@ add_h (Error "Oops1") (Ok 2) (Ok 3)
; printRes2 "add_h (Ok 1) (Error 'Oops2') (Ok 3) " @@ add_h (Ok 1) (Error "Oops2") (Ok 3)
; printRes2 "add_h (Ok 1) (Error 'Oops2') (Error 'Oops3')" @@ add_h (Ok 1) (Error "Oops2") (Error "Oops3")
; print_endline ""
(* add_h: Elm style again, but verbose mode. It processes the arguments being processed in the correct order.
* Note that I had to create a custom Result.bind (and_then) with a reversed order to make that work.
*)
; printRes2 "add_i (Ok 1) (Ok 2) (Ok 3) " @@ add_i (Ok 1) (Ok 2) (Ok 3)
; printRes2 "add_i (Error 'Oops1') (Ok 2) (Ok 3) " @@ add_i (Error "Oops1") (Ok 2) (Ok 3)
; printRes2 "add_i (Ok 1) (Error 'Oops2') (Ok 3) " @@ add_i (Ok 1) (Error "Oops2") (Ok 3)
; printRes2 "add_i (Ok 1) (Error 'Oops2') (Error 'Oops3')" @@ add_i (Ok 1) (Error "Oops2") (Error "Oops3")
; print_endline ""
; (* Applicative style *)
printRes "add_j (Some 1) (Some 2) (Some 3)" @@ add_j (Some 1) (Some 2) (Some 3)
; printRes "add_j None (Some 2) (Some 3) " @@ add_j None (Some 2) (Some 3)
; printRes "add_j (Some 1) None (Some 3) " @@ add_j (Some 1) None (Some 3)
; printRes "add_j (Some 1) None None " @@ add_j (Some 1) None (Some 3)
; print_endline ""
; (* Applicative style2 *)
printRes "add_k (Some 1) (Some 2) (Some 3)" @@ add_k (Some 1) (Some 2) (Some 3)
; printRes "add_k None (Some 2) (Some 3) " @@ add_k None (Some 2) (Some 3)
; printRes "add_k (Some 1) None (Some 3) " @@ add_k (Some 1) None (Some 3)
; printRes "add_k (Some 1) None None " @@ add_k (Some 1) None (Some 3)
; print_endline ""
[@@ocamlformat "disable"]
let succeed b =
if b then
Ok 1
else
Error "Oops!"
;;
let ( let* ) = Result.bind
let compute_ok_ok =
let* a = succeed true in
let* b = succeed true in
Ok (a + b)
;;
let compute_ok_err =
let* a = succeed true in
let* b = succeed false in
Ok (a + b)
;;
let print_my_result = function
| Error s -> s
| Ok n -> string_of_int n
;;
(** returns either Ok, otherwise the Error of the same type
* This is somewhat unusual I suppose, I just want to print the original error below.
* I could also use the `print_result` function above.
*)
let ok_or_err r =
match r with
| Ok v -> v
| Error e -> e
;;
let () =
()
; Printf.printf "a) Add with (Ok, Ok): %s\n%!"
(compute_ok_ok
|> Result.map (fun n -> string_of_int n)
|> Result.value ~default:"oops")
; Printf.printf "a) Add with (Ok, Err): %s\n%!"
(compute_ok_err
|> Result.map (fun n -> string_of_int n)
|> Result.value ~default:"oops")
; Printf.printf "b) Add with (Ok, Ok): %s\n%!"
(compute_ok_ok |> Result.map (fun n -> string_of_int n) |> ok_or_err)
; Printf.printf "b) Add with (Ok, Err): %s\n%!"
(compute_ok_err |> Result.map (fun n -> string_of_int n) |> ok_or_err)
; Printf.printf "c) Add with (Ok, Ok): %s\n%!"
(compute_ok_ok |> print_my_result)
; Printf.printf "c) Add with (Ok, Err): %s\n%!"
(compute_ok_err |> print_my_result)
;;