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