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
open Containers
module TS = Tuple.Set
type t = TS.t
let pp out b =
Fmtc.pf out "@[<hov 2>{";
TS.pp ~sep:" " Tuple.pp out b;
Fmtc.pf out "}@]"
module P = Intf.Print.Mixin (struct
type nonrec t = t
let pp = pp
end)
include P
let to_list = TS.elements
let to_iter = TS.to_iter
let of_iter = TS.of_iter
let empty = TS.empty
let of_tuples tuples =
match tuples with
| [] ->
empty
| t :: ts ->
let ar = Tuple.arity t in
assert (List.for_all (fun t2 -> Tuple.arity t2 = ar) ts);
TS.of_list tuples
let is_empty = TS.is_empty
let inferred_arity b = if is_empty b then 0 else Tuple.arity @@ TS.choose b
let singleton = TS.singleton
let add = TS.add
let tuples t = t
let inter b1 b2 = TS.inter b1 b2
let size bnd = TS.cardinal bnd
let subset b1 b2 = TS.subset b1 b2
let equal b1 b2 = TS.equal b1 b2
let compare b1 b2 = TS.compare b1 b2
let product b1 b2 =
let prod =
Iter.product (TS.to_iter b1) (TS.to_iter b2)
|> Iter.map Fun.(uncurry Tuple.( @@@ ))
|> TS.of_iter
in
assert (TS.cardinal prod = TS.cardinal b1 * TS.cardinal b2);
prod
let union b1 b2 = TS.union b1 b2
let diff = TS.diff
let map f ts = TS.to_iter ts |> Iter.map f |> TS.of_iter
let filter = TS.filter
let transpose b =
let ar = inferred_arity b in
assert (ar = 2 || ar = 0);
map Tuple.transpose b
let override r s =
let in_r_but_not_in_s1 =
filter
(fun tr ->
not
@@ TS.exists (fun ts1 -> Tuple.(Atom.equal (ith 0 tr) (ith 0 ts1))) s)
r
in
TS.union s in_r_but_not_in_s1
let lproj s r = filter (fun tr -> TS.mem Tuple.([ ith 0 tr ] |> of_list1) s) r
let rproj r s = lproj s @@ transpose r
let diagonal b = map Tuple.(fun e -> e @@@ e) b
let join b1 b2 =
let module S = Iter in
let ar1 = inferred_arity b1 in
let ar2 = inferred_arity b2 in
assert (ar1 <> 1 || ar2 <> 1);
let s1 = to_iter b1 in
let s2 = to_iter b2 in
S.product s1 s2
|> S.filter_map (fun (t1, t2) ->
if Atom.equal (Tuple.ith (ar1 - 1) t1) (Tuple.ith 0 t2)
then Some (Tuple.join t1 t2)
else None)
|> of_iter
let transitive_closure b =
let ar = inferred_arity b in
assert (ar = 2 || ar = 0);
if ar = 0
then b
else
let old = ref b in
let cur = ref (union b (join b b)) in
let b_to_the_k = ref (join b b) in
while not @@ TS.equal !old !cur do
old := !cur;
b_to_the_k := join b !b_to_the_k;
cur := union !cur !b_to_the_k
done;
!cur
let transitive_closure_is b =
let ar = inferred_arity b in
assert (ar = 2 || ar = 0);
if ar = 0
then b
else
let old = ref b in
let cur = ref (union b (join b b)) in
while not @@ TS.equal !old !cur do
old := !cur;
cur := union !cur (join !cur !cur)
done;
!cur
let mem t bnd = TS.mem t bnd
let rename atom_renaming ts = TS.map (Tuple.rename atom_renaming) ts