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