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