package bonsai

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

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
open! Core
open! Import

module Constants_in_scope =
  Univ_map.Make
    (Univ_map.Type_id_key)
    (struct
      include Value

      let sexp_of_t _ = sexp_of_opaque
    end)

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 value_id name = Type_equal.Id.create ~name sexp_of_opaque
  let wrap_value ~here name v = { Value.value = v; here; id = value_id name }

  let value_exception_folder ~f =
    try f () with
    | exn -> wrap_value ~here:None "exception" (Value.Exception exn)
  ;;

  let computation_exception_folder name ~f =
    try f () with
    | exn -> Computation.Return (wrap_value ~here:None name (Exception exn))
  ;;

  let lazy_contents_if_value_is_constant : type a. a Value.t -> a Lazy.t option =
    fun { value; here = _; id = _ } ->
      match value with
      | Incr _
      | Named _
      | Both _
      | Cutoff _
      | Map _
      | Map2 _
      | Map3 _
      | Map4 _
      | Map5 _
      | Map6 _
      | Map7 _ -> None
      | Constant x -> Some (lazy x)
      | Exception ex -> Some (lazy (raise ex))
  ;;

  let contents_if_value_is_constant value =
    Option.map (lazy_contents_if_value_is_constant value) ~f:Lazy.force
  ;;

  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 -> { with_id with value = Constant constant }
      | None -> with_id)
  ;;

  let simplify_assoc_if_simpl
        (type k v cmp)
        ~(key_comparator : (k, cmp) comparator)
        ~(key_id : k Type_equal.Id.t)
        ~(data_id : v Type_equal.Id.t)
        (map : (k, v, cmp) Map.t Value.t)
        by
    =
    let module C = (val key_comparator) in
    let%map.Option by =
      Simplify.computation_to_function
        by
        ~key_compare:C.comparator.compare
        ~key_id
        ~data_id
    in
    Computation.Assoc_simpl { map; by }
  ;;
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 = _; id } as value_with_id) =
      Recurse.on_value { constants_in_scope; evaluated } () `Skipping_over value
    in
    let rebuild value = { value_with_id with value } in
    let open Option.Let_syntax in
    match value with
    | Exception _ | Constant _ | Incr _ -> value_with_id
    | Named _ ->
      (match Constants_in_scope.find constants_in_scope id with
       | Some value -> value
       | None -> value_with_id)
    | Both (a, b) as original ->
      value_exception_folder ~f:(fun () ->
        let value =
          match contents_if_value_is_constant a, contents_if_value_is_constant b with
          | Some l, Some r -> Value.Constant (l, r)
          | Some l, None -> Map { t = b; f = (fun b -> l, b) }
          | None, Some r -> Map { t = a; f = (fun a -> a, 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 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_id ~f:(fun () ->
        let%map t1 = contents_if_value_is_constant t in
        f t1)
    | Map2 { t1; t2; f } ->
      constant_or_value value_with_id ~f:(fun () ->
        let%bind t1 = contents_if_value_is_constant t1 in
        let%map t2 = contents_if_value_is_constant t2 in
        f t1 t2)
    | Map3 { t1; t2; t3; f } ->
      constant_or_value value_with_id ~f:(fun () ->
        let%bind t1 = contents_if_value_is_constant t1 in
        let%bind t2 = contents_if_value_is_constant t2 in
        let%map t3 = contents_if_value_is_constant t3 in
        f t1 t2 t3)
    | Map4 { t1; t2; t3; t4; f } ->
      constant_or_value value_with_id ~f:(fun () ->
        let%bind t1 = contents_if_value_is_constant t1 in
        let%bind t2 = contents_if_value_is_constant t2 in
        let%bind t3 = contents_if_value_is_constant t3 in
        let%map t4 = contents_if_value_is_constant t4 in
        f t1 t2 t3 t4)
    | Map5 { t1; t2; t3; t4; t5; f } ->
      constant_or_value value_with_id ~f:(fun () ->
        let%bind t1 = contents_if_value_is_constant t1 in
        let%bind t2 = contents_if_value_is_constant t2 in
        let%bind t3 = contents_if_value_is_constant t3 in
        let%bind t4 = contents_if_value_is_constant t4 in
        let%map t5 = contents_if_value_is_constant t5 in
        f t1 t2 t3 t4 t5)
    | Map6 { t1; t2; t3; t4; t5; t6; f } ->
      constant_or_value value_with_id ~f:(fun () ->
        let%bind t1 = contents_if_value_is_constant t1 in
        let%bind t2 = contents_if_value_is_constant t2 in
        let%bind t3 = contents_if_value_is_constant t3 in
        let%bind t4 = contents_if_value_is_constant t4 in
        let%bind t5 = contents_if_value_is_constant t5 in
        let%map t6 = contents_if_value_is_constant t6 in
        f t1 t2 t3 t4 t5 t6)
    | Map7 { t1; t2; t3; t4; t5; t6; t7; f } ->
      constant_or_value value_with_id ~f:(fun () ->
        let%bind t1 = contents_if_value_is_constant t1 in
        let%bind t2 = contents_if_value_is_constant t2 in
        let%bind t3 = contents_if_value_is_constant t3 in
        let%bind t4 = contents_if_value_is_constant t4 in
        let%bind t5 = contents_if_value_is_constant t5 in
        let%bind t6 = contents_if_value_is_constant t6 in
        let%map t7 = contents_if_value_is_constant t7 in
        f t1 t2 t3 t4 t5 t6 t7)
  ;;

  let transform_c (type a) { constants_in_scope; evaluated } (t : a Computation.t)
    : a Computation.t
    =
    match t with
    | Assoc ({ map; key_id; data_id; by; key_comparator; _ } as assoc_t) ->
      let (), (), map_v =
        Recurse.on_value { constants_in_scope; evaluated } () `Directly_on map
      in
      (match map_v.value with
       | Exception exn -> Proc.read (Value.return_exn exn)
       | Constant map ->
         Map.mapi map ~f:(fun ~key ~data ->
           (* In this case, the map is constant, so we have access to the key/data pair
              directly. We use the [Sub]s below with the correct [key_id]/[data_id] so
              that [by] will refer to these constants and then we can recursively rely on
              the constant-folding optimizations to clean up these [Sub]s for us. *)
           let data_binding =
             Computation.Sub
               { here = None; from = Proc.const data; via = data_id; into = by }
           in
           Computation.Sub
             { here = None; from = Proc.const key; via = key_id; into = data_binding })
         |> Proc.Computation.all_map
         |> Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on
         |> fun ((), (), r) -> r
       | _ ->
         let (), (), by =
           Recurse.on_computation
             { constants_in_scope; evaluated = Maybe }
             ()
             `Directly_on
             by
         in
         (match simplify_assoc_if_simpl ~key_comparator ~key_id ~data_id map_v by with
          | Some kind -> kind
          | None -> Assoc { assoc_t with map = map_v; by }))
    | Assoc_on
        ({ map; io_comparator = key_comparator; io_key_id = key_id; data_id; by; _ } as
         assoc_on_t) ->
      let (), (), map =
        Recurse.on_value { constants_in_scope; evaluated } () `Directly_on map
      in
      let (), (), by =
        Recurse.on_computation
          { constants_in_scope; evaluated = Maybe }
          ()
          `Directly_on
          by
      in
      (match simplify_assoc_if_simpl ~key_comparator ~key_id ~data_id map by with
       | Some kind -> kind
       | None -> 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 match_.value with
       | Exception exn -> Proc.read (Value.return_exn exn)
       | Constant i ->
         (match Map.find arms i with
          | Some c ->
            let (), (), r =
              Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on c
            in
            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)
       | _ ->
         let arms =
           Map.map arms ~f:(fun c ->
             let (), (), r =
               Recurse.on_computation
                 { constants_in_scope; evaluated = Maybe }
                 ()
                 `Directly_on
                 c
             in
             r)
         in
         Switch { match_; arms; here })
    | Sub { from; via; into; here } ->
      let (), (), from =
        Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on from
      in
      (match from with
       | Return with_id 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 (), (), c =
           Recurse.on_computation
             { constants_in_scope = new_constants_in_scope; evaluated }
             ()
             `Directly_on
             into
         in
         c
       | _ ->
         let (), (), into =
           Recurse.on_computation { constants_in_scope; evaluated } () `Directly_on into
         in
         Sub { from; via; into; here })
    | Leaf1 { input; input_id; model; dynamic_action; apply_action; reset } ->
      let (), (), input =
        Recurse.on_value { constants_in_scope; evaluated } () `Directly_on input
      in
      computation_exception_folder "leaf1" ~f:(fun () ->
        match contents_if_value_is_constant input with
        | None -> Leaf1 { input; input_id; model; dynamic_action; apply_action; reset }
        | Some input ->
          let apply_action ~inject_dynamic ~inject_static =
            apply_action
              ~inject_static:inject_dynamic
              ~inject_dynamic:inject_static
              (Some input)
          in
          let reset ~inject_dynamic ~inject_static =
            reset ~inject_static:inject_dynamic ~inject_dynamic:inject_static
          in
          Leaf0 { model; static_action = dynamic_action; apply_action; reset })
    | Lazy t ->
      (match evaluated with
       | Unconditionally ->
         let (), (), c =
           Recurse.on_computation
             { constants_in_scope; evaluated = Unconditionally }
             ()
             `Directly_on
             (Lazy.force t)
         in
         c
       | Maybe ->
         Lazy.map t ~f:(fun t ->
           (* Because this recursion is inside of a Lazy.map, it'll only proceed
              be run if the lazy is forced, at which point we _are_ unconditionally
              running it. *)
           (* NOTE: Constant folding on lazys is deferred until lazys are forced. One
              important consideration is that multiple deferred optimizations should occur
              as if the lazy's were not there to begin with. One possible bug here is that
              nested optimizations could be reversed e.g. (constant_fold (lazy (comp))) ==>
              (lazy (constant_fold (comp))), but because we use Lazy.map the same order is
              preserved. There is not a use case for this right now, but if we want more
              interesting interacctions between deferred optimizations, we could mint a
              type for deferred optimizations which we can introspect.
           *)
           let (), (), t =
             Recurse.on_computation
               { constants_in_scope; evaluated = Unconditionally }
               ()
               `Directly_on
               t
           in
           t)
         |> Computation.Lazy)
    | Return _
    | Leaf0 _
    | Leaf01 _
    | Leaf_incr _
    | Store _
    | Fetch _
    | Wrap _
    | With_model_resetter _
    | Path
    | Assoc_simpl _
    | Lifecycle _ ->
      let (), (), c =
        Recurse.on_computation { constants_in_scope; evaluated } () `Skipping_over t
      in
      c
  ;;

  let transform_v constants_in_scope () v = (), (), transform_v constants_in_scope v
  let transform_c constants_in_scope () c = (), (), transform_c constants_in_scope c
end

open Fix_transform.Make (Types) (Constant_fold)

let constant_fold c =
  let (), (), r =
    transform_c
      { constants_in_scope = Constants_in_scope.empty; evaluated = Unconditionally }
      ()
      c
  in
  r
;;
OCaml

Innovation. Community. Security.