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