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