package incremental

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

Source file types.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
(* This module has a giant [module rec] defining all the mutually recursive types used
   in the implementation.  The definition of each type is then repeated in its file;
   comments belong there, not here.

   We put just enough sexp converters here to display node ids.  The detailed sexp
   converters are generated via [with sexp] in the individual files.  Defining the sexp
   converters outside of the [module rec] makes it impossible to accidentally introduce a
   sexp converter that would try to produce an infinite sexp.
*)

open Core_kernel
open Import

module rec Adjust_heights_heap : sig
  type t =
    { mutable length : int
    ; mutable height_lower_bound : int
    ; mutable max_height_seen : int
    ; mutable nodes_by_height : Node.Packed.t Uopt.t array
    }
end =
  Adjust_heights_heap

and Alarm : sig
  type t = Alarm_value.t Timing_wheel.Alarm.t
end =
  Alarm

and Alarm_value : sig
  module Action : sig
    type t =
      | At of At.t
      | At_intervals of At_intervals.t
      | Snapshot : _ Snapshot.t -> t
      | Step_function : _ Step_function_node.t -> t
  end

  type t =
    { action : Action.t
    ; mutable next_fired : t Uopt.t
    }
end =
  Alarm_value

and Array_fold : sig
  type ('a, 'acc) t =
    { init : 'acc
    ; f : 'acc -> 'a -> 'acc
    ; children : 'a Node.t array
    }
end =
  Array_fold

and At : sig
  type t =
    { main : Before_or_after.t Node.t
    ; at : Time_ns.t
    ; mutable alarm : Alarm.t
    ; clock : Clock.t
    }
end =
  At

and At_intervals : sig
  type t =
    { main : unit Node.t
    ; base : Time_ns.t
    ; interval : Time_ns.Span.t
    ; mutable alarm : Alarm.t
    ; clock : Clock.t
    }
end =
  At_intervals

and Bind : sig
  type ('a, 'b) t =
    { main : 'b Node.t
    ; mutable f : 'a -> 'b Node.t
    ; lhs : 'a Node.t
    ; lhs_change : unit Node.t
    ; mutable rhs : 'b Node.t Uopt.t
    ; mutable rhs_scope : Scope.t
    ; mutable all_nodes_created_on_rhs : Node.Packed.t Uopt.t
    }
end =
  Bind

and Clock : sig
  type t =
    { timing_wheel : Alarm_value.t Timing_wheel.t
    ; now : Time_ns.t Var.t
    ; handle_fired : Alarm.t -> unit
    ; mutable fired_alarm_values : Alarm_value.t Uopt.t
    }
end =
  Clock

and Expert : sig
  type 'a edge =
    { child : 'a Node.t
    ; on_change : 'a -> unit
    ; mutable index : int Uopt.t
    }

  type packed_edge = E : 'a edge -> packed_edge [@@unboxed]

  type 'a t =
    { f : unit -> 'a
    ; on_observability_change : is_now_observable:bool -> unit
    ; mutable children : packed_edge Uopt.t Array.t
    ; mutable num_children : int
    ; mutable force_stale : bool
    ; mutable num_invalid_children : int
    ; mutable will_fire_all_callbacks : bool
    }
end =
  Expert

and Freeze : sig
  type 'a t =
    { main : 'a Node.t
    ; child : 'a Node.t
    ; only_freeze_when : 'a -> bool
    }
end =
  Freeze

and If_then_else : sig
  type 'a t =
    { main : 'a Node.t
    ; test : bool Node.t
    ; test_change : unit Node.t
    ; mutable current_branch : 'a Node.t Uopt.t
    ; then_ : 'a Node.t
    ; else_ : 'a Node.t
    }
end =
  If_then_else

and Internal_observer : sig
  module State : sig
    type t =
      | Created
      | In_use
      | Disallowed
      | Unlinked
  end

  type 'a t =
    { mutable state : State.t
    ; observing : 'a Node.t
    ; mutable on_update_handlers : 'a On_update_handler.t list
    ; mutable prev_in_all : Internal_observer.Packed.t Uopt.t
    ; mutable next_in_all : Internal_observer.Packed.t Uopt.t
    ; mutable prev_in_observing : 'a t Uopt.t
    ; mutable next_in_observing : 'a t Uopt.t
    }

  type 'a internal_observer = 'a t

  module Packed : sig
    type t = T : _ internal_observer -> t [@@unboxed]
  end
end =
  Internal_observer

and Join : sig
  type 'a t =
    { main : 'a Node.t
    ; lhs : 'a Node.t Node.t
    ; lhs_change : unit Node.t
    ; mutable rhs : 'a Node.t Uopt.t
    }
end =
  Join

and Kind : sig
  type 'a t =
    | Array_fold : (_, 'a) Array_fold.t -> 'a t
    | At : At.t -> Before_or_after.t t
    | At_intervals : At_intervals.t -> unit t
    | Bind_lhs_change : (_, _) Bind.t -> unit t
    | Bind_main : (_, 'a) Bind.t -> 'a t
    | Const of 'a
    | Expert of 'a Expert.t
    | Freeze of 'a Freeze.t
    | If_test_change : _ If_then_else.t -> unit t
    | If_then_else of 'a If_then_else.t
    | Invalid
    | Join_lhs_change : _ Join.t -> unit t
    | Join_main of 'a Join.t
    | Map : ('a1 -> 'a) * 'a1 Node.t -> 'a t
    | Snapshot of 'a Snapshot.t
    | Step_function of 'a Step_function_node.t
    | Uninitialized
    | Unordered_array_fold : (_, 'a) Unordered_array_fold.t -> 'a t
    | Var of 'a Var.t
    | Map2 : ('a1 -> 'a2 -> 'a) * 'a1 Node.t * 'a2 Node.t -> 'a t
    | Map3 : ('a1 -> 'a2 -> 'a3 -> 'a) * 'a1 Node.t * 'a2 Node.t * 'a3 Node.t -> 'a t
    | Map4 :
        ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        -> 'a t
    | Map5 :
        ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        -> 'a t
    | Map6 :
        ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        -> 'a t
    | Map7 :
        ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        -> 'a t
    | Map8 :
        ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 -> 'a8 -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        -> 'a t
    | Map9 :
        ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 -> 'a8 -> 'a9 -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        * 'a9 Node.t
        -> 'a t
    | Map10 :
        ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 -> 'a8 -> 'a9 -> 'a10 -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        * 'a9 Node.t
        * 'a10 Node.t
        -> 'a t
    | Map11 :
        ('a1
         -> 'a2
         -> 'a3
         -> 'a4
         -> 'a5
         -> 'a6
         -> 'a7
         -> 'a8
         -> 'a9
         -> 'a10
         -> 'a11
         -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        * 'a9 Node.t
        * 'a10 Node.t
        * 'a11 Node.t
        -> 'a t
    | Map12 :
        ('a1
         -> 'a2
         -> 'a3
         -> 'a4
         -> 'a5
         -> 'a6
         -> 'a7
         -> 'a8
         -> 'a9
         -> 'a10
         -> 'a11
         -> 'a12
         -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        * 'a9 Node.t
        * 'a10 Node.t
        * 'a11 Node.t
        * 'a12 Node.t
        -> 'a t
    | Map13 :
        ('a1
         -> 'a2
         -> 'a3
         -> 'a4
         -> 'a5
         -> 'a6
         -> 'a7
         -> 'a8
         -> 'a9
         -> 'a10
         -> 'a11
         -> 'a12
         -> 'a13
         -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        * 'a9 Node.t
        * 'a10 Node.t
        * 'a11 Node.t
        * 'a12 Node.t
        * 'a13 Node.t
        -> 'a t
    | Map14 :
        ('a1
         -> 'a2
         -> 'a3
         -> 'a4
         -> 'a5
         -> 'a6
         -> 'a7
         -> 'a8
         -> 'a9
         -> 'a10
         -> 'a11
         -> 'a12
         -> 'a13
         -> 'a14
         -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        * 'a9 Node.t
        * 'a10 Node.t
        * 'a11 Node.t
        * 'a12 Node.t
        * 'a13 Node.t
        * 'a14 Node.t
        -> 'a t
    | Map15 :
        ('a1
         -> 'a2
         -> 'a3
         -> 'a4
         -> 'a5
         -> 'a6
         -> 'a7
         -> 'a8
         -> 'a9
         -> 'a10
         -> 'a11
         -> 'a12
         -> 'a13
         -> 'a14
         -> 'a15
         -> 'a)
        * 'a1 Node.t
        * 'a2 Node.t
        * 'a3 Node.t
        * 'a4 Node.t
        * 'a5 Node.t
        * 'a6 Node.t
        * 'a7 Node.t
        * 'a8 Node.t
        * 'a9 Node.t
        * 'a10 Node.t
        * 'a11 Node.t
        * 'a12 Node.t
        * 'a13 Node.t
        * 'a14 Node.t
        * 'a15 Node.t
        -> 'a t
end =
  Kind

and Node : sig
  type 'a t =
    { id : Node_id.t
    ; state : State.t
    ; mutable recomputed_at : Stabilization_num.t
    ; mutable value_opt : 'a Uopt.t
    ; mutable kind : 'a Kind.t
    ; mutable cutoff : 'a Cutoff.t
    ; mutable changed_at : Stabilization_num.t
    ; mutable num_on_update_handlers : int
    ; mutable num_parents : int
    ;
      mutable parent1_and_beyond : Node.Packed.t Uopt.t array
    ; mutable parent0 : Node.Packed.t Uopt.t
    ; mutable created_in : Scope.t
    ; mutable next_node_in_same_scope : Node.Packed.t Uopt.t
    ; mutable height : int
    ; mutable height_in_recompute_heap : int
    ; mutable prev_in_recompute_heap : Node.Packed.t Uopt.t
    ; mutable next_in_recompute_heap : Node.Packed.t Uopt.t
    ; mutable height_in_adjust_heights_heap : int
    ; mutable next_in_adjust_heights_heap : Node.Packed.t Uopt.t
    ; mutable old_value_opt : 'a Uopt.t
    ; mutable observers : 'a Internal_observer.t Uopt.t
    ; mutable is_in_handle_after_stabilization : bool
    ; mutable on_update_handlers : 'a On_update_handler.t list
    ; mutable my_parent_index_in_child_at_index : int array
    ; mutable my_child_index_in_parent_at_index : int array
    ; mutable force_necessary : bool
    ; mutable user_info : Info.t option
    ; creation_backtrace : Backtrace.t option
    }
  [@@deriving sexp_of]

  module Packed : sig
    type 'a node = 'a t
    type t = T : _ node -> t [@@deriving sexp_of] [@@unboxed]
  end

  val is_valid : _ t -> bool
  val is_necessary : _ t -> bool
  val type_equal_if_phys_same : 'a t -> 'b t -> ('a, 'b) Type_equal.t option
end = struct
  include (
    Node :
      module type of struct
      include Node
    end
    with module Packed := Node.Packed)

  let sexp_of_t _ t = concat [ "n"; Node_id.to_string t.id ] |> [%sexp_of: string]

  module Packed = struct
    type 'a node = 'a t [@@deriving sexp_of]
    type t = T : _ node -> t [@@unboxed]

    let sexp_of_t (T t) = t |> [%sexp_of: _ node]
  end

  let is_valid t =
    match t.kind with
    | Invalid -> false
    | _ -> true
  ;;

  (* [is_necessary] is defined here because we need it before node.ml is available.  It is
     used during graph manipulation, and so is written with some care to be fast. *)
  let is_necessary t =
    t.num_parents > 0
    || Uopt.is_some t.observers
    || (match t.kind with
      | Freeze _ -> true
      | _ -> false)
    || t.force_necessary
  ;;

  let t_is_injective (type a b) (T : (a t, b t) Type_equal.t) : (a, b) Type_equal.t = T

  let type_equal_if_phys_same (type a b) (t1 : a t) (t2 : b t) =
    (* This is type-safe assuming no one can give the same incremental node two different
       types.  This is true because the field [mutable old_value_opt : 'a option] prevents
       both subtyping and parameteric polymorphism.  But this allows to break
       abstractions, as in someone could write:

       {[
         type t
         type u (* = t underneath *)
         val create : unit -> t Incr.t * u Incr.t (* the two incrementals are phys_equal *)
       ]}

       and we would figure out that type t = u.  However, we could add a Type_equal.Id to
       nodes and do the same, so it seems to be a more general issue. *)
    if phys_same t1 t2
    then Some (t_is_injective (Obj.magic (T : _ Type_equal.t) : (a t, b t) Type_equal.t))
    else None
  ;;
end

and Observer : sig
  type 'a t = 'a Internal_observer.t ref
end =
  Observer

and Only_in_debug : sig
  type t =
    { mutable currently_running_node : Node.Packed.t option
    ; mutable expert_nodes_created_by_current_node : Node.Packed.t list
    }
end =
  Only_in_debug

and Packed_weak_hashtbl : sig
  type t = T : (_, _) Weak_hashtbl.t -> t
end =
  Packed_weak_hashtbl

and Recompute_heap : sig
  type t =
    { mutable length : int
    ; mutable height_lower_bound : int
    ; mutable nodes_by_height : Node.Packed.t Uopt.t array
    }
end =
  Recompute_heap

and Run_on_update_handlers : sig
  type t = T : 'a Node.t * 'a On_update_handler.Node_update.t -> t
end =
  Run_on_update_handlers

and Scope : sig
  type t =
    | Top
    | Bind : (_, _) Bind.t -> t
  [@@deriving sexp_of]
end = struct
  type t =
    | Top
    | Bind : (_, _) Bind.t -> t

  let sexp_of_t = function
    | Top -> "Top" |> [%sexp_of: string]
    | Bind bind -> bind.main |> [%sexp_of: _ Node.t]
  ;;
end

and Snapshot : sig
  type 'a t =
    { main : 'a Node.t
    ; at : Time_ns.t
    ; before : 'a
    ; value_at : 'a Node.t
    ; clock : Clock.t
    }
end =
  Snapshot

and State : sig
  type t =
    { mutable status : Status.t
    ; bind_lhs_change_should_invalidate_rhs : bool
    ; mutable stabilization_num : Stabilization_num.t
    ; mutable current_scope : Scope.t
    ; recompute_heap : Recompute_heap.t
    ; adjust_heights_heap : Adjust_heights_heap.t
    ; propagate_invalidity : Node.Packed.t Stack.t
    ; mutable num_active_observers : int
    ; mutable all_observers : Internal_observer.Packed.t Uopt.t
    ; finalized_observers : Internal_observer.Packed.t Thread_safe_queue.t
    ; new_observers : Internal_observer.Packed.t Stack.t
    ; disallowed_observers : Internal_observer.Packed.t Stack.t
    ; set_during_stabilization : Var.Packed.t Stack.t
    ; handle_after_stabilization : Node.Packed.t Stack.t
    ; run_on_update_handlers : Run_on_update_handlers.t Stack.t
    ; mutable only_in_debug : Only_in_debug.t
    ; weak_hashtbls : Packed_weak_hashtbl.t Thread_safe_queue.t
    ; mutable keep_node_creation_backtrace : bool
    ; mutable num_nodes_became_necessary : int
    ; mutable num_nodes_became_unnecessary : int
    ; mutable num_nodes_changed : int
    ; mutable num_nodes_created : int
    ; mutable num_nodes_invalidated : int
    ; mutable num_nodes_recomputed : int
    ; mutable num_nodes_recomputed_directly_because_one_child : int
    ; mutable num_nodes_recomputed_directly_because_min_height : int
    ; mutable num_var_sets : int
    }
end =
  State

and Status : sig
  type t =
    | Stabilizing
    | Running_on_update_handlers
    | Not_stabilizing
    | Stabilize_previously_raised of Raised_exn.t
end =
  Status

and Step_function_node : sig
  type 'a t =
    { main : 'a Node.t
    ; mutable child : 'a Step_function.t Node.t Uopt.t
    ; mutable extracted_step_function_from_child_at : Stabilization_num.t
    ; mutable value : 'a Uopt.t
    ; mutable upcoming_steps : (Time_ns.t * 'a) Sequence.t
    ; mutable alarm : Alarm.t
    ; mutable alarm_value : Alarm_value.t
    ; clock : Clock.t
    }
end =
  Step_function_node

and Unordered_array_fold : sig
  type ('a, 'acc) t =
    { main : 'acc Node.t
    ; init : 'acc
    ; f : 'acc -> 'a -> 'acc
    ; update : 'acc -> old_value:'a -> new_value:'a -> 'acc
    ; full_compute_every_n_changes : int
    ; children : 'a Node.t array
    ; mutable fold_value : 'acc Uopt.t
    ; mutable num_changes_since_last_full_compute : int
    }
end =
  Unordered_array_fold

and Var : sig
  type 'a t =
    { mutable value : 'a
    ; mutable value_set_during_stabilization : 'a Uopt.t
    ; mutable set_at : Stabilization_num.t
    ; watch : 'a Node.t
    }

  type 'a var := 'a t

  module Packed : sig
    type t = T : _ var -> t [@@unboxed]
  end
end =
  Var
OCaml

Innovation. Community. Security.