package visitors

  1. Overview
  2. Docs
An OCaml syntax extension for generating visitor classes

Install

Dune Dependency

Authors

Maintainers

Sources

archive.tar.gz
md5=80fc467552d944dcae0c5d7895cfba64
sha512=42522af2845fab409cdf0766cce83ac1345e0169248252ad74da2d72eefdb5d846dff2ece566667b9d80a8db57dabdbf333c32c50fef9c39f7837e78b3476b5b

doc/src/visitors.runtime/VisitorsRuntime.ml.html

Source file VisitorsRuntime.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
(* This file provides useful / reasonable visitor methods for many of the
   built-in types of OCaml. *)

(* The classes defined in this file are automatically inherited by
   auto-generated visitors. If this is not desired, this behavior can be
   turned off at generation time by specifying [nude = true]. *)

(* Some of the code in this file can be (or has been) auto-generated by
   the [visitors] package itself: see [test/VisitorsRuntimeBootstrap].
   To avoid a complicated process and to facilitate code review, we
   keep this code under manual control in this file. *)

(* -------------------------------------------------------------------------- *)

(* For compatibility with OCaml 4.02, we take the type [('a, 'b) result] from
   the package [result]. This type appeared in the standard library in OCaml
   4.03. *)

open Result

(* -------------------------------------------------------------------------- *)

(* [array_equal eq xs1 xs2] tests whether the arrays [xs1] and [xs2] have the
   same components. The arrays must have the same length. The components are
   compared using [eq]. *)

let rec array_equal eq i n xs1 xs2 =
  i = n ||
  let x1 = Array.unsafe_get xs1 i
  and x2 = Array.unsafe_get xs2 i in
  eq x1 x2 && array_equal eq (i + 1) n xs1 xs2

let array_equal eq xs1 xs2 =
  let n = Array.length xs1 in
  assert (Array.length xs2 = n);
  array_equal eq 0 n xs1 xs2

(* -------------------------------------------------------------------------- *)

(* An exception used at arity 2 and above. *)

exception StructuralMismatch

let fail () =
  raise StructuralMismatch

let wrap f t =
  try
    f t;
    true
  with StructuralMismatch ->
    false

let wrap2 f t1 t2 =
  try
    f t1 t2;
    true
  with StructuralMismatch ->
    false

(* -------------------------------------------------------------------------- *)

(* A virtual base class for monoids. *)

class virtual ['s] monoid = object
  method private virtual zero: 's
  method private virtual plus: 's -> 's -> 's
end

(* -------------------------------------------------------------------------- *)

(* Common monoids. *)

class ['s] addition_monoid = object
  inherit ['s] monoid
  method private zero = 0
  method private plus = (+)
end

class ['s] unit_monoid = object
  inherit ['s] monoid
  method private zero = ()
  method private plus () () = ()
end

(* -------------------------------------------------------------------------- *)

(* Visitor methods for the primitive types. *)

(* Must the methods below be declared polymorphic in ['env]? The fact is, they
   ARE polymorphic in ['env], because they do not extend it or look it up.

   By declaring them polymorphic, we gain in generality: e.g., [visit_list]
   can be called by two visitor methods which happen to have different types
   of environments. (This happens in alphaLib, where visitor methods for terms
   and patterns manipulate different types of environments.)

   However, by declaring them polymorphic, we also lose some generality, as we
   PREVENT users from overriding these methods with code that extends or looks
   up the environment.

   Here, it seems reasonable to take both the gain and the loss, and declare
   these methods polymorphic.

   We could give the user a choice by providing multiple base classes, but that
   would messy. Note that, when using [@@deriving visitors { ... }], the user
   does have a choice whether the generated methods should be polymorphic in
   ['env]. *)

(* -------------------------------------------------------------------------- *)

(* [iter] *)

class ['self] iter = object (self)

  method private visit_array: 'env 'a .
    ('env -> 'a -> unit) -> 'env -> 'a array -> unit
  = fun f env xs ->
      (* For speed, we inline [Array.iter]. Chances are, we save a closure
         allocation, as using [Array.iter] would require us to build [f env]. *)
      for i = 0 to Array.length xs - 1 do
        f env (Array.unsafe_get xs i)
      done

  method private visit_bool: 'env .
    'env -> bool -> unit
  = fun _ _ -> ()

  method private visit_bytes: 'env .
    'env -> bytes -> unit
  = fun _ _ -> ()

  method private visit_char: 'env .
    'env -> char -> unit
  = fun _ _ -> ()

  method private visit_float: 'env .
    'env -> float -> unit
  = fun _ _ -> ()

  method private visit_int: 'env .
    'env -> int -> unit
  = fun _ _ -> ()

  method private visit_int32: 'env .
    'env -> int32 -> unit
  = fun _ _ -> ()

  method private visit_int64: 'env .
    'env -> int64 -> unit
  = fun _ _ -> ()

  method private visit_lazy_t: 'env 'a .
    ('env -> 'a -> unit) -> 'env -> 'a Lazy.t -> unit
  = fun f env (lazy x) ->
      f env x

  method private visit_list: 'env 'a .
    ('env -> 'a -> unit) -> 'env -> 'a list -> unit
  = fun f env xs ->
      match xs with
      | [] ->
          ()
      | x :: xs ->
          f env x;
          self # visit_list f env xs

  method private visit_nativeint: 'env .
    'env -> nativeint -> unit
  = fun _ _ -> ()

  method private visit_option: 'env 'a .
    ('env -> 'a -> unit) -> 'env -> 'a option -> unit
  = fun f env ox ->
      match ox with
      | None ->
          ()
      | Some x ->
          f env x

  method private visit_ref: 'env 'a .
    ('env -> 'a -> unit) -> 'env -> 'a ref -> unit
  = fun f env rx ->
      f env !rx

  method private visit_result: 'env 'a 'e.
    ('env -> 'a -> unit) ->
    ('env -> 'e -> unit) ->
     'env -> ('a, 'e) result -> unit
  = fun f g env r ->
      match r with
      | Ok a -> f env a
      | Error b -> g env b

  method private visit_string: 'env .
    'env -> string -> unit
  = fun _ _ -> ()

  method private visit_unit: 'env .
    'env -> unit -> unit
  = fun _ _ -> ()

end

(* -------------------------------------------------------------------------- *)

(* [map] *)

class ['self] map = object (self)

  method private visit_array: 'env 'a 'b .
    ('env -> 'a -> 'b) -> 'env -> 'a array -> 'b array
  = fun f env xs ->
      Array.map (f env) xs
      (* We could in principle inline [Array.map] so as to avoid allocating
         the closure [f env]. That would be a bit painful, though. Anyway,
         in [flambda] mode, the compiler might be able to do that for us. *)

  method private visit_bool: 'env .
    'env -> bool -> bool
  = fun _ x -> x

  method private visit_bytes: 'env .
    'env -> bytes -> bytes
  = fun _ x -> x

  method private visit_char: 'env .
    'env -> char -> char
  = fun _ x -> x

  method private visit_float: 'env .
    'env -> float -> float
  = fun _ x -> x

  method private visit_int: 'env .
    'env -> int -> int
  = fun _ x -> x

  method private visit_int32: 'env .
    'env -> int32 -> int32
  = fun _ x -> x

  method private visit_int64: 'env .
    'env -> int64 -> int64
  = fun _ x -> x

  method private visit_lazy_t: 'env 'a 'b .
    ('env -> 'a -> 'b) -> 'env -> 'a Lazy.t -> 'b Lazy.t
  = fun f env thx ->
      (* We seem to have two options: either force the suspension now
         and rebuild a trivial suspension, or build now a suspension
         that will perform the traversal when forced. We choose the
         latter, which seems more interesting. If this is not the
         desired behavior, it can of course be overridden. *)
      lazy (f env (Lazy.force thx))

  method private visit_list: 'env 'a 'b .
    ('env -> 'a -> 'b) -> 'env -> 'a list -> 'b list
  = fun f env xs ->
      match xs with
      | [] ->
          []
      | x :: xs ->
          let x = f env x in
          x :: self # visit_list f env xs

  method private visit_nativeint: 'env .
    'env -> nativeint -> nativeint
  = fun _ x -> x

  method private visit_option: 'env 'a 'b .
    ('env -> 'a -> 'b) -> 'env -> 'a option -> 'b option
  = fun f env ox ->
      match ox with
      | None ->
          None
      | Some x ->
          Some (f env x)

  method private visit_ref: 'env 'a 'b .
    ('env -> 'a -> 'b) -> 'env -> 'a ref -> 'b ref
  = fun f env rx ->
      ref (f env !rx)

  method private visit_result: 'env 'a 'b 'e 'f .
    ('env -> 'a -> 'b) ->
    ('env -> 'e -> 'f) ->
     'env -> ('a, 'e) result -> ('b, 'f) result
  = fun f g env r ->
      match r with
      | Ok a -> Ok (f env a)
      | Error b -> Error (g env b)

  method private visit_string: 'env .
    'env -> string -> string
  = fun _ x -> x

  method private visit_unit: 'env .
    'env -> unit -> unit
  = fun _ x -> x

end

(* -------------------------------------------------------------------------- *)

(* [endo] *)

class ['self] endo = object (self)

  (* We might wish to inherit from [map] and override only those methods where
     a physical equality check is needed. Yet, we cannot do that, because some
     methods, like [visit_list], have more restrictive types in this class than
     in the class [map]. *)

  (* It may seem fishy to use an [endo] visitor at type [array], but one never
     knows -- maybe the user wants this. Maybe she is using an array as an
     immutable data structure. *)

  method private visit_array: 'env 'a .
    ('env -> 'a -> 'a) -> 'env -> 'a array -> 'a array
  = fun f env xs ->
      let xs' = Array.map (f env) xs in
      if array_equal (==) xs xs' then xs else xs'

  method private visit_bool: 'env .
    'env -> bool -> bool
  = fun _ x -> x

  method private visit_bytes: 'env .
    'env -> bytes -> bytes
  = fun _ x -> x

  method private visit_char:'env .
    'env -> char -> char
  = fun _ x -> x

  method private visit_float: 'env .
    'env -> float -> float
  = fun _ x -> x

  method private visit_int: 'env .
    'env -> int -> int
  = fun _ x -> x

  method private visit_int32: 'env .
    'env -> int32 -> int32
  = fun _ x -> x

  method private visit_int64: 'env .
    'env -> int64 -> int64
  = fun _ x -> x

  method private visit_lazy_t : 'env 'a .
    ('env -> 'a -> 'a) -> 'env -> 'a Lazy.t -> 'a Lazy.t
  = fun f env thx ->
      (* We could use the same code as in [map], which does not preserve sharing.
         Or, we can force the suspension now, compute [x'], and if [x] and
         [x'] coincide, then we can return the original suspension (now
         forced), so as to preserve sharing. We choose the latter behavior. If
         this is not the desired behavior, it can of course be overridden. *)
      let x = Lazy.force thx in
      let x' = f env x in
      if x == x' then thx else lazy x'

  method private visit_list: 'env 'a .
    ('env -> 'a -> 'a) -> 'env -> 'a list -> 'a list
  = fun f env this ->
      match this with
      | [] ->
          []
      | x :: xs ->
          let x' = f env x in
          let xs' = self # visit_list f env xs in
          if x == x' && xs == xs' then
            this
          else
            x' :: xs'

  method private visit_nativeint: 'env .
    'env -> nativeint -> nativeint
  = fun _ x -> x

  method private visit_option: 'env 'a .
    ('env -> 'a -> 'a) -> 'env -> 'a option -> 'a option
  = fun f env ox ->
      match ox with
      | None ->
          None
      | Some x ->
          let x' = f env x in
          if x == x' then
            ox
          else
            Some x'

  (* It probably does not make sense to use an [endo] visitor at type
     [ref], but one never knows -- maybe the user wants this. Anyway,
     it is consistent with the behavior of [endo] visitors at mutable
     record types. *)

  method private visit_ref: 'env 'a .
    ('env -> 'a -> 'a) -> 'env -> 'a ref -> 'a ref
  = fun f env rx ->
      let x = !rx in
      let x' = f env x in
      if x == x' then
        rx
      else
        ref x'

  method private visit_result: 'env 'a 'e .
    ('env -> 'a -> 'a) ->
    ('env -> 'e -> 'e) ->
     'env -> ('a, 'e) result -> ('a, 'e) result
  = fun f g env r ->
      match r with
      | Ok a ->
          let a' = f env a in
          if a == a' then r else Ok a'
      | Error b ->
          let b' = g env b in
          if b == b' then r else Error b'

  method private visit_string: 'env .
    'env -> string -> string
  = fun _ x -> x

  method private visit_unit: 'env .
    'env -> unit -> unit
  = fun _ x -> x

end

(* -------------------------------------------------------------------------- *)

(* [reduce] *)

(* For arrays and lists, we use [fold_left] instead of a natural (bottom-up)
   fold. The order in which the elements are traversed is the same either way
   (namely, left-to-right) but the manner in which the [plus] operations are
   associated is not the same, so the [plus] operator should be associative.

   We could go back to a natural fold, but we would lose tail recursion. *)

class virtual ['self] reduce = object (self : 'self)

  inherit ['s] monoid

  method private visit_array: 'env 'a .
    ('env -> 'a -> 's) -> 'env -> 'a array -> 's
  = fun f env xs ->
      Array.fold_left (fun s x -> self#plus s (f env x)) self#zero xs
      (* We might wish to inline [Array.fold_left] and save a closure
         allocation. That said, in flambda mode, the compiler might be
         able to do that automatically. *)

  method private visit_bool: 'env .
    'env -> bool -> 's
  = fun _env _ -> self#zero

  method private visit_bytes: 'env .
    'env -> bytes -> 's
  = fun _env _ -> self#zero

  method private visit_char: 'env .
    'env -> char -> 's
  = fun _env _ -> self#zero

  method private visit_float: 'env .
    'env -> float -> 's
  = fun _env _ -> self#zero

  method private visit_int: 'env .
    'env -> int -> 's
  = fun _env _ -> self#zero

  method private visit_int32: 'env .
    'env -> int32 -> 's
  = fun _env _ -> self#zero

  method private visit_int64: 'env .
    'env -> int64 -> 's
  = fun _env _ -> self#zero

  method private visit_lazy_t: 'env 'a .
    ('env -> 'a -> 's) -> 'env -> 'a Lazy.t -> 's
  = fun f env (lazy x) ->
      f env x

  method private visit_list: 'env 'a .
    ('env -> 'a -> 's) -> 'env -> 'a list -> 's
  = fun f env xs ->
      self # list_fold_left f env self#zero xs
      (* The above line is equivalent to the following: *)
      (* List.fold_left (fun s x -> self#plus s (f env x)) self#zero xs *)
      (* By using the auxiliary method [list_fold_left] instead of calling
         the library function [List.fold_left], we save a closure allocation,
         at least in non-flambda mode. A micro-benchmark shows no performance
         impact, either way. *)

  method private list_fold_left: 'env 'a .
    ('env -> 'a -> 's) -> 'env -> 's -> 'a list -> 's
  = fun f env s xs ->
    match xs with
    | [] ->
        s
    | x :: xs ->
        let s = self#plus s (f env x) in
        self # list_fold_left f env s xs

  method private visit_nativeint: 'env .
    'env -> nativeint -> 's
  = fun _env _ -> self#zero

  method private visit_option: 'env 'a .
    ('env -> 'a -> 's) -> 'env -> 'a option -> 's
  = fun f env ox ->
      match ox with
      | Some x ->
          f env x
      | None ->
          self#zero

  method private visit_ref: 'env 'a .
    ('env -> 'a -> 's) -> 'env -> 'a ref -> 's
  = fun f env rx ->
      f env !rx

  method private visit_result: 'env 'a 'e .
    ('env -> 'a -> 's) ->
    ('env -> 'e -> 's) ->
     'env -> ('a, 'e) result -> 's
  = fun f g env r ->
      match r with
      | Ok a ->
          f env a
      | Error b ->
          g env b

  method private visit_string: 'env .
    'env -> string -> 's
  = fun _env _ -> self#zero

  method private visit_unit: 'env .
    'env -> unit -> 's
  = fun _env _ -> self#zero

end

(* -------------------------------------------------------------------------- *)

(* [mapreduce] *)

class virtual ['self] mapreduce = object (self : 'self)

  inherit ['s] monoid

  method private visit_array: 'env 'a 'b .
    ('env -> 'a -> 'b * 's) -> 'env -> 'a array -> 'b array * 's
  = fun f env xs ->
      let s = ref self#zero in
      let xs =
        Array.map (fun x ->
          let x, sx = f env x in
          s := self#plus !s sx;
          x
        ) xs
      in
      xs, !s

  method private visit_bool: 'env .
    'env -> bool -> bool * 's
  = fun _ x -> x, self#zero

  method private visit_bytes: 'env .
    'env -> bytes -> bytes * 's
  = fun _ x -> x, self#zero

  method private visit_char: 'env .
    'env -> char -> char * 's
  = fun _ x -> x, self#zero

  method private visit_float: 'env .
    'env -> float -> float * 's
  = fun _ x -> x, self#zero

  method private visit_int: 'env .
    'env -> int -> int * 's
  = fun _ x -> x, self#zero

  method private visit_int32: 'env .
    'env -> int32 -> int32 * 's
  = fun _ x -> x, self#zero

  method private visit_int64: 'env .
    'env -> int64 -> int64 * 's
  = fun _ x -> x, self#zero

  method private visit_lazy_t: 'env 'a 'b .
    ('env -> 'a -> 'b * 's) -> 'env -> 'a Lazy.t -> 'b Lazy.t * 's
  = fun f env (lazy x) ->
      (* Because we must compute a summary now, it seems that we have to
         force the suspension now. One should be aware that this is not
         the same behavior as the one we chose in the class [map]. *)
      let y, s = f env x in
      lazy y, s

  method private visit_list: 'env 'a 'b .
    ('env -> 'a -> 'b * 's) -> 'env -> 'a list -> 'b list * 's
  = fun f env xs ->
      match xs with
      | [] ->
          [], self#zero
      | x :: xs ->
          let x, sx = f env x in
          let xs, sxs = self # visit_list f env xs in
          x :: xs, self#plus sx sxs
      (* This is not the same strategy as in the class [reduce], where we
         used an accumulator and a tail-recursive left fold. Here, we are
         using a right fold. The order in which list elements are visited
         is left-to-right in both cases, but the tree of [self#plus] ops
         is not balanced the same way. *)

  method private visit_nativeint: 'env .
    'env -> nativeint -> nativeint * 's
  = fun _ x -> x, self#zero

  method private visit_option: 'env 'a_0 'a_1 .
    ('env -> 'a_0 -> 'a_1 * 's) ->
    'env -> 'a_0 option -> 'a_1 option * 's
  = fun visit_'a env this ->
      match this with
      | None ->
          None, self#zero
      | Some c0 ->
          let r0, s0 = visit_'a env c0 in
          Some r0, s0

  method private visit_ref: 'env 'a_0 'a_1 .
    ('env -> 'a_0 -> 'a_1 * 's) ->
    'env -> 'a_0 ref -> 'a_1 ref * 's
  = fun visit_'a env this ->
      let r0, s0 = visit_'a env !this in
      ref r0, s0

  method private visit_result: 'env 'a_0 'a_1 'b_0 'b_1 .
    ('env -> 'a_0 -> 'a_1 * 's) ->
    ('env -> 'b_0 -> 'b_1 * 's) ->
    'env -> ('a_0, 'b_0) result -> ('a_1, 'b_1) result * 's
  = fun visit_'a visit_'b env this ->
      match this with
      | Ok c0 ->
          let r0, s0 = visit_'a env c0 in
          Ok r0, s0
      | Error c0 ->
          let r0, s0 = visit_'b env c0 in
          Error r0, s0

  method private visit_string: 'env .
    'env -> string -> string * 's
  = fun _ x -> x, self#zero

  method private visit_unit: 'env .
    'env -> unit -> unit * 's
  = fun _ x -> x, self#zero

end

(* -------------------------------------------------------------------------- *)

(* [fold] *)

class ['self] fold = object (_self)

  (* No methods are provided, as we do not wish to fix the types of these
     methods. It is up to the user to inherit from a class that defines
     appropriate methods. Note that [VisitorsRuntime.map] is likely to be
     appropriate in many situations. *)

end

(* -------------------------------------------------------------------------- *)

(* [iter2] *)

class ['self] iter2 = object (self)

  method private visit_array: 'env 'a 'b .
    ('env -> 'a -> 'b -> unit) -> 'env -> 'a array -> 'b array -> unit
  = fun f env xs1 xs2 ->
      (* We inline [Array.iter2]. *)
      if Array.length xs1 = Array.length xs2 then
        for i = 0 to Array.length xs1 - 1 do
          f env (Array.unsafe_get xs1 i) (Array.unsafe_get xs2 i)
        done
      else
        fail()

  method private visit_bool: 'env .
    'env -> bool -> bool -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_bytes: 'env .
    'env -> bytes -> bytes -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_char: 'env .
    'env -> char -> char -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_float: 'env .
    'env -> float -> float -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_int: 'env .
    'env -> int -> int -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_int32: 'env .
    'env -> int32 -> int32 -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_int64: 'env .
    'env -> int64 -> int64 -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_lazy_t: 'env 'a 'b .
    ('env -> 'a -> 'b -> unit) -> 'env -> 'a Lazy.t -> 'b Lazy.t -> unit
  = fun f env (lazy x1) (lazy x2) ->
      f env x1 x2

  method private visit_list: 'env 'a 'b .
    ('env -> 'a -> 'b -> unit) -> 'env -> 'a list -> 'b list -> unit
  = fun f env xs1 xs2 ->
      match xs1, xs2 with
      | [], [] ->
          ()
      | x1 :: xs1, x2 :: xs2 ->
          f env x1 x2;
          self # visit_list f env xs1 xs2
      | _, _ ->
          fail()

  method private visit_nativeint: 'env .
    'env -> nativeint -> nativeint -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_option: 'env 'a 'b .
    ('env -> 'a -> 'b -> unit) -> 'env -> 'a option -> 'b option -> unit
  = fun f env ox1 ox2 ->
      match ox1, ox2 with
      | None, None ->
          ()
      | Some x1, Some x2 ->
          f env x1 x2
      | _, _ ->
          fail()

  method private visit_ref: 'env 'a 'b .
    ('env -> 'a -> 'b -> unit) -> 'env -> 'a ref -> 'b ref -> unit
  = fun f env rx1 rx2 ->
      f env !rx1 !rx2

  method private visit_result: 'env 'a 'b 'e 'f .
    ('env -> 'a -> 'b -> unit) ->
    ('env -> 'e -> 'f -> unit) ->
     'env -> ('a, 'e) result -> ('b, 'f) result -> unit
  = fun f g env r1 r2 ->
      match r1, r2 with
      | Ok a1, Ok a2 -> f env a1 a2
      | Error b1, Error b2 -> g env b1 b2
      | _, _ -> fail()

  method private visit_string: 'env .
    'env -> string -> string -> unit
  = fun _ x1 x2 -> if x1 = x2 then () else fail()

  method private visit_unit: 'env .
    'env -> unit -> unit -> unit
  = fun _ _x1 _x2 -> ()

end

(* -------------------------------------------------------------------------- *)

(* [map2] *)

class ['self] map2 = object (self)

  method private visit_array: 'env 'a 'b 'c .
    ('env -> 'a -> 'b -> 'c) -> 'env -> 'a array -> 'b array -> 'c array
  = fun f env xs1 xs2 ->
      if Array.length xs1 = Array.length xs2 then
        Array.mapi (fun i x1 -> f env x1 xs2.(i)) xs1
        (* Array.map2 (f env) xs1 xs2 *)
        (* We avoid [Array.map2] because it does not exist in OCaml 4.02. *)
      else
        fail()

  method private visit_bool: 'env .
    'env -> bool -> bool -> bool
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_bytes: 'env .
    'env -> bytes -> bytes -> bytes
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_char: 'env .
    'env -> char -> char -> char
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_float: 'env .
    'env -> float -> float -> float
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_int: 'env .
    'env -> int -> int -> int
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_int32: 'env .
    'env -> int32 -> int32 -> int32
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_int64: 'env .
    'env -> int64 -> int64 -> int64
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_lazy_t: 'env 'a 'b 'c .
    ('env -> 'a -> 'b -> 'c) -> 'env -> 'a Lazy.t -> 'b Lazy.t -> 'c Lazy.t
  = fun f env thx1 thx2 ->
      (* As in [map]. *)
      lazy (f env (Lazy.force thx1) (Lazy.force thx2))

  method private visit_list: 'env 'a 'b 'c .
    ('env -> 'a -> 'b -> 'c) -> 'env -> 'a list -> 'b list -> 'c list
  = fun f env xs1 xs2 ->
      match xs1, xs2 with
      | [], [] ->
          []
      | x1 :: xs1, x2 :: xs2 ->
          let x = f env x1 x2 in
          x :: self # visit_list f env xs1 xs2
      | _, _ ->
          fail()

  method private visit_nativeint: 'env .
    'env -> nativeint -> nativeint -> nativeint
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_option: 'env 'a 'b 'c .
    ('env -> 'a -> 'b -> 'c) -> 'env -> 'a option -> 'b option -> 'c option
  = fun f env ox1 ox2 ->
      match ox1, ox2 with
      | None, None ->
          None
      | Some x1, Some x2 ->
          let x = f env x1 x2 in
          Some x
      | _, _ ->
          fail()

  method private visit_ref: 'env 'a 'b 'c .
    ('env -> 'a -> 'b -> 'c) -> 'env -> 'a ref -> 'b ref -> 'c ref
  = fun f env rx1 rx2 ->
      ref (f env !rx1 !rx2)

  method private visit_result: 'env 'a 'b 'c 'e 'f 'g .
    ('env -> 'a -> 'b -> 'c) ->
    ('env -> 'e -> 'f -> 'g) ->
     'env -> ('a, 'e) result -> ('b, 'f) result -> ('c, 'g) result
  = fun f g env r1 r2 ->
      match r1, r2 with
      | Ok a1, Ok a2 -> Ok (f env a1 a2)
      | Error b1, Error b2 -> Error (g env b1 b2)
      | _, _ -> fail()

  method private visit_string: 'env .
    'env -> string -> string -> string
  = fun _ x1 x2 -> if x1 = x2 then x1 else fail()

  method private visit_unit: 'env .
    'env -> unit -> unit -> unit
  = fun _ _x1 _x2 -> ()

end

(* -------------------------------------------------------------------------- *)

(* [reduce2] *)

class virtual ['self] reduce2 = object (self : 'self)

  inherit ['s] monoid

  method private visit_array: 'env 'a 'b .
    ('env -> 'a -> 'b -> 's) -> 'env -> 'a array -> 'b array -> 's
  = fun f env xs1 xs2 ->
      (* OCaml does not offer [Array.fold_left2], so we use [Array.iter2],
         which we inline. *)
      if Array.length xs1 = Array.length xs2 then
        let s = ref self#zero in
        for i = 0 to Array.length xs1 - 1 do
          let x1 = Array.unsafe_get xs1 i
          and x2 = Array.unsafe_get xs2 i in
          s := self#plus !s (f env x1 x2)
        done;
        !s
      else
        fail()

  method private visit_bool: 'env .
    'env -> bool -> bool -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_bytes: 'env .
    'env -> bytes -> bytes -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_char: 'env .
    'env -> char -> char -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_float: 'env .
    'env -> float -> float -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_int: 'env .
    'env -> int -> int -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_int32: 'env .
    'env -> int32 -> int32 -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_int64: 'env .
    'env -> int64 -> int64 -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_lazy_t: 'env 'a 'b .
    ('env -> 'a -> 'b -> 's) -> 'env -> 'a Lazy.t -> 'b Lazy.t -> 's
  = fun f env (lazy x1) (lazy x2) ->
      f env x1 x2

  method private visit_list: 'env 'a 'b .
    ('env -> 'a -> 'b -> 's) -> 'env -> 'a list -> 'b list -> 's
  = fun f env xs1 xs2 ->
      if List.length xs1 = List.length xs2 then
        List.fold_left2 (fun s x1 x2 -> self#plus s (f env x1 x2)) self#zero xs1 xs2
      else
        fail()

  method private visit_nativeint: 'env .
    'env -> nativeint -> nativeint -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_option: 'env 'a 'b .
    ('env -> 'a -> 'b -> 's) -> 'env -> 'a option -> 'b option -> 's
  = fun f env ox1 ox2 ->
      match ox1, ox2 with
      | Some x1, Some x2 ->
          f env x1 x2
      | None, None ->
          self#zero
      | Some _, None
      | None, Some _ ->
          fail()

  method private visit_ref: 'env 'a 'b .
    ('env -> 'a -> 'b -> 's) -> 'env -> 'a ref -> 'b ref -> 's
  = fun f env rx1 rx2 ->
      f env !rx1 !rx2

  method private visit_result: 'env 'a 'b 'e 'f .
    ('env -> 'a -> 'b -> 's) ->
    ('env -> 'e -> 'f -> 's) ->
     'env -> ('a, 'e) result -> ('b, 'f) result -> 's
  = fun f g env r1 r2 ->
      match r1, r2 with
      | Ok a1, Ok a2 ->
          f env a1 a2
      | Error b1, Error b2 ->
          g env b1 b2
      | Ok _, Error _
      | Error _, Ok _ ->
          fail()

  method private visit_string: 'env .
    'env -> string -> string -> 's
  = fun _env x1 x2 ->
      if x1 = x2 then self#zero else fail()

  method private visit_unit: 'env .
    'env -> unit -> unit -> 's
  = fun _env () () ->
      self#zero

end

(* -------------------------------------------------------------------------- *)

(* [mapreduce2] *)

class virtual ['self] mapreduce2 = object (self)

  inherit ['s] monoid

  method private visit_array: 'env 'a 'b 'c .
    ('env -> 'a -> 'b -> 'c * 's) -> 'env -> 'a array -> 'b array -> 'c array * 's
  = fun f env xs1 xs2 ->
      let n1 = Array.length xs1
      and n2 = Array.length xs2 in
      if n1 = n2 then
        let s = ref self#zero in
        let xs = Array.init n1 (fun i ->
          let x1 = Array.unsafe_get xs1 i
          and x2 = Array.unsafe_get xs2 i in
          let x, sx = f env x1 x2 in
          s := self#plus !s sx;
          x
        ) in
        xs, !s
      else
        fail()

  method private visit_bool: 'env .
    'env -> bool -> bool -> bool * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_bytes: 'env .
    'env -> bytes -> bytes -> bytes * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_char: 'env .
    'env -> char -> char -> char * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_float: 'env .
    'env -> float -> float -> float * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_int: 'env .
    'env -> int -> int -> int * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_int32: 'env .
    'env -> int32 -> int32 -> int32 * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_int64: 'env .
    'env -> int64 -> int64 -> int64 * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_lazy_t: 'env 'a 'b 'c .
    ('env -> 'a -> 'b -> 'c * 's) -> 'env -> 'a Lazy.t -> 'b Lazy.t -> 'c Lazy.t * 's
  = fun f env (lazy x1) (lazy x2) ->
      (* As in [mapreduce]. *)
      let y, s = f env x1 x2 in
      lazy y, s

  method private visit_list: 'env 'a_0 'a_1 'a_2 .
    ('env -> 'a_0 -> 'a_1 -> 'a_2 * 's) ->
    'env -> 'a_0 list -> 'a_1 list -> 'a_2 list * 's
  = fun visit_'a env this_0 this_1 ->
      match this_0, this_1 with
      | [], [] ->
          [], self#zero
      | c0_0 :: c1_0, c0_1 :: c1_1 ->
          let r0, s0 = visit_'a env c0_0 c0_1 in
          let r1, s1 = self#visit_list visit_'a env c1_0 c1_1 in
          r0 :: r1, self#plus s0 s1
      | _, _ ->
          fail()

  method private visit_nativeint: 'env .
    'env -> nativeint -> nativeint -> nativeint * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_option: 'env 'a_0 'a_1 'a_2 .
    ('env -> 'a_0 -> 'a_1 -> 'a_2 * 's) ->
    'env -> 'a_0 option -> 'a_1 option -> 'a_2 option * 's
  = fun visit_'a env this_0 this_1 ->
      match this_0, this_1 with
      | None, None ->
          None, self#zero
      | Some c0_0, Some c0_1 ->
          let r0, s0 = visit_'a env c0_0 c0_1 in
          Some r0, s0
      | _, _ ->
          fail()

  method private visit_ref: 'env 'a_0 'a_1 'a_2 .
    ('env -> 'a_0 -> 'a_1 -> 'a_2 * 's) ->
    'env -> 'a_0 ref -> 'a_1 ref -> 'a_2 ref * 's
  = fun visit_'a env this_0 this_1 ->
      let r0, s0 = visit_'a env !this_0 !this_1 in
      ref r0, s0

  method private visit_result: 'env 'a_0 'a_1 'a_2 'b_0 'b_1 'b_2 .
    ('env -> 'a_0 -> 'a_1 -> 'a_2 * 's) ->
    ('env -> 'b_0 -> 'b_1 -> 'b_2 * 's) ->
    'env -> ('a_0, 'b_0) result -> ('a_1, 'b_1) result -> ('a_2, 'b_2) result * 's
  = fun visit_'a visit_'b env this_0 this_1 ->
      match this_0, this_1 with
      | Ok c0_0, Ok c0_1 ->
          let r0, s0 = visit_'a env c0_0 c0_1 in
          Ok r0, s0
      | Error c0_0, Error c0_1 ->
          let r0, s0 = visit_'b env c0_0 c0_1 in
          Error r0, s0
      | _, _ ->
          fail()

  method private visit_string: 'env .
    'env -> string -> string -> string * 's
  = fun _ x1 x2 -> if x1 = x2 then x1, self#zero else fail()

  method private visit_unit: 'env .
    'env -> unit -> unit -> unit * 's
  = fun _ () () -> (), self#zero

end

(* -------------------------------------------------------------------------- *)

(* [fold2] *)

class ['self] fold2 = object (_self)

  (* See the comment in the class [fold] above. *)

end
OCaml

Innovation. Community. Security.