aboutsummaryrefslogtreecommitdiffstats
path: root/lib/prelude/standard.ml
blob: 38227002e5e0ded43906c3153868928850044393 (plain) (blame)
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
(** Comparison and arithmetic relations over the natural domain. *)

(* TODO: all relations here are hardcoded to naturals. The make_comparison
   factory accepts a domain_name string but membership_criteria always uses
   Stdlib.compare on Obj.t, which only works correctly for unboxed integers.
   Extending to other domains requires a proper typed comparison dispatch. *)

let mk v = {Attribute.value= Obj.repr v}

(** Build a comparison relation over a single domain. [pred] receives the result
    of [Stdlib.compare left right]. *)
let make_comparison ~name ~domain_name ~pred ~cardinality ~generator =
  let schema = Schema.empty |> Schema.add "left" domain_name |> Schema.add "right" domain_name in
  let membership_criteria : (*(string -> Merkle.t option) ->*) Tuple.t -> bool =
    (* fun _tree_of -> function *)
    function
    | Tuple.Materialized m -> (
      match
        ( Tuple.AttributeMap.find_opt "left" m.attributes,
          Tuple.AttributeMap.find_opt "right" m.attributes )
      with
      | Some l, Some r -> pred (Stdlib.compare l.Attribute.value r.Attribute.value)
      | _ -> false )
    | Tuple.NonMaterialized _ -> false
  in
  new Relation.ephemeral
    ~name ~schema ~constraints:None ~cardinality ~generator ~membership_criteria ~provenance:None
    ~lineage:None

(** Enumerate pairs (a, b) where a < b using triangular indexing. *)
let pair_of_nat_lt n =
  let nf = float_of_int n in
  let r = int_of_float (floor ((1. +. sqrt (1. +. (8. *. nf))) /. 2.)) in
  let t_prev = r * (r - 1) / 2 in
  let left = n - t_prev in
  left, r

(** Enumerate all pairs (a, b) via Cantor pairing. *)
let cantor_pair_of_nat n =
  let w = int_of_float (floor ((sqrt (float_of_int ((8 * n) + 1)) -. 1.) /. 2.)) in
  let t = w * (w + 1) / 2 in
  let b = n - t in
  let a = w - b in
  a, b

let less_than_natural : Relation.ephemeral =
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some position ->
        let left, right = pair_of_nat_lt position in
        let attributes : Attribute.materialized Tuple.AttributeMap.t =
          Tuple.AttributeMap.of_list ["left", mk left; "right", mk right]
        in
        Generator.Value (Tuple.make_materialized ~relation:"less_than" ~attributes, generator)
    | None -> Error "Cannot produce a randomly enumerated value for less_than over naturals."
  in
  make_comparison ~name:"natural_natural_less_than" ~domain_name:"natural"
    ~pred:(fun c -> c < 0)
    ~cardinality:Conventions.Cardinality.AlephZero ~generator

let less_than_or_equal_natural : Relation.ephemeral =
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        let a, b = cantor_pair_of_nat n in
        (* a <= b is always true when a <= b; we enumerate all (a, b) with a <= b *)
        let left = min a b in
        let right = max a b in
        let attributes = Tuple.AttributeMap.of_list ["left", mk left; "right", mk right] in
        Generator.Value
          (Tuple.make_materialized ~relation:"less_than_or_equal" ~attributes, generator)
    | None -> Error "Cannot enumerate less_than_or_equal randomly."
  in
  make_comparison ~name:"natural_natural_less_than_or_equal" ~domain_name:"natural"
    ~pred:(fun c -> c <= 0)
    ~cardinality:Conventions.Cardinality.AlephZero ~generator

let greater_than_natural : Relation.ephemeral =
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        let left, right = pair_of_nat_lt n in
        (* Swap: right > left *)
        let attributes = Tuple.AttributeMap.of_list ["left", mk right; "right", mk left] in
        Generator.Value (Tuple.make_materialized ~relation:"greater_than" ~attributes, generator)
    | None -> Error "Cannot enumerate greater_than randomly."
  in
  make_comparison ~name:"natural_natural_greater_than" ~domain_name:"natural"
    ~pred:(fun c -> c > 0)
    ~cardinality:Conventions.Cardinality.AlephZero ~generator

let greater_than_or_equal_natural : Relation.ephemeral =
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        let a, b = cantor_pair_of_nat n in
        let left = max a b in
        let right = min a b in
        let attributes = Tuple.AttributeMap.of_list ["left", mk left; "right", mk right] in
        Generator.Value
          (Tuple.make_materialized ~relation:"greater_than_or_equal" ~attributes, generator)
    | None -> Error "Cannot enumerate greater_than_or_equal randomly."
  in
  make_comparison ~name:"natural_natural_greater_than_or_equal" ~domain_name:"natural"
    ~pred:(fun c -> c >= 0)
    ~cardinality:Conventions.Cardinality.AlephZero ~generator

let equal_natural : Relation.ephemeral =
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        let attributes = Tuple.AttributeMap.of_list ["left", mk n; "right", mk n] in
        Generator.Value (Tuple.make_materialized ~relation:"equal" ~attributes, generator)
    | None -> Error "Cannot enumerate equal randomly."
  in
  make_comparison ~name:"natural_natural_equal" ~domain_name:"natural"
    ~pred:(fun c -> c = 0)
    ~cardinality:Conventions.Cardinality.AlephZero ~generator

let not_equal_natural : Relation.ephemeral =
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        let a, b = cantor_pair_of_nat n in
        let left, right = if a = b then a, b + 1 else a, b in
        let attributes = Tuple.AttributeMap.of_list ["left", mk left; "right", mk right] in
        Generator.Value (Tuple.make_materialized ~relation:"not_equal" ~attributes, generator)
    | None -> Error "Cannot enumerate not_equal randomly."
  in
  make_comparison ~name:"natural_natural_not_equal" ~domain_name:"natural"
    ~pred:(fun c -> c <> 0)
    ~cardinality:Conventions.Cardinality.AlephZero ~generator

let plus_natural : Relation.ephemeral =
  let schema =
    Schema.empty
    |> Schema.add "a" "natural"
    |> Schema.add "b" "natural"
    |> Schema.add "sum" "natural"
  in
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        let a, b = cantor_pair_of_nat n in
        let s = a + b in
        let attributes = Tuple.AttributeMap.of_list ["a", mk a; "b", mk b; "sum", mk s] in
        Generator.Value (Tuple.make_materialized ~relation:"plus" ~attributes, generator)
    | None -> Error "Cannot enumerate plus randomly."
  in
  let membership_criteria : Tuple.t -> bool =
    (* fun _tree_of ->  *)
    function
    | Tuple.Materialized m -> (
      match
        ( Tuple.AttributeMap.find_opt "a" m.attributes,
          Tuple.AttributeMap.find_opt "b" m.attributes,
          Tuple.AttributeMap.find_opt "sum" m.attributes )
      with
      | Some a, Some b, Some s ->
          (Obj.magic a.Attribute.value : int) + (Obj.magic b.Attribute.value : int)
          = (Obj.magic s.Attribute.value : int)
      | _ -> false )
    | Tuple.NonMaterialized _ -> false
  in
  new Relation.ephemeral
    ~name:"natural_plus" ~schema ~constraints:None ~cardinality:Conventions.Cardinality.AlephZero
    ~generator ~membership_criteria ~provenance:None ~lineage:None

let times_natural : Relation.ephemeral =
  let schema =
    Schema.empty
    |> Schema.add "a" "natural"
    |> Schema.add "b" "natural"
    |> Schema.add "product" "natural"
  in
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        let a, b = cantor_pair_of_nat n in
        let p = a * b in
        let attributes = Tuple.AttributeMap.of_list ["a", mk a; "b", mk b; "product", mk p] in
        Generator.Value (Tuple.make_materialized ~relation:"times" ~attributes, generator)
    | None -> Error "Cannot enumerate times randomly."
  in
  let membership_criteria : Tuple.t -> bool =
    (* fun _tree_of -> *)
    function
    | Tuple.Materialized m -> (
      match
        ( Tuple.AttributeMap.find_opt "a" m.attributes,
          Tuple.AttributeMap.find_opt "b" m.attributes,
          Tuple.AttributeMap.find_opt "product" m.attributes )
      with
      | Some a, Some b, Some p ->
          (Obj.magic a.Attribute.value : int) * (Obj.magic b.Attribute.value : int)
          = (Obj.magic p.Attribute.value : int)
      | _ -> false )
    | Tuple.NonMaterialized _ -> false
  in
  new Relation.ephemeral
    ~name:"natural_times" ~schema ~constraints:None ~cardinality:Conventions.Cardinality.AlephZero
    ~generator ~membership_criteria ~provenance:None ~lineage:None

let minus_natural : Relation.ephemeral =
  let schema =
    Schema.empty
    |> Schema.add "a" "natural"
    |> Schema.add "b" "natural"
    |> Schema.add "difference" "natural"
  in
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        (* Enumerate pairs where a >= b, so difference >= 0 *)
        let b, diff = cantor_pair_of_nat n in
        let a = b + diff in
        let attributes = Tuple.AttributeMap.of_list ["a", mk a; "b", mk b; "difference", mk diff] in
        Generator.Value (Tuple.make_materialized ~relation:"minus" ~attributes, generator)
    | None -> Error "Cannot enumerate minus randomly."
  in
  let membership_criteria : Tuple.t -> bool =
    (* fun _tree_of ->  *)
    function
    | Tuple.Materialized m ->
      begin match
        ( Tuple.AttributeMap.find_opt "a" m.attributes,
          Tuple.AttributeMap.find_opt "b" m.attributes,
          Tuple.AttributeMap.find_opt "difference" m.attributes )
      with
      | Some a, Some b, Some d ->
          let av = (Obj.magic a.Attribute.value : int) in
          let bv = (Obj.magic b.Attribute.value : int) in
          let dv = (Obj.magic d.Attribute.value : int) in
          av - bv = dv && dv >= 0
      | _ -> false
      end
    | Tuple.NonMaterialized _ -> false
  in
  new Relation.ephemeral
    ~name:"natural_minus" ~schema ~constraints:None ~cardinality:Conventions.Cardinality.AlephZero
    ~generator ~membership_criteria ~provenance:None ~lineage:None

let divide_natural : Relation.ephemeral =
  let schema =
    Schema.empty
    |> Schema.add "a" "natural"
    |> Schema.add "b" "natural"
    |> Schema.add "quotient" "natural"
    |> Schema.add "remainder" "natural"
  in
  let rec generator (position : int option) : Generator.result =
    match position with
    | Some n ->
        (* Enumerate (b, q, r) triples where b > 0, then a = b*q + r, 0 <= r < b *)
        (* We use a nested Cantor pairing: n -> (b_idx, qr_idx) -> (q, r) *)
        let b_idx, qr_idx = cantor_pair_of_nat n in
        let b = b_idx + 1 in
        (* b > 0 *)
        let q, r_raw = cantor_pair_of_nat qr_idx in
        let r = r_raw mod b in
        let a = (b * q) + r in
        let attributes =
          Tuple.AttributeMap.of_list ["a", mk a; "b", mk b; "quotient", mk q; "remainder", mk r]
        in
        Generator.Value (Tuple.make_materialized ~relation:"divide" ~attributes, generator)
    | None -> Error "Cannot enumerate divide randomly."
  in
  let membership_criteria : Tuple.t -> bool =
    (* fun _tree_of ->  *)
    function
    | Tuple.Materialized m -> (
      match
        ( Tuple.AttributeMap.find_opt "a" m.attributes,
          Tuple.AttributeMap.find_opt "b" m.attributes,
          Tuple.AttributeMap.find_opt "quotient" m.attributes,
          Tuple.AttributeMap.find_opt "remainder" m.attributes )
      with
      | Some a, Some b, Some q, Some r ->
          let av = (Obj.magic a.Attribute.value : int) in
          let bv = (Obj.magic b.Attribute.value : int) in
          let qv = (Obj.magic q.Attribute.value : int) in
          let rv = (Obj.magic r.Attribute.value : int) in
          bv > 0 && av = (bv * qv) + rv && rv >= 0 && rv < bv
      | _ -> false )
    | Tuple.NonMaterialized _ -> false
  in
  new Relation.ephemeral
    ~name:"natural_divide" ~schema ~constraints:None ~cardinality:Conventions.Cardinality.AlephZero
    ~generator ~membership_criteria ~provenance:None ~lineage:None

let prelude_relations =
  [ less_than_natural;
    less_than_or_equal_natural;
    greater_than_natural;
    greater_than_or_equal_natural;
    equal_natural;
    not_equal_natural;
    plus_natural;
    times_natural;
    minus_natural;
    divide_natural ]