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