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
(** Disjunctive normal form. *)
type 'a t = 'a list list
let singleton (a: 'a) : 'a t = [[a]]
let mk_true : 'a t = [[]]
let mk_false : 'a t = []
let is_true a = a = [[]]
let is_false a = a = []
let rec mk_and
(a: 'a t) (b: 'a t) : 'a t
=
if a == b then a else
if is_true a then b else
if is_true b then a
else
List.fold_left
(fun acc conj1 ->
List.fold_left
(fun acc conj2 ->
let conj = conj1 @ conj2 in
mk_or acc [conj]
) acc b
) mk_false a
and mk_or
(a: 'a t) (b: 'a t) : 'a t
=
if a == b then a else
if is_false a then b else
if is_false b then a
else a @ b
and mk_neg neg (a: 'a t) : 'a t =
a |> List.fold_left (fun acc conj ->
mk_and acc (
conj |>
List.fold_left (fun acc x ->
mk_or acc (neg x)
) []
)
) [[]]
let is_empty a = is_false a || is_true a
let map
(f: 'a -> 'b)
(dnf: 'a t)
: 'b t =
match dnf with
| [] -> []
| [[x]] -> [[f x]]
| [[x]; [y]] -> [[f x]; [f y]]
| _ ->
List.map (List.map f) dnf
let map_conjunction
(f:'a list -> 'b list)
(dnf: 'a t)
: 'b t =
List.map (fun conj -> f conj) dnf
let rec to_cnf (dnf:'a t) : 'a list list =
match dnf with
| [] -> [[]]
| conj::tl ->
let next = to_cnf tl in
List.fold_left
(fun acc x ->
List.fold_left
(fun acc nexti -> (x::nexti) :: acc)
acc next
)
[] conj
let from_cnf (cnf:'a list list) : 'a t = to_cnf cnf
let map_disjunction
(f: 'a list -> 'b list)
(dnf: 'a t)
: 'b t =
let cnf = to_cnf dnf in
let cnf' = List.map f cnf in
from_cnf cnf'
let iter
(f:'a -> unit)
(dnf:'a t)
: unit
=
List.iter (List.iter f) dnf
let reduce
(f: 'a -> 'b)
~(join: 'b -> 'b -> 'b)
~(meet: 'b -> 'b -> 'b)
(dnf: 'a t)
: 'b =
let rec apply_conj = function
| [] -> assert false
| [e] -> f e
| e :: tl -> meet (f e) (apply_conj tl)
in
let rec apply_disj = function
| [conj] -> apply_conj conj
| conj :: tl -> join (apply_conj conj) (apply_disj tl)
| _ -> assert false
in
apply_disj dnf
let fold_reduce
(f:'a -> 'b -> 'a * 'c)
~(join:'c -> 'c -> 'c)
~(meet:'c -> 'c -> 'c)
(init:'a)
(dnf:'b t) : 'a * 'c =
match dnf with
| [] -> assert false
| [[x]] -> f init x
| [[x];[y]] ->
let acc', x = f init x in
let acc'', y = f acc' y in
acc'', join x y
| _ ->
let rec apply_conj acc = function
| [e] -> f acc e
| e :: tl ->
let acc',x = f acc e in
let acc'',y = apply_conj acc' tl in
acc'',meet x y
| [] -> assert false
in
let rec apply_disj acc = function
| [conj] -> apply_conj acc conj
| conj :: tl ->
let acc',x = apply_conj acc conj in
let acc'',y = apply_disj acc' tl in
acc'',join x y
| _ -> assert false
in
apply_disj init dnf
let reduce_conjunction
(f: 'a list -> 'b)
~(join: 'b -> 'b -> 'b)
(dnf: 'a t)
: 'b =
let rec apply_disj = function
| [conj] -> f conj
| conj :: tl -> join (f conj) (apply_disj tl)
| _ -> assert false
in
apply_disj dnf
let fold_reduce_conjunction
(f: 'a -> 'b list -> 'a * 'c)
~(join: 'c -> 'c -> 'c)
(init:'a)
(dnf: 'b t)
: 'a * 'c =
let rec apply_disj acc = function
| [conj] -> f acc conj
| conj :: tl ->
let acc',x = f acc conj in
let acc'',y = apply_disj acc' tl in
acc', join x y
| _ -> assert false
in
apply_disj init dnf
let reduce_disjunction
(f: 'a list -> 'b)
~(meet: 'b -> 'b -> 'b)
(dnf: 'a t)
: 'b =
let cnf = to_cnf dnf in
let rec apply_conj = function
| [disj] -> f disj
| disj::tl -> meet (f disj) (apply_conj tl)
| _ -> assert false
in
apply_conj cnf
let fold_reduce_disjunction
(f: 'a -> 'b list -> 'a * 'c)
~(meet: 'c -> 'c -> 'c)
(init:'a)
(dnf: 'b t)
: 'a * 'c =
let cnf = to_cnf dnf in
let rec apply_conj acc = function
| [disj] -> f acc disj
| disj::tl ->
let acc',x = f acc disj in
let acc'',y = apply_conj acc' tl in
acc', meet x y
| _ -> assert false
in
apply_conj init cnf
let bind
(f: 'a -> 'b t)
(dnf: 'a t) : 'b t =
reduce f ~join:mk_or ~meet:mk_and dnf
let fold_bind
(f: 'a -> 'b -> 'a * 'c t)
(init:'a)
(dnf: 'b t) : 'a * 'c t =
fold_reduce f ~join:mk_or ~meet:mk_and init dnf
let bind_conjunction
(f: 'a list -> 'b t)
(dnf: 'a t) : 'b t =
reduce_conjunction f ~join:mk_or dnf
let fold_bind_conjunction
(f: 'a -> 'b list -> 'a * 'c t)
(init:'a)
(dnf: 'b t) : 'a * 'c t =
fold_reduce_conjunction f ~join:mk_or init dnf
let bind_disjunction
(f: 'a list -> 'b t)
(dnf: 'a t) : 'b t =
reduce_disjunction f ~meet:mk_and dnf
let fold_bind_disjunction
(f: 'a -> 'b list -> 'a * 'c t)
(init:'a)
(dnf: 'b t) : 'a * 'c t =
fold_reduce_disjunction f ~meet:mk_and init dnf
let fold
(f: 'b -> 'a -> 'b)
(init: 'b)
(dnf: 'a t)
: 'b =
match dnf with
| [] -> init
| [[x]] -> f init x
| [[x]; [y]] -> f (f init x) y
| _ ->
List.fold_left (List.fold_left f) init dnf
let partition (f:'a -> bool) (a:'a t) =
let r1,r2 =
List.fold_left
(fun (acc1,acc2) conj ->
let conj1,conj2 = List.partition f conj in
let acc1' = if conj1 = [] then acc1 else mk_or acc1 [conj1] in
let acc2' = if conj2 = [] then acc2 else mk_or acc2 [conj2] in
acc1',acc2'
) (mk_false, mk_false) a in
if is_false r1 then None,Some a else
if is_false r2 then Some a, None
else Some r1, Some r2
let choose (dnf: 'a t) : 'a option =
match dnf with
| [] | [[]] -> None
| (hd :: _) :: _ -> Some hd
| _ -> assert false
let to_list (dnf: 'a t) : 'a list list = dnf
let from_list (l: 'a list list) : 'a t = l
let print pp fmt (dnf:'a t) =
let open Format in
if is_true dnf then pp_print_string fmt "true" else
if is_false dnf then pp_print_string fmt "false" else
let l = to_list dnf in
fprintf fmt "@[<hv 2>%a@]"
(pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt " ∨@;")
(fun fmt conj ->
match conj with
| [] -> ()
| [e] -> pp fmt e
| _ ->
fprintf fmt "(@[<hv 2>@,%a@;@])"
(pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt " ∧@;")
pp
)
conj
)
)
l
let cardinal (dnf:'a t) : int =
List.fold_left (fun acc conj ->
List.length conj + acc
) 0 dnf