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