-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprop.ml
More file actions
490 lines (367 loc) · 18.2 KB
/
Copy pathprop.ml
File metadata and controls
490 lines (367 loc) · 18.2 KB
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
(* dummy commit *)
(* ========================================================================= *)
(* Basic stuff for propositional logic: datatype, parsing and printing. *)
(* ========================================================================= *)
type prop = P of string;;
let pname(P s) = s;;
(* ------------------------------------------------------------------------- *)
(* Parsing of propositional formulas. *)
(* ------------------------------------------------------------------------- *)
let parse_propvar vs inp =
match inp with
p::oinp when p <> "(" -> Atom(P(p)),oinp
| _ -> failwith "parse_propvar";;
let parse_prop_formula = make_parser
(parse_formula ((fun _ _ -> failwith ""),parse_propvar) []);;
(* ------------------------------------------------------------------------- *)
(* Set this up as default for quotations. *)
(* ------------------------------------------------------------------------- *)
let default_parser = parse_prop_formula;;
(* ------------------------------------------------------------------------- *)
(* Printer. *)
(* ------------------------------------------------------------------------- *)
let print_propvar prec p = print_string(pname p);;
let print_prop_formula = print_qformula print_propvar;;
#install_printer print_prop_formula;;
(* ------------------------------------------------------------------------- *)
(* Testing the parser and printer. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
let fm = <<p ==> q <=> r /\ s \/ (t <=> ~ ~u /\ v)>>;;
And(fm,fm);;
And(Or(fm,fm),fm);;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Interpretation of formulas. *)
(* ------------------------------------------------------------------------- *)
let rec eval fm v =
match fm with
False -> false
| True -> true
| Atom(x) -> v(x)
| Not(p) -> not(eval p v)
| And(p,q) -> (eval p v) & (eval q v)
| Or(p,q) -> (eval p v) or (eval q v)
| Imp(p,q) -> not(eval p v) or (eval q v)
| Iff(p,q) -> (eval p v) = (eval q v);;
(* ------------------------------------------------------------------------- *)
(* Example of use. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
eval <<p /\ q ==> q /\ r>>
(function P"p" -> true | P"q" -> false | P"r" -> true);;
eval <<p /\ q ==> q /\ r>>
(function P"p" -> true | P"q" -> true | P"r" -> false);;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Return the set of propositional variables in a formula. *)
(* ------------------------------------------------------------------------- *)
let atoms fm = atom_union (fun a -> [a]) fm;;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
atoms <<p /\ q \/ s ==> ~p \/ (r <=> s)>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Code to print out truth tables. *)
(* ------------------------------------------------------------------------- *)
let rec onallvaluations subfn v ats =
match ats with
[] -> subfn v
| p::ps -> let v' t q = if q = p then t else v(q) in
onallvaluations subfn (v' false) ps &
onallvaluations subfn (v' true) ps;;
let print_truthtable fm =
let ats = atoms fm in
let width = itlist (max ** String.length ** pname) ats 5 + 1 in
let fixw s = s^String.make(width - String.length s) ' ' in
let truthstring p = fixw (if p then "true" else "false") in
let mk_row v =
let lis = map (fun x -> truthstring(v x)) ats
and ans = truthstring(eval fm v) in
print_string(itlist (^) lis ("| "^ans)); print_newline(); true in
let separator = String.make (width * length ats + 9) '-' in
print_string(itlist (fun s t -> fixw(pname s) ^ t) ats "| formula");
print_newline(); print_string separator; print_newline();
let _ = onallvaluations mk_row (fun x -> false) ats in
print_string separator; print_newline();;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
print_truthtable <<p /\ q ==> q /\ r>>;;
let fm = <<p /\ q ==> q /\ r>>;;
print_truthtable fm;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Additional examples illustrating formula classes. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
print_truthtable <<((p ==> q) ==> p) ==> p>>;;
print_truthtable <<p /\ ~p>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Recognizing tautologies. *)
(* ------------------------------------------------------------------------- *)
let tautology fm =
onallvaluations (eval fm) (fun s -> false) (atoms fm);;
(* ------------------------------------------------------------------------- *)
(* Examples. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
tautology <<p \/ ~p>>;;
tautology <<p \/ q ==> p>>;;
tautology <<p \/ q ==> q \/ (p <=> q)>>;;
tautology <<(p \/ q) /\ ~(p /\ q) ==> (~p <=> q)>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Related concepts. *)
(* ------------------------------------------------------------------------- *)
let unsatisfiable fm = tautology(Not fm);;
let satisfiable fm = not(unsatisfiable fm);;
(* ------------------------------------------------------------------------- *)
(* Substitution operation. *)
(* ------------------------------------------------------------------------- *)
let psubst subfn = onatoms (fun p -> tryapplyd subfn p (Atom p));;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
psubst (P"p" |=> <<p /\ q>>) <<p /\ q /\ p /\ q>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Surprising tautologies including Dijkstra's "Golden rule". *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
tautology <<(p ==> q) \/ (q ==> p)>>;;
tautology <<p \/ (q <=> r) <=> (p \/ q <=> p \/ r)>>;;
tautology <<p /\ q <=> ((p <=> q) <=> p \/ q)>>;;
tautology <<(p ==> q) <=> (~q ==> ~p)>>;;
tautology <<(p ==> ~q) <=> (q ==> ~p)>>;;
tautology <<(p ==> q) <=> (q ==> p)>>;;
(* ------------------------------------------------------------------------- *)
(* Some logical equivalences allowing elimination of connectives. *)
(* ------------------------------------------------------------------------- *)
forall tautology
[<<true <=> false ==> false>>;
<<~p <=> p ==> false>>;
<<p /\ q <=> (p ==> q ==> false) ==> false>>;
<<p \/ q <=> (p ==> false) ==> q>>;
<<(p <=> q) <=> ((p ==> q) ==> (q ==> p) ==> false) ==> false>>];;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Dualization. *)
(* ------------------------------------------------------------------------- *)
let rec dual fm =
match fm with
False -> True
| True -> False
| Atom(p) -> fm
| Not(p) -> Not(dual p)
| And(p,q) -> Or(dual p,dual q)
| Or(p,q) -> And(dual p,dual q)
| _ -> failwith "Formula involves connectives ==> or <=>";;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
dual <<p \/ ~p>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Routine simplification. *)
(* ------------------------------------------------------------------------- *)
let psimplify1 fm =
match fm with
Not False -> True
| Not True -> False
| Not(Not p) -> p
| And(p,False) | And(False,p) -> False
| And(p,True) | And(True,p) -> p
| Or(p,False) | Or(False,p) -> p
| Or(p,True) | Or(True,p) -> True
| Imp(False,p) | Imp(p,True) -> True
| Imp(True,p) -> p
| Imp(p,False) -> Not p
| Iff(p,True) | Iff(True,p) -> p
| Iff(p,False) | Iff(False,p) -> Not p
| _ -> fm;;
let rec psimplify fm =
match fm with
| Not p -> psimplify1 (Not(psimplify p))
| And(p,q) -> psimplify1 (And(psimplify p,psimplify q))
| Or(p,q) -> psimplify1 (Or(psimplify p,psimplify q))
| Imp(p,q) -> psimplify1 (Imp(psimplify p,psimplify q))
| Iff(p,q) -> psimplify1 (Iff(psimplify p,psimplify q))
| _ -> fm;;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
psimplify <<(true ==> (x <=> false)) ==> ~(y \/ false /\ z)>>;;
psimplify <<((x ==> y) ==> true) \/ ~false>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Some operations on literals. *)
(* ------------------------------------------------------------------------- *)
let negative = function (Not p) -> true | _ -> false;;
let positive lit = not(negative lit);;
let negate = function (Not p) -> p | p -> Not p;;
(* ------------------------------------------------------------------------- *)
(* Negation normal form. *)
(* ------------------------------------------------------------------------- *)
let rec nnf fm =
match fm with
| And(p,q) -> And(nnf p,nnf q)
| Or(p,q) -> Or(nnf p,nnf q)
| Imp(p,q) -> Or(nnf(Not p),nnf q)
| Iff(p,q) -> Or(And(nnf p,nnf q),And(nnf(Not p),nnf(Not q)))
| Not(Not p) -> nnf p
| Not(And(p,q)) -> Or(nnf(Not p),nnf(Not q))
| Not(Or(p,q)) -> And(nnf(Not p),nnf(Not q))
| Not(Imp(p,q)) -> And(nnf p,nnf(Not q))
| Not(Iff(p,q)) -> Or(And(nnf p,nnf(Not q)),And(nnf(Not p),nnf q))
| _ -> fm;;
(* ------------------------------------------------------------------------- *)
(* Roll in simplification. *)
(* ------------------------------------------------------------------------- *)
let nnf fm = nnf(psimplify fm);;
(* ------------------------------------------------------------------------- *)
(* Example of NNF function in action. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
let fm = <<(p <=> q) <=> ~(r ==> s)>>;;
let fm' = nnf fm;;
tautology(Iff(fm,fm'));;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Simple negation-pushing when we don't care to distinguish occurrences. *)
(* ------------------------------------------------------------------------- *)
let rec nenf fm =
match fm with
Not(Not p) -> nenf p
| Not(And(p,q)) -> Or(nenf(Not p),nenf(Not q))
| Not(Or(p,q)) -> And(nenf(Not p),nenf(Not q))
| Not(Imp(p,q)) -> And(nenf p,nenf(Not q))
| Not(Iff(p,q)) -> Iff(nenf p,nenf(Not q))
| And(p,q) -> And(nenf p,nenf q)
| Or(p,q) -> Or(nenf p,nenf q)
| Imp(p,q) -> Or(nenf(Not p),nenf q)
| Iff(p,q) -> Iff(nenf p,nenf q)
| _ -> fm;;
let nenf fm = nenf(psimplify fm);;
(* ------------------------------------------------------------------------- *)
(* Some tautologies remarked on. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
tautology <<(p ==> p') /\ (q ==> q') ==> (p /\ q ==> p' /\ q')>>;;
tautology <<(p ==> p') /\ (q ==> q') ==> (p \/ q ==> p' \/ q')>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Disjunctive normal form (DNF) via truth tables. *)
(* ------------------------------------------------------------------------- *)
let list_conj l = if l = [] then True else end_itlist mk_and l;;
let list_disj l = if l = [] then False else end_itlist mk_or l;;
let mk_lits pvs v =
list_conj (map (fun p -> if eval p v then p else Not p) pvs);;
let rec allsatvaluations subfn v pvs =
match pvs with
[] -> if subfn v then [v] else []
| p::ps -> let v' t q = if q = p then t else v(q) in
allsatvaluations subfn (v' false) ps @
allsatvaluations subfn (v' true) ps;;
let dnf fm =
let pvs = atoms fm in
let satvals = allsatvaluations (eval fm) (fun s -> false) pvs in
list_disj (map (mk_lits (map (fun p -> Atom p) pvs)) satvals);;
(* ------------------------------------------------------------------------- *)
(* Examples. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
let fm = <<(p \/ q /\ r) /\ (~p \/ ~r)>>;;
dnf fm;;
print_truthtable fm;;
dnf <<p /\ q /\ r /\ s /\ t /\ u \/ u /\ v>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* DNF via distribution. *)
(* ------------------------------------------------------------------------- *)
let rec distrib fm =
match fm with
And(p,(Or(q,r))) -> Or(distrib(And(p,q)),distrib(And(p,r)))
| And(Or(p,q),r) -> Or(distrib(And(p,r)),distrib(And(q,r)))
| _ -> fm;;
let rec rawdnf fm =
match fm with
And(p,q) -> distrib(And(rawdnf p,rawdnf q))
| Or(p,q) -> Or(rawdnf p,rawdnf q)
| _ -> fm;;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
rawdnf <<(p \/ q /\ r) /\ (~p \/ ~r)>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* A version using a list representation. *)
(* ------------------------------------------------------------------------- *)
let distrib s1 s2 = setify(allpairs union s1 s2);;
let rec purednf fm =
match fm with
And(p,q) -> distrib (purednf p) (purednf q)
| Or(p,q) -> union (purednf p) (purednf q)
| _ -> [[fm]];;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
purednf <<(p \/ q /\ r) /\ (~p \/ ~r)>>;;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Filtering out trivial disjuncts (in this guise, contradictory). *)
(* ------------------------------------------------------------------------- *)
let trivial lits =
let pos,neg = partition positive lits in
intersect pos (image negate neg) <> [];;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
filter (non trivial) (purednf fm);;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* With subsumption checking, done very naively (quadratic). *)
(* ------------------------------------------------------------------------- *)
let simpdnf fm =
if fm = False then [] else if fm = True then [[]] else
let djs = filter (non trivial) (purednf(nnf fm)) in
filter (fun d -> not(exists (fun d' -> psubset d' d) djs)) djs;;
(* ------------------------------------------------------------------------- *)
(* Mapping back to a formula. *)
(* ------------------------------------------------------------------------- *)
let dnf fm = list_disj(map list_conj (simpdnf fm));;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
let fm = <<(p \/ q /\ r) /\ (~p \/ ~r)>>;;
dnf fm;;
tautology(Iff(fm,dnf fm));;
END_INTERACTIVE;;
(* ------------------------------------------------------------------------- *)
(* Conjunctive normal form (CNF) by essentially the same code. *)
(* ------------------------------------------------------------------------- *)
let purecnf fm = image (image negate) (purednf(nnf(Not fm)));;
let simpcnf fm =
if fm = False then [[]] else if fm = True then [] else
let cjs = filter (non trivial) (purecnf fm) in
filter (fun c -> not(exists (fun c' -> psubset c' c) cjs)) cjs;;
let cnf fm = list_conj(map list_disj (simpcnf fm));;
(* ------------------------------------------------------------------------- *)
(* Example. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
let fm = <<(p \/ q /\ r) /\ (~p \/ ~r)>>;;
cnf fm;;
tautology(Iff(fm,cnf fm));;
END_INTERACTIVE;;