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