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