-
Notifications
You must be signed in to change notification settings - Fork 0
/
Introduction.ml
302 lines (255 loc) · 7.95 KB
/
Introduction.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(* DO NOT EDIT! THIS IS AUTO EXTRACTED FROM THE README. *)
open Rea
module Fib = struct
let rec eager n =
if n <= 1 then
pure n
else
lift'2 ( + ) (eager (n - 2)) (eager (n - 1))
let _ =
(eager
: int ->
(< map' : 'e 'a 'b. ('b -> 'a) -> ('R, 'e, 'b, 'D) er -> ('R, 'e, 'a) s
; pair' :
'e 'a 'b.
('R, 'e, 'a, 'D) er -> ('R, 'e, 'b, 'D) er -> ('R, 'e, 'a * 'b) s
; pure' : 'e 'a. 'a -> ('R, 'e, 'a) s
; .. >
as
'D) ->
('R, 'e, int) s)
let _ =
(eager
: int ->
(< ('R, 'D) map' ; ('R, 'D) pair' ; ('R, 'D) pure' ; .. > as 'D) ->
('R, 'e, int) s)
let _ = (eager : int -> (('R, 'D) #applicative' as 'D) -> ('R, 'e, int) s)
let _ = (eager : int -> ('R, 'e, int, (('R, 'D) #applicative' as 'D)) er)
let () = assert (55 = Identity.of_rea (run Identity.monad (eager 10)))
let () = assert (`Ok 55 = Tailrec.run Tailrec.sync (eager 10))
let rec inert n =
eta'0 @@ fun () ->
if n <= 1 then
pure n
else
lift'2 ( + ) (inert (n - 2)) (inert (n - 1))
let () = assert (55 = Identity.of_rea (run Identity.monad (inert 10)))
let () = assert (`Ok 55 = Tailrec.run Tailrec.sync (inert 10))
end
module Num = struct
type 't t =
[`Num of int | `Uop of [`Neg] * 't | `Bop of [`Add | `Mul] * 't * 't]
let uop = function
| `Neg -> ( ~-)
let bop = function
| `Add -> ( + )
| `Mul -> ( * )
let eval eval =
eta'1 @@ function
| `Num _ as v -> pure v
| `Uop (op, x) -> (
eval x >>= function
| `Num x -> pure @@ `Num (uop op x)
| x -> fail @@ `Error_attempt_to_apply_uop (op, x))
| `Bop (op, l, r) -> (
eval l <*> eval r >>= function
| `Num l, `Num r -> pure @@ `Num (bop op l r)
| l, r -> fail @@ `Error_attempt_to_apply_bop (op, l, r))
let _ =
(eval
: ('t ->
( 'R,
([> `Error_attempt_to_apply_bop of
([< `Add | `Mul] as 'bop) * ([> `Num of int] as 'v) * 'v
| `Error_attempt_to_apply_uop of ([< `Neg] as 'uop) * 'v ]
as
'e),
'v,
(< ('R, 'D) sync' ; .. > as 'D) )
er) ->
[< 't t] ->
('R, 'e, [> `Num of int], 'D) er)
end
module Lam = struct
module Id = String
type 't t = [`Lam of Id.t * 't | `App of 't * 't | `Var of Id.t]
module Bindings = Map.Make (Id)
class ['v] bindings :
object
method bindings : 'v Bindings.t Prop.t
end =
object
val mutable v : 'v Bindings.t = Bindings.empty
method bindings = Prop.make (fun () -> v) (fun x -> v <- x)
end
let bindings d = d#bindings
let eval eval =
eta'1 @@ function
| `Lam (i, e) ->
let+ bs = get bindings in
`Fun (bs, i, e)
| `App (f, x) -> (
eval f >>= function
| `Fun (bs, i, e) ->
let* v = eval x in
setting bindings (Bindings.add i v bs) (eval e)
| f -> fail @@ `Error_attempt_to_apply f)
| `Var i -> (
get_as bindings (Bindings.find_opt i) >>= function
| None -> fail @@ `Error_unbound_var i
| Some v -> pure v)
let _ =
(eval
: ('t ->
( 'R,
([> `Error_attempt_to_apply of
([> `Fun of 'v Bindings.t * Id.t * 't] as 'v)
| `Error_unbound_var of Id.t ]
as
'e),
'v,
(< ('R, 'D) sync' ; 'v bindings ; .. > as 'D) )
er) ->
[< 't t] ->
('R, 'e, 'v, 'D) er)
end
module Full = struct
let rec eval = function
| #Num.t as e -> Num.eval eval e
| #Lam.t as e -> Lam.eval eval e
let _ =
(eval
: ([< 't Num.t | 't Lam.t] as 't) ->
( 'R,
[> `Error_attempt_to_apply of
([> `Fun of 'v Lam.Bindings.t * Lam.Id.t * 't | `Num of int] as 'v)
| `Error_attempt_to_apply_bop of [`Add | `Mul] * 'v * 'v
| `Error_attempt_to_apply_uop of [`Neg] * 'v
| `Error_unbound_var of Lam.Id.t ],
'v,
(< ('R, 'D) sync' ; 'v Lam.bindings ; .. > as 'D) )
er)
let () =
assert (
Error (`Error_unbound_var "y")
= StdRea.Result.of_rea
(run
(object
inherit [_] StdRea.Result.monad_errors
inherit [_] Lam.bindings
end)
(eval (`App (`Lam ("x", `Bop (`Add, `Num 2, `Var "y")), `Num 1)))))
let () =
assert (
`Ok (`Num 3)
= Tailrec.run
(object
inherit [_] Tailrec.sync
inherit [_] Lam.bindings
end)
(eval (`App (`Lam ("x", `Bop (`Add, `Num 2, `Var "x")), `Num 1))))
let () =
let result = ref @@ Ok (`Num 0) in
Tailrec.spawn
(object
inherit [_] Tailrec.async
inherit [_] Lam.bindings
end)
(eval (`App (`Lam ("x", `Bop (`Add, `Num 2, `Var "x")), `Num 1))
|> tryin
(fun e -> pure (result := Error e))
(fun v -> pure (result := Ok v)));
assert (!result = Ok (`Num 3))
end
module Scoped = struct
let eval e =
Full.eval e
|> mapping_env @@ fun o ->
object
inherit [_, _, _] sync'of o
inherit [_] Lam.bindings
end
let _ =
(eval
: ([< 't Num.t | 't Lam.t] as 't) ->
( 'R,
[> `Error_attempt_to_apply of
([> `Fun of 'v Lam.Bindings.t * Lam.Id.t * 't | `Num of int] as 'v)
| `Error_attempt_to_apply_bop of [`Add | `Mul] * 'v * 'v
| `Error_attempt_to_apply_uop of [`Neg] * 'v
| `Error_unbound_var of Lam.Id.t ],
'v,
(('R, 'D) #sync' as 'D) )
er)
let () =
assert (
`Ok (`Num 42)
= Tailrec.run Tailrec.sync
(eval (`App (`Lam ("x", `Bop (`Add, `Num 2, `Var "x")), `Num 40))))
end
module Cont : sig
type 'a t
val return : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
val callcc : (('a -> 'b t) -> 'a t) -> 'a t
val run : 'a t -> 'a
end = struct
type 'a t = ('a -> unit) -> unit
let return x k = k x
let bind xK xyK k = xK (fun x -> (xyK x) k)
let callcc kxK k = kxK (fun x _ -> k x) k
let run xK =
let result = ref None in
xK (fun x -> result := Some x);
Option.get !result
end
class virtual ['R, 'D] callcc' =
object
method virtual callcc'
: 'e 'f 'a 'b.
(('a -> ('R, 'f, 'b, 'D) er) -> ('R, 'e, 'a, 'D) er) -> ('R, 'e, 'a) s
end
let callcc f (d : (_, _) #callcc') = d#callcc' f
module ContRea = struct
type r
external to_rea : 'a Cont.t -> (r, 'e, 'a) s = "%identity"
external of_rea : (r, 'e, 'a) s -> 'a Cont.t = "%identity"
class ['D] monad_callcc =
object (d : 'D)
inherit [r, 'D] monad'd
method pure' x = to_rea (Cont.return x)
method bind' x f =
to_rea (Cont.bind (of_rea (x d)) (fun x -> of_rea (f x d)))
inherit [r, 'D] callcc'
method callcc' f =
to_rea (Cont.callcc (fun k -> of_rea (f (fun x _ -> to_rea (k x)) d)))
end
let () =
assert (55 = Cont.run (of_rea (run (new monad_callcc) (Fib.inert 10))))
let () =
assert (
101 = Cont.run (of_rea (run (new monad_callcc) (callcc (fun k -> k 101)))))
end
module Answer = struct
let map_er' nE o1E o2E iE eE =
eta'1 @@ function
| `Num x -> map_er'1 nE x >>- fun x -> `Num x
| `Uop x -> map_er'2 o1E eE x >>- fun x -> `Uop x
| `Bop x -> map_er'3 o2E eE eE x >>- fun x -> `Bop x
| `Lam x -> map_er'2 iE eE x >>- fun x -> `Lam x
| `App x -> map_er'2 eE eE x >>- fun x -> `App x
| `Var x -> map_er'1 iE x >>- fun x -> `Var x
let map_er eE = map_er' pure pure pure pure eE
type 't t = ['t Num.t | 't Lam.t]
let _ =
(map_er
: ('s -> ('R, 'e, 't, (('R, 'D) #applicative' as 'D)) er) ->
[< 's t] ->
('R, 'e, [> 't t], 'D) er)
let rec is_free i' = function
| `Var i -> i = i'
| `Lam (i, _) when i = i' -> false
| e -> Traverse.to_exists map_er (is_free i') e
let () = assert (is_free "y" (`App (`Lam ("x", `Var "x"), `Var "y")))
let () = assert (not (is_free "x" (`App (`Lam ("x", `Var "x"), `Var "y"))))
end