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