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