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