Source file bonsai_extra.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
open! Core
open Bonsai.For_open
open Bonsai.Let_syntax
let with_inject_fixed_point f =
let%sub r, _ =
Bonsai.wrap
(module Unit)
~default_model:()
~apply_action:(fun ~inject:_ ~schedule_event (_result, inject) () action ->
schedule_event (inject action))
~f:(fun _model inject -> f inject)
in
return r
;;
let with_self_effect
(type a)
(module M : Bonsai.Model with type t = a)
~(f : a Bonsai.Computation_status.t Effect.t Value.t -> a Computation.t)
: a Computation.t
=
Bonsai.wrap
(module struct
type t = M.t option [@@deriving sexp, equal]
end)
~default_model:None
~apply_action:(fun ~inject:_ ~schedule_event:_ result _model () -> Some result)
~f:(fun model inject ->
let%sub current_model =
let%sub get_model = Bonsai.yoink model in
let%arr inject = inject
and get_model = get_model in
let%bind.Effect () = inject () in
let%map.Effect model = get_model in
match model with
| Inactive
| Active None -> Bonsai.Computation_status.Inactive
| Active (Some v) -> Active v
in
f current_model)
;;
let state_machine1_dynamic_model
(type m a)
(module M : Bonsai.Model with type t = m)
(module A : Bonsai.Action with type t = a)
~model
~apply_action
input
=
let model_creator =
match model with
| `Given m ->
Value.map m ~f:(fun m -> function
| None -> m
| Some a -> a)
| `Computed f -> f
in
let module M_actual = struct
type t = M.t option [@@deriving sexp, equal]
end
in
let apply_action ~inject ~schedule_event input model action =
match input with
| Bonsai.Computation_status.Active (input, model_creator) ->
let model = model_creator model in
Some (apply_action ~inject ~schedule_event input model action)
| Inactive ->
eprint_s
[%message
[%here]
"An action sent to a [state_machine1_dynamic_model] has been dropped because \
its input was not present. This happens when the \
[state_machine1_dynamic_model] is inactive when it receives a message."
(action : A.t)];
model
in
let%sub model_and_inject =
Bonsai.state_machine1
(module M_actual)
(module A)
~default_model:None
~apply_action
(Value.both input model_creator)
in
let%arr model, inject = model_and_inject
and model_creator = model_creator in
model_creator model, inject
;;
let state_machine0_dynamic_model model_mod action_mod ~model ~apply_action =
let apply_action ~inject ~schedule_event () model action =
apply_action ~inject ~schedule_event model action
in
state_machine1_dynamic_model model_mod action_mod ~model ~apply_action (Value.return ())
;;
let state_dynamic_model (type m) (module M : Bonsai.Model with type t = m) ~model =
let apply_action ~inject:_ ~schedule_event:_ _old_model new_model = new_model in
state_machine0_dynamic_model (module M) (module M) ~model ~apply_action
;;
let exactly_once effect =
let%sub has_run, set_has_run = Bonsai.state (module Bool) ~default_model:false in
if%sub has_run
then Bonsai.const ()
else
Bonsai.Edge.lifecycle
~on_activate:
(let%map set_has_run = set_has_run
and event = effect in
Effect.Many [ set_has_run true; event ])
()
;;
let exactly_once_with_value modul effect =
let%sub value, set_value = Bonsai.state_opt modul in
let%sub () =
match%sub value with
| None ->
Bonsai.Edge.lifecycle
~on_activate:
(let%map set_value = set_value
and effect = effect in
let%bind.Effect r = effect in
set_value (Some r))
()
| Some _ -> Bonsai.const ()
in
return value
;;
let value_with_override (type m) (module M : Bonsai.Model with type t = m) value =
let%sub state, set_state = Bonsai.state_opt (module M) in
let%sub value =
match%sub state with
| Some override -> return override
| None -> return value
in
let%sub setter =
let%arr set_state = set_state in
fun v -> set_state (Some v)
in
return (Value.both value setter)
;;
let pipe (type a) (module A : Bonsai.Model with type t = a) =
let module Model = struct
type t =
{ queued_actions : A.t Fdeque.t
; queued_receivers : (unit, a) Effect.Private.Callback.t Fdeque.t
}
let equal = phys_equal
let default = { queued_actions = Fdeque.empty; queued_receivers = Fdeque.empty }
let sexp_of_t { queued_actions; _ } = [%sexp_of: A.t Fdeque.t] queued_actions
let t_of_sexp sexp =
let queued_actions = [%of_sexp: A.t Fdeque.t] sexp in
{ default with queued_actions }
;;
end
in
let module Action = struct
type t =
| Add_action of a
| Add_receiver of (unit, a) Effect.Private.Callback.t
let sexp_of_t = function
| Add_action a -> A.sexp_of_t a
| Add_receiver r -> sexp_of_opaque r
;;
end
in
let%sub _, inject =
Bonsai.state_machine0
(module Model)
(module Action)
~default_model:Model.default
~apply_action:(fun ~inject:_ ~schedule_event model -> function
| Add_action a ->
(match Fdeque.dequeue_front model.queued_receivers with
| None ->
let queued_actions = Fdeque.enqueue_back model.queued_actions a in
{ model with queued_actions }
| Some (hd, queued_receivers) ->
schedule_event (Effect.Private.Callback.respond_to hd a);
{ model with queued_receivers })
| Add_receiver r ->
(match Fdeque.dequeue_front model.queued_actions with
| None ->
let queued_receivers = Fdeque.enqueue_back model.queued_receivers r in
{ model with queued_receivers }
| Some (hd, queued_actions) ->
schedule_event (Effect.Private.Callback.respond_to r hd);
{ model with queued_actions }))
in
let%arr inject = inject in
let request =
Effect.Private.make ~request:() ~evaluator:(fun r -> inject (Add_receiver r))
in
(fun a -> inject (Add_action a)), request
;;
module Id_gen (T : Int_intf.S) () = struct
include T
let component =
let%map.Computation _, fetch =
Bonsai.actor0
(module T)
(module Unit)
~default_model:T.zero
~recv:(fun ~schedule_event:_ i () -> T.( + ) i T.one, i)
in
fetch ()
;;
end
let mirror'
(type m)
(module M : Bonsai.Model with type t = m)
~(store_set : (m -> unit Effect.t) Value.t)
~(store_value : m option Value.t)
~(interactive_set : (m -> unit Effect.t) Value.t)
~(interactive_value : m option Value.t)
=
let module M2 = struct
type t =
{ store : M.t option
; interactive : M.t option
}
[@@deriving sexp, equal]
end
in
let callback =
let%map store_set = store_set
and interactive_set = interactive_set in
fun old_pair { M2.store = store_value; interactive = interactive_value } ->
let stability =
if [%equal: M.t option] store_value interactive_value then `Stable else `Unstable
in
match stability with
| `Stable ->
Effect.Ignore
| `Unstable ->
(match old_pair with
| None ->
(match store_value, interactive_value with
| Some store_value, _ -> interactive_set store_value
| None, Some interactive_value -> store_set interactive_value
| None, None ->
eprint_s
[%message
"BUG" [%here] {|if both are None, then we shouldn't be `Unstable |}];
Effect.Ignore)
| Some { M2.store = old_store_value; interactive = old_interactive_value } ->
let store_changed = not ([%equal: M.t option] old_store_value store_value) in
let interactive_changed =
not ([%equal: M.t option] old_interactive_value interactive_value)
in
(match interactive_changed, store_changed with
| true, true ->
(match interactive_value, store_value with
| Some interactive_value, (Some _ | None) -> store_set interactive_value
| None, Some store_value -> interactive_set store_value
| None, None -> Effect.Ignore)
| true, false ->
(match interactive_value with
| Some interactive_value -> store_set interactive_value
| None -> Effect.Ignore)
| false, true ->
(match store_value with
| Some store_value -> interactive_set store_value
| None -> Effect.Ignore)
| false, false ->
eprint_s
[%message
"BUG" [%here] "on_change triggered when nothing actually changed?"];
Effect.Ignore))
in
Bonsai.Edge.on_change'
(module M2)
(let%map store = store_value
and interactive = interactive_value in
{ M2.store; interactive })
~callback
;;
let mirror
(type m)
(module M : Bonsai.Model with type t = m)
~store_set
~store_value
~interactive_set
~interactive_value
=
let store_value = store_value >>| Option.some in
let interactive_value = interactive_value >>| Option.some in
mirror' (module M) ~store_set ~store_value ~interactive_set ~interactive_value
;;
let with_last_modified_time ~equal input =
let%sub now = Bonsai.Clock.now in
let%sub result = return (Bonsai.Value.both input now) in
Bonsai.Incr.value_cutoff result ~equal:(fun (a, _) (b, _) -> equal a b)
;;
let is_stable ~equal input ~time_to_stable =
match Time_ns.Span.sign time_to_stable with
| Zero | Neg ->
eprint_s [%message "Bonsai_extra.is_stable: [time_to_stable] should be positive"];
Bonsai.const false
| Pos ->
let%sub _, last_modified_time = with_last_modified_time ~equal input in
let%sub next_stable_time =
let%arr last_modified_time = last_modified_time in
Time_ns.add last_modified_time time_to_stable
in
let%sub at_next_stable_time = Bonsai.Clock.at next_stable_time in
(match%arr at_next_stable_time with
| Before -> false
| After -> true)
;;
let most_recent_value_satisfying m input ~condition =
Bonsai.most_recent_some m input ~f:(fun a -> if condition a then Some a else None)
;;
module Stability = struct
type 'a t =
| Stable of 'a
| Unstable of
{ previously_stable : 'a option
; unstable_value : 'a
}
[@@deriving sexp, equal]
let most_recent_stable_value = function
| Stable a -> Some a
| Unstable { previously_stable; _ } -> previously_stable
;;
end
let value_stability
(type a)
(module M : Bonsai.Model with type t = a)
input
~time_to_stable
=
let%sub is_stable = is_stable ~equal:M.equal input ~time_to_stable in
let%sub most_recent_stable_and_true =
let%sub input_and_stability = return (Value.both input is_stable) in
most_recent_value_satisfying
(module struct
type t = M.t * bool [@@deriving sexp, equal]
end)
input_and_stability
~condition:(fun (_input, is_stable) -> is_stable)
in
match%sub most_recent_stable_and_true with
| Some most_recent_stable_and_true ->
let%arr most_recent_stable, must_be_true = most_recent_stable_and_true
and is_stable = is_stable
and input = input in
(match must_be_true with
| true -> ()
| false ->
eprint_s [%message "BUG:" [%here] "value which passed through filter must be true"]);
if M.equal input most_recent_stable && is_stable
then Stability.Stable input
else Unstable { previously_stable = Some most_recent_stable; unstable_value = input }
| None ->
let%arr input = input in
Stability.Unstable { previously_stable = None; unstable_value = input }
;;
module One_at_a_time = struct
module Status = struct
type t =
| Busy
| Idle
[@@deriving sexp, equal]
end
module Response = struct
type 'a t =
| Result of 'a
| Busy
[@@deriving sexp]
end
module Lock_action = struct
type t =
| Acquire
| Release
[@@deriving sexp]
end
let effect f =
let%sub status, inject_status =
Bonsai.actor0
(module Status)
(module Lock_action)
~default_model:Idle
~recv:(fun ~schedule_event:_ model action ->
match action with
| Acquire ->
let response =
match model with
| Busy -> false
| Idle -> true
in
Busy, response
| Release -> Idle, true)
in
let%sub effect =
let%arr inject_status = inject_status
and f = f in
let open Effect.Let_syntax in
fun query ->
match%bind inject_status Acquire with
| false -> return Response.Busy
| true ->
let%bind result = f query in
let%map (_ : bool) = inject_status Release in
Response.Result result
in
return (Value.both effect status)
;;
end