src/HOL/HOL.thy
author nipkow
Fri Aug 28 18:52:41 2009 +0200 (2009-08-28)
changeset 32436 10cd49e0c067
parent 32402 5731300da417
child 32544 e129333b9df0
permissions -rw-r--r--
Turned "x <= y ==> sup x y = y" (and relatives) into simp rules
     1 (*  Title:      HOL/HOL.thy
     2     Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
     3 *)
     4 
     5 header {* The basis of Higher-Order Logic *}
     6 
     7 theory HOL
     8 imports Pure "~~/src/Tools/Code_Generator"
     9 uses
    10   ("Tools/hologic.ML")
    11   "~~/src/Tools/auto_solve.ML"
    12   "~~/src/Tools/IsaPlanner/zipper.ML"
    13   "~~/src/Tools/IsaPlanner/isand.ML"
    14   "~~/src/Tools/IsaPlanner/rw_tools.ML"
    15   "~~/src/Tools/IsaPlanner/rw_inst.ML"
    16   "~~/src/Tools/intuitionistic.ML"
    17   "~~/src/Tools/project_rule.ML"
    18   "~~/src/Provers/hypsubst.ML"
    19   "~~/src/Provers/splitter.ML"
    20   "~~/src/Provers/classical.ML"
    21   "~~/src/Provers/blast.ML"
    22   "~~/src/Provers/clasimp.ML"
    23   "~~/src/Tools/coherent.ML"
    24   "~~/src/Tools/eqsubst.ML"
    25   "~~/src/Provers/quantifier1.ML"
    26   ("Tools/simpdata.ML")
    27   "~~/src/Tools/random_word.ML"
    28   "~~/src/Tools/atomize_elim.ML"
    29   "~~/src/Tools/induct.ML"
    30   ("~~/src/Tools/induct_tacs.ML")
    31   ("Tools/recfun_codegen.ML")
    32   "~~/src/Tools/more_conv.ML"
    33 begin
    34 
    35 setup {* Intuitionistic.method_setup @{binding iprover} *}
    36 
    37 
    38 subsection {* Primitive logic *}
    39 
    40 subsubsection {* Core syntax *}
    41 
    42 classes type
    43 defaultsort type
    44 setup {* ObjectLogic.add_base_sort @{sort type} *}
    45 
    46 arities
    47   "fun" :: (type, type) type
    48   itself :: (type) type
    49 
    50 global
    51 
    52 typedecl bool
    53 
    54 judgment
    55   Trueprop      :: "bool => prop"                   ("(_)" 5)
    56 
    57 consts
    58   Not           :: "bool => bool"                   ("~ _" [40] 40)
    59   True          :: bool
    60   False         :: bool
    61 
    62   The           :: "('a => bool) => 'a"
    63   All           :: "('a => bool) => bool"           (binder "ALL " 10)
    64   Ex            :: "('a => bool) => bool"           (binder "EX " 10)
    65   Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
    66   Let           :: "['a, 'a => 'b] => 'b"
    67 
    68   "op ="        :: "['a, 'a] => bool"               (infixl "=" 50)
    69   "op &"        :: "[bool, bool] => bool"           (infixr "&" 35)
    70   "op |"        :: "[bool, bool] => bool"           (infixr "|" 30)
    71   "op -->"      :: "[bool, bool] => bool"           (infixr "-->" 25)
    72 
    73 local
    74 
    75 consts
    76   If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" 10)
    77 
    78 
    79 subsubsection {* Additional concrete syntax *}
    80 
    81 notation (output)
    82   "op ="  (infix "=" 50)
    83 
    84 abbreviation
    85   not_equal :: "['a, 'a] => bool"  (infixl "~=" 50) where
    86   "x ~= y == ~ (x = y)"
    87 
    88 notation (output)
    89   not_equal  (infix "~=" 50)
    90 
    91 notation (xsymbols)
    92   Not  ("\<not> _" [40] 40) and
    93   "op &"  (infixr "\<and>" 35) and
    94   "op |"  (infixr "\<or>" 30) and
    95   "op -->"  (infixr "\<longrightarrow>" 25) and
    96   not_equal  (infix "\<noteq>" 50)
    97 
    98 notation (HTML output)
    99   Not  ("\<not> _" [40] 40) and
   100   "op &"  (infixr "\<and>" 35) and
   101   "op |"  (infixr "\<or>" 30) and
   102   not_equal  (infix "\<noteq>" 50)
   103 
   104 abbreviation (iff)
   105   iff :: "[bool, bool] => bool"  (infixr "<->" 25) where
   106   "A <-> B == A = B"
   107 
   108 notation (xsymbols)
   109   iff  (infixr "\<longleftrightarrow>" 25)
   110 
   111 nonterminals
   112   letbinds  letbind
   113   case_syn  cases_syn
   114 
   115 syntax
   116   "_The"        :: "[pttrn, bool] => 'a"                 ("(3THE _./ _)" [0, 10] 10)
   117 
   118   "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
   119   ""            :: "letbind => letbinds"                 ("_")
   120   "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
   121   "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" 10)
   122 
   123   "_case_syntax":: "['a, cases_syn] => 'b"               ("(case _ of/ _)" 10)
   124   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ =>/ _)" 10)
   125   ""            :: "case_syn => cases_syn"               ("_")
   126   "_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
   127 
   128 translations
   129   "THE x. P"              == "The (%x. P)"
   130   "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
   131   "let x = a in e"        == "Let a (%x. e)"
   132 
   133 print_translation {*
   134 (* To avoid eta-contraction of body: *)
   135 [("The", fn [Abs abs] =>
   136      let val (x,t) = atomic_abs_tr' abs
   137      in Syntax.const "_The" $ x $ t end)]
   138 *}
   139 
   140 syntax (xsymbols)
   141   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
   142 
   143 notation (xsymbols)
   144   All  (binder "\<forall>" 10) and
   145   Ex  (binder "\<exists>" 10) and
   146   Ex1  (binder "\<exists>!" 10)
   147 
   148 notation (HTML output)
   149   All  (binder "\<forall>" 10) and
   150   Ex  (binder "\<exists>" 10) and
   151   Ex1  (binder "\<exists>!" 10)
   152 
   153 notation (HOL)
   154   All  (binder "! " 10) and
   155   Ex  (binder "? " 10) and
   156   Ex1  (binder "?! " 10)
   157 
   158 
   159 subsubsection {* Axioms and basic definitions *}
   160 
   161 axioms
   162   refl:           "t = (t::'a)"
   163   subst:          "s = t \<Longrightarrow> P s \<Longrightarrow> P t"
   164   ext:            "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
   165     -- {*Extensionality is built into the meta-logic, and this rule expresses
   166          a related property.  It is an eta-expanded version of the traditional
   167          rule, and similar to the ABS rule of HOL*}
   168 
   169   the_eq_trivial: "(THE x. x = a) = (a::'a)"
   170 
   171   impI:           "(P ==> Q) ==> P-->Q"
   172   mp:             "[| P-->Q;  P |] ==> Q"
   173 
   174 
   175 defs
   176   True_def:     "True      == ((%x::bool. x) = (%x. x))"
   177   All_def:      "All(P)    == (P = (%x. True))"
   178   Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
   179   False_def:    "False     == (!P. P)"
   180   not_def:      "~ P       == P-->False"
   181   and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
   182   or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
   183   Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
   184 
   185 axioms
   186   iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
   187   True_or_False:  "(P=True) | (P=False)"
   188 
   189 defs
   190   Let_def [code]: "Let s f == f(s)"
   191   if_def:         "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
   192 
   193 finalconsts
   194   "op ="
   195   "op -->"
   196   The
   197 
   198 axiomatization
   199   undefined :: 'a
   200 
   201 class default =
   202   fixes default :: 'a
   203 
   204 
   205 subsection {* Fundamental rules *}
   206 
   207 subsubsection {* Equality *}
   208 
   209 lemma sym: "s = t ==> t = s"
   210   by (erule subst) (rule refl)
   211 
   212 lemma ssubst: "t = s ==> P s ==> P t"
   213   by (drule sym) (erule subst)
   214 
   215 lemma trans: "[| r=s; s=t |] ==> r=t"
   216   by (erule subst)
   217 
   218 lemma meta_eq_to_obj_eq: 
   219   assumes meq: "A == B"
   220   shows "A = B"
   221   by (unfold meq) (rule refl)
   222 
   223 text {* Useful with @{text erule} for proving equalities from known equalities. *}
   224      (* a = b
   225         |   |
   226         c = d   *)
   227 lemma box_equals: "[| a=b;  a=c;  b=d |] ==> c=d"
   228 apply (rule trans)
   229 apply (rule trans)
   230 apply (rule sym)
   231 apply assumption+
   232 done
   233 
   234 text {* For calculational reasoning: *}
   235 
   236 lemma forw_subst: "a = b ==> P b ==> P a"
   237   by (rule ssubst)
   238 
   239 lemma back_subst: "P a ==> a = b ==> P b"
   240   by (rule subst)
   241 
   242 
   243 subsubsection {*Congruence rules for application*}
   244 
   245 (*similar to AP_THM in Gordon's HOL*)
   246 lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
   247 apply (erule subst)
   248 apply (rule refl)
   249 done
   250 
   251 (*similar to AP_TERM in Gordon's HOL and FOL's subst_context*)
   252 lemma arg_cong: "x=y ==> f(x)=f(y)"
   253 apply (erule subst)
   254 apply (rule refl)
   255 done
   256 
   257 lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
   258 apply (erule ssubst)+
   259 apply (rule refl)
   260 done
   261 
   262 lemma cong: "[| f = g; (x::'a) = y |] ==> f(x) = g(y)"
   263 apply (erule subst)+
   264 apply (rule refl)
   265 done
   266 
   267 
   268 subsubsection {*Equality of booleans -- iff*}
   269 
   270 lemma iffI: assumes "P ==> Q" and "Q ==> P" shows "P=Q"
   271   by (iprover intro: iff [THEN mp, THEN mp] impI assms)
   272 
   273 lemma iffD2: "[| P=Q; Q |] ==> P"
   274   by (erule ssubst)
   275 
   276 lemma rev_iffD2: "[| Q; P=Q |] ==> P"
   277   by (erule iffD2)
   278 
   279 lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
   280   by (drule sym) (rule iffD2)
   281 
   282 lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
   283   by (drule sym) (rule rev_iffD2)
   284 
   285 lemma iffE:
   286   assumes major: "P=Q"
   287     and minor: "[| P --> Q; Q --> P |] ==> R"
   288   shows R
   289   by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
   290 
   291 
   292 subsubsection {*True*}
   293 
   294 lemma TrueI: "True"
   295   unfolding True_def by (rule refl)
   296 
   297 lemma eqTrueI: "P ==> P = True"
   298   by (iprover intro: iffI TrueI)
   299 
   300 lemma eqTrueE: "P = True ==> P"
   301   by (erule iffD2) (rule TrueI)
   302 
   303 
   304 subsubsection {*Universal quantifier*}
   305 
   306 lemma allI: assumes "!!x::'a. P(x)" shows "ALL x. P(x)"
   307   unfolding All_def by (iprover intro: ext eqTrueI assms)
   308 
   309 lemma spec: "ALL x::'a. P(x) ==> P(x)"
   310 apply (unfold All_def)
   311 apply (rule eqTrueE)
   312 apply (erule fun_cong)
   313 done
   314 
   315 lemma allE:
   316   assumes major: "ALL x. P(x)"
   317     and minor: "P(x) ==> R"
   318   shows R
   319   by (iprover intro: minor major [THEN spec])
   320 
   321 lemma all_dupE:
   322   assumes major: "ALL x. P(x)"
   323     and minor: "[| P(x); ALL x. P(x) |] ==> R"
   324   shows R
   325   by (iprover intro: minor major major [THEN spec])
   326 
   327 
   328 subsubsection {* False *}
   329 
   330 text {*
   331   Depends upon @{text spec}; it is impossible to do propositional
   332   logic before quantifiers!
   333 *}
   334 
   335 lemma FalseE: "False ==> P"
   336   apply (unfold False_def)
   337   apply (erule spec)
   338   done
   339 
   340 lemma False_neq_True: "False = True ==> P"
   341   by (erule eqTrueE [THEN FalseE])
   342 
   343 
   344 subsubsection {* Negation *}
   345 
   346 lemma notI:
   347   assumes "P ==> False"
   348   shows "~P"
   349   apply (unfold not_def)
   350   apply (iprover intro: impI assms)
   351   done
   352 
   353 lemma False_not_True: "False ~= True"
   354   apply (rule notI)
   355   apply (erule False_neq_True)
   356   done
   357 
   358 lemma True_not_False: "True ~= False"
   359   apply (rule notI)
   360   apply (drule sym)
   361   apply (erule False_neq_True)
   362   done
   363 
   364 lemma notE: "[| ~P;  P |] ==> R"
   365   apply (unfold not_def)
   366   apply (erule mp [THEN FalseE])
   367   apply assumption
   368   done
   369 
   370 lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
   371   by (erule notE [THEN notI]) (erule meta_mp)
   372 
   373 
   374 subsubsection {*Implication*}
   375 
   376 lemma impE:
   377   assumes "P-->Q" "P" "Q ==> R"
   378   shows "R"
   379 by (iprover intro: assms mp)
   380 
   381 (* Reduces Q to P-->Q, allowing substitution in P. *)
   382 lemma rev_mp: "[| P;  P --> Q |] ==> Q"
   383 by (iprover intro: mp)
   384 
   385 lemma contrapos_nn:
   386   assumes major: "~Q"
   387       and minor: "P==>Q"
   388   shows "~P"
   389 by (iprover intro: notI minor major [THEN notE])
   390 
   391 (*not used at all, but we already have the other 3 combinations *)
   392 lemma contrapos_pn:
   393   assumes major: "Q"
   394       and minor: "P ==> ~Q"
   395   shows "~P"
   396 by (iprover intro: notI minor major notE)
   397 
   398 lemma not_sym: "t ~= s ==> s ~= t"
   399   by (erule contrapos_nn) (erule sym)
   400 
   401 lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
   402   by (erule subst, erule ssubst, assumption)
   403 
   404 (*still used in HOLCF*)
   405 lemma rev_contrapos:
   406   assumes pq: "P ==> Q"
   407       and nq: "~Q"
   408   shows "~P"
   409 apply (rule nq [THEN contrapos_nn])
   410 apply (erule pq)
   411 done
   412 
   413 subsubsection {*Existential quantifier*}
   414 
   415 lemma exI: "P x ==> EX x::'a. P x"
   416 apply (unfold Ex_def)
   417 apply (iprover intro: allI allE impI mp)
   418 done
   419 
   420 lemma exE:
   421   assumes major: "EX x::'a. P(x)"
   422       and minor: "!!x. P(x) ==> Q"
   423   shows "Q"
   424 apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
   425 apply (iprover intro: impI [THEN allI] minor)
   426 done
   427 
   428 
   429 subsubsection {*Conjunction*}
   430 
   431 lemma conjI: "[| P; Q |] ==> P&Q"
   432 apply (unfold and_def)
   433 apply (iprover intro: impI [THEN allI] mp)
   434 done
   435 
   436 lemma conjunct1: "[| P & Q |] ==> P"
   437 apply (unfold and_def)
   438 apply (iprover intro: impI dest: spec mp)
   439 done
   440 
   441 lemma conjunct2: "[| P & Q |] ==> Q"
   442 apply (unfold and_def)
   443 apply (iprover intro: impI dest: spec mp)
   444 done
   445 
   446 lemma conjE:
   447   assumes major: "P&Q"
   448       and minor: "[| P; Q |] ==> R"
   449   shows "R"
   450 apply (rule minor)
   451 apply (rule major [THEN conjunct1])
   452 apply (rule major [THEN conjunct2])
   453 done
   454 
   455 lemma context_conjI:
   456   assumes "P" "P ==> Q" shows "P & Q"
   457 by (iprover intro: conjI assms)
   458 
   459 
   460 subsubsection {*Disjunction*}
   461 
   462 lemma disjI1: "P ==> P|Q"
   463 apply (unfold or_def)
   464 apply (iprover intro: allI impI mp)
   465 done
   466 
   467 lemma disjI2: "Q ==> P|Q"
   468 apply (unfold or_def)
   469 apply (iprover intro: allI impI mp)
   470 done
   471 
   472 lemma disjE:
   473   assumes major: "P|Q"
   474       and minorP: "P ==> R"
   475       and minorQ: "Q ==> R"
   476   shows "R"
   477 by (iprover intro: minorP minorQ impI
   478                  major [unfolded or_def, THEN spec, THEN mp, THEN mp])
   479 
   480 
   481 subsubsection {*Classical logic*}
   482 
   483 lemma classical:
   484   assumes prem: "~P ==> P"
   485   shows "P"
   486 apply (rule True_or_False [THEN disjE, THEN eqTrueE])
   487 apply assumption
   488 apply (rule notI [THEN prem, THEN eqTrueI])
   489 apply (erule subst)
   490 apply assumption
   491 done
   492 
   493 lemmas ccontr = FalseE [THEN classical, standard]
   494 
   495 (*notE with premises exchanged; it discharges ~R so that it can be used to
   496   make elimination rules*)
   497 lemma rev_notE:
   498   assumes premp: "P"
   499       and premnot: "~R ==> ~P"
   500   shows "R"
   501 apply (rule ccontr)
   502 apply (erule notE [OF premnot premp])
   503 done
   504 
   505 (*Double negation law*)
   506 lemma notnotD: "~~P ==> P"
   507 apply (rule classical)
   508 apply (erule notE)
   509 apply assumption
   510 done
   511 
   512 lemma contrapos_pp:
   513   assumes p1: "Q"
   514       and p2: "~P ==> ~Q"
   515   shows "P"
   516 by (iprover intro: classical p1 p2 notE)
   517 
   518 
   519 subsubsection {*Unique existence*}
   520 
   521 lemma ex1I:
   522   assumes "P a" "!!x. P(x) ==> x=a"
   523   shows "EX! x. P(x)"
   524 by (unfold Ex1_def, iprover intro: assms exI conjI allI impI)
   525 
   526 text{*Sometimes easier to use: the premises have no shared variables.  Safe!*}
   527 lemma ex_ex1I:
   528   assumes ex_prem: "EX x. P(x)"
   529       and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
   530   shows "EX! x. P(x)"
   531 by (iprover intro: ex_prem [THEN exE] ex1I eq)
   532 
   533 lemma ex1E:
   534   assumes major: "EX! x. P(x)"
   535       and minor: "!!x. [| P(x);  ALL y. P(y) --> y=x |] ==> R"
   536   shows "R"
   537 apply (rule major [unfolded Ex1_def, THEN exE])
   538 apply (erule conjE)
   539 apply (iprover intro: minor)
   540 done
   541 
   542 lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
   543 apply (erule ex1E)
   544 apply (rule exI)
   545 apply assumption
   546 done
   547 
   548 
   549 subsubsection {*THE: definite description operator*}
   550 
   551 lemma the_equality:
   552   assumes prema: "P a"
   553       and premx: "!!x. P x ==> x=a"
   554   shows "(THE x. P x) = a"
   555 apply (rule trans [OF _ the_eq_trivial])
   556 apply (rule_tac f = "The" in arg_cong)
   557 apply (rule ext)
   558 apply (rule iffI)
   559  apply (erule premx)
   560 apply (erule ssubst, rule prema)
   561 done
   562 
   563 lemma theI:
   564   assumes "P a" and "!!x. P x ==> x=a"
   565   shows "P (THE x. P x)"
   566 by (iprover intro: assms the_equality [THEN ssubst])
   567 
   568 lemma theI': "EX! x. P x ==> P (THE x. P x)"
   569 apply (erule ex1E)
   570 apply (erule theI)
   571 apply (erule allE)
   572 apply (erule mp)
   573 apply assumption
   574 done
   575 
   576 (*Easier to apply than theI: only one occurrence of P*)
   577 lemma theI2:
   578   assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
   579   shows "Q (THE x. P x)"
   580 by (iprover intro: assms theI)
   581 
   582 lemma the1I2: assumes "EX! x. P x" "\<And>x. P x \<Longrightarrow> Q x" shows "Q (THE x. P x)"
   583 by(iprover intro:assms(2) theI2[where P=P and Q=Q] ex1E[OF assms(1)]
   584            elim:allE impE)
   585 
   586 lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
   587 apply (rule the_equality)
   588 apply  assumption
   589 apply (erule ex1E)
   590 apply (erule all_dupE)
   591 apply (drule mp)
   592 apply  assumption
   593 apply (erule ssubst)
   594 apply (erule allE)
   595 apply (erule mp)
   596 apply assumption
   597 done
   598 
   599 lemma the_sym_eq_trivial: "(THE y. x=y) = x"
   600 apply (rule the_equality)
   601 apply (rule refl)
   602 apply (erule sym)
   603 done
   604 
   605 
   606 subsubsection {*Classical intro rules for disjunction and existential quantifiers*}
   607 
   608 lemma disjCI:
   609   assumes "~Q ==> P" shows "P|Q"
   610 apply (rule classical)
   611 apply (iprover intro: assms disjI1 disjI2 notI elim: notE)
   612 done
   613 
   614 lemma excluded_middle: "~P | P"
   615 by (iprover intro: disjCI)
   616 
   617 text {*
   618   case distinction as a natural deduction rule.
   619   Note that @{term "~P"} is the second case, not the first
   620 *}
   621 lemma case_split [case_names True False]:
   622   assumes prem1: "P ==> Q"
   623       and prem2: "~P ==> Q"
   624   shows "Q"
   625 apply (rule excluded_middle [THEN disjE])
   626 apply (erule prem2)
   627 apply (erule prem1)
   628 done
   629 
   630 (*Classical implies (-->) elimination. *)
   631 lemma impCE:
   632   assumes major: "P-->Q"
   633       and minor: "~P ==> R" "Q ==> 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 (*This version of --> elimination works on Q before P.  It works best for
   640   those cases in which P holds "almost everywhere".  Can't install as
   641   default: would break old proofs.*)
   642 lemma impCE':
   643   assumes major: "P-->Q"
   644       and minor: "Q ==> R" "~P ==> R"
   645   shows "R"
   646 apply (rule excluded_middle [of P, THEN disjE])
   647 apply (iprover intro: minor major [THEN mp])+
   648 done
   649 
   650 (*Classical <-> elimination. *)
   651 lemma iffCE:
   652   assumes major: "P=Q"
   653       and minor: "[| P; Q |] ==> R"  "[| ~P; ~Q |] ==> R"
   654   shows "R"
   655 apply (rule major [THEN iffE])
   656 apply (iprover intro: minor elim: impCE notE)
   657 done
   658 
   659 lemma exCI:
   660   assumes "ALL x. ~P(x) ==> P(a)"
   661   shows "EX x. P(x)"
   662 apply (rule ccontr)
   663 apply (iprover intro: assms exI allI notI notE [of "\<exists>x. P x"])
   664 done
   665 
   666 
   667 subsubsection {* Intuitionistic Reasoning *}
   668 
   669 lemma impE':
   670   assumes 1: "P --> Q"
   671     and 2: "Q ==> R"
   672     and 3: "P --> Q ==> P"
   673   shows R
   674 proof -
   675   from 3 and 1 have P .
   676   with 1 have Q by (rule impE)
   677   with 2 show R .
   678 qed
   679 
   680 lemma allE':
   681   assumes 1: "ALL x. P x"
   682     and 2: "P x ==> ALL x. P x ==> Q"
   683   shows Q
   684 proof -
   685   from 1 have "P x" by (rule spec)
   686   from this and 1 show Q by (rule 2)
   687 qed
   688 
   689 lemma notE':
   690   assumes 1: "~ P"
   691     and 2: "~ P ==> P"
   692   shows R
   693 proof -
   694   from 2 and 1 have P .
   695   with 1 show R by (rule notE)
   696 qed
   697 
   698 lemma TrueE: "True ==> P ==> P" .
   699 lemma notFalseE: "~ False ==> P ==> P" .
   700 
   701 lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
   702   and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
   703   and [Pure.elim 2] = allE notE' impE'
   704   and [Pure.intro] = exI disjI2 disjI1
   705 
   706 lemmas [trans] = trans
   707   and [sym] = sym not_sym
   708   and [Pure.elim?] = iffD1 iffD2 impE
   709 
   710 use "Tools/hologic.ML"
   711 
   712 
   713 subsubsection {* Atomizing meta-level connectives *}
   714 
   715 axiomatization where
   716   eq_reflection: "x = y \<Longrightarrow> x \<equiv> y" (*admissible axiom*)
   717 
   718 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
   719 proof
   720   assume "!!x. P x"
   721   then show "ALL x. P x" ..
   722 next
   723   assume "ALL x. P x"
   724   then show "!!x. P x" by (rule allE)
   725 qed
   726 
   727 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
   728 proof
   729   assume r: "A ==> B"
   730   show "A --> B" by (rule impI) (rule r)
   731 next
   732   assume "A --> B" and A
   733   then show B by (rule mp)
   734 qed
   735 
   736 lemma atomize_not: "(A ==> False) == Trueprop (~A)"
   737 proof
   738   assume r: "A ==> False"
   739   show "~A" by (rule notI) (rule r)
   740 next
   741   assume "~A" and A
   742   then show False by (rule notE)
   743 qed
   744 
   745 lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
   746 proof
   747   assume "x == y"
   748   show "x = y" by (unfold `x == y`) (rule refl)
   749 next
   750   assume "x = y"
   751   then show "x == y" by (rule eq_reflection)
   752 qed
   753 
   754 lemma atomize_conj [atomize]: "(A &&& B) == Trueprop (A & B)"
   755 proof
   756   assume conj: "A &&& B"
   757   show "A & B"
   758   proof (rule conjI)
   759     from conj show A by (rule conjunctionD1)
   760     from conj show B by (rule conjunctionD2)
   761   qed
   762 next
   763   assume conj: "A & B"
   764   show "A &&& B"
   765   proof -
   766     from conj show A ..
   767     from conj show B ..
   768   qed
   769 qed
   770 
   771 lemmas [symmetric, rulify] = atomize_all atomize_imp
   772   and [symmetric, defn] = atomize_all atomize_imp atomize_eq
   773 
   774 
   775 subsubsection {* Atomizing elimination rules *}
   776 
   777 setup AtomizeElim.setup
   778 
   779 lemma atomize_exL[atomize_elim]: "(!!x. P x ==> Q) == ((EX x. P x) ==> Q)"
   780   by rule iprover+
   781 
   782 lemma atomize_conjL[atomize_elim]: "(A ==> B ==> C) == (A & B ==> C)"
   783   by rule iprover+
   784 
   785 lemma atomize_disjL[atomize_elim]: "((A ==> C) ==> (B ==> C) ==> C) == ((A | B ==> C) ==> C)"
   786   by rule iprover+
   787 
   788 lemma atomize_elimL[atomize_elim]: "(!!B. (A ==> B) ==> B) == Trueprop A" ..
   789 
   790 
   791 subsection {* Package setup *}
   792 
   793 subsubsection {* Classical Reasoner setup *}
   794 
   795 lemma imp_elim: "P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R"
   796   by (rule classical) iprover
   797 
   798 lemma swap: "~ P ==> (~ R ==> P) ==> R"
   799   by (rule classical) iprover
   800 
   801 lemma thin_refl:
   802   "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
   803 
   804 ML {*
   805 structure Hypsubst = HypsubstFun(
   806 struct
   807   structure Simplifier = Simplifier
   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   val prop_subst = @{lemma "PROP P t ==> PROP prop (x = t ==> PROP P x)"
   819                      by (unfold prop_def) (drule eq_reflection, unfold)}
   820 end);
   821 open Hypsubst;
   822 
   823 structure Classical = ClassicalFun(
   824 struct
   825   val imp_elim = @{thm imp_elim}
   826   val not_elim = @{thm notE}
   827   val swap = @{thm swap}
   828   val classical = @{thm classical}
   829   val sizef = Drule.size_of_thm
   830   val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
   831 end);
   832 
   833 structure BasicClassical: BASIC_CLASSICAL = Classical; 
   834 open BasicClassical;
   835 
   836 ML_Antiquote.value "claset"
   837   (Scan.succeed "Classical.claset_of (ML_Context.the_local_context ())");
   838 
   839 structure ResAtpset = Named_Thms
   840   (val name = "atp" val description = "ATP rules");
   841 
   842 structure ResBlacklist = Named_Thms
   843   (val name = "noatp" val description = "theorems blacklisted for ATP");
   844 *}
   845 
   846 text {*ResBlacklist holds theorems blacklisted to sledgehammer. 
   847   These theorems typically produce clauses that are prolific (match too many equality or
   848   membership literals) and relate to seldom-used facts. Some duplicate other rules.*}
   849 
   850 setup {*
   851 let
   852   (*prevent substitution on bool*)
   853   fun hyp_subst_tac' i thm = if i <= Thm.nprems_of thm andalso
   854     Term.exists_Const (fn ("op =", Type (_, [T, _])) => T <> Type ("bool", []) | _ => false)
   855       (nth (Thm.prems_of thm) (i - 1)) then Hypsubst.hyp_subst_tac i thm else no_tac thm;
   856 in
   857   Hypsubst.hypsubst_setup
   858   #> ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac)
   859   #> Classical.setup
   860   #> ResAtpset.setup
   861   #> ResBlacklist.setup
   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   and conjE [elim!]
   879 
   880 declare ex_ex1I [intro!]
   881   and allI [intro!]
   882   and the_equality [intro]
   883   and exI [intro]
   884 
   885 declare exE [elim!]
   886   allE [elim]
   887 
   888 ML {* val HOL_cs = @{claset} *}
   889 
   890 lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
   891   apply (erule swap)
   892   apply (erule (1) meta_mp)
   893   done
   894 
   895 declare ex_ex1I [rule del, intro! 2]
   896   and ex1I [intro]
   897 
   898 lemmas [intro?] = ext
   899   and [elim?] = ex1_implies_ex
   900 
   901 (*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
   902 lemma alt_ex1E [elim!]:
   903   assumes major: "\<exists>!x. P x"
   904       and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
   905   shows R
   906 apply (rule ex1E [OF major])
   907 apply (rule prem)
   908 apply (tactic {* ares_tac @{thms allI} 1 *})+
   909 apply (tactic {* etac (Classical.dup_elim @{thm allE}) 1 *})
   910 apply iprover
   911 done
   912 
   913 ML {*
   914 structure Blast = Blast
   915 (
   916   val thy = @{theory}
   917   type claset = Classical.claset
   918   val equality_name = @{const_name "op ="}
   919   val not_name = @{const_name Not}
   920   val notE = @{thm notE}
   921   val ccontr = @{thm ccontr}
   922   val contr_tac = Classical.contr_tac
   923   val dup_intr = Classical.dup_intr
   924   val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
   925   val rep_cs = Classical.rep_cs
   926   val cla_modifiers = Classical.cla_modifiers
   927   val cla_meth' = Classical.cla_meth'
   928 );
   929 val blast_tac = Blast.blast_tac;
   930 *}
   931 
   932 setup Blast.setup
   933 
   934 
   935 subsubsection {* Simplifier *}
   936 
   937 lemma eta_contract_eq: "(%s. f s) = f" ..
   938 
   939 lemma simp_thms:
   940   shows not_not: "(~ ~ P) = P"
   941   and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
   942   and
   943     "(P ~= Q) = (P = (~Q))"
   944     "(P | ~P) = True"    "(~P | P) = True"
   945     "(x = x) = True"
   946   and not_True_eq_False [code]: "(\<not> True) = False"
   947   and not_False_eq_True [code]: "(\<not> False) = True"
   948   and
   949     "(~P) ~= P"  "P ~= (~P)"
   950     "(True=P) = P"
   951   and eq_True: "(P = True) = P"
   952   and "(False=P) = (~P)"
   953   and eq_False: "(P = False) = (\<not> P)"
   954   and
   955     "(True --> P) = P"  "(False --> P) = True"
   956     "(P --> True) = True"  "(P --> P) = True"
   957     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
   958     "(P & True) = P"  "(True & P) = P"
   959     "(P & False) = False"  "(False & P) = False"
   960     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
   961     "(P & ~P) = False"    "(~P & P) = False"
   962     "(P | True) = True"  "(True | P) = True"
   963     "(P | False) = P"  "(False | P) = P"
   964     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
   965     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
   966   and
   967     "!!P. (EX x. x=t & P(x)) = P(t)"
   968     "!!P. (EX x. t=x & P(x)) = P(t)"
   969     "!!P. (ALL x. x=t --> P(x)) = P(t)"
   970     "!!P. (ALL x. t=x --> P(x)) = P(t)"
   971   by (blast, blast, blast, blast, blast, iprover+)
   972 
   973 lemma disj_absorb: "(A | A) = A"
   974   by blast
   975 
   976 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
   977   by blast
   978 
   979 lemma conj_absorb: "(A & A) = A"
   980   by blast
   981 
   982 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
   983   by blast
   984 
   985 lemma eq_ac:
   986   shows eq_commute: "(a=b) = (b=a)"
   987     and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
   988     and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
   989 lemma neq_commute: "(a~=b) = (b~=a)" by iprover
   990 
   991 lemma conj_comms:
   992   shows conj_commute: "(P&Q) = (Q&P)"
   993     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
   994 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
   995 
   996 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
   997 
   998 lemma disj_comms:
   999   shows disj_commute: "(P|Q) = (Q|P)"
  1000     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
  1001 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
  1002 
  1003 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
  1004 
  1005 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
  1006 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
  1007 
  1008 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
  1009 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
  1010 
  1011 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
  1012 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
  1013 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
  1014 
  1015 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
  1016 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
  1017 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
  1018 
  1019 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
  1020 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
  1021 
  1022 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
  1023   by iprover
  1024 
  1025 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
  1026 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
  1027 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
  1028 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
  1029 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
  1030 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
  1031   by blast
  1032 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
  1033 
  1034 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
  1035 
  1036 
  1037 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
  1038   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
  1039   -- {* cases boil down to the same thing. *}
  1040   by blast
  1041 
  1042 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
  1043 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
  1044 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
  1045 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
  1046 lemma all_not_ex: "(ALL x. P x) = (~ (EX x. ~ P x ))" by blast
  1047 
  1048 declare All_def [noatp]
  1049 
  1050 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
  1051 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
  1052 
  1053 text {*
  1054   \medskip The @{text "&"} congruence rule: not included by default!
  1055   May slow rewrite proofs down by as much as 50\% *}
  1056 
  1057 lemma conj_cong:
  1058     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
  1059   by iprover
  1060 
  1061 lemma rev_conj_cong:
  1062     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
  1063   by iprover
  1064 
  1065 text {* The @{text "|"} congruence rule: not included by default! *}
  1066 
  1067 lemma disj_cong:
  1068     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
  1069   by blast
  1070 
  1071 
  1072 text {* \medskip if-then-else rules *}
  1073 
  1074 lemma if_True [code]: "(if True then x else y) = x"
  1075   by (unfold if_def) blast
  1076 
  1077 lemma if_False [code]: "(if False then x else y) = y"
  1078   by (unfold if_def) blast
  1079 
  1080 lemma if_P: "P ==> (if P then x else y) = x"
  1081   by (unfold if_def) blast
  1082 
  1083 lemma if_not_P: "~P ==> (if P then x else y) = y"
  1084   by (unfold if_def) blast
  1085 
  1086 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
  1087   apply (rule case_split [of Q])
  1088    apply (simplesubst if_P)
  1089     prefer 3 apply (simplesubst if_not_P, blast+)
  1090   done
  1091 
  1092 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
  1093 by (simplesubst split_if, blast)
  1094 
  1095 lemmas if_splits [noatp] = split_if split_if_asm
  1096 
  1097 lemma if_cancel: "(if c then x else x) = x"
  1098 by (simplesubst split_if, blast)
  1099 
  1100 lemma if_eq_cancel: "(if x = y then y else x) = x"
  1101 by (simplesubst split_if, blast)
  1102 
  1103 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
  1104   -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
  1105   by (rule split_if)
  1106 
  1107 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
  1108   -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
  1109   apply (simplesubst split_if, blast)
  1110   done
  1111 
  1112 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
  1113 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
  1114 
  1115 text {* \medskip let rules for simproc *}
  1116 
  1117 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
  1118   by (unfold Let_def)
  1119 
  1120 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
  1121   by (unfold Let_def)
  1122 
  1123 text {*
  1124   The following copy of the implication operator is useful for
  1125   fine-tuning congruence rules.  It instructs the simplifier to simplify
  1126   its premise.
  1127 *}
  1128 
  1129 constdefs
  1130   simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1)
  1131   [code del]: "simp_implies \<equiv> op ==>"
  1132 
  1133 lemma simp_impliesI:
  1134   assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
  1135   shows "PROP P =simp=> PROP Q"
  1136   apply (unfold simp_implies_def)
  1137   apply (rule PQ)
  1138   apply assumption
  1139   done
  1140 
  1141 lemma simp_impliesE:
  1142   assumes PQ: "PROP P =simp=> PROP Q"
  1143   and P: "PROP P"
  1144   and QR: "PROP Q \<Longrightarrow> PROP R"
  1145   shows "PROP R"
  1146   apply (rule QR)
  1147   apply (rule PQ [unfolded simp_implies_def])
  1148   apply (rule P)
  1149   done
  1150 
  1151 lemma simp_implies_cong:
  1152   assumes PP' :"PROP P == PROP P'"
  1153   and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
  1154   shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
  1155 proof (unfold simp_implies_def, rule equal_intr_rule)
  1156   assume PQ: "PROP P \<Longrightarrow> PROP Q"
  1157   and P': "PROP P'"
  1158   from PP' [symmetric] and P' have "PROP P"
  1159     by (rule equal_elim_rule1)
  1160   then have "PROP Q" by (rule PQ)
  1161   with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
  1162 next
  1163   assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
  1164   and P: "PROP P"
  1165   from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
  1166   then have "PROP Q'" by (rule P'Q')
  1167   with P'QQ' [OF P', symmetric] show "PROP Q"
  1168     by (rule equal_elim_rule1)
  1169 qed
  1170 
  1171 lemma uncurry:
  1172   assumes "P \<longrightarrow> Q \<longrightarrow> R"
  1173   shows "P \<and> Q \<longrightarrow> R"
  1174   using assms by blast
  1175 
  1176 lemma iff_allI:
  1177   assumes "\<And>x. P x = Q x"
  1178   shows "(\<forall>x. P x) = (\<forall>x. Q x)"
  1179   using assms by blast
  1180 
  1181 lemma iff_exI:
  1182   assumes "\<And>x. P x = Q x"
  1183   shows "(\<exists>x. P x) = (\<exists>x. Q x)"
  1184   using assms by blast
  1185 
  1186 lemma all_comm:
  1187   "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
  1188   by blast
  1189 
  1190 lemma ex_comm:
  1191   "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
  1192   by blast
  1193 
  1194 use "Tools/simpdata.ML"
  1195 ML {* open Simpdata *}
  1196 
  1197 setup {*
  1198   Simplifier.method_setup Splitter.split_modifiers
  1199   #> Simplifier.map_simpset (K Simpdata.simpset_simprocs)
  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 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 = ProofContext.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.capply 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 = symmetric (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 [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   unfolding if_def 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 {* This lemma restricts the effect of the rewrite rule u=v to the left-hand
  1383   side of an equality.  Used in @{text "{Integ,Real}/simproc.ML"} *}
  1384 lemma restrict_to_left:
  1385   assumes "x = y"
  1386   shows "(x = z) = (y = z)"
  1387   using assms by simp
  1388 
  1389 
  1390 subsubsection {* Generic cases and induction *}
  1391 
  1392 text {* Rule projections: *}
  1393 
  1394 ML {*
  1395 structure Project_Rule = Project_Rule
  1396 (
  1397   val conjunct1 = @{thm conjunct1}
  1398   val conjunct2 = @{thm conjunct2}
  1399   val mp = @{thm mp}
  1400 )
  1401 *}
  1402 
  1403 constdefs
  1404   induct_forall where "induct_forall P == \<forall>x. P x"
  1405   induct_implies where "induct_implies A B == A \<longrightarrow> B"
  1406   induct_equal where "induct_equal x y == x = y"
  1407   induct_conj where "induct_conj A B == A \<and> B"
  1408 
  1409 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
  1410   by (unfold atomize_all induct_forall_def)
  1411 
  1412 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
  1413   by (unfold atomize_imp induct_implies_def)
  1414 
  1415 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
  1416   by (unfold atomize_eq induct_equal_def)
  1417 
  1418 lemma induct_conj_eq: "(A &&& B) == Trueprop (induct_conj A B)"
  1419   by (unfold atomize_conj induct_conj_def)
  1420 
  1421 lemmas induct_atomize = induct_forall_eq induct_implies_eq induct_equal_eq induct_conj_eq
  1422 lemmas induct_rulify [symmetric, standard] = induct_atomize
  1423 lemmas induct_rulify_fallback =
  1424   induct_forall_def induct_implies_def induct_equal_def induct_conj_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 hide const induct_forall induct_implies induct_equal induct_conj
  1447 
  1448 text {* Method setup. *}
  1449 
  1450 ML {*
  1451 structure Induct = Induct
  1452 (
  1453   val cases_default = @{thm case_split}
  1454   val atomize = @{thms induct_atomize}
  1455   val rulify = @{thms induct_rulify}
  1456   val rulify_fallback = @{thms induct_rulify_fallback}
  1457 )
  1458 *}
  1459 
  1460 setup Induct.setup
  1461 
  1462 use "~~/src/Tools/induct_tacs.ML"
  1463 setup InductTacs.setup
  1464 
  1465 
  1466 subsubsection {* Coherent logic *}
  1467 
  1468 ML {*
  1469 structure Coherent = CoherentFun
  1470 (
  1471   val atomize_elimL = @{thm atomize_elimL}
  1472   val atomize_exL = @{thm atomize_exL}
  1473   val atomize_conjL = @{thm atomize_conjL}
  1474   val atomize_disjL = @{thm atomize_disjL}
  1475   val operator_names =
  1476     [@{const_name "op |"}, @{const_name "op &"}, @{const_name "Ex"}]
  1477 );
  1478 *}
  1479 
  1480 setup Coherent.setup
  1481 
  1482 
  1483 subsubsection {* Reorienting equalities *}
  1484 
  1485 ML {*
  1486 signature REORIENT_PROC =
  1487 sig
  1488   val init : theory -> theory
  1489   val add : (term -> bool) -> theory -> theory
  1490   val proc : morphism -> simpset -> cterm -> thm option
  1491 end;
  1492 
  1493 structure ReorientProc : REORIENT_PROC =
  1494 struct
  1495   structure Data = TheoryDataFun
  1496   (
  1497     type T = term -> bool;
  1498     val empty = (fn _ => false);
  1499     val copy = I;
  1500     val extend = I;
  1501     fun merge _ (m1, m2) = (fn t => m1 t orelse m2 t);
  1502   )
  1503 
  1504   val init = Data.init;
  1505   fun add m = Data.map (fn matches => fn t => matches t orelse m t);
  1506   val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
  1507   fun proc phi ss ct =
  1508     let
  1509       val ctxt = Simplifier.the_context ss;
  1510       val thy = ProofContext.theory_of ctxt;
  1511       val matches = Data.get thy;
  1512     in
  1513       case Thm.term_of ct of
  1514         (_ $ t $ u) => if matches u then NONE else SOME meta_reorient
  1515       | _ => NONE
  1516     end;
  1517 end;
  1518 *}
  1519 
  1520 setup ReorientProc.init
  1521 
  1522 
  1523 subsection {* Other simple lemmas and lemma duplicates *}
  1524 
  1525 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
  1526   by blast+
  1527 
  1528 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
  1529   apply (rule iffI)
  1530   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
  1531   apply (fast dest!: theI')
  1532   apply (fast intro: ext the1_equality [symmetric])
  1533   apply (erule ex1E)
  1534   apply (rule allI)
  1535   apply (rule ex1I)
  1536   apply (erule spec)
  1537   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
  1538   apply (erule impE)
  1539   apply (rule allI)
  1540   apply (case_tac "xa = x")
  1541   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1542   done
  1543 
  1544 lemmas eq_sym_conv = eq_commute
  1545 
  1546 lemma nnf_simps:
  1547   "(\<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)" 
  1548   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))" 
  1549   "(\<not> \<not>(P)) = P"
  1550 by blast+
  1551 
  1552 
  1553 subsection {* Generic classes and algebraic operations *}
  1554 
  1555 class zero = 
  1556   fixes zero :: 'a  ("0")
  1557 
  1558 class one =
  1559   fixes one  :: 'a  ("1")
  1560 
  1561 lemma Let_0 [simp]: "Let 0 f = f 0"
  1562   unfolding Let_def ..
  1563 
  1564 lemma Let_1 [simp]: "Let 1 f = f 1"
  1565   unfolding Let_def ..
  1566 
  1567 setup {*
  1568   ReorientProc.add
  1569     (fn Const(@{const_name HOL.zero}, _) => true
  1570       | Const(@{const_name HOL.one}, _) => true
  1571       | _ => false)
  1572 *}
  1573 
  1574 simproc_setup reorient_zero ("0 = x") = ReorientProc.proc
  1575 simproc_setup reorient_one ("1 = x") = ReorientProc.proc
  1576 
  1577 typed_print_translation {*
  1578 let
  1579   fun tr' c = (c, fn show_sorts => fn T => fn ts =>
  1580     if (not o null) ts orelse T = dummyT
  1581       orelse not (! show_types) andalso can Term.dest_Type T
  1582     then raise Match
  1583     else Syntax.const Syntax.constrainC $ Syntax.const c $ Syntax.term_of_typ show_sorts T);
  1584 in map tr' [@{const_syntax HOL.one}, @{const_syntax HOL.zero}] end;
  1585 *} -- {* show types that are presumably too general *}
  1586 
  1587 hide (open) const zero one
  1588 
  1589 class plus =
  1590   fixes plus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "+" 65)
  1591 
  1592 class minus =
  1593   fixes minus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "-" 65)
  1594 
  1595 class uminus =
  1596   fixes uminus :: "'a \<Rightarrow> 'a"  ("- _" [81] 80)
  1597 
  1598 class times =
  1599   fixes times :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "*" 70)
  1600 
  1601 class inverse =
  1602   fixes inverse :: "'a \<Rightarrow> 'a"
  1603     and divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "'/" 70)
  1604 
  1605 class abs =
  1606   fixes abs :: "'a \<Rightarrow> 'a"
  1607 begin
  1608 
  1609 notation (xsymbols)
  1610   abs  ("\<bar>_\<bar>")
  1611 
  1612 notation (HTML output)
  1613   abs  ("\<bar>_\<bar>")
  1614 
  1615 end
  1616 
  1617 class sgn =
  1618   fixes sgn :: "'a \<Rightarrow> 'a"
  1619 
  1620 class ord =
  1621   fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1622     and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1623 begin
  1624 
  1625 notation
  1626   less_eq  ("op <=") and
  1627   less_eq  ("(_/ <= _)" [51, 51] 50) and
  1628   less  ("op <") and
  1629   less  ("(_/ < _)"  [51, 51] 50)
  1630   
  1631 notation (xsymbols)
  1632   less_eq  ("op \<le>") and
  1633   less_eq  ("(_/ \<le> _)"  [51, 51] 50)
  1634 
  1635 notation (HTML output)
  1636   less_eq  ("op \<le>") and
  1637   less_eq  ("(_/ \<le> _)"  [51, 51] 50)
  1638 
  1639 abbreviation (input)
  1640   greater_eq  (infix ">=" 50) where
  1641   "x >= y \<equiv> y <= x"
  1642 
  1643 notation (input)
  1644   greater_eq  (infix "\<ge>" 50)
  1645 
  1646 abbreviation (input)
  1647   greater  (infix ">" 50) where
  1648   "x > y \<equiv> y < x"
  1649 
  1650 end
  1651 
  1652 syntax
  1653   "_index1"  :: index    ("\<^sub>1")
  1654 translations
  1655   (index) "\<^sub>1" => (index) "\<^bsub>\<struct>\<^esub>"
  1656 
  1657 lemma mk_left_commute:
  1658   fixes f (infix "\<otimes>" 60)
  1659   assumes a: "\<And>x y z. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)" and
  1660           c: "\<And>x y. x \<otimes> y = y \<otimes> x"
  1661   shows "x \<otimes> (y \<otimes> z) = y \<otimes> (x \<otimes> z)"
  1662   by (rule trans [OF trans [OF c a] arg_cong [OF c, of "f y"]])
  1663 
  1664 
  1665 subsection {* Basic ML bindings *}
  1666 
  1667 ML {*
  1668 val FalseE = @{thm FalseE}
  1669 val Let_def = @{thm Let_def}
  1670 val TrueI = @{thm TrueI}
  1671 val allE = @{thm allE}
  1672 val allI = @{thm allI}
  1673 val all_dupE = @{thm all_dupE}
  1674 val arg_cong = @{thm arg_cong}
  1675 val box_equals = @{thm box_equals}
  1676 val ccontr = @{thm ccontr}
  1677 val classical = @{thm classical}
  1678 val conjE = @{thm conjE}
  1679 val conjI = @{thm conjI}
  1680 val conjunct1 = @{thm conjunct1}
  1681 val conjunct2 = @{thm conjunct2}
  1682 val disjCI = @{thm disjCI}
  1683 val disjE = @{thm disjE}
  1684 val disjI1 = @{thm disjI1}
  1685 val disjI2 = @{thm disjI2}
  1686 val eq_reflection = @{thm eq_reflection}
  1687 val ex1E = @{thm ex1E}
  1688 val ex1I = @{thm ex1I}
  1689 val ex1_implies_ex = @{thm ex1_implies_ex}
  1690 val exE = @{thm exE}
  1691 val exI = @{thm exI}
  1692 val excluded_middle = @{thm excluded_middle}
  1693 val ext = @{thm ext}
  1694 val fun_cong = @{thm fun_cong}
  1695 val iffD1 = @{thm iffD1}
  1696 val iffD2 = @{thm iffD2}
  1697 val iffI = @{thm iffI}
  1698 val impE = @{thm impE}
  1699 val impI = @{thm impI}
  1700 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1701 val mp = @{thm mp}
  1702 val notE = @{thm notE}
  1703 val notI = @{thm notI}
  1704 val not_all = @{thm not_all}
  1705 val not_ex = @{thm not_ex}
  1706 val not_iff = @{thm not_iff}
  1707 val not_not = @{thm not_not}
  1708 val not_sym = @{thm not_sym}
  1709 val refl = @{thm refl}
  1710 val rev_mp = @{thm rev_mp}
  1711 val spec = @{thm spec}
  1712 val ssubst = @{thm ssubst}
  1713 val subst = @{thm subst}
  1714 val sym = @{thm sym}
  1715 val trans = @{thm trans}
  1716 *}
  1717 
  1718 
  1719 subsection {* Code generator setup *}
  1720 
  1721 subsubsection {* SML code generator setup *}
  1722 
  1723 use "Tools/recfun_codegen.ML"
  1724 
  1725 setup {*
  1726   Codegen.setup
  1727   #> RecfunCodegen.setup
  1728   #> Codegen.map_unfold (K HOL_basic_ss)
  1729 *}
  1730 
  1731 types_code
  1732   "bool"  ("bool")
  1733 attach (term_of) {*
  1734 fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
  1735 *}
  1736 attach (test) {*
  1737 fun gen_bool i =
  1738   let val b = one_of [false, true]
  1739   in (b, fn () => term_of_bool b) end;
  1740 *}
  1741   "prop"  ("bool")
  1742 attach (term_of) {*
  1743 fun term_of_prop b =
  1744   HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
  1745 *}
  1746 
  1747 consts_code
  1748   "Trueprop" ("(_)")
  1749   "True"    ("true")
  1750   "False"   ("false")
  1751   "Not"     ("Bool.not")
  1752   "op |"    ("(_ orelse/ _)")
  1753   "op &"    ("(_ andalso/ _)")
  1754   "If"      ("(if _/ then _/ else _)")
  1755 
  1756 setup {*
  1757 let
  1758 
  1759 fun eq_codegen thy defs dep thyname b t gr =
  1760     (case strip_comb t of
  1761        (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
  1762      | (Const ("op =", _), [t, u]) =>
  1763           let
  1764             val (pt, gr') = Codegen.invoke_codegen thy defs dep thyname false t gr;
  1765             val (pu, gr'') = Codegen.invoke_codegen thy defs dep thyname false u gr';
  1766             val (_, gr''') = Codegen.invoke_tycodegen thy defs dep thyname false HOLogic.boolT gr'';
  1767           in
  1768             SOME (Codegen.parens
  1769               (Pretty.block [pt, Codegen.str " =", Pretty.brk 1, pu]), gr''')
  1770           end
  1771      | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
  1772          thy defs dep thyname b (Codegen.eta_expand t ts 2) gr)
  1773      | _ => NONE);
  1774 
  1775 in
  1776   Codegen.add_codegen "eq_codegen" eq_codegen
  1777 end
  1778 *}
  1779 
  1780 subsubsection {* Generic code generator preprocessor setup *}
  1781 
  1782 setup {*
  1783   Code_Preproc.map_pre (K HOL_basic_ss)
  1784   #> Code_Preproc.map_post (K HOL_basic_ss)
  1785 *}
  1786 
  1787 subsubsection {* Equality *}
  1788 
  1789 class eq =
  1790   fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1791   assumes eq_equals: "eq x y \<longleftrightarrow> x = y"
  1792 begin
  1793 
  1794 lemma eq [code_unfold, code_inline del]: "eq = (op =)"
  1795   by (rule ext eq_equals)+
  1796 
  1797 lemma eq_refl: "eq x x \<longleftrightarrow> True"
  1798   unfolding eq by rule+
  1799 
  1800 lemma equals_eq: "(op =) \<equiv> eq"
  1801   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule eq_equals)
  1802 
  1803 declare equals_eq [symmetric, code_post]
  1804 
  1805 end
  1806 
  1807 declare equals_eq [code]
  1808 
  1809 setup {*
  1810   Code_Preproc.map_pre (fn simpset =>
  1811     simpset addsimprocs [Simplifier.simproc_i @{theory} "eq" [@{term "op ="}]
  1812       (fn thy => fn _ => fn t as Const (_, T) => case strip_type T
  1813         of ((T as Type _) :: _, _) => SOME @{thm equals_eq}
  1814          | _ => NONE)])
  1815 *}
  1816 
  1817 
  1818 subsubsection {* Generic code generator foundation *}
  1819 
  1820 text {* Datatypes *}
  1821 
  1822 code_datatype True False
  1823 
  1824 code_datatype "TYPE('a\<Colon>{})"
  1825 
  1826 code_datatype Trueprop "prop"
  1827 
  1828 text {* Code equations *}
  1829 
  1830 lemma [code]:
  1831   shows "(True \<Longrightarrow> PROP P) \<equiv> PROP P" 
  1832     and "(False \<Longrightarrow> Q) \<equiv> Trueprop True" 
  1833     and "(PROP P \<Longrightarrow> True) \<equiv> Trueprop True" 
  1834     and "(Q \<Longrightarrow> False) \<equiv> Trueprop (\<not> Q)" by (auto intro!: equal_intr_rule)
  1835 
  1836 lemma [code]:
  1837   shows "False \<and> x \<longleftrightarrow> False"
  1838     and "True \<and> x \<longleftrightarrow> x"
  1839     and "x \<and> False \<longleftrightarrow> False"
  1840     and "x \<and> True \<longleftrightarrow> x" by simp_all
  1841 
  1842 lemma [code]:
  1843   shows "False \<or> x \<longleftrightarrow> x"
  1844     and "True \<or> x \<longleftrightarrow> True"
  1845     and "x \<or> False \<longleftrightarrow> x"
  1846     and "x \<or> True \<longleftrightarrow> True" by simp_all
  1847 
  1848 declare imp_conv_disj [code, code_unfold_post]
  1849 
  1850 instantiation itself :: (type) eq
  1851 begin
  1852 
  1853 definition eq_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
  1854   "eq_itself x y \<longleftrightarrow> x = y"
  1855 
  1856 instance proof
  1857 qed (fact eq_itself_def)
  1858 
  1859 end
  1860 
  1861 lemma eq_itself_code [code]:
  1862   "eq_class.eq TYPE('a) TYPE('a) \<longleftrightarrow> True"
  1863   by (simp add: eq)
  1864 
  1865 text {* Equality *}
  1866 
  1867 declare simp_thms(6) [code nbe]
  1868 
  1869 setup {*
  1870   Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"})
  1871 *}
  1872 
  1873 lemma equals_alias_cert: "OFCLASS('a, eq_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> eq)" (is "?ofclass \<equiv> ?eq")
  1874 proof
  1875   assume "PROP ?ofclass"
  1876   show "PROP ?eq"
  1877     by (tactic {* ALLGOALS (rtac (Drule.unconstrainTs @{thm equals_eq})) *}) 
  1878       (fact `PROP ?ofclass`)
  1879 next
  1880   assume "PROP ?eq"
  1881   show "PROP ?ofclass" proof
  1882   qed (simp add: `PROP ?eq`)
  1883 qed
  1884   
  1885 setup {*
  1886   Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>eq \<Rightarrow> 'a \<Rightarrow> bool"})
  1887 *}
  1888 
  1889 setup {*
  1890   Code.add_const_alias @{thm equals_alias_cert}
  1891 *}
  1892 
  1893 hide (open) const eq
  1894 hide const eq
  1895 
  1896 text {* Cases *}
  1897 
  1898 lemma Let_case_cert:
  1899   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
  1900   shows "CASE x \<equiv> f x"
  1901   using assms by simp_all
  1902 
  1903 lemma If_case_cert:
  1904   assumes "CASE \<equiv> (\<lambda>b. If b f g)"
  1905   shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
  1906   using assms by simp_all
  1907 
  1908 setup {*
  1909   Code.add_case @{thm Let_case_cert}
  1910   #> Code.add_case @{thm If_case_cert}
  1911   #> Code.add_undefined @{const_name undefined}
  1912 *}
  1913 
  1914 code_abort undefined
  1915 
  1916 subsubsection {* Generic code generator target languages *}
  1917 
  1918 text {* type bool *}
  1919 
  1920 code_type bool
  1921   (SML "bool")
  1922   (OCaml "bool")
  1923   (Haskell "Bool")
  1924 
  1925 code_const True and False and Not and "op &" and "op |" and If
  1926   (SML "true" and "false" and "not"
  1927     and infixl 1 "andalso" and infixl 0 "orelse"
  1928     and "!(if (_)/ then (_)/ else (_))")
  1929   (OCaml "true" and "false" and "not"
  1930     and infixl 4 "&&" and infixl 2 "||"
  1931     and "!(if (_)/ then (_)/ else (_))")
  1932   (Haskell "True" and "False" and "not"
  1933     and infixl 3 "&&" and infixl 2 "||"
  1934     and "!(if (_)/ then (_)/ else (_))")
  1935 
  1936 code_reserved SML
  1937   bool true false not
  1938 
  1939 code_reserved OCaml
  1940   bool not
  1941 
  1942 text {* using built-in Haskell equality *}
  1943 
  1944 code_class eq
  1945   (Haskell "Eq")
  1946 
  1947 code_const "eq_class.eq"
  1948   (Haskell infixl 4 "==")
  1949 
  1950 code_const "op ="
  1951   (Haskell infixl 4 "==")
  1952 
  1953 text {* undefined *}
  1954 
  1955 code_const undefined
  1956   (SML "!(raise/ Fail/ \"undefined\")")
  1957   (OCaml "failwith/ \"undefined\"")
  1958   (Haskell "error/ \"undefined\"")
  1959 
  1960 subsubsection {* Evaluation and normalization by evaluation *}
  1961 
  1962 setup {*
  1963   Value.add_evaluator ("SML", Codegen.eval_term o ProofContext.theory_of)
  1964 *}
  1965 
  1966 ML {*
  1967 structure Eval_Method =
  1968 struct
  1969 
  1970 val eval_ref : (unit -> bool) option ref = ref NONE;
  1971 
  1972 end;
  1973 *}
  1974 
  1975 oracle eval_oracle = {* fn ct =>
  1976   let
  1977     val thy = Thm.theory_of_cterm ct;
  1978     val t = Thm.term_of ct;
  1979     val dummy = @{cprop True};
  1980   in case try HOLogic.dest_Trueprop t
  1981    of SOME t' => if Code_ML.eval NONE
  1982          ("Eval_Method.eval_ref", Eval_Method.eval_ref) (K I) thy t' [] 
  1983        then Thm.capply (Thm.capply @{cterm "op \<equiv> \<Colon> prop \<Rightarrow> prop \<Rightarrow> prop"} ct) dummy
  1984        else dummy
  1985     | NONE => dummy
  1986   end
  1987 *}
  1988 
  1989 ML {*
  1990 fun gen_eval_method conv ctxt = SIMPLE_METHOD'
  1991   (CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
  1992     THEN' rtac TrueI)
  1993 *}
  1994 
  1995 method_setup eval = {* Scan.succeed (gen_eval_method eval_oracle) *}
  1996   "solve goal by evaluation"
  1997 
  1998 method_setup evaluation = {* Scan.succeed (gen_eval_method Codegen.evaluation_conv) *}
  1999   "solve goal by evaluation"
  2000 
  2001 method_setup normalization = {*
  2002   Scan.succeed (K (SIMPLE_METHOD' (CONVERSION Nbe.norm_conv THEN' (fn k => TRY (rtac TrueI k)))))
  2003 *} "solve goal by normalization"
  2004 
  2005 
  2006 subsubsection {* Quickcheck *}
  2007 
  2008 ML {*
  2009 structure Quickcheck_RecFun_Simps = Named_Thms
  2010 (
  2011   val name = "quickcheck_recfun_simp"
  2012   val description = "simplification rules of recursive functions as needed by Quickcheck"
  2013 )
  2014 *}
  2015 
  2016 setup Quickcheck_RecFun_Simps.setup
  2017 
  2018 setup {*
  2019   Quickcheck.add_generator ("SML", Codegen.test_term)
  2020 *}
  2021 
  2022 quickcheck_params [size = 5, iterations = 50]
  2023 
  2024 
  2025 subsection {* Nitpick setup *}
  2026 
  2027 text {* This will be relocated once Nitpick is moved to HOL. *}
  2028 
  2029 ML {*
  2030 structure Nitpick_Const_Defs = Named_Thms
  2031 (
  2032   val name = "nitpick_const_def"
  2033   val description = "alternative definitions of constants as needed by Nitpick"
  2034 )
  2035 structure Nitpick_Const_Simps = Named_Thms
  2036 (
  2037   val name = "nitpick_const_simp"
  2038   val description = "equational specification of constants as needed by Nitpick"
  2039 )
  2040 structure Nitpick_Const_Psimps = Named_Thms
  2041 (
  2042   val name = "nitpick_const_psimp"
  2043   val description = "partial equational specification of constants as needed by Nitpick"
  2044 )
  2045 structure Nitpick_Ind_Intros = Named_Thms
  2046 (
  2047   val name = "nitpick_ind_intro"
  2048   val description = "introduction rules for (co)inductive predicates as needed by Nitpick"
  2049 )
  2050 *}
  2051 
  2052 setup {*
  2053   Nitpick_Const_Defs.setup
  2054   #> Nitpick_Const_Simps.setup
  2055   #> Nitpick_Const_Psimps.setup
  2056   #> Nitpick_Ind_Intros.setup
  2057 *}
  2058 
  2059 
  2060 subsection {* Legacy tactics and ML bindings *}
  2061 
  2062 ML {*
  2063 fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
  2064 
  2065 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  2066 local
  2067   fun wrong_prem (Const ("All", _) $ (Abs (_, _, t))) = wrong_prem t
  2068     | wrong_prem (Bound _) = true
  2069     | wrong_prem _ = false;
  2070   val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  2071 in
  2072   fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
  2073   fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
  2074 end;
  2075 
  2076 val all_conj_distrib = thm "all_conj_distrib";
  2077 val all_simps = thms "all_simps";
  2078 val atomize_not = thm "atomize_not";
  2079 val case_split = thm "case_split";
  2080 val cases_simp = thm "cases_simp";
  2081 val choice_eq = thm "choice_eq"
  2082 val cong = thm "cong"
  2083 val conj_comms = thms "conj_comms";
  2084 val conj_cong = thm "conj_cong";
  2085 val de_Morgan_conj = thm "de_Morgan_conj";
  2086 val de_Morgan_disj = thm "de_Morgan_disj";
  2087 val disj_assoc = thm "disj_assoc";
  2088 val disj_comms = thms "disj_comms";
  2089 val disj_cong = thm "disj_cong";
  2090 val eq_ac = thms "eq_ac";
  2091 val eq_cong2 = thm "eq_cong2"
  2092 val Eq_FalseI = thm "Eq_FalseI";
  2093 val Eq_TrueI = thm "Eq_TrueI";
  2094 val Ex1_def = thm "Ex1_def"
  2095 val ex_disj_distrib = thm "ex_disj_distrib";
  2096 val ex_simps = thms "ex_simps";
  2097 val if_cancel = thm "if_cancel";
  2098 val if_eq_cancel = thm "if_eq_cancel";
  2099 val if_False = thm "if_False";
  2100 val iff_conv_conj_imp = thm "iff_conv_conj_imp";
  2101 val iff = thm "iff"
  2102 val if_splits = thms "if_splits";
  2103 val if_True = thm "if_True";
  2104 val if_weak_cong = thm "if_weak_cong"
  2105 val imp_all = thm "imp_all";
  2106 val imp_cong = thm "imp_cong";
  2107 val imp_conjL = thm "imp_conjL";
  2108 val imp_conjR = thm "imp_conjR";
  2109 val imp_conv_disj = thm "imp_conv_disj";
  2110 val simp_implies_def = thm "simp_implies_def";
  2111 val simp_thms = thms "simp_thms";
  2112 val split_if = thm "split_if";
  2113 val the1_equality = thm "the1_equality"
  2114 val theI = thm "theI"
  2115 val theI' = thm "theI'"
  2116 val True_implies_equals = thm "True_implies_equals";
  2117 val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps simp_thms @ @{thms "nnf_simps"})
  2118 
  2119 *}
  2120 
  2121 end