-
Notifications
You must be signed in to change notification settings - Fork 9
/
parameterised-handlers.ml
319 lines (268 loc) · 9.14 KB
/
parameterised-handlers.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
(*
Parameterised effect handlers for OCaml.
*)
module type EFF =
sig
type ('a, 'h) clause
type ('p, 'r) op = 'p -> 'r
type ('a, 'b) return_clause = 'a -> 'b
type ('a, 'b, 'h) handler = ('h -> ('b, 'h) clause list) * ('a, 'b) return_clause
type ('a, 'b) plain_handler = ('b, unit) clause list * ('a, 'b) return_clause
val new_op : unit -> ('p,'r) op
val (|->) : ('p,'r) op -> ('p -> ('h -> 'r -> 'a) -> 'a) -> ('a, 'h) clause
val plain : ('p,'r) op -> ('p -> ('r -> 'a) -> 'a) -> ('a, 'h) clause
val shallow : ('p,'r) op -> ('p -> ('r -> 'a) -> 'a) -> ('a, 'h) clause
val local : ('p,'r) op -> ('p -> 'r) -> ('a, 'h) clause
val escape : ('p,'r) op -> ('p -> 'a) -> ('a, 'h) clause
val handle : (unit -> 'a) -> ('a, 'b, 'h) handler -> 'h -> 'b
val handle_plain : (unit -> 'a) -> ('a, 'b) plain_handler -> 'b
val stack_size : unit -> int
end
module Eff : EFF =
struct
open Delimcc
let control0 p f = take_subcont p (fun sk () ->
f (fun c -> push_subcont sk (fun () -> c)))
type ('p, 'r) op = 'p -> 'r
(* An effector interpets a handler as a function that given an
operation and an argument dispatches to the appropriate operation
clause with the current delimited continuation. *)
type effector = {active : 'p 'r.('p, 'r) op -> 'p -> 'r;
template : 'h 'p 'r.'h -> ('p, 'r) op -> 'p -> 'r}
type ('a, 'h) clause = {clause : 'p 'r.('h -> 'a) prompt -> effector -> ('p, 'r) op -> ('p -> 'r) option}
type ('a, 'b) return_clause = 'a -> 'b
type ('a, 'b, 'h) handler = ('h -> ('b, 'h) clause list) * ('a, 'b) return_clause
type ('a, 'b) plain_handler = ('b, unit) clause list * ('a, 'b) return_clause
(* the stack of effectors represents the stack of handlers *)
let effector_stack = ref []
let stack_size () = List.length !effector_stack
let push e = effector_stack := (e :: !effector_stack)
let pop () =
match !effector_stack with
| [] -> failwith "unhandled operation"
| e::es -> effector_stack := es
let peek () =
match !effector_stack with
| [] -> None
| e::es -> Some e
let new_op_with_default default =
let rec me p =
(* the effector at the top of the stack handles this
operation *)
match peek() with
| None -> default p
| Some effector -> effector.active me p
in
me
let new_op () = new_op_with_default (fun _ -> failwith "unhandled operation")
(* Obj.magic is used to coerce quantified types to their concrete
representations. Correctness is guaranteed by pointer equality on
OCaml functions. If op and op' are equal then p and k must have
types compatible with body. *)
let (|->) op body =
{clause = fun prompt effector op' ->
if op == Obj.magic op' then
Some (fun p ->
shift0 prompt
(fun k h ->
body
(Obj.magic p)
(fun h x ->
push {effector with active = Obj.magic (effector.template h)};
let result = Obj.magic k x h in
pop (); result)))
else
None}
let plain op body =
{clause = fun prompt effector op' ->
if op == Obj.magic op' then
Some (fun p ->
shift0 prompt
(fun k h ->
body
(Obj.magic p)
(fun x ->
push {effector with active = Obj.magic (effector.template h)};
let result = Obj.magic k x h in
pop (); result)))
else
None}
let shallow op body =
{clause = fun prompt effector op' ->
if op == Obj.magic op' then
Some (fun p ->
control0 prompt
(fun k h ->
body (Obj.magic p)
(fun x -> Obj.magic k x h)))
else
None}
(* A local clause can be used as an optimisation for direct-style
operations that do not need to manipulate the continuation. *)
let local op body =
{clause = fun prompt effector op' ->
if op == Obj.magic op' then
Some
(fun p -> Obj.magic (body (Obj.magic p)))
else
None}
(* An escape clause can be used as an optimisation for aborting
operations (such as exceptions) that discard the continuation. *)
let escape op body =
{clause = fun prompt effector op' ->
if op == Obj.magic op' then
Some
(fun p ->
abort prompt (fun h -> body (Obj.magic p)))
else
None}
let effector_of_op_clauses h prompt op_clauses =
(* Morally an effector is just a recursive function. We use a
record to get proper polymorphism, and value recursion to
define the recursive function. *)
let rec effector =
let rec me h op_clauses =
fun op p ->
match op_clauses with
| [] ->
(* reinvoke an unhandled operation - to be handled
by an outer handler *)
pop ();
let result = op p in
(* push this effector back on the stack in order
to correctly handle any operations in the
continuation *)
push {effector with active = Obj.magic (effector.template h)};
result
| op_clause::op_clauses ->
begin
match op_clause.clause prompt effector op with
| None -> me h op_clauses op p
| Some f -> f p
end
in
(* eta expansion circumvents the value restriction *)
{active = (fun op -> me h (op_clauses h) op);
template = (fun h -> me h (Obj.magic (op_clauses) h))}
in
effector
let handle m (op_clauses, return_clause) h =
let prompt = new_prompt () in
let effector = effector_of_op_clauses h prompt op_clauses in
push effector;
let thunk =
push_prompt prompt
(fun () ->
let result = m () in
fun _ -> return_clause result)
in
pop (); thunk h
let handle_plain m (op_clauses, return_clause) =
handle m ((fun () -> op_clauses), return_clause) ()
end
open Eff
let get : (unit, int) op = new_op ()
let put : (int, unit) op = new_op ()
let parameterised_state s m =
handle m
((fun s ->
[local get (fun () -> s);
put |-> (fun s k -> k s ())]),
(fun x -> x))
s
let rec shallow_state s m =
handle m
((fun () ->
[local get (fun () -> s);
shallow put (fun s k -> shallow_state s k)]),
(fun x -> x)) ()
let rec shallow_state' s m =
handle m
((function
| `Handle ->
[local get (fun () -> s);
put |-> (fun s k -> shallow_state' s (fun () -> k `Forward ()))]
| `Forward ->
[]),
(fun x -> x)) `Handle
let rec shallow_state''' s m =
handle_plain m
([plain get (fun () k ->
function
| `Handle ->
shallow_state''' s (fun () -> k s `Forward) `Handle
| `Forward ->
k (get ()) `Forward);
plain put (fun s k ->
function
| `Handle ->
shallow_state''' s (fun () -> k () `Forward) `Handle
| `Forward ->
k (put s) `Forward)],
(fun x _ -> x))
let rec shallow_state'''' s m =
function
| `Handle ->
handle_plain m
([plain get (fun () k h ->
shallow_state'''' s (fun () -> k s `Forward) h);
plain put (fun s k h ->
shallow_state'''' s (fun () -> k () `Forward) h)],
(fun x _ -> x)) `Handle
| `Forward -> m ()
let handle_shallow m (op_clauses, return_clause) =
handle m
((function
| `Handle -> op_clauses
| `Forward -> []),
(fun x -> x)) `Handle
let rec shallow_state'' s m =
handle_shallow m
([local get (fun () -> s);
put |-> (fun s k -> shallow_state'' s (fun () -> k `Forward ()))],
(fun x -> x))
let no_state m =
handle_plain m
([plain get (fun p k -> k (get p));
plain put (fun p k -> k (put p))],
fun x -> x)
let handle_state s m =
handle_plain m
([plain get (fun () k s -> k s s);
plain put (fun s k _ -> k () s)],
fun x s -> x) s
let handle_state_local s m =
let r = ref s in
handle_plain m
([local get (fun () -> !r);
local put (fun s -> r := s)],
fun x -> x)
(* let choice : (unit, bool) op = new_op () *)
(* let nondeterminism m = *)
(* handle m *)
(* ([choice |-> (fun () k -> k true @ k false)], *)
(* fun x -> [x]) *)
let fail : (unit, 'a) op = new_op ()
let failure m =
handle_plain m
([plain fail (fun () k -> None)],
fun x -> Some x)
let fast_failure m =
handle_plain m
([escape fail (fun () -> None)],
fun x -> Some x)
let stop = new_op ()
let rec stupid n =
handle_plain (fun () -> if n = 0 then stop () else n-1)
([escape stop (fun () -> ())],
(fun n -> stupid n))
let rec count : unit -> unit =
fun () ->
let n = get() in
print_int (stack_size());
if n = 0 then ()
else (put (n-1); count())
(* let rec repeat n = *)
(* if n = 0 then () *)
(* else (let x = shallow_state 42 get in repeat (n-1)) *)
(* (\* let _ = shallow_state 10000 count *\) *)