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