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