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