Source file gather_switch.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
open! Core
open! Bonsai_private_base.Import
open! Bonsai_private_base
open Incr.Let_syntax

let f ~gather ~recursive_scopes ~time_source ~match_ ~arms ~here =
  let wrap_switch ~branch ~type_id inject = Action.switch ~branch ~type_id >>> inject in
  let%bind.Trampoline gathered =
    Trampoline.all_map (Map.map arms ~f:(gather ~recursive_scopes ~time_source))
  in
  let may_contain =
    Map.fold
      gathered
      ~init:(May_contain.Unresolved.non_recursive ~path:No ~lifecycle:No ~input:No)
      ~f:(fun ~key:_ ~data:(Computation.T gathered) acc ->
        May_contain.Unresolved.merge acc gathered.may_contain)
  in
  let run ~environment ~fix_envs ~path ~model ~inject =
    let resolved = Environment.Recursive.resolve_may_contain fix_envs may_contain in
    let path_needs_disambiguation =
      let num_contain_path =
        Map.count gathered ~f:(fun (T { may_contain; _ }) ->
          match Environment.Recursive.resolve_may_contain fix_envs may_contain with
          | { path = Yes_or_maybe; _ } -> true
          | { path = No; _ } -> false)
      in
      num_contain_path > 1
    in
    annotate ~here Switch_model model;
    let index = Value.eval environment match_ in
    let result_input_and_lifecycle =
      let%bind index in
      (* !!!This is a load-bearing bind!!!

         If this bind isn't here, the scope that is created for the bind doesn't exist,
         and old incremental nodes might still be active, and with things like [match%sub]
         or [Bonsai.match_either] can witness old nodes, which can cause [assert false] to
         trigger. *)
      let path =
        if path_needs_disambiguation
        then Path.append path (Path.Elem.Switch index)
        else path
      in
      let (T
            { model = model_info
            ; input = input_info
            ; action = action_info
            ; apply_action = _
            ; reset = _
            ; may_contain = _
            ; run
            })
        =
        Map.find_exn gathered index
      in
      let chosen_model =
        Incremental.map model ~f:(fun map ->
          let (Meta.Model.Hidden.T { model; info }) =
            Meta.Multi_model.find_exn map index
          in
          let equal =
            Meta.Model.Type_id.same_witness_exn info.type_id model_info.type_id
          in
          Type_equal.conv equal model)
      in
      let snapshot, () =
        run
          ~environment
          ~fix_envs
          ~model:chosen_model
          ~path
          ~inject:(wrap_switch ~type_id:action_info ~branch:index inject)
        |> Trampoline.run
      in
      let input =
        let%mapn input = Input.to_incremental (Snapshot.input snapshot) in
        Meta.Input.Hidden.T { input; type_id = input_info; key = index }
      in
      Incr.return
        (Snapshot.result snapshot, input, Snapshot.lifecycle_or_empty ~here snapshot)
    in
    let result = Incr.bind result_input_and_lifecycle ~f:Tuple3.get1
    and input = Incr.bind result_input_and_lifecycle ~f:Tuple3.get2
    and lifecycle = Incr.bind result_input_and_lifecycle ~f:Tuple3.get3 in
    let lifecycle =
      (* if we can prove that none of the switch cases have lifecycle functions, then
         return None, dropping the incremental node on the floor. *)
      match resolved.lifecycle with
      | No -> None
      | Yes_or_maybe -> Some lifecycle
    in
    let input =
      match resolved.input with
      | Yes_or_maybe -> Input.dynamic input
      | No -> Input.static_dummy_for_switch
    in
    Trampoline.return (Snapshot.create ~here ~result ~input ~lifecycle, ())
  in
  let apply_action
    ~inject
    ~schedule_event
    input
    model
    (Action.Switch { action; type_id = action_type_id; branch = index })
    =
    let (T
          { model = tm
          ; input = im
          ; action = am
          ; apply_action
          ; run = _
          ; reset = _
          ; may_contain = _
          })
      =
      Map.find_exn gathered index
    in
    let (T { model = chosen_model; info = chosen_model_info; _ }) =
      Meta.Multi_model.find_exn model index
    in
    match
      ( Action.Type_id.same_witness action_type_id am
      , Meta.Model.Type_id.same_witness chosen_model_info.type_id tm.type_id )
    with
    | Some T, Some T ->
      let new_model =
        match input with
        | Computation_status.Active
            (Meta.Input.Hidden.T
              { input = chosen_input; type_id = chosen_input_info; key = index' }) ->
          (match index = index', Meta.Input.same_witness chosen_input_info im with
           | true, Some T ->
             apply_action
               ~inject:(wrap_switch ~type_id:am ~branch:index inject)
               ~schedule_event
               (Active chosen_input)
               chosen_model
               action
           | _ ->
             apply_action
               ~inject:(wrap_switch ~type_id:am ~branch:index inject)
               ~schedule_event
               Inactive
               chosen_model
               action)
        | Inactive ->
          apply_action
            ~inject:(wrap_switch ~type_id:am ~branch:index inject)
            ~schedule_event
            Inactive
            chosen_model
            action
      in
      let new_model = Meta.Model.Hidden.create tm new_model in
      Meta.Multi_model.set model ~key:index ~data:new_model
    | None, None | Some T, None | None, Some T ->
      let action = Action.Type_id.to_sexp action_type_id action in
      Ui_metrics.Counters.observe Bonsai_switch_action_dropped;
      eprint_s
        [%message
          "an action inside of Bonsai.switch has been dropped because the computation is \
           no longer active"
            (index : int)
            (action : Sexp.t)];
      model
  in
  let reset ~inject ~schedule_event model =
    let f ~key:index ~data:(model : Meta.Model.Hidden.t) =
      let (T { model = chosen_model; info = chosen_model_info; _ }) = model in
      let (T
            { model = tm
            ; input = _
            ; action = am
            ; reset
            ; apply_action = _
            ; may_contain = _
            ; run = _
            })
        =
        Map.find_exn gathered index
      in
      let T = Meta.Model.Type_id.same_witness_exn tm.type_id chosen_model_info.type_id in
      let new_model =
        reset
          ~inject:(wrap_switch ~type_id:am ~branch:index inject)
          ~schedule_event
          chosen_model
      in
      Meta.Model.Hidden.create tm new_model
    in
    Meta.Multi_model.of_models (Map.mapi (Meta.Multi_model.to_models model) ~f)
  in
  let model =
    let models =
      Map.map gathered ~f:(fun (T { model; _ }) ->
        Meta.Model.Hidden.create model model.default)
    in
    Meta.Multi_model.model_info (Meta.Multi_model.of_models models)
  in
  Trampoline.return
    (Computation.T
       { model
       ; input = Meta.Input.Hidden.int
       ; action = Action.Type_id.switch
       ; apply_action
       ; reset
       ; run
       ; may_contain
       })
;;