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