package bonsai

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

Source file optimize.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
open! Core_kernel
open! Import
open Component
open Composition_infix

(* Notes:
   - map_input being merged into a leaf node is _not_ implemented because map_input
     frequently decreases the amount of incremental recomputation, e.g., by making the new
     input a small part of the old one.
   - project_model being merged into a leaf node is _not_ implemented for the same reasons
     as above. *)

let map_over_constant = function
  | Packed.T { unpacked = Mapn.Map1 { t = Const.C x; f }; action_type_id; model } ->
    Packed.T { unpacked = Const.C (f x); action_type_id; model }
  | other -> other
;;

let map_over_map = function
  | Packed.T
      { unpacked = Mapn.Map1 { t = Mapn.Map1 { t; f = f2 }; f = f1 }
      ; action_type_id
      ; model
      } -> Packed.T { unpacked = Mapn.Map1 { t; f = f2 >> f1 }; action_type_id; model }
  | other -> other
;;

let map_over_map2 = function
  | Packed.T
      { unpacked =
          Mapn.Map1
            { f = f1
            ; t =
                Mapn.Map2
                  { t1; action_type_id1; t2; action_type_id2; f = f2; model1; model2 }
            }
      ; action_type_id
      ; model
      } ->
    Packed.T
      { unpacked =
          Mapn.Map2
            { t1
            ; t2
            ; action_type_id1
            ; action_type_id2
            ; f = (fun x y -> f1 (f2 x y))
            ; model1
            ; model2
            }
      ; action_type_id
      ; model
      }
  | other -> other
;;

let map_over_leaf = function
  | Packed.T
      { unpacked = Mapn.Map1 { t = Leaf.C { apply_action; compute; name }; f }
      ; action_type_id
      ; model
      } ->
    let compute ~inject input model = compute ~inject input model |> f in
    Packed.T { unpacked = Leaf.C { apply_action; compute; name }; action_type_id; model }
  | other -> other
;;

let map_input_over_map_input = function
  | Packed.T
      { unpacked = Map_input.C { t = Map_input.C { t; f = f1 }; f = f2 }
      ; action_type_id
      ; model
      } -> Packed.T { unpacked = Map_input.C { t; f = f2 >> f1 }; action_type_id; model }
  | other -> other
;;

let compose_over_pure = function
  | Packed.T { unpacked = Compose.C { t1 = Const.C c; t2 = Pure.Pure_input f; _ }; _ } ->
    Packed.T
      { unpacked = Const.C (f c)
      ; action_type_id = nothing_type_id
      ; model = Packed.unit_model_info
      }
  | Packed.T
      { unpacked = Compose.C { t1 = Pure.Pure_input g; t2 = Pure.Pure_input f; _ }; _ }
    ->
    Packed.T
      { unpacked = Pure.Pure_input (g >> f)
      ; action_type_id = nothing_type_id
      ; model = Packed.unit_model_info
      }
  | Packed.T
      { unpacked =
          Compose.C { t1 = Pure.Pure_input f; t2 = t; action_type_id2; model2; _ }
      ; _
      } ->
    Packed.T
      { unpacked = Map_input.C { t; f }
      ; action_type_id = action_type_id2
      ; model = model2
      }
  | Packed.T
      { unpacked =
          Compose.C { t1 = t; t2 = Pure.Pure_input f; action_type_id1; model1; _ }
      ; _
      } ->
    Packed.T
      { unpacked = Mapn.Map1 { t; f }; action_type_id = action_type_id1; model = model1 }
  | other -> other
;;

let compose_into_return_input (type input result)
  : (input, result, 'incr, 'event) Packed.t -> (input, result, 'incr, 'event) Packed.t
  = function
    | Packed.T
        { unpacked = Compose.C { t1; t2 = Pure.Return_input; action_type_id1; model1; _ }
        ; _
        } -> Packed.T { unpacked = t1; action_type_id = action_type_id1; model = model1 }
    | other -> other
;;

let writer_over_reader (type input result)
  : (input, result, 'incr, 'event) Packed.t -> (input, result, 'incr, 'event) Packed.t
  = function
    | Packed.T
        { unpacked = Proc.Abstraction { type_id = w; t = Proc.Var { type_id = r } }; _ } as
      t ->
      (match Type_equal.Id.same_witness w r with
       | Some T -> Pure.input
       | None -> t)
    | other -> other
;;

let optimize component =
  let visit node =
    node
    |> writer_over_reader
    |> compose_into_return_input
    |> compose_over_pure
    |> map_over_constant
    |> map_over_map
    |> map_over_map2
    |> map_over_leaf
    |> map_input_over_map_input
  in
  visit_ext component ({ visit } : Visitor.t)
;;