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