src/HOL/Library/Extended_Nat.thy
author noschinl
Tue Dec 20 11:40:56 2011 +0100 (2011-12-20)
changeset 45934 9321cd2572fe
parent 45775 6c340de26a0d
child 47108 2a1953f0d20d
permissions -rw-r--r--
add simp rules for enat and ereal
hoelzl@43919
     1
(*  Title:      HOL/Library/Extended_Nat.thy
haftmann@27110
     2
    Author:     David von Oheimb, TU Muenchen;  Florian Haftmann, TU Muenchen
nipkow@41853
     3
    Contributions: David Trachtenherz, TU Muenchen
oheimb@11351
     4
*)
oheimb@11351
     5
hoelzl@43919
     6
header {* Extended natural numbers (i.e. with infinity) *}
oheimb@11351
     7
hoelzl@43919
     8
theory Extended_Nat
haftmann@30663
     9
imports Main
nipkow@15131
    10
begin
oheimb@11351
    11
hoelzl@43921
    12
class infinity =
hoelzl@43921
    13
  fixes infinity :: "'a"
hoelzl@43921
    14
hoelzl@43921
    15
notation (xsymbols)
hoelzl@43921
    16
  infinity  ("\<infinity>")
hoelzl@43921
    17
hoelzl@43921
    18
notation (HTML output)
hoelzl@43921
    19
  infinity  ("\<infinity>")
hoelzl@43921
    20
haftmann@27110
    21
subsection {* Type definition *}
oheimb@11351
    22
oheimb@11351
    23
text {*
wenzelm@11355
    24
  We extend the standard natural numbers by a special value indicating
haftmann@27110
    25
  infinity.
oheimb@11351
    26
*}
oheimb@11351
    27
hoelzl@43921
    28
typedef (open) enat = "UNIV :: nat option set" ..
hoelzl@43921
    29
 
hoelzl@43924
    30
definition enat :: "nat \<Rightarrow> enat" where
hoelzl@43924
    31
  "enat n = Abs_enat (Some n)"
hoelzl@43921
    32
 
hoelzl@43921
    33
instantiation enat :: infinity
hoelzl@43921
    34
begin
hoelzl@43921
    35
  definition "\<infinity> = Abs_enat None"
hoelzl@43921
    36
  instance proof qed
hoelzl@43921
    37
end
hoelzl@43921
    38
 
hoelzl@43924
    39
rep_datatype enat "\<infinity> :: enat"
hoelzl@43921
    40
proof -
hoelzl@43924
    41
  fix P i assume "\<And>j. P (enat j)" "P \<infinity>"
hoelzl@43921
    42
  then show "P i"
hoelzl@43921
    43
  proof induct
hoelzl@43921
    44
    case (Abs_enat y) then show ?case
hoelzl@43921
    45
      by (cases y rule: option.exhaust)
hoelzl@43924
    46
         (auto simp: enat_def infinity_enat_def)
hoelzl@43921
    47
  qed
hoelzl@43924
    48
qed (auto simp add: enat_def infinity_enat_def Abs_enat_inject)
wenzelm@19736
    49
hoelzl@43924
    50
declare [[coercion "enat::nat\<Rightarrow>enat"]]
wenzelm@19736
    51
noschinl@45934
    52
lemmas enat2_cases = enat.exhaust[case_product enat.exhaust]
noschinl@45934
    53
lemmas enat3_cases = enat.exhaust[case_product enat.exhaust enat.exhaust]
noschinl@45934
    54
huffman@44019
    55
lemma not_infinity_eq [iff]: "(x \<noteq> \<infinity>) = (EX i. x = enat i)"
huffman@44019
    56
  by (cases x) auto
nipkow@31084
    57
hoelzl@43924
    58
lemma not_enat_eq [iff]: "(ALL y. x ~= enat y) = (x = \<infinity>)"
huffman@44019
    59
  by (cases x) auto
nipkow@31077
    60
hoelzl@43924
    61
primrec the_enat :: "enat \<Rightarrow> nat"
huffman@44019
    62
  where "the_enat (enat n) = n"
nipkow@41855
    63
haftmann@27110
    64
subsection {* Constructors and numbers *}
haftmann@27110
    65
hoelzl@43919
    66
instantiation enat :: "{zero, one, number}"
haftmann@25594
    67
begin
haftmann@25594
    68
haftmann@25594
    69
definition
hoelzl@43924
    70
  "0 = enat 0"
haftmann@25594
    71
haftmann@25594
    72
definition
hoelzl@43924
    73
  [code_unfold]: "1 = enat 1"
haftmann@25594
    74
haftmann@25594
    75
definition
hoelzl@43924
    76
  [code_unfold, code del]: "number_of k = enat (number_of k)"
oheimb@11351
    77
haftmann@25594
    78
instance ..
haftmann@25594
    79
haftmann@25594
    80
end
haftmann@25594
    81
huffman@44019
    82
definition eSuc :: "enat \<Rightarrow> enat" where
huffman@44019
    83
  "eSuc i = (case i of enat n \<Rightarrow> enat (Suc n) | \<infinity> \<Rightarrow> \<infinity>)"
oheimb@11351
    84
hoelzl@43924
    85
lemma enat_0: "enat 0 = 0"
hoelzl@43919
    86
  by (simp add: zero_enat_def)
haftmann@27110
    87
hoelzl@43924
    88
lemma enat_1: "enat 1 = 1"
hoelzl@43919
    89
  by (simp add: one_enat_def)
haftmann@27110
    90
hoelzl@43924
    91
lemma enat_number: "enat (number_of k) = number_of k"
hoelzl@43919
    92
  by (simp add: number_of_enat_def)
haftmann@27110
    93
huffman@44019
    94
lemma one_eSuc: "1 = eSuc 0"
huffman@44019
    95
  by (simp add: zero_enat_def one_enat_def eSuc_def)
oheimb@11351
    96
huffman@44019
    97
lemma infinity_ne_i0 [simp]: "(\<infinity>::enat) \<noteq> 0"
hoelzl@43919
    98
  by (simp add: zero_enat_def)
oheimb@11351
    99
huffman@44019
   100
lemma i0_ne_infinity [simp]: "0 \<noteq> (\<infinity>::enat)"
hoelzl@43919
   101
  by (simp add: zero_enat_def)
haftmann@27110
   102
hoelzl@43919
   103
lemma zero_enat_eq [simp]:
hoelzl@43919
   104
  "number_of k = (0\<Colon>enat) \<longleftrightarrow> number_of k = (0\<Colon>nat)"
hoelzl@43919
   105
  "(0\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (0\<Colon>nat)"
hoelzl@43919
   106
  unfolding zero_enat_def number_of_enat_def by simp_all
haftmann@27110
   107
hoelzl@43919
   108
lemma one_enat_eq [simp]:
hoelzl@43919
   109
  "number_of k = (1\<Colon>enat) \<longleftrightarrow> number_of k = (1\<Colon>nat)"
hoelzl@43919
   110
  "(1\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (1\<Colon>nat)"
hoelzl@43919
   111
  unfolding one_enat_def number_of_enat_def by simp_all
haftmann@27110
   112
hoelzl@43919
   113
lemma zero_one_enat_neq [simp]:
hoelzl@43919
   114
  "\<not> 0 = (1\<Colon>enat)"
hoelzl@43919
   115
  "\<not> 1 = (0\<Colon>enat)"
hoelzl@43919
   116
  unfolding zero_enat_def one_enat_def by simp_all
oheimb@11351
   117
huffman@44019
   118
lemma infinity_ne_i1 [simp]: "(\<infinity>::enat) \<noteq> 1"
hoelzl@43919
   119
  by (simp add: one_enat_def)
haftmann@27110
   120
huffman@44019
   121
lemma i1_ne_infinity [simp]: "1 \<noteq> (\<infinity>::enat)"
hoelzl@43919
   122
  by (simp add: one_enat_def)
haftmann@27110
   123
huffman@44019
   124
lemma infinity_ne_number [simp]: "(\<infinity>::enat) \<noteq> number_of k"
hoelzl@43919
   125
  by (simp add: number_of_enat_def)
haftmann@27110
   126
huffman@44019
   127
lemma number_ne_infinity [simp]: "number_of k \<noteq> (\<infinity>::enat)"
hoelzl@43919
   128
  by (simp add: number_of_enat_def)
haftmann@27110
   129
huffman@44019
   130
lemma eSuc_enat: "eSuc (enat n) = enat (Suc n)"
huffman@44019
   131
  by (simp add: eSuc_def)
haftmann@27110
   132
huffman@44019
   133
lemma eSuc_number_of: "eSuc (number_of k) = enat (Suc (number_of k))"
huffman@44019
   134
  by (simp add: eSuc_enat number_of_enat_def)
oheimb@11351
   135
huffman@44019
   136
lemma eSuc_infinity [simp]: "eSuc \<infinity> = \<infinity>"
huffman@44019
   137
  by (simp add: eSuc_def)
oheimb@11351
   138
huffman@44019
   139
lemma eSuc_ne_0 [simp]: "eSuc n \<noteq> 0"
huffman@44019
   140
  by (simp add: eSuc_def zero_enat_def split: enat.splits)
haftmann@27110
   141
huffman@44019
   142
lemma zero_ne_eSuc [simp]: "0 \<noteq> eSuc n"
huffman@44019
   143
  by (rule eSuc_ne_0 [symmetric])
oheimb@11351
   144
huffman@44019
   145
lemma eSuc_inject [simp]: "eSuc m = eSuc n \<longleftrightarrow> m = n"
huffman@44019
   146
  by (simp add: eSuc_def split: enat.splits)
haftmann@27110
   147
hoelzl@43919
   148
lemma number_of_enat_inject [simp]:
hoelzl@43919
   149
  "(number_of k \<Colon> enat) = number_of l \<longleftrightarrow> (number_of k \<Colon> nat) = number_of l"
hoelzl@43919
   150
  by (simp add: number_of_enat_def)
oheimb@11351
   151
oheimb@11351
   152
haftmann@27110
   153
subsection {* Addition *}
haftmann@27110
   154
hoelzl@43919
   155
instantiation enat :: comm_monoid_add
haftmann@27110
   156
begin
haftmann@27110
   157
blanchet@38167
   158
definition [nitpick_simp]:
hoelzl@43924
   159
  "m + n = (case m of \<infinity> \<Rightarrow> \<infinity> | enat m \<Rightarrow> (case n of \<infinity> \<Rightarrow> \<infinity> | enat n \<Rightarrow> enat (m + n)))"
oheimb@11351
   160
hoelzl@43919
   161
lemma plus_enat_simps [simp, code]:
hoelzl@43921
   162
  fixes q :: enat
hoelzl@43924
   163
  shows "enat m + enat n = enat (m + n)"
hoelzl@43921
   164
    and "\<infinity> + q = \<infinity>"
hoelzl@43921
   165
    and "q + \<infinity> = \<infinity>"
hoelzl@43919
   166
  by (simp_all add: plus_enat_def split: enat.splits)
haftmann@27110
   167
haftmann@27110
   168
instance proof
hoelzl@43919
   169
  fix n m q :: enat
haftmann@27110
   170
  show "n + m + q = n + (m + q)"
noschinl@45934
   171
    by (cases n m q rule: enat3_cases) auto
haftmann@27110
   172
  show "n + m = m + n"
noschinl@45934
   173
    by (cases n m rule: enat2_cases) auto
haftmann@27110
   174
  show "0 + n = n"
hoelzl@43919
   175
    by (cases n) (simp_all add: zero_enat_def)
huffman@26089
   176
qed
huffman@26089
   177
haftmann@27110
   178
end
oheimb@11351
   179
hoelzl@43919
   180
lemma plus_enat_number [simp]:
hoelzl@43919
   181
  "(number_of k \<Colon> enat) + number_of l = (if k < Int.Pls then number_of l
huffman@29012
   182
    else if l < Int.Pls then number_of k else number_of (k + l))"
hoelzl@43924
   183
  unfolding number_of_enat_def plus_enat_simps nat_arith(1) if_distrib [symmetric, of _ enat] ..
oheimb@11351
   184
huffman@44019
   185
lemma eSuc_number [simp]:
huffman@44019
   186
  "eSuc (number_of k) = (if neg (number_of k \<Colon> int) then 1 else number_of (Int.succ k))"
huffman@44019
   187
  unfolding eSuc_number_of
hoelzl@43919
   188
  unfolding one_enat_def number_of_enat_def Suc_nat_number_of if_distrib [symmetric] ..
oheimb@11351
   189
huffman@44019
   190
lemma eSuc_plus_1:
huffman@44019
   191
  "eSuc n = n + 1"
huffman@44019
   192
  by (cases n) (simp_all add: eSuc_enat one_enat_def)
haftmann@27110
   193
  
huffman@44019
   194
lemma plus_1_eSuc:
huffman@44019
   195
  "1 + q = eSuc q"
huffman@44019
   196
  "q + 1 = eSuc q"
huffman@44019
   197
  by (simp_all add: eSuc_plus_1 add_ac)
nipkow@41853
   198
huffman@44019
   199
lemma iadd_Suc: "eSuc m + n = eSuc (m + n)"
huffman@44019
   200
  by (simp_all add: eSuc_plus_1 add_ac)
oheimb@11351
   201
huffman@44019
   202
lemma iadd_Suc_right: "m + eSuc n = eSuc (m + n)"
huffman@44019
   203
  by (simp only: add_commute[of m] iadd_Suc)
nipkow@41853
   204
hoelzl@43919
   205
lemma iadd_is_0: "(m + n = (0::enat)) = (m = 0 \<and> n = 0)"
huffman@44019
   206
  by (cases m, cases n, simp_all add: zero_enat_def)
oheimb@11351
   207
huffman@29014
   208
subsection {* Multiplication *}
huffman@29014
   209
hoelzl@43919
   210
instantiation enat :: comm_semiring_1
huffman@29014
   211
begin
huffman@29014
   212
hoelzl@43919
   213
definition times_enat_def [nitpick_simp]:
hoelzl@43924
   214
  "m * n = (case m of \<infinity> \<Rightarrow> if n = 0 then 0 else \<infinity> | enat m \<Rightarrow>
hoelzl@43924
   215
    (case n of \<infinity> \<Rightarrow> if m = 0 then 0 else \<infinity> | enat n \<Rightarrow> enat (m * n)))"
huffman@29014
   216
hoelzl@43919
   217
lemma times_enat_simps [simp, code]:
hoelzl@43924
   218
  "enat m * enat n = enat (m * n)"
hoelzl@43921
   219
  "\<infinity> * \<infinity> = (\<infinity>::enat)"
hoelzl@43924
   220
  "\<infinity> * enat n = (if n = 0 then 0 else \<infinity>)"
hoelzl@43924
   221
  "enat m * \<infinity> = (if m = 0 then 0 else \<infinity>)"
hoelzl@43919
   222
  unfolding times_enat_def zero_enat_def
hoelzl@43919
   223
  by (simp_all split: enat.split)
huffman@29014
   224
huffman@29014
   225
instance proof
hoelzl@43919
   226
  fix a b c :: enat
huffman@29014
   227
  show "(a * b) * c = a * (b * c)"
hoelzl@43919
   228
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   229
    by (simp split: enat.split)
huffman@29014
   230
  show "a * b = b * a"
hoelzl@43919
   231
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   232
    by (simp split: enat.split)
huffman@29014
   233
  show "1 * a = a"
hoelzl@43919
   234
    unfolding times_enat_def zero_enat_def one_enat_def
hoelzl@43919
   235
    by (simp split: enat.split)
huffman@29014
   236
  show "(a + b) * c = a * c + b * c"
hoelzl@43919
   237
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   238
    by (simp split: enat.split add: left_distrib)
huffman@29014
   239
  show "0 * a = 0"
hoelzl@43919
   240
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   241
    by (simp split: enat.split)
huffman@29014
   242
  show "a * 0 = 0"
hoelzl@43919
   243
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   244
    by (simp split: enat.split)
hoelzl@43919
   245
  show "(0::enat) \<noteq> 1"
hoelzl@43919
   246
    unfolding zero_enat_def one_enat_def
huffman@29014
   247
    by simp
huffman@29014
   248
qed
huffman@29014
   249
huffman@29014
   250
end
huffman@29014
   251
huffman@44019
   252
lemma mult_eSuc: "eSuc m * n = n + m * n"
huffman@44019
   253
  unfolding eSuc_plus_1 by (simp add: algebra_simps)
huffman@29014
   254
huffman@44019
   255
lemma mult_eSuc_right: "m * eSuc n = m + m * n"
huffman@44019
   256
  unfolding eSuc_plus_1 by (simp add: algebra_simps)
huffman@29014
   257
hoelzl@43924
   258
lemma of_nat_eq_enat: "of_nat n = enat n"
huffman@29023
   259
  apply (induct n)
hoelzl@43924
   260
  apply (simp add: enat_0)
huffman@44019
   261
  apply (simp add: plus_1_eSuc eSuc_enat)
huffman@29023
   262
  done
huffman@29023
   263
hoelzl@43919
   264
instance enat :: number_semiring
huffman@43532
   265
proof
hoelzl@43919
   266
  fix n show "number_of (int n) = (of_nat n :: enat)"
hoelzl@43924
   267
    unfolding number_of_enat_def number_of_int of_nat_id of_nat_eq_enat ..
huffman@43532
   268
qed
huffman@43532
   269
hoelzl@43919
   270
instance enat :: semiring_char_0 proof
hoelzl@43924
   271
  have "inj enat" by (rule injI) simp
hoelzl@43924
   272
  then show "inj (\<lambda>n. of_nat n :: enat)" by (simp add: of_nat_eq_enat)
haftmann@38621
   273
qed
huffman@29023
   274
huffman@44019
   275
lemma imult_is_0 [simp]: "((m::enat) * n = 0) = (m = 0 \<or> n = 0)"
huffman@44019
   276
  by (auto simp add: times_enat_def zero_enat_def split: enat.split)
nipkow@41853
   277
huffman@44019
   278
lemma imult_is_infinity: "((a::enat) * b = \<infinity>) = (a = \<infinity> \<and> b \<noteq> 0 \<or> b = \<infinity> \<and> a \<noteq> 0)"
huffman@44019
   279
  by (auto simp add: times_enat_def zero_enat_def split: enat.split)
nipkow@41853
   280
nipkow@41853
   281
nipkow@41853
   282
subsection {* Subtraction *}
nipkow@41853
   283
hoelzl@43919
   284
instantiation enat :: minus
nipkow@41853
   285
begin
nipkow@41853
   286
hoelzl@43919
   287
definition diff_enat_def:
hoelzl@43924
   288
"a - b = (case a of (enat x) \<Rightarrow> (case b of (enat y) \<Rightarrow> enat (x - y) | \<infinity> \<Rightarrow> 0)
nipkow@41853
   289
          | \<infinity> \<Rightarrow> \<infinity>)"
nipkow@41853
   290
nipkow@41853
   291
instance ..
nipkow@41853
   292
nipkow@41853
   293
end
nipkow@41853
   294
huffman@44019
   295
lemma idiff_enat_enat [simp,code]: "enat a - enat b = enat (a - b)"
huffman@44019
   296
  by (simp add: diff_enat_def)
nipkow@41853
   297
huffman@44019
   298
lemma idiff_infinity [simp,code]: "\<infinity> - n = (\<infinity>::enat)"
huffman@44019
   299
  by (simp add: diff_enat_def)
nipkow@41853
   300
huffman@44019
   301
lemma idiff_infinity_right [simp,code]: "enat a - \<infinity> = 0"
huffman@44019
   302
  by (simp add: diff_enat_def)
nipkow@41853
   303
huffman@44019
   304
lemma idiff_0 [simp]: "(0::enat) - n = 0"
huffman@44019
   305
  by (cases n, simp_all add: zero_enat_def)
nipkow@41853
   306
huffman@44019
   307
lemmas idiff_enat_0 [simp] = idiff_0 [unfolded zero_enat_def]
nipkow@41853
   308
huffman@44019
   309
lemma idiff_0_right [simp]: "(n::enat) - 0 = n"
huffman@44019
   310
  by (cases n) (simp_all add: zero_enat_def)
nipkow@41853
   311
huffman@44019
   312
lemmas idiff_enat_0_right [simp] = idiff_0_right [unfolded zero_enat_def]
nipkow@41853
   313
huffman@44019
   314
lemma idiff_self [simp]: "n \<noteq> \<infinity> \<Longrightarrow> (n::enat) - n = 0"
huffman@44019
   315
  by (auto simp: zero_enat_def)
nipkow@41853
   316
huffman@44019
   317
lemma eSuc_minus_eSuc [simp]: "eSuc n - eSuc m = n - m"
huffman@44019
   318
  by (simp add: eSuc_def split: enat.split)
nipkow@41855
   319
huffman@44019
   320
lemma eSuc_minus_1 [simp]: "eSuc n - 1 = n"
huffman@44019
   321
  by (simp add: one_enat_def eSuc_enat[symmetric] zero_enat_def[symmetric])
nipkow@41855
   322
hoelzl@43924
   323
(*lemmas idiff_self_eq_0_enat = idiff_self_eq_0[unfolded zero_enat_def]*)
nipkow@41853
   324
haftmann@27110
   325
subsection {* Ordering *}
haftmann@27110
   326
hoelzl@43919
   327
instantiation enat :: linordered_ab_semigroup_add
haftmann@27110
   328
begin
oheimb@11351
   329
blanchet@38167
   330
definition [nitpick_simp]:
hoelzl@43924
   331
  "m \<le> n = (case n of enat n1 \<Rightarrow> (case m of enat m1 \<Rightarrow> m1 \<le> n1 | \<infinity> \<Rightarrow> False)
haftmann@27110
   332
    | \<infinity> \<Rightarrow> True)"
oheimb@11351
   333
blanchet@38167
   334
definition [nitpick_simp]:
hoelzl@43924
   335
  "m < n = (case m of enat m1 \<Rightarrow> (case n of enat n1 \<Rightarrow> m1 < n1 | \<infinity> \<Rightarrow> True)
haftmann@27110
   336
    | \<infinity> \<Rightarrow> False)"
oheimb@11351
   337
hoelzl@43919
   338
lemma enat_ord_simps [simp]:
hoelzl@43924
   339
  "enat m \<le> enat n \<longleftrightarrow> m \<le> n"
hoelzl@43924
   340
  "enat m < enat n \<longleftrightarrow> m < n"
hoelzl@43921
   341
  "q \<le> (\<infinity>::enat)"
hoelzl@43921
   342
  "q < (\<infinity>::enat) \<longleftrightarrow> q \<noteq> \<infinity>"
hoelzl@43921
   343
  "(\<infinity>::enat) \<le> q \<longleftrightarrow> q = \<infinity>"
hoelzl@43921
   344
  "(\<infinity>::enat) < q \<longleftrightarrow> False"
hoelzl@43919
   345
  by (simp_all add: less_eq_enat_def less_enat_def split: enat.splits)
oheimb@11351
   346
noschinl@45934
   347
lemma number_of_le_enat_iff[simp]:
noschinl@45934
   348
  shows "number_of m \<le> enat n \<longleftrightarrow> number_of m \<le> n"
noschinl@45934
   349
by (auto simp: number_of_enat_def)
noschinl@45934
   350
noschinl@45934
   351
lemma number_of_less_enat_iff[simp]:
noschinl@45934
   352
  shows "number_of m < enat n \<longleftrightarrow> number_of m < n"
noschinl@45934
   353
by (auto simp: number_of_enat_def)
noschinl@45934
   354
hoelzl@43919
   355
lemma enat_ord_code [code]:
hoelzl@43924
   356
  "enat m \<le> enat n \<longleftrightarrow> m \<le> n"
hoelzl@43924
   357
  "enat m < enat n \<longleftrightarrow> m < n"
hoelzl@43921
   358
  "q \<le> (\<infinity>::enat) \<longleftrightarrow> True"
hoelzl@43924
   359
  "enat m < \<infinity> \<longleftrightarrow> True"
hoelzl@43924
   360
  "\<infinity> \<le> enat n \<longleftrightarrow> False"
hoelzl@43921
   361
  "(\<infinity>::enat) < q \<longleftrightarrow> False"
haftmann@27110
   362
  by simp_all
oheimb@11351
   363
haftmann@27110
   364
instance by default
hoelzl@43919
   365
  (auto simp add: less_eq_enat_def less_enat_def plus_enat_def split: enat.splits)
oheimb@11351
   366
haftmann@27110
   367
end
haftmann@27110
   368
hoelzl@43919
   369
instance enat :: ordered_comm_semiring
huffman@29014
   370
proof
hoelzl@43919
   371
  fix a b c :: enat
huffman@29014
   372
  assume "a \<le> b" and "0 \<le> c"
huffman@29014
   373
  thus "c * a \<le> c * b"
hoelzl@43919
   374
    unfolding times_enat_def less_eq_enat_def zero_enat_def
hoelzl@43919
   375
    by (simp split: enat.splits)
huffman@29014
   376
qed
huffman@29014
   377
hoelzl@43919
   378
lemma enat_ord_number [simp]:
hoelzl@43919
   379
  "(number_of m \<Colon> enat) \<le> number_of n \<longleftrightarrow> (number_of m \<Colon> nat) \<le> number_of n"
hoelzl@43919
   380
  "(number_of m \<Colon> enat) < number_of n \<longleftrightarrow> (number_of m \<Colon> nat) < number_of n"
hoelzl@43919
   381
  by (simp_all add: number_of_enat_def)
oheimb@11351
   382
hoelzl@43919
   383
lemma i0_lb [simp]: "(0\<Colon>enat) \<le> n"
hoelzl@43919
   384
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
oheimb@11351
   385
hoelzl@43919
   386
lemma ile0_eq [simp]: "n \<le> (0\<Colon>enat) \<longleftrightarrow> n = 0"
hoelzl@43919
   387
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
oheimb@11351
   388
huffman@44019
   389
lemma infinity_ileE [elim!]: "\<infinity> \<le> enat m \<Longrightarrow> R"
huffman@44019
   390
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
huffman@44019
   391
huffman@44019
   392
lemma infinity_ilessE [elim!]: "\<infinity> < enat m \<Longrightarrow> R"
haftmann@27110
   393
  by simp
oheimb@11351
   394
hoelzl@43919
   395
lemma not_iless0 [simp]: "\<not> n < (0\<Colon>enat)"
hoelzl@43919
   396
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
haftmann@27110
   397
hoelzl@43919
   398
lemma i0_less [simp]: "(0\<Colon>enat) < n \<longleftrightarrow> n \<noteq> 0"
huffman@44019
   399
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
oheimb@11351
   400
huffman@44019
   401
lemma eSuc_ile_mono [simp]: "eSuc n \<le> eSuc m \<longleftrightarrow> n \<le> m"
huffman@44019
   402
  by (simp add: eSuc_def less_eq_enat_def split: enat.splits)
haftmann@27110
   403
 
huffman@44019
   404
lemma eSuc_mono [simp]: "eSuc n < eSuc m \<longleftrightarrow> n < m"
huffman@44019
   405
  by (simp add: eSuc_def less_enat_def split: enat.splits)
oheimb@11351
   406
huffman@44019
   407
lemma ile_eSuc [simp]: "n \<le> eSuc n"
huffman@44019
   408
  by (simp add: eSuc_def less_eq_enat_def split: enat.splits)
oheimb@11351
   409
huffman@44019
   410
lemma not_eSuc_ilei0 [simp]: "\<not> eSuc n \<le> 0"
huffman@44019
   411
  by (simp add: zero_enat_def eSuc_def less_eq_enat_def split: enat.splits)
haftmann@27110
   412
huffman@44019
   413
lemma i0_iless_eSuc [simp]: "0 < eSuc n"
huffman@44019
   414
  by (simp add: zero_enat_def eSuc_def less_enat_def split: enat.splits)
haftmann@27110
   415
huffman@44019
   416
lemma iless_eSuc0[simp]: "(n < eSuc 0) = (n = 0)"
huffman@44019
   417
  by (simp add: zero_enat_def eSuc_def less_enat_def split: enat.split)
nipkow@41853
   418
huffman@44019
   419
lemma ileI1: "m < n \<Longrightarrow> eSuc m \<le> n"
huffman@44019
   420
  by (simp add: eSuc_def less_eq_enat_def less_enat_def split: enat.splits)
haftmann@27110
   421
hoelzl@43924
   422
lemma Suc_ile_eq: "enat (Suc m) \<le> n \<longleftrightarrow> enat m < n"
haftmann@27110
   423
  by (cases n) auto
haftmann@27110
   424
huffman@44019
   425
lemma iless_Suc_eq [simp]: "enat m < eSuc n \<longleftrightarrow> enat m \<le> n"
huffman@44019
   426
  by (auto simp add: eSuc_def less_enat_def split: enat.splits)
oheimb@11351
   427
huffman@44019
   428
lemma imult_infinity: "(0::enat) < n \<Longrightarrow> \<infinity> * n = \<infinity>"
huffman@44019
   429
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
nipkow@41853
   430
huffman@44019
   431
lemma imult_infinity_right: "(0::enat) < n \<Longrightarrow> n * \<infinity> = \<infinity>"
huffman@44019
   432
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
nipkow@41853
   433
hoelzl@43919
   434
lemma enat_0_less_mult_iff: "(0 < (m::enat) * n) = (0 < m \<and> 0 < n)"
huffman@44019
   435
  by (simp only: i0_less imult_is_0, simp)
nipkow@41853
   436
huffman@44019
   437
lemma mono_eSuc: "mono eSuc"
huffman@44019
   438
  by (simp add: mono_def)
nipkow@41853
   439
nipkow@41853
   440
hoelzl@43919
   441
lemma min_enat_simps [simp]:
hoelzl@43924
   442
  "min (enat m) (enat n) = enat (min m n)"
haftmann@27110
   443
  "min q 0 = 0"
haftmann@27110
   444
  "min 0 q = 0"
hoelzl@43921
   445
  "min q (\<infinity>::enat) = q"
hoelzl@43921
   446
  "min (\<infinity>::enat) q = q"
haftmann@27110
   447
  by (auto simp add: min_def)
oheimb@11351
   448
hoelzl@43919
   449
lemma max_enat_simps [simp]:
hoelzl@43924
   450
  "max (enat m) (enat n) = enat (max m n)"
haftmann@27110
   451
  "max q 0 = q"
haftmann@27110
   452
  "max 0 q = q"
hoelzl@43921
   453
  "max q \<infinity> = (\<infinity>::enat)"
hoelzl@43921
   454
  "max \<infinity> q = (\<infinity>::enat)"
haftmann@27110
   455
  by (simp_all add: max_def)
haftmann@27110
   456
hoelzl@43924
   457
lemma enat_ile: "n \<le> enat m \<Longrightarrow> \<exists>k. n = enat k"
haftmann@27110
   458
  by (cases n) simp_all
haftmann@27110
   459
hoelzl@43924
   460
lemma enat_iless: "n < enat m \<Longrightarrow> \<exists>k. n = enat k"
haftmann@27110
   461
  by (cases n) simp_all
oheimb@11351
   462
hoelzl@43924
   463
lemma chain_incr: "\<forall>i. \<exists>j. Y i < Y j ==> \<exists>j. enat k < Y j"
nipkow@25134
   464
apply (induct_tac k)
hoelzl@43924
   465
 apply (simp (no_asm) only: enat_0)
haftmann@27110
   466
 apply (fast intro: le_less_trans [OF i0_lb])
nipkow@25134
   467
apply (erule exE)
nipkow@25134
   468
apply (drule spec)
nipkow@25134
   469
apply (erule exE)
nipkow@25134
   470
apply (drule ileI1)
huffman@44019
   471
apply (rule eSuc_enat [THEN subst])
nipkow@25134
   472
apply (rule exI)
haftmann@27110
   473
apply (erule (1) le_less_trans)
nipkow@25134
   474
done
oheimb@11351
   475
hoelzl@43919
   476
instantiation enat :: "{bot, top}"
haftmann@29337
   477
begin
haftmann@29337
   478
hoelzl@43919
   479
definition bot_enat :: enat where
hoelzl@43919
   480
  "bot_enat = 0"
haftmann@29337
   481
hoelzl@43919
   482
definition top_enat :: enat where
hoelzl@43919
   483
  "top_enat = \<infinity>"
haftmann@29337
   484
haftmann@29337
   485
instance proof
hoelzl@43919
   486
qed (simp_all add: bot_enat_def top_enat_def)
haftmann@29337
   487
haftmann@29337
   488
end
haftmann@29337
   489
hoelzl@43924
   490
lemma finite_enat_bounded:
hoelzl@43924
   491
  assumes le_fin: "\<And>y. y \<in> A \<Longrightarrow> y \<le> enat n"
noschinl@42993
   492
  shows "finite A"
noschinl@42993
   493
proof (rule finite_subset)
hoelzl@43924
   494
  show "finite (enat ` {..n})" by blast
noschinl@42993
   495
nipkow@44890
   496
  have "A \<subseteq> {..enat n}" using le_fin by fastforce
hoelzl@43924
   497
  also have "\<dots> \<subseteq> enat ` {..n}"
noschinl@42993
   498
    by (rule subsetI) (case_tac x, auto)
hoelzl@43924
   499
  finally show "A \<subseteq> enat ` {..n}" .
noschinl@42993
   500
qed
noschinl@42993
   501
huffman@26089
   502
huffman@45775
   503
subsection {* Cancellation simprocs *}
huffman@45775
   504
huffman@45775
   505
lemma enat_add_left_cancel: "a + b = a + c \<longleftrightarrow> a = (\<infinity>::enat) \<or> b = c"
huffman@45775
   506
  unfolding plus_enat_def by (simp split: enat.split)
huffman@45775
   507
huffman@45775
   508
lemma enat_add_left_cancel_le: "a + b \<le> a + c \<longleftrightarrow> a = (\<infinity>::enat) \<or> b \<le> c"
huffman@45775
   509
  unfolding plus_enat_def by (simp split: enat.split)
huffman@45775
   510
huffman@45775
   511
lemma enat_add_left_cancel_less: "a + b < a + c \<longleftrightarrow> a \<noteq> (\<infinity>::enat) \<and> b < c"
huffman@45775
   512
  unfolding plus_enat_def by (simp split: enat.split)
huffman@45775
   513
huffman@45775
   514
ML {*
huffman@45775
   515
structure Cancel_Enat_Common =
huffman@45775
   516
struct
huffman@45775
   517
  (* copied from src/HOL/Tools/nat_numeral_simprocs.ML *)
huffman@45775
   518
  fun find_first_t _    _ []         = raise TERM("find_first_t", [])
huffman@45775
   519
    | find_first_t past u (t::terms) =
huffman@45775
   520
          if u aconv t then (rev past @ terms)
huffman@45775
   521
          else find_first_t (t::past) u terms
huffman@45775
   522
huffman@45775
   523
  val mk_sum = Arith_Data.long_mk_sum
huffman@45775
   524
  val dest_sum = Arith_Data.dest_sum
huffman@45775
   525
  val find_first = find_first_t []
huffman@45775
   526
  val trans_tac = Numeral_Simprocs.trans_tac
huffman@45775
   527
  val norm_ss = HOL_basic_ss addsimps
huffman@45775
   528
    @{thms add_ac semiring_numeral_0_eq_0 add_0_left add_0_right}
huffman@45775
   529
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
huffman@45775
   530
  fun simplify_meta_eq ss cancel_th th =
huffman@45775
   531
    Arith_Data.simplify_meta_eq @{thms semiring_numeral_0_eq_0} ss
huffman@45775
   532
      ([th, cancel_th] MRS trans)
huffman@45775
   533
  fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
huffman@45775
   534
end
huffman@45775
   535
huffman@45775
   536
structure Eq_Enat_Cancel = ExtractCommonTermFun
huffman@45775
   537
(open Cancel_Enat_Common
huffman@45775
   538
  val mk_bal = HOLogic.mk_eq
huffman@45775
   539
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} @{typ enat}
huffman@45775
   540
  fun simp_conv _ _ = SOME @{thm enat_add_left_cancel}
huffman@45775
   541
)
huffman@45775
   542
huffman@45775
   543
structure Le_Enat_Cancel = ExtractCommonTermFun
huffman@45775
   544
(open Cancel_Enat_Common
huffman@45775
   545
  val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq}
huffman@45775
   546
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} @{typ enat}
huffman@45775
   547
  fun simp_conv _ _ = SOME @{thm enat_add_left_cancel_le}
huffman@45775
   548
)
huffman@45775
   549
huffman@45775
   550
structure Less_Enat_Cancel = ExtractCommonTermFun
huffman@45775
   551
(open Cancel_Enat_Common
huffman@45775
   552
  val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less}
huffman@45775
   553
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} @{typ enat}
huffman@45775
   554
  fun simp_conv _ _ = SOME @{thm enat_add_left_cancel_less}
huffman@45775
   555
)
huffman@45775
   556
*}
huffman@45775
   557
huffman@45775
   558
simproc_setup enat_eq_cancel
huffman@45775
   559
  ("(l::enat) + m = n" | "(l::enat) = m + n") =
huffman@45775
   560
  {* fn phi => fn ss => fn ct => Eq_Enat_Cancel.proc ss (term_of ct) *}
huffman@45775
   561
huffman@45775
   562
simproc_setup enat_le_cancel
huffman@45775
   563
  ("(l::enat) + m \<le> n" | "(l::enat) \<le> m + n") =
huffman@45775
   564
  {* fn phi => fn ss => fn ct => Le_Enat_Cancel.proc ss (term_of ct) *}
huffman@45775
   565
huffman@45775
   566
simproc_setup enat_less_cancel
huffman@45775
   567
  ("(l::enat) + m < n" | "(l::enat) < m + n") =
huffman@45775
   568
  {* fn phi => fn ss => fn ct => Less_Enat_Cancel.proc ss (term_of ct) *}
huffman@45775
   569
huffman@45775
   570
text {* TODO: add regression tests for these simprocs *}
huffman@45775
   571
huffman@45775
   572
text {* TODO: add simprocs for combining and cancelling numerals *}
huffman@45775
   573
huffman@45775
   574
haftmann@27110
   575
subsection {* Well-ordering *}
huffman@26089
   576
hoelzl@43924
   577
lemma less_enatE:
hoelzl@43924
   578
  "[| n < enat m; !!k. n = enat k ==> k < m ==> P |] ==> P"
huffman@26089
   579
by (induct n) auto
huffman@26089
   580
huffman@44019
   581
lemma less_infinityE:
hoelzl@43924
   582
  "[| n < \<infinity>; !!k. n = enat k ==> P |] ==> P"
huffman@26089
   583
by (induct n) auto
huffman@26089
   584
hoelzl@43919
   585
lemma enat_less_induct:
hoelzl@43919
   586
  assumes prem: "!!n. \<forall>m::enat. m < n --> P m ==> P n" shows "P n"
huffman@26089
   587
proof -
hoelzl@43924
   588
  have P_enat: "!!k. P (enat k)"
huffman@26089
   589
    apply (rule nat_less_induct)
huffman@26089
   590
    apply (rule prem, clarify)
hoelzl@43924
   591
    apply (erule less_enatE, simp)
huffman@26089
   592
    done
huffman@26089
   593
  show ?thesis
huffman@26089
   594
  proof (induct n)
huffman@26089
   595
    fix nat
hoelzl@43924
   596
    show "P (enat nat)" by (rule P_enat)
huffman@26089
   597
  next
hoelzl@43921
   598
    show "P \<infinity>"
huffman@26089
   599
      apply (rule prem, clarify)
huffman@44019
   600
      apply (erule less_infinityE)
hoelzl@43924
   601
      apply (simp add: P_enat)
huffman@26089
   602
      done
huffman@26089
   603
  qed
huffman@26089
   604
qed
huffman@26089
   605
hoelzl@43919
   606
instance enat :: wellorder
huffman@26089
   607
proof
haftmann@27823
   608
  fix P and n
hoelzl@43919
   609
  assume hyp: "(\<And>n\<Colon>enat. (\<And>m\<Colon>enat. m < n \<Longrightarrow> P m) \<Longrightarrow> P n)"
hoelzl@43919
   610
  show "P n" by (blast intro: enat_less_induct hyp)
huffman@26089
   611
qed
huffman@26089
   612
noschinl@42993
   613
subsection {* Complete Lattice *}
noschinl@42993
   614
hoelzl@43919
   615
instantiation enat :: complete_lattice
noschinl@42993
   616
begin
noschinl@42993
   617
hoelzl@43919
   618
definition inf_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
hoelzl@43919
   619
  "inf_enat \<equiv> min"
noschinl@42993
   620
hoelzl@43919
   621
definition sup_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
hoelzl@43919
   622
  "sup_enat \<equiv> max"
noschinl@42993
   623
hoelzl@43919
   624
definition Inf_enat :: "enat set \<Rightarrow> enat" where
hoelzl@43919
   625
  "Inf_enat A \<equiv> if A = {} then \<infinity> else (LEAST x. x \<in> A)"
noschinl@42993
   626
hoelzl@43919
   627
definition Sup_enat :: "enat set \<Rightarrow> enat" where
hoelzl@43919
   628
  "Sup_enat A \<equiv> if A = {} then 0
noschinl@42993
   629
    else if finite A then Max A
noschinl@42993
   630
                     else \<infinity>"
noschinl@42993
   631
instance proof
hoelzl@43919
   632
  fix x :: "enat" and A :: "enat set"
noschinl@42993
   633
  { assume "x \<in> A" then show "Inf A \<le> x"
hoelzl@43919
   634
      unfolding Inf_enat_def by (auto intro: Least_le) }
noschinl@42993
   635
  { assume "\<And>y. y \<in> A \<Longrightarrow> x \<le> y" then show "x \<le> Inf A"
hoelzl@43919
   636
      unfolding Inf_enat_def
noschinl@42993
   637
      by (cases "A = {}") (auto intro: LeastI2_ex) }
noschinl@42993
   638
  { assume "x \<in> A" then show "x \<le> Sup A"
hoelzl@43919
   639
      unfolding Sup_enat_def by (cases "finite A") auto }
noschinl@42993
   640
  { assume "\<And>y. y \<in> A \<Longrightarrow> y \<le> x" then show "Sup A \<le> x"
hoelzl@43924
   641
      unfolding Sup_enat_def using finite_enat_bounded by auto }
hoelzl@43919
   642
qed (simp_all add: inf_enat_def sup_enat_def)
noschinl@42993
   643
end
noschinl@42993
   644
hoelzl@43978
   645
instance enat :: complete_linorder ..
haftmann@27110
   646
haftmann@27110
   647
subsection {* Traditional theorem names *}
haftmann@27110
   648
huffman@44019
   649
lemmas enat_defs = zero_enat_def one_enat_def number_of_enat_def eSuc_def
hoelzl@43919
   650
  plus_enat_def less_eq_enat_def less_enat_def
haftmann@27110
   651
oheimb@11351
   652
end