src/HOL/Library/Extended_Nat.thy
author hoelzl
Tue Jul 19 14:35:44 2011 +0200 (2011-07-19)
changeset 43919 a7e4fb1a0502
parent 43532 src/HOL/Library/Nat_Infinity.thy@d32d72ea3215
child 43921 e8511be08ddd
permissions -rw-r--r--
rename Nat_Infinity (inat) to Extended_Nat (enat)
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
haftmann@27110
    12
subsection {* Type definition *}
oheimb@11351
    13
oheimb@11351
    14
text {*
wenzelm@11355
    15
  We extend the standard natural numbers by a special value indicating
haftmann@27110
    16
  infinity.
oheimb@11351
    17
*}
oheimb@11351
    18
hoelzl@43919
    19
datatype enat = Fin nat | Infty
oheimb@11351
    20
wenzelm@21210
    21
notation (xsymbols)
wenzelm@19736
    22
  Infty  ("\<infinity>")
wenzelm@19736
    23
wenzelm@21210
    24
notation (HTML output)
wenzelm@19736
    25
  Infty  ("\<infinity>")
wenzelm@19736
    26
oheimb@11351
    27
nipkow@31084
    28
lemma not_Infty_eq[iff]: "(x ~= Infty) = (EX i. x = Fin i)"
nipkow@31084
    29
by (cases x) auto
nipkow@31084
    30
nipkow@31084
    31
lemma not_Fin_eq [iff]: "(ALL y. x ~= Fin y) = (x = Infty)"
nipkow@31077
    32
by (cases x) auto
nipkow@31077
    33
nipkow@31077
    34
hoelzl@43919
    35
primrec the_Fin :: "enat \<Rightarrow> nat"
nipkow@41855
    36
where "the_Fin (Fin n) = n"
nipkow@41855
    37
nipkow@41855
    38
haftmann@27110
    39
subsection {* Constructors and numbers *}
haftmann@27110
    40
hoelzl@43919
    41
instantiation enat :: "{zero, one, number}"
haftmann@25594
    42
begin
haftmann@25594
    43
haftmann@25594
    44
definition
haftmann@27110
    45
  "0 = Fin 0"
haftmann@25594
    46
haftmann@25594
    47
definition
haftmann@32069
    48
  [code_unfold]: "1 = Fin 1"
haftmann@25594
    49
haftmann@25594
    50
definition
haftmann@32069
    51
  [code_unfold, code del]: "number_of k = Fin (number_of k)"
oheimb@11351
    52
haftmann@25594
    53
instance ..
haftmann@25594
    54
haftmann@25594
    55
end
haftmann@25594
    56
hoelzl@43919
    57
definition iSuc :: "enat \<Rightarrow> enat" where
haftmann@27110
    58
  "iSuc i = (case i of Fin n \<Rightarrow> Fin (Suc n) | \<infinity> \<Rightarrow> \<infinity>)"
oheimb@11351
    59
oheimb@11351
    60
lemma Fin_0: "Fin 0 = 0"
hoelzl@43919
    61
  by (simp add: zero_enat_def)
haftmann@27110
    62
haftmann@27110
    63
lemma Fin_1: "Fin 1 = 1"
hoelzl@43919
    64
  by (simp add: one_enat_def)
haftmann@27110
    65
haftmann@27110
    66
lemma Fin_number: "Fin (number_of k) = number_of k"
hoelzl@43919
    67
  by (simp add: number_of_enat_def)
haftmann@27110
    68
haftmann@27110
    69
lemma one_iSuc: "1 = iSuc 0"
hoelzl@43919
    70
  by (simp add: zero_enat_def one_enat_def iSuc_def)
oheimb@11351
    71
oheimb@11351
    72
lemma Infty_ne_i0 [simp]: "\<infinity> \<noteq> 0"
hoelzl@43919
    73
  by (simp add: zero_enat_def)
oheimb@11351
    74
oheimb@11351
    75
lemma i0_ne_Infty [simp]: "0 \<noteq> \<infinity>"
hoelzl@43919
    76
  by (simp add: zero_enat_def)
haftmann@27110
    77
hoelzl@43919
    78
lemma zero_enat_eq [simp]:
hoelzl@43919
    79
  "number_of k = (0\<Colon>enat) \<longleftrightarrow> number_of k = (0\<Colon>nat)"
hoelzl@43919
    80
  "(0\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (0\<Colon>nat)"
hoelzl@43919
    81
  unfolding zero_enat_def number_of_enat_def by simp_all
haftmann@27110
    82
hoelzl@43919
    83
lemma one_enat_eq [simp]:
hoelzl@43919
    84
  "number_of k = (1\<Colon>enat) \<longleftrightarrow> number_of k = (1\<Colon>nat)"
hoelzl@43919
    85
  "(1\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (1\<Colon>nat)"
hoelzl@43919
    86
  unfolding one_enat_def number_of_enat_def by simp_all
haftmann@27110
    87
hoelzl@43919
    88
lemma zero_one_enat_neq [simp]:
hoelzl@43919
    89
  "\<not> 0 = (1\<Colon>enat)"
hoelzl@43919
    90
  "\<not> 1 = (0\<Colon>enat)"
hoelzl@43919
    91
  unfolding zero_enat_def one_enat_def by simp_all
oheimb@11351
    92
haftmann@27110
    93
lemma Infty_ne_i1 [simp]: "\<infinity> \<noteq> 1"
hoelzl@43919
    94
  by (simp add: one_enat_def)
haftmann@27110
    95
haftmann@27110
    96
lemma i1_ne_Infty [simp]: "1 \<noteq> \<infinity>"
hoelzl@43919
    97
  by (simp add: one_enat_def)
haftmann@27110
    98
haftmann@27110
    99
lemma Infty_ne_number [simp]: "\<infinity> \<noteq> number_of k"
hoelzl@43919
   100
  by (simp add: number_of_enat_def)
haftmann@27110
   101
haftmann@27110
   102
lemma number_ne_Infty [simp]: "number_of k \<noteq> \<infinity>"
hoelzl@43919
   103
  by (simp add: number_of_enat_def)
haftmann@27110
   104
haftmann@27110
   105
lemma iSuc_Fin: "iSuc (Fin n) = Fin (Suc n)"
haftmann@27110
   106
  by (simp add: iSuc_def)
haftmann@27110
   107
haftmann@27110
   108
lemma iSuc_number_of: "iSuc (number_of k) = Fin (Suc (number_of k))"
hoelzl@43919
   109
  by (simp add: iSuc_Fin number_of_enat_def)
oheimb@11351
   110
oheimb@11351
   111
lemma iSuc_Infty [simp]: "iSuc \<infinity> = \<infinity>"
haftmann@27110
   112
  by (simp add: iSuc_def)
oheimb@11351
   113
oheimb@11351
   114
lemma iSuc_ne_0 [simp]: "iSuc n \<noteq> 0"
hoelzl@43919
   115
  by (simp add: iSuc_def zero_enat_def split: enat.splits)
haftmann@27110
   116
haftmann@27110
   117
lemma zero_ne_iSuc [simp]: "0 \<noteq> iSuc n"
haftmann@27110
   118
  by (rule iSuc_ne_0 [symmetric])
oheimb@11351
   119
haftmann@27110
   120
lemma iSuc_inject [simp]: "iSuc m = iSuc n \<longleftrightarrow> m = n"
hoelzl@43919
   121
  by (simp add: iSuc_def split: enat.splits)
haftmann@27110
   122
hoelzl@43919
   123
lemma number_of_enat_inject [simp]:
hoelzl@43919
   124
  "(number_of k \<Colon> enat) = number_of l \<longleftrightarrow> (number_of k \<Colon> nat) = number_of l"
hoelzl@43919
   125
  by (simp add: number_of_enat_def)
oheimb@11351
   126
oheimb@11351
   127
haftmann@27110
   128
subsection {* Addition *}
haftmann@27110
   129
hoelzl@43919
   130
instantiation enat :: comm_monoid_add
haftmann@27110
   131
begin
haftmann@27110
   132
blanchet@38167
   133
definition [nitpick_simp]:
haftmann@37765
   134
  "m + n = (case m of \<infinity> \<Rightarrow> \<infinity> | Fin m \<Rightarrow> (case n of \<infinity> \<Rightarrow> \<infinity> | Fin n \<Rightarrow> Fin (m + n)))"
oheimb@11351
   135
hoelzl@43919
   136
lemma plus_enat_simps [simp, code]:
haftmann@27110
   137
  "Fin m + Fin n = Fin (m + n)"
haftmann@27110
   138
  "\<infinity> + q = \<infinity>"
haftmann@27110
   139
  "q + \<infinity> = \<infinity>"
hoelzl@43919
   140
  by (simp_all add: plus_enat_def split: enat.splits)
haftmann@27110
   141
haftmann@27110
   142
instance proof
hoelzl@43919
   143
  fix n m q :: enat
haftmann@27110
   144
  show "n + m + q = n + (m + q)"
haftmann@27110
   145
    by (cases n, auto, cases m, auto, cases q, auto)
haftmann@27110
   146
  show "n + m = m + n"
haftmann@27110
   147
    by (cases n, auto, cases m, auto)
haftmann@27110
   148
  show "0 + n = n"
hoelzl@43919
   149
    by (cases n) (simp_all add: zero_enat_def)
huffman@26089
   150
qed
huffman@26089
   151
haftmann@27110
   152
end
oheimb@11351
   153
hoelzl@43919
   154
lemma plus_enat_0 [simp]:
hoelzl@43919
   155
  "0 + (q\<Colon>enat) = q"
hoelzl@43919
   156
  "(q\<Colon>enat) + 0 = q"
hoelzl@43919
   157
  by (simp_all add: plus_enat_def zero_enat_def split: enat.splits)
oheimb@11351
   158
hoelzl@43919
   159
lemma plus_enat_number [simp]:
hoelzl@43919
   160
  "(number_of k \<Colon> enat) + number_of l = (if k < Int.Pls then number_of l
huffman@29012
   161
    else if l < Int.Pls then number_of k else number_of (k + l))"
hoelzl@43919
   162
  unfolding number_of_enat_def plus_enat_simps nat_arith(1) if_distrib [symmetric, of _ Fin] ..
oheimb@11351
   163
haftmann@27110
   164
lemma iSuc_number [simp]:
haftmann@27110
   165
  "iSuc (number_of k) = (if neg (number_of k \<Colon> int) then 1 else number_of (Int.succ k))"
haftmann@27110
   166
  unfolding iSuc_number_of
hoelzl@43919
   167
  unfolding one_enat_def number_of_enat_def Suc_nat_number_of if_distrib [symmetric] ..
oheimb@11351
   168
haftmann@27110
   169
lemma iSuc_plus_1:
haftmann@27110
   170
  "iSuc n = n + 1"
hoelzl@43919
   171
  by (cases n) (simp_all add: iSuc_Fin one_enat_def)
haftmann@27110
   172
  
haftmann@27110
   173
lemma plus_1_iSuc:
haftmann@27110
   174
  "1 + q = iSuc q"
haftmann@27110
   175
  "q + 1 = iSuc q"
nipkow@41853
   176
by (simp_all add: iSuc_plus_1 add_ac)
nipkow@41853
   177
nipkow@41853
   178
lemma iadd_Suc: "iSuc m + n = iSuc (m + n)"
nipkow@41853
   179
by (simp_all add: iSuc_plus_1 add_ac)
oheimb@11351
   180
nipkow@41853
   181
lemma iadd_Suc_right: "m + iSuc n = iSuc (m + n)"
nipkow@41853
   182
by (simp only: add_commute[of m] iadd_Suc)
nipkow@41853
   183
hoelzl@43919
   184
lemma iadd_is_0: "(m + n = (0::enat)) = (m = 0 \<and> n = 0)"
hoelzl@43919
   185
by (cases m, cases n, simp_all add: zero_enat_def)
oheimb@11351
   186
huffman@29014
   187
subsection {* Multiplication *}
huffman@29014
   188
hoelzl@43919
   189
instantiation enat :: comm_semiring_1
huffman@29014
   190
begin
huffman@29014
   191
hoelzl@43919
   192
definition times_enat_def [nitpick_simp]:
huffman@29014
   193
  "m * n = (case m of \<infinity> \<Rightarrow> if n = 0 then 0 else \<infinity> | Fin m \<Rightarrow>
huffman@29014
   194
    (case n of \<infinity> \<Rightarrow> if m = 0 then 0 else \<infinity> | Fin n \<Rightarrow> Fin (m * n)))"
huffman@29014
   195
hoelzl@43919
   196
lemma times_enat_simps [simp, code]:
huffman@29014
   197
  "Fin m * Fin n = Fin (m * n)"
huffman@29014
   198
  "\<infinity> * \<infinity> = \<infinity>"
huffman@29014
   199
  "\<infinity> * Fin n = (if n = 0 then 0 else \<infinity>)"
huffman@29014
   200
  "Fin m * \<infinity> = (if m = 0 then 0 else \<infinity>)"
hoelzl@43919
   201
  unfolding times_enat_def zero_enat_def
hoelzl@43919
   202
  by (simp_all split: enat.split)
huffman@29014
   203
huffman@29014
   204
instance proof
hoelzl@43919
   205
  fix a b c :: enat
huffman@29014
   206
  show "(a * b) * c = a * (b * c)"
hoelzl@43919
   207
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   208
    by (simp split: enat.split)
huffman@29014
   209
  show "a * b = b * a"
hoelzl@43919
   210
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   211
    by (simp split: enat.split)
huffman@29014
   212
  show "1 * a = a"
hoelzl@43919
   213
    unfolding times_enat_def zero_enat_def one_enat_def
hoelzl@43919
   214
    by (simp split: enat.split)
huffman@29014
   215
  show "(a + b) * c = a * c + b * c"
hoelzl@43919
   216
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   217
    by (simp split: enat.split add: left_distrib)
huffman@29014
   218
  show "0 * a = 0"
hoelzl@43919
   219
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   220
    by (simp split: enat.split)
huffman@29014
   221
  show "a * 0 = 0"
hoelzl@43919
   222
    unfolding times_enat_def zero_enat_def
hoelzl@43919
   223
    by (simp split: enat.split)
hoelzl@43919
   224
  show "(0::enat) \<noteq> 1"
hoelzl@43919
   225
    unfolding zero_enat_def one_enat_def
huffman@29014
   226
    by simp
huffman@29014
   227
qed
huffman@29014
   228
huffman@29014
   229
end
huffman@29014
   230
huffman@29014
   231
lemma mult_iSuc: "iSuc m * n = n + m * n"
nipkow@29667
   232
  unfolding iSuc_plus_1 by (simp add: algebra_simps)
huffman@29014
   233
huffman@29014
   234
lemma mult_iSuc_right: "m * iSuc n = m + m * n"
nipkow@29667
   235
  unfolding iSuc_plus_1 by (simp add: algebra_simps)
huffman@29014
   236
huffman@29023
   237
lemma of_nat_eq_Fin: "of_nat n = Fin n"
huffman@29023
   238
  apply (induct n)
huffman@29023
   239
  apply (simp add: Fin_0)
huffman@29023
   240
  apply (simp add: plus_1_iSuc iSuc_Fin)
huffman@29023
   241
  done
huffman@29023
   242
hoelzl@43919
   243
instance enat :: number_semiring
huffman@43532
   244
proof
hoelzl@43919
   245
  fix n show "number_of (int n) = (of_nat n :: enat)"
hoelzl@43919
   246
    unfolding number_of_enat_def number_of_int of_nat_id of_nat_eq_Fin ..
huffman@43532
   247
qed
huffman@43532
   248
hoelzl@43919
   249
instance enat :: semiring_char_0 proof
haftmann@38621
   250
  have "inj Fin" by (rule injI) simp
hoelzl@43919
   251
  then show "inj (\<lambda>n. of_nat n :: enat)" by (simp add: of_nat_eq_Fin)
haftmann@38621
   252
qed
huffman@29023
   253
hoelzl@43919
   254
lemma imult_is_0[simp]: "((m::enat) * n = 0) = (m = 0 \<or> n = 0)"
hoelzl@43919
   255
by(auto simp add: times_enat_def zero_enat_def split: enat.split)
nipkow@41853
   256
hoelzl@43919
   257
lemma imult_is_Infty: "((a::enat) * b = \<infinity>) = (a = \<infinity> \<and> b \<noteq> 0 \<or> b = \<infinity> \<and> a \<noteq> 0)"
hoelzl@43919
   258
by(auto simp add: times_enat_def zero_enat_def split: enat.split)
nipkow@41853
   259
nipkow@41853
   260
nipkow@41853
   261
subsection {* Subtraction *}
nipkow@41853
   262
hoelzl@43919
   263
instantiation enat :: minus
nipkow@41853
   264
begin
nipkow@41853
   265
hoelzl@43919
   266
definition diff_enat_def:
nipkow@41853
   267
"a - b = (case a of (Fin x) \<Rightarrow> (case b of (Fin y) \<Rightarrow> Fin (x - y) | \<infinity> \<Rightarrow> 0)
nipkow@41853
   268
          | \<infinity> \<Rightarrow> \<infinity>)"
nipkow@41853
   269
nipkow@41853
   270
instance ..
nipkow@41853
   271
nipkow@41853
   272
end
nipkow@41853
   273
nipkow@41853
   274
lemma idiff_Fin_Fin[simp,code]: "Fin a - Fin b = Fin (a - b)"
hoelzl@43919
   275
by(simp add: diff_enat_def)
nipkow@41853
   276
nipkow@41853
   277
lemma idiff_Infty[simp,code]: "\<infinity> - n = \<infinity>"
hoelzl@43919
   278
by(simp add: diff_enat_def)
nipkow@41853
   279
nipkow@41853
   280
lemma idiff_Infty_right[simp,code]: "Fin a - \<infinity> = 0"
hoelzl@43919
   281
by(simp add: diff_enat_def)
nipkow@41853
   282
hoelzl@43919
   283
lemma idiff_0[simp]: "(0::enat) - n = 0"
hoelzl@43919
   284
by (cases n, simp_all add: zero_enat_def)
nipkow@41853
   285
hoelzl@43919
   286
lemmas idiff_Fin_0[simp] = idiff_0[unfolded zero_enat_def]
nipkow@41853
   287
hoelzl@43919
   288
lemma idiff_0_right[simp]: "(n::enat) - 0 = n"
hoelzl@43919
   289
by (cases n) (simp_all add: zero_enat_def)
nipkow@41853
   290
hoelzl@43919
   291
lemmas idiff_Fin_0_right[simp] = idiff_0_right[unfolded zero_enat_def]
nipkow@41853
   292
hoelzl@43919
   293
lemma idiff_self[simp]: "n \<noteq> \<infinity> \<Longrightarrow> (n::enat) - n = 0"
hoelzl@43919
   294
by(auto simp: zero_enat_def)
nipkow@41853
   295
nipkow@41855
   296
lemma iSuc_minus_iSuc [simp]: "iSuc n - iSuc m = n - m"
hoelzl@43919
   297
by(simp add: iSuc_def split: enat.split)
nipkow@41855
   298
nipkow@41855
   299
lemma iSuc_minus_1 [simp]: "iSuc n - 1 = n"
hoelzl@43919
   300
by(simp add: one_enat_def iSuc_Fin[symmetric] zero_enat_def[symmetric])
nipkow@41855
   301
hoelzl@43919
   302
(*lemmas idiff_self_eq_0_Fin = idiff_self_eq_0[unfolded zero_enat_def]*)
nipkow@41853
   303
huffman@29014
   304
haftmann@27110
   305
subsection {* Ordering *}
haftmann@27110
   306
hoelzl@43919
   307
instantiation enat :: linordered_ab_semigroup_add
haftmann@27110
   308
begin
oheimb@11351
   309
blanchet@38167
   310
definition [nitpick_simp]:
haftmann@37765
   311
  "m \<le> n = (case n of Fin n1 \<Rightarrow> (case m of Fin m1 \<Rightarrow> m1 \<le> n1 | \<infinity> \<Rightarrow> False)
haftmann@27110
   312
    | \<infinity> \<Rightarrow> True)"
oheimb@11351
   313
blanchet@38167
   314
definition [nitpick_simp]:
haftmann@37765
   315
  "m < n = (case m of Fin m1 \<Rightarrow> (case n of Fin n1 \<Rightarrow> m1 < n1 | \<infinity> \<Rightarrow> True)
haftmann@27110
   316
    | \<infinity> \<Rightarrow> False)"
oheimb@11351
   317
hoelzl@43919
   318
lemma enat_ord_simps [simp]:
haftmann@27110
   319
  "Fin m \<le> Fin n \<longleftrightarrow> m \<le> n"
haftmann@27110
   320
  "Fin m < Fin n \<longleftrightarrow> m < n"
haftmann@27110
   321
  "q \<le> \<infinity>"
haftmann@27110
   322
  "q < \<infinity> \<longleftrightarrow> q \<noteq> \<infinity>"
haftmann@27110
   323
  "\<infinity> \<le> q \<longleftrightarrow> q = \<infinity>"
haftmann@27110
   324
  "\<infinity> < q \<longleftrightarrow> False"
hoelzl@43919
   325
  by (simp_all add: less_eq_enat_def less_enat_def split: enat.splits)
oheimb@11351
   326
hoelzl@43919
   327
lemma enat_ord_code [code]:
haftmann@27110
   328
  "Fin m \<le> Fin n \<longleftrightarrow> m \<le> n"
haftmann@27110
   329
  "Fin m < Fin n \<longleftrightarrow> m < n"
haftmann@27110
   330
  "q \<le> \<infinity> \<longleftrightarrow> True"
haftmann@27110
   331
  "Fin m < \<infinity> \<longleftrightarrow> True"
haftmann@27110
   332
  "\<infinity> \<le> Fin n \<longleftrightarrow> False"
haftmann@27110
   333
  "\<infinity> < q \<longleftrightarrow> False"
haftmann@27110
   334
  by simp_all
oheimb@11351
   335
haftmann@27110
   336
instance by default
hoelzl@43919
   337
  (auto simp add: less_eq_enat_def less_enat_def plus_enat_def split: enat.splits)
oheimb@11351
   338
haftmann@27110
   339
end
haftmann@27110
   340
hoelzl@43919
   341
instance enat :: ordered_comm_semiring
huffman@29014
   342
proof
hoelzl@43919
   343
  fix a b c :: enat
huffman@29014
   344
  assume "a \<le> b" and "0 \<le> c"
huffman@29014
   345
  thus "c * a \<le> c * b"
hoelzl@43919
   346
    unfolding times_enat_def less_eq_enat_def zero_enat_def
hoelzl@43919
   347
    by (simp split: enat.splits)
huffman@29014
   348
qed
huffman@29014
   349
hoelzl@43919
   350
lemma enat_ord_number [simp]:
hoelzl@43919
   351
  "(number_of m \<Colon> enat) \<le> number_of n \<longleftrightarrow> (number_of m \<Colon> nat) \<le> number_of n"
hoelzl@43919
   352
  "(number_of m \<Colon> enat) < number_of n \<longleftrightarrow> (number_of m \<Colon> nat) < number_of n"
hoelzl@43919
   353
  by (simp_all add: number_of_enat_def)
oheimb@11351
   354
hoelzl@43919
   355
lemma i0_lb [simp]: "(0\<Colon>enat) \<le> n"
hoelzl@43919
   356
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
oheimb@11351
   357
hoelzl@43919
   358
lemma ile0_eq [simp]: "n \<le> (0\<Colon>enat) \<longleftrightarrow> n = 0"
hoelzl@43919
   359
by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
haftmann@27110
   360
haftmann@27110
   361
lemma Infty_ileE [elim!]: "\<infinity> \<le> Fin m \<Longrightarrow> R"
hoelzl@43919
   362
  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
oheimb@11351
   363
haftmann@27110
   364
lemma Infty_ilessE [elim!]: "\<infinity> < Fin m \<Longrightarrow> R"
haftmann@27110
   365
  by simp
oheimb@11351
   366
hoelzl@43919
   367
lemma not_iless0 [simp]: "\<not> n < (0\<Colon>enat)"
hoelzl@43919
   368
  by (simp add: zero_enat_def less_enat_def split: enat.splits)
haftmann@27110
   369
hoelzl@43919
   370
lemma i0_less [simp]: "(0\<Colon>enat) < n \<longleftrightarrow> n \<noteq> 0"
hoelzl@43919
   371
by (simp add: zero_enat_def less_enat_def split: enat.splits)
oheimb@11351
   372
haftmann@27110
   373
lemma iSuc_ile_mono [simp]: "iSuc n \<le> iSuc m \<longleftrightarrow> n \<le> m"
hoelzl@43919
   374
  by (simp add: iSuc_def less_eq_enat_def split: enat.splits)
haftmann@27110
   375
 
haftmann@27110
   376
lemma iSuc_mono [simp]: "iSuc n < iSuc m \<longleftrightarrow> n < m"
hoelzl@43919
   377
  by (simp add: iSuc_def less_enat_def split: enat.splits)
oheimb@11351
   378
haftmann@27110
   379
lemma ile_iSuc [simp]: "n \<le> iSuc n"
hoelzl@43919
   380
  by (simp add: iSuc_def less_eq_enat_def split: enat.splits)
oheimb@11351
   381
wenzelm@11355
   382
lemma not_iSuc_ilei0 [simp]: "\<not> iSuc n \<le> 0"
hoelzl@43919
   383
  by (simp add: zero_enat_def iSuc_def less_eq_enat_def split: enat.splits)
haftmann@27110
   384
haftmann@27110
   385
lemma i0_iless_iSuc [simp]: "0 < iSuc n"
hoelzl@43919
   386
  by (simp add: zero_enat_def iSuc_def less_enat_def split: enat.splits)
haftmann@27110
   387
nipkow@41853
   388
lemma iless_iSuc0[simp]: "(n < iSuc 0) = (n = 0)"
hoelzl@43919
   389
by (simp add: zero_enat_def iSuc_def less_enat_def split: enat.split)
nipkow@41853
   390
haftmann@27110
   391
lemma ileI1: "m < n \<Longrightarrow> iSuc m \<le> n"
hoelzl@43919
   392
  by (simp add: iSuc_def less_eq_enat_def less_enat_def split: enat.splits)
haftmann@27110
   393
haftmann@27110
   394
lemma Suc_ile_eq: "Fin (Suc m) \<le> n \<longleftrightarrow> Fin m < n"
haftmann@27110
   395
  by (cases n) auto
haftmann@27110
   396
haftmann@27110
   397
lemma iless_Suc_eq [simp]: "Fin m < iSuc n \<longleftrightarrow> Fin m \<le> n"
hoelzl@43919
   398
  by (auto simp add: iSuc_def less_enat_def split: enat.splits)
oheimb@11351
   399
hoelzl@43919
   400
lemma imult_Infty: "(0::enat) < n \<Longrightarrow> \<infinity> * n = \<infinity>"
hoelzl@43919
   401
by (simp add: zero_enat_def less_enat_def split: enat.splits)
nipkow@41853
   402
hoelzl@43919
   403
lemma imult_Infty_right: "(0::enat) < n \<Longrightarrow> n * \<infinity> = \<infinity>"
hoelzl@43919
   404
by (simp add: zero_enat_def less_enat_def split: enat.splits)
nipkow@41853
   405
hoelzl@43919
   406
lemma enat_0_less_mult_iff: "(0 < (m::enat) * n) = (0 < m \<and> 0 < n)"
nipkow@41853
   407
by (simp only: i0_less imult_is_0, simp)
nipkow@41853
   408
nipkow@41853
   409
lemma mono_iSuc: "mono iSuc"
nipkow@41853
   410
by(simp add: mono_def)
nipkow@41853
   411
nipkow@41853
   412
hoelzl@43919
   413
lemma min_enat_simps [simp]:
haftmann@27110
   414
  "min (Fin m) (Fin n) = Fin (min m n)"
haftmann@27110
   415
  "min q 0 = 0"
haftmann@27110
   416
  "min 0 q = 0"
haftmann@27110
   417
  "min q \<infinity> = q"
haftmann@27110
   418
  "min \<infinity> q = q"
haftmann@27110
   419
  by (auto simp add: min_def)
oheimb@11351
   420
hoelzl@43919
   421
lemma max_enat_simps [simp]:
haftmann@27110
   422
  "max (Fin m) (Fin n) = Fin (max m n)"
haftmann@27110
   423
  "max q 0 = q"
haftmann@27110
   424
  "max 0 q = q"
haftmann@27110
   425
  "max q \<infinity> = \<infinity>"
haftmann@27110
   426
  "max \<infinity> q = \<infinity>"
haftmann@27110
   427
  by (simp_all add: max_def)
haftmann@27110
   428
haftmann@27110
   429
lemma Fin_ile: "n \<le> Fin m \<Longrightarrow> \<exists>k. n = Fin k"
haftmann@27110
   430
  by (cases n) simp_all
haftmann@27110
   431
haftmann@27110
   432
lemma Fin_iless: "n < Fin m \<Longrightarrow> \<exists>k. n = Fin k"
haftmann@27110
   433
  by (cases n) simp_all
oheimb@11351
   434
oheimb@11351
   435
lemma chain_incr: "\<forall>i. \<exists>j. Y i < Y j ==> \<exists>j. Fin k < Y j"
nipkow@25134
   436
apply (induct_tac k)
nipkow@25134
   437
 apply (simp (no_asm) only: Fin_0)
haftmann@27110
   438
 apply (fast intro: le_less_trans [OF i0_lb])
nipkow@25134
   439
apply (erule exE)
nipkow@25134
   440
apply (drule spec)
nipkow@25134
   441
apply (erule exE)
nipkow@25134
   442
apply (drule ileI1)
nipkow@25134
   443
apply (rule iSuc_Fin [THEN subst])
nipkow@25134
   444
apply (rule exI)
haftmann@27110
   445
apply (erule (1) le_less_trans)
nipkow@25134
   446
done
oheimb@11351
   447
hoelzl@43919
   448
instantiation enat :: "{bot, top}"
haftmann@29337
   449
begin
haftmann@29337
   450
hoelzl@43919
   451
definition bot_enat :: enat where
hoelzl@43919
   452
  "bot_enat = 0"
haftmann@29337
   453
hoelzl@43919
   454
definition top_enat :: enat where
hoelzl@43919
   455
  "top_enat = \<infinity>"
haftmann@29337
   456
haftmann@29337
   457
instance proof
hoelzl@43919
   458
qed (simp_all add: bot_enat_def top_enat_def)
haftmann@29337
   459
haftmann@29337
   460
end
haftmann@29337
   461
noschinl@42993
   462
lemma finite_Fin_bounded:
noschinl@42993
   463
  assumes le_fin: "\<And>y. y \<in> A \<Longrightarrow> y \<le> Fin n"
noschinl@42993
   464
  shows "finite A"
noschinl@42993
   465
proof (rule finite_subset)
noschinl@42993
   466
  show "finite (Fin ` {..n})" by blast
noschinl@42993
   467
noschinl@42993
   468
  have "A \<subseteq> {..Fin n}" using le_fin by fastsimp
noschinl@42993
   469
  also have "\<dots> \<subseteq> Fin ` {..n}"
noschinl@42993
   470
    by (rule subsetI) (case_tac x, auto)
noschinl@42993
   471
  finally show "A \<subseteq> Fin ` {..n}" .
noschinl@42993
   472
qed
noschinl@42993
   473
huffman@26089
   474
haftmann@27110
   475
subsection {* Well-ordering *}
huffman@26089
   476
huffman@26089
   477
lemma less_FinE:
huffman@26089
   478
  "[| n < Fin m; !!k. n = Fin k ==> k < m ==> P |] ==> P"
huffman@26089
   479
by (induct n) auto
huffman@26089
   480
huffman@26089
   481
lemma less_InftyE:
huffman@26089
   482
  "[| n < Infty; !!k. n = Fin k ==> P |] ==> P"
huffman@26089
   483
by (induct n) auto
huffman@26089
   484
hoelzl@43919
   485
lemma enat_less_induct:
hoelzl@43919
   486
  assumes prem: "!!n. \<forall>m::enat. m < n --> P m ==> P n" shows "P n"
huffman@26089
   487
proof -
huffman@26089
   488
  have P_Fin: "!!k. P (Fin k)"
huffman@26089
   489
    apply (rule nat_less_induct)
huffman@26089
   490
    apply (rule prem, clarify)
huffman@26089
   491
    apply (erule less_FinE, simp)
huffman@26089
   492
    done
huffman@26089
   493
  show ?thesis
huffman@26089
   494
  proof (induct n)
huffman@26089
   495
    fix nat
huffman@26089
   496
    show "P (Fin nat)" by (rule P_Fin)
huffman@26089
   497
  next
huffman@26089
   498
    show "P Infty"
huffman@26089
   499
      apply (rule prem, clarify)
huffman@26089
   500
      apply (erule less_InftyE)
huffman@26089
   501
      apply (simp add: P_Fin)
huffman@26089
   502
      done
huffman@26089
   503
  qed
huffman@26089
   504
qed
huffman@26089
   505
hoelzl@43919
   506
instance enat :: wellorder
huffman@26089
   507
proof
haftmann@27823
   508
  fix P and n
hoelzl@43919
   509
  assume hyp: "(\<And>n\<Colon>enat. (\<And>m\<Colon>enat. m < n \<Longrightarrow> P m) \<Longrightarrow> P n)"
hoelzl@43919
   510
  show "P n" by (blast intro: enat_less_induct hyp)
huffman@26089
   511
qed
huffman@26089
   512
noschinl@42993
   513
subsection {* Complete Lattice *}
noschinl@42993
   514
hoelzl@43919
   515
instantiation enat :: complete_lattice
noschinl@42993
   516
begin
noschinl@42993
   517
hoelzl@43919
   518
definition inf_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
hoelzl@43919
   519
  "inf_enat \<equiv> min"
noschinl@42993
   520
hoelzl@43919
   521
definition sup_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
hoelzl@43919
   522
  "sup_enat \<equiv> max"
noschinl@42993
   523
hoelzl@43919
   524
definition Inf_enat :: "enat set \<Rightarrow> enat" where
hoelzl@43919
   525
  "Inf_enat A \<equiv> if A = {} then \<infinity> else (LEAST x. x \<in> A)"
noschinl@42993
   526
hoelzl@43919
   527
definition Sup_enat :: "enat set \<Rightarrow> enat" where
hoelzl@43919
   528
  "Sup_enat A \<equiv> if A = {} then 0
noschinl@42993
   529
    else if finite A then Max A
noschinl@42993
   530
                     else \<infinity>"
noschinl@42993
   531
instance proof
hoelzl@43919
   532
  fix x :: "enat" and A :: "enat set"
noschinl@42993
   533
  { assume "x \<in> A" then show "Inf A \<le> x"
hoelzl@43919
   534
      unfolding Inf_enat_def by (auto intro: Least_le) }
noschinl@42993
   535
  { assume "\<And>y. y \<in> A \<Longrightarrow> x \<le> y" then show "x \<le> Inf A"
hoelzl@43919
   536
      unfolding Inf_enat_def
noschinl@42993
   537
      by (cases "A = {}") (auto intro: LeastI2_ex) }
noschinl@42993
   538
  { assume "x \<in> A" then show "x \<le> Sup A"
hoelzl@43919
   539
      unfolding Sup_enat_def by (cases "finite A") auto }
noschinl@42993
   540
  { assume "\<And>y. y \<in> A \<Longrightarrow> y \<le> x" then show "Sup A \<le> x"
hoelzl@43919
   541
      unfolding Sup_enat_def using finite_Fin_bounded by auto }
hoelzl@43919
   542
qed (simp_all add: inf_enat_def sup_enat_def)
noschinl@42993
   543
end
noschinl@42993
   544
haftmann@27110
   545
haftmann@27110
   546
subsection {* Traditional theorem names *}
haftmann@27110
   547
hoelzl@43919
   548
lemmas enat_defs = zero_enat_def one_enat_def number_of_enat_def iSuc_def
hoelzl@43919
   549
  plus_enat_def less_eq_enat_def less_enat_def
haftmann@27110
   550
oheimb@11351
   551
end