jon.recoil.org

Source file scalar.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
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*                 Jacob Van Buren, Jane Street, New York                 *)
(*                                                                        *)
(*   Copyright 2025 Jane Street Group LLC                                 *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

type any_locality_mode = Any_locality_mode

let equal_any_locality_mode Any_locality_mode Any_locality_mode = true

let ignore_locality _ = Any_locality_mode

module Maybe_naked = struct
  type ('a, 'b) t =
    | Value of 'a
    | Naked of 'b

  module Make1 (M : sig
    type 'a t

    val all : any_locality_mode t list

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

    val to_string : any_locality_mode t -> string

    val naked_sort : any_locality_mode t -> Jkind_types.Sort.Const.t

    val equal : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
  end) =
  struct
    type nonrec 'a t = ('a M.t, any_locality_mode M.t) t

    let map (t : _ t) ~f =
      match t with
      | Naked (_ : any_locality_mode M.t) as t -> t
      | Value t -> Value (M.map t ~f)

    let width : _ t -> any_locality_mode M.t = function
      | Naked t -> t
      | Value t -> M.map t ~f:ignore_locality

    let to_string = function
      | Value m -> M.to_string m
      | Naked m -> M.to_string m ^ "#"

    let ignore_locality = map ~f:ignore_locality

    let all = List.concat_map (fun m -> [Value m; Naked m]) M.all

    let sort = function
      | Value (_ : any_locality_mode M.t) -> Jkind_types.Sort.Const.scannable
      | Naked t -> M.naked_sort t

    let equal eq x y =
      match x, y with
      | Value x, Value y -> M.equal eq x y
      | Value _, _ -> false
      | Naked x, Naked y -> M.equal equal_any_locality_mode x y
      | Naked _, _ -> false
  end
end

let naked n = Maybe_naked.Naked n

module Integral = struct
  module Taggable = struct
    module Width = struct
      type t =
        | Int8
        | Int16
        | Int

      let all = [Int8; Int16; Int]

      let to_string = function
        | Int8 -> "int8"
        | Int16 -> "int16"
        | Int -> "int"

      let naked_sort = function
        | Int8 -> Jkind_types.Sort.Const.bits8
        | Int16 -> Jkind_types.Sort.Const.bits16
        | Int -> Jkind_types.Sort.Const.untagged_immediate

      let equal _eq ((Int8 | Int16 | Int) as x) y = x = y
    end

    include Maybe_naked.Make1 (struct
      include Width

      type nonrec 'a t = t

      let map t ~f:_ = t
    end)
  end

  module Boxable = struct
    module Width = struct
      type 'mode t =
        | Int32 of 'mode
        | Nativeint of 'mode
        | Int64 of 'mode

      let all =
        [ Int32 Any_locality_mode;
          Nativeint Any_locality_mode;
          Int64 Any_locality_mode ]

      let map t ~f =
        match t with
        | Int32 mode -> Int32 (f mode)
        | Nativeint mode -> Nativeint (f mode)
        | Int64 mode -> Int64 (f mode)

      let to_string = function
        | Int32 Any_locality_mode -> "int32"
        | Nativeint Any_locality_mode -> "nativeint"
        | Int64 Any_locality_mode -> "int64"

      let naked_sort = function
        | Int32 Any_locality_mode -> Jkind_types.Sort.Const.bits32
        | Int64 Any_locality_mode -> Jkind_types.Sort.Const.bits64
        | Nativeint Any_locality_mode -> Jkind_types.Sort.Const.word

      let equal eq x y =
        match x, y with
        | Int32 x, Int32 y -> eq x y
        | Int32 _, _ -> false
        | Nativeint x, Nativeint y -> eq x y
        | Nativeint _, _ -> false
        | Int64 x, Int64 y -> eq x y
        | Int64 _, _ -> false
    end

    include Maybe_naked.Make1 (Width)
  end

  module Width = struct
    type 'mode t =
      | Taggable of Taggable.Width.t
      | Boxable of 'mode Boxable.Width.t

    let all =
      List.concat
        [ List.map (fun t -> Taggable t) Taggable.Width.all;
          List.map (fun t -> Boxable t) Boxable.Width.all ]

    let map t ~f =
      match t with
      | Taggable (Int8 | Int16 | Int) as t -> t
      | Boxable b -> Boxable (Boxable.Width.map b ~f)

    let to_string = function
      | Taggable t -> Taggable.Width.to_string t
      | Boxable b -> Boxable.Width.to_string b

    let naked_sort = function
      | Taggable t -> Taggable.Width.naked_sort t
      | Boxable b -> Boxable.Width.naked_sort b

    let equal eq x y =
      match x, y with
      | Taggable x, Taggable y -> Taggable.Width.equal eq x y
      | Taggable _, _ -> false
      | Boxable x, Boxable y -> Boxable.Width.equal eq x y
      | Boxable _, _ -> false
  end

  include Maybe_naked.Make1 (Width)
end

module Floating = struct
  module Width = struct
    type 'mode t =
      | Float32 of 'mode
      | Float64 of 'mode

    let all = [Float32 Any_locality_mode; Float64 Any_locality_mode]

    let map t ~f =
      match t with
      | Float32 mode -> Float32 (f mode)
      | Float64 mode -> Float64 (f mode)

    let to_string = function
      | Float32 Any_locality_mode -> "float32"
      | Float64 Any_locality_mode -> "float"

    let naked_sort = function
      | Float32 Any_locality_mode -> Jkind_types.Sort.Const.float32
      | Float64 Any_locality_mode -> Jkind_types.Sort.Const.float64

    let equal eq x y =
      match x, y with
      | Float32 x, Float32 y -> eq x y
      | Float32 _, _ -> false
      | Float64 x, Float64 y -> eq x y
      | Float64 _, _ -> false
  end

  include Maybe_naked.Make1 (Width)
end

module Width = struct
  type 'mode t =
    | Floating of 'mode Floating.Width.t
    | Integral of 'mode Integral.Width.t

  let all =
    List.concat
      [ List.map (fun t -> Floating t) Floating.Width.all;
        List.map (fun t -> Integral t) Integral.Width.all ]

  let map t ~f =
    match t with
    | Floating g -> Floating (Floating.Width.map g ~f)
    | Integral i -> Integral (Integral.Width.map i ~f)

  let naked_sort = function
    | Floating f -> Floating.Width.naked_sort f
    | Integral i -> Integral.Width.naked_sort i

  let ignore_locality = map ~f:ignore_locality

  let to_string = function
    | Floating f -> Floating.Width.to_string f
    | Integral i -> Integral.Width.to_string i

  let equal eq x y =
    match x, y with
    | Floating x, Floating y -> Floating.Width.equal eq x y
    | Floating _, _ -> false
    | Integral x, Integral y -> Integral.Width.equal eq x y
    | Integral _, _ -> false
end

include Maybe_naked.Make1 (Width)

let integral : 'a Integral.t -> 'a t = function
  | Value i -> Value (Integral i)
  | Naked i -> Naked (Integral i)

let floating : 'a Floating.t -> 'a t = function
  | Value f -> Value (Floating f)
  | Naked f -> Naked (Floating f)

module Signedness = struct
  type t =
    | Signed
    | Unsigned

  let all = [Signed; Unsigned]

  let equal ((Signed | Unsigned) as x : t) (y : t) =
    (* polymorphic equality is fast and simple when they are all constant constructors *)
    x = y

  let print ppf t =
    match t with
    | Signed -> Format.pp_print_string ppf "signed"
    | Unsigned -> Format.pp_print_string ppf "unsigned"
end

module Integer_comparison = struct
  type t =
    | Ceq
    | Cne
    | Clt
    | Cgt
    | Cle
    | Cge
    | Cult
    | Cugt
    | Cule
    | Cuge

  let all = [Ceq; Cne; Clt; Cgt; Cle; Cge; Cult; Cugt; Cule; Cuge]

  let equal
      ((Ceq | Cne | Clt | Cgt | Cle | Cge | Cult | Cugt | Cule | Cuge) as x : t)
      (y : t) =
    (* polymorphic equality is fast and simple when they are all constant constructors *)
    x = y

  let to_string = function
    | Ceq -> "equal"
    | Cne -> "notequal"
    | Cgt -> "greaterthan"
    | Cge -> "greaterequal"
    | Clt -> "lessthan"
    | Cle -> "lessequal"
    | Cugt -> "unsigned_greaterthan"
    | Cuge -> "unsigned_greaterequal"
    | Cult -> "unsigned_lessthan"
    | Cule -> "unsigned_lessequal"

  let negate = function
    | Ceq -> Cne
    | Cne -> Ceq
    | Clt -> Cge
    | Cle -> Cgt
    | Cgt -> Cle
    | Cge -> Clt
    | Cult -> Cuge
    | Cule -> Cugt
    | Cugt -> Cule
    | Cuge -> Cult

  let swap = function
    | Ceq -> Ceq
    | Cne -> Cne
    | Clt -> Cgt
    | Cle -> Cge
    | Cgt -> Clt
    | Cge -> Cle
    | Cult -> Cugt
    | Cule -> Cuge
    | Cugt -> Cult
    | Cuge -> Cule

  let create signedness ~lt ~eq ~gt =
    match lt, eq, gt, (signedness : Signedness.t) with
    | false, true, false, (Signed | Unsigned) -> Ok Ceq
    | true, false, true, (Signed | Unsigned) -> Ok Cne
    | true, false, false, Signed -> Ok Clt
    | false, false, true, Signed -> Ok Cgt
    | true, true, false, Signed -> Ok Cle
    | false, true, true, Signed -> Ok Cge
    | true, false, false, Unsigned -> Ok Cult
    | false, false, true, Unsigned -> Ok Cugt
    | true, true, false, Unsigned -> Ok Cule
    | false, true, true, Unsigned -> Ok Cuge
    | true, true, true, (Signed | Unsigned) -> Error true
    | false, false, false, (Signed | Unsigned) -> Error false
end

module Float_comparison = struct
  type t =
    | CFeq
    | CFneq
    | CFlt
    | CFnlt
    | CFgt
    | CFngt
    | CFle
    | CFnle
    | CFge
    | CFnge

  let all = [CFeq; CFneq; CFlt; CFnlt; CFgt; CFngt; CFle; CFnle; CFge; CFnge]

  let to_string = function
    | CFeq -> "ordered_and_equal"
    | CFgt -> "ordered_and_greaterthan"
    | CFge -> "ordered_and_greaterequal"
    | CFlt -> "ordered_and_lessthan"
    | CFle -> "ordered_and_lessequal"
    | CFneq -> "unordered_or_notequal"
    | CFngt -> "unordered_or_lessequal"
    | CFnge -> "unordered_or_lessthan"
    | CFnlt -> "unordered_or_greaterequal"
    | CFnle -> "unordered_or_greaterthan"

  let negate = function
    | CFeq -> CFneq
    | CFneq -> CFeq
    | CFlt -> CFnlt
    | CFnlt -> CFlt
    | CFgt -> CFngt
    | CFngt -> CFgt
    | CFle -> CFnle
    | CFnle -> CFle
    | CFge -> CFnge
    | CFnge -> CFge

  let swap = function
    | CFeq -> CFeq
    | CFneq -> CFneq
    | CFlt -> CFgt
    | CFnlt -> CFngt
    | CFle -> CFge
    | CFnle -> CFnge
    | CFgt -> CFlt
    | CFngt -> CFnlt
    | CFge -> CFle
    | CFnge -> CFnle
end

module Operation = struct
  type 'mode info =
    { can_raise : bool;
      result : 'mode t
    }

  (* CR jvanburen: nullary primitives for 0/1/-1/min/max once we can put
     unboxed values into structures?
  *)

  module Unary = struct
    (* Remember to update [all] right below this if you add a constructor *)
    module Int_op = struct
      type t =
        | Neg
        | Succ  (** add 1 *)
        | Pred  (** subtract 1 *)
        | Bswap

      let all = [Neg; Succ; Pred; Bswap]

      let to_string = function
        | Neg -> "neg"
        | Succ -> "succ"
        | Pred -> "pred"
        | Bswap -> "bswap"
    end

    module Float_op = struct
      type t =
        | Neg
        | Abs

      let all = [Neg; Abs]

      let to_string = function Neg -> "neg" | Abs -> "abs"
    end

    type nonrec 'mode t =
      (* CR jvanburen: logical Not, int Abs, float bitcast *)
      | Integral of 'mode Integral.t * Int_op.t
      | Floating of 'mode Floating.t * Float_op.t
      | Static_cast of
          { src : any_locality_mode t;
            dst : 'mode t
          }

    let all =
      List.concat
        [ ListLabels.concat_map Integral.all ~f:(fun size ->
              ListLabels.map Int_op.all ~f:(fun op -> Integral (size, op)));
          ListLabels.concat_map Floating.all ~f:(fun size ->
              ListLabels.map Float_op.all ~f:(fun op -> Floating (size, op)));
          ListLabels.concat_map all ~f:(fun src ->
              ListLabels.concat_map all ~f:(fun dst ->
                  if src = dst then [] else [Static_cast { src; dst }])) ]

    let map (type a b) (t : a t) ~(f : a -> b) : b t =
      match t with
      | Integral (size, op) -> Integral (Integral.map size ~f, op)
      | Floating (size, op) -> Floating (Floating.map size ~f, op)
      | Static_cast { src; dst } -> Static_cast { src; dst = map dst ~f }

    let info = function
      | Integral (size, (Neg | Bswap | Succ | Pred)) ->
        { result = integral size; can_raise = false }
      | Floating (size, (Neg | Abs)) ->
        { result = floating size; can_raise = false }
      | Static_cast { src = _; dst } -> { result = dst; can_raise = false }

    let to_string t =
      let i = Integral.to_string in
      let f = Floating.to_string in
      match t with
      | Integral (size, op) ->
        Printf.sprintf "%s_%s" (i size) (Int_op.to_string op)
      | Floating (size, op) ->
        Printf.sprintf "%s_%s" (f size) (Float_op.to_string op)
      | Static_cast { src; dst } ->
        Printf.sprintf "%s_of_%s" (to_string dst) (to_string src)

    let sort = function
      | Integral (width, _) ->
        let sort = Integral.sort width in
        sort, sort
      | Floating (width, _) ->
        let sort = Floating.sort width in
        sort, sort
      | Static_cast { src; dst } -> sort src, sort dst
  end

  module Binary = struct
    module Int_op = struct
      type division_is_safe =
        | Safe
        | Unsafe

      type t =
        | Add
        | Sub
        | Mul
        | Div of division_is_safe
        | Mod of division_is_safe
        | And
        | Or
        | Xor

      let all =
        [Add; Sub; Mul; Div Safe; Div Unsafe; Mod Safe; Mod Unsafe; And; Or; Xor]

      let to_string = function
        | Add -> "add"
        | Sub -> "sub"
        | Mul -> "mul"
        | Div Safe -> "div"
        | Div Unsafe -> "unsafe_div"
        | Mod Safe -> "mod"
        | Mod Unsafe -> "unsafe_mod"
        | And -> "and"
        | Or -> "or"
        | Xor -> "xor"
    end

    module Shift_op = struct
      module Rhs = struct
        (* CR jvanburen: expand this to all of [Integral.t] *)
        type t = Int

        let all = [Int]
      end

      type t =
        | Lsl
        | Asr
        | Lsr

      let all = [Lsl; Asr; Lsr]

      let to_string = function Lsl -> "lsl" | Asr -> "asr" | Lsr -> "lsr"
    end

    module Float_op = struct
      type t =
        | Add
        | Sub
        | Mul
        | Div

      let all = [Add; Sub; Mul; Div]

      let to_string = function
        | Add -> "add"
        | Sub -> "sub"
        | Mul -> "mul"
        | Div -> "div"
    end

    (* CR jvanburen: comparisons that return naked values *)

    (** comparisons return a tagged immediate *)
    type 'mode t =
      (* CR jvanburen: Fmod, Min, Max? *)
      | Integral of 'mode Integral.t * Int_op.t
      | Shift of 'mode Integral.t * Shift_op.t * Shift_op.Rhs.t
      | Floating of 'mode Floating.t * Float_op.t
      | Icmp of any_locality_mode Integral.t * Integer_comparison.t
      | Fcmp of any_locality_mode Floating.t * Float_comparison.t
      | Three_way_compare_int of Signedness.t * any_locality_mode Integral.t
      | Three_way_compare_float of any_locality_mode Floating.t

    let all =
      List.concat
        [ ListLabels.concat_map Integral.all ~f:(fun size ->
              ListLabels.map Int_op.all ~f:(fun op -> Integral (size, op)));
          ListLabels.concat_map Floating.all ~f:(fun size ->
              ListLabels.map Float_op.all ~f:(fun op -> Floating (size, op)));
          ListLabels.concat_map Integral.all ~f:(fun size ->
              ListLabels.concat_map Shift_op.all ~f:(fun op ->
                  ListLabels.map Shift_op.Rhs.all ~f:(fun rhs ->
                      Shift (size, op, rhs))));
          ListLabels.concat_map Integer_comparison.all ~f:(fun cmp ->
              List.map (fun size -> Icmp (size, cmp)) Integral.all);
          ListLabels.concat_map Float_comparison.all ~f:(fun cmp ->
              List.map (fun size -> Fcmp (size, cmp)) Floating.all);
          ListLabels.concat_map Signedness.all ~f:(fun signedness ->
              ListLabels.map Integral.all ~f:(fun size ->
                  Three_way_compare_int (signedness, size)));
          ListLabels.map Floating.all ~f:(fun size ->
              Three_way_compare_float size) ]

    let sort = function
      | Integral
          ( width,
            ( Add | Sub | Mul
            | Div (Safe | Unsafe)
            | Mod (Safe | Unsafe)
            | And | Or | Xor ) ) ->
        let sort = Integral.sort width in
        sort, sort, sort
      | Shift (width, (Lsl | Lsr | Asr), Int) ->
        let sort = Integral.sort width in
        sort, Jkind_types.Sort.Const.scannable, sort
      | Floating (width, (Add | Sub | Mul | Div)) ->
        let sort = Floating.sort width in
        sort, sort, sort
      | Icmp (width, (_ : Integer_comparison.t)) ->
        let sort = Integral.sort width in
        sort, sort, Jkind_types.Sort.Const.scannable
      | Fcmp (width, (_ : Float_comparison.t)) ->
        let sort = Floating.sort width in
        sort, sort, Jkind_types.Sort.Const.scannable
      | Three_way_compare_int ((Signed | Unsigned), width) ->
        let sort = Integral.sort width in
        sort, sort, Jkind_types.Sort.Const.scannable
      | Three_way_compare_float width ->
        let sort = Floating.sort width in
        sort, sort, Jkind_types.Sort.Const.scannable

    let to_string t =
      let make size name = String.concat "_" [to_string size; name] in
      match t with
      | Integral (size, op) -> make (integral size) (Int_op.to_string op)
      | Floating (size, op) -> make (floating size) (Float_op.to_string op)
      | Shift (size, op, Int) -> make (integral size) (Shift_op.to_string op)
      | Icmp (size, cmp) ->
        make (integral size) (Integer_comparison.to_string cmp)
      | Fcmp (size, cmp) ->
        make (floating size) (Float_comparison.to_string cmp)
      | Three_way_compare_int (Signed, size) -> make (integral size) "compare"
      | Three_way_compare_int (Unsigned, size) ->
        make (integral size) "unsigned_compare"
      | Three_way_compare_float size -> make (floating size) "compare"

    let map t ~f =
      match t with
      | Integral (size, op) -> Integral (Integral.map size ~f, op)
      | Floating (size, op) -> Floating (Floating.map size ~f, op)
      | Shift (size, op, rhs) -> Shift (Integral.map size ~f, op, rhs)
      | Icmp (size, cmp) -> Icmp (size, cmp)
      | Fcmp (size, cmp) -> Fcmp (size, cmp)
      | Three_way_compare_int (signedness, size) ->
        Three_way_compare_int (signedness, size)
      | Three_way_compare_float size -> Three_way_compare_float size

    let info = function
      | Integral
          (size, (Add | Sub | Mul | Div Unsafe | Mod Unsafe | And | Or | Xor))
      | Shift (size, (Lsl | Lsr | Asr), Int) ->
        { result = integral size; can_raise = false }
      | Integral (size, (Div Safe | Mod Safe)) ->
        { result = integral size; can_raise = true }
      | Floating (size, (Add | Sub | Mul | Div)) ->
        { result = floating size; can_raise = false }
      | Icmp ((_ : any_locality_mode Integral.t), (_ : Integer_comparison.t))
      | Fcmp ((_ : any_locality_mode Floating.t), (_ : Float_comparison.t))
      | Three_way_compare_int
          ((Signed | Unsigned), (_ : any_locality_mode Integral.t))
      | Three_way_compare_float (_ : any_locality_mode Floating.t) ->
        { result = integral (Value (Taggable Int)); can_raise = false }
  end

  type 'mode t =
    | Unary of 'mode Unary.t
    | Binary of 'mode Binary.t

  let all =
    List.concat
      [ List.map (fun u -> Unary u) Unary.all;
        List.map (fun b -> Binary b) Binary.all ]

  let sort = function
    | Unary u ->
      let x, y = Unary.sort u in
      [x; y]
    | Binary b ->
      let x, y, z = Binary.sort b in
      [x; y; z]

  let map t ~f =
    match t with
    | Unary u -> Unary (Unary.map u ~f)
    | Binary b -> Binary (Binary.map b ~f)

  let info = function Unary u -> Unary.info u | Binary b -> Binary.info b

  let to_string = function
    | Unary u -> Unary.to_string u
    | Binary b -> Binary.to_string b

  let arity = function Unary _ -> 1 | Binary _ -> 2

  let of_string =
    let module Tbl = Misc.Stdlib.String.Tbl in
    let tbl =
      lazy
        (let tbl = Tbl.create (2 * List.length all) in
         ListLabels.iter all ~f:(fun t -> Tbl.add tbl (to_string t) t);
         tbl)
    in
    fun s -> Tbl.find (Lazy.force tbl) s

  module With_percent_prefix = struct
    type nonrec t = any_locality_mode t

    let to_string t = "%" ^ to_string t

    let of_string s =
      let len = String.length s in
      if len = 0 || s.[0] <> '%'
      then raise Not_found
      else of_string (StringLabels.sub s ~pos:1 ~len:(len - 1))
  end
end