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