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