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