src/HOL/HOL.thy
author nipkow
Thu Jun 29 13:52:28 2006 +0200 (2006-06-29)
changeset 19961 5aa2e37e250c
parent 19890 1aad48bcc674
child 19970 d6e238c46d1b
permissions -rw-r--r--
new method "normalization"
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@18595
    10
uses ("cladata.ML") ("blastdata.ML") ("simpdata.ML")
paulson@19174
    11
    "Tools/res_atpset.ML"
avigad@16775
    12
nipkow@15131
    13
begin
wenzelm@2260
    14
wenzelm@11750
    15
subsection {* Primitive logic *}
wenzelm@11750
    16
wenzelm@11750
    17
subsubsection {* Core syntax *}
wenzelm@2260
    18
wenzelm@14854
    19
classes type
wenzelm@12338
    20
defaultsort type
wenzelm@3947
    21
wenzelm@12338
    22
global
clasohm@923
    23
wenzelm@7357
    24
typedecl bool
clasohm@923
    25
clasohm@923
    26
arities
wenzelm@12338
    27
  bool :: type
wenzelm@12338
    28
  fun :: (type, type) type
clasohm@923
    29
wenzelm@11750
    30
judgment
wenzelm@11750
    31
  Trueprop      :: "bool => prop"                   ("(_)" 5)
clasohm@923
    32
wenzelm@11750
    33
consts
wenzelm@7357
    34
  Not           :: "bool => bool"                   ("~ _" [40] 40)
wenzelm@7357
    35
  True          :: bool
wenzelm@7357
    36
  False         :: bool
wenzelm@3947
    37
  arbitrary     :: 'a
clasohm@923
    38
wenzelm@11432
    39
  The           :: "('a => bool) => 'a"
wenzelm@7357
    40
  All           :: "('a => bool) => bool"           (binder "ALL " 10)
wenzelm@7357
    41
  Ex            :: "('a => bool) => bool"           (binder "EX " 10)
wenzelm@7357
    42
  Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
wenzelm@7357
    43
  Let           :: "['a, 'a => 'b] => 'b"
clasohm@923
    44
wenzelm@7357
    45
  "="           :: "['a, 'a] => bool"               (infixl 50)
wenzelm@7357
    46
  &             :: "[bool, bool] => bool"           (infixr 35)
wenzelm@7357
    47
  "|"           :: "[bool, bool] => bool"           (infixr 30)
wenzelm@7357
    48
  -->           :: "[bool, bool] => bool"           (infixr 25)
clasohm@923
    49
wenzelm@10432
    50
local
wenzelm@10432
    51
paulson@16587
    52
consts
paulson@16587
    53
  If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" 10)
wenzelm@2260
    54
wenzelm@19656
    55
wenzelm@11750
    56
subsubsection {* Additional concrete syntax *}
wenzelm@2260
    57
wenzelm@19656
    58
const_syntax (output)
wenzelm@19656
    59
  "op ="  (infix "=" 50)
wenzelm@19656
    60
wenzelm@19656
    61
abbreviation
wenzelm@19656
    62
  not_equal     :: "['a, 'a] => bool"               (infixl "~=" 50)
wenzelm@19656
    63
  "x ~= y == ~ (x = y)"
wenzelm@19656
    64
wenzelm@19656
    65
const_syntax (output)
wenzelm@19656
    66
  not_equal  (infix "~=" 50)
wenzelm@19656
    67
wenzelm@19656
    68
const_syntax (xsymbols)
wenzelm@19656
    69
  Not  ("\<not> _" [40] 40)
wenzelm@19656
    70
  "op &"  (infixr "\<and>" 35)
wenzelm@19656
    71
  "op |"  (infixr "\<or>" 30)
wenzelm@19656
    72
  "op -->"  (infixr "\<longrightarrow>" 25)
wenzelm@19656
    73
  not_equal  (infix "\<noteq>" 50)
wenzelm@19656
    74
wenzelm@19656
    75
const_syntax (HTML output)
wenzelm@19656
    76
  Not  ("\<not> _" [40] 40)
wenzelm@19656
    77
  "op &"  (infixr "\<and>" 35)
wenzelm@19656
    78
  "op |"  (infixr "\<or>" 30)
wenzelm@19656
    79
  not_equal  (infix "\<noteq>" 50)
wenzelm@19656
    80
wenzelm@19656
    81
abbreviation (iff)
wenzelm@19656
    82
  iff :: "[bool, bool] => bool"  (infixr "<->" 25)
wenzelm@19656
    83
  "A <-> B == A = B"
wenzelm@19656
    84
wenzelm@19656
    85
const_syntax (xsymbols)
wenzelm@19656
    86
  iff  (infixr "\<longleftrightarrow>" 25)
wenzelm@19656
    87
wenzelm@19656
    88
wenzelm@4868
    89
nonterminals
clasohm@923
    90
  letbinds  letbind
clasohm@923
    91
  case_syn  cases_syn
clasohm@923
    92
clasohm@923
    93
syntax
wenzelm@11432
    94
  "_The"        :: "[pttrn, bool] => 'a"                 ("(3THE _./ _)" [0, 10] 10)
clasohm@923
    95
wenzelm@7357
    96
  "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
wenzelm@7357
    97
  ""            :: "letbind => letbinds"                 ("_")
wenzelm@7357
    98
  "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
wenzelm@7357
    99
  "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" 10)
clasohm@923
   100
wenzelm@9060
   101
  "_case_syntax":: "['a, cases_syn] => 'b"               ("(case _ of/ _)" 10)
wenzelm@9060
   102
  "_case1"      :: "['a, 'b] => case_syn"                ("(2_ =>/ _)" 10)
wenzelm@7357
   103
  ""            :: "case_syn => cases_syn"               ("_")
wenzelm@9060
   104
  "_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
clasohm@923
   105
clasohm@923
   106
translations
nipkow@13764
   107
  "THE x. P"              == "The (%x. P)"
clasohm@923
   108
  "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
nipkow@1114
   109
  "let x = a in e"        == "Let a (%x. e)"
clasohm@923
   110
nipkow@13763
   111
print_translation {*
nipkow@13763
   112
(* To avoid eta-contraction of body: *)
nipkow@13763
   113
[("The", fn [Abs abs] =>
nipkow@13763
   114
     let val (x,t) = atomic_abs_tr' abs
nipkow@13763
   115
     in Syntax.const "_The" $ x $ t end)]
nipkow@13763
   116
*}
nipkow@13763
   117
wenzelm@12114
   118
syntax (xsymbols)
wenzelm@11687
   119
  "ALL "        :: "[idts, bool] => bool"                ("(3\<forall>_./ _)" [0, 10] 10)
wenzelm@11687
   120
  "EX "         :: "[idts, bool] => bool"                ("(3\<exists>_./ _)" [0, 10] 10)
wenzelm@11687
   121
  "EX! "        :: "[idts, bool] => bool"                ("(3\<exists>!_./ _)" [0, 10] 10)
wenzelm@11687
   122
  "_case1"      :: "['a, 'b] => case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
schirmer@14361
   123
(*"_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ \<orelse> _")*)
wenzelm@2372
   124
wenzelm@6340
   125
syntax (HTML output)
kleing@14565
   126
  "ALL "        :: "[idts, bool] => bool"                ("(3\<forall>_./ _)" [0, 10] 10)
kleing@14565
   127
  "EX "         :: "[idts, bool] => bool"                ("(3\<exists>_./ _)" [0, 10] 10)
kleing@14565
   128
  "EX! "        :: "[idts, bool] => bool"                ("(3\<exists>!_./ _)" [0, 10] 10)
wenzelm@6340
   129
wenzelm@7238
   130
syntax (HOL)
wenzelm@7357
   131
  "ALL "        :: "[idts, bool] => bool"                ("(3! _./ _)" [0, 10] 10)
wenzelm@7357
   132
  "EX "         :: "[idts, bool] => bool"                ("(3? _./ _)" [0, 10] 10)
wenzelm@7357
   133
  "EX! "        :: "[idts, bool] => bool"                ("(3?! _./ _)" [0, 10] 10)
wenzelm@7238
   134
wenzelm@7238
   135
wenzelm@11750
   136
subsubsection {* Axioms and basic definitions *}
wenzelm@2260
   137
wenzelm@7357
   138
axioms
paulson@15380
   139
  eq_reflection:  "(x=y) ==> (x==y)"
clasohm@923
   140
paulson@15380
   141
  refl:           "t = (t::'a)"
paulson@6289
   142
paulson@15380
   143
  ext:            "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
paulson@15380
   144
    -- {*Extensionality is built into the meta-logic, and this rule expresses
paulson@15380
   145
         a related property.  It is an eta-expanded version of the traditional
paulson@15380
   146
         rule, and similar to the ABS rule of HOL*}
paulson@6289
   147
wenzelm@11432
   148
  the_eq_trivial: "(THE x. x = a) = (a::'a)"
clasohm@923
   149
paulson@15380
   150
  impI:           "(P ==> Q) ==> P-->Q"
paulson@15380
   151
  mp:             "[| P-->Q;  P |] ==> Q"
paulson@15380
   152
paulson@15380
   153
paulson@15380
   154
text{*Thanks to Stephan Merz*}
paulson@15380
   155
theorem subst:
paulson@15380
   156
  assumes eq: "s = t" and p: "P(s)"
paulson@15380
   157
  shows "P(t::'a)"
paulson@15380
   158
proof -
paulson@15380
   159
  from eq have meta: "s \<equiv> t"
paulson@15380
   160
    by (rule eq_reflection)
paulson@15380
   161
  from p show ?thesis
paulson@15380
   162
    by (unfold meta)
paulson@15380
   163
qed
clasohm@923
   164
clasohm@923
   165
defs
wenzelm@7357
   166
  True_def:     "True      == ((%x::bool. x) = (%x. x))"
wenzelm@7357
   167
  All_def:      "All(P)    == (P = (%x. True))"
paulson@11451
   168
  Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
wenzelm@7357
   169
  False_def:    "False     == (!P. P)"
wenzelm@7357
   170
  not_def:      "~ P       == P-->False"
wenzelm@7357
   171
  and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
wenzelm@7357
   172
  or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
wenzelm@7357
   173
  Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
clasohm@923
   174
wenzelm@7357
   175
axioms
wenzelm@7357
   176
  iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
wenzelm@7357
   177
  True_or_False:  "(P=True) | (P=False)"
clasohm@923
   178
clasohm@923
   179
defs
wenzelm@7357
   180
  Let_def:      "Let s f == f(s)"
paulson@11451
   181
  if_def:       "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
wenzelm@5069
   182
skalberg@14223
   183
finalconsts
skalberg@14223
   184
  "op ="
skalberg@14223
   185
  "op -->"
skalberg@14223
   186
  The
skalberg@14223
   187
  arbitrary
nipkow@3320
   188
wenzelm@19656
   189
wenzelm@11750
   190
subsubsection {* Generic algebraic operations *}
wenzelm@4868
   191
wenzelm@12338
   192
axclass zero < type
wenzelm@12338
   193
axclass one < type
wenzelm@12338
   194
axclass plus < type
wenzelm@12338
   195
axclass minus < type
wenzelm@12338
   196
axclass times < type
wenzelm@12338
   197
axclass inverse < type
wenzelm@11750
   198
haftmann@19233
   199
consts
haftmann@19233
   200
  plus             :: "['a::plus, 'a]  => 'a"       (infixl "+" 65)
haftmann@19233
   201
  uminus           :: "'a::minus => 'a"             ("- _" [81] 80)
haftmann@19233
   202
  minus            :: "['a::minus, 'a] => 'a"       (infixl "-" 65)
haftmann@19233
   203
  abs              :: "'a::minus => 'a"
haftmann@19233
   204
  times            :: "['a::times, 'a] => 'a"       (infixl "*" 70)
haftmann@19233
   205
  inverse          :: "'a::inverse => 'a"
haftmann@19233
   206
  divide           :: "['a::inverse, 'a] => 'a"     (infixl "'/" 70)
haftmann@19233
   207
wenzelm@11750
   208
global
wenzelm@11750
   209
wenzelm@11750
   210
consts
wenzelm@11750
   211
  "0"           :: "'a::zero"                       ("0")
wenzelm@11750
   212
  "1"           :: "'a::one"                        ("1")
wenzelm@11750
   213
wenzelm@13456
   214
syntax
wenzelm@13456
   215
  "_index1"  :: index    ("\<^sub>1")
wenzelm@13456
   216
translations
wenzelm@14690
   217
  (index) "\<^sub>1" => (index) "\<^bsub>\<struct>\<^esub>"
wenzelm@13456
   218
wenzelm@11750
   219
local
wenzelm@11750
   220
wenzelm@11750
   221
typed_print_translation {*
wenzelm@11750
   222
  let
wenzelm@11750
   223
    fun tr' c = (c, fn show_sorts => fn T => fn ts =>
wenzelm@11750
   224
      if T = dummyT orelse not (! show_types) andalso can Term.dest_Type T then raise Match
wenzelm@11750
   225
      else Syntax.const Syntax.constrainC $ Syntax.const c $ Syntax.term_of_typ show_sorts T);
wenzelm@11750
   226
  in [tr' "0", tr' "1"] end;
wenzelm@11750
   227
*} -- {* show types that are presumably too general *}
wenzelm@11750
   228
wenzelm@11750
   229
syntax (xsymbols)
wenzelm@11750
   230
  abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
wenzelm@11750
   231
syntax (HTML output)
wenzelm@11750
   232
  abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
wenzelm@11750
   233
wenzelm@11750
   234
paulson@15411
   235
subsection {*Equality*}
paulson@15411
   236
wenzelm@18457
   237
lemma sym: "s = t ==> t = s"
wenzelm@18457
   238
  by (erule subst) (rule refl)
paulson@15411
   239
wenzelm@18457
   240
lemma ssubst: "t = s ==> P s ==> P t"
wenzelm@18457
   241
  by (drule sym) (erule subst)
paulson@15411
   242
paulson@15411
   243
lemma trans: "[| r=s; s=t |] ==> r=t"
wenzelm@18457
   244
  by (erule subst)
paulson@15411
   245
wenzelm@18457
   246
lemma def_imp_eq: assumes meq: "A == B" shows "A = B"
wenzelm@18457
   247
  by (unfold meq) (rule refl)
wenzelm@18457
   248
paulson@15411
   249
paulson@15411
   250
(*Useful with eresolve_tac for proving equalties from known equalities.
paulson@15411
   251
        a = b
paulson@15411
   252
        |   |
paulson@15411
   253
        c = d   *)
paulson@15411
   254
lemma box_equals: "[| a=b;  a=c;  b=d |] ==> c=d"
paulson@15411
   255
apply (rule trans)
paulson@15411
   256
apply (rule trans)
paulson@15411
   257
apply (rule sym)
paulson@15411
   258
apply assumption+
paulson@15411
   259
done
paulson@15411
   260
nipkow@15524
   261
text {* For calculational reasoning: *}
nipkow@15524
   262
nipkow@15524
   263
lemma forw_subst: "a = b ==> P b ==> P a"
nipkow@15524
   264
  by (rule ssubst)
nipkow@15524
   265
nipkow@15524
   266
lemma back_subst: "P a ==> a = b ==> P b"
nipkow@15524
   267
  by (rule subst)
nipkow@15524
   268
paulson@15411
   269
paulson@15411
   270
subsection {*Congruence rules for application*}
paulson@15411
   271
paulson@15411
   272
(*similar to AP_THM in Gordon's HOL*)
paulson@15411
   273
lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
paulson@15411
   274
apply (erule subst)
paulson@15411
   275
apply (rule refl)
paulson@15411
   276
done
paulson@15411
   277
paulson@15411
   278
(*similar to AP_TERM in Gordon's HOL and FOL's subst_context*)
paulson@15411
   279
lemma arg_cong: "x=y ==> f(x)=f(y)"
paulson@15411
   280
apply (erule subst)
paulson@15411
   281
apply (rule refl)
paulson@15411
   282
done
paulson@15411
   283
paulson@15655
   284
lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
paulson@15655
   285
apply (erule ssubst)+
paulson@15655
   286
apply (rule refl)
paulson@15655
   287
done
paulson@15655
   288
paulson@15655
   289
paulson@15411
   290
lemma cong: "[| f = g; (x::'a) = y |] ==> f(x) = g(y)"
paulson@15411
   291
apply (erule subst)+
paulson@15411
   292
apply (rule refl)
paulson@15411
   293
done
paulson@15411
   294
paulson@15411
   295
paulson@15411
   296
subsection {*Equality of booleans -- iff*}
paulson@15411
   297
paulson@15411
   298
lemma iffI: assumes prems: "P ==> Q" "Q ==> P" shows "P=Q"
wenzelm@18457
   299
  by (iprover intro: iff [THEN mp, THEN mp] impI prems)
paulson@15411
   300
paulson@15411
   301
lemma iffD2: "[| P=Q; Q |] ==> P"
wenzelm@18457
   302
  by (erule ssubst)
paulson@15411
   303
paulson@15411
   304
lemma rev_iffD2: "[| Q; P=Q |] ==> P"
wenzelm@18457
   305
  by (erule iffD2)
paulson@15411
   306
paulson@15411
   307
lemmas iffD1 = sym [THEN iffD2, standard]
paulson@15411
   308
lemmas rev_iffD1 = sym [THEN [2] rev_iffD2, standard]
paulson@15411
   309
paulson@15411
   310
lemma iffE:
paulson@15411
   311
  assumes major: "P=Q"
paulson@15411
   312
      and minor: "[| P --> Q; Q --> P |] ==> R"
wenzelm@18457
   313
  shows R
wenzelm@18457
   314
  by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
paulson@15411
   315
paulson@15411
   316
paulson@15411
   317
subsection {*True*}
paulson@15411
   318
paulson@15411
   319
lemma TrueI: "True"
wenzelm@18457
   320
  by (unfold True_def) (rule refl)
paulson@15411
   321
paulson@15411
   322
lemma eqTrueI: "P ==> P=True"
wenzelm@18457
   323
  by (iprover intro: iffI TrueI)
paulson@15411
   324
paulson@15411
   325
lemma eqTrueE: "P=True ==> P"
paulson@15411
   326
apply (erule iffD2)
paulson@15411
   327
apply (rule TrueI)
paulson@15411
   328
done
paulson@15411
   329
paulson@15411
   330
paulson@15411
   331
subsection {*Universal quantifier*}
paulson@15411
   332
paulson@15411
   333
lemma allI: assumes p: "!!x::'a. P(x)" shows "ALL x. P(x)"
paulson@15411
   334
apply (unfold All_def)
nipkow@17589
   335
apply (iprover intro: ext eqTrueI p)
paulson@15411
   336
done
paulson@15411
   337
paulson@15411
   338
lemma spec: "ALL x::'a. P(x) ==> P(x)"
paulson@15411
   339
apply (unfold All_def)
paulson@15411
   340
apply (rule eqTrueE)
paulson@15411
   341
apply (erule fun_cong)
paulson@15411
   342
done
paulson@15411
   343
paulson@15411
   344
lemma allE:
paulson@15411
   345
  assumes major: "ALL x. P(x)"
paulson@15411
   346
      and minor: "P(x) ==> R"
paulson@15411
   347
  shows "R"
nipkow@17589
   348
by (iprover intro: minor major [THEN spec])
paulson@15411
   349
paulson@15411
   350
lemma all_dupE:
paulson@15411
   351
  assumes major: "ALL x. P(x)"
paulson@15411
   352
      and minor: "[| P(x); ALL x. P(x) |] ==> R"
paulson@15411
   353
  shows "R"
nipkow@17589
   354
by (iprover intro: minor major major [THEN spec])
paulson@15411
   355
paulson@15411
   356
paulson@15411
   357
subsection {*False*}
paulson@15411
   358
(*Depends upon spec; it is impossible to do propositional logic before quantifiers!*)
paulson@15411
   359
paulson@15411
   360
lemma FalseE: "False ==> P"
paulson@15411
   361
apply (unfold False_def)
paulson@15411
   362
apply (erule spec)
paulson@15411
   363
done
paulson@15411
   364
paulson@15411
   365
lemma False_neq_True: "False=True ==> P"
paulson@15411
   366
by (erule eqTrueE [THEN FalseE])
paulson@15411
   367
paulson@15411
   368
paulson@15411
   369
subsection {*Negation*}
paulson@15411
   370
paulson@15411
   371
lemma notI:
paulson@15411
   372
  assumes p: "P ==> False"
paulson@15411
   373
  shows "~P"
paulson@15411
   374
apply (unfold not_def)
nipkow@17589
   375
apply (iprover intro: impI p)
paulson@15411
   376
done
paulson@15411
   377
paulson@15411
   378
lemma False_not_True: "False ~= True"
paulson@15411
   379
apply (rule notI)
paulson@15411
   380
apply (erule False_neq_True)
paulson@15411
   381
done
paulson@15411
   382
paulson@15411
   383
lemma True_not_False: "True ~= False"
paulson@15411
   384
apply (rule notI)
paulson@15411
   385
apply (drule sym)
paulson@15411
   386
apply (erule False_neq_True)
paulson@15411
   387
done
paulson@15411
   388
paulson@15411
   389
lemma notE: "[| ~P;  P |] ==> R"
paulson@15411
   390
apply (unfold not_def)
paulson@15411
   391
apply (erule mp [THEN FalseE])
paulson@15411
   392
apply assumption
paulson@15411
   393
done
paulson@15411
   394
paulson@15411
   395
(* Alternative ~ introduction rule: [| P ==> ~ Pa; P ==> Pa |] ==> ~ P *)
paulson@15411
   396
lemmas notI2 = notE [THEN notI, standard]
paulson@15411
   397
paulson@15411
   398
paulson@15411
   399
subsection {*Implication*}
paulson@15411
   400
paulson@15411
   401
lemma impE:
paulson@15411
   402
  assumes "P-->Q" "P" "Q ==> R"
paulson@15411
   403
  shows "R"
nipkow@17589
   404
by (iprover intro: prems mp)
paulson@15411
   405
paulson@15411
   406
(* Reduces Q to P-->Q, allowing substitution in P. *)
paulson@15411
   407
lemma rev_mp: "[| P;  P --> Q |] ==> Q"
nipkow@17589
   408
by (iprover intro: mp)
paulson@15411
   409
paulson@15411
   410
lemma contrapos_nn:
paulson@15411
   411
  assumes major: "~Q"
paulson@15411
   412
      and minor: "P==>Q"
paulson@15411
   413
  shows "~P"
nipkow@17589
   414
by (iprover intro: notI minor major [THEN notE])
paulson@15411
   415
paulson@15411
   416
(*not used at all, but we already have the other 3 combinations *)
paulson@15411
   417
lemma contrapos_pn:
paulson@15411
   418
  assumes major: "Q"
paulson@15411
   419
      and minor: "P ==> ~Q"
paulson@15411
   420
  shows "~P"
nipkow@17589
   421
by (iprover intro: notI minor major notE)
paulson@15411
   422
paulson@15411
   423
lemma not_sym: "t ~= s ==> s ~= t"
paulson@15411
   424
apply (erule contrapos_nn)
paulson@15411
   425
apply (erule sym)
paulson@15411
   426
done
paulson@15411
   427
paulson@15411
   428
(*still used in HOLCF*)
paulson@15411
   429
lemma rev_contrapos:
paulson@15411
   430
  assumes pq: "P ==> Q"
paulson@15411
   431
      and nq: "~Q"
paulson@15411
   432
  shows "~P"
paulson@15411
   433
apply (rule nq [THEN contrapos_nn])
paulson@15411
   434
apply (erule pq)
paulson@15411
   435
done
paulson@15411
   436
paulson@15411
   437
subsection {*Existential quantifier*}
paulson@15411
   438
paulson@15411
   439
lemma exI: "P x ==> EX x::'a. P x"
paulson@15411
   440
apply (unfold Ex_def)
nipkow@17589
   441
apply (iprover intro: allI allE impI mp)
paulson@15411
   442
done
paulson@15411
   443
paulson@15411
   444
lemma exE:
paulson@15411
   445
  assumes major: "EX x::'a. P(x)"
paulson@15411
   446
      and minor: "!!x. P(x) ==> Q"
paulson@15411
   447
  shows "Q"
paulson@15411
   448
apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
nipkow@17589
   449
apply (iprover intro: impI [THEN allI] minor)
paulson@15411
   450
done
paulson@15411
   451
paulson@15411
   452
paulson@15411
   453
subsection {*Conjunction*}
paulson@15411
   454
paulson@15411
   455
lemma conjI: "[| P; Q |] ==> P&Q"
paulson@15411
   456
apply (unfold and_def)
nipkow@17589
   457
apply (iprover intro: impI [THEN allI] mp)
paulson@15411
   458
done
paulson@15411
   459
paulson@15411
   460
lemma conjunct1: "[| P & Q |] ==> P"
paulson@15411
   461
apply (unfold and_def)
nipkow@17589
   462
apply (iprover intro: impI dest: spec mp)
paulson@15411
   463
done
paulson@15411
   464
paulson@15411
   465
lemma conjunct2: "[| P & Q |] ==> Q"
paulson@15411
   466
apply (unfold and_def)
nipkow@17589
   467
apply (iprover intro: impI dest: spec mp)
paulson@15411
   468
done
paulson@15411
   469
paulson@15411
   470
lemma conjE:
paulson@15411
   471
  assumes major: "P&Q"
paulson@15411
   472
      and minor: "[| P; Q |] ==> R"
paulson@15411
   473
  shows "R"
paulson@15411
   474
apply (rule minor)
paulson@15411
   475
apply (rule major [THEN conjunct1])
paulson@15411
   476
apply (rule major [THEN conjunct2])
paulson@15411
   477
done
paulson@15411
   478
paulson@15411
   479
lemma context_conjI:
paulson@15411
   480
  assumes prems: "P" "P ==> Q" shows "P & Q"
nipkow@17589
   481
by (iprover intro: conjI prems)
paulson@15411
   482
paulson@15411
   483
paulson@15411
   484
subsection {*Disjunction*}
paulson@15411
   485
paulson@15411
   486
lemma disjI1: "P ==> P|Q"
paulson@15411
   487
apply (unfold or_def)
nipkow@17589
   488
apply (iprover intro: allI impI mp)
paulson@15411
   489
done
paulson@15411
   490
paulson@15411
   491
lemma disjI2: "Q ==> P|Q"
paulson@15411
   492
apply (unfold or_def)
nipkow@17589
   493
apply (iprover intro: allI impI mp)
paulson@15411
   494
done
paulson@15411
   495
paulson@15411
   496
lemma disjE:
paulson@15411
   497
  assumes major: "P|Q"
paulson@15411
   498
      and minorP: "P ==> R"
paulson@15411
   499
      and minorQ: "Q ==> R"
paulson@15411
   500
  shows "R"
nipkow@17589
   501
by (iprover intro: minorP minorQ impI
paulson@15411
   502
                 major [unfolded or_def, THEN spec, THEN mp, THEN mp])
paulson@15411
   503
paulson@15411
   504
paulson@15411
   505
subsection {*Classical logic*}
paulson@15411
   506
paulson@15411
   507
paulson@15411
   508
lemma classical:
paulson@15411
   509
  assumes prem: "~P ==> P"
paulson@15411
   510
  shows "P"
paulson@15411
   511
apply (rule True_or_False [THEN disjE, THEN eqTrueE])
paulson@15411
   512
apply assumption
paulson@15411
   513
apply (rule notI [THEN prem, THEN eqTrueI])
paulson@15411
   514
apply (erule subst)
paulson@15411
   515
apply assumption
paulson@15411
   516
done
paulson@15411
   517
paulson@15411
   518
lemmas ccontr = FalseE [THEN classical, standard]
paulson@15411
   519
paulson@15411
   520
(*notE with premises exchanged; it discharges ~R so that it can be used to
paulson@15411
   521
  make elimination rules*)
paulson@15411
   522
lemma rev_notE:
paulson@15411
   523
  assumes premp: "P"
paulson@15411
   524
      and premnot: "~R ==> ~P"
paulson@15411
   525
  shows "R"
paulson@15411
   526
apply (rule ccontr)
paulson@15411
   527
apply (erule notE [OF premnot premp])
paulson@15411
   528
done
paulson@15411
   529
paulson@15411
   530
(*Double negation law*)
paulson@15411
   531
lemma notnotD: "~~P ==> P"
paulson@15411
   532
apply (rule classical)
paulson@15411
   533
apply (erule notE)
paulson@15411
   534
apply assumption
paulson@15411
   535
done
paulson@15411
   536
paulson@15411
   537
lemma contrapos_pp:
paulson@15411
   538
  assumes p1: "Q"
paulson@15411
   539
      and p2: "~P ==> ~Q"
paulson@15411
   540
  shows "P"
nipkow@17589
   541
by (iprover intro: classical p1 p2 notE)
paulson@15411
   542
paulson@15411
   543
paulson@15411
   544
subsection {*Unique existence*}
paulson@15411
   545
paulson@15411
   546
lemma ex1I:
paulson@15411
   547
  assumes prems: "P a" "!!x. P(x) ==> x=a"
paulson@15411
   548
  shows "EX! x. P(x)"
nipkow@17589
   549
by (unfold Ex1_def, iprover intro: prems exI conjI allI impI)
paulson@15411
   550
paulson@15411
   551
text{*Sometimes easier to use: the premises have no shared variables.  Safe!*}
paulson@15411
   552
lemma ex_ex1I:
paulson@15411
   553
  assumes ex_prem: "EX x. P(x)"
paulson@15411
   554
      and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
paulson@15411
   555
  shows "EX! x. P(x)"
nipkow@17589
   556
by (iprover intro: ex_prem [THEN exE] ex1I eq)
paulson@15411
   557
paulson@15411
   558
lemma ex1E:
paulson@15411
   559
  assumes major: "EX! x. P(x)"
paulson@15411
   560
      and minor: "!!x. [| P(x);  ALL y. P(y) --> y=x |] ==> R"
paulson@15411
   561
  shows "R"
paulson@15411
   562
apply (rule major [unfolded Ex1_def, THEN exE])
paulson@15411
   563
apply (erule conjE)
nipkow@17589
   564
apply (iprover intro: minor)
paulson@15411
   565
done
paulson@15411
   566
paulson@15411
   567
lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
paulson@15411
   568
apply (erule ex1E)
paulson@15411
   569
apply (rule exI)
paulson@15411
   570
apply assumption
paulson@15411
   571
done
paulson@15411
   572
paulson@15411
   573
paulson@15411
   574
subsection {*THE: definite description operator*}
paulson@15411
   575
paulson@15411
   576
lemma the_equality:
paulson@15411
   577
  assumes prema: "P a"
paulson@15411
   578
      and premx: "!!x. P x ==> x=a"
paulson@15411
   579
  shows "(THE x. P x) = a"
paulson@15411
   580
apply (rule trans [OF _ the_eq_trivial])
paulson@15411
   581
apply (rule_tac f = "The" in arg_cong)
paulson@15411
   582
apply (rule ext)
paulson@15411
   583
apply (rule iffI)
paulson@15411
   584
 apply (erule premx)
paulson@15411
   585
apply (erule ssubst, rule prema)
paulson@15411
   586
done
paulson@15411
   587
paulson@15411
   588
lemma theI:
paulson@15411
   589
  assumes "P a" and "!!x. P x ==> x=a"
paulson@15411
   590
  shows "P (THE x. P x)"
nipkow@17589
   591
by (iprover intro: prems the_equality [THEN ssubst])
paulson@15411
   592
paulson@15411
   593
lemma theI': "EX! x. P x ==> P (THE x. P x)"
paulson@15411
   594
apply (erule ex1E)
paulson@15411
   595
apply (erule theI)
paulson@15411
   596
apply (erule allE)
paulson@15411
   597
apply (erule mp)
paulson@15411
   598
apply assumption
paulson@15411
   599
done
paulson@15411
   600
paulson@15411
   601
(*Easier to apply than theI: only one occurrence of P*)
paulson@15411
   602
lemma theI2:
paulson@15411
   603
  assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
paulson@15411
   604
  shows "Q (THE x. P x)"
nipkow@17589
   605
by (iprover intro: prems theI)
paulson@15411
   606
wenzelm@18697
   607
lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
paulson@15411
   608
apply (rule the_equality)
paulson@15411
   609
apply  assumption
paulson@15411
   610
apply (erule ex1E)
paulson@15411
   611
apply (erule all_dupE)
paulson@15411
   612
apply (drule mp)
paulson@15411
   613
apply  assumption
paulson@15411
   614
apply (erule ssubst)
paulson@15411
   615
apply (erule allE)
paulson@15411
   616
apply (erule mp)
paulson@15411
   617
apply assumption
paulson@15411
   618
done
paulson@15411
   619
paulson@15411
   620
lemma the_sym_eq_trivial: "(THE y. x=y) = x"
paulson@15411
   621
apply (rule the_equality)
paulson@15411
   622
apply (rule refl)
paulson@15411
   623
apply (erule sym)
paulson@15411
   624
done
paulson@15411
   625
paulson@15411
   626
paulson@15411
   627
subsection {*Classical intro rules for disjunction and existential quantifiers*}
paulson@15411
   628
paulson@15411
   629
lemma disjCI:
paulson@15411
   630
  assumes "~Q ==> P" shows "P|Q"
paulson@15411
   631
apply (rule classical)
nipkow@17589
   632
apply (iprover intro: prems disjI1 disjI2 notI elim: notE)
paulson@15411
   633
done
paulson@15411
   634
paulson@15411
   635
lemma excluded_middle: "~P | P"
nipkow@17589
   636
by (iprover intro: disjCI)
paulson@15411
   637
paulson@15411
   638
text{*case distinction as a natural deduction rule. Note that @{term "~P"}
paulson@15411
   639
   is the second case, not the first.*}
paulson@15411
   640
lemma case_split_thm:
paulson@15411
   641
  assumes prem1: "P ==> Q"
paulson@15411
   642
      and prem2: "~P ==> Q"
paulson@15411
   643
  shows "Q"
paulson@15411
   644
apply (rule excluded_middle [THEN disjE])
paulson@15411
   645
apply (erule prem2)
paulson@15411
   646
apply (erule prem1)
paulson@15411
   647
done
paulson@15411
   648
paulson@15411
   649
(*Classical implies (-->) elimination. *)
paulson@15411
   650
lemma impCE:
paulson@15411
   651
  assumes major: "P-->Q"
paulson@15411
   652
      and minor: "~P ==> R" "Q ==> R"
paulson@15411
   653
  shows "R"
paulson@15411
   654
apply (rule excluded_middle [of P, THEN disjE])
nipkow@17589
   655
apply (iprover intro: minor major [THEN mp])+
paulson@15411
   656
done
paulson@15411
   657
paulson@15411
   658
(*This version of --> elimination works on Q before P.  It works best for
paulson@15411
   659
  those cases in which P holds "almost everywhere".  Can't install as
paulson@15411
   660
  default: would break old proofs.*)
paulson@15411
   661
lemma impCE':
paulson@15411
   662
  assumes major: "P-->Q"
paulson@15411
   663
      and minor: "Q ==> R" "~P ==> R"
paulson@15411
   664
  shows "R"
paulson@15411
   665
apply (rule excluded_middle [of P, THEN disjE])
nipkow@17589
   666
apply (iprover intro: minor major [THEN mp])+
paulson@15411
   667
done
paulson@15411
   668
paulson@15411
   669
(*Classical <-> elimination. *)
paulson@15411
   670
lemma iffCE:
paulson@15411
   671
  assumes major: "P=Q"
paulson@15411
   672
      and minor: "[| P; Q |] ==> R"  "[| ~P; ~Q |] ==> R"
paulson@15411
   673
  shows "R"
paulson@15411
   674
apply (rule major [THEN iffE])
nipkow@17589
   675
apply (iprover intro: minor elim: impCE notE)
paulson@15411
   676
done
paulson@15411
   677
paulson@15411
   678
lemma exCI:
paulson@15411
   679
  assumes "ALL x. ~P(x) ==> P(a)"
paulson@15411
   680
  shows "EX x. P(x)"
paulson@15411
   681
apply (rule ccontr)
nipkow@17589
   682
apply (iprover intro: prems exI allI notI notE [of "\<exists>x. P x"])
paulson@15411
   683
done
paulson@15411
   684
paulson@15411
   685
paulson@15411
   686
wenzelm@11750
   687
subsection {* Theory and package setup *}
wenzelm@11750
   688
paulson@15411
   689
ML
paulson@15411
   690
{*
paulson@15411
   691
val eq_reflection = thm "eq_reflection"
paulson@15411
   692
val refl = thm "refl"
paulson@15411
   693
val subst = thm "subst"
paulson@15411
   694
val ext = thm "ext"
paulson@15411
   695
val impI = thm "impI"
paulson@15411
   696
val mp = thm "mp"
paulson@15411
   697
val True_def = thm "True_def"
paulson@15411
   698
val All_def = thm "All_def"
paulson@15411
   699
val Ex_def = thm "Ex_def"
paulson@15411
   700
val False_def = thm "False_def"
paulson@15411
   701
val not_def = thm "not_def"
paulson@15411
   702
val and_def = thm "and_def"
paulson@15411
   703
val or_def = thm "or_def"
paulson@15411
   704
val Ex1_def = thm "Ex1_def"
paulson@15411
   705
val iff = thm "iff"
paulson@15411
   706
val True_or_False = thm "True_or_False"
paulson@15411
   707
val Let_def = thm "Let_def"
paulson@15411
   708
val if_def = thm "if_def"
paulson@15411
   709
val sym = thm "sym"
paulson@15411
   710
val ssubst = thm "ssubst"
paulson@15411
   711
val trans = thm "trans"
paulson@15411
   712
val def_imp_eq = thm "def_imp_eq"
paulson@15411
   713
val box_equals = thm "box_equals"
paulson@15411
   714
val fun_cong = thm "fun_cong"
paulson@15411
   715
val arg_cong = thm "arg_cong"
paulson@15411
   716
val cong = thm "cong"
paulson@15411
   717
val iffI = thm "iffI"
paulson@15411
   718
val iffD2 = thm "iffD2"
paulson@15411
   719
val rev_iffD2 = thm "rev_iffD2"
paulson@15411
   720
val iffD1 = thm "iffD1"
paulson@15411
   721
val rev_iffD1 = thm "rev_iffD1"
paulson@15411
   722
val iffE = thm "iffE"
paulson@15411
   723
val TrueI = thm "TrueI"
paulson@15411
   724
val eqTrueI = thm "eqTrueI"
paulson@15411
   725
val eqTrueE = thm "eqTrueE"
paulson@15411
   726
val allI = thm "allI"
paulson@15411
   727
val spec = thm "spec"
paulson@15411
   728
val allE = thm "allE"
paulson@15411
   729
val all_dupE = thm "all_dupE"
paulson@15411
   730
val FalseE = thm "FalseE"
paulson@15411
   731
val False_neq_True = thm "False_neq_True"
paulson@15411
   732
val notI = thm "notI"
paulson@15411
   733
val False_not_True = thm "False_not_True"
paulson@15411
   734
val True_not_False = thm "True_not_False"
paulson@15411
   735
val notE = thm "notE"
paulson@15411
   736
val notI2 = thm "notI2"
paulson@15411
   737
val impE = thm "impE"
paulson@15411
   738
val rev_mp = thm "rev_mp"
paulson@15411
   739
val contrapos_nn = thm "contrapos_nn"
paulson@15411
   740
val contrapos_pn = thm "contrapos_pn"
paulson@15411
   741
val not_sym = thm "not_sym"
paulson@15411
   742
val rev_contrapos = thm "rev_contrapos"
paulson@15411
   743
val exI = thm "exI"
paulson@15411
   744
val exE = thm "exE"
paulson@15411
   745
val conjI = thm "conjI"
paulson@15411
   746
val conjunct1 = thm "conjunct1"
paulson@15411
   747
val conjunct2 = thm "conjunct2"
paulson@15411
   748
val conjE = thm "conjE"
paulson@15411
   749
val context_conjI = thm "context_conjI"
paulson@15411
   750
val disjI1 = thm "disjI1"
paulson@15411
   751
val disjI2 = thm "disjI2"
paulson@15411
   752
val disjE = thm "disjE"
paulson@15411
   753
val classical = thm "classical"
paulson@15411
   754
val ccontr = thm "ccontr"
paulson@15411
   755
val rev_notE = thm "rev_notE"
paulson@15411
   756
val notnotD = thm "notnotD"
paulson@15411
   757
val contrapos_pp = thm "contrapos_pp"
paulson@15411
   758
val ex1I = thm "ex1I"
paulson@15411
   759
val ex_ex1I = thm "ex_ex1I"
paulson@15411
   760
val ex1E = thm "ex1E"
paulson@15411
   761
val ex1_implies_ex = thm "ex1_implies_ex"
paulson@15411
   762
val the_equality = thm "the_equality"
paulson@15411
   763
val theI = thm "theI"
paulson@15411
   764
val theI' = thm "theI'"
paulson@15411
   765
val theI2 = thm "theI2"
paulson@15411
   766
val the1_equality = thm "the1_equality"
paulson@15411
   767
val the_sym_eq_trivial = thm "the_sym_eq_trivial"
paulson@15411
   768
val disjCI = thm "disjCI"
paulson@15411
   769
val excluded_middle = thm "excluded_middle"
paulson@15411
   770
val case_split_thm = thm "case_split_thm"
paulson@15411
   771
val impCE = thm "impCE"
paulson@15411
   772
val impCE = thm "impCE"
paulson@15411
   773
val iffCE = thm "iffCE"
paulson@15411
   774
val exCI = thm "exCI"
wenzelm@4868
   775
paulson@15411
   776
(* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
paulson@15411
   777
local
paulson@15411
   778
  fun wrong_prem (Const ("All", _) $ (Abs (_, _, t))) = wrong_prem t
paulson@15411
   779
  |   wrong_prem (Bound _) = true
paulson@15411
   780
  |   wrong_prem _ = false
skalberg@15570
   781
  val filter_right = List.filter (fn t => not (wrong_prem (HOLogic.dest_Trueprop (hd (Thm.prems_of t)))))
paulson@15411
   782
in
paulson@15411
   783
  fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp])
paulson@15411
   784
  fun smp_tac j = EVERY'[dresolve_tac (smp j), atac]
paulson@15411
   785
end
paulson@15411
   786
paulson@15411
   787
paulson@15411
   788
fun strip_tac i = REPEAT(resolve_tac [impI,allI] i)
paulson@15411
   789
paulson@15411
   790
(*Obsolete form of disjunctive case analysis*)
paulson@15411
   791
fun excluded_middle_tac sP =
paulson@15411
   792
    res_inst_tac [("Q",sP)] (excluded_middle RS disjE)
paulson@15411
   793
paulson@15411
   794
fun case_tac a = res_inst_tac [("P",a)] case_split_thm
paulson@15411
   795
*}
paulson@15411
   796
wenzelm@11687
   797
theorems case_split = case_split_thm [case_names True False]
wenzelm@9869
   798
wenzelm@18457
   799
ML {*
wenzelm@18457
   800
structure ProjectRule = ProjectRuleFun
wenzelm@18457
   801
(struct
wenzelm@18457
   802
  val conjunct1 = thm "conjunct1";
wenzelm@18457
   803
  val conjunct2 = thm "conjunct2";
wenzelm@18457
   804
  val mp = thm "mp";
wenzelm@18457
   805
end)
wenzelm@18457
   806
*}
wenzelm@18457
   807
wenzelm@12386
   808
wenzelm@12386
   809
subsubsection {* Intuitionistic Reasoning *}
wenzelm@12386
   810
wenzelm@12386
   811
lemma impE':
wenzelm@12937
   812
  assumes 1: "P --> Q"
wenzelm@12937
   813
    and 2: "Q ==> R"
wenzelm@12937
   814
    and 3: "P --> Q ==> P"
wenzelm@12937
   815
  shows R
wenzelm@12386
   816
proof -
wenzelm@12386
   817
  from 3 and 1 have P .
wenzelm@12386
   818
  with 1 have Q by (rule impE)
wenzelm@12386
   819
  with 2 show R .
wenzelm@12386
   820
qed
wenzelm@12386
   821
wenzelm@12386
   822
lemma allE':
wenzelm@12937
   823
  assumes 1: "ALL x. P x"
wenzelm@12937
   824
    and 2: "P x ==> ALL x. P x ==> Q"
wenzelm@12937
   825
  shows Q
wenzelm@12386
   826
proof -
wenzelm@12386
   827
  from 1 have "P x" by (rule spec)
wenzelm@12386
   828
  from this and 1 show Q by (rule 2)
wenzelm@12386
   829
qed
wenzelm@12386
   830
wenzelm@12937
   831
lemma notE':
wenzelm@12937
   832
  assumes 1: "~ P"
wenzelm@12937
   833
    and 2: "~ P ==> P"
wenzelm@12937
   834
  shows R
wenzelm@12386
   835
proof -
wenzelm@12386
   836
  from 2 and 1 have P .
wenzelm@12386
   837
  with 1 show R by (rule notE)
wenzelm@12386
   838
qed
wenzelm@12386
   839
wenzelm@15801
   840
lemmas [Pure.elim!] = disjE iffE FalseE conjE exE
wenzelm@15801
   841
  and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
wenzelm@15801
   842
  and [Pure.elim 2] = allE notE' impE'
wenzelm@15801
   843
  and [Pure.intro] = exI disjI2 disjI1
wenzelm@12386
   844
wenzelm@12386
   845
lemmas [trans] = trans
wenzelm@12386
   846
  and [sym] = sym not_sym
wenzelm@15801
   847
  and [Pure.elim?] = iffD1 iffD2 impE
wenzelm@11750
   848
wenzelm@11438
   849
wenzelm@11750
   850
subsubsection {* Atomizing meta-level connectives *}
wenzelm@11750
   851
wenzelm@11750
   852
lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
wenzelm@12003
   853
proof
wenzelm@9488
   854
  assume "!!x. P x"
wenzelm@10383
   855
  show "ALL x. P x" by (rule allI)
wenzelm@9488
   856
next
wenzelm@9488
   857
  assume "ALL x. P x"
wenzelm@10383
   858
  thus "!!x. P x" by (rule allE)
wenzelm@9488
   859
qed
wenzelm@9488
   860
wenzelm@11750
   861
lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
wenzelm@12003
   862
proof
wenzelm@9488
   863
  assume r: "A ==> B"
wenzelm@10383
   864
  show "A --> B" by (rule impI) (rule r)
wenzelm@9488
   865
next
wenzelm@9488
   866
  assume "A --> B" and A
wenzelm@10383
   867
  thus B by (rule mp)
wenzelm@9488
   868
qed
wenzelm@9488
   869
paulson@14749
   870
lemma atomize_not: "(A ==> False) == Trueprop (~A)"
paulson@14749
   871
proof
paulson@14749
   872
  assume r: "A ==> False"
paulson@14749
   873
  show "~A" by (rule notI) (rule r)
paulson@14749
   874
next
paulson@14749
   875
  assume "~A" and A
paulson@14749
   876
  thus False by (rule notE)
paulson@14749
   877
qed
paulson@14749
   878
wenzelm@11750
   879
lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
wenzelm@12003
   880
proof
wenzelm@10432
   881
  assume "x == y"
wenzelm@10432
   882
  show "x = y" by (unfold prems) (rule refl)
wenzelm@10432
   883
next
wenzelm@10432
   884
  assume "x = y"
wenzelm@10432
   885
  thus "x == y" by (rule eq_reflection)
wenzelm@10432
   886
qed
wenzelm@10432
   887
wenzelm@12023
   888
lemma atomize_conj [atomize]:
wenzelm@19121
   889
  includes meta_conjunction_syntax
wenzelm@19121
   890
  shows "(A && B) == Trueprop (A & B)"
wenzelm@12003
   891
proof
wenzelm@19121
   892
  assume conj: "A && B"
wenzelm@19121
   893
  show "A & B"
wenzelm@19121
   894
  proof (rule conjI)
wenzelm@19121
   895
    from conj show A by (rule conjunctionD1)
wenzelm@19121
   896
    from conj show B by (rule conjunctionD2)
wenzelm@19121
   897
  qed
wenzelm@11953
   898
next
wenzelm@19121
   899
  assume conj: "A & B"
wenzelm@19121
   900
  show "A && B"
wenzelm@19121
   901
  proof -
wenzelm@19121
   902
    from conj show A ..
wenzelm@19121
   903
    from conj show B ..
wenzelm@11953
   904
  qed
wenzelm@11953
   905
qed
wenzelm@11953
   906
wenzelm@12386
   907
lemmas [symmetric, rulify] = atomize_all atomize_imp
wenzelm@18832
   908
  and [symmetric, defn] = atomize_all atomize_imp atomize_eq
wenzelm@12386
   909
wenzelm@11750
   910
wenzelm@11750
   911
subsubsection {* Classical Reasoner setup *}
wenzelm@9529
   912
wenzelm@10383
   913
use "cladata.ML"
wenzelm@10383
   914
setup hypsubst_setup
wenzelm@11977
   915
wenzelm@18708
   916
setup {* ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac) *}
wenzelm@11977
   917
wenzelm@10383
   918
setup Classical.setup
mengj@19162
   919
mengj@19162
   920
setup ResAtpSet.setup
mengj@19162
   921
wenzelm@10383
   922
setup clasetup
wenzelm@10383
   923
wenzelm@18689
   924
declare ex_ex1I [rule del, intro! 2]
wenzelm@18689
   925
  and ex1I [intro]
wenzelm@18689
   926
wenzelm@12386
   927
lemmas [intro?] = ext
wenzelm@12386
   928
  and [elim?] = ex1_implies_ex
wenzelm@11977
   929
wenzelm@9869
   930
use "blastdata.ML"
wenzelm@9869
   931
setup Blast.setup
wenzelm@4868
   932
wenzelm@11750
   933
wenzelm@17459
   934
subsubsection {* Simplifier setup *}
wenzelm@11750
   935
wenzelm@12281
   936
lemma meta_eq_to_obj_eq: "x == y ==> x = y"
wenzelm@12281
   937
proof -
wenzelm@12281
   938
  assume r: "x == y"
wenzelm@12281
   939
  show "x = y" by (unfold r) (rule refl)
wenzelm@12281
   940
qed
wenzelm@12281
   941
wenzelm@12281
   942
lemma eta_contract_eq: "(%s. f s) = f" ..
wenzelm@12281
   943
wenzelm@12281
   944
lemma simp_thms:
wenzelm@12937
   945
  shows not_not: "(~ ~ P) = P"
nipkow@15354
   946
  and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
wenzelm@12937
   947
  and
berghofe@12436
   948
    "(P ~= Q) = (P = (~Q))"
berghofe@12436
   949
    "(P | ~P) = True"    "(~P | P) = True"
wenzelm@12281
   950
    "(x = x) = True"
wenzelm@12281
   951
    "(~True) = False"  "(~False) = True"
berghofe@12436
   952
    "(~P) ~= P"  "P ~= (~P)"
wenzelm@12281
   953
    "(True=P) = P"  "(P=True) = P"  "(False=P) = (~P)"  "(P=False) = (~P)"
wenzelm@12281
   954
    "(True --> P) = P"  "(False --> P) = True"
wenzelm@12281
   955
    "(P --> True) = True"  "(P --> P) = True"
wenzelm@12281
   956
    "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
wenzelm@12281
   957
    "(P & True) = P"  "(True & P) = P"
wenzelm@12281
   958
    "(P & False) = False"  "(False & P) = False"
wenzelm@12281
   959
    "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
wenzelm@12281
   960
    "(P & ~P) = False"    "(~P & P) = False"
wenzelm@12281
   961
    "(P | True) = True"  "(True | P) = True"
wenzelm@12281
   962
    "(P | False) = P"  "(False | P) = P"
berghofe@12436
   963
    "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
wenzelm@12281
   964
    "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
wenzelm@12281
   965
    -- {* needed for the one-point-rule quantifier simplification procs *}
wenzelm@12281
   966
    -- {* essential for termination!! *} and
wenzelm@12281
   967
    "!!P. (EX x. x=t & P(x)) = P(t)"
wenzelm@12281
   968
    "!!P. (EX x. t=x & P(x)) = P(t)"
wenzelm@12281
   969
    "!!P. (ALL x. x=t --> P(x)) = P(t)"
wenzelm@12937
   970
    "!!P. (ALL x. t=x --> P(x)) = P(t)"
nipkow@17589
   971
  by (blast, blast, blast, blast, blast, iprover+)
wenzelm@13421
   972
wenzelm@12281
   973
lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
nipkow@17589
   974
  by iprover
wenzelm@12281
   975
wenzelm@12281
   976
lemma ex_simps:
wenzelm@12281
   977
  "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
wenzelm@12281
   978
  "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
wenzelm@12281
   979
  "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
wenzelm@12281
   980
  "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
wenzelm@12281
   981
  "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
wenzelm@12281
   982
  "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
wenzelm@12281
   983
  -- {* Miniscoping: pushing in existential quantifiers. *}
nipkow@17589
   984
  by (iprover | blast)+
wenzelm@12281
   985
wenzelm@12281
   986
lemma all_simps:
wenzelm@12281
   987
  "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
wenzelm@12281
   988
  "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
wenzelm@12281
   989
  "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
wenzelm@12281
   990
  "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
wenzelm@12281
   991
  "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
wenzelm@12281
   992
  "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
wenzelm@12281
   993
  -- {* Miniscoping: pushing in universal quantifiers. *}
nipkow@17589
   994
  by (iprover | blast)+
wenzelm@12281
   995
paulson@14201
   996
lemma disj_absorb: "(A | A) = A"
paulson@14201
   997
  by blast
paulson@14201
   998
paulson@14201
   999
lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
paulson@14201
  1000
  by blast
paulson@14201
  1001
paulson@14201
  1002
lemma conj_absorb: "(A & A) = A"
paulson@14201
  1003
  by blast
paulson@14201
  1004
paulson@14201
  1005
lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
paulson@14201
  1006
  by blast
paulson@14201
  1007
wenzelm@12281
  1008
lemma eq_ac:
wenzelm@12937
  1009
  shows eq_commute: "(a=b) = (b=a)"
wenzelm@12937
  1010
    and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
nipkow@17589
  1011
    and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
nipkow@17589
  1012
lemma neq_commute: "(a~=b) = (b~=a)" by iprover
wenzelm@12281
  1013
wenzelm@12281
  1014
lemma conj_comms:
wenzelm@12937
  1015
  shows conj_commute: "(P&Q) = (Q&P)"
nipkow@17589
  1016
    and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
nipkow@17589
  1017
lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
wenzelm@12281
  1018
paulson@19174
  1019
lemmas conj_ac = conj_commute conj_left_commute conj_assoc
paulson@19174
  1020
wenzelm@12281
  1021
lemma disj_comms:
wenzelm@12937
  1022
  shows disj_commute: "(P|Q) = (Q|P)"
nipkow@17589
  1023
    and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
nipkow@17589
  1024
lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
wenzelm@12281
  1025
paulson@19174
  1026
lemmas disj_ac = disj_commute disj_left_commute disj_assoc
paulson@19174
  1027
nipkow@17589
  1028
lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
nipkow@17589
  1029
lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
wenzelm@12281
  1030
nipkow@17589
  1031
lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
nipkow@17589
  1032
lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
wenzelm@12281
  1033
nipkow@17589
  1034
lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
nipkow@17589
  1035
lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
nipkow@17589
  1036
lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
wenzelm@12281
  1037
wenzelm@12281
  1038
text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
wenzelm@12281
  1039
lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
wenzelm@12281
  1040
lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
wenzelm@12281
  1041
wenzelm@12281
  1042
lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
wenzelm@12281
  1043
lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
wenzelm@12281
  1044
nipkow@17589
  1045
lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
wenzelm@12281
  1046
lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
wenzelm@12281
  1047
lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
wenzelm@12281
  1048
lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
wenzelm@12281
  1049
lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
wenzelm@12281
  1050
lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
wenzelm@12281
  1051
  by blast
wenzelm@12281
  1052
lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
wenzelm@12281
  1053
nipkow@17589
  1054
lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
wenzelm@12281
  1055
wenzelm@12281
  1056
wenzelm@12281
  1057
lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
wenzelm@12281
  1058
  -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
wenzelm@12281
  1059
  -- {* cases boil down to the same thing. *}
wenzelm@12281
  1060
  by blast
wenzelm@12281
  1061
wenzelm@12281
  1062
lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
wenzelm@12281
  1063
lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
nipkow@17589
  1064
lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
nipkow@17589
  1065
lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
wenzelm@12281
  1066
nipkow@17589
  1067
lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
nipkow@17589
  1068
lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
wenzelm@12281
  1069
wenzelm@12281
  1070
text {*
wenzelm@12281
  1071
  \medskip The @{text "&"} congruence rule: not included by default!
wenzelm@12281
  1072
  May slow rewrite proofs down by as much as 50\% *}
wenzelm@12281
  1073
wenzelm@12281
  1074
lemma conj_cong:
wenzelm@12281
  1075
    "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1076
  by iprover
wenzelm@12281
  1077
wenzelm@12281
  1078
lemma rev_conj_cong:
wenzelm@12281
  1079
    "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1080
  by iprover
wenzelm@12281
  1081
wenzelm@12281
  1082
text {* The @{text "|"} congruence rule: not included by default! *}
wenzelm@12281
  1083
wenzelm@12281
  1084
lemma disj_cong:
wenzelm@12281
  1085
    "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
wenzelm@12281
  1086
  by blast
wenzelm@12281
  1087
wenzelm@12281
  1088
lemma eq_sym_conv: "(x = y) = (y = x)"
nipkow@17589
  1089
  by iprover
wenzelm@12281
  1090
wenzelm@12281
  1091
wenzelm@12281
  1092
text {* \medskip if-then-else rules *}
wenzelm@12281
  1093
wenzelm@12281
  1094
lemma if_True: "(if True then x else y) = x"
wenzelm@12281
  1095
  by (unfold if_def) blast
wenzelm@12281
  1096
wenzelm@12281
  1097
lemma if_False: "(if False then x else y) = y"
wenzelm@12281
  1098
  by (unfold if_def) blast
wenzelm@12281
  1099
wenzelm@12281
  1100
lemma if_P: "P ==> (if P then x else y) = x"
wenzelm@12281
  1101
  by (unfold if_def) blast
wenzelm@12281
  1102
wenzelm@12281
  1103
lemma if_not_P: "~P ==> (if P then x else y) = y"
wenzelm@12281
  1104
  by (unfold if_def) blast
wenzelm@12281
  1105
wenzelm@12281
  1106
lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
wenzelm@12281
  1107
  apply (rule case_split [of Q])
paulson@15481
  1108
   apply (simplesubst if_P)
paulson@15481
  1109
    prefer 3 apply (simplesubst if_not_P, blast+)
wenzelm@12281
  1110
  done
wenzelm@12281
  1111
wenzelm@12281
  1112
lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
paulson@15481
  1113
by (simplesubst split_if, blast)
wenzelm@12281
  1114
wenzelm@12281
  1115
lemmas if_splits = split_if split_if_asm
wenzelm@12281
  1116
wenzelm@12281
  1117
lemma if_def2: "(if Q then x else y) = ((Q --> x) & (~ Q --> y))"
wenzelm@12281
  1118
  by (rule split_if)
wenzelm@12281
  1119
wenzelm@12281
  1120
lemma if_cancel: "(if c then x else x) = x"
paulson@15481
  1121
by (simplesubst split_if, blast)
wenzelm@12281
  1122
wenzelm@12281
  1123
lemma if_eq_cancel: "(if x = y then y else x) = x"
paulson@15481
  1124
by (simplesubst split_if, blast)
wenzelm@12281
  1125
wenzelm@12281
  1126
lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
wenzelm@19796
  1127
  -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
wenzelm@12281
  1128
  by (rule split_if)
wenzelm@12281
  1129
wenzelm@12281
  1130
lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
wenzelm@19796
  1131
  -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
paulson@15481
  1132
  apply (simplesubst split_if, blast)
wenzelm@12281
  1133
  done
wenzelm@12281
  1134
nipkow@17589
  1135
lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
nipkow@17589
  1136
lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
wenzelm@12281
  1137
schirmer@15423
  1138
text {* \medskip let rules for simproc *}
schirmer@15423
  1139
schirmer@15423
  1140
lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
schirmer@15423
  1141
  by (unfold Let_def)
schirmer@15423
  1142
schirmer@15423
  1143
lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
schirmer@15423
  1144
  by (unfold Let_def)
schirmer@15423
  1145
berghofe@16633
  1146
text {*
ballarin@16999
  1147
  The following copy of the implication operator is useful for
ballarin@16999
  1148
  fine-tuning congruence rules.  It instructs the simplifier to simplify
ballarin@16999
  1149
  its premise.
berghofe@16633
  1150
*}
berghofe@16633
  1151
wenzelm@17197
  1152
constdefs
wenzelm@17197
  1153
  simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1)
wenzelm@17197
  1154
  "simp_implies \<equiv> op ==>"
berghofe@16633
  1155
wenzelm@18457
  1156
lemma simp_impliesI:
berghofe@16633
  1157
  assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
berghofe@16633
  1158
  shows "PROP P =simp=> PROP Q"
berghofe@16633
  1159
  apply (unfold simp_implies_def)
berghofe@16633
  1160
  apply (rule PQ)
berghofe@16633
  1161
  apply assumption
berghofe@16633
  1162
  done
berghofe@16633
  1163
berghofe@16633
  1164
lemma simp_impliesE:
berghofe@16633
  1165
  assumes PQ:"PROP P =simp=> PROP Q"
berghofe@16633
  1166
  and P: "PROP P"
berghofe@16633
  1167
  and QR: "PROP Q \<Longrightarrow> PROP R"
berghofe@16633
  1168
  shows "PROP R"
berghofe@16633
  1169
  apply (rule QR)
berghofe@16633
  1170
  apply (rule PQ [unfolded simp_implies_def])
berghofe@16633
  1171
  apply (rule P)
berghofe@16633
  1172
  done
berghofe@16633
  1173
berghofe@16633
  1174
lemma simp_implies_cong:
berghofe@16633
  1175
  assumes PP' :"PROP P == PROP P'"
berghofe@16633
  1176
  and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
berghofe@16633
  1177
  shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
berghofe@16633
  1178
proof (unfold simp_implies_def, rule equal_intr_rule)
berghofe@16633
  1179
  assume PQ: "PROP P \<Longrightarrow> PROP Q"
berghofe@16633
  1180
  and P': "PROP P'"
berghofe@16633
  1181
  from PP' [symmetric] and P' have "PROP P"
berghofe@16633
  1182
    by (rule equal_elim_rule1)
berghofe@16633
  1183
  hence "PROP Q" by (rule PQ)
berghofe@16633
  1184
  with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
berghofe@16633
  1185
next
berghofe@16633
  1186
  assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
berghofe@16633
  1187
  and P: "PROP P"
berghofe@16633
  1188
  from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
berghofe@16633
  1189
  hence "PROP Q'" by (rule P'Q')
berghofe@16633
  1190
  with P'QQ' [OF P', symmetric] show "PROP Q"
berghofe@16633
  1191
    by (rule equal_elim_rule1)
berghofe@16633
  1192
qed
berghofe@16633
  1193
wenzelm@17459
  1194
wenzelm@17459
  1195
text {* \medskip Actual Installation of the Simplifier. *}
paulson@14201
  1196
wenzelm@9869
  1197
use "simpdata.ML"
wenzelm@9869
  1198
setup "Simplifier.method_setup Splitter.split_modifiers" setup simpsetup
wenzelm@9869
  1199
setup Splitter.setup setup Clasimp.setup
wenzelm@18591
  1200
setup EqSubst.setup
paulson@15481
  1201
wenzelm@17459
  1202
wenzelm@17459
  1203
subsubsection {* Code generator setup *}
wenzelm@17459
  1204
wenzelm@17459
  1205
types_code
wenzelm@17459
  1206
  "bool"  ("bool")
wenzelm@17459
  1207
attach (term_of) {*
wenzelm@17459
  1208
fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
wenzelm@17459
  1209
*}
wenzelm@17459
  1210
attach (test) {*
wenzelm@17459
  1211
fun gen_bool i = one_of [false, true];
wenzelm@17459
  1212
*}
berghofe@18887
  1213
  "prop"  ("bool")
berghofe@18887
  1214
attach (term_of) {*
berghofe@18887
  1215
fun term_of_prop b =
berghofe@18887
  1216
  HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
berghofe@18887
  1217
*}
wenzelm@17459
  1218
wenzelm@17459
  1219
consts_code
berghofe@18887
  1220
  "Trueprop" ("(_)")
wenzelm@17459
  1221
  "True"    ("true")
wenzelm@17459
  1222
  "False"   ("false")
wenzelm@17459
  1223
  "Not"     ("not")
wenzelm@17459
  1224
  "op |"    ("(_ orelse/ _)")
wenzelm@17459
  1225
  "op &"    ("(_ andalso/ _)")
wenzelm@17459
  1226
  "HOL.If"      ("(if _/ then _/ else _)")
wenzelm@17459
  1227
wenzelm@17459
  1228
ML {*
wenzelm@17459
  1229
local
wenzelm@17459
  1230
wenzelm@17459
  1231
fun eq_codegen thy defs gr dep thyname b t =
wenzelm@17459
  1232
    (case strip_comb t of
wenzelm@17459
  1233
       (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
wenzelm@17459
  1234
     | (Const ("op =", _), [t, u]) =>
wenzelm@17459
  1235
          let
wenzelm@17459
  1236
            val (gr', pt) = Codegen.invoke_codegen thy defs dep thyname false (gr, t);
berghofe@17639
  1237
            val (gr'', pu) = Codegen.invoke_codegen thy defs dep thyname false (gr', u);
berghofe@17639
  1238
            val (gr''', _) = Codegen.invoke_tycodegen thy defs dep thyname false (gr'', HOLogic.boolT)
wenzelm@17459
  1239
          in
berghofe@17639
  1240
            SOME (gr''', Codegen.parens
wenzelm@17459
  1241
              (Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu]))
wenzelm@17459
  1242
          end
wenzelm@17459
  1243
     | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
wenzelm@17459
  1244
         thy defs dep thyname b (gr, Codegen.eta_expand t ts 2))
wenzelm@17459
  1245
     | _ => NONE);
wenzelm@17459
  1246
berghofe@18887
  1247
exception Evaluation of term;
berghofe@18887
  1248
berghofe@18887
  1249
fun evaluation_oracle (thy, Evaluation t) =
berghofe@18887
  1250
  Logic.mk_equals (t, Codegen.eval_term thy t);
berghofe@18887
  1251
berghofe@18887
  1252
fun evaluation_conv ct =
berghofe@18887
  1253
  let val {sign, t, ...} = rep_cterm ct
berghofe@18887
  1254
  in Thm.invoke_oracle_i sign "HOL.Evaluation" (sign, Evaluation t) end;
berghofe@18887
  1255
berghofe@18887
  1256
fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
berghofe@18887
  1257
  (Drule.goals_conv (equal i) evaluation_conv));
berghofe@18887
  1258
berghofe@18887
  1259
val evaluation_meth =
berghofe@18887
  1260
  Method.no_args (Method.METHOD (fn _ => evaluation_tac 1 THEN rtac TrueI 1));
berghofe@18887
  1261
wenzelm@17459
  1262
in
wenzelm@17459
  1263
wenzelm@18708
  1264
val eq_codegen_setup = Codegen.add_codegen "eq_codegen" eq_codegen;
wenzelm@17459
  1265
berghofe@18887
  1266
val evaluation_oracle_setup =
berghofe@18887
  1267
  Theory.add_oracle ("Evaluation", evaluation_oracle) #>
berghofe@18887
  1268
  Method.add_method ("evaluation", evaluation_meth, "solve goal by evaluation");
berghofe@18887
  1269
wenzelm@17459
  1270
end;
wenzelm@17459
  1271
*}
wenzelm@17459
  1272
wenzelm@17459
  1273
setup eq_codegen_setup
berghofe@18887
  1274
setup evaluation_oracle_setup
paulson@15481
  1275
paulson@15481
  1276
nipkow@19961
  1277
ML {*
nipkow@19961
  1278
local
nipkow@19961
  1279
nipkow@19961
  1280
exception Normalization of term;
nipkow@19961
  1281
nipkow@19961
  1282
fun normalization_oracle (thy, Normalization t) = Logic.mk_equals
nipkow@19961
  1283
  (t, HOLogic.mk_Trueprop (NBE.norm_term thy (HOLogic.dest_Trueprop t)));
nipkow@19961
  1284
nipkow@19961
  1285
fun normalization_conv ct =
nipkow@19961
  1286
  let val {sign, t, ...} = rep_cterm ct
nipkow@19961
  1287
  in Thm.invoke_oracle_i sign "HOL.Normalization" (sign, Normalization t) end;
nipkow@19961
  1288
nipkow@19961
  1289
fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
nipkow@19961
  1290
  (Drule.goals_conv (equal i) normalization_conv));
nipkow@19961
  1291
nipkow@19961
  1292
val normalization_meth =
nipkow@19961
  1293
  Method.no_args (Method.METHOD (fn _ => normalization_tac 1 THEN rtac TrueI 1));
nipkow@19961
  1294
nipkow@19961
  1295
in
nipkow@19961
  1296
nipkow@19961
  1297
val normalization_oracle_setup =
nipkow@19961
  1298
  Theory.add_oracle ("Normalization", normalization_oracle) #>
nipkow@19961
  1299
  Method.add_method ("normalization", normalization_meth, "solve goal by normalization");
nipkow@19961
  1300
nipkow@19961
  1301
end;
nipkow@19961
  1302
*}
nipkow@19961
  1303
nipkow@19961
  1304
setup normalization_oracle_setup
nipkow@19961
  1305
nipkow@19961
  1306
paulson@15481
  1307
subsection {* Other simple lemmas *}
paulson@15481
  1308
paulson@15411
  1309
declare disj_absorb [simp] conj_absorb [simp]
paulson@14201
  1310
nipkow@13723
  1311
lemma ex1_eq[iff]: "EX! x. x = t" "EX! x. t = x"
nipkow@13723
  1312
by blast+
nipkow@13723
  1313
paulson@15481
  1314
berghofe@13638
  1315
theorem choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
berghofe@13638
  1316
  apply (rule iffI)
berghofe@13638
  1317
  apply (rule_tac a = "%x. THE y. P x y" in ex1I)
berghofe@13638
  1318
  apply (fast dest!: theI')
berghofe@13638
  1319
  apply (fast intro: ext the1_equality [symmetric])
berghofe@13638
  1320
  apply (erule ex1E)
berghofe@13638
  1321
  apply (rule allI)
berghofe@13638
  1322
  apply (rule ex1I)
berghofe@13638
  1323
  apply (erule spec)
berghofe@13638
  1324
  apply (erule_tac x = "%z. if z = x then y else f z" in allE)
berghofe@13638
  1325
  apply (erule impE)
berghofe@13638
  1326
  apply (rule allI)
berghofe@13638
  1327
  apply (rule_tac P = "xa = x" in case_split_thm)
paulson@14208
  1328
  apply (drule_tac [3] x = x in fun_cong, simp_all)
berghofe@13638
  1329
  done
berghofe@13638
  1330
nipkow@13438
  1331
text{*Needs only HOL-lemmas:*}
nipkow@13438
  1332
lemma mk_left_commute:
nipkow@13438
  1333
  assumes a: "\<And>x y z. f (f x y) z = f x (f y z)" and
nipkow@13438
  1334
          c: "\<And>x y. f x y = f y x"
nipkow@13438
  1335
  shows "f x (f y z) = f y (f x z)"
nipkow@13438
  1336
by(rule trans[OF trans[OF c a] arg_cong[OF c, of "f y"]])
nipkow@13438
  1337
wenzelm@11750
  1338
paulson@15481
  1339
subsection {* Generic cases and induction *}
wenzelm@11824
  1340
wenzelm@11824
  1341
constdefs
wenzelm@18457
  1342
  induct_forall where "induct_forall P == \<forall>x. P x"
wenzelm@18457
  1343
  induct_implies where "induct_implies A B == A \<longrightarrow> B"
wenzelm@18457
  1344
  induct_equal where "induct_equal x y == x = y"
wenzelm@18457
  1345
  induct_conj where "induct_conj A B == A \<and> B"
wenzelm@11824
  1346
wenzelm@11989
  1347
lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
wenzelm@18457
  1348
  by (unfold atomize_all induct_forall_def)
wenzelm@11824
  1349
wenzelm@11989
  1350
lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
wenzelm@18457
  1351
  by (unfold atomize_imp induct_implies_def)
wenzelm@11824
  1352
wenzelm@11989
  1353
lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
wenzelm@18457
  1354
  by (unfold atomize_eq induct_equal_def)
wenzelm@18457
  1355
wenzelm@18457
  1356
lemma induct_conj_eq:
wenzelm@18457
  1357
  includes meta_conjunction_syntax
wenzelm@18457
  1358
  shows "(A && B) == Trueprop (induct_conj A B)"
wenzelm@18457
  1359
  by (unfold atomize_conj induct_conj_def)
wenzelm@18457
  1360
wenzelm@18457
  1361
lemmas induct_atomize = induct_forall_eq induct_implies_eq induct_equal_eq induct_conj_eq
wenzelm@18457
  1362
lemmas induct_rulify [symmetric, standard] = induct_atomize
wenzelm@18457
  1363
lemmas induct_rulify_fallback =
wenzelm@18457
  1364
  induct_forall_def induct_implies_def induct_equal_def induct_conj_def
wenzelm@18457
  1365
wenzelm@11824
  1366
wenzelm@11989
  1367
lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
wenzelm@11989
  1368
    induct_conj (induct_forall A) (induct_forall B)"
nipkow@17589
  1369
  by (unfold induct_forall_def induct_conj_def) iprover
wenzelm@11824
  1370
wenzelm@11989
  1371
lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
wenzelm@11989
  1372
    induct_conj (induct_implies C A) (induct_implies C B)"
nipkow@17589
  1373
  by (unfold induct_implies_def induct_conj_def) iprover
wenzelm@11989
  1374
berghofe@13598
  1375
lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
berghofe@13598
  1376
proof
berghofe@13598
  1377
  assume r: "induct_conj A B ==> PROP C" and A B
wenzelm@18457
  1378
  show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
berghofe@13598
  1379
next
berghofe@13598
  1380
  assume r: "A ==> B ==> PROP C" and "induct_conj A B"
wenzelm@18457
  1381
  show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
berghofe@13598
  1382
qed
wenzelm@11824
  1383
wenzelm@11989
  1384
lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
wenzelm@11824
  1385
wenzelm@11989
  1386
hide const induct_forall induct_implies induct_equal induct_conj
wenzelm@11824
  1387
wenzelm@11824
  1388
wenzelm@11824
  1389
text {* Method setup. *}
wenzelm@11824
  1390
wenzelm@11824
  1391
ML {*
wenzelm@11824
  1392
  structure InductMethod = InductMethodFun
wenzelm@11824
  1393
  (struct
paulson@15411
  1394
    val cases_default = thm "case_split"
paulson@15411
  1395
    val atomize = thms "induct_atomize"
wenzelm@18457
  1396
    val rulify = thms "induct_rulify"
wenzelm@18457
  1397
    val rulify_fallback = thms "induct_rulify_fallback"
wenzelm@11824
  1398
  end);
wenzelm@11824
  1399
*}
wenzelm@11824
  1400
wenzelm@11824
  1401
setup InductMethod.setup
wenzelm@11824
  1402
wenzelm@18457
  1403
wenzelm@18457
  1404
subsubsection {*Tags, for the ATP Linkup *}
paulson@17404
  1405
paulson@17404
  1406
constdefs
paulson@17404
  1407
  tag :: "bool => bool"
wenzelm@18457
  1408
  "tag P == P"
paulson@17404
  1409
paulson@17404
  1410
text{*These label the distinguished literals of introduction and elimination
paulson@17404
  1411
rules.*}
paulson@17404
  1412
paulson@17404
  1413
lemma tagI: "P ==> tag P"
paulson@17404
  1414
by (simp add: tag_def)
paulson@17404
  1415
paulson@17404
  1416
lemma tagD: "tag P ==> P"
paulson@17404
  1417
by (simp add: tag_def)
paulson@17404
  1418
paulson@17404
  1419
text{*Applications of "tag" to True and False must go!*}
paulson@17404
  1420
paulson@17404
  1421
lemma tag_True: "tag True = True"
paulson@17404
  1422
by (simp add: tag_def)
paulson@17404
  1423
paulson@17404
  1424
lemma tag_False: "tag False = False"
paulson@17404
  1425
by (simp add: tag_def)
wenzelm@11824
  1426
haftmann@18702
  1427
haftmann@18702
  1428
subsection {* Code generator setup *}
haftmann@18702
  1429
haftmann@19598
  1430
setup {*
haftmann@19598
  1431
  CodegenTheorems.init_obj ((TrueI, FalseE), (conjI, thm "HOL.atomize_eq" |> Thm.symmetric))
haftmann@19347
  1432
*}
haftmann@19347
  1433
haftmann@18702
  1434
code_alias
haftmann@18702
  1435
  bool "HOL.bool"
haftmann@18702
  1436
  True "HOL.True"
haftmann@18702
  1437
  False "HOL.False"
haftmann@18702
  1438
  "op =" "HOL.op_eq"
haftmann@18702
  1439
  "op -->" "HOL.op_implies"
haftmann@18702
  1440
  "op &" "HOL.op_and"
haftmann@18702
  1441
  "op |" "HOL.op_or"
haftmann@18702
  1442
  Not "HOL.not"
haftmann@18867
  1443
  arbitrary "HOL.arbitrary"
haftmann@18702
  1444
haftmann@19890
  1445
code_constapp
haftmann@19039
  1446
  "op =" (* an intermediate solution for polymorphic equality *)
haftmann@18702
  1447
    ml (infixl 6 "=")
haftmann@18702
  1448
    haskell (infixl 4 "==")
haftmann@18702
  1449
kleing@14357
  1450
end