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