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