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