package MlFront_Thunk

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

Source file BuildConstraints.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
(** An OCaml embedding of functors, applicatives and monads described in "Build
    systems à la carte: Theory and practice".

    This embedding is not necessary for MlFront thunks; we can implement thunks
    without using a generalized framework! However, this embedding may aid
    modifications to thunk implementations.

    Confer
    {{:https://www.cambridge.org/core/services/aop-cambridge-core/content/view/097CE52C750E69BD16B78C318754C7A4/S0956796820000088a.pdf/build-systems-a-la-carte-theory-and-practice.pdf}3.4
     The need for polymorphism in task}, although we abandon Haskell-style
    polymorphism in OCaml since we can use a distinct module functor (with
    distinct applicative/monad) for each build system.

    {v
Andrey Mokhov, Neil Mitchell, and Simon Peyton Jones. 2018. Build Systems à la Carte. Proc. ACM Program. Lang. 2, ICFP, Article 79 (September 2018) 
doi:10.1017/S0956796820000088
    v} *)

module type FUNCTOR = sig
  type 'a t

  val map : ('a -> 'b) -> 'a t -> 'b t

  (* We didn't add [mapConst] since it is an optimization and it is subsumed by [pure] in PURE_SEQ
       when we restrict ourselves to Applicatives and Monads.
       Confer https://lean-lang.org/doc/reference/4.19.0/Functors___-Monads-and--do--Notation/ *)
end

module type MONOID = sig
  type t

  val identity : t
  (** [identity] is the monoid identity value where the equalities
      [identity • a = a] and [a • identity = a] hold. *)

  val mappend : t -> t -> t
  (** [mappend a b] is the monoid binary operation [•] where the associativity
      equation [(a • b) • c = a • (b • c)] holds. *)
end

(** A set of [Pure], [Functor], [SeqLeft], and [SeqRight] operations that are
    common to Applicative Functors and Monads.

    You should prefer the operations that are specific to Applicative Functors
    in {!APPLICATIVE} or the operations specific to Monads in {!MONAD}.

    Confer
    {{:https://lean-lang.org/doc/reference/4.19.0/Functors___-Monads-and--do--Notation}Functors,
     Monads and do-Notation}*)
module type PURE_SEQ = sig
  include FUNCTOR

  val pure : 'a -> 'a t
  val seq : ('a -> 'b) t -> (unit -> 'a t) -> 'b t
  val seq_left : 'a t -> (unit -> 'b t) -> 'a t
  val seq_right : 'a t -> (unit -> 'b t) -> 'b t
end

module PureSeqSyntax (PS : PURE_SEQ) = struct
  (* Symbols from https://lean-lang.org/doc/reference/4.19.0/Functors___-Monads-and--do--Notation *)

  let ( <$> ) = PS.map
  let ( <*> ) x f = PS.seq f x
  let ( <* ) x f = PS.seq_left f x
  let ( *> ) x f = PS.seq_right f x
end

(** {1 Applicatives} *)

module type APPLICATIVE = sig
  include FUNCTOR

  val pure : 'a -> 'a t
  val apply : ('a -> 'b) t -> 'a t -> 'b t
end

module ApplicativeSyntax (F : APPLICATIVE) = struct
  (* Symbols from https://dl.acm.org/doi/pdf/10.1145/3341694 *)

  let ( <$> ) = F.map
  let ( <*> ) = F.apply
end

module ApplicativeWithPureSeq (F : APPLICATIVE) : sig
  include APPLICATIVE with type 'a t = 'a F.t
  include PURE_SEQ with type 'a t := 'a t
end = struct
  type 'a t = 'a F.t

  let apply = F.apply

  (** PURE_SEQ definitions come from
      {{:https://lean-lang.org/functional_programming_in_lean/Functors___-Applicative-Functors___-and-Monads/The-Complete-Definitions/}Functional
       Programming in Lean} *)

  let pure = F.pure
  let map (type a) (type b) (f : a -> b) (x : a t) = F.apply (pure f) x

  let seq (type a) (type b) (f : (a -> b) t) (x : unit -> a t) : b t =
    F.apply f (x ())

  let seq_left (type a) (type b) (x : a t) (y : unit -> b t) =
    seq (map (fun a _b -> a) x) y

  let seq_right (type a) (type b) (x : a t) (y : unit -> b t) =
    seq (map (fun _a b -> b) x) y
end

(** The [Const] applicative ignores the second type parameter (ex. the value
    type) but accumulates the first type parameter (ex. the key type) with a
    monoid specified as the functor parameter.

    It is described in
    {{:https://hackage.haskell.org/package/base-4.21.0.0/docs/Data-Functor-Const.html}Const
     functor}.

    Use {!const} rather than {!pure} if you need to introduce a value of the
    first type parameter. *)
module ConstApplicativeWithPureSeq (Monoid : sig
  include MONOID

  type const

  val const : const -> t
end) =
struct
  include ApplicativeWithPureSeq (struct
    type 'a t = Monoid.t

    let pure v =
      ignore v;
      Monoid.identity

    let map (type a) (type b) (f : a -> b) (x : a t) =
      ignore f;
      x

    let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
      Monoid.mappend x f
  end)

  type const = Monoid.const

  (** [const k] is the monoid constant [k]. *)
  let const = Monoid.const
end

(** {1 Monads}

    {2 Lifting Monads into Applicatives}

    The order of evaluation is (f state) then (x (f state)) rather than (x
    state) then (f (x state)).

    Why? Haskell has two operators for apply ...

    <*>
    https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:-60--42--62-
    (f state) then (x (f state))

    <**>
    https://hackage.haskell.org/package/base-4.14.0.0/docs/Control-Applicative.html#v:-60--42--42--62-
    (f state) then (x (f state))

    Since [apply] is a synomym for [<*>] we chose (f state) then (x (f state)).

    Confer: https://blog.plover.com/prog/haskell/how-to-ap.html

    Conforming example: https://kndrck.co/posts/peeking_into_state_monads/.

    {2 Definitions} *)

module type MONAD = sig
  include FUNCTOR
  include APPLICATIVE with type 'a t := 'a t

  val return : 'a -> 'a t
  val bind : 'a t -> ('a -> 'b t) -> 'b t
end

module MonadSyntax (F : MONAD) = struct
  let ( <$> ) = F.map
  let ( >>= ) = F.bind
end

module MonadLetSyntax (F : MONAD) = struct
  let ( let* ) = F.bind
end

(** {2 Writer Monads} *)

module type WRITER = sig
  type output
end

module type WRITER_IMPL = sig
  include WRITER

  val empty : output
  val append : output -> output -> output
end

module type MUTABLE_WRITER_ASYNC = sig
  type journal
  type journal_entry_id
  type journal_entry_value
  type 'a promise
end

module type MUTABLE_WRITER_ASYNC_IMPL = sig
  include MUTABLE_WRITER_ASYNC

  val create_empty : unit -> journal

  val new_journal_entry :
    journal_entry_value -> journal_entry_id * journal_entry_value

  val compare_journal_entry_id : journal_entry_id -> journal_entry_id -> int

  val update :
    journal ->
    (journal_entry_id * journal_entry_value) UniqueInsertionList.t ->
    unit
  (** [update journal entries] updates the journal [journal] with the entries
      [entries].

      Each journal entry should have its own {!journal_entry_id} from
      {!new_journal_entry}.

      After the update the journal entries are no longer needed. *)

  val reconcile : dest:journal -> journal -> unit
  (** [reconcile ~dest:journal_dest journal_src] merges and resolves journal
      entry conflicts between journal [journal_dest] and journal [journal_src],
      placing the merged results in [journal_dest]. *)

  val flush : journal -> journal promise
  (** [flush journal] saves any buffer cache of the journal to disk or remote
      storage. *)
end

(** The type of Writer Monads. *)
module type MONAD_WRITER = sig
  include WRITER
  include MONAD with type 'a t = 'a * output

  val tell : output -> unit t
  val run_writer : 'a t -> 'a * output
end

module type MONAD_WRITER_ASYNC = sig
  type journal
  type journal_entry_id
  type journal_entry_value
  type 'a promise

  include
    MONAD
      with type 'a t =
        'a * (journal_entry_id * journal_entry_value) UniqueInsertionList.t

  val post_entry : journal_entry_value -> unit t
end

(** The type of Writer Monads transformers. *)
module type MONAD_WRITER_T = sig
  include WRITER
  module LiftedIntoWriter : MONAD
  include MONAD with type 'a t = ('a * output) LiftedIntoWriter.t

  val tell : output -> unit t
  val run_writer : 'a t -> ('a * output) LiftedIntoWriter.t
  val lift : 'a LiftedIntoWriter.t -> 'a t
end

module type MONAD_WRITER_ASYNC_T = sig
  include MUTABLE_WRITER_ASYNC
  module LiftedIntoWriter : MONAD

  include
    MONAD
      with type 'a t =
        ('a
        * journal
        * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
        LiftedIntoWriter.t

  val post_entry : journal_entry_value -> unit t
  val sync_writer : 'a t -> 'a t
  val lift : 'a LiftedIntoWriter.t -> 'a t
end

(** Writer Monad transformer. *)
module MonadWriterT (S : sig
  include WRITER_IMPL
  module LiftedIntoWriter : MONAD
end) :
  MONAD_WRITER_T
    with type output = S.output
     and module LiftedIntoWriter = S.LiftedIntoWriter = struct
  type output = S.output

  module LiftedIntoWriter = S.LiftedIntoWriter

  type 'a t = ('a * output) S.LiftedIntoWriter.t

  let pure a = S.LiftedIntoWriter.pure (a, S.empty)
  let return = pure

  let map (type a) (type b) (f : a -> b) (x : a t) : b t =
    let ( let* ) = S.LiftedIntoWriter.bind in
    let* a, output' = x in
    S.LiftedIntoWriter.return (f a, output')

  let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
    (* REMEMBER: Our odoc comments say order of evaluation is (f state) then (x (f state))
         rather than (x state) then (f (x state)). *)
    let ( let* ) = S.LiftedIntoWriter.bind in
    let* f_a_b, f_output = f in
    let* a, a_output = x in
    S.LiftedIntoWriter.return (f_a_b a, S.append f_output a_output)

  let bind (type a) (type b) (m : a t) (f : a -> b t) : b t =
    let ( let* ) = S.LiftedIntoWriter.bind in
    let* a, m_output = m in
    let* b, f_output = f a in
    S.LiftedIntoWriter.return (b, S.append m_output f_output)

  let tell o = S.LiftedIntoWriter.return ((), o)

  let lift m =
    S.LiftedIntoWriter.bind m (fun x -> S.LiftedIntoWriter.return (x, S.empty))

  let run_writer m = m
end

(** Writer Async Monad transformer. *)
module MonadWriterAsyncT (WA : sig
  include MUTABLE_WRITER_ASYNC_IMPL
  module LiftedIntoWriter : MONAD
end) : sig
  include
    MONAD_WRITER_ASYNC_T
      with type journal = WA.journal
       and type journal_entry_id = WA.journal_entry_id
       and type journal_entry_value = WA.journal_entry_value
       and type 'a promise = 'a WA.promise
       and module LiftedIntoWriter = WA.LiftedIntoWriter

  val run_writer_async : 'a t -> ('a * journal) LiftedIntoWriter.t
end = struct
  type journal = WA.journal
  type journal_entry_id = WA.journal_entry_id
  type journal_entry_value = WA.journal_entry_value
  type 'a promise = 'a WA.promise

  module LiftedIntoWriter = WA.LiftedIntoWriter

  module JournalEntryOrd = struct
    type t = journal_entry_id * journal_entry_value

    let compare a b = WA.compare_journal_entry_id (fst a) (fst b)
  end

  let empty_journalentries = UniqueInsertionList.empty (module JournalEntryOrd)

  type 'a t =
    ('a
    * journal
    * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
    WA.LiftedIntoWriter.t

  let pure a =
    WA.LiftedIntoWriter.pure (a, WA.create_empty (), empty_journalentries)

  let return = pure

  let map (type a) (type b) (f : a -> b) (x : a t) : b t =
    let ( let* ) = WA.LiftedIntoWriter.bind in
    let* a, j', je' = x in
    WA.LiftedIntoWriter.return (f a, j', je')

  let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
    (* REMEMBER: Our odoc comments say order of evaluation is (f state) then (x (f state))
         rather than (x state) then (f (x state)). *)
    let ( let* ) = WA.LiftedIntoWriter.bind in
    let* f_a_b, f_j, f_je = f in
    let* a, a_j, a_je = x in
    WA.reconcile ~dest:f_j a_j;
    WA.LiftedIntoWriter.return
      (f_a_b a, f_j, UniqueInsertionList.append f_je a_je)

  let bind (type a) (type b) (m : a t) (f : a -> b t) : b t =
    let ( let* ) = WA.LiftedIntoWriter.bind in
    let* a, m_j, m_je = m in
    let* b, f_j, f_je = f a in
    WA.reconcile ~dest:m_j f_j;
    WA.LiftedIntoWriter.return (b, m_j, UniqueInsertionList.append m_je f_je)

  let post_entry value =
    let id, value = WA.new_journal_entry value in
    let je = UniqueInsertionList.add (id, value) empty_journalentries in
    let journal = WA.create_empty () in
    WA.update journal je;
    WA.LiftedIntoWriter.return ((), journal, empty_journalentries)

  let lift m =
    WA.LiftedIntoWriter.bind m (fun x ->
        WA.LiftedIntoWriter.return (x, WA.create_empty (), empty_journalentries))

  let sync_writer (type a) (m : a t) : a t =
    let ( let* ) = WA.LiftedIntoWriter.bind in
    let* a, j', je' = m in
    WA.update j' je';
    WA.LiftedIntoWriter.return (a, j', empty_journalentries)

  let run_writer_async (type a) (m : a t) : (a * journal) LiftedIntoWriter.t =
    WA.LiftedIntoWriter.map
      (fun (a, j', je') ->
        WA.update j' je';
        (a, j'))
      m
end

(** {2 State Monads} *)

(** Messages to asynchronously update states.

    The [StateMessage] contains an "message object" {!obj} which is a member of
    an extensible type called a "message class" {!cls}. The class is present for
    debugging, especially to find why an state message was not handled.

    The extensibility lets multiple monad implementations, especially monad
    transformations, have a unified way to communicate state updates. In other
    words, the extensibility is the primary way that async state monads compose.
*)
module StateMessage : sig
  type cls = private int
  type obj = ..
  type t = private Value : cls * obj -> t

  val new_class : string -> (cls, string * Printexc.raw_backtrace) result
  (** [new_class name where] makes a new class named [name].

      The [name] must be unique, and should have enough information for a
      developer to know where the class was created. For example, in MlFront use
      the fully qualified module id as part of the name. In conventional OCaml,
      use a combination of the findlib library name and module name.

      An error is returned if the class already exists. The error
      [(message, location)] will contain an error message and the location where
      the original class with the same name was created. *)

  val new_class_exn : string -> cls
  (** [new_class_exn name where] makes a new class named [name], raising an
      {!Invalid_argument} exception if the class has already been created.

      The [name] must be unique, and should have enough information for a
      developer to know where the class was created. For example, in MlFront use
      the fully qualified module id as part of the name. In conventional OCaml,
      use a combination of the findlib library name and module name.

      An {!Invalid_argument} exception is raised if the class already exists.
      The exception [(message, location)] will contain an error message and the
      location where the original class with the same name was created. *)

  val create : cls -> obj -> t
  (** [create cls obj] creates a state message of class [cls] with the object
      [obj]. *)

  val show : t -> string
  (** [show message] shows the name of the class that the state message
      [message] belongs to and the location where the class was created. *)

  val downcast : t -> cls -> obj option
  (** [downcast message cls] returns the object if and only if the message
      [message] belongs to the class [cls]. *)
end = struct
  type cls = int
  type obj = ..
  type t = Value : cls * obj -> t

  module ClassMap = Map.Make (String)
  module InverseClassMap = Map.Make (Int)

  type classinfo = { origin : Printexc.raw_backtrace; class_name : string }

  let classmap : classinfo ClassMap.t ref = ref ClassMap.empty
  let invclassmap : classinfo InverseClassMap.t ref = ref InverseClassMap.empty

  let new_class =
    let next_clazz_id = ref 1 in
    fun class_name ->
      match ClassMap.find_opt class_name !classmap with
      | Some { origin; class_name = _ } ->
          Error
            ( Printf.sprintf "The class `%s` was already created." class_name,
              origin )
      | None ->
          let origin : Printexc.raw_backtrace = Printexc.get_callstack 20 in
          let id = !next_clazz_id in
          next_clazz_id := id + 1;
          let info = { origin; class_name } in
          classmap := ClassMap.add class_name info !classmap;
          invclassmap := InverseClassMap.add id info !invclassmap;
          Ok id

  let new_class_exn class_name =
    match new_class class_name with
    | Ok v -> v
    | Error (message, origin) ->
        raise
          (Invalid_argument
             (Printf.sprintf "%s\nThe original class was created at:\n%s%!"
                message
                (Printexc.raw_backtrace_to_string origin)))

  let create cls obj = Value (cls, obj)

  let show : t -> string = function
    | Value (cls, _) ->
        let { class_name; origin } = InverseClassMap.find cls !invclassmap in
        let origin_string = Printexc.raw_backtrace_to_string origin in
        Printf.sprintf "A message of class `%s` created at:\n%s%!" class_name
          origin_string

  let downcast : t -> cls -> obj option = function
    | Value (cls, obj) -> fun cls' -> if cls = cls' then Some obj else None
end

module type STATE = sig
  type state
end

module type MUTABLE_STATE_ASYNC = sig
  include STATE

  type 'a promise
end

(** The implementation of state in an asynchronous environment.

    The state implementation must have atomic updates. *)
module type MUTABLE_STATE_ASYNC_IMPL = sig
  include MUTABLE_STATE_ASYNC

  val update : state -> StateMessage.t list -> unit
  (** [update state messages] synchronously and atomically updates the state
      [state] with the state messages [messages].

      After the update the messages are no longer needed.

      Only a local, in-memory cache of the state must have synchronous updates.
      If the state is remote, it is expected that the state implementation will
      maintain a local list of messages that have yet to be sent to the remote
      state service. The {!flush} function is provided to move the pending
      messages from the local state to the remote state service. *)

  val flush : state -> unit promise
  (** [flush state] saves any buffer cache of the state to disk or remote
      storage. *)
end

module type MONAD_STATE = sig
  include STATE
  include MONAD with type 'a t = state -> 'a * state

  val get : state t
  val gets : (state -> 'a) -> 'a t
  val put : state -> unit t
  val modify : (state -> state) -> unit t
  val run_state : 'a t -> state -> 'a * state
  val exec_state : 'a t -> state -> state
end

module type MONAD_STATE_ASYNC = sig
  type state
  type 'a promise

  include MONAD with type 'a t = (state -> 'a * StateMessage.t list) promise

  val get : state t
  val gets : (state -> 'a) -> 'a t
  val put : state -> unit t
  val post_msg : StateMessage.t list -> unit t
  val sync_state : 'a t -> 'a t
  val run_state_async : 'a t -> state -> ('a * state) promise
  val exec_state_async : 'a t -> state -> state promise
end

module MonadState (S : STATE) : MONAD_STATE with type state = S.state = struct
  type state = S.state
  type 'a t = state -> 'a * state

  let pure a (state : state) = (a, state)
  let return a (state : state) = (a, state)

  let map (type a) (type b) (f : a -> b) (x : a t) : b t =
   fun state ->
    let a, state' = x state in
    (f a, state')

  let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
   fun state ->
    (* REMEMBER: Our odoc comments say order of evaluation is (f state) then (x (f state))
         rather than (x state) then (f (x state)). *)
    let f_a_b, state' = f state in
    let a, state'' = x state' in
    (f_a_b a, state'')

  let bind (type a) (type b) (t : a t) (f : a -> b t) : b t =
   fun state ->
    let a, state' = t state in
    let b, state'' = f a state' in
    (b, state'')

  let get = fun state -> (state, state)
  let gets = fun read_state -> fun state -> (read_state state, state)
  let put s = fun (_ : state) -> ((), s)
  let modify = fun map_state -> fun state -> ((), map_state state)
  let run_state = fun (t : 'a t) -> fun (s : state) -> t s
  let exec_state = fun (t : 'a t) -> fun (s : state) -> snd (t s)
end

open struct
  (** [compose f g x] is [f (g x)] and is available in OCaml 5 as
      {!Fun.compose}. *)
  let compose f g x = f (g x)

  let fmap ~pure ~bind f xs =
    let ( >>= ) = bind in
    xs >>= fun x -> pure (f x)
end

module MonadStateWithPureSeq (S : STATE) : sig
  include
    MONAD_STATE
      with type state = S.state
       and type 'a t = S.state -> 'a * S.state

  include PURE_SEQ with type 'a t := S.state -> 'a * S.state

  val get : S.state t
  val gets : (S.state -> 'a) -> 'a t
  val put : S.state -> unit t
  val modify : (S.state -> S.state) -> unit t
  val run_state : 'a t -> S.state -> 'a * S.state
  val exec_state : 'a t -> S.state -> S.state
end = struct
  module M1 = MonadState (S)

  type state = S.state
  type 'a t = S.state -> 'a * S.state

  let return = M1.return
  let bind = M1.bind

  (** PURE_SEQ definitions come from
      {{:https://lean-lang.org/functional_programming_in_lean/Functors___-Applicative-Functors___-and-Monads/The-Complete-Definitions/}Functional
       Programming in Lean} *)

  let pure = M1.return
  let map (type a) (type b) (f : a -> b) (x : a t) = bind x (compose pure f)

  let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
    bind f (fun y -> map y x)

  let seq f x = bind f (fun y -> map y (x ()))

  let seq_left (type a) (type b) (x : a t) (y : unit -> b t) : a t =
    bind x (fun a -> bind (y ()) (fun _b -> pure a))

  let seq_right (type a) (type b) (x : a t) (y : unit -> b t) : b t =
    bind x (fun _a -> y ())

  let get = M1.get
  let gets = M1.gets
  let put = M1.put
  let modify = M1.modify
  let run_state = M1.run_state
  let exec_state = M1.exec_state
end

(** {2 Promise Monads} *)

module type PROMISE = sig
  type 'a promise
end

module type PROMISE_IMPL = sig
  include PROMISE

  val return_promise : 'a -> 'a promise
  val bind_promise : 'a promise -> ('a -> 'b promise) -> 'b promise
end

type 'resolved_promise_state universal_promise_state =
  | Pending
  | Resolved of 'resolved_promise_state
  | Rejected of exn
      (** The type ['resolved_promise_state universal_promise_state] is the set
          of states a promise can be in. *)

(** The type of promise monad.

    The promise monad is described in
    {{:https://courses.cs.cornell.edu/cs3110/2021sp/textbook/adv/promises.html}CS
     3110 - Functional Programming in OCaml}.

    Unlike other monads, there is no:

    {[
      val run_promise : unit t -> unit
      (** [run_promise promise] runs the promise [promise]. *)
    ]}

    That is because some promise implementations are control of the entire
    runtime or are part of the language (ex. effects in OCaml 5, await in JS,
    etc.). It is the users' responsibility to unpack the promise that works in
    whatever environment the program is running. *)
module type MONAD_PROMISE = sig
  type 'a t
  (** The type ['a t] is the set of (future) promises for values of type ['a].
  *)

  type 'a resolver
  (** The type ['a t] is the type of resolvers for pending promises of a value
      of type ['a]. *)

  type resolved_promise_state
  (** The type [resolved_promise_state] is the set of states a [Resolved]
      promise can be in. The set will be either type ['a] in {!t} or a reversed
      monadic transformation of type ['a] which you need to unwrap. In other
      words, the resolution has no knowledge of the transformations of the monad
      (nor should it!). *)

  include MONAD with type 'a t := 'a t

  val make_promise : unit -> 'a t * 'a resolver
  (** [make_promise ()] is a new promise and resolver. The promise is pending.
  *)

  val return : 'a -> 'a t
  (** [return x] is a new promise that is already resolved with value [x]. *)

  val promise_state : 'a t -> resolved_promise_state universal_promise_state
  (** [promise_state p] is the state of the promise *)

  val resolve : 'a resolver -> 'a -> unit
  (** [resolve r x] resolves the promise [p] associated with [r] with value [x],
      meaning that [state p] will become [Resolved x]. Requires: [p] is pending.
  *)

  val reject : 'a resolver -> exn -> unit
  (** [reject r x] rejects the promise [p] associated with [r] with exception
      [x], meaning that [state p] will become [Rejected x]. Requires: [p] is
      pending. *)

  val parallel : 'a t list -> 'a list t
  (** [parallel ps] is a promise that resolves when all the promises in [ps]
      resolve, or rejects when any promise in [ps] rejects. The order of the
      resolved values is the same as the order of the promises in [ps]. *)
end

(** The type of Promise Monads transformers. *)
module type MONAD_PROMISE_T = sig
  type 'a promise
  type 'a resolver
  type resolved_promise_state

  module LiftedIntoPromise : MONAD
  include MONAD with type 'a t = 'a LiftedIntoPromise.t promise

  val make_promise : unit -> 'a t * 'a resolver
  val return : 'a -> 'a t
  val promise_state : 'a t -> resolved_promise_state universal_promise_state
  val resolve : 'a resolver -> 'a -> unit
  val reject : 'a resolver -> exn -> unit
  val lift : 'a LiftedIntoPromise.t -> 'a t
  (* val run_promise : 'a t -> 'a LiftedIntoPromise.t promise *)
end

(** Promise Monad transformer. *)
module MonadPromiseT (S : sig
  include MONAD_PROMISE
  module LiftedIntoPromise : MONAD
end) :
  MONAD_PROMISE_T
    with type 'a promise = 'a S.t
     and type 'a resolver = ('a -> unit) * (exn -> unit)
     and type resolved_promise_state = S.resolved_promise_state
     and module LiftedIntoPromise = S.LiftedIntoPromise = struct
  type 'a promise = 'a S.t

  type 'a resolver = ('a -> unit) * (exn -> unit)
  (** Since we need to map the ['a resolver], and Lwt and maybe others do not
      offer the [map], we split up the resolver into a pair of [resolve] and
      [reject] functions. *)

  type resolved_promise_state = S.resolved_promise_state

  module LiftedIntoPromise = S.LiftedIntoPromise

  type 'a t = 'a S.LiftedIntoPromise.t promise

  let pure a = S.return @@ S.LiftedIntoPromise.pure a
  let return = pure

  let lifted_map (type a) (type b) (f : a -> b) (x : a LiftedIntoPromise.t) :
      b LiftedIntoPromise.t =
    fmap ~pure:LiftedIntoPromise.pure ~bind:LiftedIntoPromise.bind f x

  let promise_map (type a) (type b) (f : a -> b) (x : a S.t) : b S.t =
    fmap ~pure:S.return ~bind:S.bind f x

  let lifted_apply mf mx =
    let ( >>= ) = LiftedIntoPromise.bind in
    mf >>= fun f ->
    mx >>= fun x -> LiftedIntoPromise.return (f x)

  let map (type a) (type b) (f : a -> b) (x : a t) : b t =
    promise_map
      (fun (l_x : a LiftedIntoPromise.t) -> lifted_map (fun (a : a) -> f a) l_x)
      x

  let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
    (* REMEMBER: Our odoc comments say order of evaluation is (f state) then (x (f state))
       rather than (x state) then (f (x state)). *)
    let ( >>=- ) = S.bind in
    x >>=- fun (l_a : a LiftedIntoPromise.t) ->
    f >>=- fun (l_f : (a -> b) LiftedIntoPromise.t) ->
    let (l_b : b LiftedIntoPromise.t) = lifted_apply l_f l_a in
    S.return l_b

  let bind (type a) (type b) (m : a t) (f : a -> b t) : b t =
    let ( >>=- ) = S.bind in
    let ( >>=| ) = S.LiftedIntoPromise.bind in
    let (promise : b LiftedIntoPromise.t promise), resolver =
      S.make_promise ()
    in
    let (_ : unit LiftedIntoPromise.t promise) =
      m >>=- fun (l_a : a LiftedIntoPromise.t) ->
      let (l_b : _ LiftedIntoPromise.t) =
        l_a >>=| fun (a : a) ->
        let (b : b t) = f a in
        let (l_b : b LiftedIntoPromise.t promise) =
          b >>=- fun (l_b : b LiftedIntoPromise.t) ->
          S.resolve resolver l_b;
          S.return l_b
        in
        (* We must ignore the promise because we can't use it now!
           We are very very unlikely to have a resolved promise at this point,
           since the LiftedIntoPromise monad may have a delayed callback
           to the above [fun (l_b : b LiftedIntoPromise.t)]. *)
        ignore l_b;
        LiftedIntoPromise.pure ()
      in
      S.return l_b
    in
    promise

  let lift m = S.return m

  let make_promise () =
    let promise, resolver = S.make_promise () in
    let resolve a = S.resolve resolver (LiftedIntoPromise.return a) in
    (promise, (resolve, S.reject resolver))

  let promise_state (type a) (m : a t) :
      resolved_promise_state universal_promise_state =
    S.promise_state m

  let resolve (resolve', _reject') = resolve'
  let reject (_resolve', reject') = reject'
  (* let run_promise m = m *)
end

(** {2 Monad Combinations} *)

(** The type of monads that are both State and Writer monads. *)
module type MONAD_STATE_WRITER = sig
  type state
  type output

  include MONAD with type 'a t = state -> 'a * state * output

  (** {1 State Monad} *)

  val get : state t
  val gets : (state -> 'a) -> 'a t
  val put : state -> unit t
  val modify : (state -> state) -> unit t
  val run_state : 'a t -> state -> 'a * state
  val exec_state : 'a t -> state -> state

  (** {1 Writer Monad} *)

  val tell : output -> unit t
  val run_writer : 'a t -> ('a * output) t
end

module type MONAD_STATE_WRITER_ASYNC = sig
  type state
  type 'a promise
  type journal
  type journal_entry_id
  type journal_entry_value

  include
    MONAD
      with type 'a t =
        (state * journal ->
        ('a
        * StateMessage.t list
        * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
        promise)
        promise

  (** {1 Promise Monad} *)

  val lift_promise : 'a promise -> 'a t

  (** {1 State Monad} *)

  val get : state t
  val gets : (state -> 'a) -> 'a t
  val post_msg : StateMessage.t list -> unit t
  val sync_state : 'a t -> 'a t
  val run_state_async : 'a t -> state -> ('a * state) promise
  val exec_state_async : 'a t -> state -> state promise

  (** {1 Writer Monad} *)

  val post_entry : journal_entry_value -> unit t
end

(** The type of monads that are async State and Writer monads and also Promise
    monads. *)
module type MONAD_STATE_WRITER_ASYNC_PROMISE = sig
  type state
  type journal
  type journal_entry_id
  type journal_entry_value
  type 'a promise
  type 'a resolver
  type resolved_promise_state

  include
    MONAD
      with type 'a t =
        (state * journal ->
        ('a
        * StateMessage.t list
        * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
        promise)
        promise

  (** {1 Promise Monad} *)

  val make_promise : unit -> 'a t * 'a resolver
  val promise_state : 'a t -> resolved_promise_state universal_promise_state
  val resolve : 'a resolver -> 'a -> unit
  val reject : 'a resolver -> exn -> unit
  val lift_promise : 'a promise -> 'a t
  val parallel : 'a t list -> 'a list t
  val bind_promise : 'a promise -> ('a -> 'b promise) -> 'b promise
  val return_promise : 'a -> 'a promise

  (** {1 State Monad} *)

  val get : state t
  val gets : (state -> 'a) -> 'a t
  val post_msg : StateMessage.t list -> unit t
  val run_state_async : 'a t -> state -> ('a * state) promise
  val exec_state_async : 'a t -> state -> state promise

  (** {1 Writer Monad} *)

  val post_entry : journal_entry_value -> unit t

  (* NOT PRESENT: val run_writer_async : 'a t -> ('a * journal) promise
     We need the [state] to get the writer, but the API for run_writer_async does not supply it.
     This is a consequence of the design decision to give the writer monad the lowest priority
     among the monads combined in this module. *)

  (** {1 Combined Monad} *)

  val run_state_writer_async : 'a t -> state -> ('a * state * journal) promise
end

(** A builder of a monad that is a state monad and a writer monad. *)
module MonadStateWriterWithPureSeq (S : STATE) (W : WRITER_IMPL) : sig
  include
    MONAD_STATE_WRITER
      with type state = S.state
       and type output = W.output
       and type 'a t = S.state -> 'a * S.state * W.output

  include PURE_SEQ with type 'a t := S.state -> 'a * S.state * W.output
end = struct
  type state = S.state
  type output = W.output
  type 'a t = S.state -> 'a * S.state * W.output

  module M1 = MonadState (S)

  module M2 = MonadWriterT (struct
    type output = W.output

    let empty = W.empty
    let append = W.append

    module LiftedIntoWriter = M1
  end)

  let return v = fun state -> (v, state, W.empty)
  let pure = return

  let bind (type a) (type b) (m : a t) (f : a -> b t) : b t =
   fun state ->
    let a', state', output' = m state in
    let b'', state'', output'' = f a' state' in
    (b'', state'', W.append output' output'')

  let map (type a) (type b) (f : a -> b) (x : a t) : b t =
    bind x (compose pure f)

  let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
    bind f (fun y -> map y x)

  let seq f x = bind f (fun y -> map y (x ()))

  let seq_left (type a) (type b) (x : a t) (y : unit -> b t) : a t =
    bind x (fun a -> bind (y ()) (fun _b -> pure a))

  let seq_right (type a) (type b) (x : a t) (y : unit -> b t) : b t =
    bind x (fun _a -> y ())

  let get : S.state t = fun state -> (state, state, W.empty)
  let gets = fun read_state -> fun state -> (read_state state, state, W.empty)

  let put state =
   fun state' ->
    ignore state';
    ((), state, W.empty)

  let modify trans_state =
   fun state' ->
    let state'' = trans_state state' in
    ((), state'', W.empty)

  let run_state =
   fun (t : 'a t) ->
    fun (s : state) ->
     let a', state', _ = t s in
     (a', state')

  let exec_state =
   fun (t : 'a t) ->
    fun (s : state) ->
     let _, state', _ = t s in
     state'

  let tell o =
   fun state ->
    let m2 = M2.tell o in
    let ((), o'), state' = m2 state in
    ((), state', o')

  let run_writer =
   fun (t : 'a t) ->
    fun (state : state) ->
     let a', state', writer' = t state in
     ((a', writer'), state', writer')
end

(** A builder of a monad that is a combination of a state monad, a writer monad,
    and a promise monad.

    The built monad is not thread safe if it is shared across system threads or
    OCaml 5 domains.

    There were tradeoffs made to combine multiple monads into one, and those
    tradeoffs are informed by the relative priority each monad has to each
    other:

    - The promise monad is the highest priority monad. That means the built
      combo monad {b is} a promise monad, without any loss of fidelity.
    - The state monad is the medium priority monad. You can't access the state
      without waiting for a promise. Furthermore, the (async) state monad's
      update and flusing requires the promise monad.
    - The writer monad is the lowest priority monad, You can't access the
      journal without waiting for a promise {b and} supplying an initial or
      existing state. Furthermore the (async) writer monad's posting requires
      the promise monad.

    {1 State amongst cooperative promise threads}

    Unlike the traditional state monad, the monad that
    [MonadStateWriterPromiseWithPureSeq] builds will only carry a
    {b state identifier} along the sequential series of [bind] calls
    (conventionally using the [let*] or [>>=] operators). The state itself is
    maintained in a map of state identifiers to states that is stored in the
    monad module.

    Precisely, the [MonadStateWriterPromiseWithPureSeq] ... after the functor
    parameters are applied ... maintains a map of state identifiers to states. A
    different state map is maintained if you apply functor parameters again
    (even the same parameters) to [MonadStateWriterPromiseWithPureSeq].

    {b Caution}: The allocated memory for the state value is never reclaimed.

    If you follow the
    {b best practice recommendation to only apply the functor parameters once}
    to build the monad [MonadStateWriterPromiseWithPureSeq], then will only be
    one authorative owner who maintains the states and their storage.

    {1 Writer output amongst cooperative promise threads}

    Unlike the traditional writer monad, the monad that
    [MonadStateWriterPromiseWithPureSeq] builds will only carry the latest
    output (called "journal entries") that has yet to be written to the output
    (called the "journal"). However, the journal itself is stored within the
    monad module's map of state identifiers to states. Periodically the journal
    entries are moved into the journal; you can explicitly do this with
    {!sync_writer}.

    A good analogy is having thread-local buffered log messages (the journal
    entries), which are periodically synced into global log messages (the
    journal).

    The monad's module state identifier map is the authoritative owner of the
    journal.

    You are required to {!flush} the journal as well. *)
module MonadStateWriterPromiseWithPureSeq
    (P : MONAD_PROMISE)
    (SA : sig
      include MUTABLE_STATE_ASYNC_IMPL with type 'a promise = 'a P.t
    end)
    (WA : sig
      include MUTABLE_WRITER_ASYNC_IMPL with type 'a promise = 'a P.t
    end) : sig
  include
    MONAD_STATE_WRITER_ASYNC_PROMISE
      with type state = SA.state
       and type journal = WA.journal
       and type journal_entry_id = WA.journal_entry_id
       and type journal_entry_value = WA.journal_entry_value
       and type 'a promise = 'a P.t
       and type 'a resolver = ('a -> unit) * (exn -> unit)
       and type resolved_promise_state = P.resolved_promise_state
       and type 'a t =
        (SA.state * WA.journal ->
        ('a
        * StateMessage.t list
        * (WA.journal_entry_id * WA.journal_entry_value) UniqueInsertionList.t)
        P.t)
        P.t

  include
    PURE_SEQ
      with type 'a t :=
        (SA.state * WA.journal ->
        ('a
        * StateMessage.t list
        * (WA.journal_entry_id * WA.journal_entry_value) UniqueInsertionList.t)
        P.t)
        P.t

  val sync_writer : 'a t -> 'a t
  val sync_state : 'a t -> 'a t
end = struct
  type state = SA.state
  type journal = WA.journal
  type journal_entry_id = WA.journal_entry_id
  type journal_entry_value = WA.journal_entry_value
  type 'a promise = 'a P.t

  type 'a resolver = ('a -> unit) * (exn -> unit)
  (** Since we need to map the ['a resolver], and Lwt and maybe others do not
      offer the [map], we split up the resolver into a pair of [resolve] and
      [reject] functions. *)

  type resolved_promise_state = P.resolved_promise_state

  type 'a t =
    (state * journal ->
    ('a
    * StateMessage.t list
    * (WA.journal_entry_id * WA.journal_entry_value) UniqueInsertionList.t)
    P.t)
    P.t

  let ( let* ) = P.bind
  let bind_promise = P.bind
  let return_promise = P.return

  module JournalEntryOrd = struct
    type t = WA.journal_entry_id * WA.journal_entry_value

    let compare a b = WA.compare_journal_entry_id (fst a) (fst b)
  end

  let empty_journalentries = UniqueInsertionList.empty (module JournalEntryOrd)

  let lift_promise (type a) (p : a promise) : a t =
    P.pure @@ fun (_state, _journal) ->
    let lifted :
        (a
        * StateMessage.t list
        * (WA.journal_entry_id * WA.journal_entry_value) UniqueInsertionList.t)
        P.t =
      P.map (fun (a : a) -> (a, [], empty_journalentries)) p
    in
    lifted

  module M1 = MonadState (SA)

  module M2 = MonadWriterAsyncT (struct
    type journal = WA.journal
    type journal_entry_id = WA.journal_entry_id
    type journal_entry_value = WA.journal_entry_value
    type 'a promise = 'a WA.promise

    let new_journal_entry = WA.new_journal_entry
    let compare_journal_entry_id = WA.compare_journal_entry_id
    let create_empty = WA.create_empty
    let update = WA.update
    let reconcile = WA.reconcile
    let flush = WA.flush

    module LiftedIntoWriter = M1
  end)

  let return v =
    P.pure @@ fun (state, journal) ->
    ignore state;
    ignore journal;
    P.pure (v, [], empty_journalentries)

  let pure = return

  let mutate_records (state, journal) msgs journalentries =
    SA.update state msgs;
    WA.update journal journalentries

  let bind (type a) (type b) (m : a t) (f : a -> b t) : b t =
    let* a_t = m in
    P.pure @@ fun (state, journal) ->
    let* a, msgs, journalentries = a_t (state, journal) in
    mutate_records (state, journal) msgs journalentries;
    let* b_t = f a in
    let* b, msgs, journalentries = b_t (state, journal) in
    mutate_records (state, journal) msgs journalentries;
    ignore journal;
    P.pure (b, [], empty_journalentries)

  let map (type a) (type b) (f : a -> b) (x : a t) : b t =
    bind x (compose pure f)

  let apply (type a) (type b) (f : (a -> b) t) (x : a t) : b t =
    bind f (fun y -> map y x)

  let seq f x = bind f (fun y -> map y (x ()))

  let seq_left (type a) (type b) (x : a t) (y : unit -> b t) : a t =
    bind x (fun a -> bind (y ()) (fun _b -> pure a))

  let seq_right (type a) (type b) (x : a t) (y : unit -> b t) : b t =
    bind x (fun _a -> y ())

  let get : SA.state t =
    P.pure @@ fun (state, journal) ->
    ignore journal;
    P.pure @@ (state, [], empty_journalentries)

  let gets =
   fun read_state ->
    P.pure @@ fun (state, journal) ->
    ignore journal;
    P.pure @@ (read_state state, [], empty_journalentries)

  let post_msg msgs =
    P.pure @@ fun (state, journal) ->
    mutate_records (state, journal) msgs empty_journalentries;
    ignore journal;
    P.pure ((), [], empty_journalentries)

  let flush_all (state, journal) =
    let* journal = WA.flush journal in
    let* () = SA.flush state in
    P.pure (state, journal)

  let sync_state (type a) (t : a t) : a t =
    P.pure @@ fun (state, journal) ->
    let* a_t = t in
    let* a, msgs, journalentries = a_t (state, journal) in
    mutate_records (state, journal) msgs journalentries;
    let* state = flush_all (state, journal) in
    ignore state;
    P.pure (a, [], empty_journalentries)

  let run_state_async (t : 'a t) (state : state) =
    let* a_t = t in
    let journal = WA.create_empty () in
    let* a, msgs, journalentries = a_t (state, journal) in
    mutate_records (state, journal) msgs journalentries;
    let* state, journal = flush_all (state, journal) in
    ignore journal;
    P.pure (a, state)

  let exec_state_async (t : 'a t) (state : state) =
    let* a_t = t in
    let journal = WA.create_empty () in
    let* a, msgs, journalentries = a_t (state, journal) in
    ignore a;
    mutate_records (state, journal) msgs journalentries;
    let* state, journal = flush_all (state, journal) in
    ignore journal;
    P.pure state

  let post_entry value =
    P.pure @@ fun (state, journal) ->
    let id, value = WA.new_journal_entry value in
    (* Using WA.update now will execute the entry now. *)
    let journalentries =
      UniqueInsertionList.add (id, value) empty_journalentries
    in
    mutate_records (state, journal) [] journalentries;
    ignore journal;
    P.pure ((), [], empty_journalentries)

  let sync_writer (type a) (t : a t) : a t =
    P.pure @@ fun (state, journal) ->
    let* a_t = t in
    let* a, msgs, journalentries = a_t (state, journal) in
    let* journal = WA.flush journal in
    mutate_records (state, journal) msgs journalentries;
    ignore journal;
    P.pure (a, [], empty_journalentries)

  let make_promise () =
    let promise, resolver = P.make_promise () in
    let resolve a =
      P.resolve resolver (fun (state, journal) ->
          (* no update to [state] needed *)
          ignore state;
          ignore journal;
          P.pure (a, [], empty_journalentries))
    in
    let t = promise in
    (t, (resolve, P.reject resolver))

  let promise_state (type a) (m : a t) :
      resolved_promise_state universal_promise_state =
    P.promise_state m

  let resolve (resolve', _reject') = resolve'
  let reject (_resolve', reject') = reject'

  let parallel (type a) (ps : a t list) : a list t =
    P.pure @@ fun (state, journal) ->
    let list_of_child_promises :
        (a
        * StateMessage.t list
        * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
        promise
        list =
      List.map
        (fun p ->
          let* p' = p in
          p' (state, journal))
        ps
    in
    let promise_of_child_list :
        (a
        * StateMessage.t list
        * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
        list
        promise =
      P.parallel list_of_child_promises
    in
    let* child_list :
        (a
        * StateMessage.t list
        * (journal_entry_id * journal_entry_value) UniqueInsertionList.t)
        list =
      promise_of_child_list
    in
    let (a_list, msgs, journalentries) :
        a list
        * StateMessage.t list list
        * (journal_entry_id * journal_entry_value) UniqueInsertionList.t =
      List.fold_right
        (fun (a, b, c) (acc_a, acc_b, acc_c) ->
          (a :: acc_a, b :: acc_b, UniqueInsertionList.append acc_c c))
        child_list
        ([], [], empty_journalentries)
    in
    P.pure (a_list, List.flatten msgs, journalentries)

  let run_state_writer_async (t : 'a t) (state : state) =
    let* a_t = t in
    let journal = WA.create_empty () in
    let* a, msgs, journalentries = a_t (state, journal) in
    let* state, journal = flush_all (state, journal) in
    mutate_records (state, journal) msgs journalentries;
    P.pure (a, state, journal)
end

module type SELECTIVE = sig
  type 'a t

  val return : 'a -> 'a t
  val map : ('a -> 'b) -> 'a t -> 'b t

  val select : ('a, 'b) Either.t t -> ('a -> 'b) t -> 'b t
  (** The [select] method which gives meaning to the composition of two
      effectful computations where the second computation depends on the first
      one.

      Described in
      {{:https://dl.acm.org/doi/pdf/10.1145/3341694}Selective Applicative
       Functors} *)
end

module SelectiveSyntax (F : SELECTIVE) = struct
  (* Symbols from https://dl.acm.org/doi/pdf/10.1145/3341694 *)

  let ( <$> ) = F.map
  let ( <*? ) = F.select
end

(** An intermediate constraint between an applicative and a monad. It behaves as
    if it were a monad unless the build script uses {!S.seq_left}, where the
    Selective constraint can choose to short-circuit with the right-side of the
    selection.

    Described in
    {{:https://dl.acm.org/doi/pdf/10.1145/3341694}Selective Applicative
     Functors}. *)
module SelectiveWithPureSeq (F : SELECTIVE) : sig
  include SELECTIVE with type 'a t = 'a F.t
  include PURE_SEQ with type 'a t := 'a t
end = struct
  type 'a t = 'a F.t

  let return = F.return
  let select = F.select
  let pure = F.return
  let map = F.map

  let seq (type a) (type b) (f : (a -> b) t) (x : unit -> a t) : b t =
    F.select
      (F.map (fun a -> Either.left a) (x ()) : (a, b) Either.t t)
      (f : (a -> b) t)

  let seq_left (type a) (type b) (x : a t) (y : unit -> b t) : a t =
    F.select
      (F.map (fun a -> Either.right a) x : (a, a) Either.t t)
      (F.map (fun _b -> fun a -> a) (y ()) : (a -> a) t)

  let seq_right (type a) (type b) (x : a t) (y : unit -> b t) : b t =
    F.select
      (F.map (fun a -> Either.left a) x : (a, b) Either.t t)
      (F.map (fun b -> fun _a -> b) (y ()) : (a -> b) t)
end