src/HOL/HOL.thy
author haftmann
Thu, 06 Apr 2006 16:11:30 +0200
changeset 19347 e2e709f3f955
parent 19233 77ca20b0ed77
child 19536 1a3a3cf8b4fa
permissions -rw-r--r--
adapted for definitional code generation
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@12338
    21
wenzelm@3947
    22
global
wenzelm@3947
    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)
wenzelm@11750
    32
clasohm@923
    33
consts
wenzelm@7357
    34
  Not           :: "bool => bool"                   ("~ _" [40] 40)
wenzelm@7357
    35
  True          :: bool
wenzelm@7357
    36
  False         :: bool
wenzelm@3947
    37
  arbitrary     :: 'a
clasohm@923
    38
wenzelm@11432
    39
  The           :: "('a => bool) => 'a"
wenzelm@7357
    40
  All           :: "('a => bool) => bool"           (binder "ALL " 10)
wenzelm@7357
    41
  Ex            :: "('a => bool) => bool"           (binder "EX " 10)
wenzelm@7357
    42
  Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
wenzelm@7357
    43
  Let           :: "['a, 'a => 'b] => 'b"
clasohm@923
    44
wenzelm@7357
    45
  "="           :: "['a, 'a] => bool"               (infixl 50)
wenzelm@7357
    46
  &             :: "[bool, bool] => bool"           (infixr 35)
wenzelm@7357
    47
  "|"           :: "[bool, bool] => bool"           (infixr 30)
wenzelm@7357
    48
  -->           :: "[bool, bool] => bool"           (infixr 25)
clasohm@923
    49
wenzelm@10432
    50
local
wenzelm@10432
    51
paulson@16587
    52
consts
paulson@16587
    53
  If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" 10)
clasohm@923
    54
wenzelm@11750
    55
subsubsection {* Additional concrete syntax *}
wenzelm@2260
    56
wenzelm@4868
    57
nonterminals
clasohm@923
    58
  letbinds  letbind
clasohm@923
    59
  case_syn  cases_syn
clasohm@923
    60
clasohm@923
    61
syntax
wenzelm@12650
    62
  "_not_equal"  :: "['a, 'a] => bool"                    (infixl "~=" 50)
wenzelm@11432
    63
  "_The"        :: "[pttrn, bool] => 'a"                 ("(3THE _./ _)" [0, 10] 10)
clasohm@923
    64
wenzelm@7357
    65
  "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
wenzelm@7357
    66
  ""            :: "letbind => letbinds"                 ("_")
wenzelm@7357
    67
  "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
wenzelm@7357
    68
  "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" 10)
clasohm@923
    69
wenzelm@9060
    70
  "_case_syntax":: "['a, cases_syn] => 'b"               ("(case _ of/ _)" 10)
wenzelm@9060
    71
  "_case1"      :: "['a, 'b] => case_syn"                ("(2_ =>/ _)" 10)
wenzelm@7357
    72
  ""            :: "case_syn => cases_syn"               ("_")
wenzelm@9060
    73
  "_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
clasohm@923
    74
clasohm@923
    75
translations
wenzelm@7238
    76
  "x ~= y"                == "~ (x = y)"
nipkow@13764
    77
  "THE x. P"              == "The (%x. P)"
clasohm@923
    78
  "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
nipkow@1114
    79
  "let x = a in e"        == "Let a (%x. e)"
clasohm@923
    80
nipkow@13763
    81
print_translation {*
nipkow@13763
    82
(* To avoid eta-contraction of body: *)
nipkow@13763
    83
[("The", fn [Abs abs] =>
nipkow@13763
    84
     let val (x,t) = atomic_abs_tr' abs
nipkow@13763
    85
     in Syntax.const "_The" $ x $ t end)]
nipkow@13763
    86
*}
nipkow@13763
    87
wenzelm@12633
    88
syntax (output)
wenzelm@11687
    89
  "="           :: "['a, 'a] => bool"                    (infix 50)
wenzelm@12650
    90
  "_not_equal"  :: "['a, 'a] => bool"                    (infix "~=" 50)
wenzelm@2260
    91
wenzelm@12114
    92
syntax (xsymbols)
wenzelm@11687
    93
  Not           :: "bool => bool"                        ("\<not> _" [40] 40)
wenzelm@11687
    94
  "op &"        :: "[bool, bool] => bool"                (infixr "\<and>" 35)
wenzelm@11687
    95
  "op |"        :: "[bool, bool] => bool"                (infixr "\<or>" 30)
wenzelm@12114
    96
  "op -->"      :: "[bool, bool] => bool"                (infixr "\<longrightarrow>" 25)
wenzelm@12650
    97
  "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
wenzelm@11687
    98
  "ALL "        :: "[idts, bool] => bool"                ("(3\<forall>_./ _)" [0, 10] 10)
wenzelm@11687
    99
  "EX "         :: "[idts, bool] => bool"                ("(3\<exists>_./ _)" [0, 10] 10)
wenzelm@11687
   100
  "EX! "        :: "[idts, bool] => bool"                ("(3\<exists>!_./ _)" [0, 10] 10)
wenzelm@11687
   101
  "_case1"      :: "['a, 'b] => case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
schirmer@14361
   102
(*"_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ \<orelse> _")*)
wenzelm@2372
   103
wenzelm@12114
   104
syntax (xsymbols output)
wenzelm@12650
   105
  "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
wenzelm@3820
   106
wenzelm@6340
   107
syntax (HTML output)
kleing@14565
   108
  "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
wenzelm@11687
   109
  Not           :: "bool => bool"                        ("\<not> _" [40] 40)
kleing@14565
   110
  "op &"        :: "[bool, bool] => bool"                (infixr "\<and>" 35)
kleing@14565
   111
  "op |"        :: "[bool, bool] => bool"                (infixr "\<or>" 30)
kleing@14565
   112
  "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
kleing@14565
   113
  "ALL "        :: "[idts, bool] => bool"                ("(3\<forall>_./ _)" [0, 10] 10)
kleing@14565
   114
  "EX "         :: "[idts, bool] => bool"                ("(3\<exists>_./ _)" [0, 10] 10)
kleing@14565
   115
  "EX! "        :: "[idts, bool] => bool"                ("(3\<exists>!_./ _)" [0, 10] 10)
wenzelm@6340
   116
wenzelm@7238
   117
syntax (HOL)
wenzelm@7357
   118
  "ALL "        :: "[idts, bool] => bool"                ("(3! _./ _)" [0, 10] 10)
wenzelm@7357
   119
  "EX "         :: "[idts, bool] => bool"                ("(3? _./ _)" [0, 10] 10)
wenzelm@7357
   120
  "EX! "        :: "[idts, bool] => bool"                ("(3?! _./ _)" [0, 10] 10)
wenzelm@7238
   121
wenzelm@17992
   122
syntax
wenzelm@17992
   123
  "_iff" :: "bool => bool => bool"                       (infixr "<->" 25)
wenzelm@17992
   124
syntax (xsymbols)
wenzelm@17992
   125
  "_iff" :: "bool => bool => bool"                       (infixr "\<longleftrightarrow>" 25)
wenzelm@17992
   126
translations
wenzelm@17992
   127
  "op <->" => "op = :: bool => bool => bool"
wenzelm@17992
   128
wenzelm@17992
   129
typed_print_translation {*
wenzelm@17992
   130
  let
wenzelm@17992
   131
    fun iff_tr' _ (Type ("fun", (Type ("bool", _) :: _))) ts =
wenzelm@17992
   132
          if Output.has_mode "iff" then Term.list_comb (Syntax.const "_iff", ts)
wenzelm@17992
   133
          else raise Match
wenzelm@17992
   134
      | iff_tr' _ _ _ = raise Match;
wenzelm@17992
   135
  in [("op =", iff_tr')] end
wenzelm@17992
   136
*}
wenzelm@17992
   137
wenzelm@7238
   138
wenzelm@11750
   139
subsubsection {* Axioms and basic definitions *}
wenzelm@2260
   140
wenzelm@7357
   141
axioms
paulson@15380
   142
  eq_reflection:  "(x=y) ==> (x==y)"
clasohm@923
   143
paulson@15380
   144
  refl:           "t = (t::'a)"
paulson@6289
   145
paulson@15380
   146
  ext:            "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
paulson@15380
   147
    -- {*Extensionality is built into the meta-logic, and this rule expresses
paulson@15380
   148
         a related property.  It is an eta-expanded version of the traditional
paulson@15380
   149
         rule, and similar to the ABS rule of HOL*}
paulson@6289
   150
wenzelm@11432
   151
  the_eq_trivial: "(THE x. x = a) = (a::'a)"
clasohm@923
   152
paulson@15380
   153
  impI:           "(P ==> Q) ==> P-->Q"
paulson@15380
   154
  mp:             "[| P-->Q;  P |] ==> Q"
paulson@15380
   155
paulson@15380
   156
paulson@15380
   157
text{*Thanks to Stephan Merz*}
paulson@15380
   158
theorem subst:
paulson@15380
   159
  assumes eq: "s = t" and p: "P(s)"
paulson@15380
   160
  shows "P(t::'a)"
paulson@15380
   161
proof -
paulson@15380
   162
  from eq have meta: "s \<equiv> t"
paulson@15380
   163
    by (rule eq_reflection)
paulson@15380
   164
  from p show ?thesis
paulson@15380
   165
    by (unfold meta)
paulson@15380
   166
qed
clasohm@923
   167
clasohm@923
   168
defs
wenzelm@7357
   169
  True_def:     "True      == ((%x::bool. x) = (%x. x))"
wenzelm@7357
   170
  All_def:      "All(P)    == (P = (%x. True))"
paulson@11451
   171
  Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
wenzelm@7357
   172
  False_def:    "False     == (!P. P)"
wenzelm@7357
   173
  not_def:      "~ P       == P-->False"
wenzelm@7357
   174
  and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
wenzelm@7357
   175
  or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
wenzelm@7357
   176
  Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
clasohm@923
   177
wenzelm@7357
   178
axioms
wenzelm@7357
   179
  iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
wenzelm@7357
   180
  True_or_False:  "(P=True) | (P=False)"
clasohm@923
   181
clasohm@923
   182
defs
wenzelm@7357
   183
  Let_def:      "Let s f == f(s)"
paulson@11451
   184
  if_def:       "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
wenzelm@5069
   185
skalberg@14223
   186
finalconsts
skalberg@14223
   187
  "op ="
skalberg@14223
   188
  "op -->"
skalberg@14223
   189
  The
skalberg@14223
   190
  arbitrary
nipkow@3320
   191
wenzelm@11750
   192
subsubsection {* Generic algebraic operations *}
wenzelm@4793
   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@4793
   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@11438
   850
wenzelm@11750
   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@11977
   917
wenzelm@18708
   918
setup {* ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac) *}
wenzelm@11977
   919
wenzelm@10383
   920
setup Classical.setup
mengj@19162
   921
mengj@19162
   922
setup ResAtpSet.setup
mengj@19162
   923
wenzelm@10383
   924
setup clasetup
wenzelm@10383
   925
wenzelm@18689
   926
declare ex_ex1I [rule del, intro! 2]
wenzelm@18689
   927
  and ex1I [intro]
wenzelm@18689
   928
wenzelm@12386
   929
lemmas [intro?] = ext
wenzelm@12386
   930
  and [elim?] = ex1_implies_ex
wenzelm@11977
   931
wenzelm@9869
   932
use "blastdata.ML"
wenzelm@9869
   933
setup Blast.setup
wenzelm@4868
   934
wenzelm@11750
   935
wenzelm@17459
   936
subsubsection {* Simplifier setup *}
wenzelm@11750
   937
wenzelm@12281
   938
lemma meta_eq_to_obj_eq: "x == y ==> x = y"
wenzelm@12281
   939
proof -
wenzelm@12281
   940
  assume r: "x == y"
wenzelm@12281
   941
  show "x = y" by (unfold r) (rule refl)
wenzelm@12281
   942
qed
wenzelm@12281
   943
wenzelm@12281
   944
lemma eta_contract_eq: "(%s. f s) = f" ..
wenzelm@12281
   945
wenzelm@12281
   946
lemma simp_thms:
wenzelm@12937
   947
  shows not_not: "(~ ~ P) = P"
nipkow@15354
   948
  and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
wenzelm@12937
   949
  and
berghofe@12436
   950
    "(P ~= Q) = (P = (~Q))"
berghofe@12436
   951
    "(P | ~P) = True"    "(~P | P) = True"
wenzelm@12281
   952
    "(x = x) = True"
wenzelm@12281
   953
    "(~True) = False"  "(~False) = True"
berghofe@12436
   954
    "(~P) ~= P"  "P ~= (~P)"
wenzelm@12281
   955
    "(True=P) = P"  "(P=True) = P"  "(False=P) = (~P)"  "(P=False) = (~P)"
wenzelm@12281
   956
    "(True --> P) = P"  "(False --> P) = True"
wenzelm@12281
   957
    "(P --> True) = True"  "(P --> P) = True"
wenzelm@12281
   958
    "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
wenzelm@12281
   959
    "(P & True) = P"  "(True & P) = P"
wenzelm@12281
   960
    "(P & False) = False"  "(False & P) = False"
wenzelm@12281
   961
    "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
wenzelm@12281
   962
    "(P & ~P) = False"    "(~P & P) = False"
wenzelm@12281
   963
    "(P | True) = True"  "(True | P) = True"
wenzelm@12281
   964
    "(P | False) = P"  "(False | P) = P"
berghofe@12436
   965
    "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
wenzelm@12281
   966
    "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
wenzelm@12281
   967
    -- {* needed for the one-point-rule quantifier simplification procs *}
wenzelm@12281
   968
    -- {* essential for termination!! *} and
wenzelm@12281
   969
    "!!P. (EX x. x=t & P(x)) = P(t)"
wenzelm@12281
   970
    "!!P. (EX x. t=x & P(x)) = P(t)"
wenzelm@12281
   971
    "!!P. (ALL x. x=t --> P(x)) = P(t)"
wenzelm@12937
   972
    "!!P. (ALL x. t=x --> P(x)) = P(t)"
nipkow@17589
   973
  by (blast, blast, blast, blast, blast, iprover+)
wenzelm@13421
   974
wenzelm@12281
   975
lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
nipkow@17589
   976
  by iprover
wenzelm@12281
   977
wenzelm@12281
   978
lemma ex_simps:
wenzelm@12281
   979
  "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
wenzelm@12281
   980
  "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
wenzelm@12281
   981
  "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
wenzelm@12281
   982
  "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
wenzelm@12281
   983
  "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
wenzelm@12281
   984
  "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
wenzelm@12281
   985
  -- {* Miniscoping: pushing in existential quantifiers. *}
nipkow@17589
   986
  by (iprover | blast)+
wenzelm@12281
   987
wenzelm@12281
   988
lemma all_simps:
wenzelm@12281
   989
  "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
wenzelm@12281
   990
  "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
wenzelm@12281
   991
  "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
wenzelm@12281
   992
  "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
wenzelm@12281
   993
  "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
wenzelm@12281
   994
  "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
wenzelm@12281
   995
  -- {* Miniscoping: pushing in universal quantifiers. *}
nipkow@17589
   996
  by (iprover | blast)+
wenzelm@12281
   997
paulson@14201
   998
lemma disj_absorb: "(A | A) = A"
paulson@14201
   999
  by blast
paulson@14201
  1000
paulson@14201
  1001
lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
paulson@14201
  1002
  by blast
paulson@14201
  1003
paulson@14201
  1004
lemma conj_absorb: "(A & A) = A"
paulson@14201
  1005
  by blast
paulson@14201
  1006
paulson@14201
  1007
lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
paulson@14201
  1008
  by blast
paulson@14201
  1009
wenzelm@12281
  1010
lemma eq_ac:
wenzelm@12937
  1011
  shows eq_commute: "(a=b) = (b=a)"
wenzelm@12937
  1012
    and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
nipkow@17589
  1013
    and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
nipkow@17589
  1014
lemma neq_commute: "(a~=b) = (b~=a)" by iprover
wenzelm@12281
  1015
wenzelm@12281
  1016
lemma conj_comms:
wenzelm@12937
  1017
  shows conj_commute: "(P&Q) = (Q&P)"
nipkow@17589
  1018
    and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
nipkow@17589
  1019
lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
wenzelm@12281
  1020
paulson@19174
  1021
lemmas conj_ac = conj_commute conj_left_commute conj_assoc
paulson@19174
  1022
wenzelm@12281
  1023
lemma disj_comms:
wenzelm@12937
  1024
  shows disj_commute: "(P|Q) = (Q|P)"
nipkow@17589
  1025
    and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
nipkow@17589
  1026
lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
wenzelm@12281
  1027
paulson@19174
  1028
lemmas disj_ac = disj_commute disj_left_commute disj_assoc
paulson@19174
  1029
nipkow@17589
  1030
lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
nipkow@17589
  1031
lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
wenzelm@12281
  1032
nipkow@17589
  1033
lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
nipkow@17589
  1034
lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
wenzelm@12281
  1035
nipkow@17589
  1036
lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
nipkow@17589
  1037
lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
nipkow@17589
  1038
lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
wenzelm@12281
  1039
wenzelm@12281
  1040
text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
wenzelm@12281
  1041
lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
wenzelm@12281
  1042
lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
wenzelm@12281
  1043
wenzelm@12281
  1044
lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
wenzelm@12281
  1045
lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
wenzelm@12281
  1046
nipkow@17589
  1047
lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
wenzelm@12281
  1048
lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
wenzelm@12281
  1049
lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
wenzelm@12281
  1050
lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
wenzelm@12281
  1051
lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
wenzelm@12281
  1052
lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
wenzelm@12281
  1053
  by blast
wenzelm@12281
  1054
lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
wenzelm@12281
  1055
nipkow@17589
  1056
lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
wenzelm@12281
  1057
wenzelm@12281
  1058
wenzelm@12281
  1059
lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
wenzelm@12281
  1060
  -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
wenzelm@12281
  1061
  -- {* cases boil down to the same thing. *}
wenzelm@12281
  1062
  by blast
wenzelm@12281
  1063
wenzelm@12281
  1064
lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
wenzelm@12281
  1065
lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
nipkow@17589
  1066
lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
nipkow@17589
  1067
lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
wenzelm@12281
  1068
nipkow@17589
  1069
lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
nipkow@17589
  1070
lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
wenzelm@12281
  1071
wenzelm@12281
  1072
text {*
wenzelm@12281
  1073
  \medskip The @{text "&"} congruence rule: not included by default!
wenzelm@12281
  1074
  May slow rewrite proofs down by as much as 50\% *}
wenzelm@12281
  1075
wenzelm@12281
  1076
lemma conj_cong:
wenzelm@12281
  1077
    "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1078
  by iprover
wenzelm@12281
  1079
wenzelm@12281
  1080
lemma rev_conj_cong:
wenzelm@12281
  1081
    "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
nipkow@17589
  1082
  by iprover
wenzelm@12281
  1083
wenzelm@12281
  1084
text {* The @{text "|"} congruence rule: not included by default! *}
wenzelm@12281
  1085
wenzelm@12281
  1086
lemma disj_cong:
wenzelm@12281
  1087
    "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
wenzelm@12281
  1088
  by blast
wenzelm@12281
  1089
wenzelm@12281
  1090
lemma eq_sym_conv: "(x = y) = (y = x)"
nipkow@17589
  1091
  by iprover
wenzelm@12281
  1092
wenzelm@12281
  1093
wenzelm@12281
  1094
text {* \medskip if-then-else rules *}
wenzelm@12281
  1095
wenzelm@12281
  1096
lemma if_True: "(if True then x else y) = x"
wenzelm@12281
  1097
  by (unfold if_def) blast
wenzelm@12281
  1098
wenzelm@12281
  1099
lemma if_False: "(if False then x else y) = y"
wenzelm@12281
  1100
  by (unfold if_def) blast
wenzelm@12281
  1101
wenzelm@12281
  1102
lemma if_P: "P ==> (if P then x else y) = x"
wenzelm@12281
  1103
  by (unfold if_def) blast
wenzelm@12281
  1104
wenzelm@12281
  1105
lemma if_not_P: "~P ==> (if P then x else y) = y"
wenzelm@12281
  1106
  by (unfold if_def) blast
wenzelm@12281
  1107
wenzelm@12281
  1108
lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
wenzelm@12281
  1109
  apply (rule case_split [of Q])
paulson@15481
  1110
   apply (simplesubst if_P)
paulson@15481
  1111
    prefer 3 apply (simplesubst if_not_P, blast+)
wenzelm@12281
  1112
  done
wenzelm@12281
  1113
wenzelm@12281
  1114
lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
paulson@15481
  1115
by (simplesubst split_if, blast)
wenzelm@12281
  1116
wenzelm@12281
  1117
lemmas if_splits = split_if split_if_asm
wenzelm@12281
  1118
wenzelm@12281
  1119
lemma if_def2: "(if Q then x else y) = ((Q --> x) & (~ Q --> y))"
wenzelm@12281
  1120
  by (rule split_if)
wenzelm@12281
  1121
wenzelm@12281
  1122
lemma if_cancel: "(if c then x else x) = x"
paulson@15481
  1123
by (simplesubst split_if, blast)
wenzelm@12281
  1124
wenzelm@12281
  1125
lemma if_eq_cancel: "(if x = y then y else x) = x"
paulson@15481
  1126
by (simplesubst split_if, blast)
wenzelm@12281
  1127
wenzelm@12281
  1128
lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
wenzelm@12281
  1129
  -- {* This form is useful for expanding @{text if}s on the RIGHT of the @{text "==>"} symbol. *}
wenzelm@12281
  1130
  by (rule split_if)
wenzelm@12281
  1131
wenzelm@12281
  1132
lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
wenzelm@12281
  1133
  -- {* And this form is useful for expanding @{text if}s on the LEFT. *}
paulson@15481
  1134
  apply (simplesubst split_if, blast)
wenzelm@12281
  1135
  done
wenzelm@12281
  1136
nipkow@17589
  1137
lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
nipkow@17589
  1138
lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
wenzelm@12281
  1139
schirmer@15423
  1140
text {* \medskip let rules for simproc *}
schirmer@15423
  1141
schirmer@15423
  1142
lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
schirmer@15423
  1143
  by (unfold Let_def)
schirmer@15423
  1144
schirmer@15423
  1145
lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
schirmer@15423
  1146
  by (unfold Let_def)
schirmer@15423
  1147
berghofe@16633
  1148
text {*
ballarin@16999
  1149
  The following copy of the implication operator is useful for
ballarin@16999
  1150
  fine-tuning congruence rules.  It instructs the simplifier to simplify
ballarin@16999
  1151
  its premise.
berghofe@16633
  1152
*}
berghofe@16633
  1153
wenzelm@17197
  1154
constdefs
wenzelm@17197
  1155
  simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1)
wenzelm@17197
  1156
  "simp_implies \<equiv> op ==>"
berghofe@16633
  1157
wenzelm@18457
  1158
lemma simp_impliesI:
berghofe@16633
  1159
  assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
berghofe@16633
  1160
  shows "PROP P =simp=> PROP Q"
berghofe@16633
  1161
  apply (unfold simp_implies_def)
berghofe@16633
  1162
  apply (rule PQ)
berghofe@16633
  1163
  apply assumption
berghofe@16633
  1164
  done
berghofe@16633
  1165
berghofe@16633
  1166
lemma simp_impliesE:
berghofe@16633
  1167
  assumes PQ:"PROP P =simp=> PROP Q"
berghofe@16633
  1168
  and P: "PROP P"
berghofe@16633
  1169
  and QR: "PROP Q \<Longrightarrow> PROP R"
berghofe@16633
  1170
  shows "PROP R"
berghofe@16633
  1171
  apply (rule QR)
berghofe@16633
  1172
  apply (rule PQ [unfolded simp_implies_def])
berghofe@16633
  1173
  apply (rule P)
berghofe@16633
  1174
  done
berghofe@16633
  1175
berghofe@16633
  1176
lemma simp_implies_cong:
berghofe@16633
  1177
  assumes PP' :"PROP P == PROP P'"
berghofe@16633
  1178
  and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
berghofe@16633
  1179
  shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
berghofe@16633
  1180
proof (unfold simp_implies_def, rule equal_intr_rule)
berghofe@16633
  1181
  assume PQ: "PROP P \<Longrightarrow> PROP Q"
berghofe@16633
  1182
  and P': "PROP P'"
berghofe@16633
  1183
  from PP' [symmetric] and P' have "PROP P"
berghofe@16633
  1184
    by (rule equal_elim_rule1)
berghofe@16633
  1185
  hence "PROP Q" by (rule PQ)
berghofe@16633
  1186
  with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
berghofe@16633
  1187
next
berghofe@16633
  1188
  assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
berghofe@16633
  1189
  and P: "PROP P"
berghofe@16633
  1190
  from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
berghofe@16633
  1191
  hence "PROP Q'" by (rule P'Q')
berghofe@16633
  1192
  with P'QQ' [OF P', symmetric] show "PROP Q"
berghofe@16633
  1193
    by (rule equal_elim_rule1)
berghofe@16633
  1194
qed
berghofe@16633
  1195
wenzelm@17459
  1196
wenzelm@17459
  1197
text {* \medskip Actual Installation of the Simplifier. *}
paulson@14201
  1198
wenzelm@9869
  1199
use "simpdata.ML"
wenzelm@9869
  1200
setup "Simplifier.method_setup Splitter.split_modifiers" setup simpsetup
wenzelm@9869
  1201
setup Splitter.setup setup Clasimp.setup
wenzelm@18591
  1202
setup EqSubst.setup
paulson@15481
  1203
wenzelm@17459
  1204
wenzelm@17459
  1205
subsubsection {* Code generator setup *}
wenzelm@17459
  1206
wenzelm@17459
  1207
types_code
wenzelm@17459
  1208
  "bool"  ("bool")
wenzelm@17459
  1209
attach (term_of) {*
wenzelm@17459
  1210
fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
wenzelm@17459
  1211
*}
wenzelm@17459
  1212
attach (test) {*
wenzelm@17459
  1213
fun gen_bool i = one_of [false, true];
wenzelm@17459
  1214
*}
berghofe@18887
  1215
  "prop"  ("bool")
berghofe@18887
  1216
attach (term_of) {*
berghofe@18887
  1217
fun term_of_prop b =
berghofe@18887
  1218
  HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
berghofe@18887
  1219
*}
wenzelm@17459
  1220
wenzelm@17459
  1221
consts_code
berghofe@18887
  1222
  "Trueprop" ("(_)")
wenzelm@17459
  1223
  "True"    ("true")
wenzelm@17459
  1224
  "False"   ("false")
wenzelm@17459
  1225
  "Not"     ("not")
wenzelm@17459
  1226
  "op |"    ("(_ orelse/ _)")
wenzelm@17459
  1227
  "op &"    ("(_ andalso/ _)")
wenzelm@17459
  1228
  "HOL.If"      ("(if _/ then _/ else _)")
wenzelm@17459
  1229
wenzelm@17459
  1230
ML {*
wenzelm@17459
  1231
local
wenzelm@17459
  1232
wenzelm@17459
  1233
fun eq_codegen thy defs gr dep thyname b t =
wenzelm@17459
  1234
    (case strip_comb t of
wenzelm@17459
  1235
       (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
wenzelm@17459
  1236
     | (Const ("op =", _), [t, u]) =>
wenzelm@17459
  1237
          let
wenzelm@17459
  1238
            val (gr', pt) = Codegen.invoke_codegen thy defs dep thyname false (gr, t);
berghofe@17639
  1239
            val (gr'', pu) = Codegen.invoke_codegen thy defs dep thyname false (gr', u);
berghofe@17639
  1240
            val (gr''', _) = Codegen.invoke_tycodegen thy defs dep thyname false (gr'', HOLogic.boolT)
wenzelm@17459
  1241
          in
berghofe@17639
  1242
            SOME (gr''', Codegen.parens
wenzelm@17459
  1243
              (Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu]))
wenzelm@17459
  1244
          end
wenzelm@17459
  1245
     | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
wenzelm@17459
  1246
         thy defs dep thyname b (gr, Codegen.eta_expand t ts 2))
wenzelm@17459
  1247
     | _ => NONE);
wenzelm@17459
  1248
berghofe@18887
  1249
exception Evaluation of term;
berghofe@18887
  1250
berghofe@18887
  1251
fun evaluation_oracle (thy, Evaluation t) =
berghofe@18887
  1252
  Logic.mk_equals (t, Codegen.eval_term thy t);
berghofe@18887
  1253
berghofe@18887
  1254
fun evaluation_conv ct =
berghofe@18887
  1255
  let val {sign, t, ...} = rep_cterm ct
berghofe@18887
  1256
  in Thm.invoke_oracle_i sign "HOL.Evaluation" (sign, Evaluation t) end;
berghofe@18887
  1257
berghofe@18887
  1258
fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
berghofe@18887
  1259
  (Drule.goals_conv (equal i) evaluation_conv));
berghofe@18887
  1260
berghofe@18887
  1261
val evaluation_meth =
berghofe@18887
  1262
  Method.no_args (Method.METHOD (fn _ => evaluation_tac 1 THEN rtac TrueI 1));
berghofe@18887
  1263
wenzelm@17459
  1264
in
wenzelm@17459
  1265
wenzelm@18708
  1266
val eq_codegen_setup = Codegen.add_codegen "eq_codegen" eq_codegen;
wenzelm@17459
  1267
berghofe@18887
  1268
val evaluation_oracle_setup =
berghofe@18887
  1269
  Theory.add_oracle ("Evaluation", evaluation_oracle) #>
berghofe@18887
  1270
  Method.add_method ("evaluation", evaluation_meth, "solve goal by evaluation");
berghofe@18887
  1271
wenzelm@17459
  1272
end;
wenzelm@17459
  1273
*}
wenzelm@17459
  1274
wenzelm@17459
  1275
setup eq_codegen_setup
berghofe@18887
  1276
setup evaluation_oracle_setup
paulson@15481
  1277
paulson@15481
  1278
paulson@15481
  1279
subsection {* Other simple lemmas *}
paulson@15481
  1280
paulson@15411
  1281
declare disj_absorb [simp] conj_absorb [simp]
paulson@14201
  1282
nipkow@13723
  1283
lemma ex1_eq[iff]: "EX! x. x = t" "EX! x. t = x"
nipkow@13723
  1284
by blast+
nipkow@13723
  1285
paulson@15481
  1286
berghofe@13638
  1287
theorem choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
berghofe@13638
  1288
  apply (rule iffI)
berghofe@13638
  1289
  apply (rule_tac a = "%x. THE y. P x y" in ex1I)
berghofe@13638
  1290
  apply (fast dest!: theI')
berghofe@13638
  1291
  apply (fast intro: ext the1_equality [symmetric])
berghofe@13638
  1292
  apply (erule ex1E)
berghofe@13638
  1293
  apply (rule allI)
berghofe@13638
  1294
  apply (rule ex1I)
berghofe@13638
  1295
  apply (erule spec)
berghofe@13638
  1296
  apply (erule_tac x = "%z. if z = x then y else f z" in allE)
berghofe@13638
  1297
  apply (erule impE)
berghofe@13638
  1298
  apply (rule allI)
berghofe@13638
  1299
  apply (rule_tac P = "xa = x" in case_split_thm)
paulson@14208
  1300
  apply (drule_tac [3] x = x in fun_cong, simp_all)
berghofe@13638
  1301
  done
berghofe@13638
  1302
nipkow@13438
  1303
text{*Needs only HOL-lemmas:*}
nipkow@13438
  1304
lemma mk_left_commute:
nipkow@13438
  1305
  assumes a: "\<And>x y z. f (f x y) z = f x (f y z)" and
nipkow@13438
  1306
          c: "\<And>x y. f x y = f y x"
nipkow@13438
  1307
  shows "f x (f y z) = f y (f x z)"
nipkow@13438
  1308
by(rule trans[OF trans[OF c a] arg_cong[OF c, of "f y"]])
nipkow@13438
  1309
wenzelm@11750
  1310
paulson@15481
  1311
subsection {* Generic cases and induction *}
wenzelm@11824
  1312
wenzelm@11824
  1313
constdefs
wenzelm@18457
  1314
  induct_forall where "induct_forall P == \<forall>x. P x"
wenzelm@18457
  1315
  induct_implies where "induct_implies A B == A \<longrightarrow> B"
wenzelm@18457
  1316
  induct_equal where "induct_equal x y == x = y"
wenzelm@18457
  1317
  induct_conj where "induct_conj A B == A \<and> B"
wenzelm@11824
  1318
wenzelm@11989
  1319
lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
wenzelm@18457
  1320
  by (unfold atomize_all induct_forall_def)
wenzelm@11824
  1321
wenzelm@11989
  1322
lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
wenzelm@18457
  1323
  by (unfold atomize_imp induct_implies_def)
wenzelm@11824
  1324
wenzelm@11989
  1325
lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
wenzelm@18457
  1326
  by (unfold atomize_eq induct_equal_def)
wenzelm@18457
  1327
wenzelm@18457
  1328
lemma induct_conj_eq:
wenzelm@18457
  1329
  includes meta_conjunction_syntax
wenzelm@18457
  1330
  shows "(A && B) == Trueprop (induct_conj A B)"
wenzelm@18457
  1331
  by (unfold atomize_conj induct_conj_def)
wenzelm@18457
  1332
wenzelm@18457
  1333
lemmas induct_atomize = induct_forall_eq induct_implies_eq induct_equal_eq induct_conj_eq
wenzelm@18457
  1334
lemmas induct_rulify [symmetric, standard] = induct_atomize
wenzelm@18457
  1335
lemmas induct_rulify_fallback =
wenzelm@18457
  1336
  induct_forall_def induct_implies_def induct_equal_def induct_conj_def
wenzelm@18457
  1337
wenzelm@11824
  1338
wenzelm@11989
  1339
lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
wenzelm@11989
  1340
    induct_conj (induct_forall A) (induct_forall B)"
nipkow@17589
  1341
  by (unfold induct_forall_def induct_conj_def) iprover
wenzelm@11824
  1342
wenzelm@11989
  1343
lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
wenzelm@11989
  1344
    induct_conj (induct_implies C A) (induct_implies C B)"
nipkow@17589
  1345
  by (unfold induct_implies_def induct_conj_def) iprover
wenzelm@11824
  1346
berghofe@13598
  1347
lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
berghofe@13598
  1348
proof
berghofe@13598
  1349
  assume r: "induct_conj A B ==> PROP C" and A B
wenzelm@18457
  1350
  show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
berghofe@13598
  1351
next
berghofe@13598
  1352
  assume r: "A ==> B ==> PROP C" and "induct_conj A B"
wenzelm@18457
  1353
  show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
berghofe@13598
  1354
qed
wenzelm@11824
  1355
wenzelm@11989
  1356
lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
wenzelm@11989
  1357
wenzelm@11989
  1358
hide const induct_forall induct_implies induct_equal induct_conj
wenzelm@11824
  1359
wenzelm@11824
  1360
wenzelm@11824
  1361
text {* Method setup. *}
wenzelm@11824
  1362
wenzelm@11824
  1363
ML {*
wenzelm@11824
  1364
  structure InductMethod = InductMethodFun
wenzelm@11824
  1365
  (struct
paulson@15411
  1366
    val cases_default = thm "case_split"
paulson@15411
  1367
    val atomize = thms "induct_atomize"
wenzelm@18457
  1368
    val rulify = thms "induct_rulify"
wenzelm@18457
  1369
    val rulify_fallback = thms "induct_rulify_fallback"
wenzelm@11824
  1370
  end);
wenzelm@11824
  1371
*}
wenzelm@11824
  1372
wenzelm@11824
  1373
setup InductMethod.setup
wenzelm@11824
  1374
wenzelm@18457
  1375
wenzelm@18457
  1376
subsubsection {*Tags, for the ATP Linkup *}
paulson@17404
  1377
paulson@17404
  1378
constdefs
paulson@17404
  1379
  tag :: "bool => bool"
wenzelm@18457
  1380
  "tag P == P"
paulson@17404
  1381
paulson@17404
  1382
text{*These label the distinguished literals of introduction and elimination
paulson@17404
  1383
rules.*}
paulson@17404
  1384
paulson@17404
  1385
lemma tagI: "P ==> tag P"
paulson@17404
  1386
by (simp add: tag_def)
paulson@17404
  1387
paulson@17404
  1388
lemma tagD: "tag P ==> P"
paulson@17404
  1389
by (simp add: tag_def)
paulson@17404
  1390
paulson@17404
  1391
text{*Applications of "tag" to True and False must go!*}
paulson@17404
  1392
paulson@17404
  1393
lemma tag_True: "tag True = True"
paulson@17404
  1394
by (simp add: tag_def)
paulson@17404
  1395
paulson@17404
  1396
lemma tag_False: "tag False = False"
paulson@17404
  1397
by (simp add: tag_def)
wenzelm@11824
  1398
haftmann@18702
  1399
haftmann@18702
  1400
subsection {* Code generator setup *}
haftmann@18702
  1401
haftmann@19347
  1402
ML {*
haftmann@19347
  1403
val _ =
haftmann@19347
  1404
  let
haftmann@19347
  1405
    fun true_tac [] = (ALLGOALS o resolve_tac) [TrueI];
haftmann@19347
  1406
    fun false_tac [false_asm] = (ALLGOALS o resolve_tac) [FalseE] THEN (ALLGOALS o resolve_tac) [false_asm]
haftmann@19347
  1407
    fun and_tac impls = (ALLGOALS o resolve_tac) [conjI]
haftmann@19347
  1408
        THEN (ALLGOALS o resolve_tac) impls;
haftmann@19347
  1409
    fun eq_tac [] = (ALLGOALS o resolve_tac o single
haftmann@19347
  1410
      o PureThy.get_thm (the_context ()) o Name) "HOL.atomize_eq";
haftmann@19347
  1411
  in
haftmann@19347
  1412
    CodegenTheorems.init_obj (the_context ())
haftmann@19347
  1413
      "bool" ("True", true_tac) ("False", false_tac)
haftmann@19347
  1414
        ("op &", and_tac) ("op =", eq_tac)
haftmann@19347
  1415
  end;
haftmann@19347
  1416
*}
haftmann@19347
  1417
haftmann@18702
  1418
code_alias
haftmann@18702
  1419
  bool "HOL.bool"
haftmann@18702
  1420
  True "HOL.True"
haftmann@18702
  1421
  False "HOL.False"
haftmann@18702
  1422
  "op =" "HOL.op_eq"
haftmann@18702
  1423
  "op -->" "HOL.op_implies"
haftmann@18702
  1424
  "op &" "HOL.op_and"
haftmann@18702
  1425
  "op |" "HOL.op_or"
haftmann@18702
  1426
  Not "HOL.not"
haftmann@18867
  1427
  arbitrary "HOL.arbitrary"
haftmann@18702
  1428
haftmann@18702
  1429
code_syntax_const
haftmann@19039
  1430
  "op =" (* an intermediate solution for polymorphic equality *)
haftmann@18702
  1431
    ml (infixl 6 "=")
haftmann@18702
  1432
    haskell (infixl 4 "==")
haftmann@18867
  1433
  arbitrary
haftmann@18867
  1434
    ml ("raise/ (Fail/ \"non-defined-result\")")
haftmann@18867
  1435
    haskell ("error/ \"non-defined result\"")
haftmann@18702
  1436
kleing@14357
  1437
end