-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy patheffects.ml
341 lines (286 loc) · 7.83 KB
/
effects.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
(* TEST
include stdlib_alpha;
runtime5;
{ bytecode; }
{ native; }
*)
module Effect = Stdlib_alpha.Effect
module Uniqueue : sig
type 'a t
val create : unit -> 'a t
val push : 'a @ once unique -> 'a t -> unit
val pop : 'a t -> 'a @ once unique
val is_empty : 'a t -> bool
end = struct
type 'a t = 'a Queue.t
let create () = Queue.create ()
let push v t = Queue.push (Obj.magic_many v) t
let pop t = Obj.magic_unique (Queue.pop t)
let is_empty t = Queue.is_empty t
end
module Exchanger : sig
type 'a t
val mk : 'a @ once unique -> 'a t
val swap : 'a @ once unique -> 'a t -> 'a @ once unique
end = struct
type 'a t = 'a ref
let mk v = ref (Obj.magic_many v)
let swap v t =
let res = Obj.magic_unique !t in
t := Obj.magic_many v;
res
end
module OnceSeq : sig
type 'a node
type 'a t = unit -> 'a node @ once
val nil : 'a node
val cons : 'a @ once -> 'a t @ once -> 'a node @ once
val to_dispenser : 'a t @ once -> (unit -> 'a option @ once)
end = struct
type 'a node = 'a Seq.node
type 'a t = unit -> 'a node @ once
let nil = Seq.Nil
let cons h t = Seq.Cons (h, fun () -> Obj.magic_many (t ()))
let to_dispenser t = Seq.to_dispenser (Obj.magic_many (fun () -> Obj.magic_many (t ())))
end
open Effect
type 'a op1 = Xchg : int -> int op1
module Eff1 = Effect.Make (struct
type 'a t = 'a op1
end)
let comp1 h =
let a = Xchg 0 in
let x = Eff1.perform h a in
let b = Xchg 1 in
let y = Eff1.perform h b in
x + y
;;
let comp2 h =
let _ = Eff1.perform h (Xchg 0) in
raise Not_found
;;
let comp3 h =
let _ = Eff1.perform h (Xchg 0) in
int_of_string "fdjsl"
;;
let handle comp =
(* try *)
let rec handle = function
| Eff1.Value x -> x - 30
| Eff1.Exception _ -> 42
| Eff1.Operation(Xchg n, k) -> handle (continue k (n + 17) [])
in
Format.printf "%d@." (handle (Eff1.run comp))
(*with Not_found -> assert false*)
;;
let () =
handle comp1;
handle comp2;
handle comp3
;;
type 'a status =
| Complete of 'a @@ global aliased
| Suspended of
{ msg : int
; cont : (int, 'a, unit) Eff1.Continuation.t
}
let handle = function
| Eff1.Value v -> Complete v
| Eff1.Exception e -> raise e
| Eff1.Operation(Xchg msg, cont) -> Suspended { msg; cont }
let step (f : local_ _ -> 'a) () : 'a status =
handle (Eff1.run f)
;;
let rec run_both a b =
match a (), b () with
| Complete va, Complete vb -> va, vb
| Suspended { msg = m1; cont = k1 }, Suspended { msg = m2; cont = k2 } ->
run_both
(fun () -> handle (continue k1 m2 []))
(fun () -> handle (continue k2 m1 []))
| _ -> failwith "Improper synchronization"
;;
let comp2 h = Eff1.perform h (Xchg 21) * Eff1.perform h (Xchg 21)
let () =
let x, y = run_both (step comp1) (step comp2) in
Format.printf ">> %d %d@." x y
;;
type ('a, 't) op2 =
| Fork : (local_ 't Handler.t -> unit) -> (unit, 't) op2
| Yield : (unit, 't) op2
| Xchg : int -> (int, 't) op2
module Eff2 = Effect.Make_rec (struct
type ('a, 't) t = ('a, 't) op2
end)
let fork h f = Eff2.perform h (Fork f)
let yield h () = Eff2.perform h Yield
let xchg h v = Eff2.perform h (Xchg v)
(* A concurrent round-robin scheduler *)
let run main : unit =
let exchanger : (int * (int, unit, unit) Eff2.Continuation.t) option Exchanger.t =
Exchanger.mk None
in
(* waiting exchanger *)
let run_q = Uniqueue.create () in
(* scheduler queue *)
let dequeue () =
if Uniqueue.is_empty run_q
then () (* done *)
else (
let task = Uniqueue.pop run_q in
task ())
in
let rec enqueue : type a. (a, _, _) Eff2.Continuation.t @ once unique -> a -> unit =
fun k v ->
let task () = handle (continue k v []) in
Uniqueue.push task run_q
and handle = function
| Eff2.Value () -> dequeue ()
| Eff2.Exception e ->
print_endline (Printexc.to_string e);
dequeue ()
| Eff2.Operation(Yield, k) ->
enqueue k ();
dequeue ()
| Eff2.Operation(Fork f, k) ->
enqueue k ();
handle (Eff2.run f)
| Eff2.Operation(Xchg n, k) -> begin
match Exchanger.swap None exchanger with
| Some (n', k') ->
enqueue k' n;
handle (continue k n' [])
| None ->
let _ = Exchanger.swap (Some (n, k)) exchanger in
dequeue ()
end
in
handle (Eff2.run main)
;;
let _ =
run (fun h ->
fork h (fun h ->
Format.printf "[t1] Sending 0@.";
let v = xchg h 0 in
Format.printf "[t1] received %d@." v);
fork h (fun h ->
Format.printf "[t2] Sending 1@.";
let v = xchg h 1 in
Format.printf "[t2] received %d@." v))
;;
(*****)
type 'a op3 =
| E : string op3
type 'a op4 =
| F : string op4
module Eff3 = Effect.Make (struct
type 'a t = 'a op3
end)
module Eff4 = Effect.Make (struct
type 'a t = 'a op4
end)
let foo h1 h2 = Eff4.perform h2 F ^ " " ^ Eff3.perform h1 E ^ " " ^ Eff4.perform h2 F
let bar h =
let rec handle = function
| Eff3.Value x -> x
| Eff3.Exception e -> raise e
| Eff3.Operation(E, k) -> handle (continue k "Coucou!" [h])
in
handle (Eff3.run_with [h] (fun [h1; h2] -> foo h1 h2)) [@nontail]
;;
let baz () =
let rec handle = function
| Eff4.Value x -> x
| Eff4.Exception e -> raise e
| Eff4.Operation(F, k) ->
handle (continue k "Hello, world!" [])
in
handle (Eff4.run bar)
;;
let () = Format.printf "%s@." (baz ())
(****)
let () =
Format.printf
"%s@."
(let rec handle = function
| Eff4.Value x -> x
| Eff4.Exception e -> raise e
| Eff4.Operation(F, k) -> handle (discontinue k Not_found [])
in
handle (Eff4.run
(fun h ->
try Eff4.perform h F with
| Not_found -> "Discontinued")))
;;
let () =
try
Format.printf "%d@."
@@ let rec handle = function
| Eff1.Value x -> x
| Eff1.Exception e -> raise e
| Eff1.Operation(Xchg _, k) ->
handle (continue (Obj.magic_unique k) 21 [])
+ handle (continue (Obj.magic_unique k) 21 [])
in
handle (Eff1.run (fun h -> Eff1.perform h (Xchg 0)))
with
| Continuation_already_resumed -> Format.printf "One-shot@."
;;
(****)
type ('a, 'p) op5 =
| Yield : 'p -> (unit, 'p) op5
module Eff5 = Effect.Make1 (struct
type ('a, 'p) t = ('a, 'p) op5
end)
let invert (type a) ~(iter : local_ (a -> unit) -> unit) : a OnceSeq.t =
fun () ->
let rec handle = function
| Eff5.Value () -> OnceSeq.nil
| Eff5.Exception e -> raise e
| Eff5.Operation(Yield v, k) ->
OnceSeq.cons v (fun () -> handle (continue k () []))
in
handle (Eff5.run (fun h -> iter (fun x -> Eff5.perform h (Yield x))[@nontail]))
;;
let string_iter f s =
for i = 0 to String.length s - 1 do f (String.unsafe_get s i) done
let s = invert ~iter:(fun yield -> string_iter yield "OCaml")
let next = OnceSeq.to_dispenser s
let rec loop () =
match next () with
| Some c ->
Format.printf "%c" c;
loop ()
| None -> Format.printf "@."
;;
let () = loop ()
(****)
type 'a op6 =
| Send : int -> unit op6
| Recv : int op6
module Eff6 = Effect.Make (struct
type 'a t = 'a op6
end)
let run comp =
let rec handle_send = function
| Eff6.Value x -> x
| Eff6.Exception e -> raise e
| Eff6.Operation(Send n, k) -> handle_recv n (continue k () [])
| Eff6.Operation(Recv, _) -> failwith "protocol violation"
and handle_recv n = function
| Eff6.Value x -> x
| Eff6.Exception e -> raise e
| Eff6.Operation(Recv, k) -> handle_send (continue k n [])
| Eff6.Operation(Send _, _) -> failwith "protocol violation"
in
handle_send (Eff6.run comp)
;;
let () =
run (fun h ->
Format.printf "Send 42@.";
Eff6.perform h (Send 42);
Format.printf "Recv: %d@." (Eff6.perform h Recv);
Format.printf "Send 43@.";
Eff6.perform h (Send 43);
Format.printf "Recv: %d@." (Eff6.perform h Recv))
;;