jon.recoil.org

Source file comparable.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
open! Import
include Comparable_intf

[%%template
[@@@mode.default m = (local, global)]

module%template.portable [@modality p] Infix =
  Base.Comparable.Infix
  [@mode m]
  [@modality p]

module%template.portable [@modality p] Comparisons =
  Base.Comparable.Comparisons
  [@modality p]
  [@mode m]

module%template.portable
  [@modality p] Validate (T : sig
    type t [@@deriving (compare [@mode m]), sexp_of]
  end) : Validate [@modality p] with type t := T.t = struct
  module V = Validate
  open Maybe_bound

  let to_string t = Base.Sexp.to_string (T.sexp_of_t t)

  [%%template
  [@@@mode.default p = (p, nonportable)]

  let validate_bound ~min ~max t =
    V.bounded ~name:to_string ~lower:min ~upper:max ~compare:T.compare t
  ;;

  let validate_lbound ~min t = (validate_bound [@mode p]) ~min ~max:Unbounded t
  let validate_ubound ~max t = (validate_bound [@mode p]) ~max ~min:Unbounded t]
end

module Validate_with_zero (T : sig
  @@ p
    type t : value mod c p [@@deriving (compare [@mode m]), sexp_of]

    val zero : t
  end) =
struct
  module V = Validate [@mode m] [@modality p] (T)
  include V

  (* Preallocate the interesting bounds to minimize allocation in the implementations of
     [validate_*]. *)
  let excl_zero = Maybe_bound.Excl T.zero
  let incl_zero = Maybe_bound.Incl T.zero

  [%%template
  [@@@mode.default p = (p, nonportable)]

  let validate_positive t = (validate_lbound [@mode p]) ~min:excl_zero t
  let validate_non_negative t = (validate_lbound [@mode p]) ~min:incl_zero t
  let validate_negative t = (validate_ubound [@mode p]) ~max:excl_zero t
  let validate_non_positive t = (validate_ubound [@mode p]) ~max:incl_zero t]
end
[@@modality (p, c) = ((nonportable, uncontended), (portable, contended))]

module With_zero (T : sig
  @@ p
    type t : value mod c p [@@deriving (compare [@mode m]), sexp_of]

    val zero : t
  end) =
struct
  include Validate_with_zero [@mode m] [@modality p] (T)
  include Base.Comparable.With_zero [@modality p] [@mode m] (T)
end
[@@modality (p, c) = ((nonportable, uncontended), (portable, contended))]

module%template.portable
  [@modality p] Map_and_set_binable_using_comparator (T : sig
    type t [@@deriving bin_io, (compare [@mode m]), sexp]

    include Comparator.S [@modality p] with type t := t
  end) =
struct
  include T
  module Map = Map.Make_binable_using_comparator [@modality p] (T)
  module Set = Set.Make_binable_using_comparator [@modality p] (T)
end

module%template.portable
  [@modality p] Map_and_set_binable (T : sig
    type t [@@deriving bin_io, (compare [@mode m]), sexp]
  end) =
Map_and_set_binable_using_comparator [@mode m] [@modality p] (struct
    include T
    include Comparator.Make [@modality p] (T)
  end)

module%template.portable
  [@modality p] Poly (T : sig
    type t [@@deriving sexp]
  end) =
struct
  module C = struct
    include T
    include Base.Comparable.Poly [@modality p] [@mode m] (T)
  end

  include C
  include Validate [@mode m] [@modality p] (C)

  module Replace_polymorphic_compare : sig @@ p
    include Comparisons [@mode m] with type t := t
  end =
    C

  module Map = Map.Make_using_comparator [@modality p] (C)
  module Set = Set.Make_using_comparator [@modality p] (C)
end

module%template.portable
  [@modality p] Make_plain_using_comparator (T : sig
    type t [@@deriving sexp_of]

    include Using_comparator_arg [@modality p] [@mode m] with type t := t
  end) :
  S_plain
  [@modality p] [@mode m]
  with type t := T.t
   and type comparator_witness = T.comparator_witness = struct
  include T
  module M = Base.Comparable.Make_using_comparator [@modality p] [@mode m] (T)
  include M

  include Validate [@mode m] [@modality p] (struct
      include T
      include M
    end)

  module Replace_polymorphic_compare : sig @@ p
    include Comparisons [@mode m] with type t := t
  end =
    M

  module Map = Map.Make_plain_using_comparator [@modality p] (T)
  module Set = Set.Make_plain_using_comparator [@modality p] (T)
end

module%template.portable
  [@modality p] Make_plain (T : sig
    type t [@@deriving (compare [@mode m]), sexp_of]
  end) =
Make_plain_using_comparator [@modality p] [@mode m] (struct
    include T
    include Comparator.Make [@modality p] (T)
  end)

module%template.portable
  [@modality p] Make_using_comparator (T : sig
    type t [@@deriving sexp]

    include Using_comparator_arg [@modality p] [@mode m] with type t := t
  end) :
  S
  [@modality p] [@mode m]
  with type t := T.t
   and type comparator_witness = T.comparator_witness = struct
  include T
  module M = Base.Comparable.Make_using_comparator [@modality p] [@mode m] (T)
  include M

  include Validate [@mode m] [@modality p] (struct
      include T
      include M
    end)

  module Replace_polymorphic_compare : sig @@ p
    include Comparisons [@mode m] with type t := t
  end =
    M

  module Map = Map.Make_using_comparator [@modality p] (T)
  module Set = Set.Make_using_comparator [@modality p] (T)
end

module%template.portable
  [@modality p] Make (T : sig
    type t [@@deriving (compare [@mode m]), sexp]
  end) : S [@modality p] [@mode m] with type t := T.t =
Make_using_comparator [@modality p] [@mode m] (struct
    include T
    include Comparator.Make [@modality p] (T)
  end)

module%template.portable
  [@modality p] Make_binable_using_comparator (T : sig
    type t [@@deriving bin_io, sexp]

    include Using_comparator_arg [@modality p] [@mode m] with type t := t
  end) =
struct
  include T
  module M = Base.Comparable.Make_using_comparator [@modality p] [@mode m] (T)

  include Validate [@mode m] [@modality p] (struct
      include T

      let compare = [%eta2 Comparator.compare T.comparator]
    end)

  include M

  module Replace_polymorphic_compare : sig @@ p
    include Comparisons [@mode m] with type t := t
  end =
    M

  module Map = Map.Make_binable_using_comparator [@modality p] (T)
  module Set = Set.Make_binable_using_comparator [@modality p] (T)
end

module%template.portable
  [@modality p] Make_binable (T : sig
    type t [@@deriving bin_io, (compare [@mode m]), sexp]
  end) =
struct
  include Make_binable_using_comparator [@modality p] [@mode m] (struct
      include T
      include Comparator.Make [@modality p] (T)
    end)
end

module%template.portable
  [@modality p] Extend_plain
    (M : Base.Comparable.S
  [@modality p] [@mode m])
    (X : sig
       type t = M.t [@@deriving sexp_of]
     end) =
struct
  module T = struct
    include M

    include (
      X :
        sig
        @@ p
          type t = M.t [@@deriving sexp_of]
        end
        with type t := t)
  end

  include T
  include Validate [@mode m] [@modality p] (T)

  module Replace_polymorphic_compare : sig @@ p
    include Comparisons [@mode m] with type t := t
  end =
    M

  module Map = Map.Make_plain_using_comparator [@modality p] (T)
  module Set = Set.Make_plain_using_comparator [@modality p] (T)
end

module%template.portable
  [@modality p] Extend
    (M : Base.Comparable.S
  [@modality p] [@mode m])
    (X : sig
       type t = M.t [@@deriving sexp]
     end) =
struct
  module T = struct
    include M

    include (
      X :
        sig
        @@ p
          type t = M.t [@@deriving sexp]
        end
        with type t := t)
  end

  include T
  include Validate [@mode m] [@modality p] (T)

  module Replace_polymorphic_compare : sig @@ p
    include Comparisons [@mode m] with type t := t
  end =
    M

  module Map = Map.Make_using_comparator [@modality p] (T)
  module Set = Set.Make_using_comparator [@modality p] (T)
end

module%template.portable
  [@modality p] Extend_binable
    (M : Base.Comparable.S
  [@modality p] [@mode m])
    (X : sig
       type t = M.t [@@deriving bin_io, sexp]
     end) =
struct
  module T = struct
    include M

    include (
      X :
        sig
        @@ p
          type t = M.t [@@deriving bin_io, sexp]
        end
        with type t := t)
  end

  include T
  include Validate [@mode m] [@modality p] (T)

  module Replace_polymorphic_compare : sig @@ p
    include Comparisons [@mode m] with type t := t
  end =
    M

  module Map = Map.Make_binable_using_comparator [@modality p] (T)
  module Set = Set.Make_binable_using_comparator [@modality p] (T)
end

module%template.portable
  [@modality p] Inherit
    (C : sig
       type t [@@deriving compare [@mode m]]
     end)
    (T : sig
       type t [@@deriving sexp]

       val component : t @ m -> C.t @ m
     end) =
Make [@modality p] [@mode m] (struct
    type t = T.t [@@deriving sexp]

    let%template compare t t' =
      (C.compare [@mode m]) (T.component t) (T.component t') [@nontail]
    [@@mode m' = (global, m)]
    ;;
  end)]

include (
  Base.Comparable :
  sig
  @@ portable
    include With_compare
  end)

module Stable = struct
  module V1 = struct
    module type S = sig
      type comparable
      type comparator_witness

      module Map :
        Map.Stable.V1.S
        with type key := comparable
        with type comparator_witness := comparator_witness

      module Set :
        Set.Stable.V1.S
        with type elt := comparable
        with type elt_comparator_witness := comparator_witness
    end

    module%template.portable [@modality p] Make (X : Stable_module_types.S0 [@modality p]) =
    struct
      module Map = Map.Stable.V1.Make [@modality p] (X)
      module Set = Set.Stable.V1.Make [@modality p] (X)
    end

    module With_stable_witness = struct
      module type S = sig
        type comparable
        type comparator_witness

        module Map :
          Map.Stable.V1.With_stable_witness.S
          with type key := comparable
          with type comparator_witness := comparator_witness

        module Set :
          Set.Stable.V1.With_stable_witness.S
          with type elt := comparable
          with type elt_comparator_witness := comparator_witness
      end

      module%template.portable
        [@modality p] Make
          (X : Stable_module_types.With_stable_witness.S0
        [@modality p]) =
      struct
        module Map = Map.Stable.V1.With_stable_witness.Make [@modality p] (X)
        module Set = Set.Stable.V1.With_stable_witness.Make [@modality p] (X)
      end
    end
  end
end