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