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