package bonsai

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file test_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
open! Core
open! Import
open Bonsai.For_open
open Bonsai.Let_syntax
module Private = Bonsai.Private

let constant_fold computation =
  computation
  |> Private.reveal_computation
  |> Private.Constant_fold.constant_fold
  |> Private.conceal_computation
;;

let sexp_of_computation c =
  c
  |> Private.reveal_computation
  |> Private.Skeleton.Computation.of_computation
  |> Private.Skeleton.Computation.sanitize_for_testing
  |> Private.Skeleton.Computation.minimal_sexp_of_t
;;

let print_computation c = print_s (sexp_of_computation c)

let constant_fold_and_assert_no_op computation =
  let after_computation = constant_fold computation in
  let before_sexp = sexp_of_computation computation in
  let after_sexp = sexp_of_computation after_computation in
  match Sexp.equal before_sexp after_sexp with
  | true -> print_s before_sexp
  | false ->
    Expect_test_helpers_core.print_cr
      [%here]
      [%message "Expected before/after computations to be equal, but they are not"];
    Expect_test_patdiff.print_patdiff_s before_sexp after_sexp
;;

let constant_fold_and_diff computation =
  let after_computation = constant_fold computation in
  let before_sexp = sexp_of_computation computation in
  let after_sexp = sexp_of_computation after_computation in
  Expect_test_patdiff.print_patdiff_s before_sexp after_sexp
;;

let%expect_test "map2_gets_folded" =
  let c =
    let%arr a = Value.return 5
    and b = Value.return 3 in
    a + b
  in
  print_computation c;
  [%expect
    {|
    (Return (
      value (
        Mapn (
          inputs ((
            Mapn (
              inputs (
                (Constant (id 0))
                (Constant (id 1)))))))))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "Demonstrate: an unused opaque is not optimized away" =
  let c =
    let%sub _ = opaque_const 5 in
    Bonsai.const 3
  in
  print_computation c;
  [%expect
    {|
    (Sub
      (from (Return (value Incr)))
      (via 1)
      (into (
        Sub
        (from (Return (value (Mapn (inputs ((Named (uid 1))))))))
        (via 3)
        (into (Return (value (Constant (id 4)))))))) |}];
  print_computation (constant_fold c);
  [%expect
    {|
    (Sub
      (from (Return (value Incr)))
      (via 1)
      (into (
        Sub
        (from (Return (value (Mapn (inputs ((Named (uid 1))))))))
        (via 3)
        (into (Return (value (Constant (id 4)))))))) |}]
;;

let%expect_test "opaque only used in a lazy not optimized away" =
  let c =
    let%sub a = opaque_const 5 in
    let%sub _ = opaque_computation (Bonsai.lazy_ (lazy (return a))) in
    Bonsai.const 3
  in
  constant_fold_and_assert_no_op c;
  [%expect
    {|
    (Sub
      (from (Return (value Incr)))
      (via 1)
      (into (
        Sub
        (from (
          Sub
          (from (Return (value Incr)))
          (via 3)
          (into (
            Switch
            (match_ (Mapn (inputs ((Named (uid 3))))))
            (arms ((Lazy (t ())) (Return (value Exception))))))))
        (via 7)
        (into (
          Sub
          (from (Return (value (Mapn (inputs ((Named (uid 7))))))))
          (via 9)
          (into (Return (value (Constant (id 10)))))))))) |}]
;;

let%expect_test "opaque only used in a lazy not optimized away (but the lazy might)" =
  let c =
    let%sub a = opaque_const 5 in
    let%sub _ = Bonsai.lazy_ (lazy (return a)) in
    Bonsai.const 3
  in
  print_computation c;
  [%expect
    {|
    (Sub
      (from (Return (value Incr)))
      (via 1)
      (into (
        Sub
        (from (Lazy (t ())))
        (via 2)
        (into (
          Sub
          (from (Return (value (Mapn (inputs ((Named (uid 2))))))))
          (via 4)
          (into (Return (value (Constant (id 5)))))))))) |}];
  constant_fold_and_diff c;
  [%expect
    {|
    -1,12 +1,12
      (Sub
        (from (Return (value Incr)))
        (via 1)
        (into (
          Sub
    -|    (from (Lazy (t ((Return (value (Named (uid 1))))))))
    +|    (from (Return (value (Named (uid 1)))))
          (via 2)
          (into (
            Sub
            (from (Return (value (Mapn (inputs ((Named (uid 2))))))))
            (via 4)
            (into (Return (value (Constant (id 5)))))))))) |}]
;;

let%expect_test "immediately-forced lazies are transparent to constant folding" =
  let c = Bonsai.lazy_ (lazy (Bonsai.const 5)) in
  print_computation c;
  [%expect {| (Lazy (t ())) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "nested immediate lazies are forced" =
  let c = Bonsai.lazy_ (lazy (Bonsai.lazy_ (lazy (Bonsai.const 5)))) in
  print_computation c;
  [%expect {| (Lazy (t ())) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "lazies inside of a switch with dynamic input are preserved" =
  let c =
    if%sub opaque_const_value true
    then Bonsai.lazy_ (lazy (Bonsai.const 5))
    else assert false
  in
  constant_fold_and_assert_no_op c;
  [%expect
    {|
      (Sub
        (from (Return (value Incr)))
        (via 1)
        (into (
          Switch
          (match_ (Mapn (inputs ((Named (uid 1))))))
          (arms ((Lazy (t ())) (Return (value Exception))))))) |}]
;;

let%expect_test "lazies inside of a switch with static input are forced" =
  let c =
    if%sub Value.return true then Bonsai.lazy_ (lazy (Bonsai.const 5)) else assert false
  in
  print_computation c;
  [%expect
    {|
      (Sub
        (from (Return (value (Constant (id 0)))))
        (via 1)
        (into (
          Switch
          (match_ (Mapn (inputs ((Named (uid 1))))))
          (arms ((Lazy (t ())) (Return (value Exception))))))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "lazies inside of an assoc with static input are forced" =
  let c =
    Bonsai.assoc
      (module Int)
      (Value.return (Int.Map.of_alist_exn [ 1, (); 2, () ]))
      ~f:(fun _ _ -> Bonsai.lazy_ (lazy (Bonsai.const 5)))
  in
  print_computation c;
  [%expect
    {|
      (Assoc
        (map (Constant (id 0)))
        (key_id  1)
        (cmp_id  2)
        (data_id 3)
        (by (Lazy (t ())))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "lazies inside of an assoc with dynamic input are not forced" =
  let c =
    Bonsai.assoc
      (module Int)
      (opaque_const_value (Int.Map.of_alist_exn [ 1, (); 2, () ]))
      ~f:(fun _ _ -> Bonsai.lazy_ (lazy (Bonsai.const 5)))
  in
  constant_fold_and_assert_no_op c;
  [%expect
    {|
    (Assoc
      (map     Incr)
      (key_id  1)
      (cmp_id  2)
      (data_id 3)
      (by (Lazy (t ())))) |}]
;;

let%expect_test "map2_of_map2_of_constants_gets_folded" =
  let sum =
    let%map a = Value.return 5
    and b = Value.return 3 in
    a + b
  in
  let doubled =
    let%arr a = sum
    and b = sum in
    a + b
  in
  print_computation doubled;
  [%expect
    {|
    (Return (
      value (
        Mapn (
          inputs ((
            Mapn (
              inputs (
                (Mapn (
                  inputs ((
                    Mapn (
                      inputs (
                        (Constant (id 0))
                        (Constant (id 1))))))))
                (Mapn (
                  inputs ((
                    Mapn (
                      inputs (
                        (Constant (id 0))
                        (Constant (id 1)))))))))))))))) |}];
  print_computation (constant_fold doubled);
  [%expect {|
    (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "cutoff" =
  let cutoff = return (Value.cutoff ~equal:( = ) (Value.return 3)) in
  print_computation cutoff;
  [%expect
    {| (Return (value (Cutoff (t (Constant (id 0))) (added_by_let_syntax false)))) |}];
  print_computation (constant_fold cutoff);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "errors_propagate_but_are_not_thrown" =
  let c =
    let%arr a =
      let%map _ = Value.return 5 in
      raise (Failure "err")
    and () = Bonsai.Var.value (Bonsai.Var.create ()) in
    a
  in
  print_computation c;
  [%expect
    {|
    (Return (
      value (
        Mapn (inputs ((Mapn (inputs ((Mapn (inputs ((Constant (id 0))))) Incr)))))))) |}];
  print_computation (constant_fold c);
  [%expect {|
    (Return (value Exception)) |}]
;;

let%expect_test "cutoff gets folded away" =
  let c =
    return
      (Value.cutoff ~equal:Int.equal (Value.cutoff ~equal:Int.equal (Value.return 5)))
  in
  print_computation c;
  [%expect
    {|
      (Return (
        value (
          Cutoff
          (t (Cutoff (t (Constant (id 0))) (added_by_let_syntax false)))
          (added_by_let_syntax false)))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "nested cutoffs get merged" =
  let c =
    return
      (Value.cutoff
         ~equal:Int.equal
         (Value.cutoff ~equal:Int.equal (opaque_const_value 5)))
  in
  print_computation c;
  [%expect
    {|
    (Return (
      value (
        Cutoff
        (t (
          Cutoff
          (t                   Incr)
          (added_by_let_syntax false)))
        (added_by_let_syntax false)))) |}];
  print_computation (constant_fold c);
  [%expect
    {|
    (Return (
      value (
        Cutoff
        (t                   Incr)
        (added_by_let_syntax false)))) |}]
;;

let%expect_test "state_machine1 with constant input is converted to state_machine0" =
  let c =
    Bonsai.state_machine1
      (module Int)
      (module Int)
      ~default_model:0
      ~apply_action:(fun ~inject:_ ~schedule_event:_ _model action _input -> action)
      (Value.return 5)
  in
  print_computation c;
  [%expect {| (Leaf1 (input (Constant (id 0)))) |}];
  print_computation (constant_fold c);
  [%expect {| Leaf0 |}]
;;

let%expect_test "a constant input to assoc gets distributed to a bunch of subs" =
  let c =
    let%sub from_outside = opaque_const "testing" in
    Bonsai.assoc
      (module String)
      (Value.return (String.Map.of_alist_exn [ "hello", 0; "world", 5 ]))
      ~f:(fun k v ->
        let%arr k = k
        and v = v
        and from_outside = from_outside in
        k, v, from_outside)
  in
  print_computation c;
  [%expect
    {|
    (Sub
      (from (Return (value Incr)))
      (via 1)
      (into (
        Assoc
        (map (Constant (id 3)))
        (key_id  4)
        (cmp_id  5)
        (data_id 6)
        (by (
          Return (
            value (
              Mapn (
                inputs ((
                  Mapn (
                    inputs (
                      (Named (uid 4))
                      (Mapn (
                        inputs (
                          (Named (uid 6))
                          (Named (uid 1))))))))))))))))) |}];
  constant_fold_and_diff c;
  [%expect
    {|
    -1,21 +1,40
      (Sub
        (from (Return (value Incr)))
        (via 1)
        (into (
    -|    Assoc
    -|    (map (Constant (id 3)))
    -|    (key_id  4)
    -|    (cmp_id  5)
    -|    (data_id 6)
    -|    (by (
    +|    Sub
    +|    (from (
    +|      Sub
    +|      (from (
    +|        Sub
    +|        (from (
    +|          Return (
    +|            value (
    +|              Mapn (
    +|                inputs ((Mapn (inputs ((Mapn (inputs ((Named (uid 1))))))))))))))
    +|        (via 16)
    +|        (into (Return (value (Mapn (inputs ((Named (uid 16))))))))))
    +|      (via 20)
    +|      (into (
    +|        Sub
    +|        (from (
    +|          Sub
    +|          (from (
    +|            Return (
    +|              value (
    +|                Mapn (
    +|                  inputs ((Mapn (inputs ((Mapn (inputs ((Named (uid 1))))))))))))))
    +|          (via 18)
    +|          (into (Return (value (Mapn (inputs ((Named (uid 18))))))))))
    +|        (via 21)
    +|        (into (
                Return (
                  value (
                    Mapn (
                      inputs ((
                        Mapn (
                          inputs (
    -|                  (Named (uid 4))
    -|                  (Mapn (
    -|                    inputs (
    -|                      (Named (uid 6))
    -|                      (Named (uid 1)))))))))))))))))
    +|                      (Named (uid 20))
    +|                      (Named (uid 21)))))))))))))))
    +|    (via 24)
    +|    (into (Return (value (Mapn (inputs ((Named (uid 24))))))))))) |}]
;;

let%expect_test "constant map + simplifiable assoc function => constant map" =
  let c =
    Bonsai.assoc
      (module String)
      (Value.return (String.Map.of_alist_exn [ "hello", 0; "world", 5 ]))
      ~f:(fun k v ->
        let%arr k = k
        and v = v in
        k, v)
  in
  print_computation c;
  [%expect
    {|
    (Assoc
      (map (Constant (id 0)))
      (key_id  1)
      (cmp_id  2)
      (data_id 3)
      (by (
        Return (
          value (
            Mapn (
              inputs ((
                Mapn (
                  inputs (
                    (Named (uid 1))
                    (Named (uid 3)))))))))))) |}];
  print_computation (constant_fold c);
  [%expect {|
    (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "a constant input with no external dependencies is folded into a constant"
  =
  let c =
    Bonsai.assoc
      (module String)
      (Value.return (String.Map.of_alist_exn [ "hello", 0; "world", 5 ]))
      ~f:(fun k v ->
        let%arr k = k
        and v = v in
        k, v)
  in
  print_computation c;
  [%expect
    {|
    (Assoc
      (map (Constant (id 0)))
      (key_id  1)
      (cmp_id  2)
      (data_id 3)
      (by (
        Return (
          value (
            Mapn (
              inputs ((
                Mapn (
                  inputs (
                    (Named (uid 1))
                    (Named (uid 3)))))))))))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "a switch with constant input is optimized away" =
  let c =
    match%sub Value.return true with
    | true -> Bonsai.const "hello"
    | false -> Bonsai.const "world"
  in
  print_computation c;
  [%expect
    {|
    (Sub
      (from (Return (value (Constant (id 0)))))
      (via 1)
      (into (
        Switch
        (match_ (Mapn (inputs ((Named (uid 1))))))
        (arms (
          (Return (value (Constant (id 4)))) (Return (value (Constant (id 5))))))))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "an assert-false is caught (and then optimized away)" =
  let c =
    match%sub Value.return true with
    | true -> Bonsai.const "hello"
    | _ -> assert false
  in
  print_computation c;
  [%expect
    {|
    (Sub
      (from (Return (value (Constant (id 0)))))
      (via 1)
      (into (
        Switch
        (match_ (Mapn (inputs ((Named (uid 1))))))
        (arms ((Return (value (Constant (id 4)))) (Return (value Exception))))))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value (Constant (id 0)))) |}]
;;

let%expect_test "Static exception node on missing index of [Let_syntax.switch]." =
  let c =
    Bonsai.Let_syntax.Let_syntax.switch
      ~here:[%here]
      ~match_:(Value.return 1)
      ~branches:1
      ~with_:(function
        | 0 -> Bonsai.const "hi"
        | _ -> assert false)
  in
  print_computation c;
  [%expect
    {|
    (Switch
      (match_ (Constant (id 0)))
      (arms ((Return (value (Constant (id 2))))))) |}];
  print_computation (constant_fold c);
  [%expect {| (Return (value Exception)) |}]
;;
OCaml

Innovation. Community. Security.