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