Source file constant_fold.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
open! Core
open! Import
let assoc_max_input_size_to_fold = 20
module Constants_in_scope = Var_id.Map.Make (Value)
module Evaluated = struct
type t =
| Unconditionally
| Maybe
end
module Types = struct
module Down = struct
type t =
{ constants_in_scope : Constants_in_scope.t
; evaluated : Evaluated.t
}
end
module Acc = Unit
module Up = Fix_transform.Unit
end
open Types.Down
include struct
let wrap_value ~(here : [%call_pos]) v = { Value.value = v; here }
let value_exception_folder ~f =
try f () with
| exn -> wrap_value (Value.Exception exn)
;;
let computation_exception_folder ~here ~f =
try f () with
| exn ->
Trampoline.return (Computation.Return { value = wrap_value (Exception exn); here })
;;
let lazy_contents_if_value_is_constant : type a. a Value.t -> a Lazy.t option =
fun { value; here = _ } ->
match value with
| Incr _
| Named _
| Both _
| Cutoff _
| Map _
| Map2 _
| Map3 _
| Map4 _
| Map5 _
| Map6 _
| Map7 _ -> None
| Constant x -> Some x
| Exception ex -> Some (lazy (raise ex))
;;
let force_but_capture_exceptions ({ Value.value; here } as t) =
match value with
| Constant x ->
(try
let _ : _ = Lazy.force x in
t
with
| exn -> wrap_value ~here (Value.Exception exn))
| _ -> t
;;
let value_is_constant value = Option.is_some (lazy_contents_if_value_is_constant value)
let constant_or_value (with_id : _ Value.t) ~f =
value_exception_folder ~f:(fun () ->
match f () with
| Some (`Constant constant) -> { with_id with value = Constant constant }
| Some (`Value value) -> { with_id with value }
| None -> with_id)
;;
let simplify_assoc_if_simpl
(type k v cmp)
~(here : Source_code_position.t)
~(key_comparator : (k, cmp) Comparator.Module.t)
~(key_id : k Var_id.t)
~(data_id : v Var_id.t)
(map : (k, v, cmp) Map.t Value.t)
by
=
let%map.Option by, may_contain =
Simplify.computation_to_function by ~key_id ~key_comparator ~data_id
in
Computation.Assoc_simpl { map; by; may_contain; here }
;;
end
module Constant_fold (Recurse : Fix_transform.Recurse with module Types := Types) = struct
let transform_v (type a) { constants_in_scope; evaluated } (value : a Value.t)
: a Value.t
=
let (), (), ({ Value.value; here = _ } as value_with_position) =
Recurse.on_value { constants_in_scope; evaluated } () `Skipping_over value
in
let rebuild value = { value_with_position with value } in
let open Option.Let_syntax in
match value with
| Exception _ | Constant _ | Incr _ -> value_with_position
| Named (_, id) ->
(match Constants_in_scope.find constants_in_scope id with
| Some value -> value
| None -> value_with_position)
| Both (a, b) as original ->
value_exception_folder ~f:(fun () ->
let value =
match
lazy_contents_if_value_is_constant a, lazy_contents_if_value_is_constant b
with
| Some l, Some r -> Value.Constant (lazy (Lazy.force l, Lazy.force r))
| Some l, None -> Map { t = b; f = (fun b -> Lazy.force l, b) }
| None, Some r -> Map { t = a; f = (fun a -> a, Lazy.force r) }
| None, None -> original
in
rebuild value)
| Cutoff { t; equal; added_by_let_syntax = was_wrapper_cutoff_added_by_let_syntax } as
original ->
value_exception_folder ~f:(fun () ->
rebuild
(match lazy_contents_if_value_is_constant t, t.value with
| Some v, _ -> Constant v
| ( None
, Cutoff
{ t
; equal = inner_equal
; added_by_let_syntax = was_nested_cutoff_added_by_let_syntax
} ) ->
let added_by_let_syntax =
was_wrapper_cutoff_added_by_let_syntax
&& was_nested_cutoff_added_by_let_syntax
in
Cutoff
{ t
; equal = (fun a b -> inner_equal a b || equal a b)
; added_by_let_syntax
}
| None, _ -> original))
| Map { t; f } ->
constant_or_value value_with_position ~f:(fun () ->
let%map t1 = lazy_contents_if_value_is_constant t in
`Constant (Lazy.map t1 ~f))
| Map2 { t1; t2; f } ->
constant_or_value value_with_position ~f:(fun () ->
match
lazy_contents_if_value_is_constant t1, lazy_contents_if_value_is_constant t2
with
| Some t1, Some t2 -> Some (`Constant (lazy (f (Lazy.force t1) (Lazy.force t2))))
| Some t1, None ->
Some (`Value (Value.Map { t = t2; f = (fun t2 -> f (Lazy.force t1) t2) }))
| None, Some t2 ->
Some (`Value (Value.Map { t = t1; f = (fun t1 -> f t1 (Lazy.force t2)) }))
| None, None -> None)
| Map3 { t1; t2; t3; f } ->
constant_or_value value_with_position ~f:(fun () ->
match
( lazy_contents_if_value_is_constant t1
, lazy_contents_if_value_is_constant t2
, lazy_contents_if_value_is_constant t3 )
with
| Some t1, Some t2, Some t3 ->
Some (`Constant (lazy (f (Lazy.force t1) (Lazy.force t2) (Lazy.force t3))))
| Some t1, Some t2, None ->
Some
(`Value
(Value.Map { t = t3; f = (fun t3 -> f (Lazy.force t1) (Lazy.force t2) t3) }))
| Some t1, None, Some t3 ->
Some
(`Value
(Value.Map { t = t2; f = (fun t2 -> f (Lazy.force t1) t2 (Lazy.force t3)) }))
| None, Some t2, Some t3 ->
Some
(`Value
(Value.Map { t = t1; f = (fun t1 -> f t1 (Lazy.force t2) (Lazy.force t3)) }))
| Some t1, None, None ->
Some
(`Value
(Value.Map2 { t1 = t2; t2 = t3; f = (fun t2 t3 -> f (Lazy.force t1) t2 t3) }))
| None, None, Some t3 ->
Some
(`Value (Value.Map2 { t1; t2; f = (fun t1 t2 -> f t1 t2 (Lazy.force t3)) }))
| None, Some t2, None ->
Some
(`Value
(Value.Map2 { t1; t2 = t3; f = (fun t1 t3 -> f t1 (Lazy.force t2) t3) }))
| None, None, None -> None)
| Map4 { t1; t2; t3; t4; f } ->
constant_or_value value_with_position ~f:(fun () ->
match
( lazy_contents_if_value_is_constant t1
, lazy_contents_if_value_is_constant t2
, lazy_contents_if_value_is_constant t3
, lazy_contents_if_value_is_constant t4 )
with
| Some t1, Some t2, Some t3, Some t4 ->
Some
(`Constant
(lazy (f (Lazy.force t1) (Lazy.force t2) (Lazy.force t3) (Lazy.force t4))))
| Some t1, Some t2, Some t3, None ->
Some
(`Value
(Value.Map
{ t = t4
; f = (fun t4 -> f (Lazy.force t1) (Lazy.force t2) (Lazy.force t3) t4)
}))
| Some t1, Some t2, None, Some t4 ->
Some
(`Value
(Value.Map
{ t = t3
; f = (fun t3 -> f (Lazy.force t1) (Lazy.force t2) t3 (Lazy.force t4))
}))
| Some t1, None, Some t3, Some t4 ->
Some
(`Value
(Value.Map
{ t = t2
; f = (fun t2 -> f (Lazy.force t1) t2 (Lazy.force t3) (Lazy.force t4))
}))
| None, Some t2, Some t3, Some t4 ->
Some
(`Value
(Value.Map
{ t = t1
; f = (fun t1 -> f t1 (Lazy.force t2) (Lazy.force t3) (Lazy.force t4))
}))
| Some t1, Some t2, None, None ->
Some
(`Value
(Value.Map2
{ t1 = t3
; t2 = t4
; f = (fun t3 t4 -> f (Lazy.force t1) (Lazy.force t2) t3 t4)
}))
| Some t1, None, Some t3, None ->
Some
(`Value
(Value.Map2
{ t1 = t2
; t2 = t4
; f = (fun t2 t4 -> f (Lazy.force t1) t2 (Lazy.force t3) t4)
}))
| None, Some t2, Some t3, None ->
Some
(`Value
(Value.Map2
{ t1
; t2 = t4
; f = (fun t1 t4 -> f t1 (Lazy.force t2) (Lazy.force t3) t4)
}))
| Some t1, None, None, Some t4 ->
Some
(`Value
(Value.Map2
{ t1 = t2
; t2 = t3
; f = (fun t2 t3 -> f (Lazy.force t1) t2 t3 (Lazy.force t4))
}))
| None, Some t2, None, Some t4 ->
Some
(`Value
(Value.Map2
{ t1
; t2 = t3
; f = (fun t1 t3 -> f t1 (Lazy.force t2) t3 (Lazy.force t4))
}))
| None, None, Some t3, Some t4 ->
Some
(`Value
(Value.Map2
{ t1; t2; f = (fun t1 t2 -> f t1 t2 (Lazy.force t3) (Lazy.force t4)) }))
| Some t1, None, None, None ->
Some
(`Value
(Value.Map3
{ t1 = t2
; t2 = t3
; t3 = t4
; f = (fun t2 t3 t4 -> f (Lazy.force t1) t2 t3 t4)
}))
| None, Some t2, None, None ->
Some
(`Value
(Value.Map3
{ t1
; t2 = t3
; t3 = t4
; f = (fun t1 t3 t4 -> f t1 (Lazy.force t2) t3 t4)
}))
| None, None, Some t3, None ->
Some
(`Value
(Value.Map3
{ t1; t2; t3 = t4; f = (fun t1 t2 t4 -> f t1 t2 (Lazy.force t3) t4) }))
| None, None, None, Some t4 ->
Some
(`Value
(Value.Map3 { t1; t2; t3; f = (fun t1 t2 t3 -> f t1 t2 t3 (Lazy.force t4)) }))
| None, None, None, None -> None)
| Map5 { t1; t2; t3; t4; t5; f } ->
constant_or_value value_with_position ~f:(fun () ->
match
( lazy_contents_if_value_is_constant t1
, lazy_contents_if_value_is_constant t2
, lazy_contents_if_value_is_constant t3
, lazy_contents_if_value_is_constant t4
, lazy_contents_if_value_is_constant t5 )
with
| Some t1, Some t2, Some t3, Some t4, Some t5 ->
Some
(`Constant
(lazy
(f
(Lazy.force t1)
(Lazy.force t2)
(Lazy.force t3)
(Lazy.force t4)
(Lazy.force t5))))
| _ -> None)
| Map6 { t1; t2; t3; t4; t5; t6; f } ->
constant_or_value value_with_position ~f:(fun () ->
match
( lazy_contents_if_value_is_constant t1
, lazy_contents_if_value_is_constant t2
, lazy_contents_if_value_is_constant t3
, lazy_contents_if_value_is_constant t4
, lazy_contents_if_value_is_constant t5
, lazy_contents_if_value_is_constant t6 )
with
| Some t1, Some t2, Some t3, Some t4, Some t5, Some t6 ->
Some
(`Constant
(lazy
(f
(Lazy.force t1)
(Lazy.force t2)
(Lazy.force t3)
(Lazy.force t4)
(Lazy.force t5)
(Lazy.force t6))))
| _ -> None)
| Map7 { t1; t2; t3; t4; t5; t6; t7; f } ->
constant_or_value value_with_position ~f:(fun () ->
match
( lazy_contents_if_value_is_constant t1
, lazy_contents_if_value_is_constant t2
, lazy_contents_if_value_is_constant t3
, lazy_contents_if_value_is_constant t4
, lazy_contents_if_value_is_constant t5
, lazy_contents_if_value_is_constant t6
, lazy_contents_if_value_is_constant t7 )
with
| Some t1, Some t2, Some t3, Some t4, Some t5, Some t6, Some t7 ->
Some
(`Constant
(lazy
(f
(Lazy.force t1)
(Lazy.force t2)
(Lazy.force t3)
(Lazy.force t4)
(Lazy.force t5)
(Lazy.force t6)
(Lazy.force t7))))
| _ -> None)
;;
open Trampoline.Let_syntax
let transform_c (type a) { constants_in_scope; evaluated } (t : a Computation.t)
: a Computation.t Trampoline.t
=
match t with
| Assoc ({ map; key_id; data_id; by; key_comparator; here; _ } as assoc_t) ->
let (), (), map_v =
Recurse.on_value { constants_in_scope; evaluated } () `Directly_on map
in
(match (force_but_capture_exceptions map_v).value with
| Exception exn -> return (Proc.read (Value.return_exn exn))
| Constant (lazy map) when Map.length map <= assoc_max_input_size_to_fold ->
let folded =
Map.mapi map ~f:(fun ~key ~data ->
let data_binding =
Computation.Sub
{ here
; from = Proc.const data
; via = data_id
; into = by
; invert_lifecycles = false
}
in
Computation.Sub
{ here
; from = Proc.const key
; via = key_id
; into = data_binding
; invert_lifecycles = false
})
|> Proc.Computation.all_map ~here
in
let%bind (), (), r =
Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on folded
in
return r
| _ ->
let%bind (), (), by =
Recurse.on_computation
{ constants_in_scope; evaluated = Maybe }
()
`Directly_on
by
in
(match
simplify_assoc_if_simpl ~here ~key_comparator ~key_id ~data_id map_v by
with
| Some kind -> return kind
| None -> return (Computation.Assoc { assoc_t with map = map_v; by })))
| Assoc_on
({ map; io_comparator = key_comparator; io_key_id = key_id; data_id; by; here; _ }
as assoc_on_t) ->
let (), (), map =
Recurse.on_value { constants_in_scope; evaluated } () `Directly_on map
in
let%bind (), (), by =
Recurse.on_computation
{ constants_in_scope; evaluated = Maybe }
()
`Directly_on
by
in
(match simplify_assoc_if_simpl ~here ~key_comparator ~key_id ~data_id map by with
| Some kind -> return kind
| None -> return (Computation.Assoc_on { assoc_on_t with map; by }))
| Switch { match_; arms; here } ->
let (), (), match_ =
Recurse.on_value { constants_in_scope; evaluated } () `Directly_on match_
in
(match (force_but_capture_exceptions match_).value with
| Exception exn -> return (Proc.read (Value.return_exn exn))
| Constant (lazy i) ->
(match Map.find arms i with
| Some c ->
let%bind (), (), r =
Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on c
in
return r
| None ->
[%sexp
"switch with value", ((i : int), "does not have a corresponding computation")]
|> Error.create_s
|> Error.to_exn
|> Value.return_exn
|> Proc.read
|> return)
| _ ->
let%bind arms =
Map.map arms ~f:(fun c ->
let%bind (), (), r =
Recurse.on_computation
{ constants_in_scope; evaluated = Maybe }
()
`Directly_on
c
in
return r)
|> Trampoline.all_map
in
return (Computation.Switch { match_; arms; here }))
| Sub { from; via; into; invert_lifecycles; here } ->
let%bind (), (), from =
Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on from
in
(match from with
| Return { value = with_id; here = _ } when value_is_constant with_id ->
let new_constants_in_scope =
Constants_in_scope.add_exn ~key:via ~data:with_id constants_in_scope
in
let%bind (), (), c =
Recurse.on_computation
{ constants_in_scope = new_constants_in_scope; evaluated }
()
`Directly_on
into
in
return c
| _ ->
let%bind (), (), into =
Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on into
in
return (Computation.Sub { from; via; into; invert_lifecycles; here }))
| Leaf1
{ input
; input_id
; model
; dynamic_action
; apply_action
; reset
; action_name
; sexp_of_action
; here
} ->
let (), (), input =
Recurse.on_value { constants_in_scope; evaluated } () `Directly_on input
in
computation_exception_folder ~here ~f:(fun () ->
match lazy_contents_if_value_is_constant input with
| None ->
return
(Computation.Leaf1
{ input
; input_id
; model
; dynamic_action
; apply_action
; reset
; action_name
; sexp_of_action
; here
})
| Some input ->
let apply_action ~inject =
apply_action ~inject (Computation_status.Active (Lazy.force input))
in
return
(Computation.Leaf0
{ model
; static_action = dynamic_action
; apply_action
; reset
; action_name
; sexp_of_action
; here
}))
| Lazy { t; here } ->
(match evaluated with
| Unconditionally ->
let%bind (), (), c =
Recurse.on_computation
{ constants_in_scope; evaluated = Unconditionally }
()
`Directly_on
(Lazy.force t)
in
return c
| Maybe ->
Lazy.map t ~f:(fun t ->
(Timer.timer ()).time `Preprocess ~f:(fun () ->
Trampoline.run
(let%bind (), (), t =
Recurse.on_computation
{ constants_in_scope; evaluated = Unconditionally }
()
`Directly_on
t
in
return t)))
|> fun t -> Computation.Lazy { t; here } |> return)
| Fix_define _
| Fix_recurse _
| Return _
| Leaf0 _
| Leaf_incr _
| Store _
| Fetch _
| Wrap _
| With_model_resetter _
| Path _
| Assoc_simpl _
| Computation_watcher _
| Lifecycle _ ->
let%bind (), (), c =
Recurse.on_computation { constants_in_scope; evaluated } () `Skipping_over t
in
return c
;;
let transform_v constants_in_scope () v = (), (), transform_v constants_in_scope v
let transform_c constants_in_scope () c =
let%bind r = transform_c constants_in_scope c in
return ((), (), r)
;;
end
open Fix_transform.Make (Types) (Constant_fold)
let constant_fold c =
let (), (), r =
Trampoline.run
(transform_c
{ constants_in_scope = Constants_in_scope.empty; evaluated = Unconditionally }
()
c)
in
r
;;