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/Tools/misc_legacy.ML"
19 "~~/src/Provers/hypsubst.ML"
20 "~~/src/Provers/splitter.ML"
21 "~~/src/Provers/classical.ML"
22 "~~/src/Provers/blast.ML"
23 "~~/src/Provers/clasimp.ML"
24 "~~/src/Tools/coherent.ML"
25 "~~/src/Tools/eqsubst.ML"
26 "~~/src/Provers/quantifier1.ML"
28 "~~/src/Tools/atomize_elim.ML"
29 "~~/src/Tools/induct.ML"
30 ("~~/src/Tools/induct_tacs.ML")
31 ("Tools/recfun_codegen.ML")
32 ("Tools/cnf_funcs.ML")
33 "~~/src/Tools/subtyping.ML"
36 setup {* Intuitionistic.method_setup @{binding iprover} *}
40 subsection {* Primitive logic *}
42 subsubsection {* Core syntax *}
46 setup {* Object_Logic.add_base_sort @{sort type} *}
49 "fun" :: (type, type) type
55 Trueprop :: "bool => prop" ("(_)" 5)
60 Not :: "bool => bool" ("~ _" [40] 40)
62 conj :: "[bool, bool] => bool" (infixr "&" 35)
63 disj :: "[bool, bool] => bool" (infixr "|" 30)
64 implies :: "[bool, bool] => bool" (infixr "-->" 25)
66 eq :: "['a, 'a] => bool" (infixl "=" 50)
68 The :: "('a => bool) => 'a"
69 All :: "('a => bool) => bool" (binder "ALL " 10)
70 Ex :: "('a => bool) => bool" (binder "EX " 10)
71 Ex1 :: "('a => bool) => bool" (binder "EX! " 10)
74 subsubsection {* Additional concrete syntax *}
80 not_equal :: "['a, 'a] => bool" (infixl "~=" 50) where
84 not_equal (infix "~=" 50)
87 Not ("\<not> _" [40] 40) and
88 conj (infixr "\<and>" 35) and
89 disj (infixr "\<or>" 30) and
90 implies (infixr "\<longrightarrow>" 25) and
91 not_equal (infix "\<noteq>" 50)
93 notation (HTML output)
94 Not ("\<not> _" [40] 40) and
95 conj (infixr "\<and>" 35) and
96 disj (infixr "\<or>" 30) and
97 not_equal (infix "\<noteq>" 50)
100 iff :: "[bool, bool] => bool" (infixr "<->" 25) where
104 iff (infixr "\<longleftrightarrow>" 25)
111 "_The" :: "[pttrn, bool] => 'a" ("(3THE _./ _)" [0, 10] 10)
113 "_bind" :: "[pttrn, 'a] => letbind" ("(2_ =/ _)" 10)
114 "" :: "letbind => letbinds" ("_")
115 "_binds" :: "[letbind, letbinds] => letbinds" ("_;/ _")
116 "_Let" :: "[letbinds, 'a] => 'a" ("(let (_)/ in (_))" [0, 10] 10)
118 "_case_syntax":: "['a, cases_syn] => 'b" ("(case _ of/ _)" 10)
119 "_case1" :: "['a, 'b] => case_syn" ("(2_ =>/ _)" 10)
120 "" :: "case_syn => cases_syn" ("_")
121 "_case2" :: "[case_syn, cases_syn] => cases_syn" ("_/ | _")
124 "THE x. P" == "CONST The (%x. P)"
127 [(@{const_syntax The}, fn [Abs abs] =>
128 let val (x, t) = atomic_abs_tr' abs
129 in Syntax.const @{syntax_const "_The"} $ x $ t end)]
130 *} -- {* To avoid eta-contraction of body *}
133 "_case1" :: "['a, 'b] => case_syn" ("(2_ \<Rightarrow>/ _)" 10)
136 All (binder "\<forall>" 10) and
137 Ex (binder "\<exists>" 10) and
138 Ex1 (binder "\<exists>!" 10)
140 notation (HTML output)
141 All (binder "\<forall>" 10) and
142 Ex (binder "\<exists>" 10) and
143 Ex1 (binder "\<exists>!" 10)
146 All (binder "! " 10) and
147 Ex (binder "? " 10) and
148 Ex1 (binder "?! " 10)
151 subsubsection {* Axioms and basic definitions *}
155 subst: "s = t \<Longrightarrow> P s \<Longrightarrow> P t"
156 ext: "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
157 -- {*Extensionality is built into the meta-logic, and this rule expresses
158 a related property. It is an eta-expanded version of the traditional
159 rule, and similar to the ABS rule of HOL*}
161 the_eq_trivial: "(THE x. x = a) = (a::'a)"
163 impI: "(P ==> Q) ==> P-->Q"
164 mp: "[| P-->Q; P |] ==> Q"
168 True_def: "True == ((%x::bool. x) = (%x. x))"
169 All_def: "All(P) == (P = (%x. True))"
170 Ex_def: "Ex(P) == !Q. (!x. P x --> Q) --> Q"
171 False_def: "False == (!P. P)"
172 not_def: "~ P == P-->False"
173 and_def: "P & Q == !R. (P-->Q-->R) --> R"
174 or_def: "P | Q == !R. (P-->R) --> (Q-->R) --> R"
175 Ex1_def: "Ex1(P) == ? x. P(x) & (! y. P(y) --> y=x)"
178 iff: "(P-->Q) --> (Q-->P) --> (P=Q)"
179 True_or_False: "(P=True) | (P=False)"
186 definition If :: "bool \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" ("(if (_)/ then (_)/ else (_))" [0, 0, 10] 10) where
187 "If P x y \<equiv> (THE z::'a. (P=True --> z=x) & (P=False --> z=y))"
189 definition Let :: "'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b" where
190 "Let s f \<equiv> f s"
193 "_Let (_binds b bs) e" == "_Let b (_Let bs e)"
194 "let x = a in e" == "CONST Let a (%x. e)"
203 subsection {* Fundamental rules *}
205 subsubsection {* Equality *}
207 lemma sym: "s = t ==> t = s"
208 by (erule subst) (rule refl)
210 lemma ssubst: "t = s ==> P s ==> P t"
211 by (drule sym) (erule subst)
213 lemma trans: "[| r=s; s=t |] ==> r=t"
216 lemma trans_sym [Pure.elim?]: "r = s ==> t = s ==> r = t"
217 by (rule trans [OF _ sym])
219 lemma meta_eq_to_obj_eq:
220 assumes meq: "A == B"
222 by (unfold meq) (rule refl)
224 text {* Useful with @{text erule} for proving equalities from known equalities. *}
228 lemma box_equals: "[| a=b; a=c; b=d |] ==> c=d"
235 text {* For calculational reasoning: *}
237 lemma forw_subst: "a = b ==> P b ==> P a"
240 lemma back_subst: "P a ==> a = b ==> P b"
244 subsubsection {* Congruence rules for application *}
246 text {* Similar to @{text AP_THM} in Gordon's HOL. *}
247 lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
252 text {* Similar to @{text AP_TERM} in Gordon's HOL and FOL's @{text subst_context}. *}
253 lemma arg_cong: "x=y ==> f(x)=f(y)"
258 lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
259 apply (erule ssubst)+
263 lemma cong: "[| f = g; (x::'a) = y |] ==> f x = g y"
268 ML {* val cong_tac = Cong_Tac.cong_tac @{thm cong} *}
271 subsubsection {* Equality of booleans -- iff *}
273 lemma iffI: assumes "P ==> Q" and "Q ==> P" shows "P=Q"
274 by (iprover intro: iff [THEN mp, THEN mp] impI assms)
276 lemma iffD2: "[| P=Q; Q |] ==> P"
279 lemma rev_iffD2: "[| Q; P=Q |] ==> P"
282 lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
283 by (drule sym) (rule iffD2)
285 lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
286 by (drule sym) (rule rev_iffD2)
290 and minor: "[| P --> Q; Q --> P |] ==> R"
292 by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
295 subsubsection {*True*}
298 unfolding True_def by (rule refl)
300 lemma eqTrueI: "P ==> P = True"
301 by (iprover intro: iffI TrueI)
303 lemma eqTrueE: "P = True ==> P"
304 by (erule iffD2) (rule TrueI)
307 subsubsection {*Universal quantifier*}
309 lemma allI: assumes "!!x::'a. P(x)" shows "ALL x. P(x)"
310 unfolding All_def by (iprover intro: ext eqTrueI assms)
312 lemma spec: "ALL x::'a. P(x) ==> P(x)"
313 apply (unfold All_def)
315 apply (erule fun_cong)
319 assumes major: "ALL x. P(x)"
320 and minor: "P(x) ==> R"
322 by (iprover intro: minor major [THEN spec])
325 assumes major: "ALL x. P(x)"
326 and minor: "[| P(x); ALL x. P(x) |] ==> R"
328 by (iprover intro: minor major major [THEN spec])
331 subsubsection {* False *}
334 Depends upon @{text spec}; it is impossible to do propositional
335 logic before quantifiers!
338 lemma FalseE: "False ==> P"
339 apply (unfold False_def)
343 lemma False_neq_True: "False = True ==> P"
344 by (erule eqTrueE [THEN FalseE])
347 subsubsection {* Negation *}
350 assumes "P ==> False"
352 apply (unfold not_def)
353 apply (iprover intro: impI assms)
356 lemma False_not_True: "False ~= True"
358 apply (erule False_neq_True)
361 lemma True_not_False: "True ~= False"
364 apply (erule False_neq_True)
367 lemma notE: "[| ~P; P |] ==> R"
368 apply (unfold not_def)
369 apply (erule mp [THEN FalseE])
373 lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
374 by (erule notE [THEN notI]) (erule meta_mp)
377 subsubsection {*Implication*}
380 assumes "P-->Q" "P" "Q ==> R"
382 by (iprover intro: assms mp)
384 (* Reduces Q to P-->Q, allowing substitution in P. *)
385 lemma rev_mp: "[| P; P --> Q |] ==> Q"
386 by (iprover intro: mp)
392 by (iprover intro: notI minor major [THEN notE])
394 (*not used at all, but we already have the other 3 combinations *)
397 and minor: "P ==> ~Q"
399 by (iprover intro: notI minor major notE)
401 lemma not_sym: "t ~= s ==> s ~= t"
402 by (erule contrapos_nn) (erule sym)
404 lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
405 by (erule subst, erule ssubst, assumption)
407 (*still used in HOLCF*)
409 assumes pq: "P ==> Q"
412 apply (rule nq [THEN contrapos_nn])
416 subsubsection {*Existential quantifier*}
418 lemma exI: "P x ==> EX x::'a. P x"
419 apply (unfold Ex_def)
420 apply (iprover intro: allI allE impI mp)
424 assumes major: "EX x::'a. P(x)"
425 and minor: "!!x. P(x) ==> Q"
427 apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
428 apply (iprover intro: impI [THEN allI] minor)
432 subsubsection {*Conjunction*}
434 lemma conjI: "[| P; Q |] ==> P&Q"
435 apply (unfold and_def)
436 apply (iprover intro: impI [THEN allI] mp)
439 lemma conjunct1: "[| P & Q |] ==> P"
440 apply (unfold and_def)
441 apply (iprover intro: impI dest: spec mp)
444 lemma conjunct2: "[| P & Q |] ==> Q"
445 apply (unfold and_def)
446 apply (iprover intro: impI dest: spec mp)
451 and minor: "[| P; Q |] ==> R"
454 apply (rule major [THEN conjunct1])
455 apply (rule major [THEN conjunct2])
459 assumes "P" "P ==> Q" shows "P & Q"
460 by (iprover intro: conjI assms)
463 subsubsection {*Disjunction*}
465 lemma disjI1: "P ==> P|Q"
466 apply (unfold or_def)
467 apply (iprover intro: allI impI mp)
470 lemma disjI2: "Q ==> P|Q"
471 apply (unfold or_def)
472 apply (iprover intro: allI impI mp)
477 and minorP: "P ==> R"
478 and minorQ: "Q ==> R"
480 by (iprover intro: minorP minorQ impI
481 major [unfolded or_def, THEN spec, THEN mp, THEN mp])
484 subsubsection {*Classical logic*}
487 assumes prem: "~P ==> P"
489 apply (rule True_or_False [THEN disjE, THEN eqTrueE])
491 apply (rule notI [THEN prem, THEN eqTrueI])
496 lemmas ccontr = FalseE [THEN classical, standard]
498 (*notE with premises exchanged; it discharges ~R so that it can be used to
499 make elimination rules*)
502 and premnot: "~R ==> ~P"
505 apply (erule notE [OF premnot premp])
508 (*Double negation law*)
509 lemma notnotD: "~~P ==> P"
510 apply (rule classical)
519 by (iprover intro: classical p1 p2 notE)
522 subsubsection {*Unique existence*}
525 assumes "P a" "!!x. P(x) ==> x=a"
527 by (unfold Ex1_def, iprover intro: assms exI conjI allI impI)
529 text{*Sometimes easier to use: the premises have no shared variables. Safe!*}
531 assumes ex_prem: "EX x. P(x)"
532 and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
534 by (iprover intro: ex_prem [THEN exE] ex1I eq)
537 assumes major: "EX! x. P(x)"
538 and minor: "!!x. [| P(x); ALL y. P(y) --> y=x |] ==> R"
540 apply (rule major [unfolded Ex1_def, THEN exE])
542 apply (iprover intro: minor)
545 lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
552 subsubsection {*THE: definite description operator*}
556 and premx: "!!x. P x ==> x=a"
557 shows "(THE x. P x) = a"
558 apply (rule trans [OF _ the_eq_trivial])
559 apply (rule_tac f = "The" in arg_cong)
563 apply (erule ssubst, rule prema)
567 assumes "P a" and "!!x. P x ==> x=a"
568 shows "P (THE x. P x)"
569 by (iprover intro: assms the_equality [THEN ssubst])
571 lemma theI': "EX! x. P x ==> P (THE x. P x)"
579 (*Easier to apply than theI: only one occurrence of P*)
581 assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
582 shows "Q (THE x. P x)"
583 by (iprover intro: assms theI)
585 lemma the1I2: assumes "EX! x. P x" "\<And>x. P x \<Longrightarrow> Q x" shows "Q (THE x. P x)"
586 by(iprover intro:assms(2) theI2[where P=P and Q=Q] ex1E[OF assms(1)]
589 lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
590 apply (rule the_equality)
593 apply (erule all_dupE)
602 lemma the_sym_eq_trivial: "(THE y. x=y) = x"
603 apply (rule the_equality)
609 subsubsection {*Classical intro rules for disjunction and existential quantifiers*}
612 assumes "~Q ==> P" shows "P|Q"
613 apply (rule classical)
614 apply (iprover intro: assms disjI1 disjI2 notI elim: notE)
617 lemma excluded_middle: "~P | P"
618 by (iprover intro: disjCI)
621 case distinction as a natural deduction rule.
622 Note that @{term "~P"} is the second case, not the first
624 lemma case_split [case_names True False]:
625 assumes prem1: "P ==> Q"
626 and prem2: "~P ==> Q"
628 apply (rule excluded_middle [THEN disjE])
633 (*Classical implies (-->) elimination. *)
635 assumes major: "P-->Q"
636 and minor: "~P ==> R" "Q ==> R"
638 apply (rule excluded_middle [of P, THEN disjE])
639 apply (iprover intro: minor major [THEN mp])+
642 (*This version of --> elimination works on Q before P. It works best for
643 those cases in which P holds "almost everywhere". Can't install as
644 default: would break old proofs.*)
646 assumes major: "P-->Q"
647 and minor: "Q ==> R" "~P ==> R"
649 apply (rule excluded_middle [of P, THEN disjE])
650 apply (iprover intro: minor major [THEN mp])+
653 (*Classical <-> elimination. *)
656 and minor: "[| P; Q |] ==> R" "[| ~P; ~Q |] ==> R"
658 apply (rule major [THEN iffE])
659 apply (iprover intro: minor elim: impCE notE)
663 assumes "ALL x. ~P(x) ==> P(a)"
666 apply (iprover intro: assms exI allI notI notE [of "\<exists>x. P x"])
670 subsubsection {* Intuitionistic Reasoning *}
675 and 3: "P --> Q ==> P"
678 from 3 and 1 have P .
679 with 1 have Q by (rule impE)
684 assumes 1: "ALL x. P x"
685 and 2: "P x ==> ALL x. P x ==> Q"
688 from 1 have "P x" by (rule spec)
689 from this and 1 show Q by (rule 2)
697 from 2 and 1 have P .
698 with 1 show R by (rule notE)
701 lemma TrueE: "True ==> P ==> P" .
702 lemma notFalseE: "~ False ==> P ==> P" .
704 lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
705 and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
706 and [Pure.elim 2] = allE notE' impE'
707 and [Pure.intro] = exI disjI2 disjI1
709 lemmas [trans] = trans
710 and [sym] = sym not_sym
711 and [Pure.elim?] = iffD1 iffD2 impE
713 use "Tools/hologic.ML"
716 subsubsection {* Atomizing meta-level connectives *}
719 eq_reflection: "x = y \<Longrightarrow> x \<equiv> y" (*admissible axiom*)
721 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
724 then show "ALL x. P x" ..
727 then show "!!x. P x" by (rule allE)
730 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
733 show "A --> B" by (rule impI) (rule r)
735 assume "A --> B" and A
736 then show B by (rule mp)
739 lemma atomize_not: "(A ==> False) == Trueprop (~A)"
741 assume r: "A ==> False"
742 show "~A" by (rule notI) (rule r)
745 then show False by (rule notE)
748 lemma atomize_eq [atomize, code]: "(x == y) == Trueprop (x = y)"
751 show "x = y" by (unfold `x == y`) (rule refl)
754 then show "x == y" by (rule eq_reflection)
757 lemma atomize_conj [atomize]: "(A &&& B) == Trueprop (A & B)"
759 assume conj: "A &&& B"
762 from conj show A by (rule conjunctionD1)
763 from conj show B by (rule conjunctionD2)
774 lemmas [symmetric, rulify] = atomize_all atomize_imp
775 and [symmetric, defn] = atomize_all atomize_imp atomize_eq
778 subsubsection {* Atomizing elimination rules *}
780 setup AtomizeElim.setup
782 lemma atomize_exL[atomize_elim]: "(!!x. P x ==> Q) == ((EX x. P x) ==> Q)"
785 lemma atomize_conjL[atomize_elim]: "(A ==> B ==> C) == (A & B ==> C)"
788 lemma atomize_disjL[atomize_elim]: "((A ==> C) ==> (B ==> C) ==> C) == ((A | B ==> C) ==> C)"
791 lemma atomize_elimL[atomize_elim]: "(!!B. (A ==> B) ==> B) == Trueprop A" ..
794 subsection {* Package setup *}
796 subsubsection {* Sledgehammer setup *}
799 Theorems blacklisted to Sledgehammer. These theorems typically produce clauses
800 that are prolific (match too many equality or membership literals) and relate to
801 seldom-used facts. Some duplicate other rules.
805 structure No_ATPs = Named_Thms
808 val description = "theorems that should be filtered out by Sledgehammer"
812 setup {* No_ATPs.setup *}
815 subsubsection {* Classical Reasoner setup *}
817 lemma imp_elim: "P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R"
818 by (rule classical) iprover
820 lemma swap: "~ P ==> (~ R ==> P) ==> R"
821 by (rule classical) iprover
824 "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
827 structure Hypsubst = HypsubstFun(
829 structure Simplifier = Simplifier
830 val dest_eq = HOLogic.dest_eq
831 val dest_Trueprop = HOLogic.dest_Trueprop
832 val dest_imp = HOLogic.dest_imp
833 val eq_reflection = @{thm eq_reflection}
834 val rev_eq_reflection = @{thm meta_eq_to_obj_eq}
835 val imp_intr = @{thm impI}
836 val rev_mp = @{thm rev_mp}
837 val subst = @{thm subst}
839 val thin_refl = @{thm thin_refl};
843 structure Classical = ClassicalFun(
845 val imp_elim = @{thm imp_elim}
846 val not_elim = @{thm notE}
847 val swap = @{thm swap}
848 val classical = @{thm classical}
849 val sizef = Drule.size_of_thm
850 val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
853 structure Basic_Classical: BASIC_CLASSICAL = Classical;
854 open Basic_Classical;
856 ML_Antiquote.value "claset"
857 (Scan.succeed "Classical.claset_of (ML_Context.the_local_context ())");
860 setup Classical.setup
864 fun non_bool_eq (@{const_name HOL.eq}, Type (_, [T, _])) = T <> @{typ bool}
865 | non_bool_eq _ = false;
867 SUBGOAL (fn (goal, i) =>
868 if Term.exists_Const non_bool_eq goal
869 then Hypsubst.hyp_subst_tac i
872 Hypsubst.hypsubst_setup
873 (*prevent substitution on bool*)
874 #> Context_Rules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac)
878 declare iffI [intro!]
886 declare iffCE [elim!]
892 declare ex_ex1I [intro!]
894 and the_equality [intro]
900 ML {* val HOL_cs = @{claset} *}
902 lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
904 apply (erule (1) meta_mp)
907 declare ex_ex1I [rule del, intro! 2]
910 lemmas [intro?] = ext
911 and [elim?] = ex1_implies_ex
913 (*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
914 lemma alt_ex1E [elim!]:
915 assumes major: "\<exists>!x. P x"
916 and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
918 apply (rule ex1E [OF major])
920 apply (tactic {* ares_tac @{thms allI} 1 *})+
921 apply (tactic {* etac (Classical.dup_elim @{thm allE}) 1 *})
926 structure Blast = Blast
929 type claset = Classical.claset
930 val equality_name = @{const_name HOL.eq}
931 val not_name = @{const_name Not}
932 val notE = @{thm notE}
933 val ccontr = @{thm ccontr}
934 val contr_tac = Classical.contr_tac
935 val dup_intr = Classical.dup_intr
936 val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
937 val rep_cs = Classical.rep_cs
938 val cla_modifiers = Classical.cla_modifiers
939 val cla_meth' = Classical.cla_meth'
941 val blast_tac = Blast.blast_tac;
947 subsubsection {* Simplifier *}
949 lemma eta_contract_eq: "(%s. f s) = f" ..
952 shows not_not: "(~ ~ P) = P"
953 and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
955 "(P ~= Q) = (P = (~Q))"
956 "(P | ~P) = True" "(~P | P) = True"
958 and not_True_eq_False [code]: "(\<not> True) = False"
959 and not_False_eq_True [code]: "(\<not> False) = True"
961 "(~P) ~= P" "P ~= (~P)"
963 and eq_True: "(P = True) = P"
964 and "(False=P) = (~P)"
965 and eq_False: "(P = False) = (\<not> P)"
967 "(True --> P) = P" "(False --> P) = True"
968 "(P --> True) = True" "(P --> P) = True"
969 "(P --> False) = (~P)" "(P --> ~P) = (~P)"
970 "(P & True) = P" "(True & P) = P"
971 "(P & False) = False" "(False & P) = False"
972 "(P & P) = P" "(P & (P & Q)) = (P & Q)"
973 "(P & ~P) = False" "(~P & P) = False"
974 "(P | True) = True" "(True | P) = True"
975 "(P | False) = P" "(False | P) = P"
976 "(P | P) = P" "(P | (P | Q)) = (P | Q)" and
977 "(ALL x. P) = P" "(EX x. P) = P" "EX x. x=t" "EX x. t=x"
979 "!!P. (EX x. x=t & P(x)) = P(t)"
980 "!!P. (EX x. t=x & P(x)) = P(t)"
981 "!!P. (ALL x. x=t --> P(x)) = P(t)"
982 "!!P. (ALL x. t=x --> P(x)) = P(t)"
983 by (blast, blast, blast, blast, blast, iprover+)
985 lemma disj_absorb: "(A | A) = A"
988 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
991 lemma conj_absorb: "(A & A) = A"
994 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
998 shows eq_commute: "(a=b) = (b=a)"
999 and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
1000 and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
1001 lemma neq_commute: "(a~=b) = (b~=a)" by iprover
1004 shows conj_commute: "(P&Q) = (Q&P)"
1005 and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
1006 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
1008 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
1011 shows disj_commute: "(P|Q) = (Q|P)"
1012 and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
1013 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
1015 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
1017 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
1018 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
1020 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
1021 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
1023 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
1024 lemma imp_conjL: "((P&Q) -->R) = (P --> (Q --> R))" by iprover
1025 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
1027 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
1028 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
1029 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
1031 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
1032 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
1034 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
1037 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
1038 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
1039 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
1040 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
1041 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
1042 lemma disj_not2: "(P | ~Q) = (Q --> P)" -- {* changes orientation :-( *}
1044 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
1046 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
1049 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
1050 -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
1051 -- {* cases boil down to the same thing. *}
1054 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
1055 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
1056 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
1057 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
1058 lemma all_not_ex: "(ALL x. P x) = (~ (EX x. ~ P x ))" by blast
1060 declare All_def [no_atp]
1062 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
1063 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
1066 \medskip The @{text "&"} congruence rule: not included by default!
1067 May slow rewrite proofs down by as much as 50\% *}
1070 "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
1073 lemma rev_conj_cong:
1074 "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
1077 text {* The @{text "|"} congruence rule: not included by default! *}
1080 "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
1084 text {* \medskip if-then-else rules *}
1086 lemma if_True [code]: "(if True then x else y) = x"
1087 by (unfold If_def) blast
1089 lemma if_False [code]: "(if False then x else y) = y"
1090 by (unfold If_def) blast
1092 lemma if_P: "P ==> (if P then x else y) = x"
1093 by (unfold If_def) blast
1095 lemma if_not_P: "~P ==> (if P then x else y) = y"
1096 by (unfold If_def) blast
1098 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
1099 apply (rule case_split [of Q])
1100 apply (simplesubst if_P)
1101 prefer 3 apply (simplesubst if_not_P, blast+)
1104 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
1105 by (simplesubst split_if, blast)
1107 lemmas if_splits [no_atp] = split_if split_if_asm
1109 lemma if_cancel: "(if c then x else x) = x"
1110 by (simplesubst split_if, blast)
1112 lemma if_eq_cancel: "(if x = y then y else x) = x"
1113 by (simplesubst split_if, blast)
1115 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
1116 -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
1119 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
1120 -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
1121 apply (simplesubst split_if, blast)
1124 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
1125 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
1127 text {* \medskip let rules for simproc *}
1129 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow> Let x f \<equiv> Let x g"
1132 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow> Let x f \<equiv> g"
1136 The following copy of the implication operator is useful for
1137 fine-tuning congruence rules. It instructs the simplifier to simplify
1141 definition simp_implies :: "[prop, prop] => prop" (infixr "=simp=>" 1) where
1142 "simp_implies \<equiv> op ==>"
1144 lemma simp_impliesI:
1145 assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
1146 shows "PROP P =simp=> PROP Q"
1147 apply (unfold simp_implies_def)
1152 lemma simp_impliesE:
1153 assumes PQ: "PROP P =simp=> PROP Q"
1155 and QR: "PROP Q \<Longrightarrow> PROP R"
1158 apply (rule PQ [unfolded simp_implies_def])
1162 lemma simp_implies_cong:
1163 assumes PP' :"PROP P == PROP P'"
1164 and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
1165 shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
1166 proof (unfold simp_implies_def, rule equal_intr_rule)
1167 assume PQ: "PROP P \<Longrightarrow> PROP Q"
1169 from PP' [symmetric] and P' have "PROP P"
1170 by (rule equal_elim_rule1)
1171 then have "PROP Q" by (rule PQ)
1172 with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
1174 assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
1176 from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
1177 then have "PROP Q'" by (rule P'Q')
1178 with P'QQ' [OF P', symmetric] show "PROP Q"
1179 by (rule equal_elim_rule1)
1183 assumes "P \<longrightarrow> Q \<longrightarrow> R"
1184 shows "P \<and> Q \<longrightarrow> R"
1185 using assms by blast
1188 assumes "\<And>x. P x = Q x"
1189 shows "(\<forall>x. P x) = (\<forall>x. Q x)"
1190 using assms by blast
1193 assumes "\<And>x. P x = Q x"
1194 shows "(\<exists>x. P x) = (\<exists>x. Q x)"
1195 using assms by blast
1198 "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
1202 "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
1205 use "Tools/simpdata.ML"
1206 ML {* open Simpdata *}
1209 Simplifier.method_setup Splitter.split_modifiers
1210 #> Simplifier.map_simpset (K Simpdata.simpset_simprocs)
1216 text {* Simproc for proving @{text "(y = x) == False"} from premise @{text "~(x = y)"}: *}
1218 simproc_setup neq ("x = y") = {* fn _ =>
1220 val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
1221 fun is_neq eq lhs rhs thm =
1222 (case Thm.prop_of thm of
1223 _ $ (Not $ (eq' $ l' $ r')) =>
1224 Not = HOLogic.Not andalso eq' = eq andalso
1225 r' aconv lhs andalso l' aconv rhs
1228 (case Thm.term_of ct of
1230 (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of_ss ss) of
1231 SOME thm => SOME (thm RS neq_to_EQ_False)
1237 simproc_setup let_simp ("Let x f") = {*
1239 val (f_Let_unfold, x_Let_unfold) =
1240 let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_unfold}
1241 in (cterm_of @{theory} f, cterm_of @{theory} x) end
1242 val (f_Let_folded, x_Let_folded) =
1243 let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_folded}
1244 in (cterm_of @{theory} f, cterm_of @{theory} x) end;
1246 let val [(_ $ _ $ (g $ _))] = prems_of @{thm Let_folded}
1247 in cterm_of @{theory} g end;
1248 fun count_loose (Bound i) k = if i >= k then 1 else 0
1249 | count_loose (s $ t) k = count_loose s k + count_loose t k
1250 | count_loose (Abs (_, _, t)) k = count_loose t (k + 1)
1251 | count_loose _ _ = 0;
1252 fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
1254 of Abs (_, _, t') => count_loose t' 0 <= 1
1256 in fn _ => fn ss => fn ct => if is_trivial_let (Thm.term_of ct)
1257 then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
1258 else let (*Norbert Schirmer's case*)
1259 val ctxt = Simplifier.the_context ss;
1260 val thy = ProofContext.theory_of ctxt;
1261 val t = Thm.term_of ct;
1262 val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
1263 in Option.map (hd o Variable.export ctxt' ctxt o single)
1264 (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
1265 if is_Free x orelse is_Bound x orelse is_Const x
1266 then SOME @{thm Let_def}
1269 val n = case f of (Abs (x, _, _)) => x | _ => "x";
1270 val cx = cterm_of thy x;
1271 val {T = xT, ...} = rep_cterm cx;
1272 val cf = cterm_of thy f;
1273 val fx_g = Simplifier.rewrite ss (Thm.capply cf cx);
1274 val (_ $ _ $ g) = prop_of fx_g;
1275 val g' = abstract_over (x,g);
1280 cterm_instantiate [(f_Let_unfold, cf), (x_Let_unfold, cx)] @{thm Let_unfold};
1281 in SOME (rl OF [fx_g]) end
1282 else if Term.betapply (f, x) aconv g then NONE (*avoid identity conversion*)
1284 val abs_g'= Abs (n,xT,g');
1286 val g_g'x = Thm.symmetric (Thm.beta_conversion false (cterm_of thy g'x));
1287 val rl = cterm_instantiate
1288 [(f_Let_folded, cterm_of thy f), (x_Let_folded, cx),
1289 (g_Let_folded, cterm_of thy abs_g')]
1291 in SOME (rl OF [Thm.transitive fx_g g_g'x])
1298 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
1300 assume "True \<Longrightarrow> PROP P"
1301 from this [OF TrueI] show "PROP P" .
1304 then show "PROP P" .
1308 "!!P Q. (EX x. P x & Q) = ((EX x. P x) & Q)"
1309 "!!P Q. (EX x. P & Q x) = (P & (EX x. Q x))"
1310 "!!P Q. (EX x. P x | Q) = ((EX x. P x) | Q)"
1311 "!!P Q. (EX x. P | Q x) = (P | (EX x. Q x))"
1312 "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
1313 "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
1314 -- {* Miniscoping: pushing in existential quantifiers. *}
1315 by (iprover | blast)+
1318 "!!P Q. (ALL x. P x & Q) = ((ALL x. P x) & Q)"
1319 "!!P Q. (ALL x. P & Q x) = (P & (ALL x. Q x))"
1320 "!!P Q. (ALL x. P x | Q) = ((ALL x. P x) | Q)"
1321 "!!P Q. (ALL x. P | Q x) = (P | (ALL x. Q x))"
1322 "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
1323 "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
1324 -- {* Miniscoping: pushing in universal quantifiers. *}
1325 by (iprover | blast)+
1328 triv_forall_equality (*prunes params*)
1329 True_implies_equals (*prune asms `True'*)
1335 (*In general it seems wrong to add distributive laws by default: they
1336 might cause exponential blow-up. But imp_disjL has been in for a while
1337 and cannot be removed without affecting existing proofs. Moreover,
1338 rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
1339 grounds that it allows simplification of R in the two cases.*)
1357 lemmas [cong] = imp_cong simp_implies_cong
1358 lemmas [split] = split_if
1360 ML {* val HOL_ss = @{simpset} *}
1362 text {* Simplifies x assuming c and y assuming ~c *}
1365 and "c \<Longrightarrow> x = u"
1366 and "\<not> c \<Longrightarrow> y = v"
1367 shows "(if b then x else y) = (if c then u else v)"
1370 text {* Prevents simplification of x and y:
1371 faster and allows the execution of functional programs. *}
1372 lemma if_weak_cong [cong]:
1374 shows "(if b then x else y) = (if c then x else y)"
1375 using assms by (rule arg_cong)
1377 text {* Prevents simplification of t: much faster *}
1378 lemma let_weak_cong:
1380 shows "(let x = a in t x) = (let x = b in t x)"
1381 using assms by (rule arg_cong)
1383 text {* To tidy up the result of a simproc. Only the RHS will be simplified. *}
1386 shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
1390 "f (if c then x else y) = (if c then f x else f y)"
1394 subsubsection {* Generic cases and induction *}
1396 text {* Rule projections: *}
1399 structure Project_Rule = Project_Rule
1401 val conjunct1 = @{thm conjunct1}
1402 val conjunct2 = @{thm conjunct2}
1407 definition induct_forall where
1408 "induct_forall P == \<forall>x. P x"
1410 definition induct_implies where
1411 "induct_implies A B == A \<longrightarrow> B"
1413 definition induct_equal where
1414 "induct_equal x y == x = y"
1416 definition induct_conj where
1417 "induct_conj A B == A \<and> B"
1419 definition induct_true where
1420 "induct_true == True"
1422 definition induct_false where
1423 "induct_false == False"
1425 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
1426 by (unfold atomize_all induct_forall_def)
1428 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
1429 by (unfold atomize_imp induct_implies_def)
1431 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
1432 by (unfold atomize_eq induct_equal_def)
1434 lemma induct_conj_eq: "(A &&& B) == Trueprop (induct_conj A B)"
1435 by (unfold atomize_conj induct_conj_def)
1437 lemmas induct_atomize' = induct_forall_eq induct_implies_eq induct_conj_eq
1438 lemmas induct_atomize = induct_atomize' induct_equal_eq
1439 lemmas induct_rulify' [symmetric, standard] = induct_atomize'
1440 lemmas induct_rulify [symmetric, standard] = induct_atomize
1441 lemmas induct_rulify_fallback =
1442 induct_forall_def induct_implies_def induct_equal_def induct_conj_def
1443 induct_true_def induct_false_def
1446 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
1447 induct_conj (induct_forall A) (induct_forall B)"
1448 by (unfold induct_forall_def induct_conj_def) iprover
1450 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
1451 induct_conj (induct_implies C A) (induct_implies C B)"
1452 by (unfold induct_implies_def induct_conj_def) iprover
1454 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
1456 assume r: "induct_conj A B ==> PROP C" and A B
1457 show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
1459 assume r: "A ==> B ==> PROP C" and "induct_conj A B"
1460 show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
1463 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
1465 lemma induct_trueI: "induct_true"
1466 by (simp add: induct_true_def)
1468 text {* Method setup. *}
1471 structure Induct = Induct
1473 val cases_default = @{thm case_split}
1474 val atomize = @{thms induct_atomize}
1475 val rulify = @{thms induct_rulify'}
1476 val rulify_fallback = @{thms induct_rulify_fallback}
1477 val equal_def = @{thm induct_equal_def}
1478 fun dest_def (Const (@{const_name induct_equal}, _) $ t $ u) = SOME (t, u)
1480 val trivial_tac = match_tac @{thms induct_trueI}
1486 Context.theory_map (Induct.map_simpset (fn ss => ss
1487 setmksimps (fn ss => Simpdata.mksimps Simpdata.mksimps_pairs ss #>
1488 map (Simplifier.rewrite_rule (map Thm.symmetric
1489 @{thms induct_rulify_fallback})))
1491 [Simplifier.simproc_global @{theory} "swap_induct_false"
1492 ["induct_false ==> PROP P ==> PROP Q"]
1494 (fn _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
1495 if P <> Q then SOME Drule.swap_prems_eq else NONE
1497 Simplifier.simproc_global @{theory} "induct_equal_conj_curry"
1498 ["induct_conj P Q ==> PROP R"]
1500 (fn _ $ (_ $ P) $ _ =>
1502 fun is_conj (@{const induct_conj} $ P $ Q) =
1503 is_conj P andalso is_conj Q
1504 | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
1505 | is_conj @{const induct_true} = true
1506 | is_conj @{const induct_false} = true
1508 in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
1512 text {* Pre-simplification of induction and cases rules *}
1514 lemma [induct_simp]: "(!!x. induct_equal x t ==> PROP P x) == PROP P t"
1515 unfolding induct_equal_def
1517 assume R: "!!x. x = t ==> PROP P x"
1518 show "PROP P t" by (rule R [OF refl])
1520 fix x assume "PROP P t" "x = t"
1521 then show "PROP P x" by simp
1524 lemma [induct_simp]: "(!!x. induct_equal t x ==> PROP P x) == PROP P t"
1525 unfolding induct_equal_def
1527 assume R: "!!x. t = x ==> PROP P x"
1528 show "PROP P t" by (rule R [OF refl])
1530 fix x assume "PROP P t" "t = x"
1531 then show "PROP P x" by simp
1534 lemma [induct_simp]: "(induct_false ==> P) == Trueprop induct_true"
1535 unfolding induct_false_def induct_true_def
1536 by (iprover intro: equal_intr_rule)
1538 lemma [induct_simp]: "(induct_true ==> PROP P) == PROP P"
1539 unfolding induct_true_def
1541 assume R: "True \<Longrightarrow> PROP P"
1542 from TrueI show "PROP P" by (rule R)
1545 then show "PROP P" .
1548 lemma [induct_simp]: "(PROP P ==> induct_true) == Trueprop induct_true"
1549 unfolding induct_true_def
1550 by (iprover intro: equal_intr_rule)
1552 lemma [induct_simp]: "(!!x. induct_true) == Trueprop induct_true"
1553 unfolding induct_true_def
1554 by (iprover intro: equal_intr_rule)
1556 lemma [induct_simp]: "induct_implies induct_true P == P"
1557 by (simp add: induct_implies_def induct_true_def)
1559 lemma [induct_simp]: "(x = x) = True"
1562 hide_const induct_forall induct_implies induct_equal induct_conj induct_true induct_false
1564 use "~~/src/Tools/induct_tacs.ML"
1565 setup InductTacs.setup
1568 subsubsection {* Coherent logic *}
1571 structure Coherent = Coherent
1573 val atomize_elimL = @{thm atomize_elimL}
1574 val atomize_exL = @{thm atomize_exL}
1575 val atomize_conjL = @{thm atomize_conjL}
1576 val atomize_disjL = @{thm atomize_disjL}
1577 val operator_names =
1578 [@{const_name HOL.disj}, @{const_name HOL.conj}, @{const_name Ex}]
1582 setup Coherent.setup
1585 subsubsection {* Reorienting equalities *}
1588 signature REORIENT_PROC =
1590 val add : (term -> bool) -> theory -> theory
1591 val proc : morphism -> simpset -> cterm -> thm option
1594 structure Reorient_Proc : REORIENT_PROC =
1596 structure Data = Theory_Data
1598 type T = ((term -> bool) * stamp) list;
1601 fun merge data : T = Library.merge (eq_snd op =) data;
1603 fun add m = Data.map (cons (m, stamp ()));
1604 fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
1606 val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
1607 fun proc phi ss ct =
1609 val ctxt = Simplifier.the_context ss;
1610 val thy = ProofContext.theory_of ctxt;
1612 case Thm.term_of ct of
1613 (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
1620 subsection {* Other simple lemmas and lemma duplicates *}
1622 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
1625 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
1627 apply (rule_tac a = "%x. THE y. P x y" in ex1I)
1628 apply (fast dest!: theI')
1629 apply (fast intro: ext the1_equality [symmetric])
1634 apply (erule_tac x = "%z. if z = x then y else f z" in allE)
1637 apply (case_tac "xa = x")
1638 apply (drule_tac [3] x = x in fun_cong, simp_all)
1641 lemmas eq_sym_conv = eq_commute
1644 "(\<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)"
1645 "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))"
1646 "(\<not> \<not>(P)) = P"
1649 subsection {* Basic ML bindings *}
1652 val FalseE = @{thm FalseE}
1653 val Let_def = @{thm Let_def}
1654 val TrueI = @{thm TrueI}
1655 val allE = @{thm allE}
1656 val allI = @{thm allI}
1657 val all_dupE = @{thm all_dupE}
1658 val arg_cong = @{thm arg_cong}
1659 val box_equals = @{thm box_equals}
1660 val ccontr = @{thm ccontr}
1661 val classical = @{thm classical}
1662 val conjE = @{thm conjE}
1663 val conjI = @{thm conjI}
1664 val conjunct1 = @{thm conjunct1}
1665 val conjunct2 = @{thm conjunct2}
1666 val disjCI = @{thm disjCI}
1667 val disjE = @{thm disjE}
1668 val disjI1 = @{thm disjI1}
1669 val disjI2 = @{thm disjI2}
1670 val eq_reflection = @{thm eq_reflection}
1671 val ex1E = @{thm ex1E}
1672 val ex1I = @{thm ex1I}
1673 val ex1_implies_ex = @{thm ex1_implies_ex}
1674 val exE = @{thm exE}
1675 val exI = @{thm exI}
1676 val excluded_middle = @{thm excluded_middle}
1677 val ext = @{thm ext}
1678 val fun_cong = @{thm fun_cong}
1679 val iffD1 = @{thm iffD1}
1680 val iffD2 = @{thm iffD2}
1681 val iffI = @{thm iffI}
1682 val impE = @{thm impE}
1683 val impI = @{thm impI}
1684 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
1686 val notE = @{thm notE}
1687 val notI = @{thm notI}
1688 val not_all = @{thm not_all}
1689 val not_ex = @{thm not_ex}
1690 val not_iff = @{thm not_iff}
1691 val not_not = @{thm not_not}
1692 val not_sym = @{thm not_sym}
1693 val refl = @{thm refl}
1694 val rev_mp = @{thm rev_mp}
1695 val spec = @{thm spec}
1696 val ssubst = @{thm ssubst}
1697 val subst = @{thm subst}
1698 val sym = @{thm sym}
1699 val trans = @{thm trans}
1702 use "Tools/cnf_funcs.ML"
1704 subsection {* Code generator setup *}
1706 subsubsection {* SML code generator setup *}
1708 use "Tools/recfun_codegen.ML"
1712 #> RecfunCodegen.setup
1713 #> Codegen.map_unfold (K HOL_basic_ss)
1719 fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
1723 let val b = one_of [false, true]
1724 in (b, fn () => term_of_bool b) end;
1728 fun term_of_prop b =
1729 HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
1737 HOL.disj ("(_ orelse/ _)")
1738 HOL.conj ("(_ andalso/ _)")
1739 "If" ("(if _/ then _/ else _)")
1744 fun eq_codegen thy defs dep thyname b t gr =
1745 (case strip_comb t of
1746 (Const (@{const_name HOL.eq}, Type (_, [Type ("fun", _), _])), _) => NONE
1747 | (Const (@{const_name HOL.eq}, _), [t, u]) =>
1749 val (pt, gr') = Codegen.invoke_codegen thy defs dep thyname false t gr;
1750 val (pu, gr'') = Codegen.invoke_codegen thy defs dep thyname false u gr';
1751 val (_, gr''') = Codegen.invoke_tycodegen thy defs dep thyname false HOLogic.boolT gr'';
1753 SOME (Codegen.parens
1754 (Pretty.block [pt, Codegen.str " =", Pretty.brk 1, pu]), gr''')
1756 | (t as Const (@{const_name HOL.eq}, _), ts) => SOME (Codegen.invoke_codegen
1757 thy defs dep thyname b (Codegen.eta_expand t ts 2) gr)
1761 Codegen.add_codegen "eq_codegen" eq_codegen
1765 subsubsection {* Generic code generator preprocessor setup *}
1768 Code_Preproc.map_pre (K HOL_basic_ss)
1769 #> Code_Preproc.map_post (K HOL_basic_ss)
1770 #> Code_Simp.map_ss (K HOL_basic_ss)
1773 subsubsection {* Equality *}
1776 fixes equal :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
1777 assumes equal_eq: "equal x y \<longleftrightarrow> x = y"
1780 lemma equal [code_unfold, code_inline del]: "equal = (op =)"
1781 by (rule ext equal_eq)+
1783 lemma equal_refl: "equal x x \<longleftrightarrow> True"
1784 unfolding equal by rule+
1786 lemma eq_equal: "(op =) \<equiv> equal"
1787 by (rule eq_reflection) (rule ext, rule ext, rule sym, rule equal_eq)
1791 declare eq_equal [symmetric, code_post]
1792 declare eq_equal [code]
1795 Code_Preproc.map_pre (fn simpset =>
1796 simpset addsimprocs [Simplifier.simproc_global_i @{theory} "equal" [@{term HOL.eq}]
1798 fn Const (_, Type ("fun", [Type _, _])) => SOME @{thm eq_equal} | _ => NONE)])
1802 subsubsection {* Generic code generator foundation *}
1804 text {* Datatype @{typ bool} *}
1806 code_datatype True False
1809 shows "False \<and> P \<longleftrightarrow> False"
1810 and "True \<and> P \<longleftrightarrow> P"
1811 and "P \<and> False \<longleftrightarrow> False"
1812 and "P \<and> True \<longleftrightarrow> P" by simp_all
1815 shows "False \<or> P \<longleftrightarrow> P"
1816 and "True \<or> P \<longleftrightarrow> True"
1817 and "P \<or> False \<longleftrightarrow> P"
1818 and "P \<or> True \<longleftrightarrow> True" by simp_all
1821 shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
1822 and "(True \<longrightarrow> P) \<longleftrightarrow> P"
1823 and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
1824 and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
1826 text {* More about @{typ prop} *}
1829 shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q"
1830 and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
1831 and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
1833 lemma Trueprop_code [code]:
1834 "Trueprop True \<equiv> Code_Generator.holds"
1835 by (auto intro!: equal_intr_rule holds)
1837 declare Trueprop_code [symmetric, code_post]
1841 declare simp_thms(6) [code nbe]
1843 instantiation itself :: (type) equal
1846 definition equal_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
1847 "equal_itself x y \<longleftrightarrow> x = y"
1850 qed (fact equal_itself_def)
1854 lemma equal_itself_code [code]:
1855 "equal TYPE('a) TYPE('a) \<longleftrightarrow> True"
1856 by (simp add: equal)
1859 Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"})
1862 lemma equal_alias_cert: "OFCLASS('a, equal_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> equal)" (is "?ofclass \<equiv> ?equal")
1864 assume "PROP ?ofclass"
1866 by (tactic {* ALLGOALS (rtac (Thm.unconstrainT @{thm eq_equal})) *})
1867 (fact `PROP ?ofclass`)
1869 assume "PROP ?equal"
1870 show "PROP ?ofclass" proof
1871 qed (simp add: `PROP ?equal`)
1875 Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>equal \<Rightarrow> 'a \<Rightarrow> bool"})
1879 Nbe.add_const_alias @{thm equal_alias_cert}
1884 lemma Let_case_cert:
1885 assumes "CASE \<equiv> (\<lambda>x. Let x f)"
1886 shows "CASE x \<equiv> f x"
1887 using assms by simp_all
1890 assumes "CASE \<equiv> (\<lambda>b. If b f g)"
1891 shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
1892 using assms by simp_all
1895 Code.add_case @{thm Let_case_cert}
1896 #> Code.add_case @{thm If_case_cert}
1897 #> Code.add_undefined @{const_name undefined}
1900 code_abort undefined
1903 subsubsection {* Generic code generator target languages *}
1905 text {* type @{typ bool} *}
1913 code_const True and False and Not and HOL.conj and HOL.disj and If
1914 (SML "true" and "false" and "not"
1915 and infixl 1 "andalso" and infixl 0 "orelse"
1916 and "!(if (_)/ then (_)/ else (_))")
1917 (OCaml "true" and "false" and "not"
1918 and infixl 3 "&&" and infixl 2 "||"
1919 and "!(if (_)/ then (_)/ else (_))")
1920 (Haskell "True" and "False" and "not"
1921 and infixl 3 "&&" and infixl 2 "||"
1922 and "!(if (_)/ then (_)/ else (_))")
1923 (Scala "true" and "false" and "'! _"
1924 and infixl 3 "&&" and infixl 1 "||"
1925 and "!(if ((_))/ (_)/ else (_))")
1936 code_modulename SML Pure HOL
1937 code_modulename OCaml Pure HOL
1938 code_modulename Haskell Pure HOL
1940 text {* using built-in Haskell equality *}
1945 code_const "HOL.equal"
1946 (Haskell infix 4 "==")
1949 (Haskell infix 4 "==")
1951 text {* undefined *}
1953 code_const undefined
1954 (SML "!(raise/ Fail/ \"undefined\")")
1955 (OCaml "failwith/ \"undefined\"")
1956 (Haskell "error/ \"undefined\"")
1957 (Scala "!error(\"undefined\")")
1959 subsubsection {* Evaluation and normalization by evaluation *}
1962 Value.add_evaluator ("SML", Codegen.eval_term o ProofContext.theory_of)
1966 fun gen_eval_method conv ctxt = SIMPLE_METHOD'
1967 (CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
1971 method_setup eval = {* Scan.succeed (gen_eval_method Code_Runtime.dynamic_holds_conv) *}
1972 "solve goal by evaluation"
1974 method_setup evaluation = {* Scan.succeed (gen_eval_method Codegen.evaluation_conv) *}
1975 "solve goal by evaluation"
1977 method_setup normalization = {*
1978 Scan.succeed (K (SIMPLE_METHOD'
1979 (CHANGED_PROP o (CONVERSION Nbe.dynamic_eval_conv THEN' (fn k => TRY (rtac TrueI k))))))
1980 *} "solve goal by normalization"
1983 subsection {* Counterexample Search Units *}
1985 subsubsection {* Quickcheck *}
1987 quickcheck_params [size = 5, iterations = 50]
1990 subsubsection {* Nitpick setup *}
1993 structure Nitpick_Defs = Named_Thms
1995 val name = "nitpick_def"
1996 val description = "alternative definitions of constants as needed by Nitpick"
1998 structure Nitpick_Simps = Named_Thms
2000 val name = "nitpick_simp"
2001 val description = "equational specification of constants as needed by Nitpick"
2003 structure Nitpick_Psimps = Named_Thms
2005 val name = "nitpick_psimp"
2006 val description = "partial equational specification of constants as needed by Nitpick"
2008 structure Nitpick_Choice_Specs = Named_Thms
2010 val name = "nitpick_choice_spec"
2011 val description = "choice specification of constants as needed by Nitpick"
2017 #> Nitpick_Simps.setup
2018 #> Nitpick_Psimps.setup
2019 #> Nitpick_Choice_Specs.setup
2023 subsection {* Preprocessing for the predicate compiler *}
2026 structure Predicate_Compile_Alternative_Defs = Named_Thms
2028 val name = "code_pred_def"
2029 val description = "alternative definitions of constants for the Predicate Compiler"
2031 structure Predicate_Compile_Inline_Defs = Named_Thms
2033 val name = "code_pred_inline"
2034 val description = "inlining definitions for the Predicate Compiler"
2036 structure Predicate_Compile_Simps = Named_Thms
2038 val name = "code_pred_simp"
2039 val description = "simplification rules for the optimisations in the Predicate Compiler"
2044 Predicate_Compile_Alternative_Defs.setup
2045 #> Predicate_Compile_Inline_Defs.setup
2046 #> Predicate_Compile_Simps.setup
2050 subsection {* Legacy tactics and ML bindings *}
2053 fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
2055 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
2057 fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
2058 | wrong_prem (Bound _) = true
2059 | wrong_prem _ = false;
2060 val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
2062 fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
2063 fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
2066 val all_conj_distrib = @{thm all_conj_distrib};
2067 val all_simps = @{thms all_simps};
2068 val atomize_not = @{thm atomize_not};
2069 val case_split = @{thm case_split};
2070 val cases_simp = @{thm cases_simp};
2071 val choice_eq = @{thm choice_eq};
2072 val cong = @{thm cong};
2073 val conj_comms = @{thms conj_comms};
2074 val conj_cong = @{thm conj_cong};
2075 val de_Morgan_conj = @{thm de_Morgan_conj};
2076 val de_Morgan_disj = @{thm de_Morgan_disj};
2077 val disj_assoc = @{thm disj_assoc};
2078 val disj_comms = @{thms disj_comms};
2079 val disj_cong = @{thm disj_cong};
2080 val eq_ac = @{thms eq_ac};
2081 val eq_cong2 = @{thm eq_cong2}
2082 val Eq_FalseI = @{thm Eq_FalseI};
2083 val Eq_TrueI = @{thm Eq_TrueI};
2084 val Ex1_def = @{thm Ex1_def};
2085 val ex_disj_distrib = @{thm ex_disj_distrib};
2086 val ex_simps = @{thms ex_simps};
2087 val if_cancel = @{thm if_cancel};
2088 val if_eq_cancel = @{thm if_eq_cancel};
2089 val if_False = @{thm if_False};
2090 val iff_conv_conj_imp = @{thm iff_conv_conj_imp};
2091 val iff = @{thm iff};
2092 val if_splits = @{thms if_splits};
2093 val if_True = @{thm if_True};
2094 val if_weak_cong = @{thm if_weak_cong};
2095 val imp_all = @{thm imp_all};
2096 val imp_cong = @{thm imp_cong};
2097 val imp_conjL = @{thm imp_conjL};
2098 val imp_conjR = @{thm imp_conjR};
2099 val imp_conv_disj = @{thm imp_conv_disj};
2100 val simp_implies_def = @{thm simp_implies_def};
2101 val simp_thms = @{thms simp_thms};
2102 val split_if = @{thm split_if};
2103 val the1_equality = @{thm the1_equality};
2104 val theI = @{thm theI};
2105 val theI' = @{thm theI'};
2106 val True_implies_equals = @{thm True_implies_equals};
2107 val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps simp_thms @ @{thms "nnf_simps"})
2111 hide_const (open) eq equal