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