1 (* Title: FOLP/IFOLP.thy
2 Author: Martin D Coen, Cambridge University Computer Laboratory
3 Copyright 1992 University of Cambridge
6 header {* Intuitionistic First-Order Logic with Proofs *}
12 ML_file "~~/src/Tools/misc_legacy.ML"
14 setup Pure_Thy.old_appl_syntax_setup
24 Proof :: "[o,p]=>prop"
25 EqProof :: "[p,p,o]=>prop" ("(3_ /= _ :/ _)" [10,10,10] 5)
27 (*** Logical Connectives -- Type Formers ***)
28 eq :: "['a,'a] => o" (infixl "=" 50)
31 Not :: "o => o" ("~ _" [40] 40)
32 conj :: "[o,o] => o" (infixr "&" 35)
33 disj :: "[o,o] => o" (infixr "|" 30)
34 imp :: "[o,o] => o" (infixr "-->" 25)
35 iff :: "[o,o] => o" (infixr "<->" 25)
37 All :: "('a => o) => o" (binder "ALL " 10)
38 Ex :: "('a => o) => o" (binder "EX " 10)
39 Ex1 :: "('a => o) => o" (binder "EX! " 10)
44 (*** Proof Term Formers: precedence must exceed 50 ***)
49 pair :: "[p,p]=>p" ("(1<_,/_>)")
50 split :: "[p, [p,p]=>p] =>p"
53 when :: "[p, p=>p, p=>p]=>p"
54 lambda :: "(p => p) => p" (binder "lam " 55)
55 App :: "[p,p]=>p" (infixl "`" 60)
56 alll :: "['a=>p]=>p" (binder "all " 55)
57 app :: "[p,'a]=>p" (infixl "^" 55)
58 exists :: "['a,p]=>p" ("(1[_,/_])")
59 xsplit :: "[p,['a,p]=>p]=>p"
61 idpeel :: "[p,'a=>p]=>p"
65 syntax "_Proof" :: "[p,o]=>prop" ("(_ /: _)" [51, 10] 5)
68 let fun proof_tr [p, P] = Const (@{const_syntax Proof}, dummyT) $ P $ p
69 in [(@{syntax_const "_Proof"}, proof_tr)] end
72 (*show_proofs = true displays the proof terms -- they are ENORMOUS*)
73 ML {* val show_proofs = Attrib.setup_config_bool @{binding show_proofs} (K false) *}
75 print_translation (advanced) {*
77 fun proof_tr' ctxt [P, p] =
78 if Config.get ctxt show_proofs then Const (@{syntax_const "_Proof"}, dummyT) $ p $ P
80 in [(@{const_syntax Proof}, proof_tr')] end
84 (**** Propositional logic ****)
87 (* Like Intensional Equality in MLTT - but proofs distinct from terms *)
90 ieqI: "ideq(a) : a=a" and
91 ieqE: "[| p : a=b; !!x. f(x) : P(x,x) |] ==> idpeel(p,f) : P(a,b)"
93 (* Truth and Falsity *)
96 TrueI: "tt : True" and
97 FalseE: "a:False ==> contr(a):P"
102 conjI: "[| a:P; b:Q |] ==> <a,b> : P&Q" and
103 conjunct1: "p:P&Q ==> fst(p):P" and
104 conjunct2: "p:P&Q ==> snd(p):Q"
109 disjI1: "a:P ==> inl(a):P|Q" and
110 disjI2: "b:Q ==> inr(b):P|Q" and
111 disjE: "[| a:P|Q; !!x. x:P ==> f(x):R; !!x. x:Q ==> g(x):R
112 |] ==> when(a,f,g):R"
117 impI: "\<And>P Q f. (!!x. x:P ==> f(x):Q) ==> lam x. f(x):P-->Q" and
118 mp: "\<And>P Q f. [| f:P-->Q; a:P |] ==> f`a:Q"
123 allI: "\<And>P. (!!x. f(x) : P(x)) ==> all x. f(x) : ALL x. P(x)" and
124 spec: "\<And>P f. (f:ALL x. P(x)) ==> f^x : P(x)"
127 exI: "p : P(x) ==> [x,p] : EX x. P(x)" and
128 exE: "[| p: EX x. P(x); !!x u. u:P(x) ==> f(x,u) : R |] ==> xsplit(p,f):R"
130 (**** Equality between proofs ****)
133 prefl: "a : P ==> a = a : P" and
134 psym: "a = b : P ==> b = a : P" and
135 ptrans: "[| a = b : P; b = c : P |] ==> a = c : P"
138 idpeelB: "[| !!x. f(x) : P(x,x) |] ==> idpeel(ideq(a),f) = f(a) : P(a,a)"
141 fstB: "a:P ==> fst(<a,b>) = a : P" and
142 sndB: "b:Q ==> snd(<a,b>) = b : Q" and
143 pairEC: "p:P&Q ==> p = <fst(p),snd(p)> : P&Q"
146 whenBinl: "[| a:P; !!x. x:P ==> f(x) : Q |] ==> when(inl(a),f,g) = f(a) : Q" and
147 whenBinr: "[| b:P; !!x. x:P ==> g(x) : Q |] ==> when(inr(b),f,g) = g(b) : Q" and
148 plusEC: "a:P|Q ==> when(a,%x. inl(x),%y. inr(y)) = a : P|Q"
151 applyB: "[| a:P; !!x. x:P ==> b(x) : Q |] ==> (lam x. b(x)) ` a = b(a) : Q" and
152 funEC: "f:P ==> f = lam x. f`x : P"
155 specB: "[| !!x. f(x) : P(x) |] ==> (all x. f(x)) ^ a = f(a) : P(a)"
158 (**** Definitions ****)
161 not_def: "~P == P-->False"
162 iff_def: "P<->Q == (P-->Q) & (Q-->P)"
165 ex1_def: "EX! x. P(x) == EX x. P(x) & (ALL y. P(y) --> y=x)"
167 (*Rewriting -- special constants to flag normalized terms and formulae*)
169 norm_eq: "nrm : norm(x) = x" and
170 NORM_iff: "NRM : NORM(P) <-> P"
172 (*** Sequent-style elimination rules for & --> and ALL ***)
174 schematic_lemma conjE:
176 and "!!x y.[| x:P; y:Q |] ==> f(x,y):R"
178 apply (rule assms(2))
179 apply (rule conjunct1 [OF assms(1)])
180 apply (rule conjunct2 [OF assms(1)])
183 schematic_lemma impE:
186 and "!!x. x:Q ==> r(x):R"
188 apply (rule assms mp)+
191 schematic_lemma allE:
192 assumes "p:ALL x. P(x)"
193 and "!!y. y:P(x) ==> q(y):R"
195 apply (rule assms spec)+
198 (*Duplicates the quantifier; for use with eresolve_tac*)
199 schematic_lemma all_dupE:
200 assumes "p:ALL x. P(x)"
201 and "!!y z.[| y:P(x); z:ALL x. P(x) |] ==> q(y,z):R"
203 apply (rule assms spec)+
207 (*** Negation rules, which translate between ~P and P-->False ***)
209 schematic_lemma notI:
210 assumes "!!x. x:P ==> q(x):False"
213 apply (assumption | rule assms impI)+
216 schematic_lemma notE: "p:~P \<Longrightarrow> q:P \<Longrightarrow> ?p:R"
222 (*This is useful with the special implication rules for each kind of P. *)
223 schematic_lemma not_to_imp:
225 and "!!x. x:(P-->False) ==> q(x):Q"
227 apply (assumption | rule assms impI notE)+
230 (* For substitution int an assumption P, reduce Q to P-->Q, substitute into
231 this implication, then apply impI to move P back into the assumptions.*)
232 schematic_lemma rev_mp: "[| p:P; q:P --> Q |] ==> ?p:Q"
233 apply (assumption | rule mp)+
237 (*Contrapositive of an inference rule*)
238 schematic_lemma contrapos:
239 assumes major: "p:~Q"
240 and minor: "!!y. y:P==>q(y):Q"
242 apply (rule major [THEN notE, THEN notI])
246 (** Unique assumption tactic.
247 Ignores proof objects.
248 Fails unless one assumption is equal and exactly one is unifiable
253 fun discard_proof (Const (@{const_name Proof}, _) $ P $ _) = P;
255 val uniq_assume_tac =
258 let val hyps = map discard_proof (Logic.strip_assums_hyp prem)
259 and concl = discard_proof (Logic.strip_assums_concl prem)
261 if exists (fn hyp => hyp aconv concl) hyps
262 then case distinct (op =) (filter (fn hyp => Term.could_unify (hyp, concl)) hyps) of
271 (*** Modus Ponens Tactics ***)
273 (*Finds P-->Q and P in the assumptions, replaces implication by Q *)
275 fun mp_tac i = eresolve_tac [@{thm notE}, make_elim @{thm mp}] i THEN assume_tac i
278 (*Like mp_tac but instantiates no variables*)
280 fun int_uniq_mp_tac i = eresolve_tac [@{thm notE}, @{thm impE}] i THEN uniq_assume_tac i
284 (*** If-and-only-if ***)
286 schematic_lemma iffI:
287 assumes "!!x. x:P ==> q(x):Q"
288 and "!!x. x:Q ==> r(x):P"
291 apply (assumption | rule assms conjI impI)+
295 (*Observe use of rewrite_rule to unfold "<->" in meta-assumptions (prems) *)
297 schematic_lemma iffE:
299 and "!!x y.[| x:P-->Q; y:Q-->P |] ==> q(x,y):R"
302 apply (rule assms(1) [unfolded iff_def])
303 apply (rule assms(2))
307 (* Destruct rules for <-> similar to Modus Ponens *)
309 schematic_lemma iffD1: "[| p:P <-> Q; q:P |] ==> ?p:Q"
311 apply (rule conjunct1 [THEN mp], assumption+)
314 schematic_lemma iffD2: "[| p:P <-> Q; q:Q |] ==> ?p:P"
316 apply (rule conjunct2 [THEN mp], assumption+)
319 schematic_lemma iff_refl: "?p:P <-> P"
324 schematic_lemma iff_sym: "p:Q <-> P ==> ?p:P <-> Q"
327 apply (erule (1) mp)+
330 schematic_lemma iff_trans: "[| p:P <-> Q; q:Q<-> R |] ==> ?p:P <-> R"
332 apply (assumption | erule iffE | erule (1) impE)+
335 (*** Unique existence. NOTE THAT the following 2 quantifications
336 EX!x such that [EX!y such that P(x,y)] (sequential)
337 EX!x,y such that P(x,y) (simultaneous)
338 do NOT mean the same thing. The parser treats EX!x y.P(x,y) as sequential.
341 schematic_lemma ex1I:
343 and "!!x u. u:P(x) ==> f(u) : x=a"
344 shows "?p:EX! x. P(x)"
346 apply (assumption | rule assms exI conjI allI impI)+
349 schematic_lemma ex1E:
350 assumes "p:EX! x. P(x)"
351 and "!!x u v. [| u:P(x); v:ALL y. P(y) --> y=x |] ==> f(x,u,v):R"
353 apply (insert assms(1) [unfolded ex1_def])
354 apply (erule exE conjE | assumption | rule assms(1))+
355 apply (erule assms(2), assumption)
359 (*** <-> congruence rules for simplification ***)
361 (*Use iffE on a premise. For conj_cong, imp_cong, all_cong, ex_cong*)
363 fun iff_tac prems i =
364 resolve_tac (prems RL [@{thm iffE}]) i THEN
365 REPEAT1 (eresolve_tac [asm_rl, @{thm mp}] i)
368 schematic_lemma conj_cong:
370 and "!!x. x:P' ==> q(x):Q <-> Q'"
371 shows "?p:(P&Q) <-> (P'&Q')"
372 apply (insert assms(1))
373 apply (assumption | rule iffI conjI |
374 erule iffE conjE mp | tactic {* iff_tac @{thms assms} 1 *})+
377 schematic_lemma disj_cong:
378 "[| p:P <-> P'; q:Q <-> Q' |] ==> ?p:(P|Q) <-> (P'|Q')"
379 apply (erule iffE disjE disjI1 disjI2 | assumption | rule iffI | tactic {* mp_tac 1 *})+
382 schematic_lemma imp_cong:
384 and "!!x. x:P' ==> q(x):Q <-> Q'"
385 shows "?p:(P-->Q) <-> (P'-->Q')"
386 apply (insert assms(1))
387 apply (assumption | rule iffI impI | erule iffE | tactic {* mp_tac 1 *} |
388 tactic {* iff_tac @{thms assms} 1 *})+
391 schematic_lemma iff_cong:
392 "[| p:P <-> P'; q:Q <-> Q' |] ==> ?p:(P<->Q) <-> (P'<->Q')"
393 apply (erule iffE | assumption | rule iffI | tactic {* mp_tac 1 *})+
396 schematic_lemma not_cong:
397 "p:P <-> P' ==> ?p:~P <-> ~P'"
398 apply (assumption | rule iffI notI | tactic {* mp_tac 1 *} | erule iffE notE)+
401 schematic_lemma all_cong:
402 assumes "!!x. f(x):P(x) <-> Q(x)"
403 shows "?p:(ALL x. P(x)) <-> (ALL x. Q(x))"
404 apply (assumption | rule iffI allI | tactic {* mp_tac 1 *} | erule allE |
405 tactic {* iff_tac @{thms assms} 1 *})+
408 schematic_lemma ex_cong:
409 assumes "!!x. f(x):P(x) <-> Q(x)"
410 shows "?p:(EX x. P(x)) <-> (EX x. Q(x))"
411 apply (erule exE | assumption | rule iffI exI | tactic {* mp_tac 1 *} |
412 tactic {* iff_tac @{thms assms} 1 *})+
416 bind_thm ("ex1_cong", prove_goal (the_context ())
417 "(!!x.f(x):P(x) <-> Q(x)) ==> ?p:(EX! x.P(x)) <-> (EX! x.Q(x))"
419 [ (REPEAT (eresolve_tac [ex1E, spec RS mp] 1 ORELSE ares_tac [iffI,ex1I] 1
421 ORELSE iff_tac prems 1)) ]))
424 (*** Equality rules ***)
428 schematic_lemma subst:
429 assumes prem1: "p:a=b"
432 apply (rule prem2 [THEN rev_mp])
433 apply (rule prem1 [THEN ieqE])
438 schematic_lemma sym: "q:a=b ==> ?c:b=a"
443 schematic_lemma trans: "[| p:a=b; q:b=c |] ==> ?d:a=c"
444 apply (erule (1) subst)
447 (** ~ b=a ==> ~ a=b **)
448 schematic_lemma not_sym: "p:~ b=a ==> ?q:~ a=b"
449 apply (erule contrapos)
453 schematic_lemma ssubst: "p:b=a \<Longrightarrow> q:P(a) \<Longrightarrow> ?p:P(b)"
459 (*A special case of ex1E that would otherwise need quantifier expansion*)
460 schematic_lemma ex1_equalsE: "[| p:EX! x. P(x); q:P(a); r:P(b) |] ==> ?d:a=b"
463 apply (rule_tac [2] sym)
464 apply (assumption | erule spec [THEN mp])+
467 (** Polymorphic congruence rules **)
469 schematic_lemma subst_context: "[| p:a=b |] ==> ?d:t(a)=t(b)"
474 schematic_lemma subst_context2: "[| p:a=b; q:c=d |] ==> ?p:t(a,c)=t(b,d)"
475 apply (erule ssubst)+
479 schematic_lemma subst_context3: "[| p:a=b; q:c=d; r:e=f |] ==> ?p:t(a,c,e)=t(b,d,f)"
480 apply (erule ssubst)+
484 (*Useful with eresolve_tac for proving equalties from known equalities.
488 schematic_lemma box_equals: "[| p:a=b; q:a=c; r:b=d |] ==> ?p:c=d"
495 (*Dual of box_equals: for proving equalities backwards*)
496 schematic_lemma simp_equals: "[| p:a=c; q:b=d; r:c=d |] ==> ?p:a=b"
499 apply (assumption | rule sym)+
502 (** Congruence rules for predicate letters **)
504 schematic_lemma pred1_cong: "p:a=a' ==> ?p:P(a) <-> P(a')"
506 apply (tactic {* DEPTH_SOLVE (atac 1 ORELSE eresolve_tac [@{thm subst}, @{thm ssubst}] 1) *})
509 schematic_lemma pred2_cong: "[| p:a=a'; q:b=b' |] ==> ?p:P(a,b) <-> P(a',b')"
511 apply (tactic {* DEPTH_SOLVE (atac 1 ORELSE eresolve_tac [@{thm subst}, @{thm ssubst}] 1) *})
514 schematic_lemma pred3_cong: "[| p:a=a'; q:b=b'; r:c=c' |] ==> ?p:P(a,b,c) <-> P(a',b',c')"
516 apply (tactic {* DEPTH_SOLVE (atac 1 ORELSE eresolve_tac [@{thm subst}, @{thm ssubst}] 1) *})
519 lemmas pred_congs = pred1_cong pred2_cong pred3_cong
521 (*special case for the equality predicate!*)
522 lemmas eq_cong = pred2_cong [where P = "op ="]
525 (*** Simplifications of assumed implications.
526 Roy Dyckhoff has proved that conj_impE, disj_impE, and imp_impE
527 used with mp_tac (restricted to atomic formulae) is COMPLETE for
528 intuitionistic propositional logic. See
529 R. Dyckhoff, Contraction-free sequent calculi for intuitionistic logic
530 (preprint, University of St Andrews, 1991) ***)
532 schematic_lemma conj_impE:
533 assumes major: "p:(P&Q)-->S"
534 and minor: "!!x. x:P-->(Q-->S) ==> q(x):R"
536 apply (assumption | rule conjI impI major [THEN mp] minor)+
539 schematic_lemma disj_impE:
540 assumes major: "p:(P|Q)-->S"
541 and minor: "!!x y.[| x:P-->S; y:Q-->S |] ==> q(x,y):R"
543 apply (tactic {* DEPTH_SOLVE (atac 1 ORELSE
544 resolve_tac [@{thm disjI1}, @{thm disjI2}, @{thm impI},
545 @{thm major} RS @{thm mp}, @{thm minor}] 1) *})
548 (*Simplifies the implication. Classical version is stronger.
549 Still UNSAFE since Q must be provable -- backtracking needed. *)
550 schematic_lemma imp_impE:
551 assumes major: "p:(P-->Q)-->S"
552 and r1: "!!x y.[| x:P; y:Q-->S |] ==> q(x,y):Q"
553 and r2: "!!x. x:S ==> r(x):R"
555 apply (assumption | rule impI major [THEN mp] r1 r2)+
558 (*Simplifies the implication. Classical version is stronger.
559 Still UNSAFE since ~P must be provable -- backtracking needed. *)
560 schematic_lemma not_impE:
561 assumes major: "p:~P --> S"
562 and r1: "!!y. y:P ==> q(y):False"
563 and r2: "!!y. y:S ==> r(y):R"
565 apply (assumption | rule notI impI major [THEN mp] r1 r2)+
568 (*Simplifies the implication. UNSAFE. *)
569 schematic_lemma iff_impE:
570 assumes major: "p:(P<->Q)-->S"
571 and r1: "!!x y.[| x:P; y:Q-->S |] ==> q(x,y):Q"
572 and r2: "!!x y.[| x:Q; y:P-->S |] ==> r(x,y):P"
573 and r3: "!!x. x:S ==> s(x):R"
575 apply (assumption | rule iffI impI major [THEN mp] r1 r2 r3)+
578 (*What if (ALL x.~~P(x)) --> ~~(ALL x.P(x)) is an assumption? UNSAFE*)
579 schematic_lemma all_impE:
580 assumes major: "p:(ALL x. P(x))-->S"
581 and r1: "!!x. q:P(x)"
582 and r2: "!!y. y:S ==> r(y):R"
584 apply (assumption | rule allI impI major [THEN mp] r1 r2)+
587 (*Unsafe: (EX x.P(x))-->S is equivalent to ALL x.P(x)-->S. *)
588 schematic_lemma ex_impE:
589 assumes major: "p:(EX x. P(x))-->S"
590 and r: "!!y. y:P(a)-->S ==> q(y):R"
592 apply (assumption | rule exI impI major [THEN mp] r)+
596 schematic_lemma rev_cut_eq:
598 and "!!x. x:a=b ==> f(x):R"
603 lemma thin_refl: "!!X. [|p:x=x; PROP W|] ==> PROP W" .
605 ML_file "hypsubst.ML"
608 structure Hypsubst = Hypsubst
610 (*Take apart an equality judgement; otherwise raise Match!*)
611 fun dest_eq (Const (@{const_name Proof}, _) $
612 (Const (@{const_name eq}, _) $ t $ u) $ _) = (t, u);
614 val imp_intr = @{thm impI}
616 (*etac rev_cut_eq moves an equality to be the last premise. *)
617 val rev_cut_eq = @{thm rev_cut_eq}
619 val rev_mp = @{thm rev_mp}
620 val subst = @{thm subst}
622 val thin_refl = @{thm thin_refl}
627 ML_file "intprover.ML"
630 (*** Rewrite rules ***)
632 schematic_lemma conj_rews:
633 "?p1 : P & True <-> P"
634 "?p2 : True & P <-> P"
635 "?p3 : P & False <-> False"
636 "?p4 : False & P <-> False"
638 "?p6 : P & ~P <-> False"
639 "?p7 : ~P & P <-> False"
640 "?p8 : (P & Q) & R <-> P & (Q & R)"
641 apply (tactic {* fn st => IntPr.fast_tac 1 st *})+
644 schematic_lemma disj_rews:
645 "?p1 : P | True <-> True"
646 "?p2 : True | P <-> True"
647 "?p3 : P | False <-> P"
648 "?p4 : False | P <-> P"
650 "?p6 : (P | Q) | R <-> P | (Q | R)"
651 apply (tactic {* IntPr.fast_tac 1 *})+
654 schematic_lemma not_rews:
655 "?p1 : ~ False <-> True"
656 "?p2 : ~ True <-> False"
657 apply (tactic {* IntPr.fast_tac 1 *})+
660 schematic_lemma imp_rews:
661 "?p1 : (P --> False) <-> ~P"
662 "?p2 : (P --> True) <-> True"
663 "?p3 : (False --> P) <-> True"
664 "?p4 : (True --> P) <-> P"
665 "?p5 : (P --> P) <-> True"
666 "?p6 : (P --> ~P) <-> ~P"
667 apply (tactic {* IntPr.fast_tac 1 *})+
670 schematic_lemma iff_rews:
671 "?p1 : (True <-> P) <-> P"
672 "?p2 : (P <-> True) <-> P"
673 "?p3 : (P <-> P) <-> True"
674 "?p4 : (False <-> P) <-> ~P"
675 "?p5 : (P <-> False) <-> ~P"
676 apply (tactic {* IntPr.fast_tac 1 *})+
679 schematic_lemma quant_rews:
680 "?p1 : (ALL x. P) <-> P"
681 "?p2 : (EX x. P) <-> P"
682 apply (tactic {* IntPr.fast_tac 1 *})+
685 (*These are NOT supplied by default!*)
686 schematic_lemma distrib_rews1:
687 "?p1 : ~(P|Q) <-> ~P & ~Q"
688 "?p2 : P & (Q | R) <-> P&Q | P&R"
689 "?p3 : (Q | R) & P <-> Q&P | R&P"
690 "?p4 : (P | Q --> R) <-> (P --> R) & (Q --> R)"
691 apply (tactic {* IntPr.fast_tac 1 *})+
694 schematic_lemma distrib_rews2:
695 "?p1 : ~(EX x. NORM(P(x))) <-> (ALL x. ~NORM(P(x)))"
696 "?p2 : ((EX x. NORM(P(x))) --> Q) <-> (ALL x. NORM(P(x)) --> Q)"
697 "?p3 : (EX x. NORM(P(x))) & NORM(Q) <-> (EX x. NORM(P(x)) & NORM(Q))"
698 "?p4 : NORM(Q) & (EX x. NORM(P(x))) <-> (EX x. NORM(Q) & NORM(P(x)))"
699 apply (tactic {* IntPr.fast_tac 1 *})+
702 lemmas distrib_rews = distrib_rews1 distrib_rews2
704 schematic_lemma P_Imp_P_iff_T: "p:P ==> ?p:(P <-> True)"
705 apply (tactic {* IntPr.fast_tac 1 *})
708 schematic_lemma not_P_imp_P_iff_F: "p:~P ==> ?p:(P <-> False)"
709 apply (tactic {* IntPr.fast_tac 1 *})