src/HOL/HOL.thy
author haftmann
Tue Jun 05 15:16:08 2007 +0200 (2007-06-05)
changeset 23247 b99dce43d252
parent 23171 861f63a35d31
child 23263 0c227412b285
permissions -rw-r--r--
merged Code_Generator.thy into HOL.thy
clasohm@923
     1
(*  Title:      HOL/HOL.thy
clasohm@923
     2
    ID:         $Id$
wenzelm@11750
     3
    Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
wenzelm@11750
     4
*)
clasohm@923
     5
wenzelm@11750
     6
header {* The basis of Higher-Order Logic *}
clasohm@923
     7
nipkow@15131
     8
theory HOL
nipkow@15140
     9
imports CPure
wenzelm@23163
    10
uses
haftmann@23247
    11
  "~~/src/Tools/integer.ML"
wenzelm@23163
    12
  "hologic.ML"
wenzelm@23163
    13
  "~~/src/Provers/splitter.ML"
wenzelm@23163
    14
  "~~/src/Provers/hypsubst.ML"
wenzelm@23171
    15
  "~~/src/Tools/IsaPlanner/zipper.ML"
wenzelm@23171
    16
  "~~/src/Tools/IsaPlanner/isand.ML"
wenzelm@23171
    17
  "~~/src/Tools/IsaPlanner/rw_tools.ML"
wenzelm@23171
    18
  "~~/src/Tools/IsaPlanner/rw_inst.ML"
wenzelm@23163
    19
  "~~/src/Provers/eqsubst.ML"
wenzelm@23163
    20
  "~~/src/Provers/induct_method.ML"
wenzelm@23163
    21
  "~~/src/Provers/classical.ML"
wenzelm@23163
    22
  "~~/src/Provers/blast.ML"
wenzelm@23163
    23
  "~~/src/Provers/clasimp.ML"
haftmann@23247
    24
  "~~/src/Tools/rat.ML"
wenzelm@23163
    25
  "~~/src/Provers/Arith/fast_lin_arith.ML"
wenzelm@23163
    26
  "~~/src/Provers/Arith/cancel_sums.ML"
wenzelm@23163
    27
  "~~/src/Provers/quantifier1.ML"
wenzelm@23163
    28
  "~~/src/Provers/project_rule.ML"
wenzelm@23163
    29
  "~~/src/Provers/Arith/cancel_numerals.ML"
wenzelm@23163
    30
  "~~/src/Provers/Arith/combine_numerals.ML"
wenzelm@23163
    31
  "~~/src/Provers/Arith/cancel_numeral_factor.ML"
wenzelm@23163
    32
  "~~/src/Provers/Arith/extract_common_term.ML"
wenzelm@23163
    33
  "~~/src/Provers/quasi.ML"
wenzelm@23163
    34
  "~~/src/Provers/order.ML"
wenzelm@23163
    35
  ("simpdata.ML")
haftmann@23247
    36
  ("~~/src/HOL/Tools/recfun_codegen.ML")
wenzelm@23163
    37
  "Tools/res_atpset.ML"
nipkow@15131
    38
begin
wenzelm@2260
    39
wenzelm@11750
    40
subsection {* Primitive logic *}
wenzelm@11750
    41
wenzelm@11750
    42
subsubsection {* Core syntax *}
wenzelm@2260
    43
wenzelm@14854
    44
classes type
wenzelm@12338
    45
defaultsort type
wenzelm@3947
    46
wenzelm@12338
    47
global
clasohm@923
    48
wenzelm@7357
    49
typedecl bool
clasohm@923
    50
clasohm@923
    51
arities
wenzelm@12338
    52
  bool :: type
haftmann@20590
    53
  "fun" :: (type, type) type
clasohm@923
    54
wenzelm@11750
    55
judgment
wenzelm@11750
    56
  Trueprop      :: "bool => prop"                   ("(_)" 5)
clasohm@923
    57
wenzelm@11750
    58
consts
wenzelm@7357
    59
  Not           :: "bool => bool"                   ("~ _" [40] 40)
wenzelm@7357
    60
  True          :: bool
wenzelm@7357
    61
  False         :: bool
wenzelm@3947
    62
  arbitrary     :: 'a
clasohm@923
    63
wenzelm@11432
    64
  The           :: "('a => bool) => 'a"
wenzelm@7357
    65
  All           :: "('a => bool) => bool"           (binder "ALL " 10)
wenzelm@7357
    66
  Ex            :: "('a => bool) => bool"           (binder "EX " 10)
wenzelm@7357
    67
  Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
wenzelm@7357
    68
  Let           :: "['a, 'a => 'b] => 'b"
clasohm@923
    69
haftmann@22839
    70
  "op ="        :: "['a, 'a] => bool"               (infixl "=" 50)
haftmann@22839
    71
  "op &"        :: "[bool, bool] => bool"           (infixr "&" 35)
haftmann@22839
    72
  "op |"        :: "[bool, bool] => bool"           (infixr "|" 30)
haftmann@22839
    73
  "op -->"      :: "[bool, bool] => bool"           (infixr "-->" 25)
clasohm@923
    74
wenzelm@10432
    75
local
wenzelm@10432
    76
paulson@16587
    77
consts
paulson@16587
    78
  If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" 10)
wenzelm@2260
    79
wenzelm@19656
    80
wenzelm@11750
    81
subsubsection {* Additional concrete syntax *}
wenzelm@2260
    82
wenzelm@21210
    83
notation (output)
wenzelm@19656
    84
  "op ="  (infix "=" 50)
wenzelm@19656
    85
wenzelm@19656
    86
abbreviation
wenzelm@21404
    87
  not_equal :: "['a, 'a] => bool"  (infixl "~=" 50) where
wenzelm@19656
    88
  "x ~= y == ~ (x = y)"
wenzelm@19656
    89
wenzelm@21210
    90
notation (output)
wenzelm@19656
    91
  not_equal  (infix "~=" 50)
wenzelm@19656
    92
wenzelm@21210
    93
notation (xsymbols)
wenzelm@21404
    94
  Not  ("\<not> _" [40] 40) and
wenzelm@21404
    95
  "op &"  (infixr "\<and>" 35) and
wenzelm@21404
    96
  "op |"  (infixr "\<or>" 30) and
wenzelm@21404
    97
  "op -->"  (infixr "\<longrightarrow>" 25) and
wenzelm@19656
    98
  not_equal  (infix "\<noteq>" 50)
wenzelm@19656
    99
wenzelm@21210
   100
notation (HTML output)
wenzelm@21404
   101
  Not  ("\<not> _" [40] 40) and
wenzelm@21404
   102
  "op &"  (infixr "\<and>" 35) and
wenzelm@21404
   103
  "op |"  (infixr "\<or>" 30) and
wenzelm@19656
   104
  not_equal  (infix "\<noteq>" 50)
wenzelm@19656
   105
wenzelm@19656
   106
abbreviation (iff)
wenzelm@21404
   107
  iff :: "[bool, bool] => bool"  (infixr "<->" 25) where
wenzelm@19656
   108
  "A <-> B == A = B"
wenzelm@19656
   109
wenzelm@21210
   110
notation (xsymbols)
wenzelm@19656
   111
  iff  (infixr "\<longleftrightarrow>" 25)
wenzelm@19656
   112
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
  eq_reflection:  "(x=y) ==> (x==y)"
clasohm@923
   166
paulson@15380
   167
  refl:           "t = (t::'a)"
paulson@6289
   168
paulson@15380
   169
  ext:            "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
paulson@15380
   170
    -- {*Extensionality is built into the meta-logic, and this rule expresses
paulson@15380
   171
         a related property.  It is an eta-expanded version of the traditional
paulson@15380
   172
         rule, and similar to the ABS rule of HOL*}
paulson@6289
   173
wenzelm@11432
   174
  the_eq_trivial: "(THE x. x = a) = (a::'a)"
clasohm@923
   175
paulson@15380
   176
  impI:           "(P ==> Q) ==> P-->Q"
paulson@15380
   177
  mp:             "[| P-->Q;  P |] ==> Q"
paulson@15380
   178
paulson@15380
   179
clasohm@923
   180
defs
wenzelm@7357
   181
  True_def:     "True      == ((%x::bool. x) = (%x. x))"
wenzelm@7357
   182
  All_def:      "All(P)    == (P = (%x. True))"
paulson@11451
   183
  Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
wenzelm@7357
   184
  False_def:    "False     == (!P. P)"
wenzelm@7357
   185
  not_def:      "~ P       == P-->False"
wenzelm@7357
   186
  and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
wenzelm@7357
   187
  or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
wenzelm@7357
   188
  Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
clasohm@923
   189
wenzelm@7357
   190
axioms
wenzelm@7357
   191
  iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
wenzelm@7357
   192
  True_or_False:  "(P=True) | (P=False)"
clasohm@923
   193
clasohm@923
   194
defs
haftmann@22744
   195
  Let_def [code func]: "Let s f == f(s)"
paulson@11451
   196
  if_def:       "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
wenzelm@5069
   197
skalberg@14223
   198
finalconsts
skalberg@14223
   199
  "op ="
skalberg@14223
   200
  "op -->"
skalberg@14223
   201
  The
skalberg@14223
   202
  arbitrary
haftmann@22481
   203
haftmann@22481
   204
axiomatization
haftmann@22481
   205
  undefined :: 'a
haftmann@22481
   206
haftmann@22744
   207
axiomatization where
haftmann@22481
   208
  undefined_fun: "undefined x = undefined"
nipkow@3320
   209
wenzelm@19656
   210
haftmann@22481
   211
subsubsection {* Generic classes and algebraic operations *}
haftmann@22481
   212
haftmann@22481
   213
class default = type +
haftmann@22481
   214
  fixes default :: "'a"
wenzelm@4868
   215
haftmann@22473
   216
class zero = type + 
wenzelm@21524
   217
  fixes zero :: "'a"  ("\<^loc>0")
haftmann@20713
   218
haftmann@22473
   219
class one = type +
wenzelm@21524
   220
  fixes one  :: "'a"  ("\<^loc>1")
haftmann@20713
   221
haftmann@20713
   222
hide (open) const zero one
haftmann@20590
   223
haftmann@22473
   224
class plus = type +
wenzelm@21524
   225
  fixes plus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>+" 65)
wenzelm@11750
   226
haftmann@22473
   227
class minus = type +
haftmann@20590
   228
  fixes uminus :: "'a \<Rightarrow> 'a" 
wenzelm@21524
   229
    and minus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>-" 65)
wenzelm@21524
   230
    and abs :: "'a \<Rightarrow> 'a"
haftmann@20590
   231
haftmann@22473
   232
class times = type +
haftmann@20713
   233
  fixes times :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>*" 70)
haftmann@20590
   234
haftmann@22473
   235
class inverse = type +
haftmann@20590
   236
  fixes inverse :: "'a \<Rightarrow> 'a"
wenzelm@21524
   237
    and divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>'/" 70)
wenzelm@21524
   238
wenzelm@21524
   239
notation
wenzelm@21524
   240
  uminus  ("- _" [81] 80)
wenzelm@21524
   241
wenzelm@21524
   242
notation (xsymbols)
wenzelm@21524
   243
  abs  ("\<bar>_\<bar>")
wenzelm@21524
   244
notation (HTML output)
wenzelm@21524
   245
  abs  ("\<bar>_\<bar>")
wenzelm@11750
   246
wenzelm@13456
   247
syntax
wenzelm@13456
   248
  "_index1"  :: index    ("\<^sub>1")
wenzelm@13456
   249
translations
wenzelm@14690
   250
  (index) "\<^sub>1" => (index) "\<^bsub>\<struct>\<^esub>"
wenzelm@13456
   251
wenzelm@11750
   252
typed_print_translation {*
haftmann@20713
   253
let
haftmann@20713
   254
  fun tr' c = (c, fn show_sorts => fn T => fn ts =>
haftmann@20713
   255
    if T = dummyT orelse not (! show_types) andalso can Term.dest_Type T then raise Match
haftmann@20713
   256
    else Syntax.const Syntax.constrainC $ Syntax.const c $ Syntax.term_of_typ show_sorts T);
haftmann@22993
   257
in map tr' [@{const_syntax HOL.one}, @{const_syntax HOL.zero}] end;
wenzelm@11750
   258
*} -- {* show types that are presumably too general *}
wenzelm@11750
   259
wenzelm@11750
   260
haftmann@20944
   261
subsection {* Fundamental rules *}
haftmann@20944
   262
haftmann@20973
   263
subsubsection {* Equality *}
haftmann@20944
   264
haftmann@20944
   265
text {* Thanks to Stephan Merz *}
haftmann@20944
   266
lemma subst:
haftmann@20944
   267
  assumes eq: "s = t" and p: "P s"
haftmann@20944
   268
  shows "P t"
haftmann@20944
   269
proof -
haftmann@20944
   270
  from eq have meta: "s \<equiv> t"
haftmann@20944
   271
    by (rule eq_reflection)
haftmann@20944
   272
  from p show ?thesis
haftmann@20944
   273
    by (unfold meta)
haftmann@20944
   274
qed
paulson@15411
   275
wenzelm@18457
   276
lemma sym: "s = t ==> t = s"
wenzelm@18457
   277
  by (erule subst) (rule refl)
paulson@15411
   278
wenzelm@18457
   279
lemma ssubst: "t = s ==> P s ==> P t"
wenzelm@18457
   280
  by (drule sym) (erule subst)
paulson@15411
   281
paulson@15411
   282
lemma trans: "[| r=s; s=t |] ==> r=t"
wenzelm@18457
   283
  by (erule subst)
paulson@15411
   284
haftmann@20944
   285
lemma meta_eq_to_obj_eq: 
haftmann@20944
   286
  assumes meq: "A == B"
haftmann@20944
   287
  shows "A = B"
haftmann@20944
   288
  by (unfold meq) (rule refl)
paulson@15411
   289
wenzelm@21502
   290
text {* Useful with @{text erule} for proving equalities from known equalities. *}
haftmann@20944
   291
     (* a = b
paulson@15411
   292
        |   |
paulson@15411
   293
        c = d   *)
paulson@15411
   294
lemma box_equals: "[| a=b;  a=c;  b=d |] ==> c=d"
paulson@15411
   295
apply (rule trans)
paulson@15411
   296
apply (rule trans)
paulson@15411
   297
apply (rule sym)
paulson@15411
   298
apply assumption+
paulson@15411
   299
done
paulson@15411
   300
nipkow@15524
   301
text {* For calculational reasoning: *}
nipkow@15524
   302
nipkow@15524
   303
lemma forw_subst: "a = b ==> P b ==> P a"
nipkow@15524
   304
  by (rule ssubst)
nipkow@15524
   305
nipkow@15524
   306
lemma back_subst: "P a ==> a = b ==> P b"
nipkow@15524
   307
  by (rule subst)
nipkow@15524
   308
paulson@15411
   309
haftmann@20944
   310
subsubsection {*Congruence rules for application*}
paulson@15411
   311
paulson@15411
   312
(*similar to AP_THM in Gordon's HOL*)
paulson@15411
   313
lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
paulson@15411
   314
apply (erule subst)
paulson@15411
   315
apply (rule refl)
paulson@15411
   316
done
paulson@15411
   317
paulson@15411
   318
(*similar to AP_TERM in Gordon's HOL and FOL's subst_context*)
paulson@15411
   319
lemma arg_cong: "x=y ==> f(x)=f(y)"
paulson@15411
   320
apply (erule subst)
paulson@15411
   321
apply (rule refl)
paulson@15411
   322
done
paulson@15411
   323
paulson@15655
   324
lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
paulson@15655
   325
apply (erule ssubst)+
paulson@15655
   326
apply (rule refl)
paulson@15655
   327
done
paulson@15655
   328
paulson@15411
   329
lemma cong: "[| f = g; (x::'a) = y |] ==> f(x) = g(y)"
paulson@15411
   330
apply (erule subst)+
paulson@15411
   331
apply (rule refl)
paulson@15411
   332
done
paulson@15411
   333
paulson@15411
   334
haftmann@20944
   335
subsubsection {*Equality of booleans -- iff*}
paulson@15411
   336
wenzelm@21504
   337
lemma iffI: assumes "P ==> Q" and "Q ==> P" shows "P=Q"
wenzelm@21504
   338
  by (iprover intro: iff [THEN mp, THEN mp] impI assms)
paulson@15411
   339
paulson@15411
   340
lemma iffD2: "[| P=Q; Q |] ==> P"
wenzelm@18457
   341
  by (erule ssubst)
paulson@15411
   342
paulson@15411
   343
lemma rev_iffD2: "[| Q; P=Q |] ==> P"
wenzelm@18457
   344
  by (erule iffD2)
paulson@15411
   345
wenzelm@21504
   346
lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
wenzelm@21504
   347
  by (drule sym) (rule iffD2)
wenzelm@21504
   348
wenzelm@21504
   349
lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
wenzelm@21504
   350
  by (drule sym) (rule rev_iffD2)
paulson@15411
   351
paulson@15411
   352
lemma iffE:
paulson@15411
   353
  assumes major: "P=Q"
wenzelm@21504
   354
    and minor: "[| P --> Q; Q --> P |] ==> R"
wenzelm@18457
   355
  shows R
wenzelm@18457
   356
  by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
paulson@15411
   357
paulson@15411
   358
haftmann@20944
   359
subsubsection {*True*}
paulson@15411
   360
paulson@15411
   361
lemma TrueI: "True"
wenzelm@21504
   362
  unfolding True_def by (rule refl)
paulson@15411
   363
wenzelm@21504
   364
lemma eqTrueI: "P ==> P = True"
wenzelm@18457
   365
  by (iprover intro: iffI TrueI)
paulson@15411
   366
wenzelm@21504
   367
lemma eqTrueE: "P = True ==> P"
wenzelm@21504
   368
  by (erule iffD2) (rule TrueI)
paulson@15411
   369
paulson@15411
   370
haftmann@20944
   371
subsubsection {*Universal quantifier*}
paulson@15411
   372
wenzelm@21504
   373
lemma allI: assumes "!!x::'a. P(x)" shows "ALL x. P(x)"
wenzelm@21504
   374
  unfolding All_def by (iprover intro: ext eqTrueI assms)
paulson@15411
   375
paulson@15411
   376
lemma spec: "ALL x::'a. P(x) ==> P(x)"
paulson@15411
   377
apply (unfold All_def)
paulson@15411
   378
apply (rule eqTrueE)
paulson@15411
   379
apply (erule fun_cong)
paulson@15411
   380
done
paulson@15411
   381
paulson@15411
   382
lemma allE:
paulson@15411
   383
  assumes major: "ALL x. P(x)"
wenzelm@21504
   384
    and minor: "P(x) ==> R"
wenzelm@21504
   385
  shows R
wenzelm@21504
   386
  by (iprover intro: minor major [THEN spec])
paulson@15411
   387
paulson@15411
   388
lemma all_dupE:
paulson@15411
   389
  assumes major: "ALL x. P(x)"
wenzelm@21504
   390
    and minor: "[| P(x); ALL x. P(x) |] ==> R"
wenzelm@21504
   391
  shows R
wenzelm@21504
   392
  by (iprover intro: minor major major [THEN spec])
paulson@15411
   393
paulson@15411
   394
wenzelm@21504
   395
subsubsection {* False *}
wenzelm@21504
   396
wenzelm@21504
   397
text {*
wenzelm@21504
   398
  Depends upon @{text spec}; it is impossible to do propositional
wenzelm@21504
   399
  logic before quantifiers!
wenzelm@21504
   400
*}
paulson@15411
   401
paulson@15411
   402
lemma FalseE: "False ==> P"
wenzelm@21504
   403
  apply (unfold False_def)
wenzelm@21504
   404
  apply (erule spec)
wenzelm@21504
   405
  done
paulson@15411
   406
wenzelm@21504
   407
lemma False_neq_True: "False = True ==> P"
wenzelm@21504
   408
  by (erule eqTrueE [THEN FalseE])
paulson@15411
   409
paulson@15411
   410
wenzelm@21504
   411
subsubsection {* Negation *}
paulson@15411
   412
paulson@15411
   413
lemma notI:
wenzelm@21504
   414
  assumes "P ==> False"
paulson@15411
   415
  shows "~P"
wenzelm@21504
   416
  apply (unfold not_def)
wenzelm@21504
   417
  apply (iprover intro: impI assms)
wenzelm@21504
   418
  done
paulson@15411
   419
paulson@15411
   420
lemma False_not_True: "False ~= True"
wenzelm@21504
   421
  apply (rule notI)
wenzelm@21504
   422
  apply (erule False_neq_True)
wenzelm@21504
   423
  done
paulson@15411
   424
paulson@15411
   425
lemma True_not_False: "True ~= False"
wenzelm@21504
   426
  apply (rule notI)
wenzelm@21504
   427
  apply (drule sym)
wenzelm@21504
   428
  apply (erule False_neq_True)
wenzelm@21504
   429
  done
paulson@15411
   430
paulson@15411
   431
lemma notE: "[| ~P;  P |] ==> R"
wenzelm@21504
   432
  apply (unfold not_def)
wenzelm@21504
   433
  apply (erule mp [THEN FalseE])
wenzelm@21504
   434
  apply assumption
wenzelm@21504
   435
  done
paulson@15411
   436
wenzelm@21504
   437
lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
wenzelm@21504
   438
  by (erule notE [THEN notI]) (erule meta_mp)
paulson@15411
   439
paulson@15411
   440
haftmann@20944
   441
subsubsection {*Implication*}
paulson@15411
   442
paulson@15411
   443
lemma impE:
paulson@15411
   444
  assumes "P-->Q" "P" "Q ==> R"
paulson@15411
   445
  shows "R"
nipkow@17589
   446
by (iprover intro: prems mp)
paulson@15411
   447
paulson@15411
   448
(* Reduces Q to P-->Q, allowing substitution in P. *)
paulson@15411
   449
lemma rev_mp: "[| P;  P --> Q |] ==> Q"
nipkow@17589
   450
by (iprover intro: mp)
paulson@15411
   451
paulson@15411
   452
lemma contrapos_nn:
paulson@15411
   453
  assumes major: "~Q"
paulson@15411
   454
      and minor: "P==>Q"
paulson@15411
   455
  shows "~P"
nipkow@17589
   456
by (iprover intro: notI minor major [THEN notE])
paulson@15411
   457
paulson@15411
   458
(*not used at all, but we already have the other 3 combinations *)
paulson@15411
   459
lemma contrapos_pn:
paulson@15411
   460
  assumes major: "Q"
paulson@15411
   461
      and minor: "P ==> ~Q"
paulson@15411
   462
  shows "~P"
nipkow@17589
   463
by (iprover intro: notI minor major notE)
paulson@15411
   464
paulson@15411
   465
lemma not_sym: "t ~= s ==> s ~= t"
haftmann@21250
   466
  by (erule contrapos_nn) (erule sym)
haftmann@21250
   467
haftmann@21250
   468
lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
haftmann@21250
   469
  by (erule subst, erule ssubst, assumption)
paulson@15411
   470
paulson@15411
   471
(*still used in HOLCF*)
paulson@15411
   472
lemma rev_contrapos:
paulson@15411
   473
  assumes pq: "P ==> Q"
paulson@15411
   474
      and nq: "~Q"
paulson@15411
   475
  shows "~P"
paulson@15411
   476
apply (rule nq [THEN contrapos_nn])
paulson@15411
   477
apply (erule pq)
paulson@15411
   478
done
paulson@15411
   479
haftmann@20944
   480
subsubsection {*Existential quantifier*}
paulson@15411
   481
paulson@15411
   482
lemma exI: "P x ==> EX x::'a. P x"
paulson@15411
   483
apply (unfold Ex_def)
nipkow@17589
   484
apply (iprover intro: allI allE impI mp)
paulson@15411
   485
done
paulson@15411
   486
paulson@15411
   487
lemma exE:
paulson@15411
   488
  assumes major: "EX x::'a. P(x)"
paulson@15411
   489
      and minor: "!!x. P(x) ==> Q"
paulson@15411
   490
  shows "Q"
paulson@15411
   491
apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
nipkow@17589
   492
apply (iprover intro: impI [THEN allI] minor)
paulson@15411
   493
done
paulson@15411
   494
paulson@15411
   495
haftmann@20944
   496
subsubsection {*Conjunction*}
paulson@15411
   497
paulson@15411
   498
lemma conjI: "[| P; Q |] ==> P&Q"
paulson@15411
   499
apply (unfold and_def)
nipkow@17589
   500
apply (iprover intro: impI [THEN allI] mp)
paulson@15411
   501
done
paulson@15411
   502
paulson@15411
   503
lemma conjunct1: "[| P & Q |] ==> P"
paulson@15411
   504
apply (unfold and_def)
nipkow@17589
   505
apply (iprover intro: impI dest: spec mp)
paulson@15411
   506
done
paulson@15411
   507
paulson@15411
   508
lemma conjunct2: "[| P & Q |] ==> Q"
paulson@15411
   509
apply (unfold and_def)
nipkow@17589
   510
apply (iprover intro: impI dest: spec mp)
paulson@15411
   511
done
paulson@15411
   512
paulson@15411
   513
lemma conjE:
paulson@15411
   514
  assumes major: "P&Q"
paulson@15411
   515
      and minor: "[| P; Q |] ==> R"
paulson@15411
   516
  shows "R"
paulson@15411
   517
apply (rule minor)
paulson@15411
   518
apply (rule major [THEN conjunct1])
paulson@15411
   519
apply (rule major [THEN conjunct2])
paulson@15411
   520
done
paulson@15411
   521
paulson@15411
   522
lemma context_conjI:
paulson@15411
   523
  assumes prems: "P" "P ==> Q" shows "P & Q"
nipkow@17589
   524
by (iprover intro: conjI prems)
paulson@15411
   525
paulson@15411
   526
haftmann@20944
   527
subsubsection {*Disjunction*}
paulson@15411
   528
paulson@15411
   529
lemma disjI1: "P ==> P|Q"
paulson@15411
   530
apply (unfold or_def)
nipkow@17589
   531
apply (iprover intro: allI impI mp)
paulson@15411
   532
done
paulson@15411
   533
paulson@15411
   534
lemma disjI2: "Q ==> P|Q"
paulson@15411
   535
apply (unfold or_def)
nipkow@17589
   536
apply (iprover intro: allI impI mp)
paulson@15411
   537
done
paulson@15411
   538
paulson@15411
   539
lemma disjE:
paulson@15411
   540
  assumes major: "P|Q"
paulson@15411
   541
      and minorP: "P ==> R"
paulson@15411
   542
      and minorQ: "Q ==> R"
paulson@15411
   543
  shows "R"
nipkow@17589
   544
by (iprover intro: minorP minorQ impI
paulson@15411
   545
                 major [unfolded or_def, THEN spec, THEN mp, THEN mp])
paulson@15411
   546
paulson@15411
   547
haftmann@20944
   548
subsubsection {*Classical logic*}
paulson@15411
   549
paulson@15411
   550
lemma classical:
paulson@15411
   551
  assumes prem: "~P ==> P"
paulson@15411
   552
  shows "P"
paulson@15411
   553
apply (rule True_or_False [THEN disjE, THEN eqTrueE])
paulson@15411
   554
apply assumption
paulson@15411
   555
apply (rule notI [THEN prem, THEN eqTrueI])
paulson@15411
   556
apply (erule subst)
paulson@15411
   557
apply assumption
paulson@15411
   558
done
paulson@15411
   559
paulson@15411
   560
lemmas ccontr = FalseE [THEN classical, standard]
paulson@15411
   561
paulson@15411
   562
(*notE with premises exchanged; it discharges ~R so that it can be used to
paulson@15411
   563
  make elimination rules*)
paulson@15411
   564
lemma rev_notE:
paulson@15411
   565
  assumes premp: "P"
paulson@15411
   566
      and premnot: "~R ==> ~P"
paulson@15411
   567
  shows "R"
paulson@15411
   568
apply (rule ccontr)
paulson@15411
   569
apply (erule notE [OF premnot premp])
paulson@15411
   570
done
paulson@15411
   571
paulson@15411
   572
(*Double negation law*)
paulson@15411
   573
lemma notnotD: "~~P ==> P"
paulson@15411
   574
apply (rule classical)
paulson@15411
   575
apply (erule notE)
paulson@15411
   576
apply assumption
paulson@15411
   577
done
paulson@15411
   578
paulson@15411
   579
lemma contrapos_pp:
paulson@15411
   580
  assumes p1: "Q"
paulson@15411
   581
      and p2: "~P ==> ~Q"
paulson@15411
   582
  shows "P"
nipkow@17589
   583
by (iprover intro: classical p1 p2 notE)
paulson@15411
   584
paulson@15411
   585
haftmann@20944
   586
subsubsection {*Unique existence*}
paulson@15411
   587
paulson@15411
   588
lemma ex1I:
paulson@15411
   589
  assumes prems: "P a" "!!x. P(x) ==> x=a"
paulson@15411
   590
  shows "EX! x. P(x)"
nipkow@17589
   591
by (unfold Ex1_def, iprover intro: prems exI conjI allI impI)
paulson@15411
   592
paulson@15411
   593
text{*Sometimes easier to use: the premises have no shared variables.  Safe!*}
paulson@15411
   594
lemma ex_ex1I:
paulson@15411
   595
  assumes ex_prem: "EX x. P(x)"
paulson@15411
   596
      and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
paulson@15411
   597
  shows "EX! x. P(x)"
nipkow@17589
   598
by (iprover intro: ex_prem [THEN exE] ex1I eq)
paulson@15411
   599
paulson@15411
   600
lemma ex1E:
paulson@15411
   601
  assumes major: "EX! x. P(x)"
paulson@15411
   602
      and minor: "!!x. [| P(x);  ALL y. P(y) --> y=x |] ==> R"
paulson@15411
   603
  shows "R"
paulson@15411
   604
apply (rule major [unfolded Ex1_def, THEN exE])
paulson@15411
   605
apply (erule conjE)
nipkow@17589
   606
apply (iprover intro: minor)
paulson@15411
   607
done
paulson@15411
   608
paulson@15411
   609
lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
paulson@15411
   610
apply (erule ex1E)
paulson@15411
   611
apply (rule exI)
paulson@15411
   612
apply assumption
paulson@15411
   613
done
paulson@15411
   614
paulson@15411
   615
haftmann@20944
   616
subsubsection {*THE: definite description operator*}
paulson@15411
   617
paulson@15411
   618
lemma the_equality:
paulson@15411
   619
  assumes prema: "P a"
paulson@15411
   620
      and premx: "!!x. P x ==> x=a"
paulson@15411
   621
  shows "(THE x. P x) = a"
paulson@15411
   622
apply (rule trans [OF _ the_eq_trivial])
paulson@15411
   623
apply (rule_tac f = "The" in arg_cong)
paulson@15411
   624
apply (rule ext)
paulson@15411
   625
apply (rule iffI)
paulson@15411
   626
 apply (erule premx)
paulson@15411
   627
apply (erule ssubst, rule prema)
paulson@15411
   628
done
paulson@15411
   629
paulson@15411
   630
lemma theI:
paulson@15411
   631
  assumes "P a" and "!!x. P x ==> x=a"
paulson@15411
   632
  shows "P (THE x. P x)"
nipkow@17589
   633
by (iprover intro: prems the_equality [THEN ssubst])
paulson@15411
   634
paulson@15411
   635
lemma theI': "EX! x. P x ==> P (THE x. P x)"
paulson@15411
   636
apply (erule ex1E)
paulson@15411
   637
apply (erule theI)
paulson@15411
   638
apply (erule allE)
paulson@15411
   639
apply (erule mp)
paulson@15411
   640
apply assumption
paulson@15411
   641
done
paulson@15411
   642
paulson@15411
   643
(*Easier to apply than theI: only one occurrence of P*)
paulson@15411
   644
lemma theI2:
paulson@15411
   645
  assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
paulson@15411
   646
  shows "Q (THE x. P x)"
nipkow@17589
   647
by (iprover intro: prems theI)
paulson@15411
   648
wenzelm@18697
   649
lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
paulson@15411
   650
apply (rule the_equality)
paulson@15411
   651
apply  assumption
paulson@15411
   652
apply (erule ex1E)
paulson@15411
   653
apply (erule all_dupE)
paulson@15411
   654
apply (drule mp)
paulson@15411
   655
apply  assumption
paulson@15411
   656
apply (erule ssubst)
paulson@15411
   657
apply (erule allE)
paulson@15411
   658
apply (erule mp)
paulson@15411
   659
apply assumption
paulson@15411
   660
done
paulson@15411
   661
paulson@15411
   662
lemma the_sym_eq_trivial: "(THE y. x=y) = x"
paulson@15411
   663
apply (rule the_equality)
paulson@15411
   664
apply (rule refl)
paulson@15411
   665
apply (erule sym)
paulson@15411
   666
done
paulson@15411
   667
paulson@15411
   668
haftmann@20944
   669
subsubsection {*Classical intro rules for disjunction and existential quantifiers*}
paulson@15411
   670
paulson@15411
   671
lemma disjCI:
paulson@15411
   672
  assumes "~Q ==> P" shows "P|Q"
paulson@15411
   673
apply (rule classical)
nipkow@17589
   674
apply (iprover intro: prems disjI1 disjI2 notI elim: notE)
paulson@15411
   675
done
paulson@15411
   676
paulson@15411
   677
lemma excluded_middle: "~P | P"
nipkow@17589
   678
by (iprover intro: disjCI)
paulson@15411
   679
haftmann@20944
   680
text {*
haftmann@20944
   681
  case distinction as a natural deduction rule.
haftmann@20944
   682
  Note that @{term "~P"} is the second case, not the first
haftmann@20944
   683
*}
paulson@15411
   684
lemma case_split_thm:
paulson@15411
   685
  assumes prem1: "P ==> Q"
paulson@15411
   686
      and prem2: "~P ==> Q"
paulson@15411
   687
  shows "Q"
paulson@15411
   688
apply (rule excluded_middle [THEN disjE])
paulson@15411
   689
apply (erule prem2)
paulson@15411
   690
apply (erule prem1)
paulson@15411
   691
done
haftmann@20944
   692
lemmas case_split = case_split_thm [case_names True False]
paulson@15411
   693
paulson@15411
   694
(*Classical implies (-->) elimination. *)
paulson@15411
   695
lemma impCE:
paulson@15411
   696
  assumes major: "P-->Q"
paulson@15411
   697
      and minor: "~P ==> R" "Q ==> R"
paulson@15411
   698
  shows "R"
paulson@15411
   699
apply (rule excluded_middle [of P, THEN disjE])
nipkow@17589
   700
apply (iprover intro: minor major [THEN mp])+
paulson@15411
   701
done
paulson@15411
   702
paulson@15411
   703
(*This version of --> elimination works on Q before P.  It works best for
paulson@15411
   704
  those cases in which P holds "almost everywhere".  Can't install as
paulson@15411
   705
  default: would break old proofs.*)
paulson@15411
   706
lemma impCE':
paulson@15411
   707
  assumes major: "P-->Q"
paulson@15411
   708
      and minor: "Q ==> R" "~P ==> R"
paulson@15411
   709
  shows "R"
paulson@15411
   710
apply (rule excluded_middle [of P, THEN disjE])
nipkow@17589
   711
apply (iprover intro: minor major [THEN mp])+
paulson@15411
   712
done
paulson@15411
   713
paulson@15411
   714
(*Classical <-> elimination. *)
paulson@15411
   715
lemma iffCE:
paulson@15411
   716
  assumes major: "P=Q"
paulson@15411
   717
      and minor: "[| P; Q |] ==> R"  "[| ~P; ~Q |] ==> R"
paulson@15411
   718
  shows "R"
paulson@15411
   719
apply (rule major [THEN iffE])
nipkow@17589
   720
apply (iprover intro: minor elim: impCE notE)
paulson@15411
   721
done
paulson@15411
   722
paulson@15411
   723
lemma exCI:
paulson@15411
   724
  assumes "ALL x. ~P(x) ==> P(a)"
paulson@15411
   725
  shows "EX x. P(x)"
paulson@15411
   726
apply (rule ccontr)
nipkow@17589
   727
apply (iprover intro: prems exI allI notI notE [of "\<exists>x. P x"])
paulson@15411
   728
done
paulson@15411
   729
paulson@15411
   730
wenzelm@12386
   731
subsubsection {* Intuitionistic Reasoning *}
wenzelm@12386
   732
wenzelm@12386
   733
lemma impE':
wenzelm@12937
   734
  assumes 1: "P --> Q"
wenzelm@12937
   735
    and 2: "Q ==> R"
wenzelm@12937
   736
    and 3: "P --> Q ==> P"
wenzelm@12937
   737
  shows R
wenzelm@12386
   738
proof -
wenzelm@12386
   739
  from 3 and 1 have P .
wenzelm@12386
   740
  with 1 have Q by (rule impE)
wenzelm@12386
   741
  with 2 show R .
wenzelm@12386
   742
qed
wenzelm@12386
   743
wenzelm@12386
   744
lemma allE':
wenzelm@12937
   745
  assumes 1: "ALL x. P x"
wenzelm@12937
   746
    and 2: "P x ==> ALL x. P x ==> Q"
wenzelm@12937
   747
  shows Q
wenzelm@12386
   748
proof -
wenzelm@12386
   749
  from 1 have "P x" by (rule spec)
wenzelm@12386
   750
  from this and 1 show Q by (rule 2)
wenzelm@12386
   751
qed
wenzelm@12386
   752
wenzelm@12937
   753
lemma notE':
wenzelm@12937
   754
  assumes 1: "~ P"
wenzelm@12937
   755
    and 2: "~ P ==> P"
wenzelm@12937
   756
  shows R
wenzelm@12386
   757
proof -
wenzelm@12386
   758
  from 2 and 1 have P .
wenzelm@12386
   759
  with 1 show R by (rule notE)
wenzelm@12386
   760
qed
wenzelm@12386
   761
dixon@22444
   762
lemma TrueE: "True ==> P ==> P" .
dixon@22444
   763
lemma notFalseE: "~ False ==> P ==> P" .
dixon@22444
   764
dixon@22467
   765
lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
wenzelm@15801
   766
  and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
wenzelm@15801
   767
  and [Pure.elim 2] = allE notE' impE'
wenzelm@15801
   768
  and [Pure.intro] = exI disjI2 disjI1
wenzelm@12386
   769
wenzelm@12386
   770
lemmas [trans] = trans
wenzelm@12386
   771
  and [sym] = sym not_sym
wenzelm@15801
   772
  and [Pure.elim?] = iffD1 iffD2 impE
wenzelm@11750
   773
wenzelm@11438
   774
wenzelm@11750
   775
subsubsection {* Atomizing meta-level connectives *}
wenzelm@11750
   776
wenzelm@11750
   777
lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
wenzelm@12003
   778
proof
wenzelm@9488
   779
  assume "!!x. P x"
wenzelm@10383
   780
  show "ALL x. P x" by (rule allI)
wenzelm@9488
   781
next
wenzelm@9488
   782
  assume "ALL x. P x"
wenzelm@10383
   783
  thus "!!x. P x" by (rule allE)
wenzelm@9488
   784
qed
wenzelm@9488
   785
wenzelm@11750
   786
lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
wenzelm@12003
   787
proof
wenzelm@9488
   788
  assume r: "A ==> B"
wenzelm@10383
   789
  show "A --> B" by (rule impI) (rule r)
wenzelm@9488
   790
next
wenzelm@9488
   791
  assume "A --> B" and A
wenzelm@10383
   792
  thus B by (rule mp)
wenzelm@9488
   793
qed
wenzelm@9488
   794
paulson@14749
   795
lemma atomize_not: "(A ==> False) == Trueprop (~A)"
paulson@14749
   796
proof
paulson@14749
   797
  assume r: "A ==> False"
paulson@14749
   798
  show "~A" by (rule notI) (rule r)
paulson@14749
   799
next
paulson@14749
   800
  assume "~A" and A
paulson@14749
   801
  thus False by (rule notE)
paulson@14749
   802
qed
paulson@14749
   803
wenzelm@11750
   804
lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
wenzelm@12003
   805
proof
wenzelm@10432
   806
  assume "x == y"
wenzelm@10432
   807
  show "x = y" by (unfold prems) (rule refl)
wenzelm@10432
   808
next
wenzelm@10432
   809
  assume "x = y"
wenzelm@10432
   810
  thus "x == y" by (rule eq_reflection)
wenzelm@10432
   811
qed
wenzelm@10432
   812
wenzelm@12023
   813
lemma atomize_conj [atomize]:
wenzelm@19121
   814
  includes meta_conjunction_syntax
wenzelm@19121
   815
  shows "(A && B) == Trueprop (A & B)"
wenzelm@12003
   816
proof
wenzelm@19121
   817
  assume conj: "A && B"
wenzelm@19121
   818
  show "A & B"
wenzelm@19121
   819
  proof (rule conjI)
wenzelm@19121
   820
    from conj show A by (rule conjunctionD1)
wenzelm@19121
   821
    from conj show B by (rule conjunctionD2)
wenzelm@19121
   822
  qed
wenzelm@11953
   823
next
wenzelm@19121
   824
  assume conj: "A & B"
wenzelm@19121
   825
  show "A && B"
wenzelm@19121
   826
  proof -
wenzelm@19121
   827
    from conj show A ..
wenzelm@19121
   828
    from conj show B ..
wenzelm@11953
   829
  qed
wenzelm@11953
   830
qed
wenzelm@11953
   831
wenzelm@12386
   832
lemmas [symmetric, rulify] = atomize_all atomize_imp
wenzelm@18832
   833
  and [symmetric, defn] = atomize_all atomize_imp atomize_eq
wenzelm@12386
   834
wenzelm@11750
   835
haftmann@20944
   836
subsection {* Package setup *}
haftmann@20944
   837
wenzelm@11750
   838
subsubsection {* Classical Reasoner setup *}
wenzelm@9529
   839
haftmann@20944
   840
lemma thin_refl:
haftmann@20944
   841
  "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
haftmann@20944
   842
haftmann@21151
   843
ML {*
haftmann@21151
   844
structure Hypsubst = HypsubstFun(
haftmann@21151
   845
struct
haftmann@21151
   846
  structure Simplifier = Simplifier
wenzelm@21218
   847
  val dest_eq = HOLogic.dest_eq
haftmann@21151
   848
  val dest_Trueprop = HOLogic.dest_Trueprop
haftmann@21151
   849
  val dest_imp = HOLogic.dest_imp
wenzelm@22129
   850
  val eq_reflection = @{thm HOL.eq_reflection}
haftmann@22218
   851
  val rev_eq_reflection = @{thm HOL.meta_eq_to_obj_eq}
wenzelm@22129
   852
  val imp_intr = @{thm HOL.impI}
wenzelm@22129
   853
  val rev_mp = @{thm HOL.rev_mp}
wenzelm@22129
   854
  val subst = @{thm HOL.subst}
wenzelm@22129
   855
  val sym = @{thm HOL.sym}
wenzelm@22129
   856
  val thin_refl = @{thm thin_refl};
haftmann@21151
   857
end);
wenzelm@21671
   858
open Hypsubst;
haftmann@21151
   859
haftmann@21151
   860
structure Classical = ClassicalFun(
haftmann@21151
   861
struct
wenzelm@22129
   862
  val mp = @{thm HOL.mp}
wenzelm@22129
   863
  val not_elim = @{thm HOL.notE}
wenzelm@22129
   864
  val classical = @{thm HOL.classical}
haftmann@21151
   865
  val sizef = Drule.size_of_thm
haftmann@21151
   866
  val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
haftmann@21151
   867
end);
haftmann@21151
   868
haftmann@21151
   869
structure BasicClassical: BASIC_CLASSICAL = Classical; 
wenzelm@21671
   870
open BasicClassical;
wenzelm@22129
   871
wenzelm@22129
   872
ML_Context.value_antiq "claset"
wenzelm@22129
   873
  (Scan.succeed ("claset", "Classical.local_claset_of (ML_Context.the_local_context ())"));
haftmann@21151
   874
*}
haftmann@21151
   875
haftmann@21009
   876
setup {*
haftmann@21009
   877
let
haftmann@21009
   878
  (*prevent substitution on bool*)
haftmann@21009
   879
  fun hyp_subst_tac' i thm = if i <= Thm.nprems_of thm andalso
haftmann@21009
   880
    Term.exists_Const (fn ("op =", Type (_, [T, _])) => T <> Type ("bool", []) | _ => false)
haftmann@21009
   881
      (nth (Thm.prems_of thm) (i - 1)) then Hypsubst.hyp_subst_tac i thm else no_tac thm;
haftmann@21009
   882
in
haftmann@21151
   883
  Hypsubst.hypsubst_setup
haftmann@21151
   884
  #> ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac)
haftmann@21151
   885
  #> Classical.setup
haftmann@21151
   886
  #> ResAtpset.setup
haftmann@21009
   887
end
haftmann@21009
   888
*}
haftmann@21009
   889
haftmann@21009
   890
declare iffI [intro!]
haftmann@21009
   891
  and notI [intro!]
haftmann@21009
   892
  and impI [intro!]
haftmann@21009
   893
  and disjCI [intro!]
haftmann@21009
   894
  and conjI [intro!]
haftmann@21009
   895
  and TrueI [intro!]
haftmann@21009
   896
  and refl [intro!]
haftmann@21009
   897
haftmann@21009
   898
declare iffCE [elim!]
haftmann@21009
   899
  and FalseE [elim!]
haftmann@21009
   900
  and impCE [elim!]
haftmann@21009
   901
  and disjE [elim!]
haftmann@21009
   902
  and conjE [elim!]
haftmann@21009
   903
  and conjE [elim!]
haftmann@21009
   904
haftmann@21009
   905
declare ex_ex1I [intro!]
haftmann@21009
   906
  and allI [intro!]
haftmann@21009
   907
  and the_equality [intro]
haftmann@21009
   908
  and exI [intro]
haftmann@21009
   909
haftmann@21009
   910
declare exE [elim!]
haftmann@21009
   911
  allE [elim]
haftmann@21009
   912
wenzelm@22377
   913
ML {* val HOL_cs = @{claset} *}
mengj@19162
   914
wenzelm@20223
   915
lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
wenzelm@20223
   916
  apply (erule swap)
wenzelm@20223
   917
  apply (erule (1) meta_mp)
wenzelm@20223
   918
  done
wenzelm@10383
   919
wenzelm@18689
   920
declare ex_ex1I [rule del, intro! 2]
wenzelm@18689
   921
  and ex1I [intro]
wenzelm@18689
   922
wenzelm@12386
   923
lemmas [intro?] = ext
wenzelm@12386
   924
  and [elim?] = ex1_implies_ex
wenzelm@11977
   925
haftmann@20944
   926
(*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
haftmann@20973
   927
lemma alt_ex1E [elim!]:
haftmann@20944
   928
  assumes major: "\<exists>!x. P x"
haftmann@20944
   929
      and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
haftmann@20944
   930
  shows R
haftmann@20944
   931
apply (rule ex1E [OF major])
haftmann@20944
   932
apply (rule prem)
wenzelm@22129
   933
apply (tactic {* ares_tac @{thms allI} 1 *})+
wenzelm@22129
   934
apply (tactic {* etac (Classical.dup_elim @{thm allE}) 1 *})
wenzelm@22129
   935
apply iprover
wenzelm@22129
   936
done
haftmann@20944
   937
haftmann@21151
   938
ML {*
haftmann@21151
   939
structure Blast = BlastFun(
haftmann@21151
   940
struct
haftmann@21151
   941
  type claset = Classical.claset
haftmann@22744
   942
  val equality_name = @{const_name "op ="}
haftmann@22993
   943
  val not_name = @{const_name Not}
wenzelm@22129
   944
  val notE = @{thm HOL.notE}
wenzelm@22129
   945
  val ccontr = @{thm HOL.ccontr}
haftmann@21151
   946
  val contr_tac = Classical.contr_tac
haftmann@21151
   947
  val dup_intr = Classical.dup_intr
haftmann@21151
   948
  val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
wenzelm@21671
   949
  val claset = Classical.claset
haftmann@21151
   950
  val rep_cs = Classical.rep_cs
haftmann@21151
   951
  val cla_modifiers = Classical.cla_modifiers
haftmann@21151
   952
  val cla_meth' = Classical.cla_meth'
haftmann@21151
   953
end);
wenzelm@21671
   954
val Blast_tac = Blast.Blast_tac;
wenzelm@21671
   955
val blast_tac = Blast.blast_tac;
haftmann@20944
   956
*}
haftmann@20944
   957
haftmann@21151
   958
setup Blast.setup
haftmann@21151
   959
haftmann@20944
   960
haftmann@20944
   961
subsubsection {* Simplifier *}
wenzelm@12281
   962
wenzelm@12281
   963
lemma eta_contract_eq: "(%s. f s) = f" ..
wenzelm@12281
   964
wenzelm@12281
   965
lemma simp_thms:
wenzelm@12937
   966
  shows not_not: "(~ ~ P) = P"
nipkow@15354
   967
  and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
wenzelm@12937
   968
  and
berghofe@12436
   969
    "(P ~= Q) = (P = (~Q))"
berghofe@12436
   970
    "(P | ~P) = True"    "(~P | P) = True"
wenzelm@12281
   971
    "(x = x) = True"
haftmann@20944
   972
  and not_True_eq_False: "(\<not> True) = False"
haftmann@20944
   973
  and not_False_eq_True: "(\<not> False) = True"
haftmann@20944
   974
  and
berghofe@12436
   975
    "(~P) ~= P"  "P ~= (~P)"
haftmann@20944
   976
    "(True=P) = P"
haftmann@20944
   977
  and eq_True: "(P = True) = P"
haftmann@20944
   978
  and "(False=P) = (~P)"
haftmann@20944
   979
  and eq_False: "(P = False) = (\<not> P)"
haftmann@20944
   980
  and
wenzelm@12281
   981
    "(True --> P) = P"  "(False --> P) = True"
wenzelm@12281
   982
    "(P --> True) = True"  "(P --> P) = True"
wenzelm@12281
   983
    "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
wenzelm@12281
   984
    "(P & True) = P"  "(True & P) = P"
wenzelm@12281
   985
    "(P & False) = False"  "(False & P) = False"
wenzelm@12281
   986
    "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
wenzelm@12281
   987
    "(P & ~P) = False"    "(~P & P) = False"
wenzelm@12281
   988
    "(P | True) = True"  "(True | P) = True"
wenzelm@12281
   989
    "(P | False) = P"  "(False | P) = P"
berghofe@12436
   990
    "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
wenzelm@12281
   991
    "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
wenzelm@12281
   992
    -- {* needed for the one-point-rule quantifier simplification procs *}
wenzelm@12281
   993
    -- {* essential for termination!! *} and
wenzelm@12281
   994
    "!!P. (EX x. x=t & P(x)) = P(t)"
wenzelm@12281
   995
    "!!P. (EX x. t=x & P(x)) = P(t)"
wenzelm@12281
   996
    "!!P. (ALL x. x=t --> P(x)) = P(t)"
wenzelm@12937
   997
    "!!P. (ALL x. t=x --> P(x)) = P(t)"
nipkow@17589
   998
  by (blast, blast, blast, blast, blast, iprover+)
wenzelm@13421
   999
paulson@14201
  1000
lemma disj_absorb: "(A | A) = A"
paulson@14201
  1001
  by blast
paulson@14201
  1002
paulson@14201
  1003
lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
paulson@14201
  1004
  by blast
paulson@14201
  1005
paulson@14201
  1006
lemma conj_absorb: "(A & A) = A"
paulson@14201
  1007
  by blast
paulson@14201
  1008
paulson@14201
  1009
lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
paulson@14201
  1010
  by blast
paulson@14201
  1011
wenzelm@12281
  1012
lemma eq_ac:
wenzelm@12937
  1013
  shows eq_commute: "(a=b) = (b=a)"
wenzelm@12937
  1014
    and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
nipkow@17589
  1015
    and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
nipkow@17589
  1016
lemma neq_commute: "(a~=b) = (b~=a)" by iprover
wenzelm@12281
  1017
wenzelm@12281
  1018
lemma conj_comms:
wenzelm@12937
  1019
  shows conj_commute: "(P&Q) = (Q&P)"
nipkow@17589
  1020
    and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
nipkow@17589
  1021
lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
wenzelm@12281
  1022
paulson@19174
  1023
lemmas conj_ac = conj_commute conj_left_commute conj_assoc
paulson@19174
  1024
wenzelm@12281
  1025
lemma disj_comms:
wenzelm@12937
  1026
  shows disj_commute: "(P|Q) = (Q|P)"
nipkow@17589
  1027
    and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
nipkow@17589
  1028
lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
wenzelm@12281
  1029
paulson@19174
  1030
lemmas disj_ac = disj_commute disj_left_commute disj_assoc
paulson@19174
  1031
nipkow@17589
  1032
lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
nipkow@17589
  1033
lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
wenzelm@12281
  1034
nipkow@17589
  1035
lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
nipkow@17589
  1036
lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
wenzelm@12281
  1037
nipkow@17589
  1038
lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
nipkow@17589
  1039
lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
nipkow@17589
  1040
lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
wenzelm@12281
  1041
wenzelm@12281
  1042
text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
wenzelm@12281
  1043
lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
wenzelm@12281
  1044
lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
wenzelm@12281
  1045
wenzelm@12281
  1046
lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
wenzelm@12281
  1047
lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
wenzelm@12281
  1048
haftmann@21151
  1049
lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
haftmann@21151
  1050
  by iprover
haftmann@21151
  1051
nipkow@17589
  1052
lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
wenzelm@12281
  1053
lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
wenzelm@12281
  1054
lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
wenzelm@12281
  1055
lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
wenzelm@12281
  1056
lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
wenzelm@12281
  1057
lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
wenzelm@12281
  1058
  by blast
wenzelm@12281
  1059
lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
wenzelm@12281
  1060
nipkow@17589
  1061
lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
wenzelm@12281
  1062
wenzelm@12281
  1063
wenzelm@12281
  1064
lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
wenzelm@12281
  1065
  -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
wenzelm@12281
  1066
  -- {* cases boil down to the same thing. *}
wenzelm@12281
  1067
  by blast
wenzelm@12281
  1068
wenzelm@12281
  1069
lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
wenzelm@12281
  1070
lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
nipkow@17589
  1071
lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
nipkow@17589
  1072
lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
wenzelm@12281
  1073
nipkow@17589
  1074
lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
nipkow@17589
  1075
lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
wenzelm@12281
  1076
wenzelm@12281
  1077
text {*
wenzelm@12281
  1078
  \medskip The @{text "&"} congruence rule: not included by default!
wenzelm@12281
  1079
  May slow rewrite proofs down by as much as 50\% *}
wenzelm@12281
  1080
wenzelm@12281
  1081
lemma conj_cong:
wenzelm@12281
  1082
    "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1083
  by iprover
wenzelm@12281
  1084
wenzelm@12281
  1085
lemma rev_conj_cong:
wenzelm@12281
  1086
    "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1087
  by iprover
wenzelm@12281
  1088
wenzelm@12281
  1089
text {* The @{text "|"} congruence rule: not included by default! *}
wenzelm@12281
  1090
wenzelm@12281
  1091
lemma disj_cong:
wenzelm@12281
  1092
    "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
wenzelm@12281
  1093
  by blast
wenzelm@12281
  1094
wenzelm@12281
  1095
wenzelm@12281
  1096
text {* \medskip if-then-else rules *}
wenzelm@12281
  1097
wenzelm@12281
  1098
lemma if_True: "(if True then x else y) = x"
wenzelm@12281
  1099
  by (unfold if_def) blast
wenzelm@12281
  1100
wenzelm@12281
  1101
lemma if_False: "(if False then x else y) = y"
wenzelm@12281
  1102
  by (unfold if_def) blast
wenzelm@12281
  1103
wenzelm@12281
  1104
lemma if_P: "P ==> (if P then x else y) = x"
wenzelm@12281
  1105
  by (unfold if_def) blast
wenzelm@12281
  1106
wenzelm@12281
  1107
lemma if_not_P: "~P ==> (if P then x else y) = y"
wenzelm@12281
  1108
  by (unfold if_def) blast
wenzelm@12281
  1109
wenzelm@12281
  1110
lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
wenzelm@12281
  1111
  apply (rule case_split [of Q])
paulson@15481
  1112
   apply (simplesubst if_P)
paulson@15481
  1113
    prefer 3 apply (simplesubst if_not_P, blast+)
wenzelm@12281
  1114
  done
wenzelm@12281
  1115
wenzelm@12281
  1116
lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
paulson@15481
  1117
by (simplesubst split_if, blast)
wenzelm@12281
  1118
wenzelm@12281
  1119
lemmas if_splits = split_if split_if_asm
wenzelm@12281
  1120
wenzelm@12281
  1121
lemma if_cancel: "(if c then x else x) = x"
paulson@15481
  1122
by (simplesubst split_if, blast)
wenzelm@12281
  1123
wenzelm@12281
  1124
lemma if_eq_cancel: "(if x = y then y else x) = x"
paulson@15481
  1125
by (simplesubst split_if, blast)
wenzelm@12281
  1126
wenzelm@12281
  1127
lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
wenzelm@19796
  1128
  -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
wenzelm@12281
  1129
  by (rule split_if)
wenzelm@12281
  1130
wenzelm@12281
  1131
lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
wenzelm@19796
  1132
  -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
paulson@15481
  1133
  apply (simplesubst split_if, blast)
wenzelm@12281
  1134
  done
wenzelm@12281
  1135
nipkow@17589
  1136
lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
nipkow@17589
  1137
lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
wenzelm@12281
  1138
schirmer@15423
  1139
text {* \medskip let rules for simproc *}
schirmer@15423
  1140
schirmer@15423
  1141
lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
schirmer@15423
  1142
  by (unfold Let_def)
schirmer@15423
  1143
schirmer@15423
  1144
lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
schirmer@15423
  1145
  by (unfold Let_def)
schirmer@15423
  1146
berghofe@16633
  1147
text {*
ballarin@16999
  1148
  The following copy of the implication operator is useful for
ballarin@16999
  1149
  fine-tuning congruence rules.  It instructs the simplifier to simplify
ballarin@16999
  1150
  its premise.
berghofe@16633
  1151
*}
berghofe@16633
  1152
wenzelm@17197
  1153
constdefs
wenzelm@17197
  1154
  simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1)
wenzelm@17197
  1155
  "simp_implies \<equiv> op ==>"
berghofe@16633
  1156
wenzelm@18457
  1157
lemma simp_impliesI:
berghofe@16633
  1158
  assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
berghofe@16633
  1159
  shows "PROP P =simp=> PROP Q"
berghofe@16633
  1160
  apply (unfold simp_implies_def)
berghofe@16633
  1161
  apply (rule PQ)
berghofe@16633
  1162
  apply assumption
berghofe@16633
  1163
  done
berghofe@16633
  1164
berghofe@16633
  1165
lemma simp_impliesE:
berghofe@16633
  1166
  assumes PQ:"PROP P =simp=> PROP Q"
berghofe@16633
  1167
  and P: "PROP P"
berghofe@16633
  1168
  and QR: "PROP Q \<Longrightarrow> PROP R"
berghofe@16633
  1169
  shows "PROP R"
berghofe@16633
  1170
  apply (rule QR)
berghofe@16633
  1171
  apply (rule PQ [unfolded simp_implies_def])
berghofe@16633
  1172
  apply (rule P)
berghofe@16633
  1173
  done
berghofe@16633
  1174
berghofe@16633
  1175
lemma simp_implies_cong:
berghofe@16633
  1176
  assumes PP' :"PROP P == PROP P'"
berghofe@16633
  1177
  and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
berghofe@16633
  1178
  shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
berghofe@16633
  1179
proof (unfold simp_implies_def, rule equal_intr_rule)
berghofe@16633
  1180
  assume PQ: "PROP P \<Longrightarrow> PROP Q"
berghofe@16633
  1181
  and P': "PROP P'"
berghofe@16633
  1182
  from PP' [symmetric] and P' have "PROP P"
berghofe@16633
  1183
    by (rule equal_elim_rule1)
berghofe@16633
  1184
  hence "PROP Q" by (rule PQ)
berghofe@16633
  1185
  with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
berghofe@16633
  1186
next
berghofe@16633
  1187
  assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
berghofe@16633
  1188
  and P: "PROP P"
berghofe@16633
  1189
  from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
berghofe@16633
  1190
  hence "PROP Q'" by (rule P'Q')
berghofe@16633
  1191
  with P'QQ' [OF P', symmetric] show "PROP Q"
berghofe@16633
  1192
    by (rule equal_elim_rule1)
berghofe@16633
  1193
qed
berghofe@16633
  1194
haftmann@20944
  1195
lemma uncurry:
haftmann@20944
  1196
  assumes "P \<longrightarrow> Q \<longrightarrow> R"
haftmann@20944
  1197
  shows "P \<and> Q \<longrightarrow> R"
haftmann@20944
  1198
  using prems by blast
haftmann@20944
  1199
haftmann@20944
  1200
lemma iff_allI:
haftmann@20944
  1201
  assumes "\<And>x. P x = Q x"
haftmann@20944
  1202
  shows "(\<forall>x. P x) = (\<forall>x. Q x)"
haftmann@20944
  1203
  using prems by blast
haftmann@20944
  1204
haftmann@20944
  1205
lemma iff_exI:
haftmann@20944
  1206
  assumes "\<And>x. P x = Q x"
haftmann@20944
  1207
  shows "(\<exists>x. P x) = (\<exists>x. Q x)"
haftmann@20944
  1208
  using prems by blast
haftmann@20944
  1209
haftmann@20944
  1210
lemma all_comm:
haftmann@20944
  1211
  "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
haftmann@20944
  1212
  by blast
haftmann@20944
  1213
haftmann@20944
  1214
lemma ex_comm:
haftmann@20944
  1215
  "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
haftmann@20944
  1216
  by blast
haftmann@20944
  1217
wenzelm@9869
  1218
use "simpdata.ML"
wenzelm@21671
  1219
ML {* open Simpdata *}
wenzelm@21671
  1220
haftmann@21151
  1221
setup {*
haftmann@21151
  1222
  Simplifier.method_setup Splitter.split_modifiers
haftmann@21547
  1223
  #> (fn thy => (change_simpset_of thy (fn _ => Simpdata.simpset_simprocs); thy))
haftmann@21151
  1224
  #> Splitter.setup
haftmann@21151
  1225
  #> Clasimp.setup
haftmann@21151
  1226
  #> EqSubst.setup
haftmann@21151
  1227
*}
haftmann@21151
  1228
haftmann@21151
  1229
lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
haftmann@21151
  1230
proof
haftmann@21151
  1231
  assume prem: "True \<Longrightarrow> PROP P"
haftmann@21151
  1232
  from prem [OF TrueI] show "PROP P" . 
haftmann@21151
  1233
next
haftmann@21151
  1234
  assume "PROP P"
haftmann@21151
  1235
  show "PROP P" .
haftmann@21151
  1236
qed
haftmann@21151
  1237
haftmann@21151
  1238
lemma ex_simps:
haftmann@21151
  1239
  "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
haftmann@21151
  1240
  "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
haftmann@21151
  1241
  "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
haftmann@21151
  1242
  "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
haftmann@21151
  1243
  "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
haftmann@21151
  1244
  "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
haftmann@21151
  1245
  -- {* Miniscoping: pushing in existential quantifiers. *}
haftmann@21151
  1246
  by (iprover | blast)+
haftmann@21151
  1247
haftmann@21151
  1248
lemma all_simps:
haftmann@21151
  1249
  "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
haftmann@21151
  1250
  "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
haftmann@21151
  1251
  "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
haftmann@21151
  1252
  "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
haftmann@21151
  1253
  "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
haftmann@21151
  1254
  "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
haftmann@21151
  1255
  -- {* Miniscoping: pushing in universal quantifiers. *}
haftmann@21151
  1256
  by (iprover | blast)+
paulson@15481
  1257
wenzelm@21671
  1258
lemmas [simp] =
wenzelm@21671
  1259
  triv_forall_equality (*prunes params*)
wenzelm@21671
  1260
  True_implies_equals  (*prune asms `True'*)
wenzelm@21671
  1261
  if_True
wenzelm@21671
  1262
  if_False
wenzelm@21671
  1263
  if_cancel
wenzelm@21671
  1264
  if_eq_cancel
wenzelm@21671
  1265
  imp_disjL
haftmann@20973
  1266
  (*In general it seems wrong to add distributive laws by default: they
haftmann@20973
  1267
    might cause exponential blow-up.  But imp_disjL has been in for a while
haftmann@20973
  1268
    and cannot be removed without affecting existing proofs.  Moreover,
haftmann@20973
  1269
    rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
haftmann@20973
  1270
    grounds that it allows simplification of R in the two cases.*)
wenzelm@21671
  1271
  conj_assoc
wenzelm@21671
  1272
  disj_assoc
wenzelm@21671
  1273
  de_Morgan_conj
wenzelm@21671
  1274
  de_Morgan_disj
wenzelm@21671
  1275
  imp_disj1
wenzelm@21671
  1276
  imp_disj2
wenzelm@21671
  1277
  not_imp
wenzelm@21671
  1278
  disj_not1
wenzelm@21671
  1279
  not_all
wenzelm@21671
  1280
  not_ex
wenzelm@21671
  1281
  cases_simp
wenzelm@21671
  1282
  the_eq_trivial
wenzelm@21671
  1283
  the_sym_eq_trivial
wenzelm@21671
  1284
  ex_simps
wenzelm@21671
  1285
  all_simps
wenzelm@21671
  1286
  simp_thms
wenzelm@21671
  1287
wenzelm@21671
  1288
lemmas [cong] = imp_cong simp_implies_cong
wenzelm@21671
  1289
lemmas [split] = split_if
haftmann@20973
  1290
wenzelm@22377
  1291
ML {* val HOL_ss = @{simpset} *}
haftmann@20973
  1292
haftmann@20944
  1293
text {* Simplifies x assuming c and y assuming ~c *}
haftmann@20944
  1294
lemma if_cong:
haftmann@20944
  1295
  assumes "b = c"
haftmann@20944
  1296
      and "c \<Longrightarrow> x = u"
haftmann@20944
  1297
      and "\<not> c \<Longrightarrow> y = v"
haftmann@20944
  1298
  shows "(if b then x else y) = (if c then u else v)"
haftmann@20944
  1299
  unfolding if_def using prems by simp
haftmann@20944
  1300
haftmann@20944
  1301
text {* Prevents simplification of x and y:
haftmann@20944
  1302
  faster and allows the execution of functional programs. *}
haftmann@20944
  1303
lemma if_weak_cong [cong]:
haftmann@20944
  1304
  assumes "b = c"
haftmann@20944
  1305
  shows "(if b then x else y) = (if c then x else y)"
haftmann@20944
  1306
  using prems by (rule arg_cong)
haftmann@20944
  1307
haftmann@20944
  1308
text {* Prevents simplification of t: much faster *}
haftmann@20944
  1309
lemma let_weak_cong:
haftmann@20944
  1310
  assumes "a = b"
haftmann@20944
  1311
  shows "(let x = a in t x) = (let x = b in t x)"
haftmann@20944
  1312
  using prems by (rule arg_cong)
haftmann@20944
  1313
haftmann@20944
  1314
text {* To tidy up the result of a simproc.  Only the RHS will be simplified. *}
haftmann@20944
  1315
lemma eq_cong2:
haftmann@20944
  1316
  assumes "u = u'"
haftmann@20944
  1317
  shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
haftmann@20944
  1318
  using prems by simp
haftmann@20944
  1319
haftmann@20944
  1320
lemma if_distrib:
haftmann@20944
  1321
  "f (if c then x else y) = (if c then f x else f y)"
haftmann@20944
  1322
  by simp
haftmann@20944
  1323
haftmann@20944
  1324
text {* This lemma restricts the effect of the rewrite rule u=v to the left-hand
wenzelm@21502
  1325
  side of an equality.  Used in @{text "{Integ,Real}/simproc.ML"} *}
haftmann@20944
  1326
lemma restrict_to_left:
haftmann@20944
  1327
  assumes "x = y"
haftmann@20944
  1328
  shows "(x = z) = (y = z)"
haftmann@20944
  1329
  using prems by simp
haftmann@20944
  1330
wenzelm@17459
  1331
haftmann@20944
  1332
subsubsection {* Generic cases and induction *}
wenzelm@17459
  1333
haftmann@20944
  1334
text {* Rule projections: *}
berghofe@18887
  1335
haftmann@20944
  1336
ML {*
haftmann@20944
  1337
structure ProjectRule = ProjectRuleFun
haftmann@20944
  1338
(struct
wenzelm@22129
  1339
  val conjunct1 = @{thm conjunct1};
wenzelm@22129
  1340
  val conjunct2 = @{thm conjunct2};
wenzelm@22129
  1341
  val mp = @{thm mp};
haftmann@20944
  1342
end)
wenzelm@17459
  1343
*}
wenzelm@17459
  1344
wenzelm@11824
  1345
constdefs
wenzelm@18457
  1346
  induct_forall where "induct_forall P == \<forall>x. P x"
wenzelm@18457
  1347
  induct_implies where "induct_implies A B == A \<longrightarrow> B"
wenzelm@18457
  1348
  induct_equal where "induct_equal x y == x = y"
wenzelm@18457
  1349
  induct_conj where "induct_conj A B == A \<and> B"
wenzelm@11824
  1350
wenzelm@11989
  1351
lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
wenzelm@18457
  1352
  by (unfold atomize_all induct_forall_def)
wenzelm@11824
  1353
wenzelm@11989
  1354
lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
wenzelm@18457
  1355
  by (unfold atomize_imp induct_implies_def)
wenzelm@11824
  1356
wenzelm@11989
  1357
lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
wenzelm@18457
  1358
  by (unfold atomize_eq induct_equal_def)
wenzelm@18457
  1359
wenzelm@18457
  1360
lemma induct_conj_eq:
wenzelm@18457
  1361
  includes meta_conjunction_syntax
wenzelm@18457
  1362
  shows "(A && B) == Trueprop (induct_conj A B)"
wenzelm@18457
  1363
  by (unfold atomize_conj induct_conj_def)
wenzelm@18457
  1364
wenzelm@18457
  1365
lemmas induct_atomize = induct_forall_eq induct_implies_eq induct_equal_eq induct_conj_eq
wenzelm@18457
  1366
lemmas induct_rulify [symmetric, standard] = induct_atomize
wenzelm@18457
  1367
lemmas induct_rulify_fallback =
wenzelm@18457
  1368
  induct_forall_def induct_implies_def induct_equal_def induct_conj_def
wenzelm@18457
  1369
wenzelm@11824
  1370
wenzelm@11989
  1371
lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
wenzelm@11989
  1372
    induct_conj (induct_forall A) (induct_forall B)"
nipkow@17589
  1373
  by (unfold induct_forall_def induct_conj_def) iprover
wenzelm@11824
  1374
wenzelm@11989
  1375
lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
wenzelm@11989
  1376
    induct_conj (induct_implies C A) (induct_implies C B)"
nipkow@17589
  1377
  by (unfold induct_implies_def induct_conj_def) iprover
wenzelm@11989
  1378
berghofe@13598
  1379
lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
berghofe@13598
  1380
proof
berghofe@13598
  1381
  assume r: "induct_conj A B ==> PROP C" and A B
wenzelm@18457
  1382
  show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
berghofe@13598
  1383
next
berghofe@13598
  1384
  assume r: "A ==> B ==> PROP C" and "induct_conj A B"
wenzelm@18457
  1385
  show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
berghofe@13598
  1386
qed
wenzelm@11824
  1387
wenzelm@11989
  1388
lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
wenzelm@11824
  1389
wenzelm@11989
  1390
hide const induct_forall induct_implies induct_equal induct_conj
wenzelm@11824
  1391
wenzelm@11824
  1392
text {* Method setup. *}
wenzelm@11824
  1393
wenzelm@11824
  1394
ML {*
wenzelm@11824
  1395
  structure InductMethod = InductMethodFun
wenzelm@11824
  1396
  (struct
wenzelm@22129
  1397
    val cases_default = @{thm case_split}
wenzelm@22129
  1398
    val atomize = @{thms induct_atomize}
wenzelm@22129
  1399
    val rulify = @{thms induct_rulify}
wenzelm@22129
  1400
    val rulify_fallback = @{thms induct_rulify_fallback}
wenzelm@11824
  1401
  end);
wenzelm@11824
  1402
*}
wenzelm@11824
  1403
wenzelm@11824
  1404
setup InductMethod.setup
wenzelm@11824
  1405
wenzelm@18457
  1406
haftmann@20944
  1407
haftmann@20944
  1408
subsection {* Other simple lemmas and lemma duplicates *}
haftmann@20944
  1409
haftmann@20944
  1410
lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
haftmann@20944
  1411
  by blast+
haftmann@20944
  1412
haftmann@20944
  1413
lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
haftmann@20944
  1414
  apply (rule iffI)
haftmann@20944
  1415
  apply (rule_tac a = "%x. THE y. P x y" in ex1I)
haftmann@20944
  1416
  apply (fast dest!: theI')
haftmann@20944
  1417
  apply (fast intro: ext the1_equality [symmetric])
haftmann@20944
  1418
  apply (erule ex1E)
haftmann@20944
  1419
  apply (rule allI)
haftmann@20944
  1420
  apply (rule ex1I)
haftmann@20944
  1421
  apply (erule spec)
haftmann@20944
  1422
  apply (erule_tac x = "%z. if z = x then y else f z" in allE)
haftmann@20944
  1423
  apply (erule impE)
haftmann@20944
  1424
  apply (rule allI)
haftmann@20944
  1425
  apply (rule_tac P = "xa = x" in case_split_thm)
haftmann@20944
  1426
  apply (drule_tac [3] x = x in fun_cong, simp_all)
haftmann@20944
  1427
  done
haftmann@20944
  1428
haftmann@20944
  1429
lemma mk_left_commute:
haftmann@21547
  1430
  fixes f (infix "\<otimes>" 60)
haftmann@21547
  1431
  assumes a: "\<And>x y z. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)" and
haftmann@21547
  1432
          c: "\<And>x y. x \<otimes> y = y \<otimes> x"
haftmann@21547
  1433
  shows "x \<otimes> (y \<otimes> z) = y \<otimes> (x \<otimes> z)"
haftmann@20944
  1434
  by (rule trans [OF trans [OF c a] arg_cong [OF c, of "f y"]])
haftmann@20944
  1435
haftmann@22218
  1436
lemmas eq_sym_conv = eq_commute
haftmann@22218
  1437
chaieb@23037
  1438
lemma nnf_simps:
chaieb@23037
  1439
  "(\<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
  1440
  "(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
  1441
  "(\<not> \<not>(P)) = P"
chaieb@23037
  1442
by blast+
chaieb@23037
  1443
wenzelm@21671
  1444
wenzelm@21671
  1445
subsection {* Basic ML bindings *}
wenzelm@21671
  1446
wenzelm@21671
  1447
ML {*
wenzelm@22129
  1448
val FalseE = @{thm FalseE}
wenzelm@22129
  1449
val Let_def = @{thm Let_def}
wenzelm@22129
  1450
val TrueI = @{thm TrueI}
wenzelm@22129
  1451
val allE = @{thm allE}
wenzelm@22129
  1452
val allI = @{thm allI}
wenzelm@22129
  1453
val all_dupE = @{thm all_dupE}
wenzelm@22129
  1454
val arg_cong = @{thm arg_cong}
wenzelm@22129
  1455
val box_equals = @{thm box_equals}
wenzelm@22129
  1456
val ccontr = @{thm ccontr}
wenzelm@22129
  1457
val classical = @{thm classical}
wenzelm@22129
  1458
val conjE = @{thm conjE}
wenzelm@22129
  1459
val conjI = @{thm conjI}
wenzelm@22129
  1460
val conjunct1 = @{thm conjunct1}
wenzelm@22129
  1461
val conjunct2 = @{thm conjunct2}
wenzelm@22129
  1462
val disjCI = @{thm disjCI}
wenzelm@22129
  1463
val disjE = @{thm disjE}
wenzelm@22129
  1464
val disjI1 = @{thm disjI1}
wenzelm@22129
  1465
val disjI2 = @{thm disjI2}
wenzelm@22129
  1466
val eq_reflection = @{thm eq_reflection}
wenzelm@22129
  1467
val ex1E = @{thm ex1E}
wenzelm@22129
  1468
val ex1I = @{thm ex1I}
wenzelm@22129
  1469
val ex1_implies_ex = @{thm ex1_implies_ex}
wenzelm@22129
  1470
val exE = @{thm exE}
wenzelm@22129
  1471
val exI = @{thm exI}
wenzelm@22129
  1472
val excluded_middle = @{thm excluded_middle}
wenzelm@22129
  1473
val ext = @{thm ext}
wenzelm@22129
  1474
val fun_cong = @{thm fun_cong}
wenzelm@22129
  1475
val iffD1 = @{thm iffD1}
wenzelm@22129
  1476
val iffD2 = @{thm iffD2}
wenzelm@22129
  1477
val iffI = @{thm iffI}
wenzelm@22129
  1478
val impE = @{thm impE}
wenzelm@22129
  1479
val impI = @{thm impI}
wenzelm@22129
  1480
val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
wenzelm@22129
  1481
val mp = @{thm mp}
wenzelm@22129
  1482
val notE = @{thm notE}
wenzelm@22129
  1483
val notI = @{thm notI}
wenzelm@22129
  1484
val not_all = @{thm not_all}
wenzelm@22129
  1485
val not_ex = @{thm not_ex}
wenzelm@22129
  1486
val not_iff = @{thm not_iff}
wenzelm@22129
  1487
val not_not = @{thm not_not}
wenzelm@22129
  1488
val not_sym = @{thm not_sym}
wenzelm@22129
  1489
val refl = @{thm refl}
wenzelm@22129
  1490
val rev_mp = @{thm rev_mp}
wenzelm@22129
  1491
val spec = @{thm spec}
wenzelm@22129
  1492
val ssubst = @{thm ssubst}
wenzelm@22129
  1493
val subst = @{thm subst}
wenzelm@22129
  1494
val sym = @{thm sym}
wenzelm@22129
  1495
val trans = @{thm trans}
wenzelm@21671
  1496
*}
wenzelm@21671
  1497
wenzelm@21671
  1498
haftmann@23247
  1499
subsection {* Code generator setup *}
haftmann@23247
  1500
haftmann@23247
  1501
subsubsection {* SML code generator setup *}
haftmann@23247
  1502
haftmann@23247
  1503
use "~~/src/HOL/Tools/recfun_codegen.ML"
haftmann@23247
  1504
haftmann@23247
  1505
types_code
haftmann@23247
  1506
  "bool"  ("bool")
haftmann@23247
  1507
attach (term_of) {*
haftmann@23247
  1508
fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
haftmann@23247
  1509
*}
haftmann@23247
  1510
attach (test) {*
haftmann@23247
  1511
fun gen_bool i = one_of [false, true];
haftmann@23247
  1512
*}
haftmann@23247
  1513
  "prop"  ("bool")
haftmann@23247
  1514
attach (term_of) {*
haftmann@23247
  1515
fun term_of_prop b =
haftmann@23247
  1516
  HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
haftmann@23247
  1517
*}
haftmann@23247
  1518
haftmann@23247
  1519
consts_code
haftmann@23247
  1520
  "Trueprop" ("(_)")
haftmann@23247
  1521
  "True"    ("true")
haftmann@23247
  1522
  "False"   ("false")
haftmann@23247
  1523
  "Not"     ("Bool.not")
haftmann@23247
  1524
  "op |"    ("(_ orelse/ _)")
haftmann@23247
  1525
  "op &"    ("(_ andalso/ _)")
haftmann@23247
  1526
  "If"      ("(if _/ then _/ else _)")
haftmann@23247
  1527
haftmann@23247
  1528
setup {*
haftmann@23247
  1529
let
haftmann@23247
  1530
haftmann@23247
  1531
fun eq_codegen thy defs gr dep thyname b t =
haftmann@23247
  1532
    (case strip_comb t of
haftmann@23247
  1533
       (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
haftmann@23247
  1534
     | (Const ("op =", _), [t, u]) =>
haftmann@23247
  1535
          let
haftmann@23247
  1536
            val (gr', pt) = Codegen.invoke_codegen thy defs dep thyname false (gr, t);
haftmann@23247
  1537
            val (gr'', pu) = Codegen.invoke_codegen thy defs dep thyname false (gr', u);
haftmann@23247
  1538
            val (gr''', _) = Codegen.invoke_tycodegen thy defs dep thyname false (gr'', HOLogic.boolT)
haftmann@23247
  1539
          in
haftmann@23247
  1540
            SOME (gr''', Codegen.parens
haftmann@23247
  1541
              (Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu]))
haftmann@23247
  1542
          end
haftmann@23247
  1543
     | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
haftmann@23247
  1544
         thy defs dep thyname b (gr, Codegen.eta_expand t ts 2))
haftmann@23247
  1545
     | _ => NONE);
haftmann@23247
  1546
haftmann@23247
  1547
in
haftmann@23247
  1548
haftmann@23247
  1549
Codegen.add_codegen "eq_codegen" eq_codegen
haftmann@23247
  1550
#> RecfunCodegen.setup
haftmann@23247
  1551
haftmann@23247
  1552
end
haftmann@23247
  1553
*}
haftmann@23247
  1554
haftmann@23247
  1555
text {* Evaluation *}
haftmann@23247
  1556
haftmann@23247
  1557
method_setup evaluation = {*
haftmann@23247
  1558
let
haftmann@23247
  1559
haftmann@23247
  1560
fun evaluation_tac i = Tactical.PRIMITIVE (Conv.fconv_rule
haftmann@23247
  1561
  (Conv.goals_conv (equal i) Codegen.evaluation_conv));
haftmann@23247
  1562
haftmann@23247
  1563
in Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI)) end
haftmann@23247
  1564
*} "solve goal by evaluation"
haftmann@23247
  1565
haftmann@23247
  1566
haftmann@23247
  1567
subsubsection {* Generic code generator setup *}
haftmann@23247
  1568
haftmann@23247
  1569
text {* operational equality for code generation *}
haftmann@23247
  1570
haftmann@23247
  1571
class eq (attach "op =") = type
haftmann@23247
  1572
haftmann@23247
  1573
haftmann@23247
  1574
text {* using built-in Haskell equality *}
haftmann@23247
  1575
haftmann@23247
  1576
code_class eq
haftmann@23247
  1577
  (Haskell "Eq" where "op =" \<equiv> "(==)")
haftmann@23247
  1578
haftmann@23247
  1579
code_const "op ="
haftmann@23247
  1580
  (Haskell infixl 4 "==")
haftmann@23247
  1581
haftmann@23247
  1582
haftmann@23247
  1583
text {* type bool *}
haftmann@23247
  1584
haftmann@23247
  1585
code_datatype True False
haftmann@23247
  1586
haftmann@23247
  1587
lemma [code func]:
haftmann@23247
  1588
  shows "(False \<and> x) = False"
haftmann@23247
  1589
    and "(True \<and> x) = x"
haftmann@23247
  1590
    and "(x \<and> False) = False"
haftmann@23247
  1591
    and "(x \<and> True) = x" by simp_all
haftmann@23247
  1592
haftmann@23247
  1593
lemma [code func]:
haftmann@23247
  1594
  shows "(False \<or> x) = x"
haftmann@23247
  1595
    and "(True \<or> x) = True"
haftmann@23247
  1596
    and "(x \<or> False) = x"
haftmann@23247
  1597
    and "(x \<or> True) = True" by simp_all
haftmann@23247
  1598
haftmann@23247
  1599
lemma [code func]:
haftmann@23247
  1600
  shows "(\<not> True) = False"
haftmann@23247
  1601
    and "(\<not> False) = True" by (rule HOL.simp_thms)+
haftmann@23247
  1602
haftmann@23247
  1603
lemmas [code] = imp_conv_disj
haftmann@23247
  1604
haftmann@23247
  1605
lemmas [code func] = if_True if_False
haftmann@23247
  1606
haftmann@23247
  1607
instance bool :: eq ..
haftmann@23247
  1608
haftmann@23247
  1609
lemma [code func]:
haftmann@23247
  1610
  shows "True = P \<longleftrightarrow> P"
haftmann@23247
  1611
    and "False = P \<longleftrightarrow> \<not> P"
haftmann@23247
  1612
    and "P = True \<longleftrightarrow> P"
haftmann@23247
  1613
    and "P = False \<longleftrightarrow> \<not> P" by simp_all
haftmann@23247
  1614
haftmann@23247
  1615
code_type bool
haftmann@23247
  1616
  (SML "bool")
haftmann@23247
  1617
  (OCaml "bool")
haftmann@23247
  1618
  (Haskell "Bool")
haftmann@23247
  1619
haftmann@23247
  1620
code_instance bool :: eq
haftmann@23247
  1621
  (Haskell -)
haftmann@23247
  1622
haftmann@23247
  1623
code_const "op = \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
haftmann@23247
  1624
  (Haskell infixl 4 "==")
haftmann@23247
  1625
haftmann@23247
  1626
code_const True and False and Not and "op &" and "op |" and If
haftmann@23247
  1627
  (SML "true" and "false" and "not"
haftmann@23247
  1628
    and infixl 1 "andalso" and infixl 0 "orelse"
haftmann@23247
  1629
    and "!(if (_)/ then (_)/ else (_))")
haftmann@23247
  1630
  (OCaml "true" and "false" and "not"
haftmann@23247
  1631
    and infixl 4 "&&" and infixl 2 "||"
haftmann@23247
  1632
    and "!(if (_)/ then (_)/ else (_))")
haftmann@23247
  1633
  (Haskell "True" and "False" and "not"
haftmann@23247
  1634
    and infixl 3 "&&" and infixl 2 "||"
haftmann@23247
  1635
    and "!(if (_)/ then (_)/ else (_))")
haftmann@23247
  1636
haftmann@23247
  1637
code_reserved SML
haftmann@23247
  1638
  bool true false not
haftmann@23247
  1639
haftmann@23247
  1640
code_reserved OCaml
haftmann@23247
  1641
  bool true false not
haftmann@23247
  1642
haftmann@23247
  1643
haftmann@23247
  1644
text {* type prop *}
haftmann@23247
  1645
haftmann@23247
  1646
code_datatype Trueprop "prop"
haftmann@23247
  1647
haftmann@23247
  1648
haftmann@23247
  1649
text {* type itself *}
haftmann@23247
  1650
haftmann@23247
  1651
code_datatype "TYPE('a)"
haftmann@23247
  1652
haftmann@23247
  1653
haftmann@23247
  1654
text {* code generation for undefined as exception *}
haftmann@23247
  1655
haftmann@23247
  1656
code_const undefined
haftmann@23247
  1657
  (SML "raise/ Fail/ \"undefined\"")
haftmann@23247
  1658
  (OCaml "failwith/ \"undefined\"")
haftmann@23247
  1659
  (Haskell "error/ \"undefined\"")
haftmann@23247
  1660
haftmann@23247
  1661
code_reserved SML Fail
haftmann@23247
  1662
code_reserved OCaml failwith
haftmann@23247
  1663
haftmann@23247
  1664
haftmann@23247
  1665
subsubsection {* Evaluation oracle *}
haftmann@23247
  1666
haftmann@23247
  1667
oracle eval_oracle ("term") = {* fn thy => fn t => 
haftmann@23247
  1668
  if CodegenPackage.satisfies thy (HOLogic.dest_Trueprop t) [] 
haftmann@23247
  1669
  then t
haftmann@23247
  1670
  else HOLogic.Trueprop $ HOLogic.true_const (*dummy*)
haftmann@23247
  1671
*}
haftmann@23247
  1672
haftmann@23247
  1673
method_setup eval = {*
haftmann@23247
  1674
let
haftmann@23247
  1675
  fun eval_tac thy = 
haftmann@23247
  1676
    SUBGOAL (fn (t, i) => rtac (eval_oracle thy t) i)
haftmann@23247
  1677
in 
haftmann@23247
  1678
  Method.ctxt_args (fn ctxt => 
haftmann@23247
  1679
    Method.SIMPLE_METHOD' (eval_tac (ProofContext.theory_of ctxt)))
haftmann@23247
  1680
end
haftmann@23247
  1681
*} "solve goal by evaluation"
haftmann@23247
  1682
haftmann@23247
  1683
haftmann@23247
  1684
subsubsection {* Normalization by evaluation *}
haftmann@23247
  1685
haftmann@23247
  1686
method_setup normalization = {*
haftmann@23247
  1687
let
haftmann@23247
  1688
  fun normalization_tac i = Tactical.PRIMITIVE (Conv.fconv_rule
haftmann@23247
  1689
    (Conv.goals_conv (equal i) (HOLogic.Trueprop_conv
haftmann@23247
  1690
      NBE.normalization_conv)));
haftmann@23247
  1691
in
haftmann@23247
  1692
  Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl]))
haftmann@23247
  1693
end
haftmann@23247
  1694
*} "solve goal by normalization"
haftmann@23247
  1695
haftmann@23247
  1696
haftmann@23247
  1697
text {* lazy @{const If} *}
haftmann@23247
  1698
haftmann@23247
  1699
definition
haftmann@23247
  1700
  if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where
haftmann@23247
  1701
  [code func del]: "if_delayed b f g = (if b then f True else g False)"
haftmann@23247
  1702
haftmann@23247
  1703
lemma [code func]:
haftmann@23247
  1704
  shows "if_delayed True f g = f True"
haftmann@23247
  1705
    and "if_delayed False f g = g False"
haftmann@23247
  1706
  unfolding if_delayed_def by simp_all
haftmann@23247
  1707
haftmann@23247
  1708
lemma [normal pre, symmetric, normal post]:
haftmann@23247
  1709
  "(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)"
haftmann@23247
  1710
  unfolding if_delayed_def ..
haftmann@23247
  1711
haftmann@23247
  1712
hide (open) const if_delayed
haftmann@23247
  1713
haftmann@23247
  1714
haftmann@22839
  1715
subsection {* Legacy tactics and ML bindings *}
wenzelm@21671
  1716
wenzelm@21671
  1717
ML {*
wenzelm@21671
  1718
fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
wenzelm@21671
  1719
wenzelm@21671
  1720
(* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
wenzelm@21671
  1721
local
wenzelm@21671
  1722
  fun wrong_prem (Const ("All", _) $ (Abs (_, _, t))) = wrong_prem t
wenzelm@21671
  1723
    | wrong_prem (Bound _) = true
wenzelm@21671
  1724
    | wrong_prem _ = false;
wenzelm@21671
  1725
  val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
wenzelm@21671
  1726
in
wenzelm@21671
  1727
  fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
wenzelm@21671
  1728
  fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
wenzelm@21671
  1729
end;
haftmann@22839
  1730
haftmann@22839
  1731
val all_conj_distrib = thm "all_conj_distrib";
haftmann@22839
  1732
val all_simps = thms "all_simps";
haftmann@22839
  1733
val atomize_not = thm "atomize_not";
haftmann@22839
  1734
val case_split = thm "case_split_thm";
haftmann@22839
  1735
val case_split_thm = thm "case_split_thm"
haftmann@22839
  1736
val cases_simp = thm "cases_simp";
haftmann@22839
  1737
val choice_eq = thm "choice_eq"
haftmann@22839
  1738
val cong = thm "cong"
haftmann@22839
  1739
val conj_comms = thms "conj_comms";
haftmann@22839
  1740
val conj_cong = thm "conj_cong";
haftmann@22839
  1741
val de_Morgan_conj = thm "de_Morgan_conj";
haftmann@22839
  1742
val de_Morgan_disj = thm "de_Morgan_disj";
haftmann@22839
  1743
val disj_assoc = thm "disj_assoc";
haftmann@22839
  1744
val disj_comms = thms "disj_comms";
haftmann@22839
  1745
val disj_cong = thm "disj_cong";
haftmann@22839
  1746
val eq_ac = thms "eq_ac";
haftmann@22839
  1747
val eq_cong2 = thm "eq_cong2"
haftmann@22839
  1748
val Eq_FalseI = thm "Eq_FalseI";
haftmann@22839
  1749
val Eq_TrueI = thm "Eq_TrueI";
haftmann@22839
  1750
val Ex1_def = thm "Ex1_def"
haftmann@22839
  1751
val ex_disj_distrib = thm "ex_disj_distrib";
haftmann@22839
  1752
val ex_simps = thms "ex_simps";
haftmann@22839
  1753
val if_cancel = thm "if_cancel";
haftmann@22839
  1754
val if_eq_cancel = thm "if_eq_cancel";
haftmann@22839
  1755
val if_False = thm "if_False";
haftmann@22839
  1756
val iff_conv_conj_imp = thm "iff_conv_conj_imp";
haftmann@22839
  1757
val iff = thm "iff"
haftmann@22839
  1758
val if_splits = thms "if_splits";
haftmann@22839
  1759
val if_True = thm "if_True";
haftmann@22839
  1760
val if_weak_cong = thm "if_weak_cong"
haftmann@22839
  1761
val imp_all = thm "imp_all";
haftmann@22839
  1762
val imp_cong = thm "imp_cong";
haftmann@22839
  1763
val imp_conjL = thm "imp_conjL";
haftmann@22839
  1764
val imp_conjR = thm "imp_conjR";
haftmann@22839
  1765
val imp_conv_disj = thm "imp_conv_disj";
haftmann@22839
  1766
val simp_implies_def = thm "simp_implies_def";
haftmann@22839
  1767
val simp_thms = thms "simp_thms";
haftmann@22839
  1768
val split_if = thm "split_if";
haftmann@22839
  1769
val the1_equality = thm "the1_equality"
haftmann@22839
  1770
val theI = thm "theI"
haftmann@22839
  1771
val theI' = thm "theI'"
haftmann@22839
  1772
val True_implies_equals = thm "True_implies_equals";
chaieb@23037
  1773
val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps simp_thms @ @{thms "nnf_simps"})
chaieb@23037
  1774
wenzelm@21671
  1775
*}
wenzelm@21671
  1776
kleing@14357
  1777
end