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