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