Source file Support.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
let support name =
Code.constant ("Sup." ^ name)
module Tuple = struct
let unnest3 (x, (y, z)) = (x, y, z)
let nest3 (x, y, z) = (x, (y, z))
module Unnest3 = struct
let appearance =
support "Tuple.unnest3"
let code =
unnest3, appearance
end
module Nest3 = struct
let appearance =
support "Tuple.nest3"
let code =
nest3, appearance
end
end
module Fun = struct
let id x = x
module Id = struct
let appearance =
support "Fun.id"
let appearance =
Code.custom (fun actuals ->
match actuals with
| x :: more ->
Print.apply x more
| _ ->
Code.apply appearance actuals
)
let code =
id, appearance
end
let rot2 f y x =
f x y
module Rot2 = struct
let appearance =
support "Fun.rot2"
let appearance =
Code.custom (fun actuals ->
match actuals with
| f :: y :: x :: more ->
Print.apply f (x :: y :: more)
| _ ->
Code.apply appearance actuals
)
let code =
rot2, appearance
end
let rot3 f z x y =
f x y z
module Rot3 = struct
let appearance =
support "Fun.rot3"
let appearance =
Code.custom (fun actuals ->
match actuals with
| f :: z :: x :: y :: more ->
Print.apply f (x :: y :: z :: more)
| _ ->
Code.apply appearance actuals
)
let code =
rot3, appearance
end
let curry f x y =
f (x, y)
module Curry = struct
let appearance =
support "Fun.curry"
let appearance =
Code.custom (fun actuals ->
match actuals with
| f :: x :: y :: more ->
Print.apply f (PPrint.OCaml.tuple [ x; y ] :: more)
| _ ->
Code.apply appearance actuals
)
let code =
curry, appearance
end
let uncurry f (x, y) =
f x y
module Uncurry = struct
let appearance =
support "Fun.uncurry"
let code =
uncurry, appearance
end
end
module Iteration = struct
type ('a, 'c) iter =
('a -> unit) -> 'c -> unit
let elements_of_iter (iter : ('a, 'c) iter) (c : 'c) : 'a list =
let xs = ref [] in
let push x = xs := x :: !xs in
iter push c;
List.rev !xs
module ElementsOfIter = struct
let appearance =
support "Iteration.elements_of_iter"
let code =
elements_of_iter, appearance
end
type ('a, 's, 'c) foldr =
('a -> 's -> 's) -> 'c -> 's -> 's
let elements_of_foldr (foldr : ('a, 's, 'c) foldr) (c : 'c) : 'a list =
let cons x xs = x :: xs in
foldr cons c []
|> List.rev
module ElementsOfFoldr = struct
let appearance =
support "Iteration.elements_of_foldr"
let code =
elements_of_foldr, appearance
end
type ('a, 's, 'c) foldl =
('s -> 'a -> 's) -> 's -> 'c -> 's
let elements_of_foldl (foldl : ('a, 's, 'c) foldl) (c : 'c) : 'a list =
let cons xs x = x :: xs in
foldl cons [] c
|> List.rev
module ElementsOfFoldl = struct
let appearance =
support "Iteration.elements_of_foldl"
let code =
elements_of_foldl, appearance
end
type ('a, 'b, 'c) iteri =
('a -> 'b -> unit) -> 'c -> unit
let elements_of_iteri (iteri : ('a, 'b, 'c) iteri) (c : 'c) : ('a * 'b) list =
let xvs = ref [] in
let push x v = xvs := (x, v) :: !xvs in
iteri push c;
List.rev !xvs
module ElementsOfIteri = struct
let appearance =
support "Iteration.elements_of_iteri"
let code =
elements_of_iteri, appearance
end
type ('a, 'b, 's, 'c) foldri =
('a -> 'b -> 's -> 's) -> 'c -> 's -> 's
let elements_of_foldri (foldri : ('a, 'b, 's, 'c) foldri) (c : 'c) : ('a * 'b) list =
let cons x v xvs = (x, v) :: xvs in
foldri cons c []
|> List.rev
module ElementsOfFoldri = struct
let appearance =
support "Iteration.elements_of_foldri"
let code =
elements_of_foldri, appearance
end
type ('a, 'b, 's, 'c) foldli =
('s -> 'a -> 'b -> 's) -> 's -> 'c -> 's
let elements_of_foldli (foldli : ('a, 'b, 's, 'c) foldli) (c : 'c) : ('a * 'b) list =
let cons xvs x v = (x, v) :: xvs in
foldli cons [] c
|> List.rev
module ElementsOfFoldli = struct
let appearance =
support "Iteration.elements_of_foldli"
let code =
elements_of_foldli, appearance
end
end
module List = struct
let equal eq xs ys =
List.length xs = List.length ys &&
List.for_all2 eq xs ys
let rec to_seq xs =
fun () ->
match xs with
| [] ->
Seq.Nil
| x :: xs ->
Seq.Cons (x, to_seq xs)
module ToSeq = struct
let appearance =
support "List.to_seq"
let code =
to_seq, appearance
end
let[@tail_mod_cons] rec of_seq xs =
match xs() with
| Seq.Nil ->
[]
| Seq.Cons (x, xs) ->
x :: of_seq xs
module OfSeq = struct
let appearance =
support "List.of_seq"
let code =
of_seq, appearance
end
end
module Exn = struct
let handle f x =
try Ok (f x) with
| Engine.PleaseBackOff -> raise Engine.PleaseBackOff
| e -> Error e
module Handle = struct
let appearance =
support "Exn.handle"
let code =
handle, appearance
end
end
module Seq = struct
include Seq
exception ForcedTwice
let oneshot f =
let forced = ref false in
fun x ->
if !forced then raise ForcedTwice;
forced := true;
f x
open Seq
let rec affine xs =
oneshot (fun () ->
match xs() with
| Nil ->
Nil
| Cons (x, xs) ->
Cons (x, affine xs)
)
let to_option xs =
match xs() with
| Nil ->
None
| Cons (x, xs) ->
Some (x, xs)
let rec list_to_affine_seq (xs : 'a list) : 'a t =
oneshot (fun () ->
match xs with
| [] ->
Nil
| x :: xs ->
Cons (x, list_to_affine_seq xs)
)
module ListToAffineSeq = struct
let appearance =
support "Seq.list_to_affine_seq"
let code =
list_to_affine_seq, appearance
end
end
module VSeq = struct
type 'a t = {
force: unit -> 'a node;
valid: unit -> bool
}
and 'a node =
| Nil
| Cons of 'a * 'a t
let valid xs =
xs.valid()
exception ForcedTwice
let oneshot f =
let forced = ref false in
let force x =
if !forced then raise ForcedTwice;
forced := true;
f x
and valid () =
not !forced
in
{ force; valid }
let rec affine (xs : 'a Seq.t) : 'a t =
oneshot (fun () ->
match xs() with
| Seq.Nil ->
Nil
| Seq.Cons (x, xs) ->
Cons (x, affine xs)
)
let to_option xs =
match xs.force() with
| Nil ->
None
| Cons (x, xs) ->
Some (x, xs)
let rec forget (xs : 'a t) : 'a Seq.t =
fun () ->
match xs.force() with
| Nil ->
Seq.Nil
| Cons (x, xs) ->
Seq.Cons (x, forget xs)
end