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