-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcfprop.ml
435 lines (348 loc) · 20.6 KB
/
lcfprop.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
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
(* ========================================================================= *)
(* Propositional reasoning by derived rules atop the LCF core. *)
(* *)
(* Copyright (c) 2003-2007, John Harrison. (See "LICENSE.txt" for details.) *)
(* ========================================================================= *)
(* ------------------------------------------------------------------------- *)
(* |- p ==> p *)
(* ------------------------------------------------------------------------- *)
let imp_refl p =
modusponens (modusponens (axiom_distribimp p (Imp(p,p)) p)
(axiom_addimp p (Imp(p,p))))
(axiom_addimp p p);;
(* ------------------------------------------------------------------------- *)
(* |- p ==> p ==> q *)
(* -------------------- imp_unduplicate *)
(* |- p ==> q *)
(* ------------------------------------------------------------------------- *)
let imp_unduplicate th =
let p,pq = dest_imp(concl th) in
let q = consequent pq in
modusponens (modusponens (axiom_distribimp p p q) th) (imp_refl p);;
(* ------------------------------------------------------------------------- *)
(* Some handy syntax operations. *)
(* ------------------------------------------------------------------------- *)
let negatef fm =
match fm with
Imp(p,False) -> p
| p -> Imp(p,False);;
let negativef fm = match fm with Imp(p,False) -> true | _ -> false;;
(* ------------------------------------------------------------------------- *)
(* |- q *)
(* ------------------------------------------------ add_assum (p) *)
(* |- p ==> q *)
(* ------------------------------------------------------------------------- *)
let add_assum p th = modusponens (axiom_addimp (concl th) p) th;;
(* ------------------------------------------------------------------------- *)
(* |- q ==> r *)
(* --------------------------------------- imp_add_assum p *)
(* |- (p ==> q) ==> (p ==> r) *)
(* ------------------------------------------------------------------------- *)
let imp_add_assum p th =
let (q,r) = dest_imp(concl th) in
modusponens (axiom_distribimp p q r) (add_assum p th);;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q |- q ==> r *)
(* ----------------------------------------- imp_trans *)
(* |- p ==> r *)
(* ------------------------------------------------------------------------- *)
let imp_trans th1 th2 =
let p = antecedent(concl th1) in
modusponens (imp_add_assum p th2) th1;;
(* ------------------------------------------------------------------------- *)
(* |- p ==> r *)
(* -------------------------- imp_insert q *)
(* |- p ==> q ==> r *)
(* ------------------------------------------------------------------------- *)
let imp_insert q th =
let (p,r) = dest_imp(concl th) in
imp_trans th (axiom_addimp r q);;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q ==> r *)
(* ---------------------- imp_swap *)
(* |- q ==> p ==> r *)
(* ------------------------------------------------------------------------- *)
let imp_swap th =
let p,qr = dest_imp(concl th) in
let q,r = dest_imp qr in
imp_trans (axiom_addimp q p)
(modusponens (axiom_distribimp p q r) th);;
(* ------------------------------------------------------------------------- *)
(* |- (q ==> r) ==> (p ==> q) ==> (p ==> r) *)
(* ------------------------------------------------------------------------- *)
let imp_trans_th p q r =
imp_trans (axiom_addimp (Imp(q,r)) p)
(axiom_distribimp p q r);;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q *)
(* ------------------------------- imp_add_concl r *)
(* |- (q ==> r) ==> (p ==> r) *)
(* ------------------------------------------------------------------------- *)
let imp_add_concl r th =
let (p,q) = dest_imp(concl th) in
modusponens (imp_swap(imp_trans_th p q r)) th;;
(* ------------------------------------------------------------------------- *)
(* |- (p ==> q ==> r) ==> (q ==> p ==> r) *)
(* ------------------------------------------------------------------------- *)
let imp_swap_th p q r =
imp_trans (axiom_distribimp p q r)
(imp_add_concl (Imp(p,r)) (axiom_addimp q p));;
(* ------------------------------------------------------------------------- *)
(* |- (p ==> q ==> r) ==> (s ==> t ==> u) *)
(* ----------------------------------------- *)
(* |- (q ==> p ==> r) ==> (t ==> s ==> u) *)
(* ------------------------------------------------------------------------- *)
let imp_swap2 th =
match concl th with
Imp(Imp(p,Imp(q,r)),Imp(s,Imp(t,u))) ->
imp_trans (imp_swap_th q p r) (imp_trans th (imp_swap_th s t u))
| _ -> failwith "imp_swap2";;
(* ------------------------------------------------------------------------- *)
(* If |- p ==> q ==> r and |- p ==> q then |- p ==> r. *)
(* ------------------------------------------------------------------------- *)
let right_mp ith th =
imp_unduplicate(imp_trans th (imp_swap ith));;
(* ------------------------------------------------------------------------- *)
(* |- p <=> q *)
(* ------------ iff_imp1 *)
(* |- p ==> q *)
(* ------------------------------------------------------------------------- *)
let iff_imp1 th =
let (p,q) = dest_iff(concl th) in
modusponens (axiom_iffimp1 p q) th;;
(* ------------------------------------------------------------------------- *)
(* |- p <=> q *)
(* ------------ iff_imp2 *)
(* |- q ==> p *)
(* ------------------------------------------------------------------------- *)
let iff_imp2 th =
let (p,q) = dest_iff(concl th) in
modusponens (axiom_iffimp2 p q) th;;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q |- q ==> p *)
(* ---------------------------- imp_antisym *)
(* |- p <=> q *)
(* ------------------------------------------------------------------------- *)
let imp_antisym th1 th2 =
let (p,q) = dest_imp(concl th1) in
modusponens (modusponens (axiom_impiff p q) th1) th2;;
(* ------------------------------------------------------------------------- *)
(* |- p ==> (q ==> false) ==> false *)
(* ----------------------------------- right_doubleneg *)
(* |- p ==> q *)
(* ------------------------------------------------------------------------- *)
let right_doubleneg th =
match concl th with
Imp(_,Imp(Imp(p,False),False)) -> imp_trans th (axiom_doubleneg p)
| _ -> failwith "right_doubleneg";;
(* ------------------------------------------------------------------------- *)
(* *)
(* ------------------------------------------- ex_falso (p) *)
(* |- false ==> p *)
(* ------------------------------------------------------------------------- *)
let ex_falso p = right_doubleneg(axiom_addimp False (Imp(p,False)));;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q ==> r |- r ==> s *)
(* ------------------------------------ imp_trans2 *)
(* |- p ==> q ==> s *)
(* ------------------------------------------------------------------------- *)
let imp_trans2 th1 th2 =
let Imp(p,Imp(q,r)) = concl th1 and Imp(r',s) = concl th2 in
let th = imp_add_assum p (modusponens (imp_trans_th q r s) th2) in
modusponens th th1;;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q1 ... |- p ==> qn |- q1 ==> ... ==> qn ==> r *)
(* -------------------------------------------------------------- *)
(* |- p ==> r *)
(* ------------------------------------------------------------------------- *)
let imp_trans_chain
ths th =
itlist (fun a b -> imp_unduplicate (imp_trans a (imp_swap b)))
(rev(tl ths)) (imp_trans (hd ths) th);;
(* ------------------------------------------------------------------------- *)
(* |- (q ==> false) ==> p ==> (p ==> q) ==> false *)
(* ------------------------------------------------------------------------- *)
let imp_truefalse p q =
imp_trans (imp_trans_th p q False) (imp_swap_th (Imp(p,q)) p False);;
(* ------------------------------------------------------------------------- *)
(* |- (p' ==> p) ==> (q ==> q') ==> (p ==> q) ==> (p' ==> q') *)
(* ------------------------------------------------------------------------- *)
let imp_mono_th p p' q q' =
let th1 = imp_trans_th (Imp(p,q)) (Imp(p',q)) (Imp(p',q'))
and th2 = imp_trans_th p' q q'
and th3 = imp_swap(imp_trans_th p' p q) in
imp_trans th3 (imp_swap(imp_trans th2 th1));;
(* ------------------------------------------------------------------------- *)
(* |- true *)
(* ------------------------------------------------------------------------- *)
let truth = modusponens (iff_imp2 axiom_true) (imp_refl False);;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q *)
(* ----------------- contrapos *)
(* |- ~q ==> ~p *)
(* ------------------------------------------------------------------------- *)
let contrapos th =
let p,q = dest_imp(concl th) in
imp_trans (imp_trans (iff_imp1(axiom_not q)) (imp_add_concl False th))
(iff_imp2(axiom_not p));;
(* ------------------------------------------------------------------------- *)
(* |- p /\ q ==> p *)
(* ------------------------------------------------------------------------- *)
let and_left p q =
let th1 = imp_add_assum p (axiom_addimp False q) in
let th2 = right_doubleneg(imp_add_concl False th1) in
imp_trans (iff_imp1(axiom_and p q)) th2;;
(* ------------------------------------------------------------------------- *)
(* |- p /\ q ==> q *)
(* ------------------------------------------------------------------------- *)
let and_right p q =
let th1 = axiom_addimp (Imp(q,False)) p in
let th2 = right_doubleneg(imp_add_concl False th1) in
imp_trans (iff_imp1(axiom_and p q)) th2;;
(* ------------------------------------------------------------------------- *)
(* |- p1 /\ ... /\ pn ==> pi for each 1 <= i <= n (input term right assoc) *)
(* ------------------------------------------------------------------------- *)
let rec conjths fm =
try let p,q = dest_and fm in
(and_left p q)::map (imp_trans (and_right p q)) (conjths q)
with Failure _ -> [imp_refl fm];;
(* ------------------------------------------------------------------------- *)
(* |- p ==> q ==> p /\ q *)
(* ------------------------------------------------------------------------- *)
let and_pair p q =
let th1 = iff_imp2(axiom_and p q)
and th2 = imp_swap_th (Imp(p,Imp(q,False))) q False in
let th3 = imp_add_assum p (imp_trans2 th2 th1) in
modusponens th3 (imp_swap (imp_refl (Imp(p,Imp(q,False)))));;
(* ------------------------------------------------------------------------- *)
(* If |- p /\ q ==> r then |- p ==> q ==> r *)
(* ------------------------------------------------------------------------- *)
let shunt th =
let p,q = dest_and(antecedent(concl th)) in
modusponens (itlist imp_add_assum [p;q] th) (and_pair p q);;
(* ------------------------------------------------------------------------- *)
(* If |- p ==> q ==> r then |- p /\ q ==> r *)
(* ------------------------------------------------------------------------- *)
let unshunt th =
let p,qr = dest_imp(concl th) in
let q,r = dest_imp qr in
imp_trans_chain [and_left p q; and_right p q] th;;
(* ------------------------------------------------------------------------- *)
(* Produce |- (p <=> q) <=> (p ==> q) /\ (q ==> p) *)
(* ------------------------------------------------------------------------- *)
let iff_def p q =
let th1 = and_pair (Imp(p,q)) (Imp(q,p)) in
let th2 = imp_trans_chain [axiom_iffimp1 p q; axiom_iffimp2 p q] th1 in
imp_antisym th2 (unshunt (axiom_impiff p q));;
let iff_def p q =
let th = and_pair (Imp(p,q)) (Imp(q,p))
and thl = [axiom_iffimp1 p q; axiom_iffimp2 p q] in
imp_antisym (imp_trans_chain thl th) (unshunt (axiom_impiff p q));;
(* ------------------------------------------------------------------------- *)
(* Produce "expansion" theorem for defined connectives. *)
(* ------------------------------------------------------------------------- *)
let expand_connective fm =
match fm with
True -> axiom_true
| Not p -> axiom_not p
| And(p,q) -> axiom_and p q
| Or(p,q) -> axiom_or p q
| Iff(p,q) -> iff_def p q
| Exists(x,p) -> axiom_exists x p
| _ -> failwith "expand_connective";;
let eliminate_connective fm =
if not(negativef fm) then iff_imp1(expand_connective fm)
else imp_add_concl False (iff_imp2(expand_connective(negatef fm)));;
(* ------------------------------------------------------------------------- *)
(* *)
(* ------------------------------------------------- imp_false_conseqs *)
(* [|- ((p ==> q) ==> false) ==> (q ==> false); *)
(* |- ((p ==> q) ==> false) ==> p] *)
(* ------------------------------------------------------------------------- *)
let imp_false_conseqs p q =
[right_doubleneg(imp_add_concl False (imp_add_assum p (ex_falso q)));
imp_add_concl False (imp_insert p (imp_refl q))];;
(* ------------------------------------------------------------------------- *)
(* |- p ==> (q ==> false) ==> r *)
(* ------------------------------------ imp_false_rule *)
(* |- ((p ==> q) ==> false) ==> r *)
(* ------------------------------------------------------------------------- *)
let imp_false_rule th =
let p,r = dest_imp (concl th) in
imp_trans_chain (imp_false_conseqs p (funpow 2 antecedent r)) th;;
(* ------------------------------------------------------------------------- *)
(* |- (p ==> false) ==> r |- q ==> r *)
(* ---------------------------------------------- imp_true_rule *)
(* |- (p ==> q) ==> r *)
(* ------------------------------------------------------------------------- *)
let imp_true_rule th1 th2 =
let p = funpow 2 antecedent (concl th1) and q = antecedent(concl th2)
and th3 = right_doubleneg(imp_add_concl False th1)
and th4 = imp_add_concl False th2 in
let th5 = imp_swap(imp_truefalse p q) in
let th6 = imp_add_concl False (imp_trans_chain [th3; th4] th5)
and th7 = imp_swap(imp_refl(Imp(Imp(p,q),False))) in
right_doubleneg(imp_trans th7 th6);;
(* ------------------------------------------------------------------------- *)
(* * *)
(* -------------------------------------- imp_contr *)
(* |- p ==> -p ==> q *)
(* ------------------------------------------------------------------------- *)
let imp_contr p q =
if negativef p then imp_add_assum (negatef p) (ex_falso q)
else imp_swap (imp_add_assum p (ex_falso q));;
(* ------------------------------------------------------------------------- *)
(* *)
(* --------------------------------------------- imp_front (this antecedent) *)
(* |- (p0 ==> p1 ==> ... ==> pn ==> q) *)
(* ==> pn ==> p0 ==> p1 ==> .. p(n-1) ==> q *)
(* ------------------------------------------------------------------------- *)
let rec imp_front_th n fm =
if n = 0 then imp_refl fm else
let p,qr = dest_imp fm in
let th1 = imp_add_assum p (imp_front_th (n - 1) qr) in
let q',r' = dest_imp(funpow 2 consequent(concl th1)) in
imp_trans th1 (imp_swap_th p q' r');;
(* ------------------------------------------------------------------------- *)
(* |- p0 ==> p1 ==> ... ==> pn ==> q *)
(* ------------------------------------------ imp_front n *)
(* |- pn ==> p0 ==> p1 ==> .. p(n-1) ==> q *)
(* ------------------------------------------------------------------------- *)
let imp_front n th = modusponens (imp_front_th n (concl th)) th;;
(* ------------------------------------------------------------------------- *)
(* Propositional tableaux procedure. *)
(* ------------------------------------------------------------------------- *)
let rec lcfptab fms lits =
match fms with
False::fl ->
ex_falso (itlist mk_imp (fl @ lits) False)
| (Imp(p,q) as fm)::fl when p = q ->
add_assum fm (lcfptab fl lits)
| Imp(Imp(p,q),False)::fl ->
imp_false_rule(lcfptab (p::Imp(q,False)::fl) lits)
| Imp(p,q)::fl when q <> False ->
imp_true_rule (lcfptab (Imp(p,False)::fl) lits)
(lcfptab (q::fl) lits)
| (Atom(_)|Forall(_,_)|Imp((Atom(_)|Forall(_,_)),False) as p)::fl ->
if mem (negatef p) lits then
let l1,l2 = chop_list (index (negatef p) lits) lits in
let th = imp_contr p (itlist mk_imp (tl l2) False) in
itlist imp_insert (fl @ l1) th
else imp_front (length fl) (lcfptab fl (p::lits))
| fm::fl ->
let th = eliminate_connective fm in
imp_trans th (lcfptab (consequent(concl th)::fl) lits)
| _ -> failwith "lcfptab: no contradiction";;
(* ------------------------------------------------------------------------- *)
(* In particular, this gives a tautology prover. *)
(* ------------------------------------------------------------------------- *)
let lcftaut p =
modusponens (axiom_doubleneg p) (lcfptab [negatef p] []);;
(* ------------------------------------------------------------------------- *)
(* The examples in the text. *)
(* ------------------------------------------------------------------------- *)
START_INTERACTIVE;;
lcftaut <<(p ==> q) \/ (q ==> p)>>;;
lcftaut <<p /\ q <=> ((p <=> q) <=> p \/ q)>>;;
lcftaut <<((p <=> q) <=> r) <=> (p <=> (q <=> r))>>;;
END_INTERACTIVE;;