src/HOL/HOL.thy
author hoelzl
Fri Oct 24 15:07:49 2014 +0200 (2014-10-24)
changeset 58775 9cd64a66a765
parent 58659 6c9821c32dd5
child 58826 2ed2eaabe3df
permissions -rw-r--r--
move NO_MATCH simproc from the AFP entry Graph_Theory to HOL
     1 (*  Title:      HOL/HOL.thy
     2     Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
     3 *)
     4 
     5 header {* The basis of Higher-Order Logic *}
     6 
     7 theory HOL
     8 imports Pure "~~/src/Tools/Code_Generator"
     9 keywords
    10   "try" "solve_direct" "quickcheck" "print_coercions" "print_claset"
    11     "print_induct_rules" :: diag and
    12   "quickcheck_params" :: thy_decl
    13 begin
    14 
    15 ML_file "~~/src/Tools/misc_legacy.ML"
    16 ML_file "~~/src/Tools/try.ML"
    17 ML_file "~~/src/Tools/quickcheck.ML"
    18 ML_file "~~/src/Tools/solve_direct.ML"
    19 ML_file "~~/src/Tools/IsaPlanner/zipper.ML"
    20 ML_file "~~/src/Tools/IsaPlanner/isand.ML"
    21 ML_file "~~/src/Tools/IsaPlanner/rw_inst.ML"
    22 ML_file "~~/src/Provers/hypsubst.ML"
    23 ML_file "~~/src/Provers/splitter.ML"
    24 ML_file "~~/src/Provers/classical.ML"
    25 ML_file "~~/src/Provers/blast.ML"
    26 ML_file "~~/src/Provers/clasimp.ML"
    27 ML_file "~~/src/Tools/eqsubst.ML"
    28 ML_file "~~/src/Provers/quantifier1.ML"
    29 ML_file "~~/src/Tools/atomize_elim.ML"
    30 ML_file "~~/src/Tools/induct.ML"
    31 ML_file "~~/src/Tools/cong_tac.ML"
    32 ML_file "~~/src/Tools/intuitionistic.ML"
    33 ML_file "~~/src/Tools/project_rule.ML"
    34 ML_file "~~/src/Tools/subtyping.ML"
    35 ML_file "~~/src/Tools/case_product.ML"
    36 
    37 setup {*
    38   Intuitionistic.method_setup @{binding iprover}
    39   #> Subtyping.setup
    40   #> Case_Product.setup
    41 *}
    42 
    43 ML \<open>Plugin_Name.declare_setup @{binding extraction}\<close>
    44 
    45 ML \<open>
    46   Plugin_Name.declare_setup @{binding quickcheck_random};
    47   Plugin_Name.declare_setup @{binding quickcheck_exhaustive};
    48   Plugin_Name.declare_setup @{binding quickcheck_bounded_forall};
    49   Plugin_Name.declare_setup @{binding quickcheck_full_exhaustive};
    50   Plugin_Name.declare_setup @{binding quickcheck_narrowing};
    51 \<close>
    52 ML \<open>
    53   Plugin_Name.define_setup @{binding quickcheck}
    54    [@{plugin quickcheck_exhaustive},
    55     @{plugin quickcheck_random},
    56     @{plugin quickcheck_bounded_forall},
    57     @{plugin quickcheck_full_exhaustive},
    58     @{plugin quickcheck_narrowing}]
    59 \<close>
    60 
    61 
    62 subsection {* Primitive logic *}
    63 
    64 subsubsection {* Core syntax *}
    65 
    66 setup {* Axclass.class_axiomatization (@{binding type}, []) *}
    67 default_sort type
    68 setup {* Object_Logic.add_base_sort @{sort type} *}
    69 
    70 axiomatization where fun_arity: "OFCLASS('a \<Rightarrow> 'b, type_class)"
    71 instance "fun" :: (type, type) type by (rule fun_arity)
    72 
    73 axiomatization where itself_arity: "OFCLASS('a itself, type_class)"
    74 instance itself :: (type) type by (rule itself_arity)
    75 
    76 typedecl bool
    77 
    78 judgment
    79   Trueprop      :: "bool => prop"                   ("(_)" 5)
    80 
    81 axiomatization
    82   implies       :: "[bool, bool] => bool"           (infixr "-->" 25)  and
    83   eq            :: "['a, 'a] => bool"               (infixl "=" 50)  and
    84   The           :: "('a => bool) => 'a"
    85 
    86 consts
    87   True          :: bool
    88   False         :: bool
    89   Not           :: "bool => bool"                   ("~ _" [40] 40)
    90 
    91   conj          :: "[bool, bool] => bool"           (infixr "&" 35)
    92   disj          :: "[bool, bool] => bool"           (infixr "|" 30)
    93 
    94   All           :: "('a => bool) => bool"           (binder "ALL " 10)
    95   Ex            :: "('a => bool) => bool"           (binder "EX " 10)
    96   Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
    97 
    98 
    99 subsubsection {* Additional concrete syntax *}
   100 
   101 notation (output)
   102   eq  (infix "=" 50)
   103 
   104 abbreviation
   105   not_equal :: "['a, 'a] => bool"  (infixl "~=" 50) where
   106   "x ~= y == ~ (x = y)"
   107 
   108 notation (output)
   109   not_equal  (infix "~=" 50)
   110 
   111 notation (xsymbols)
   112   Not  ("\<not> _" [40] 40) and
   113   conj  (infixr "\<and>" 35) and
   114   disj  (infixr "\<or>" 30) and
   115   implies  (infixr "\<longrightarrow>" 25) and
   116   not_equal  (infixl "\<noteq>" 50)
   117 
   118 notation (xsymbols output)
   119   not_equal  (infix "\<noteq>" 50)
   120 
   121 notation (HTML output)
   122   Not  ("\<not> _" [40] 40) and
   123   conj  (infixr "\<and>" 35) and
   124   disj  (infixr "\<or>" 30) and
   125   not_equal  (infix "\<noteq>" 50)
   126 
   127 abbreviation (iff)
   128   iff :: "[bool, bool] => bool"  (infixr "<->" 25) where
   129   "A <-> B == A = B"
   130 
   131 notation (xsymbols)
   132   iff  (infixr "\<longleftrightarrow>" 25)
   133 
   134 syntax "_The" :: "[pttrn, bool] => 'a"  ("(3THE _./ _)" [0, 10] 10)
   135 translations "THE x. P" == "CONST The (%x. P)"
   136 print_translation {*
   137   [(@{const_syntax The}, fn _ => fn [Abs abs] =>
   138       let val (x, t) = Syntax_Trans.atomic_abs_tr' abs
   139       in Syntax.const @{syntax_const "_The"} $ x $ t end)]
   140 *}  -- {* To avoid eta-contraction of body *}
   141 
   142 nonterminal letbinds and letbind
   143 syntax
   144   "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
   145   ""            :: "letbind => letbinds"                 ("_")
   146   "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
   147   "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" [0, 10] 10)
   148 
   149 nonterminal case_syn and cases_syn
   150 syntax
   151   "_case_syntax" :: "['a, cases_syn] => 'b"  ("(case _ of/ _)" 10)
   152   "_case1" :: "['a, 'b] => case_syn"  ("(2_ =>/ _)" 10)
   153   "" :: "case_syn => cases_syn"  ("_")
   154   "_case2" :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
   155 syntax (xsymbols)
   156   "_case1" :: "['a, 'b] => case_syn"  ("(2_ \<Rightarrow>/ _)" 10)
   157 
   158 notation (xsymbols)
   159   All  (binder "\<forall>" 10) and
   160   Ex  (binder "\<exists>" 10) and
   161   Ex1  (binder "\<exists>!" 10)
   162 
   163 notation (HTML output)
   164   All  (binder "\<forall>" 10) and
   165   Ex  (binder "\<exists>" 10) and
   166   Ex1  (binder "\<exists>!" 10)
   167 
   168 notation (HOL)
   169   All  (binder "! " 10) and
   170   Ex  (binder "? " 10) and
   171   Ex1  (binder "?! " 10)
   172 
   173 
   174 subsubsection {* Axioms and basic definitions *}
   175 
   176 axiomatization where
   177   refl: "t = (t::'a)" and
   178   subst: "s = t \<Longrightarrow> P s \<Longrightarrow> P t" and
   179   ext: "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
   180     -- {*Extensionality is built into the meta-logic, and this rule expresses
   181          a related property.  It is an eta-expanded version of the traditional
   182          rule, and similar to the ABS rule of HOL*} and
   183 
   184   the_eq_trivial: "(THE x. x = a) = (a::'a)"
   185 
   186 axiomatization where
   187   impI: "(P ==> Q) ==> P-->Q" and
   188   mp: "[| P-->Q;  P |] ==> Q" and
   189 
   190   iff: "(P-->Q) --> (Q-->P) --> (P=Q)" and
   191   True_or_False: "(P=True) | (P=False)"
   192 
   193 defs
   194   True_def:     "True      == ((%x::bool. x) = (%x. x))"
   195   All_def:      "All(P)    == (P = (%x. True))"
   196   Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
   197   False_def:    "False     == (!P. P)"
   198   not_def:      "~ P       == P-->False"
   199   and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
   200   or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
   201   Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
   202 
   203 definition If :: "bool \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" ("(if (_)/ then (_)/ else (_))" [0, 0, 10] 10)
   204   where "If P x y \<equiv> (THE z::'a. (P=True --> z=x) & (P=False --> z=y))"
   205 
   206 definition Let :: "'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b"
   207   where "Let s f \<equiv> f s"
   208 
   209 translations
   210   "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
   211   "let x = a in e"        == "CONST Let a (%x. e)"
   212 
   213 axiomatization undefined :: 'a
   214 
   215 class default = fixes default :: 'a
   216 
   217 
   218 subsection {* Fundamental rules *}
   219 
   220 subsubsection {* Equality *}
   221 
   222 lemma sym: "s = t ==> t = s"
   223   by (erule subst) (rule refl)
   224 
   225 lemma ssubst: "t = s ==> P s ==> P t"
   226   by (drule sym) (erule subst)
   227 
   228 lemma trans: "[| r=s; s=t |] ==> r=t"
   229   by (erule subst)
   230 
   231 lemma trans_sym [Pure.elim?]: "r = s ==> t = s ==> r = t"
   232   by (rule trans [OF _ sym])
   233 
   234 lemma meta_eq_to_obj_eq: 
   235   assumes meq: "A == B"
   236   shows "A = B"
   237   by (unfold meq) (rule refl)
   238 
   239 text {* Useful with @{text erule} for proving equalities from known equalities. *}
   240      (* a = b
   241         |   |
   242         c = d   *)
   243 lemma box_equals: "[| a=b;  a=c;  b=d |] ==> c=d"
   244 apply (rule trans)
   245 apply (rule trans)
   246 apply (rule sym)
   247 apply assumption+
   248 done
   249 
   250 text {* For calculational reasoning: *}
   251 
   252 lemma forw_subst: "a = b ==> P b ==> P a"
   253   by (rule ssubst)
   254 
   255 lemma back_subst: "P a ==> a = b ==> P b"
   256   by (rule subst)
   257 
   258 
   259 subsubsection {* Congruence rules for application *}
   260 
   261 text {* Similar to @{text AP_THM} in Gordon's HOL. *}
   262 lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
   263 apply (erule subst)
   264 apply (rule refl)
   265 done
   266 
   267 text {* Similar to @{text AP_TERM} in Gordon's HOL and FOL's @{text subst_context}. *}
   268 lemma arg_cong: "x=y ==> f(x)=f(y)"
   269 apply (erule subst)
   270 apply (rule refl)
   271 done
   272 
   273 lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
   274 apply (erule ssubst)+
   275 apply (rule refl)
   276 done
   277 
   278 lemma cong: "[| f = g; (x::'a) = y |] ==> f x = g y"
   279 apply (erule subst)+
   280 apply (rule refl)
   281 done
   282 
   283 ML {* val cong_tac = Cong_Tac.cong_tac @{thm cong} *}
   284 
   285 
   286 subsubsection {* Equality of booleans -- iff *}
   287 
   288 lemma iffI: assumes "P ==> Q" and "Q ==> P" shows "P=Q"
   289   by (iprover intro: iff [THEN mp, THEN mp] impI assms)
   290 
   291 lemma iffD2: "[| P=Q; Q |] ==> P"
   292   by (erule ssubst)
   293 
   294 lemma rev_iffD2: "[| Q; P=Q |] ==> P"
   295   by (erule iffD2)
   296 
   297 lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
   298   by (drule sym) (rule iffD2)
   299 
   300 lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
   301   by (drule sym) (rule rev_iffD2)
   302 
   303 lemma iffE:
   304   assumes major: "P=Q"
   305     and minor: "[| P --> Q; Q --> P |] ==> R"
   306   shows R
   307   by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
   308 
   309 
   310 subsubsection {*True*}
   311 
   312 lemma TrueI: "True"
   313   unfolding True_def by (rule refl)
   314 
   315 lemma eqTrueI: "P ==> P = True"
   316   by (iprover intro: iffI TrueI)
   317 
   318 lemma eqTrueE: "P = True ==> P"
   319   by (erule iffD2) (rule TrueI)
   320 
   321 
   322 subsubsection {*Universal quantifier*}
   323 
   324 lemma allI: assumes "!!x::'a. P(x)" shows "ALL x. P(x)"
   325   unfolding All_def by (iprover intro: ext eqTrueI assms)
   326 
   327 lemma spec: "ALL x::'a. P(x) ==> P(x)"
   328 apply (unfold All_def)
   329 apply (rule eqTrueE)
   330 apply (erule fun_cong)
   331 done
   332 
   333 lemma allE:
   334   assumes major: "ALL x. P(x)"
   335     and minor: "P(x) ==> R"
   336   shows R
   337   by (iprover intro: minor major [THEN spec])
   338 
   339 lemma all_dupE:
   340   assumes major: "ALL x. P(x)"
   341     and minor: "[| P(x); ALL x. P(x) |] ==> R"
   342   shows R
   343   by (iprover intro: minor major major [THEN spec])
   344 
   345 
   346 subsubsection {* False *}
   347 
   348 text {*
   349   Depends upon @{text spec}; it is impossible to do propositional
   350   logic before quantifiers!
   351 *}
   352 
   353 lemma FalseE: "False ==> P"
   354   apply (unfold False_def)
   355   apply (erule spec)
   356   done
   357 
   358 lemma False_neq_True: "False = True ==> P"
   359   by (erule eqTrueE [THEN FalseE])
   360 
   361 
   362 subsubsection {* Negation *}
   363 
   364 lemma notI:
   365   assumes "P ==> False"
   366   shows "~P"
   367   apply (unfold not_def)
   368   apply (iprover intro: impI assms)
   369   done
   370 
   371 lemma False_not_True: "False ~= True"
   372   apply (rule notI)
   373   apply (erule False_neq_True)
   374   done
   375 
   376 lemma True_not_False: "True ~= False"
   377   apply (rule notI)
   378   apply (drule sym)
   379   apply (erule False_neq_True)
   380   done
   381 
   382 lemma notE: "[| ~P;  P |] ==> R"
   383   apply (unfold not_def)
   384   apply (erule mp [THEN FalseE])
   385   apply assumption
   386   done
   387 
   388 lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
   389   by (erule notE [THEN notI]) (erule meta_mp)
   390 
   391 
   392 subsubsection {*Implication*}
   393 
   394 lemma impE:
   395   assumes "P-->Q" "P" "Q ==> R"
   396   shows "R"
   397 by (iprover intro: assms mp)
   398 
   399 (* Reduces Q to P-->Q, allowing substitution in P. *)
   400 lemma rev_mp: "[| P;  P --> Q |] ==> Q"
   401 by (iprover intro: mp)
   402 
   403 lemma contrapos_nn:
   404   assumes major: "~Q"
   405       and minor: "P==>Q"
   406   shows "~P"
   407 by (iprover intro: notI minor major [THEN notE])
   408 
   409 (*not used at all, but we already have the other 3 combinations *)
   410 lemma contrapos_pn:
   411   assumes major: "Q"
   412       and minor: "P ==> ~Q"
   413   shows "~P"
   414 by (iprover intro: notI minor major notE)
   415 
   416 lemma not_sym: "t ~= s ==> s ~= t"
   417   by (erule contrapos_nn) (erule sym)
   418 
   419 lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
   420   by (erule subst, erule ssubst, assumption)
   421 
   422 
   423 subsubsection {*Existential quantifier*}
   424 
   425 lemma exI: "P x ==> EX x::'a. P x"
   426 apply (unfold Ex_def)
   427 apply (iprover intro: allI allE impI mp)
   428 done
   429 
   430 lemma exE:
   431   assumes major: "EX x::'a. P(x)"
   432       and minor: "!!x. P(x) ==> Q"
   433   shows "Q"
   434 apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
   435 apply (iprover intro: impI [THEN allI] minor)
   436 done
   437 
   438 
   439 subsubsection {*Conjunction*}
   440 
   441 lemma conjI: "[| P; Q |] ==> P&Q"
   442 apply (unfold and_def)
   443 apply (iprover intro: impI [THEN allI] mp)
   444 done
   445 
   446 lemma conjunct1: "[| P & Q |] ==> P"
   447 apply (unfold and_def)
   448 apply (iprover intro: impI dest: spec mp)
   449 done
   450 
   451 lemma conjunct2: "[| P & Q |] ==> Q"
   452 apply (unfold and_def)
   453 apply (iprover intro: impI dest: spec mp)
   454 done
   455 
   456 lemma conjE:
   457   assumes major: "P&Q"
   458       and minor: "[| P; Q |] ==> R"
   459   shows "R"
   460 apply (rule minor)
   461 apply (rule major [THEN conjunct1])
   462 apply (rule major [THEN conjunct2])
   463 done
   464 
   465 lemma context_conjI:
   466   assumes "P" "P ==> Q" shows "P & Q"
   467 by (iprover intro: conjI assms)
   468 
   469 
   470 subsubsection {*Disjunction*}
   471 
   472 lemma disjI1: "P ==> P|Q"
   473 apply (unfold or_def)
   474 apply (iprover intro: allI impI mp)
   475 done
   476 
   477 lemma disjI2: "Q ==> P|Q"
   478 apply (unfold or_def)
   479 apply (iprover intro: allI impI mp)
   480 done
   481 
   482 lemma disjE:
   483   assumes major: "P|Q"
   484       and minorP: "P ==> R"
   485       and minorQ: "Q ==> R"
   486   shows "R"
   487 by (iprover intro: minorP minorQ impI
   488                  major [unfolded or_def, THEN spec, THEN mp, THEN mp])
   489 
   490 
   491 subsubsection {*Classical logic*}
   492 
   493 lemma classical:
   494   assumes prem: "~P ==> P"
   495   shows "P"
   496 apply (rule True_or_False [THEN disjE, THEN eqTrueE])
   497 apply assumption
   498 apply (rule notI [THEN prem, THEN eqTrueI])
   499 apply (erule subst)
   500 apply assumption
   501 done
   502 
   503 lemmas ccontr = FalseE [THEN classical]
   504 
   505 (*notE with premises exchanged; it discharges ~R so that it can be used to
   506   make elimination rules*)
   507 lemma rev_notE:
   508   assumes premp: "P"
   509       and premnot: "~R ==> ~P"
   510   shows "R"
   511 apply (rule ccontr)
   512 apply (erule notE [OF premnot premp])
   513 done
   514 
   515 (*Double negation law*)
   516 lemma notnotD: "~~P ==> P"
   517 apply (rule classical)
   518 apply (erule notE)
   519 apply assumption
   520 done
   521 
   522 lemma contrapos_pp:
   523   assumes p1: "Q"
   524       and p2: "~P ==> ~Q"
   525   shows "P"
   526 by (iprover intro: classical p1 p2 notE)
   527 
   528 
   529 subsubsection {*Unique existence*}
   530 
   531 lemma ex1I:
   532   assumes "P a" "!!x. P(x) ==> x=a"
   533   shows "EX! x. P(x)"
   534 by (unfold Ex1_def, iprover intro: assms exI conjI allI impI)
   535 
   536 text{*Sometimes easier to use: the premises have no shared variables.  Safe!*}
   537 lemma ex_ex1I:
   538   assumes ex_prem: "EX x. P(x)"
   539       and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
   540   shows "EX! x. P(x)"
   541 by (iprover intro: ex_prem [THEN exE] ex1I eq)
   542 
   543 lemma ex1E:
   544   assumes major: "EX! x. P(x)"
   545       and minor: "!!x. [| P(x);  ALL y. P(y) --> y=x |] ==> R"
   546   shows "R"
   547 apply (rule major [unfolded Ex1_def, THEN exE])
   548 apply (erule conjE)
   549 apply (iprover intro: minor)
   550 done
   551 
   552 lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
   553 apply (erule ex1E)
   554 apply (rule exI)
   555 apply assumption
   556 done
   557 
   558 
   559 subsubsection {*THE: definite description operator*}
   560 
   561 lemma the_equality:
   562   assumes prema: "P a"
   563       and premx: "!!x. P x ==> x=a"
   564   shows "(THE x. P x) = a"
   565 apply (rule trans [OF _ the_eq_trivial])
   566 apply (rule_tac f = "The" in arg_cong)
   567 apply (rule ext)
   568 apply (rule iffI)
   569  apply (erule premx)
   570 apply (erule ssubst, rule prema)
   571 done
   572 
   573 lemma theI:
   574   assumes "P a" and "!!x. P x ==> x=a"
   575   shows "P (THE x. P x)"
   576 by (iprover intro: assms the_equality [THEN ssubst])
   577 
   578 lemma theI': "EX! x. P x ==> P (THE x. P x)"
   579 apply (erule ex1E)
   580 apply (erule theI)
   581 apply (erule allE)
   582 apply (erule mp)
   583 apply assumption
   584 done
   585 
   586 (*Easier to apply than theI: only one occurrence of P*)
   587 lemma theI2:
   588   assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
   589   shows "Q (THE x. P x)"
   590 by (iprover intro: assms theI)
   591 
   592 lemma the1I2: assumes "EX! x. P x" "\<And>x. P x \<Longrightarrow> Q x" shows "Q (THE x. P x)"
   593 by(iprover intro:assms(2) theI2[where P=P and Q=Q] ex1E[OF assms(1)]
   594            elim:allE impE)
   595 
   596 lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
   597 apply (rule the_equality)
   598 apply  assumption
   599 apply (erule ex1E)
   600 apply (erule all_dupE)
   601 apply (drule mp)
   602 apply  assumption
   603 apply (erule ssubst)
   604 apply (erule allE)
   605 apply (erule mp)
   606 apply assumption
   607 done
   608 
   609 lemma the_sym_eq_trivial: "(THE y. x=y) = x"
   610 apply (rule the_equality)
   611 apply (rule refl)
   612 apply (erule sym)
   613 done
   614 
   615 
   616 subsubsection {*Classical intro rules for disjunction and existential quantifiers*}
   617 
   618 lemma disjCI:
   619   assumes "~Q ==> P" shows "P|Q"
   620 apply (rule classical)
   621 apply (iprover intro: assms disjI1 disjI2 notI elim: notE)
   622 done
   623 
   624 lemma excluded_middle: "~P | P"
   625 by (iprover intro: disjCI)
   626 
   627 text {*
   628   case distinction as a natural deduction rule.
   629   Note that @{term "~P"} is the second case, not the first
   630 *}
   631 lemma case_split [case_names True False]:
   632   assumes prem1: "P ==> Q"
   633       and prem2: "~P ==> Q"
   634   shows "Q"
   635 apply (rule excluded_middle [THEN disjE])
   636 apply (erule prem2)
   637 apply (erule prem1)
   638 done
   639 
   640 (*Classical implies (-->) elimination. *)
   641 lemma impCE:
   642   assumes major: "P-->Q"
   643       and minor: "~P ==> R" "Q ==> R"
   644   shows "R"
   645 apply (rule excluded_middle [of P, THEN disjE])
   646 apply (iprover intro: minor major [THEN mp])+
   647 done
   648 
   649 (*This version of --> elimination works on Q before P.  It works best for
   650   those cases in which P holds "almost everywhere".  Can't install as
   651   default: would break old proofs.*)
   652 lemma impCE':
   653   assumes major: "P-->Q"
   654       and minor: "Q ==> R" "~P ==> R"
   655   shows "R"
   656 apply (rule excluded_middle [of P, THEN disjE])
   657 apply (iprover intro: minor major [THEN mp])+
   658 done
   659 
   660 (*Classical <-> elimination. *)
   661 lemma iffCE:
   662   assumes major: "P=Q"
   663       and minor: "[| P; Q |] ==> R"  "[| ~P; ~Q |] ==> R"
   664   shows "R"
   665 apply (rule major [THEN iffE])
   666 apply (iprover intro: minor elim: impCE notE)
   667 done
   668 
   669 lemma exCI:
   670   assumes "ALL x. ~P(x) ==> P(a)"
   671   shows "EX x. P(x)"
   672 apply (rule ccontr)
   673 apply (iprover intro: assms exI allI notI notE [of "\<exists>x. P x"])
   674 done
   675 
   676 
   677 subsubsection {* Intuitionistic Reasoning *}
   678 
   679 lemma impE':
   680   assumes 1: "P --> Q"
   681     and 2: "Q ==> R"
   682     and 3: "P --> Q ==> P"
   683   shows R
   684 proof -
   685   from 3 and 1 have P .
   686   with 1 have Q by (rule impE)
   687   with 2 show R .
   688 qed
   689 
   690 lemma allE':
   691   assumes 1: "ALL x. P x"
   692     and 2: "P x ==> ALL x. P x ==> Q"
   693   shows Q
   694 proof -
   695   from 1 have "P x" by (rule spec)
   696   from this and 1 show Q by (rule 2)
   697 qed
   698 
   699 lemma notE':
   700   assumes 1: "~ P"
   701     and 2: "~ P ==> P"
   702   shows R
   703 proof -
   704   from 2 and 1 have P .
   705   with 1 show R by (rule notE)
   706 qed
   707 
   708 lemma TrueE: "True ==> P ==> P" .
   709 lemma notFalseE: "~ False ==> P ==> P" .
   710 
   711 lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
   712   and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
   713   and [Pure.elim 2] = allE notE' impE'
   714   and [Pure.intro] = exI disjI2 disjI1
   715 
   716 lemmas [trans] = trans
   717   and [sym] = sym not_sym
   718   and [Pure.elim?] = iffD1 iffD2 impE
   719 
   720 
   721 subsubsection {* Atomizing meta-level connectives *}
   722 
   723 axiomatization where
   724   eq_reflection: "x = y \<Longrightarrow> x \<equiv> y" (*admissible axiom*)
   725 
   726 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
   727 proof
   728   assume "!!x. P x"
   729   then show "ALL x. P x" ..
   730 next
   731   assume "ALL x. P x"
   732   then show "!!x. P x" by (rule allE)
   733 qed
   734 
   735 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
   736 proof
   737   assume r: "A ==> B"
   738   show "A --> B" by (rule impI) (rule r)
   739 next
   740   assume "A --> B" and A
   741   then show B by (rule mp)
   742 qed
   743 
   744 lemma atomize_not: "(A ==> False) == Trueprop (~A)"
   745 proof
   746   assume r: "A ==> False"
   747   show "~A" by (rule notI) (rule r)
   748 next
   749   assume "~A" and A
   750   then show False by (rule notE)
   751 qed
   752 
   753 lemma atomize_eq [atomize, code]: "(x == y) == Trueprop (x = y)"
   754 proof
   755   assume "x == y"
   756   show "x = y" by (unfold `x == y`) (rule refl)
   757 next
   758   assume "x = y"
   759   then show "x == y" by (rule eq_reflection)
   760 qed
   761 
   762 lemma atomize_conj [atomize]: "(A &&& B) == Trueprop (A & B)"
   763 proof
   764   assume conj: "A &&& B"
   765   show "A & B"
   766   proof (rule conjI)
   767     from conj show A by (rule conjunctionD1)
   768     from conj show B by (rule conjunctionD2)
   769   qed
   770 next
   771   assume conj: "A & B"
   772   show "A &&& B"
   773   proof -
   774     from conj show A ..
   775     from conj show B ..
   776   qed
   777 qed
   778 
   779 lemmas [symmetric, rulify] = atomize_all atomize_imp
   780   and [symmetric, defn] = atomize_all atomize_imp atomize_eq
   781 
   782 
   783 subsubsection {* Atomizing elimination rules *}
   784 
   785 lemma atomize_exL[atomize_elim]: "(!!x. P x ==> Q) == ((EX x. P x) ==> Q)"
   786   by rule iprover+
   787 
   788 lemma atomize_conjL[atomize_elim]: "(A ==> B ==> C) == (A & B ==> C)"
   789   by rule iprover+
   790 
   791 lemma atomize_disjL[atomize_elim]: "((A ==> C) ==> (B ==> C) ==> C) == ((A | B ==> C) ==> C)"
   792   by rule iprover+
   793 
   794 lemma atomize_elimL[atomize_elim]: "(!!B. (A ==> B) ==> B) == Trueprop A" ..
   795 
   796 
   797 subsection {* Package setup *}
   798 
   799 ML_file "Tools/hologic.ML"
   800 
   801 
   802 subsubsection {* Sledgehammer setup *}
   803 
   804 text {*
   805 Theorems blacklisted to Sledgehammer. These theorems typically produce clauses
   806 that are prolific (match too many equality or membership literals) and relate to
   807 seldom-used facts. Some duplicate other rules.
   808 *}
   809 
   810 named_theorems no_atp "theorems that should be filtered out by Sledgehammer"
   811 
   812 
   813 subsubsection {* Classical Reasoner setup *}
   814 
   815 lemma imp_elim: "P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R"
   816   by (rule classical) iprover
   817 
   818 lemma swap: "~ P ==> (~ R ==> P) ==> R"
   819   by (rule classical) iprover
   820 
   821 lemma thin_refl:
   822   "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
   823 
   824 ML {*
   825 structure Hypsubst = Hypsubst
   826 (
   827   val dest_eq = HOLogic.dest_eq
   828   val dest_Trueprop = HOLogic.dest_Trueprop
   829   val dest_imp = HOLogic.dest_imp
   830   val eq_reflection = @{thm eq_reflection}
   831   val rev_eq_reflection = @{thm meta_eq_to_obj_eq}
   832   val imp_intr = @{thm impI}
   833   val rev_mp = @{thm rev_mp}
   834   val subst = @{thm subst}
   835   val sym = @{thm sym}
   836   val thin_refl = @{thm thin_refl};
   837 );
   838 open Hypsubst;
   839 
   840 structure Classical = Classical
   841 (
   842   val imp_elim = @{thm imp_elim}
   843   val not_elim = @{thm notE}
   844   val swap = @{thm swap}
   845   val classical = @{thm classical}
   846   val sizef = Drule.size_of_thm
   847   val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
   848 );
   849 
   850 structure Basic_Classical: BASIC_CLASSICAL = Classical; 
   851 open Basic_Classical;
   852 *}
   853 
   854 setup Classical.setup
   855 
   856 setup {*
   857 let
   858   fun non_bool_eq (@{const_name HOL.eq}, Type (_, [T, _])) = T <> @{typ bool}
   859     | non_bool_eq _ = false;
   860   fun hyp_subst_tac' ctxt =
   861     SUBGOAL (fn (goal, i) =>
   862       if Term.exists_Const non_bool_eq goal
   863       then Hypsubst.hyp_subst_tac ctxt i
   864       else no_tac);
   865 in
   866   Hypsubst.hypsubst_setup
   867   (*prevent substitution on bool*)
   868   #> Context_Rules.addSWrapper (fn ctxt => fn tac => hyp_subst_tac' ctxt ORELSE' tac)
   869 end
   870 *}
   871 
   872 declare iffI [intro!]
   873   and notI [intro!]
   874   and impI [intro!]
   875   and disjCI [intro!]
   876   and conjI [intro!]
   877   and TrueI [intro!]
   878   and refl [intro!]
   879 
   880 declare iffCE [elim!]
   881   and FalseE [elim!]
   882   and impCE [elim!]
   883   and disjE [elim!]
   884   and conjE [elim!]
   885 
   886 declare ex_ex1I [intro!]
   887   and allI [intro!]
   888   and the_equality [intro]
   889   and exI [intro]
   890 
   891 declare exE [elim!]
   892   allE [elim]
   893 
   894 ML {* val HOL_cs = claset_of @{context} *}
   895 
   896 lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
   897   apply (erule swap)
   898   apply (erule (1) meta_mp)
   899   done
   900 
   901 declare ex_ex1I [rule del, intro! 2]
   902   and ex1I [intro]
   903 
   904 declare ext [intro]
   905 
   906 lemmas [intro?] = ext
   907   and [elim?] = ex1_implies_ex
   908 
   909 (*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
   910 lemma alt_ex1E [elim!]:
   911   assumes major: "\<exists>!x. P x"
   912       and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
   913   shows R
   914 apply (rule ex1E [OF major])
   915 apply (rule prem)
   916 apply (tactic {* ares_tac @{thms allI} 1 *})+
   917 apply (tactic {* etac (Classical.dup_elim @{thm allE}) 1 *})
   918 apply iprover
   919 done
   920 
   921 ML {*
   922   structure Blast = Blast
   923   (
   924     structure Classical = Classical
   925     val Trueprop_const = dest_Const @{const Trueprop}
   926     val equality_name = @{const_name HOL.eq}
   927     val not_name = @{const_name Not}
   928     val notE = @{thm notE}
   929     val ccontr = @{thm ccontr}
   930     val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
   931   );
   932   val blast_tac = Blast.blast_tac;
   933 *}
   934 
   935 setup Blast.setup
   936 
   937 
   938 subsubsection {* Simplifier *}
   939 
   940 lemma eta_contract_eq: "(%s. f s) = f" ..
   941 
   942 lemma simp_thms:
   943   shows not_not: "(~ ~ P) = P"
   944   and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
   945   and
   946     "(P ~= Q) = (P = (~Q))"
   947     "(P | ~P) = True"    "(~P | P) = True"
   948     "(x = x) = True"
   949   and not_True_eq_False [code]: "(\<not> True) = False"
   950   and not_False_eq_True [code]: "(\<not> False) = True"
   951   and
   952     "(~P) ~= P"  "P ~= (~P)"
   953     "(True=P) = P"
   954   and eq_True: "(P = True) = P"
   955   and "(False=P) = (~P)"
   956   and eq_False: "(P = False) = (\<not> P)"
   957   and
   958     "(True --> P) = P"  "(False --> P) = True"
   959     "(P --> True) = True"  "(P --> P) = True"
   960     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
   961     "(P & True) = P"  "(True & P) = P"
   962     "(P & False) = False"  "(False & P) = False"
   963     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
   964     "(P & ~P) = False"    "(~P & P) = False"
   965     "(P | True) = True"  "(True | P) = True"
   966     "(P | False) = P"  "(False | P) = P"
   967     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
   968     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
   969   and
   970     "!!P. (EX x. x=t & P(x)) = P(t)"
   971     "!!P. (EX x. t=x & P(x)) = P(t)"
   972     "!!P. (ALL x. x=t --> P(x)) = P(t)"
   973     "!!P. (ALL x. t=x --> P(x)) = P(t)"
   974   by (blast, blast, blast, blast, blast, iprover+)
   975 
   976 lemma disj_absorb: "(A | A) = A"
   977   by blast
   978 
   979 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
   980   by blast
   981 
   982 lemma conj_absorb: "(A & A) = A"
   983   by blast
   984 
   985 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
   986   by blast
   987 
   988 lemma eq_ac:
   989   shows eq_commute: "a = b \<longleftrightarrow> b = a"
   990     and iff_left_commute: "(P \<longleftrightarrow> (Q \<longleftrightarrow> R)) \<longleftrightarrow> (Q \<longleftrightarrow> (P \<longleftrightarrow> R))"
   991     and iff_assoc: "((P \<longleftrightarrow> Q) \<longleftrightarrow> R) \<longleftrightarrow> (P \<longleftrightarrow> (Q \<longleftrightarrow> R))" by (iprover, blast+)
   992 lemma neq_commute: "a \<noteq> b \<longleftrightarrow> b \<noteq> a" by iprover
   993 
   994 lemma conj_comms:
   995   shows conj_commute: "(P&Q) = (Q&P)"
   996     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
   997 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
   998 
   999 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
  1000 
  1001 lemma disj_comms:
  1002   shows disj_commute: "(P|Q) = (Q|P)"
  1003     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
  1004 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
  1005 
  1006 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
  1007 
  1008 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
  1009 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
  1010 
  1011 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
  1012 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
  1013 
  1014 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
  1015 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
  1016 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
  1017 
  1018 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
  1019 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
  1020 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
  1021 
  1022 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
  1023 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
  1024 
  1025 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
  1026   by iprover
  1027 
  1028 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
  1029 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
  1030 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
  1031 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
  1032 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
  1033 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
  1034   by blast
  1035 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
  1036 
  1037 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
  1038 
  1039 
  1040 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
  1041   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
  1042   -- {* cases boil down to the same thing. *}
  1043   by blast
  1044 
  1045 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
  1046 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
  1047 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
  1048 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
  1049 lemma all_not_ex: "(ALL x. P x) = (~ (EX x. ~ P x ))" by blast
  1050 
  1051 declare All_def [no_atp]
  1052 
  1053 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
  1054 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
  1055 
  1056 text {*
  1057   \medskip The @{text "&"} congruence rule: not included by default!
  1058   May slow rewrite proofs down by as much as 50\% *}
  1059 
  1060 lemma conj_cong:
  1061     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
  1062   by iprover
  1063 
  1064 lemma rev_conj_cong:
  1065     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
  1066   by iprover
  1067 
  1068 text {* The @{text "|"} congruence rule: not included by default! *}
  1069 
  1070 lemma disj_cong:
  1071     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
  1072   by blast
  1073 
  1074 
  1075 text {* \medskip if-then-else rules *}
  1076 
  1077 lemma if_True [code]: "(if True then x else y) = x"
  1078   by (unfold If_def) blast
  1079 
  1080 lemma if_False [code]: "(if False then x else y) = y"
  1081   by (unfold If_def) blast
  1082 
  1083 lemma if_P: "P ==> (if P then x else y) = x"
  1084   by (unfold If_def) blast
  1085 
  1086 lemma if_not_P: "~P ==> (if P then x else y) = y"
  1087   by (unfold If_def) blast
  1088 
  1089 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
  1090   apply (rule case_split [of Q])
  1091    apply (simplesubst if_P)
  1092     prefer 3 apply (simplesubst if_not_P, blast+)
  1093   done
  1094 
  1095 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
  1096 by (simplesubst split_if, blast)
  1097 
  1098 lemmas if_splits [no_atp] = split_if split_if_asm
  1099 
  1100 lemma if_cancel: "(if c then x else x) = x"
  1101 by (simplesubst split_if, blast)
  1102 
  1103 lemma if_eq_cancel: "(if x = y then y else x) = x"
  1104 by (simplesubst split_if, blast)
  1105 
  1106 lemma if_bool_eq_conj:
  1107 "(if P then Q else R) = ((P-->Q) & (~P-->R))"
  1108   -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
  1109   by (rule split_if)
  1110 
  1111 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
  1112   -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
  1113   apply (simplesubst split_if, blast)
  1114   done
  1115 
  1116 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
  1117 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
  1118 
  1119 text {* \medskip let rules for simproc *}
  1120 
  1121 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
  1122   by (unfold Let_def)
  1123 
  1124 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
  1125   by (unfold Let_def)
  1126 
  1127 text {*
  1128   The following copy of the implication operator is useful for
  1129   fine-tuning congruence rules.  It instructs the simplifier to simplify
  1130   its premise.
  1131 *}
  1132 
  1133 definition simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1) where
  1134   "simp_implies \<equiv> op ==>"
  1135 
  1136 lemma simp_impliesI:
  1137   assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
  1138   shows "PROP P =simp=> PROP Q"
  1139   apply (unfold simp_implies_def)
  1140   apply (rule PQ)
  1141   apply assumption
  1142   done
  1143 
  1144 lemma simp_impliesE:
  1145   assumes PQ: "PROP P =simp=> PROP Q"
  1146   and P: "PROP P"
  1147   and QR: "PROP Q \<Longrightarrow> PROP R"
  1148   shows "PROP R"
  1149   apply (rule QR)
  1150   apply (rule PQ [unfolded simp_implies_def])
  1151   apply (rule P)
  1152   done
  1153 
  1154 lemma simp_implies_cong:
  1155   assumes PP' :"PROP P == PROP P'"
  1156   and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
  1157   shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
  1158 proof (unfold simp_implies_def, rule equal_intr_rule)
  1159   assume PQ: "PROP P \<Longrightarrow> PROP Q"
  1160   and P': "PROP P'"
  1161   from PP' [symmetric] and P' have "PROP P"
  1162     by (rule equal_elim_rule1)
  1163   then have "PROP Q" by (rule PQ)
  1164   with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
  1165 next
  1166   assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
  1167   and P: "PROP P"
  1168   from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
  1169   then have "PROP Q'" by (rule P'Q')
  1170   with P'QQ' [OF P', symmetric] show "PROP Q"
  1171     by (rule equal_elim_rule1)
  1172 qed
  1173 
  1174 lemma uncurry:
  1175   assumes "P \<longrightarrow> Q \<longrightarrow> R"
  1176   shows "P \<and> Q \<longrightarrow> R"
  1177   using assms by blast
  1178 
  1179 lemma iff_allI:
  1180   assumes "\<And>x. P x = Q x"
  1181   shows "(\<forall>x. P x) = (\<forall>x. Q x)"
  1182   using assms by blast
  1183 
  1184 lemma iff_exI:
  1185   assumes "\<And>x. P x = Q x"
  1186   shows "(\<exists>x. P x) = (\<exists>x. Q x)"
  1187   using assms by blast
  1188 
  1189 lemma all_comm:
  1190   "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
  1191   by blast
  1192 
  1193 lemma ex_comm:
  1194   "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
  1195   by blast
  1196 
  1197 ML_file "Tools/simpdata.ML"
  1198 ML {* open Simpdata *}
  1199 
  1200 setup {* map_theory_simpset (put_simpset HOL_basic_ss) *}
  1201 
  1202 simproc_setup defined_Ex ("EX x. P x") = {* fn _ => Quantifier1.rearrange_ex *}
  1203 simproc_setup defined_All ("ALL x. P x") = {* fn _ => Quantifier1.rearrange_all *}
  1204 
  1205 setup {*
  1206   Simplifier.method_setup Splitter.split_modifiers
  1207   #> Splitter.setup
  1208   #> clasimp_setup
  1209   #> EqSubst.setup
  1210 *}
  1211 
  1212 text {* Simproc for proving @{text "(y = x) == False"} from premise @{text "~(x = y)"}: *}
  1213 
  1214 simproc_setup neq ("x = y") = {* fn _ =>
  1215 let
  1216   val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
  1217   fun is_neq eq lhs rhs thm =
  1218     (case Thm.prop_of thm of
  1219       _ $ (Not $ (eq' $ l' $ r')) =>
  1220         Not = HOLogic.Not andalso eq' = eq andalso
  1221         r' aconv lhs andalso l' aconv rhs
  1222     | _ => false);
  1223   fun proc ss ct =
  1224     (case Thm.term_of ct of
  1225       eq $ lhs $ rhs =>
  1226         (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of ss) of
  1227           SOME thm => SOME (thm RS neq_to_EQ_False)
  1228         | NONE => NONE)
  1229      | _ => NONE);
  1230 in proc end;
  1231 *}
  1232 
  1233 simproc_setup let_simp ("Let x f") = {*
  1234 let
  1235   val (f_Let_unfold, x_Let_unfold) =
  1236     let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_unfold}
  1237     in (cterm_of @{theory} f, cterm_of @{theory} x) end
  1238   val (f_Let_folded, x_Let_folded) =
  1239     let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_folded}
  1240     in (cterm_of @{theory} f, cterm_of @{theory} x) end;
  1241   val g_Let_folded =
  1242     let val [(_ $ _ $ (g $ _))] = prems_of @{thm Let_folded}
  1243     in cterm_of @{theory} g end;
  1244   fun count_loose (Bound i) k = if i >= k then 1 else 0
  1245     | count_loose (s $ t) k = count_loose s k + count_loose t k
  1246     | count_loose (Abs (_, _, t)) k = count_loose  t (k + 1)
  1247     | count_loose _ _ = 0;
  1248   fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
  1249    case t
  1250     of Abs (_, _, t') => count_loose t' 0 <= 1
  1251      | _ => true;
  1252 in fn _ => fn ctxt => fn ct => if is_trivial_let (Thm.term_of ct)
  1253   then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
  1254   else let (*Norbert Schirmer's case*)
  1255     val thy = Proof_Context.theory_of ctxt;
  1256     val t = Thm.term_of ct;
  1257     val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
  1258   in Option.map (hd o Variable.export ctxt' ctxt o single)
  1259     (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
  1260       if is_Free x orelse is_Bound x orelse is_Const x
  1261       then SOME @{thm Let_def}
  1262       else
  1263         let
  1264           val n = case f of (Abs (x, _, _)) => x | _ => "x";
  1265           val cx = cterm_of thy x;
  1266           val {T = xT, ...} = rep_cterm cx;
  1267           val cf = cterm_of thy f;
  1268           val fx_g = Simplifier.rewrite ctxt (Thm.apply cf cx);
  1269           val (_ $ _ $ g) = prop_of fx_g;
  1270           val g' = abstract_over (x,g);
  1271           val abs_g'= Abs (n,xT,g');
  1272         in (if (g aconv g')
  1273              then
  1274                 let
  1275                   val rl =
  1276                     cterm_instantiate [(f_Let_unfold, cf), (x_Let_unfold, cx)] @{thm Let_unfold};
  1277                 in SOME (rl OF [fx_g]) end
  1278              else if (Envir.beta_eta_contract f) aconv (Envir.beta_eta_contract abs_g') then NONE (*avoid identity conversion*)
  1279              else let
  1280                    val g'x = abs_g'$x;
  1281                    val g_g'x = Thm.symmetric (Thm.beta_conversion false (cterm_of thy g'x));
  1282                    val rl = cterm_instantiate
  1283                              [(f_Let_folded, cterm_of thy f), (x_Let_folded, cx),
  1284                               (g_Let_folded, cterm_of thy abs_g')]
  1285                              @{thm Let_folded};
  1286                  in SOME (rl OF [Thm.transitive fx_g g_g'x])
  1287                  end)
  1288         end
  1289     | _ => NONE)
  1290   end
  1291 end *}
  1292 
  1293 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
  1294 proof
  1295   assume "True \<Longrightarrow> PROP P"
  1296   from this [OF TrueI] show "PROP P" .
  1297 next
  1298   assume "PROP P"
  1299   then show "PROP P" .
  1300 qed
  1301 
  1302 lemma ex_simps:
  1303   "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
  1304   "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
  1305   "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
  1306   "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
  1307   "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
  1308   "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
  1309   -- {* Miniscoping: pushing in existential quantifiers. *}
  1310   by (iprover | blast)+
  1311 
  1312 lemma all_simps:
  1313   "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
  1314   "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
  1315   "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
  1316   "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
  1317   "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
  1318   "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
  1319   -- {* Miniscoping: pushing in universal quantifiers. *}
  1320   by (iprover | blast)+
  1321 
  1322 lemmas [simp] =
  1323   triv_forall_equality (*prunes params*)
  1324   True_implies_equals  (*prune asms `True'*)
  1325   if_True
  1326   if_False
  1327   if_cancel
  1328   if_eq_cancel
  1329   imp_disjL
  1330   (*In general it seems wrong to add distributive laws by default: they
  1331     might cause exponential blow-up.  But imp_disjL has been in for a while
  1332     and cannot be removed without affecting existing proofs.  Moreover,
  1333     rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
  1334     grounds that it allows simplification of R in the two cases.*)
  1335   conj_assoc
  1336   disj_assoc
  1337   de_Morgan_conj
  1338   de_Morgan_disj
  1339   imp_disj1
  1340   imp_disj2
  1341   not_imp
  1342   disj_not1
  1343   not_all
  1344   not_ex
  1345   cases_simp
  1346   the_eq_trivial
  1347   the_sym_eq_trivial
  1348   ex_simps
  1349   all_simps
  1350   simp_thms
  1351 
  1352 lemmas [cong] = imp_cong simp_implies_cong
  1353 lemmas [split] = split_if
  1354 
  1355 ML {* val HOL_ss = simpset_of @{context} *}
  1356 
  1357 text {* Simplifies x assuming c and y assuming ~c *}
  1358 lemma if_cong:
  1359   assumes "b = c"
  1360       and "c \<Longrightarrow> x = u"
  1361       and "\<not> c \<Longrightarrow> y = v"
  1362   shows "(if b then x else y) = (if c then u else v)"
  1363   using assms by simp
  1364 
  1365 text {* Prevents simplification of x and y:
  1366   faster and allows the execution of functional programs. *}
  1367 lemma if_weak_cong [cong]:
  1368   assumes "b = c"
  1369   shows "(if b then x else y) = (if c then x else y)"
  1370   using assms by (rule arg_cong)
  1371 
  1372 text {* Prevents simplification of t: much faster *}
  1373 lemma let_weak_cong:
  1374   assumes "a = b"
  1375   shows "(let x = a in t x) = (let x = b in t x)"
  1376   using assms by (rule arg_cong)
  1377 
  1378 text {* To tidy up the result of a simproc.  Only the RHS will be simplified. *}
  1379 lemma eq_cong2:
  1380   assumes "u = u'"
  1381   shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
  1382   using assms by simp
  1383 
  1384 lemma if_distrib:
  1385   "f (if c then x else y) = (if c then f x else f y)"
  1386   by simp
  1387 
  1388 text{*As a simplification rule, it replaces all function equalities by
  1389   first-order equalities.*}
  1390 lemma fun_eq_iff: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
  1391   by auto
  1392 
  1393 
  1394 subsubsection {* Generic cases and induction *}
  1395 
  1396 text {* Rule projections: *}
  1397 
  1398 ML {*
  1399 structure Project_Rule = Project_Rule
  1400 (
  1401   val conjunct1 = @{thm conjunct1}
  1402   val conjunct2 = @{thm conjunct2}
  1403   val mp = @{thm mp}
  1404 )
  1405 *}
  1406 
  1407 definition induct_forall where
  1408   "induct_forall P == \<forall>x. P x"
  1409 
  1410 definition induct_implies where
  1411   "induct_implies A B == A \<longrightarrow> B"
  1412 
  1413 definition induct_equal where
  1414   "induct_equal x y == x = y"
  1415 
  1416 definition induct_conj where
  1417   "induct_conj A B == A \<and> B"
  1418 
  1419 definition induct_true where
  1420   "induct_true == True"
  1421 
  1422 definition induct_false where
  1423   "induct_false == False"
  1424 
  1425 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
  1426   by (unfold atomize_all induct_forall_def)
  1427 
  1428 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
  1429   by (unfold atomize_imp induct_implies_def)
  1430 
  1431 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
  1432   by (unfold atomize_eq induct_equal_def)
  1433 
  1434 lemma induct_conj_eq: "(A &&& B) == Trueprop (induct_conj A B)"
  1435   by (unfold atomize_conj induct_conj_def)
  1436 
  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] = induct_atomize'
  1440 lemmas induct_rulify [symmetric] = 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
  1444 
  1445 
  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
  1449 
  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
  1453 
  1454 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
  1455 proof
  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`)
  1458 next
  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])
  1461 qed
  1462 
  1463 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
  1464 
  1465 lemma induct_trueI: "induct_true"
  1466   by (simp add: induct_true_def)
  1467 
  1468 text {* Method setup. *}
  1469 
  1470 ML {*
  1471 structure Induct = Induct
  1472 (
  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)
  1479     | dest_def _ = NONE
  1480   val trivial_tac = match_tac @{thms induct_trueI}
  1481 )
  1482 *}
  1483 
  1484 ML_file "~~/src/Tools/induction.ML"
  1485 
  1486 setup {*
  1487   Induct.setup #> Induction.setup #>
  1488   Context.theory_map (Induct.map_simpset (fn ss => ss
  1489     addsimprocs
  1490       [Simplifier.simproc_global @{theory} "swap_induct_false"
  1491          ["induct_false ==> PROP P ==> PROP Q"]
  1492          (fn _ =>
  1493             (fn _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
  1494                   if P <> Q then SOME Drule.swap_prems_eq else NONE
  1495               | _ => NONE)),
  1496        Simplifier.simproc_global @{theory} "induct_equal_conj_curry"
  1497          ["induct_conj P Q ==> PROP R"]
  1498          (fn _ =>
  1499             (fn _ $ (_ $ P) $ _ =>
  1500                 let
  1501                   fun is_conj (@{const induct_conj} $ P $ Q) =
  1502                         is_conj P andalso is_conj Q
  1503                     | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
  1504                     | is_conj @{const induct_true} = true
  1505                     | is_conj @{const induct_false} = true
  1506                     | is_conj _ = false
  1507                 in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
  1508               | _ => NONE))]
  1509     |> Simplifier.set_mksimps (fn ctxt =>
  1510         Simpdata.mksimps Simpdata.mksimps_pairs ctxt #>
  1511         map (rewrite_rule ctxt (map Thm.symmetric @{thms induct_rulify_fallback})))))
  1512 *}
  1513 
  1514 text {* Pre-simplification of induction and cases rules *}
  1515 
  1516 lemma [induct_simp]: "(!!x. induct_equal x t ==> PROP P x) == PROP P t"
  1517   unfolding induct_equal_def
  1518 proof
  1519   assume R: "!!x. x = t ==> PROP P x"
  1520   show "PROP P t" by (rule R [OF refl])
  1521 next
  1522   fix x assume "PROP P t" "x = t"
  1523   then show "PROP P x" by simp
  1524 qed
  1525 
  1526 lemma [induct_simp]: "(!!x. induct_equal t x ==> PROP P x) == PROP P t"
  1527   unfolding induct_equal_def
  1528 proof
  1529   assume R: "!!x. t = x ==> PROP P x"
  1530   show "PROP P t" by (rule R [OF refl])
  1531 next
  1532   fix x assume "PROP P t" "t = x"
  1533   then show "PROP P x" by simp
  1534 qed
  1535 
  1536 lemma [induct_simp]: "(induct_false ==> P) == Trueprop induct_true"
  1537   unfolding induct_false_def induct_true_def
  1538   by (iprover intro: equal_intr_rule)
  1539 
  1540 lemma [induct_simp]: "(induct_true ==> PROP P) == PROP P"
  1541   unfolding induct_true_def
  1542 proof
  1543   assume R: "True \<Longrightarrow> PROP P"
  1544   from TrueI show "PROP P" by (rule R)
  1545 next
  1546   assume "PROP P"
  1547   then show "PROP P" .
  1548 qed
  1549 
  1550 lemma [induct_simp]: "(PROP P ==> induct_true) == Trueprop induct_true"
  1551   unfolding induct_true_def
  1552   by (iprover intro: equal_intr_rule)
  1553 
  1554 lemma [induct_simp]: "(!!x. induct_true) == Trueprop induct_true"
  1555   unfolding induct_true_def
  1556   by (iprover intro: equal_intr_rule)
  1557 
  1558 lemma [induct_simp]: "induct_implies induct_true P == P"
  1559   by (simp add: induct_implies_def induct_true_def)
  1560 
  1561 lemma [induct_simp]: "(x = x) = True" 
  1562   by (rule simp_thms)
  1563 
  1564 hide_const induct_forall induct_implies induct_equal induct_conj induct_true induct_false
  1565 
  1566 ML_file "~~/src/Tools/induct_tacs.ML"
  1567 setup Induct_Tacs.setup
  1568 
  1569 
  1570 subsubsection {* Coherent logic *}
  1571 
  1572 ML_file "~~/src/Tools/coherent.ML"
  1573 ML {*
  1574 structure Coherent = Coherent
  1575 (
  1576   val atomize_elimL = @{thm atomize_elimL};
  1577   val atomize_exL = @{thm atomize_exL};
  1578   val atomize_conjL = @{thm atomize_conjL};
  1579   val atomize_disjL = @{thm atomize_disjL};
  1580   val operator_names = [@{const_name HOL.disj}, @{const_name HOL.conj}, @{const_name Ex}];
  1581 );
  1582 *}
  1583 
  1584 
  1585 subsubsection {* Reorienting equalities *}
  1586 
  1587 ML {*
  1588 signature REORIENT_PROC =
  1589 sig
  1590   val add : (term -> bool) -> theory -> theory
  1591   val proc : morphism -> Proof.context -> cterm -> thm option
  1592 end;
  1593 
  1594 structure Reorient_Proc : REORIENT_PROC =
  1595 struct
  1596   structure Data = Theory_Data
  1597   (
  1598     type T = ((term -> bool) * stamp) list;
  1599     val empty = [];
  1600     val extend = I;
  1601     fun merge data : T = Library.merge (eq_snd op =) data;
  1602   );
  1603   fun add m = Data.map (cons (m, stamp ()));
  1604   fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
  1605 
  1606   val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
  1607   fun proc phi ctxt ct =
  1608     let
  1609       val thy = Proof_Context.theory_of ctxt;
  1610     in
  1611       case Thm.term_of ct of
  1612         (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
  1613       | _ => NONE
  1614     end;
  1615 end;
  1616 *}
  1617 
  1618 
  1619 subsection {* Other simple lemmas and lemma duplicates *}
  1620 
  1621 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
  1622   by blast+
  1623 
  1624 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
  1625   apply (rule iffI)
  1626   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
  1627   apply (fast dest!: theI')
  1628   apply (fast intro: the1_equality [symmetric])
  1629   apply (erule ex1E)
  1630   apply (rule allI)
  1631   apply (rule ex1I)
  1632   apply (erule spec)
  1633   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
  1634   apply (erule impE)
  1635   apply (rule allI)
  1636   apply (case_tac "xa = x")
  1637   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1638   done
  1639 
  1640 lemmas eq_sym_conv = eq_commute
  1641 
  1642 lemma nnf_simps:
  1643   "(\<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)" 
  1644   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))" 
  1645   "(\<not> \<not>(P)) = P"
  1646 by blast+
  1647 
  1648 subsection {* Basic ML bindings *}
  1649 
  1650 ML {*
  1651 val FalseE = @{thm FalseE}
  1652 val Let_def = @{thm Let_def}
  1653 val TrueI = @{thm TrueI}
  1654 val allE = @{thm allE}
  1655 val allI = @{thm allI}
  1656 val all_dupE = @{thm all_dupE}
  1657 val arg_cong = @{thm arg_cong}
  1658 val box_equals = @{thm box_equals}
  1659 val ccontr = @{thm ccontr}
  1660 val classical = @{thm classical}
  1661 val conjE = @{thm conjE}
  1662 val conjI = @{thm conjI}
  1663 val conjunct1 = @{thm conjunct1}
  1664 val conjunct2 = @{thm conjunct2}
  1665 val disjCI = @{thm disjCI}
  1666 val disjE = @{thm disjE}
  1667 val disjI1 = @{thm disjI1}
  1668 val disjI2 = @{thm disjI2}
  1669 val eq_reflection = @{thm eq_reflection}
  1670 val ex1E = @{thm ex1E}
  1671 val ex1I = @{thm ex1I}
  1672 val ex1_implies_ex = @{thm ex1_implies_ex}
  1673 val exE = @{thm exE}
  1674 val exI = @{thm exI}
  1675 val excluded_middle = @{thm excluded_middle}
  1676 val ext = @{thm ext}
  1677 val fun_cong = @{thm fun_cong}
  1678 val iffD1 = @{thm iffD1}
  1679 val iffD2 = @{thm iffD2}
  1680 val iffI = @{thm iffI}
  1681 val impE = @{thm impE}
  1682 val impI = @{thm impI}
  1683 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1684 val mp = @{thm mp}
  1685 val notE = @{thm notE}
  1686 val notI = @{thm notI}
  1687 val not_all = @{thm not_all}
  1688 val not_ex = @{thm not_ex}
  1689 val not_iff = @{thm not_iff}
  1690 val not_not = @{thm not_not}
  1691 val not_sym = @{thm not_sym}
  1692 val refl = @{thm refl}
  1693 val rev_mp = @{thm rev_mp}
  1694 val spec = @{thm spec}
  1695 val ssubst = @{thm ssubst}
  1696 val subst = @{thm subst}
  1697 val sym = @{thm sym}
  1698 val trans = @{thm trans}
  1699 *}
  1700 
  1701 ML_file "Tools/cnf.ML"
  1702 
  1703 
  1704 section {* @{text NO_MATCH} simproc *}
  1705 
  1706 text {*
  1707  The simplification procedure can be used to avoid simplification of terms of a certain form
  1708 *}
  1709 
  1710 definition NO_MATCH :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where "NO_MATCH val pat \<equiv> True"
  1711 lemma NO_MATCH_cong[cong]: "NO_MATCH val pat = NO_MATCH val pat" by (rule refl)
  1712 
  1713 simproc_setup NO_MATCH ("NO_MATCH val pat") = {* fn _ => fn ctxt => fn ct =>
  1714   let
  1715     val thy = Proof_Context.theory_of ctxt
  1716     val dest_binop = Term.dest_comb #> apfst (Term.dest_comb #> snd)
  1717     val m = Pattern.matches thy (dest_binop (Thm.term_of ct))
  1718   in if m then NONE else SOME @{thm NO_MATCH_def} end
  1719 *}
  1720 
  1721 text {*
  1722   This setup ensures that a rewrite rule of the form @{term "NO_MATCH val pat \<Longrightarrow> t"}
  1723   is only applied, if the pattern @{term pat} does not match the value @{term val}.
  1724 *}
  1725 
  1726 
  1727 subsection {* Code generator setup *}
  1728 
  1729 subsubsection {* Generic code generator preprocessor setup *}
  1730 
  1731 lemma conj_left_cong:
  1732   "P \<longleftrightarrow> Q \<Longrightarrow> P \<and> R \<longleftrightarrow> Q \<and> R"
  1733   by (fact arg_cong)
  1734 
  1735 lemma disj_left_cong:
  1736   "P \<longleftrightarrow> Q \<Longrightarrow> P \<or> R \<longleftrightarrow> Q \<or> R"
  1737   by (fact arg_cong)
  1738 
  1739 setup {*
  1740   Code_Preproc.map_pre (put_simpset HOL_basic_ss)
  1741   #> Code_Preproc.map_post (put_simpset HOL_basic_ss)
  1742   #> Code_Simp.map_ss (put_simpset HOL_basic_ss
  1743     #> Simplifier.add_cong @{thm conj_left_cong} #> Simplifier.add_cong @{thm disj_left_cong})
  1744 *}
  1745 
  1746 
  1747 subsubsection {* Equality *}
  1748 
  1749 class equal =
  1750   fixes equal :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1751   assumes equal_eq: "equal x y \<longleftrightarrow> x = y"
  1752 begin
  1753 
  1754 lemma equal: "equal = (op =)"
  1755   by (rule ext equal_eq)+
  1756 
  1757 lemma equal_refl: "equal x x \<longleftrightarrow> True"
  1758   unfolding equal by rule+
  1759 
  1760 lemma eq_equal: "(op =) \<equiv> equal"
  1761   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule equal_eq)
  1762 
  1763 end
  1764 
  1765 declare eq_equal [symmetric, code_post]
  1766 declare eq_equal [code]
  1767 
  1768 setup {*
  1769   Code_Preproc.map_pre (fn ctxt =>
  1770     ctxt addsimprocs [Simplifier.simproc_global_i @{theory} "equal" [@{term HOL.eq}]
  1771       (fn _ => fn Const (_, Type ("fun", [Type _, _])) => SOME @{thm eq_equal} | _ => NONE)])
  1772 *}
  1773 
  1774 
  1775 subsubsection {* Generic code generator foundation *}
  1776 
  1777 text {* Datatype @{typ bool} *}
  1778 
  1779 code_datatype True False
  1780 
  1781 lemma [code]:
  1782   shows "False \<and> P \<longleftrightarrow> False"
  1783     and "True \<and> P \<longleftrightarrow> P"
  1784     and "P \<and> False \<longleftrightarrow> False"
  1785     and "P \<and> True \<longleftrightarrow> P" by simp_all
  1786 
  1787 lemma [code]:
  1788   shows "False \<or> P \<longleftrightarrow> P"
  1789     and "True \<or> P \<longleftrightarrow> True"
  1790     and "P \<or> False \<longleftrightarrow> P"
  1791     and "P \<or> True \<longleftrightarrow> True" by simp_all
  1792 
  1793 lemma [code]:
  1794   shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
  1795     and "(True \<longrightarrow> P) \<longleftrightarrow> P"
  1796     and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
  1797     and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
  1798 
  1799 text {* More about @{typ prop} *}
  1800 
  1801 lemma [code nbe]:
  1802   shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q" 
  1803     and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
  1804     and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
  1805 
  1806 lemma Trueprop_code [code]:
  1807   "Trueprop True \<equiv> Code_Generator.holds"
  1808   by (auto intro!: equal_intr_rule holds)
  1809 
  1810 declare Trueprop_code [symmetric, code_post]
  1811 
  1812 text {* Equality *}
  1813 
  1814 declare simp_thms(6) [code nbe]
  1815 
  1816 instantiation itself :: (type) equal
  1817 begin
  1818 
  1819 definition equal_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
  1820   "equal_itself x y \<longleftrightarrow> x = y"
  1821 
  1822 instance proof
  1823 qed (fact equal_itself_def)
  1824 
  1825 end
  1826 
  1827 lemma equal_itself_code [code]:
  1828   "equal TYPE('a) TYPE('a) \<longleftrightarrow> True"
  1829   by (simp add: equal)
  1830 
  1831 setup {*
  1832   Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"})
  1833 *}
  1834 
  1835 lemma equal_alias_cert: "OFCLASS('a, equal_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> equal)" (is "?ofclass \<equiv> ?equal")
  1836 proof
  1837   assume "PROP ?ofclass"
  1838   show "PROP ?equal"
  1839     by (tactic {* ALLGOALS (rtac (Thm.unconstrainT @{thm eq_equal})) *})
  1840       (fact `PROP ?ofclass`)
  1841 next
  1842   assume "PROP ?equal"
  1843   show "PROP ?ofclass" proof
  1844   qed (simp add: `PROP ?equal`)
  1845 qed
  1846   
  1847 setup {*
  1848   Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>equal \<Rightarrow> 'a \<Rightarrow> bool"})
  1849 *}
  1850 
  1851 setup {*
  1852   Nbe.add_const_alias @{thm equal_alias_cert}
  1853 *}
  1854 
  1855 text {* Cases *}
  1856 
  1857 lemma Let_case_cert:
  1858   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
  1859   shows "CASE x \<equiv> f x"
  1860   using assms by simp_all
  1861 
  1862 setup {*
  1863   Code.add_case @{thm Let_case_cert}
  1864   #> Code.add_undefined @{const_name undefined}
  1865 *}
  1866 
  1867 declare [[code abort: undefined]]
  1868 
  1869 
  1870 subsubsection {* Generic code generator target languages *}
  1871 
  1872 text {* type @{typ bool} *}
  1873 
  1874 code_printing
  1875   type_constructor bool \<rightharpoonup>
  1876     (SML) "bool" and (OCaml) "bool" and (Haskell) "Bool" and (Scala) "Boolean"
  1877 | constant True \<rightharpoonup>
  1878     (SML) "true" and (OCaml) "true" and (Haskell) "True" and (Scala) "true"
  1879 | constant False \<rightharpoonup>
  1880     (SML) "false" and (OCaml) "false" and (Haskell) "False" and (Scala) "false" 
  1881 
  1882 code_reserved SML
  1883   bool true false
  1884 
  1885 code_reserved OCaml
  1886   bool
  1887 
  1888 code_reserved Scala
  1889   Boolean
  1890 
  1891 code_printing
  1892   constant Not \<rightharpoonup>
  1893     (SML) "not" and (OCaml) "not" and (Haskell) "not" and (Scala) "'! _"
  1894 | constant HOL.conj \<rightharpoonup>
  1895     (SML) infixl 1 "andalso" and (OCaml) infixl 3 "&&" and (Haskell) infixr 3 "&&" and (Scala) infixl 3 "&&"
  1896 | constant HOL.disj \<rightharpoonup>
  1897     (SML) infixl 0 "orelse" and (OCaml) infixl 2 "||" and (Haskell) infixl 2 "||" and (Scala) infixl 1 "||"
  1898 | constant HOL.implies \<rightharpoonup>
  1899     (SML) "!(if (_)/ then (_)/ else true)"
  1900     and (OCaml) "!(if (_)/ then (_)/ else true)"
  1901     and (Haskell) "!(if (_)/ then (_)/ else True)"
  1902     and (Scala) "!(if ((_))/ (_)/ else true)"
  1903 | constant If \<rightharpoonup>
  1904     (SML) "!(if (_)/ then (_)/ else (_))"
  1905     and (OCaml) "!(if (_)/ then (_)/ else (_))"
  1906     and (Haskell) "!(if (_)/ then (_)/ else (_))"
  1907     and (Scala) "!(if ((_))/ (_)/ else (_))"
  1908 
  1909 code_reserved SML
  1910   not
  1911 
  1912 code_reserved OCaml
  1913   not
  1914 
  1915 code_identifier
  1916   code_module Pure \<rightharpoonup>
  1917     (SML) HOL and (OCaml) HOL and (Haskell) HOL and (Scala) HOL
  1918 
  1919 text {* using built-in Haskell equality *}
  1920 
  1921 code_printing
  1922   type_class equal \<rightharpoonup> (Haskell) "Eq"
  1923 | constant HOL.equal \<rightharpoonup> (Haskell) infix 4 "=="
  1924 | constant HOL.eq \<rightharpoonup> (Haskell) infix 4 "=="
  1925 
  1926 text {* undefined *}
  1927 
  1928 code_printing
  1929   constant undefined \<rightharpoonup>
  1930     (SML) "!(raise/ Fail/ \"undefined\")"
  1931     and (OCaml) "failwith/ \"undefined\""
  1932     and (Haskell) "error/ \"undefined\""
  1933     and (Scala) "!sys.error(\"undefined\")"
  1934 
  1935 
  1936 subsubsection {* Evaluation and normalization by evaluation *}
  1937 
  1938 method_setup eval = {*
  1939 let
  1940   fun eval_tac ctxt =
  1941     let val conv = Code_Runtime.dynamic_holds_conv ctxt
  1942     in CONVERSION (Conv.params_conv ~1 (K (Conv.concl_conv ~1 conv)) ctxt) THEN' rtac TrueI end
  1943 in
  1944   Scan.succeed (SIMPLE_METHOD' o eval_tac)
  1945 end
  1946 *} "solve goal by evaluation"
  1947 
  1948 method_setup normalization = {*
  1949   Scan.succeed (fn ctxt =>
  1950     SIMPLE_METHOD'
  1951       (CHANGED_PROP o
  1952         (CONVERSION (Nbe.dynamic_conv ctxt)
  1953           THEN_ALL_NEW (TRY o rtac TrueI))))
  1954 *} "solve goal by normalization"
  1955 
  1956 
  1957 subsection {* Counterexample Search Units *}
  1958 
  1959 subsubsection {* Quickcheck *}
  1960 
  1961 quickcheck_params [size = 5, iterations = 50]
  1962 
  1963 
  1964 subsubsection {* Nitpick setup *}
  1965 
  1966 named_theorems nitpick_unfold
  1967   "alternative definitions of constants as needed by Nitpick"
  1968 named_theorems nitpick_simp
  1969   "equational specification of constants as needed by Nitpick"
  1970 named_theorems nitpick_psimp
  1971   "partial equational specification of constants as needed by Nitpick"
  1972 named_theorems nitpick_choice_spec
  1973   "choice specification of constants as needed by Nitpick"
  1974 
  1975 declare if_bool_eq_conj [nitpick_unfold, no_atp]
  1976         if_bool_eq_disj [no_atp]
  1977 
  1978 
  1979 subsection {* Preprocessing for the predicate compiler *}
  1980 
  1981 named_theorems code_pred_def
  1982   "alternative definitions of constants for the Predicate Compiler"
  1983 named_theorems code_pred_inline
  1984   "inlining definitions for the Predicate Compiler"
  1985 named_theorems code_pred_simp
  1986   "simplification rules for the optimisations in the Predicate Compiler"
  1987 
  1988 
  1989 subsection {* Legacy tactics and ML bindings *}
  1990 
  1991 ML {*
  1992 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  1993 local
  1994   fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
  1995     | wrong_prem (Bound _) = true
  1996     | wrong_prem _ = false;
  1997   val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  1998 in
  1999   fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
  2000   fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
  2001 end;
  2002 
  2003 local
  2004   val nnf_ss =
  2005     simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms simp_thms nnf_simps});
  2006 in
  2007   fun nnf_conv ctxt = Simplifier.rewrite (put_simpset nnf_ss ctxt);
  2008 end
  2009 *}
  2010 
  2011 hide_const (open) eq equal
  2012 
  2013 end