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