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