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
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
open! Core
open Bonsai.For_open
open Bonsai.Let_syntax
let with_inject_fixed_point f =
let%sub r, _ =
Bonsai.wrap
()
~sexp_of_model:[%sexp_of: Unit.t]
~default_model:()
~equal:[%equal: Unit.t]
~apply_action:(fun context (_result, inject) () action ->
Bonsai.Apply_action_context.schedule_event context (inject action))
~f:(fun _model inject -> f inject)
in
return r
;;
let with_self_effect
(type a)
?sexp_of_model
?equal
~(f : a Bonsai.Computation_status.t Effect.t Value.t -> a Computation.t)
()
: a Computation.t
=
Bonsai.wrap
()
?sexp_of_model:(Option.map ~f:Option.sexp_of_t sexp_of_model)
?equal:(Option.map ~f:Option.equal equal)
~default_model:None
~apply_action:(fun (_ : _ Bonsai.Apply_action_context.t) 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 a)
?sexp_of_action
?sexp_of_model
?equal
~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 apply_action context input model action =
match input with
| Bonsai.Computation_status.Active (input, model_creator) ->
let model = model_creator model in
Some (apply_action context input model action)
| Inactive ->
let sexp_of_action = Option.value sexp_of_action ~default:sexp_of_opaque in
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:(sexp_of_action action : Sexp.t)];
model
in
let%sub model_and_inject =
Bonsai.state_machine1
?sexp_of_model:(Option.map ~f:Option.sexp_of_t sexp_of_model)
?sexp_of_action
?equal:(Option.map ~f:Option.equal equal)
~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
?sexp_of_action
?sexp_of_model
?equal
~model
~apply_action
()
=
let apply_action context () model action = apply_action context model action in
state_machine1_dynamic_model
?sexp_of_action
?sexp_of_model
?equal
~model
~apply_action
(Value.return ())
;;
let state_dynamic_model (type m) ?sexp_of_model ?equal ~model () =
let apply_action (_ : _ Bonsai.Apply_action_context.t) _old_model new_model =
new_model
in
state_machine0_dynamic_model
?sexp_of_action:sexp_of_model
?sexp_of_model
?equal
~model
~apply_action
()
;;
let exactly_once effect =
let%sub has_run, set_has_run =
Bonsai.state ~equal:[%equal: Bool.t] false ~sexp_of_model:[%sexp_of: Bool.t]
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 ?sexp_of_model ?equal effect =
let%sub value, set_value = Bonsai.state_opt ?sexp_of_model ?equal () 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 ?sexp_of_model ?equal value =
let%sub state, set_state = Bonsai.state_opt () ?sexp_of_model ?equal 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
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
()
~sexp_of_model:[%sexp_of: Model.t]
~equal:[%equal: Model.t]
~sexp_of_action:[%sexp_of: Action.t]
~default_model:Model.default
~apply_action:(fun context 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) ->
Bonsai.Apply_action_context.schedule_event
context
(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) ->
Bonsai.Apply_action_context.schedule_event
context
(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 ->
Effect.Expert.handle (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
~sexp_of_model:[%sexp_of: T.t]
~equal:[%equal: T.t]
~sexp_of_action:[%sexp_of: Unit.t]
~default_model:T.zero
~recv:(fun ~inject:_ ~schedule_event:_ i () -> T.( + ) i T.one, i)
()
in
fetch ()
;;
end
let mirror'
(type m)
?sexp_of_model
~equal
~(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 M = struct
type t = m
let sexp_of_t = Option.value ~default:sexp_of_opaque sexp_of_model
end
in
let module M2 = struct
type model = M.t
let equal_model = equal
let sexp_of_model = M.sexp_of_t
type t =
{ store : model option
; interactive : model option
}
[@@deriving sexp_of, 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 Option.equal equal 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 (Option.equal equal old_store_value store_value) in
let interactive_changed =
not (Option.equal equal 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'
~sexp_of_model:[%sexp_of: M2.t]
~equal:[%equal: M2.t]
(let%map store = store_value
and interactive = interactive_value in
{ M2.store; interactive })
~callback
;;
let mirror
?sexp_of_model
~equal
~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'
()
?sexp_of_model
~equal
~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 =
let%sub sign =
let%arr time_to_stable = time_to_stable in
Time_ns.Span.sign time_to_stable
in
match%sub sign with
| Neg ->
let on_activate =
Value.return
(Effect.of_thunk (fun () ->
eprint_s
[%message "Bonsai_extra.is_stable: [time_to_stable] should not be negative"]))
in
let%sub () = Bonsai.Edge.lifecycle ~on_activate () in
Bonsai.const true
| Zero -> Bonsai.const true
| 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
and time_to_stable = time_to_stable 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 ?sexp_of_model ~equal input ~condition =
Bonsai.most_recent_some ?sexp_of_model ~equal 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
;;
let prefer_stable_value = function
| Stable a -> a
| Unstable { previously_stable; unstable_value } ->
(match previously_stable with
| Some a -> a
| None -> unstable_value)
;;
end
let value_stability (type a) ?sexp_of_model ~equal:input_equal input ~time_to_stable =
let module M = struct
type t = a
let sexp_of_t = Option.value ~default:sexp_of_opaque sexp_of_model
end
in
let%sub is_stable = is_stable ~equal:input_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
let module M = struct
include M
let equal = input_equal
end
in
most_recent_value_satisfying
~sexp_of_model:[%sexp_of: M.t * bool]
~equal:[%equal: M.t * bool]
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 input_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
()
~sexp_of_model:[%sexp_of: Status.t]
~equal:[%equal: Status.t]
~sexp_of_action:[%sexp_of: Lock_action.t]
~default_model:Idle
~recv:(fun ~inject:_ ~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
let bonk =
let%sub (), bonk =
Bonsai.state_machine0
~default_model:()
~apply_action:(fun context () effect ->
Bonsai.Apply_action_context.schedule_event context effect)
()
in
return bonk
;;
let chain_incr_effects input =
let%sub (), inject =
Bonsai.state_machine1
input
~default_model:()
~apply_action:(fun ctx input _model effect_fns ->
match input, effect_fns with
| Bonsai.Computation_status.Inactive, _ | _, [] -> ()
| Active input, effect_fn :: dependents ->
let effect =
let%bind.Ui_effect () = effect_fn input in
Bonsai.Apply_action_context.inject ctx dependents
in
Bonsai.Apply_action_context.schedule_event ctx effect)
in
return inject
;;