2 Author: Tobias Nipkow, Markus Wenzel, and Larry Paulson
5 header {* The basis of Higher-Order Logic *}
8 imports Pure "~~/src/Tools/Code_Generator"
11 "~~/src/Tools/IsaPlanner/zipper.ML"
12 "~~/src/Tools/IsaPlanner/isand.ML"
13 "~~/src/Tools/IsaPlanner/rw_tools.ML"
14 "~~/src/Tools/IsaPlanner/rw_inst.ML"
15 "~~/src/Tools/intuitionistic.ML"
16 "~~/src/Tools/project_rule.ML"
17 "~~/src/Tools/cong_tac.ML"
18 "~~/src/Provers/hypsubst.ML"
19 "~~/src/Provers/splitter.ML"
20 "~~/src/Provers/classical.ML"
21 "~~/src/Provers/blast.ML"
22 "~~/src/Provers/clasimp.ML"
23 "~~/src/Tools/coherent.ML"
24 "~~/src/Tools/eqsubst.ML"
25 "~~/src/Provers/quantifier1.ML"
26 "Tools/res_blacklist.ML"
28 "~~/src/Tools/random_word.ML"
29 "~~/src/Tools/atomize_elim.ML"
30 "~~/src/Tools/induct.ML"
31 ("~~/src/Tools/induct_tacs.ML")
32 ("Tools/recfun_codegen.ML")
33 "~~/src/Tools/more_conv.ML"
36 setup {* Intuitionistic.method_setup @{binding iprover} *}
38 setup Res_Blacklist.setup
41 subsection {* Primitive logic *}
43 subsubsection {* Core syntax *}
47 setup {* Object_Logic.add_base_sort @{sort type} *}
50 "fun" :: (type, type) type
58 Trueprop :: "bool => prop" ("(_)" 5)
61 Not :: "bool => bool" ("~ _" [40] 40)
65 The :: "('a => bool) => 'a"
66 All :: "('a => bool) => bool" (binder "ALL " 10)
67 Ex :: "('a => bool) => bool" (binder "EX " 10)
68 Ex1 :: "('a => bool) => bool" (binder "EX! " 10)
69 Let :: "['a, 'a => 'b] => 'b"
71 "op =" :: "['a, 'a] => bool" (infixl "=" 50)
72 "op &" :: "[bool, bool] => bool" (infixr "&" 35)
73 "op |" :: "[bool, bool] => bool" (infixr "|" 30)
74 "op -->" :: "[bool, bool] => bool" (infixr "-->" 25)
79 If :: "[bool, 'a, 'a] => 'a" ("(if (_)/ then (_)/ else (_))" 10)
82 subsubsection {* Additional concrete syntax *}
88 not_equal :: "['a, 'a] => bool" (infixl "~=" 50) where
92 not_equal (infix "~=" 50)
95 Not ("\<not> _" [40] 40) and
96 "op &" (infixr "\<and>" 35) and
97 "op |" (infixr "\<or>" 30) and
98 "op -->" (infixr "\<longrightarrow>" 25) and
99 not_equal (infix "\<noteq>" 50)
101 notation (HTML output)
102 Not ("\<not> _" [40] 40) and
103 "op &" (infixr "\<and>" 35) and
104 "op |" (infixr "\<or>" 30) and
105 not_equal (infix "\<noteq>" 50)
108 iff :: "[bool, bool] => bool" (infixr "<->" 25) where
112 iff (infixr "\<longleftrightarrow>" 25)
119 "_The" :: "[pttrn, bool] => 'a" ("(3THE _./ _)" [0, 10] 10)
121 "_bind" :: "[pttrn, 'a] => letbind" ("(2_ =/ _)" 10)
122 "" :: "letbind => letbinds" ("_")
123 "_binds" :: "[letbind, letbinds] => letbinds" ("_;/ _")
124 "_Let" :: "[letbinds, 'a] => 'a" ("(let (_)/ in (_))" 10)
126 "_case_syntax":: "['a, cases_syn] => 'b" ("(case _ of/ _)" 10)
127 "_case1" :: "['a, 'b] => case_syn" ("(2_ =>/ _)" 10)
128 "" :: "case_syn => cases_syn" ("_")
129 "_case2" :: "[case_syn, cases_syn] => cases_syn" ("_/ | _")
132 "THE x. P" == "CONST The (%x. P)"
133 "_Let (_binds b bs) e" == "_Let b (_Let bs e)"
134 "let x = a in e" == "CONST Let a (%x. e)"
137 [(@{const_syntax The}, fn [Abs abs] =>
138 let val (x, t) = atomic_abs_tr' abs
139 in Syntax.const @{syntax_const "_The"} $ x $ t end)]
140 *} -- {* To avoid eta-contraction of body *}
143 "_case1" :: "['a, 'b] => case_syn" ("(2_ \<Rightarrow>/ _)" 10)
146 All (binder "\<forall>" 10) and
147 Ex (binder "\<exists>" 10) and
148 Ex1 (binder "\<exists>!" 10)
150 notation (HTML output)
151 All (binder "\<forall>" 10) and
152 Ex (binder "\<exists>" 10) and
153 Ex1 (binder "\<exists>!" 10)
156 All (binder "! " 10) and
157 Ex (binder "? " 10) and
158 Ex1 (binder "?! " 10)
161 subsubsection {* Axioms and basic definitions *}
165 subst: "s = t \<Longrightarrow> P s \<Longrightarrow> P t"
166 ext: "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
167 -- {*Extensionality is built into the meta-logic, and this rule expresses
168 a related property. It is an eta-expanded version of the traditional
169 rule, and similar to the ABS rule of HOL*}
171 the_eq_trivial: "(THE x. x = a) = (a::'a)"
173 impI: "(P ==> Q) ==> P-->Q"
174 mp: "[| P-->Q; P |] ==> Q"
178 True_def: "True == ((%x::bool. x) = (%x. x))"
179 All_def: "All(P) == (P = (%x. True))"
180 Ex_def: "Ex(P) == !Q. (!x. P x --> Q) --> Q"
181 False_def: "False == (!P. P)"
182 not_def: "~ P == P-->False"
183 and_def: "P & Q == !R. (P-->Q-->R) --> R"
184 or_def: "P | Q == !R. (P-->R) --> (Q-->R) --> R"
185 Ex1_def: "Ex1(P) == ? x. P(x) & (! y. P(y) --> y=x)"
188 iff: "(P-->Q) --> (Q-->P) --> (P=Q)"
189 True_or_False: "(P=True) | (P=False)"
192 Let_def [code]: "Let s f == f(s)"
193 if_def: "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
207 subsection {* Fundamental rules *}
209 subsubsection {* Equality *}
211 lemma sym: "s = t ==> t = s"
212 by (erule subst) (rule refl)
214 lemma ssubst: "t = s ==> P s ==> P t"
215 by (drule sym) (erule subst)
217 lemma trans: "[| r=s; s=t |] ==> r=t"
220 lemma meta_eq_to_obj_eq:
221 assumes meq: "A == B"
223 by (unfold meq) (rule refl)
225 text {* Useful with @{text erule} for proving equalities from known equalities. *}
229 lemma box_equals: "[| a=b; a=c; b=d |] ==> c=d"
236 text {* For calculational reasoning: *}
238 lemma forw_subst: "a = b ==> P b ==> P a"
241 lemma back_subst: "P a ==> a = b ==> P b"
245 subsubsection {* Congruence rules for application *}
247 text {* Similar to @{text AP_THM} in Gordon's HOL. *}
248 lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
253 text {* Similar to @{text AP_TERM} in Gordon's HOL and FOL's @{text subst_context}. *}
254 lemma arg_cong: "x=y ==> f(x)=f(y)"
259 lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
260 apply (erule ssubst)+
264 lemma cong: "[| f = g; (x::'a) = y |] ==> f x = g y"
269 ML {* val cong_tac = Cong_Tac.cong_tac @{thm cong} *}
272 subsubsection {* Equality of booleans -- iff *}
274 lemma iffI: assumes "P ==> Q" and "Q ==> P" shows "P=Q"
275 by (iprover intro: iff [THEN mp, THEN mp] impI assms)
277 lemma iffD2: "[| P=Q; Q |] ==> P"
280 lemma rev_iffD2: "[| Q; P=Q |] ==> P"
283 lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
284 by (drule sym) (rule iffD2)
286 lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
287 by (drule sym) (rule rev_iffD2)
291 and minor: "[| P --> Q; Q --> P |] ==> R"
293 by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
296 subsubsection {*True*}
299 unfolding True_def by (rule refl)
301 lemma eqTrueI: "P ==> P = True"
302 by (iprover intro: iffI TrueI)
304 lemma eqTrueE: "P = True ==> P"
305 by (erule iffD2) (rule TrueI)
308 subsubsection {*Universal quantifier*}
310 lemma allI: assumes "!!x::'a. P(x)" shows "ALL x. P(x)"
311 unfolding All_def by (iprover intro: ext eqTrueI assms)
313 lemma spec: "ALL x::'a. P(x) ==> P(x)"
314 apply (unfold All_def)
316 apply (erule fun_cong)
320 assumes major: "ALL x. P(x)"
321 and minor: "P(x) ==> R"
323 by (iprover intro: minor major [THEN spec])
326 assumes major: "ALL x. P(x)"
327 and minor: "[| P(x); ALL x. P(x) |] ==> R"
329 by (iprover intro: minor major major [THEN spec])
332 subsubsection {* False *}
335 Depends upon @{text spec}; it is impossible to do propositional
336 logic before quantifiers!
339 lemma FalseE: "False ==> P"
340 apply (unfold False_def)
344 lemma False_neq_True: "False = True ==> P"
345 by (erule eqTrueE [THEN FalseE])
348 subsubsection {* Negation *}
351 assumes "P ==> False"
353 apply (unfold not_def)
354 apply (iprover intro: impI assms)
357 lemma False_not_True: "False ~= True"
359 apply (erule False_neq_True)
362 lemma True_not_False: "True ~= False"
365 apply (erule False_neq_True)
368 lemma notE: "[| ~P; P |] ==> R"
369 apply (unfold not_def)
370 apply (erule mp [THEN FalseE])
374 lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
375 by (erule notE [THEN notI]) (erule meta_mp)
378 subsubsection {*Implication*}
381 assumes "P-->Q" "P" "Q ==> R"
383 by (iprover intro: assms mp)
385 (* Reduces Q to P-->Q, allowing substitution in P. *)
386 lemma rev_mp: "[| P; P --> Q |] ==> Q"
387 by (iprover intro: mp)
393 by (iprover intro: notI minor major [THEN notE])
395 (*not used at all, but we already have the other 3 combinations *)
398 and minor: "P ==> ~Q"
400 by (iprover intro: notI minor major notE)
402 lemma not_sym: "t ~= s ==> s ~= t"
403 by (erule contrapos_nn) (erule sym)
405 lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
406 by (erule subst, erule ssubst, assumption)
408 (*still used in HOLCF*)
410 assumes pq: "P ==> Q"
413 apply (rule nq [THEN contrapos_nn])
417 subsubsection {*Existential quantifier*}
419 lemma exI: "P x ==> EX x::'a. P x"
420 apply (unfold Ex_def)
421 apply (iprover intro: allI allE impI mp)
425 assumes major: "EX x::'a. P(x)"
426 and minor: "!!x. P(x) ==> Q"
428 apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
429 apply (iprover intro: impI [THEN allI] minor)
433 subsubsection {*Conjunction*}
435 lemma conjI: "[| P; Q |] ==> P&Q"
436 apply (unfold and_def)
437 apply (iprover intro: impI [THEN allI] mp)
440 lemma conjunct1: "[| P & Q |] ==> P"
441 apply (unfold and_def)
442 apply (iprover intro: impI dest: spec mp)
445 lemma conjunct2: "[| P & Q |] ==> Q"
446 apply (unfold and_def)
447 apply (iprover intro: impI dest: spec mp)
452 and minor: "[| P; Q |] ==> R"
455 apply (rule major [THEN conjunct1])
456 apply (rule major [THEN conjunct2])
460 assumes "P" "P ==> Q" shows "P & Q"
461 by (iprover intro: conjI assms)
464 subsubsection {*Disjunction*}
466 lemma disjI1: "P ==> P|Q"
467 apply (unfold or_def)
468 apply (iprover intro: allI impI mp)
471 lemma disjI2: "Q ==> P|Q"
472 apply (unfold or_def)
473 apply (iprover intro: allI impI mp)
478 and minorP: "P ==> R"
479 and minorQ: "Q ==> R"
481 by (iprover intro: minorP minorQ impI
482 major [unfolded or_def, THEN spec, THEN mp, THEN mp])
485 subsubsection {*Classical logic*}
488 assumes prem: "~P ==> P"
490 apply (rule True_or_False [THEN disjE, THEN eqTrueE])
492 apply (rule notI [THEN prem, THEN eqTrueI])
497 lemmas ccontr = FalseE [THEN classical, standard]
499 (*notE with premises exchanged; it discharges ~R so that it can be used to
500 make elimination rules*)
503 and premnot: "~R ==> ~P"
506 apply (erule notE [OF premnot premp])
509 (*Double negation law*)
510 lemma notnotD: "~~P ==> P"
511 apply (rule classical)
520 by (iprover intro: classical p1 p2 notE)
523 subsubsection {*Unique existence*}
526 assumes "P a" "!!x. P(x) ==> x=a"
528 by (unfold Ex1_def, iprover intro: assms exI conjI allI impI)
530 text{*Sometimes easier to use: the premises have no shared variables. Safe!*}
532 assumes ex_prem: "EX x. P(x)"
533 and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
535 by (iprover intro: ex_prem [THEN exE] ex1I eq)
538 assumes major: "EX! x. P(x)"
539 and minor: "!!x. [| P(x); ALL y. P(y) --> y=x |] ==> R"
541 apply (rule major [unfolded Ex1_def, THEN exE])
543 apply (iprover intro: minor)
546 lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
553 subsubsection {*THE: definite description operator*}
557 and premx: "!!x. P x ==> x=a"
558 shows "(THE x. P x) = a"
559 apply (rule trans [OF _ the_eq_trivial])
560 apply (rule_tac f = "The" in arg_cong)
564 apply (erule ssubst, rule prema)
568 assumes "P a" and "!!x. P x ==> x=a"
569 shows "P (THE x. P x)"
570 by (iprover intro: assms the_equality [THEN ssubst])
572 lemma theI': "EX! x. P x ==> P (THE x. P x)"
580 (*Easier to apply than theI: only one occurrence of P*)
582 assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
583 shows "Q (THE x. P x)"
584 by (iprover intro: assms theI)
586 lemma the1I2: assumes "EX! x. P x" "\<And>x. P x \<Longrightarrow> Q x" shows "Q (THE x. P x)"
587 by(iprover intro:assms(2) theI2[where P=P and Q=Q] ex1E[OF assms(1)]
590 lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
591 apply (rule the_equality)
594 apply (erule all_dupE)
603 lemma the_sym_eq_trivial: "(THE y. x=y) = x"
604 apply (rule the_equality)
610 subsubsection {*Classical intro rules for disjunction and existential quantifiers*}
613 assumes "~Q ==> P" shows "P|Q"
614 apply (rule classical)
615 apply (iprover intro: assms disjI1 disjI2 notI elim: notE)
618 lemma excluded_middle: "~P | P"
619 by (iprover intro: disjCI)
622 case distinction as a natural deduction rule.
623 Note that @{term "~P"} is the second case, not the first
625 lemma case_split [case_names True False]:
626 assumes prem1: "P ==> Q"
627 and prem2: "~P ==> Q"
629 apply (rule excluded_middle [THEN disjE])
634 (*Classical implies (-->) elimination. *)
636 assumes major: "P-->Q"
637 and minor: "~P ==> R" "Q ==> R"
639 apply (rule excluded_middle [of P, THEN disjE])
640 apply (iprover intro: minor major [THEN mp])+
643 (*This version of --> elimination works on Q before P. It works best for
644 those cases in which P holds "almost everywhere". Can't install as
645 default: would break old proofs.*)
647 assumes major: "P-->Q"
648 and minor: "Q ==> R" "~P ==> R"
650 apply (rule excluded_middle [of P, THEN disjE])
651 apply (iprover intro: minor major [THEN mp])+
654 (*Classical <-> elimination. *)
657 and minor: "[| P; Q |] ==> R" "[| ~P; ~Q |] ==> R"
659 apply (rule major [THEN iffE])
660 apply (iprover intro: minor elim: impCE notE)
664 assumes "ALL x. ~P(x) ==> P(a)"
667 apply (iprover intro: assms exI allI notI notE [of "\<exists>x. P x"])
671 subsubsection {* Intuitionistic Reasoning *}
676 and 3: "P --> Q ==> P"
679 from 3 and 1 have P .
680 with 1 have Q by (rule impE)
685 assumes 1: "ALL x. P x"
686 and 2: "P x ==> ALL x. P x ==> Q"
689 from 1 have "P x" by (rule spec)
690 from this and 1 show Q by (rule 2)
698 from 2 and 1 have P .
699 with 1 show R by (rule notE)
702 lemma TrueE: "True ==> P ==> P" .
703 lemma notFalseE: "~ False ==> P ==> P" .
705 lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
706 and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
707 and [Pure.elim 2] = allE notE' impE'
708 and [Pure.intro] = exI disjI2 disjI1
710 lemmas [trans] = trans
711 and [sym] = sym not_sym
712 and [Pure.elim?] = iffD1 iffD2 impE
714 use "Tools/hologic.ML"
717 subsubsection {* Atomizing meta-level connectives *}
720 eq_reflection: "x = y \<Longrightarrow> x \<equiv> y" (*admissible axiom*)
722 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
725 then show "ALL x. P x" ..
728 then show "!!x. P x" by (rule allE)
731 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
734 show "A --> B" by (rule impI) (rule r)
736 assume "A --> B" and A
737 then show B by (rule mp)
740 lemma atomize_not: "(A ==> False) == Trueprop (~A)"
742 assume r: "A ==> False"
743 show "~A" by (rule notI) (rule r)
746 then show False by (rule notE)
749 lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
752 show "x = y" by (unfold `x == y`) (rule refl)
755 then show "x == y" by (rule eq_reflection)
758 lemma atomize_conj [atomize]: "(A &&& B) == Trueprop (A & B)"
760 assume conj: "A &&& B"
763 from conj show A by (rule conjunctionD1)
764 from conj show B by (rule conjunctionD2)
775 lemmas [symmetric, rulify] = atomize_all atomize_imp
776 and [symmetric, defn] = atomize_all atomize_imp atomize_eq
779 subsubsection {* Atomizing elimination rules *}
781 setup AtomizeElim.setup
783 lemma atomize_exL[atomize_elim]: "(!!x. P x ==> Q) == ((EX x. P x) ==> Q)"
786 lemma atomize_conjL[atomize_elim]: "(A ==> B ==> C) == (A & B ==> C)"
789 lemma atomize_disjL[atomize_elim]: "((A ==> C) ==> (B ==> C) ==> C) == ((A | B ==> C) ==> C)"
792 lemma atomize_elimL[atomize_elim]: "(!!B. (A ==> B) ==> B) == Trueprop A" ..
795 subsection {* Package setup *}
797 subsubsection {* Classical Reasoner setup *}
799 lemma imp_elim: "P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R"
800 by (rule classical) iprover
802 lemma swap: "~ P ==> (~ R ==> P) ==> R"
803 by (rule classical) iprover
806 "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
809 structure Hypsubst = HypsubstFun(
811 structure Simplifier = Simplifier
812 val dest_eq = HOLogic.dest_eq
813 val dest_Trueprop = HOLogic.dest_Trueprop
814 val dest_imp = HOLogic.dest_imp
815 val eq_reflection = @{thm eq_reflection}
816 val rev_eq_reflection = @{thm meta_eq_to_obj_eq}
817 val imp_intr = @{thm impI}
818 val rev_mp = @{thm rev_mp}
819 val subst = @{thm subst}
821 val thin_refl = @{thm thin_refl};
822 val prop_subst = @{lemma "PROP P t ==> PROP prop (x = t ==> PROP P x)"
823 by (unfold prop_def) (drule eq_reflection, unfold)}
827 structure Classical = ClassicalFun(
829 val imp_elim = @{thm imp_elim}
830 val not_elim = @{thm notE}
831 val swap = @{thm swap}
832 val classical = @{thm classical}
833 val sizef = Drule.size_of_thm
834 val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
837 structure Basic_Classical: BASIC_CLASSICAL = Classical;
838 open Basic_Classical;
840 ML_Antiquote.value "claset"
841 (Scan.succeed "Classical.claset_of (ML_Context.the_local_context ())");
844 setup Classical.setup
848 fun non_bool_eq (@{const_name "op ="}, Type (_, [T, _])) = T <> @{typ bool}
849 | non_bool_eq _ = false;
851 SUBGOAL (fn (goal, i) =>
852 if Term.exists_Const non_bool_eq goal
853 then Hypsubst.hyp_subst_tac i
856 Hypsubst.hypsubst_setup
857 (*prevent substitution on bool*)
858 #> Context_Rules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac)
862 declare iffI [intro!]
870 declare iffCE [elim!]
876 declare ex_ex1I [intro!]
878 and the_equality [intro]
884 ML {* val HOL_cs = @{claset} *}
886 lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
888 apply (erule (1) meta_mp)
891 declare ex_ex1I [rule del, intro! 2]
894 lemmas [intro?] = ext
895 and [elim?] = ex1_implies_ex
897 (*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
898 lemma alt_ex1E [elim!]:
899 assumes major: "\<exists>!x. P x"
900 and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
902 apply (rule ex1E [OF major])
904 apply (tactic {* ares_tac @{thms allI} 1 *})+
905 apply (tactic {* etac (Classical.dup_elim @{thm allE}) 1 *})
910 structure Blast = Blast
913 type claset = Classical.claset
914 val equality_name = @{const_name "op ="}
915 val not_name = @{const_name Not}
916 val notE = @{thm notE}
917 val ccontr = @{thm ccontr}
918 val contr_tac = Classical.contr_tac
919 val dup_intr = Classical.dup_intr
920 val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
921 val rep_cs = Classical.rep_cs
922 val cla_modifiers = Classical.cla_modifiers
923 val cla_meth' = Classical.cla_meth'
925 val blast_tac = Blast.blast_tac;
931 subsubsection {* Simplifier *}
933 lemma eta_contract_eq: "(%s. f s) = f" ..
936 shows not_not: "(~ ~ P) = P"
937 and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
939 "(P ~= Q) = (P = (~Q))"
940 "(P | ~P) = True" "(~P | P) = True"
942 and not_True_eq_False [code]: "(\<not> True) = False"
943 and not_False_eq_True [code]: "(\<not> False) = True"
945 "(~P) ~= P" "P ~= (~P)"
947 and eq_True: "(P = True) = P"
948 and "(False=P) = (~P)"
949 and eq_False: "(P = False) = (\<not> P)"
951 "(True --> P) = P" "(False --> P) = True"
952 "(P --> True) = True" "(P --> P) = True"
953 "(P --> False) = (~P)" "(P --> ~P) = (~P)"
954 "(P & True) = P" "(True & P) = P"
955 "(P & False) = False" "(False & P) = False"
956 "(P & P) = P" "(P & (P & Q)) = (P & Q)"
957 "(P & ~P) = False" "(~P & P) = False"
958 "(P | True) = True" "(True | P) = True"
959 "(P | False) = P" "(False | P) = P"
960 "(P | P) = P" "(P | (P | Q)) = (P | Q)" and
961 "(ALL x. P) = P" "(EX x. P) = P" "EX x. x=t" "EX x. t=x"
963 "!!P. (EX x. x=t & P(x)) = P(t)"
964 "!!P. (EX x. t=x & P(x)) = P(t)"
965 "!!P. (ALL x. x=t --> P(x)) = P(t)"
966 "!!P. (ALL x. t=x --> P(x)) = P(t)"
967 by (blast, blast, blast, blast, blast, iprover+)
969 lemma disj_absorb: "(A | A) = A"
972 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
975 lemma conj_absorb: "(A & A) = A"
978 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
982 shows eq_commute: "(a=b) = (b=a)"
983 and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
984 and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
985 lemma neq_commute: "(a~=b) = (b~=a)" by iprover
988 shows conj_commute: "(P&Q) = (Q&P)"
989 and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
990 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
992 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
995 shows disj_commute: "(P|Q) = (Q|P)"
996 and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
997 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
999 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
1001 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
1002 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
1004 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
1005 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
1007 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
1008 lemma imp_conjL: "((P&Q) -->R) = (P --> (Q --> R))" by iprover
1009 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
1011 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
1012 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
1013 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
1015 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
1016 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
1018 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
1021 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
1022 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
1023 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
1024 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
1025 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
1026 lemma disj_not2: "(P | ~Q) = (Q --> P)" -- {* changes orientation :-( *}
1028 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
1030 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
1033 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
1034 -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
1035 -- {* cases boil down to the same thing. *}
1038 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
1039 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
1040 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
1041 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
1042 lemma all_not_ex: "(ALL x. P x) = (~ (EX x. ~ P x ))" by blast
1044 declare All_def [noatp]
1046 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
1047 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
1050 \medskip The @{text "&"} congruence rule: not included by default!
1051 May slow rewrite proofs down by as much as 50\% *}
1054 "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
1057 lemma rev_conj_cong:
1058 "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
1061 text {* The @{text "|"} congruence rule: not included by default! *}
1064 "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
1068 text {* \medskip if-then-else rules *}
1070 lemma if_True [code]: "(if True then x else y) = x"
1071 by (unfold if_def) blast
1073 lemma if_False [code]: "(if False then x else y) = y"
1074 by (unfold if_def) blast
1076 lemma if_P: "P ==> (if P then x else y) = x"
1077 by (unfold if_def) blast
1079 lemma if_not_P: "~P ==> (if P then x else y) = y"
1080 by (unfold if_def) blast
1082 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
1083 apply (rule case_split [of Q])
1084 apply (simplesubst if_P)
1085 prefer 3 apply (simplesubst if_not_P, blast+)
1088 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
1089 by (simplesubst split_if, blast)
1091 lemmas if_splits [noatp] = split_if split_if_asm
1093 lemma if_cancel: "(if c then x else x) = x"
1094 by (simplesubst split_if, blast)
1096 lemma if_eq_cancel: "(if x = y then y else x) = x"
1097 by (simplesubst split_if, blast)
1099 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
1100 -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
1103 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
1104 -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
1105 apply (simplesubst split_if, blast)
1108 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
1109 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
1111 text {* \medskip let rules for simproc *}
1113 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow> Let x f \<equiv> Let x g"
1116 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow> Let x f \<equiv> g"
1120 The following copy of the implication operator is useful for
1121 fine-tuning congruence rules. It instructs the simplifier to simplify
1125 definition simp_implies :: "[prop, prop] => prop" (infixr "=simp=>" 1) where
1126 [code del]: "simp_implies \<equiv> op ==>"
1128 lemma simp_impliesI:
1129 assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
1130 shows "PROP P =simp=> PROP Q"
1131 apply (unfold simp_implies_def)
1136 lemma simp_impliesE:
1137 assumes PQ: "PROP P =simp=> PROP Q"
1139 and QR: "PROP Q \<Longrightarrow> PROP R"
1142 apply (rule PQ [unfolded simp_implies_def])
1146 lemma simp_implies_cong:
1147 assumes PP' :"PROP P == PROP P'"
1148 and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
1149 shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
1150 proof (unfold simp_implies_def, rule equal_intr_rule)
1151 assume PQ: "PROP P \<Longrightarrow> PROP Q"
1153 from PP' [symmetric] and P' have "PROP P"
1154 by (rule equal_elim_rule1)
1155 then have "PROP Q" by (rule PQ)
1156 with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
1158 assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
1160 from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
1161 then have "PROP Q'" by (rule P'Q')
1162 with P'QQ' [OF P', symmetric] show "PROP Q"
1163 by (rule equal_elim_rule1)
1167 assumes "P \<longrightarrow> Q \<longrightarrow> R"
1168 shows "P \<and> Q \<longrightarrow> R"
1169 using assms by blast
1172 assumes "\<And>x. P x = Q x"
1173 shows "(\<forall>x. P x) = (\<forall>x. Q x)"
1174 using assms by blast
1177 assumes "\<And>x. P x = Q x"
1178 shows "(\<exists>x. P x) = (\<exists>x. Q x)"
1179 using assms by blast
1182 "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
1186 "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
1189 use "Tools/simpdata.ML"
1190 ML {* open Simpdata *}
1193 Simplifier.method_setup Splitter.split_modifiers
1194 #> Simplifier.map_simpset (K Simpdata.simpset_simprocs)
1200 text {* Simproc for proving @{text "(y = x) == False"} from premise @{text "~(x = y)"}: *}
1202 simproc_setup neq ("x = y") = {* fn _ =>
1204 val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
1205 fun is_neq eq lhs rhs thm =
1206 (case Thm.prop_of thm of
1207 _ $ (Not $ (eq' $ l' $ r')) =>
1208 Not = HOLogic.Not andalso eq' = eq andalso
1209 r' aconv lhs andalso l' aconv rhs
1212 (case Thm.term_of ct of
1214 (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of_ss ss) of
1215 SOME thm => SOME (thm RS neq_to_EQ_False)
1221 simproc_setup let_simp ("Let x f") = {*
1223 val (f_Let_unfold, x_Let_unfold) =
1224 let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_unfold}
1225 in (cterm_of @{theory} f, cterm_of @{theory} x) end
1226 val (f_Let_folded, x_Let_folded) =
1227 let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_folded}
1228 in (cterm_of @{theory} f, cterm_of @{theory} x) end;
1230 let val [(_ $ _ $ (g $ _))] = prems_of @{thm Let_folded}
1231 in cterm_of @{theory} g end;
1232 fun count_loose (Bound i) k = if i >= k then 1 else 0
1233 | count_loose (s $ t) k = count_loose s k + count_loose t k
1234 | count_loose (Abs (_, _, t)) k = count_loose t (k + 1)
1235 | count_loose _ _ = 0;
1236 fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
1238 of Abs (_, _, t') => count_loose t' 0 <= 1
1240 in fn _ => fn ss => fn ct => if is_trivial_let (Thm.term_of ct)
1241 then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
1242 else let (*Norbert Schirmer's case*)
1243 val ctxt = Simplifier.the_context ss;
1244 val thy = ProofContext.theory_of ctxt;
1245 val t = Thm.term_of ct;
1246 val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
1247 in Option.map (hd o Variable.export ctxt' ctxt o single)
1248 (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
1249 if is_Free x orelse is_Bound x orelse is_Const x
1250 then SOME @{thm Let_def}
1253 val n = case f of (Abs (x, _, _)) => x | _ => "x";
1254 val cx = cterm_of thy x;
1255 val {T = xT, ...} = rep_cterm cx;
1256 val cf = cterm_of thy f;
1257 val fx_g = Simplifier.rewrite ss (Thm.capply cf cx);
1258 val (_ $ _ $ g) = prop_of fx_g;
1259 val g' = abstract_over (x,g);
1264 cterm_instantiate [(f_Let_unfold, cf), (x_Let_unfold, cx)] @{thm Let_unfold};
1265 in SOME (rl OF [fx_g]) end
1266 else if Term.betapply (f, x) aconv g then NONE (*avoid identity conversion*)
1268 val abs_g'= Abs (n,xT,g');
1270 val g_g'x = symmetric (beta_conversion false (cterm_of thy g'x));
1271 val rl = cterm_instantiate
1272 [(f_Let_folded, cterm_of thy f), (x_Let_folded, cx),
1273 (g_Let_folded, cterm_of thy abs_g')]
1275 in SOME (rl OF [transitive fx_g g_g'x])
1282 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
1284 assume "True \<Longrightarrow> PROP P"
1285 from this [OF TrueI] show "PROP P" .
1288 then show "PROP P" .
1292 "!!P Q. (EX x. P x & Q) = ((EX x. P x) & Q)"
1293 "!!P Q. (EX x. P & Q x) = (P & (EX x. Q x))"
1294 "!!P Q. (EX x. P x | Q) = ((EX x. P x) | Q)"
1295 "!!P Q. (EX x. P | Q x) = (P | (EX x. Q x))"
1296 "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
1297 "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
1298 -- {* Miniscoping: pushing in existential quantifiers. *}
1299 by (iprover | blast)+
1302 "!!P Q. (ALL x. P x & Q) = ((ALL x. P x) & Q)"
1303 "!!P Q. (ALL x. P & Q x) = (P & (ALL x. Q x))"
1304 "!!P Q. (ALL x. P x | Q) = ((ALL x. P x) | Q)"
1305 "!!P Q. (ALL x. P | Q x) = (P | (ALL x. Q x))"
1306 "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
1307 "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
1308 -- {* Miniscoping: pushing in universal quantifiers. *}
1309 by (iprover | blast)+
1312 triv_forall_equality (*prunes params*)
1313 True_implies_equals (*prune asms `True'*)
1319 (*In general it seems wrong to add distributive laws by default: they
1320 might cause exponential blow-up. But imp_disjL has been in for a while
1321 and cannot be removed without affecting existing proofs. Moreover,
1322 rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
1323 grounds that it allows simplification of R in the two cases.*)
1341 lemmas [cong] = imp_cong simp_implies_cong
1342 lemmas [split] = split_if
1344 ML {* val HOL_ss = @{simpset} *}
1346 text {* Simplifies x assuming c and y assuming ~c *}
1349 and "c \<Longrightarrow> x = u"
1350 and "\<not> c \<Longrightarrow> y = v"
1351 shows "(if b then x else y) = (if c then u else v)"
1352 unfolding if_def using assms by simp
1354 text {* Prevents simplification of x and y:
1355 faster and allows the execution of functional programs. *}
1356 lemma if_weak_cong [cong]:
1358 shows "(if b then x else y) = (if c then x else y)"
1359 using assms by (rule arg_cong)
1361 text {* Prevents simplification of t: much faster *}
1362 lemma let_weak_cong:
1364 shows "(let x = a in t x) = (let x = b in t x)"
1365 using assms by (rule arg_cong)
1367 text {* To tidy up the result of a simproc. Only the RHS will be simplified. *}
1370 shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
1374 "f (if c then x else y) = (if c then f x else f y)"
1377 text {* This lemma restricts the effect of the rewrite rule u=v to the left-hand
1378 side of an equality. Used in @{text "{Integ,Real}/simproc.ML"} *}
1379 lemma restrict_to_left:
1381 shows "(x = z) = (y = z)"
1385 subsubsection {* Generic cases and induction *}
1387 text {* Rule projections: *}
1390 structure Project_Rule = Project_Rule
1392 val conjunct1 = @{thm conjunct1}
1393 val conjunct2 = @{thm conjunct2}
1398 definition induct_forall where
1399 "induct_forall P == \<forall>x. P x"
1401 definition induct_implies where
1402 "induct_implies A B == A \<longrightarrow> B"
1404 definition induct_equal where
1405 "induct_equal x y == x = y"
1407 definition induct_conj where
1408 "induct_conj A B == A \<and> B"
1410 definition induct_true where
1411 "induct_true == True"
1413 definition induct_false where
1414 "induct_false == False"
1416 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
1417 by (unfold atomize_all induct_forall_def)
1419 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
1420 by (unfold atomize_imp induct_implies_def)
1422 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
1423 by (unfold atomize_eq induct_equal_def)
1425 lemma induct_conj_eq: "(A &&& B) == Trueprop (induct_conj A B)"
1426 by (unfold atomize_conj induct_conj_def)
1428 lemmas induct_atomize' = induct_forall_eq induct_implies_eq induct_conj_eq
1429 lemmas induct_atomize = induct_atomize' induct_equal_eq
1430 lemmas induct_rulify' [symmetric, standard] = induct_atomize'
1431 lemmas induct_rulify [symmetric, standard] = induct_atomize
1432 lemmas induct_rulify_fallback =
1433 induct_forall_def induct_implies_def induct_equal_def induct_conj_def
1434 induct_true_def induct_false_def
1437 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
1438 induct_conj (induct_forall A) (induct_forall B)"
1439 by (unfold induct_forall_def induct_conj_def) iprover
1441 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
1442 induct_conj (induct_implies C A) (induct_implies C B)"
1443 by (unfold induct_implies_def induct_conj_def) iprover
1445 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
1447 assume r: "induct_conj A B ==> PROP C" and A B
1448 show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
1450 assume r: "A ==> B ==> PROP C" and "induct_conj A B"
1451 show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
1454 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
1456 lemma induct_trueI: "induct_true"
1457 by (simp add: induct_true_def)
1459 text {* Method setup. *}
1462 structure Induct = Induct
1464 val cases_default = @{thm case_split}
1465 val atomize = @{thms induct_atomize}
1466 val rulify = @{thms induct_rulify'}
1467 val rulify_fallback = @{thms induct_rulify_fallback}
1468 val equal_def = @{thm induct_equal_def}
1469 fun dest_def (Const (@{const_name induct_equal}, _) $ t $ u) = SOME (t, u)
1471 val trivial_tac = match_tac @{thms induct_trueI}
1477 Context.theory_map (Induct.map_simpset (fn ss => ss
1478 setmksimps (Simpdata.mksimps Simpdata.mksimps_pairs #>
1479 map (Simplifier.rewrite_rule (map Thm.symmetric
1480 @{thms induct_rulify_fallback induct_true_def induct_false_def})))
1482 [Simplifier.simproc @{theory} "swap_induct_false"
1483 ["induct_false ==> PROP P ==> PROP Q"]
1485 (fn _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
1486 if P <> Q then SOME Drule.swap_prems_eq else NONE
1488 Simplifier.simproc @{theory} "induct_equal_conj_curry"
1489 ["induct_conj P Q ==> PROP R"]
1491 (fn _ $ (_ $ P) $ _ =>
1493 fun is_conj (@{const induct_conj} $ P $ Q) =
1494 is_conj P andalso is_conj Q
1495 | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
1496 | is_conj @{const induct_true} = true
1497 | is_conj @{const induct_false} = true
1499 in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
1503 text {* Pre-simplification of induction and cases rules *}
1505 lemma [induct_simp]: "(!!x. induct_equal x t ==> PROP P x) == PROP P t"
1506 unfolding induct_equal_def
1508 assume R: "!!x. x = t ==> PROP P x"
1509 show "PROP P t" by (rule R [OF refl])
1511 fix x assume "PROP P t" "x = t"
1512 then show "PROP P x" by simp
1515 lemma [induct_simp]: "(!!x. induct_equal t x ==> PROP P x) == PROP P t"
1516 unfolding induct_equal_def
1518 assume R: "!!x. t = x ==> PROP P x"
1519 show "PROP P t" by (rule R [OF refl])
1521 fix x assume "PROP P t" "t = x"
1522 then show "PROP P x" by simp
1525 lemma [induct_simp]: "(induct_false ==> P) == Trueprop induct_true"
1526 unfolding induct_false_def induct_true_def
1527 by (iprover intro: equal_intr_rule)
1529 lemma [induct_simp]: "(induct_true ==> PROP P) == PROP P"
1530 unfolding induct_true_def
1532 assume R: "True \<Longrightarrow> PROP P"
1533 from TrueI show "PROP P" by (rule R)
1536 then show "PROP P" .
1539 lemma [induct_simp]: "(PROP P ==> induct_true) == Trueprop induct_true"
1540 unfolding induct_true_def
1541 by (iprover intro: equal_intr_rule)
1543 lemma [induct_simp]: "(!!x. induct_true) == Trueprop induct_true"
1544 unfolding induct_true_def
1545 by (iprover intro: equal_intr_rule)
1547 lemma [induct_simp]: "induct_implies induct_true P == P"
1548 by (simp add: induct_implies_def induct_true_def)
1550 lemma [induct_simp]: "(x = x) = True"
1553 hide const induct_forall induct_implies induct_equal induct_conj induct_true induct_false
1555 use "~~/src/Tools/induct_tacs.ML"
1556 setup InductTacs.setup
1559 subsubsection {* Coherent logic *}
1562 structure Coherent = Coherent
1564 val atomize_elimL = @{thm atomize_elimL}
1565 val atomize_exL = @{thm atomize_exL}
1566 val atomize_conjL = @{thm atomize_conjL}
1567 val atomize_disjL = @{thm atomize_disjL}
1568 val operator_names =
1569 [@{const_name "op |"}, @{const_name "op &"}, @{const_name "Ex"}]
1573 setup Coherent.setup
1576 subsubsection {* Reorienting equalities *}
1579 signature REORIENT_PROC =
1581 val add : (term -> bool) -> theory -> theory
1582 val proc : morphism -> simpset -> cterm -> thm option
1585 structure Reorient_Proc : REORIENT_PROC =
1587 structure Data = Theory_Data
1589 type T = ((term -> bool) * stamp) list;
1592 fun merge data : T = Library.merge (eq_snd op =) data;
1594 fun add m = Data.map (cons (m, stamp ()));
1595 fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
1597 val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
1598 fun proc phi ss ct =
1600 val ctxt = Simplifier.the_context ss;
1601 val thy = ProofContext.theory_of ctxt;
1603 case Thm.term_of ct of
1604 (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
1611 subsection {* Other simple lemmas and lemma duplicates *}
1613 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
1616 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
1618 apply (rule_tac a = "%x. THE y. P x y" in ex1I)
1619 apply (fast dest!: theI')
1620 apply (fast intro: ext the1_equality [symmetric])
1625 apply (erule_tac x = "%z. if z = x then y else f z" in allE)
1628 apply (case_tac "xa = x")
1629 apply (drule_tac [3] x = x in fun_cong, simp_all)
1632 lemmas eq_sym_conv = eq_commute
1635 "(\<not>(P \<and> Q)) = (\<not> P \<or> \<not> Q)" "(\<not> (P \<or> Q)) = (\<not> P \<and> \<not>Q)" "(P \<longrightarrow> Q) = (\<not>P \<or> Q)"
1636 "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))"
1637 "(\<not> \<not>(P)) = P"
1641 subsection {* Basic ML bindings *}
1644 val FalseE = @{thm FalseE}
1645 val Let_def = @{thm Let_def}
1646 val TrueI = @{thm TrueI}
1647 val allE = @{thm allE}
1648 val allI = @{thm allI}
1649 val all_dupE = @{thm all_dupE}
1650 val arg_cong = @{thm arg_cong}
1651 val box_equals = @{thm box_equals}
1652 val ccontr = @{thm ccontr}
1653 val classical = @{thm classical}
1654 val conjE = @{thm conjE}
1655 val conjI = @{thm conjI}
1656 val conjunct1 = @{thm conjunct1}
1657 val conjunct2 = @{thm conjunct2}
1658 val disjCI = @{thm disjCI}
1659 val disjE = @{thm disjE}
1660 val disjI1 = @{thm disjI1}
1661 val disjI2 = @{thm disjI2}
1662 val eq_reflection = @{thm eq_reflection}
1663 val ex1E = @{thm ex1E}
1664 val ex1I = @{thm ex1I}
1665 val ex1_implies_ex = @{thm ex1_implies_ex}
1666 val exE = @{thm exE}
1667 val exI = @{thm exI}
1668 val excluded_middle = @{thm excluded_middle}
1669 val ext = @{thm ext}
1670 val fun_cong = @{thm fun_cong}
1671 val iffD1 = @{thm iffD1}
1672 val iffD2 = @{thm iffD2}
1673 val iffI = @{thm iffI}
1674 val impE = @{thm impE}
1675 val impI = @{thm impI}
1676 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
1678 val notE = @{thm notE}
1679 val notI = @{thm notI}
1680 val not_all = @{thm not_all}
1681 val not_ex = @{thm not_ex}
1682 val not_iff = @{thm not_iff}
1683 val not_not = @{thm not_not}
1684 val not_sym = @{thm not_sym}
1685 val refl = @{thm refl}
1686 val rev_mp = @{thm rev_mp}
1687 val spec = @{thm spec}
1688 val ssubst = @{thm ssubst}
1689 val subst = @{thm subst}
1690 val sym = @{thm sym}
1691 val trans = @{thm trans}
1695 subsection {* Code generator setup *}
1697 subsubsection {* SML code generator setup *}
1699 use "Tools/recfun_codegen.ML"
1703 #> RecfunCodegen.setup
1704 #> Codegen.map_unfold (K HOL_basic_ss)
1710 fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
1714 let val b = one_of [false, true]
1715 in (b, fn () => term_of_bool b) end;
1719 fun term_of_prop b =
1720 HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
1728 "op |" ("(_ orelse/ _)")
1729 "op &" ("(_ andalso/ _)")
1730 "If" ("(if _/ then _/ else _)")
1735 fun eq_codegen thy defs dep thyname b t gr =
1736 (case strip_comb t of
1737 (Const (@{const_name "op ="}, Type (_, [Type ("fun", _), _])), _) => NONE
1738 | (Const (@{const_name "op ="}, _), [t, u]) =>
1740 val (pt, gr') = Codegen.invoke_codegen thy defs dep thyname false t gr;
1741 val (pu, gr'') = Codegen.invoke_codegen thy defs dep thyname false u gr';
1742 val (_, gr''') = Codegen.invoke_tycodegen thy defs dep thyname false HOLogic.boolT gr'';
1744 SOME (Codegen.parens
1745 (Pretty.block [pt, Codegen.str " =", Pretty.brk 1, pu]), gr''')
1747 | (t as Const (@{const_name "op ="}, _), ts) => SOME (Codegen.invoke_codegen
1748 thy defs dep thyname b (Codegen.eta_expand t ts 2) gr)
1752 Codegen.add_codegen "eq_codegen" eq_codegen
1756 subsubsection {* Generic code generator preprocessor setup *}
1759 Code_Preproc.map_pre (K HOL_basic_ss)
1760 #> Code_Preproc.map_post (K HOL_basic_ss)
1763 subsubsection {* Equality *}
1766 fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
1767 assumes eq_equals: "eq x y \<longleftrightarrow> x = y"
1770 lemma eq [code_unfold, code_inline del]: "eq = (op =)"
1771 by (rule ext eq_equals)+
1773 lemma eq_refl: "eq x x \<longleftrightarrow> True"
1774 unfolding eq by rule+
1776 lemma equals_eq: "(op =) \<equiv> eq"
1777 by (rule eq_reflection) (rule ext, rule ext, rule sym, rule eq_equals)
1779 declare equals_eq [symmetric, code_post]
1783 declare equals_eq [code]
1786 Code_Preproc.map_pre (fn simpset =>
1787 simpset addsimprocs [Simplifier.simproc_i @{theory} "eq" [@{term "op ="}]
1788 (fn thy => fn _ => fn t as Const (_, T) => case strip_type T
1789 of ((T as Type _) :: _, _) => SOME @{thm equals_eq}
1794 subsubsection {* Generic code generator foundation *}
1796 text {* Datatypes *}
1798 code_datatype True False
1800 code_datatype "TYPE('a\<Colon>{})"
1802 code_datatype "prop" Trueprop
1804 text {* Code equations *}
1807 shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q"
1808 and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
1809 and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
1812 shows "False \<and> P \<longleftrightarrow> False"
1813 and "True \<and> P \<longleftrightarrow> P"
1814 and "P \<and> False \<longleftrightarrow> False"
1815 and "P \<and> True \<longleftrightarrow> P" by simp_all
1818 shows "False \<or> P \<longleftrightarrow> P"
1819 and "True \<or> P \<longleftrightarrow> True"
1820 and "P \<or> False \<longleftrightarrow> P"
1821 and "P \<or> True \<longleftrightarrow> True" by simp_all
1824 shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
1825 and "(True \<longrightarrow> P) \<longleftrightarrow> P"
1826 and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
1827 and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
1829 instantiation itself :: (type) eq
1832 definition eq_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
1833 "eq_itself x y \<longleftrightarrow> x = y"
1836 qed (fact eq_itself_def)
1840 lemma eq_itself_code [code]:
1841 "eq_class.eq TYPE('a) TYPE('a) \<longleftrightarrow> True"
1846 declare simp_thms(6) [code nbe]
1849 Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"})
1852 lemma equals_alias_cert: "OFCLASS('a, eq_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> eq)" (is "?ofclass \<equiv> ?eq")
1854 assume "PROP ?ofclass"
1856 by (tactic {* ALLGOALS (rtac (Drule.unconstrainTs @{thm equals_eq})) *})
1857 (fact `PROP ?ofclass`)
1860 show "PROP ?ofclass" proof
1861 qed (simp add: `PROP ?eq`)
1865 Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>eq \<Rightarrow> 'a \<Rightarrow> bool"})
1869 Nbe.add_const_alias @{thm equals_alias_cert}
1872 hide (open) const eq
1877 lemma Let_case_cert:
1878 assumes "CASE \<equiv> (\<lambda>x. Let x f)"
1879 shows "CASE x \<equiv> f x"
1880 using assms by simp_all
1883 assumes "CASE \<equiv> (\<lambda>b. If b f g)"
1884 shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
1885 using assms by simp_all
1888 Code.add_case @{thm Let_case_cert}
1889 #> Code.add_case @{thm If_case_cert}
1890 #> Code.add_undefined @{const_name undefined}
1893 code_abort undefined
1895 subsubsection {* Generic code generator target languages *}
1897 text {* type bool *}
1905 code_const True and False and Not and "op &" and "op |" and If
1906 (SML "true" and "false" and "not"
1907 and infixl 1 "andalso" and infixl 0 "orelse"
1908 and "!(if (_)/ then (_)/ else (_))")
1909 (OCaml "true" and "false" and "not"
1910 and infixl 4 "&&" and infixl 2 "||"
1911 and "!(if (_)/ then (_)/ else (_))")
1912 (Haskell "True" and "False" and "not"
1913 and infixl 3 "&&" and infixl 2 "||"
1914 and "!(if (_)/ then (_)/ else (_))")
1915 (Scala "true" and "false" and "'! _"
1916 and infixl 3 "&&" and infixl 1 "||"
1917 and "!(if ((_))/ (_)/ else (_))")
1928 text {* using built-in Haskell equality *}
1933 code_const "eq_class.eq"
1934 (Haskell infixl 4 "==")
1937 (Haskell infixl 4 "==")
1939 text {* undefined *}
1941 code_const undefined
1942 (SML "!(raise/ Fail/ \"undefined\")")
1943 (OCaml "failwith/ \"undefined\"")
1944 (Haskell "error/ \"undefined\"")
1945 (Scala "!error(\"undefined\")")
1947 subsubsection {* Evaluation and normalization by evaluation *}
1950 Value.add_evaluator ("SML", Codegen.eval_term o ProofContext.theory_of)
1954 structure Eval_Method =
1957 val eval_ref : (unit -> bool) option Unsynchronized.ref = Unsynchronized.ref NONE;
1962 oracle eval_oracle = {* fn ct =>
1964 val thy = Thm.theory_of_cterm ct;
1965 val t = Thm.term_of ct;
1966 val dummy = @{cprop True};
1967 in case try HOLogic.dest_Trueprop t
1968 of SOME t' => if Code_Eval.eval NONE
1969 ("Eval_Method.eval_ref", Eval_Method.eval_ref) (K I) thy t' []
1970 then Thm.capply (Thm.capply @{cterm "op \<equiv> \<Colon> prop \<Rightarrow> prop \<Rightarrow> prop"} ct) dummy
1977 fun gen_eval_method conv ctxt = SIMPLE_METHOD'
1978 (CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
1982 method_setup eval = {* Scan.succeed (gen_eval_method eval_oracle) *}
1983 "solve goal by evaluation"
1985 method_setup evaluation = {* Scan.succeed (gen_eval_method Codegen.evaluation_conv) *}
1986 "solve goal by evaluation"
1988 method_setup normalization = {*
1989 Scan.succeed (K (SIMPLE_METHOD' (CONVERSION Nbe.norm_conv THEN' (fn k => TRY (rtac TrueI k)))))
1990 *} "solve goal by normalization"
1993 subsection {* Counterexample Search Units *}
1995 subsubsection {* Quickcheck *}
1997 quickcheck_params [size = 5, iterations = 50]
2000 subsubsection {* Nitpick setup *}
2002 text {* This will be relocated once Nitpick is moved to HOL. *}
2005 structure Nitpick_Defs = Named_Thms
2007 val name = "nitpick_def"
2008 val description = "alternative definitions of constants as needed by Nitpick"
2010 structure Nitpick_Simps = Named_Thms
2012 val name = "nitpick_simp"
2013 val description = "equational specification of constants as needed by Nitpick"
2015 structure Nitpick_Psimps = Named_Thms
2017 val name = "nitpick_psimp"
2018 val description = "partial equational specification of constants as needed by Nitpick"
2020 structure Nitpick_Intros = Named_Thms
2022 val name = "nitpick_intro"
2023 val description = "introduction rules for (co)inductive predicates as needed by Nitpick"
2029 #> Nitpick_Simps.setup
2030 #> Nitpick_Psimps.setup
2031 #> Nitpick_Intros.setup
2035 subsection {* Preprocessing for the predicate compiler *}
2038 structure Predicate_Compile_Alternative_Defs = Named_Thms
2040 val name = "code_pred_def"
2041 val description = "alternative definitions of constants for the Predicate Compiler"
2046 structure Predicate_Compile_Inline_Defs = Named_Thms
2048 val name = "code_pred_inline"
2049 val description = "inlining definitions for the Predicate Compiler"
2054 Predicate_Compile_Alternative_Defs.setup
2055 #> Predicate_Compile_Inline_Defs.setup
2059 subsection {* Legacy tactics and ML bindings *}
2062 fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
2064 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
2066 fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
2067 | wrong_prem (Bound _) = true
2068 | wrong_prem _ = false;
2069 val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
2071 fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
2072 fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
2075 val all_conj_distrib = thm "all_conj_distrib";
2076 val all_simps = thms "all_simps";
2077 val atomize_not = thm "atomize_not";
2078 val case_split = thm "case_split";
2079 val cases_simp = thm "cases_simp";
2080 val choice_eq = thm "choice_eq"
2081 val cong = thm "cong"
2082 val conj_comms = thms "conj_comms";
2083 val conj_cong = thm "conj_cong";
2084 val de_Morgan_conj = thm "de_Morgan_conj";
2085 val de_Morgan_disj = thm "de_Morgan_disj";
2086 val disj_assoc = thm "disj_assoc";
2087 val disj_comms = thms "disj_comms";
2088 val disj_cong = thm "disj_cong";
2089 val eq_ac = thms "eq_ac";
2090 val eq_cong2 = thm "eq_cong2"
2091 val Eq_FalseI = thm "Eq_FalseI";
2092 val Eq_TrueI = thm "Eq_TrueI";
2093 val Ex1_def = thm "Ex1_def"
2094 val ex_disj_distrib = thm "ex_disj_distrib";
2095 val ex_simps = thms "ex_simps";
2096 val if_cancel = thm "if_cancel";
2097 val if_eq_cancel = thm "if_eq_cancel";
2098 val if_False = thm "if_False";
2099 val iff_conv_conj_imp = thm "iff_conv_conj_imp";
2101 val if_splits = thms "if_splits";
2102 val if_True = thm "if_True";
2103 val if_weak_cong = thm "if_weak_cong"
2104 val imp_all = thm "imp_all";
2105 val imp_cong = thm "imp_cong";
2106 val imp_conjL = thm "imp_conjL";
2107 val imp_conjR = thm "imp_conjR";
2108 val imp_conv_disj = thm "imp_conv_disj";
2109 val simp_implies_def = thm "simp_implies_def";
2110 val simp_thms = thms "simp_thms";
2111 val split_if = thm "split_if";
2112 val the1_equality = thm "the1_equality"
2113 val theI = thm "theI"
2114 val theI' = thm "theI'"
2115 val True_implies_equals = thm "True_implies_equals";
2116 val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps simp_thms @ @{thms "nnf_simps"})