src/HOL/Library/Nat_Infinity.thy
author haftmann
Fri Aug 27 19:34:23 2010 +0200 (2010-08-27 ago)
changeset 38857 97775f3e8722
parent 38621 d6cb7e625d75
child 41853 258a489c24b2
permissions -rw-r--r--
renamed class/constant eq to equal; tuned some instantiations
     1 (*  Title:      HOL/Library/Nat_Infinity.thy
     2     Author:     David von Oheimb, TU Muenchen;  Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Natural numbers with infinity *}
     6 
     7 theory Nat_Infinity
     8 imports Main
     9 begin
    10 
    11 subsection {* Type definition *}
    12 
    13 text {*
    14   We extend the standard natural numbers by a special value indicating
    15   infinity.
    16 *}
    17 
    18 datatype inat = Fin nat | Infty
    19 
    20 notation (xsymbols)
    21   Infty  ("\<infinity>")
    22 
    23 notation (HTML output)
    24   Infty  ("\<infinity>")
    25 
    26 
    27 lemma not_Infty_eq[iff]: "(x ~= Infty) = (EX i. x = Fin i)"
    28 by (cases x) auto
    29 
    30 lemma not_Fin_eq [iff]: "(ALL y. x ~= Fin y) = (x = Infty)"
    31 by (cases x) auto
    32 
    33 
    34 subsection {* Constructors and numbers *}
    35 
    36 instantiation inat :: "{zero, one, number}"
    37 begin
    38 
    39 definition
    40   "0 = Fin 0"
    41 
    42 definition
    43   [code_unfold]: "1 = Fin 1"
    44 
    45 definition
    46   [code_unfold, code del]: "number_of k = Fin (number_of k)"
    47 
    48 instance ..
    49 
    50 end
    51 
    52 definition iSuc :: "inat \<Rightarrow> inat" where
    53   "iSuc i = (case i of Fin n \<Rightarrow> Fin (Suc n) | \<infinity> \<Rightarrow> \<infinity>)"
    54 
    55 lemma Fin_0: "Fin 0 = 0"
    56   by (simp add: zero_inat_def)
    57 
    58 lemma Fin_1: "Fin 1 = 1"
    59   by (simp add: one_inat_def)
    60 
    61 lemma Fin_number: "Fin (number_of k) = number_of k"
    62   by (simp add: number_of_inat_def)
    63 
    64 lemma one_iSuc: "1 = iSuc 0"
    65   by (simp add: zero_inat_def one_inat_def iSuc_def)
    66 
    67 lemma Infty_ne_i0 [simp]: "\<infinity> \<noteq> 0"
    68   by (simp add: zero_inat_def)
    69 
    70 lemma i0_ne_Infty [simp]: "0 \<noteq> \<infinity>"
    71   by (simp add: zero_inat_def)
    72 
    73 lemma zero_inat_eq [simp]:
    74   "number_of k = (0\<Colon>inat) \<longleftrightarrow> number_of k = (0\<Colon>nat)"
    75   "(0\<Colon>inat) = number_of k \<longleftrightarrow> number_of k = (0\<Colon>nat)"
    76   unfolding zero_inat_def number_of_inat_def by simp_all
    77 
    78 lemma one_inat_eq [simp]:
    79   "number_of k = (1\<Colon>inat) \<longleftrightarrow> number_of k = (1\<Colon>nat)"
    80   "(1\<Colon>inat) = number_of k \<longleftrightarrow> number_of k = (1\<Colon>nat)"
    81   unfolding one_inat_def number_of_inat_def by simp_all
    82 
    83 lemma zero_one_inat_neq [simp]:
    84   "\<not> 0 = (1\<Colon>inat)"
    85   "\<not> 1 = (0\<Colon>inat)"
    86   unfolding zero_inat_def one_inat_def by simp_all
    87 
    88 lemma Infty_ne_i1 [simp]: "\<infinity> \<noteq> 1"
    89   by (simp add: one_inat_def)
    90 
    91 lemma i1_ne_Infty [simp]: "1 \<noteq> \<infinity>"
    92   by (simp add: one_inat_def)
    93 
    94 lemma Infty_ne_number [simp]: "\<infinity> \<noteq> number_of k"
    95   by (simp add: number_of_inat_def)
    96 
    97 lemma number_ne_Infty [simp]: "number_of k \<noteq> \<infinity>"
    98   by (simp add: number_of_inat_def)
    99 
   100 lemma iSuc_Fin: "iSuc (Fin n) = Fin (Suc n)"
   101   by (simp add: iSuc_def)
   102 
   103 lemma iSuc_number_of: "iSuc (number_of k) = Fin (Suc (number_of k))"
   104   by (simp add: iSuc_Fin number_of_inat_def)
   105 
   106 lemma iSuc_Infty [simp]: "iSuc \<infinity> = \<infinity>"
   107   by (simp add: iSuc_def)
   108 
   109 lemma iSuc_ne_0 [simp]: "iSuc n \<noteq> 0"
   110   by (simp add: iSuc_def zero_inat_def split: inat.splits)
   111 
   112 lemma zero_ne_iSuc [simp]: "0 \<noteq> iSuc n"
   113   by (rule iSuc_ne_0 [symmetric])
   114 
   115 lemma iSuc_inject [simp]: "iSuc m = iSuc n \<longleftrightarrow> m = n"
   116   by (simp add: iSuc_def split: inat.splits)
   117 
   118 lemma number_of_inat_inject [simp]:
   119   "(number_of k \<Colon> inat) = number_of l \<longleftrightarrow> (number_of k \<Colon> nat) = number_of l"
   120   by (simp add: number_of_inat_def)
   121 
   122 
   123 subsection {* Addition *}
   124 
   125 instantiation inat :: comm_monoid_add
   126 begin
   127 
   128 definition [nitpick_simp]:
   129   "m + n = (case m of \<infinity> \<Rightarrow> \<infinity> | Fin m \<Rightarrow> (case n of \<infinity> \<Rightarrow> \<infinity> | Fin n \<Rightarrow> Fin (m + n)))"
   130 
   131 lemma plus_inat_simps [simp, code]:
   132   "Fin m + Fin n = Fin (m + n)"
   133   "\<infinity> + q = \<infinity>"
   134   "q + \<infinity> = \<infinity>"
   135   by (simp_all add: plus_inat_def split: inat.splits)
   136 
   137 instance proof
   138   fix n m q :: inat
   139   show "n + m + q = n + (m + q)"
   140     by (cases n, auto, cases m, auto, cases q, auto)
   141   show "n + m = m + n"
   142     by (cases n, auto, cases m, auto)
   143   show "0 + n = n"
   144     by (cases n) (simp_all add: zero_inat_def)
   145 qed
   146 
   147 end
   148 
   149 lemma plus_inat_0 [simp]:
   150   "0 + (q\<Colon>inat) = q"
   151   "(q\<Colon>inat) + 0 = q"
   152   by (simp_all add: plus_inat_def zero_inat_def split: inat.splits)
   153 
   154 lemma plus_inat_number [simp]:
   155   "(number_of k \<Colon> inat) + number_of l = (if k < Int.Pls then number_of l
   156     else if l < Int.Pls then number_of k else number_of (k + l))"
   157   unfolding number_of_inat_def plus_inat_simps nat_arith(1) if_distrib [symmetric, of _ Fin] ..
   158 
   159 lemma iSuc_number [simp]:
   160   "iSuc (number_of k) = (if neg (number_of k \<Colon> int) then 1 else number_of (Int.succ k))"
   161   unfolding iSuc_number_of
   162   unfolding one_inat_def number_of_inat_def Suc_nat_number_of if_distrib [symmetric] ..
   163 
   164 lemma iSuc_plus_1:
   165   "iSuc n = n + 1"
   166   by (cases n) (simp_all add: iSuc_Fin one_inat_def)
   167   
   168 lemma plus_1_iSuc:
   169   "1 + q = iSuc q"
   170   "q + 1 = iSuc q"
   171   unfolding iSuc_plus_1 by (simp_all add: add_ac)
   172 
   173 
   174 subsection {* Multiplication *}
   175 
   176 instantiation inat :: comm_semiring_1
   177 begin
   178 
   179 definition times_inat_def [nitpick_simp]:
   180   "m * n = (case m of \<infinity> \<Rightarrow> if n = 0 then 0 else \<infinity> | Fin m \<Rightarrow>
   181     (case n of \<infinity> \<Rightarrow> if m = 0 then 0 else \<infinity> | Fin n \<Rightarrow> Fin (m * n)))"
   182 
   183 lemma times_inat_simps [simp, code]:
   184   "Fin m * Fin n = Fin (m * n)"
   185   "\<infinity> * \<infinity> = \<infinity>"
   186   "\<infinity> * Fin n = (if n = 0 then 0 else \<infinity>)"
   187   "Fin m * \<infinity> = (if m = 0 then 0 else \<infinity>)"
   188   unfolding times_inat_def zero_inat_def
   189   by (simp_all split: inat.split)
   190 
   191 instance proof
   192   fix a b c :: inat
   193   show "(a * b) * c = a * (b * c)"
   194     unfolding times_inat_def zero_inat_def
   195     by (simp split: inat.split)
   196   show "a * b = b * a"
   197     unfolding times_inat_def zero_inat_def
   198     by (simp split: inat.split)
   199   show "1 * a = a"
   200     unfolding times_inat_def zero_inat_def one_inat_def
   201     by (simp split: inat.split)
   202   show "(a + b) * c = a * c + b * c"
   203     unfolding times_inat_def zero_inat_def
   204     by (simp split: inat.split add: left_distrib)
   205   show "0 * a = 0"
   206     unfolding times_inat_def zero_inat_def
   207     by (simp split: inat.split)
   208   show "a * 0 = 0"
   209     unfolding times_inat_def zero_inat_def
   210     by (simp split: inat.split)
   211   show "(0::inat) \<noteq> 1"
   212     unfolding zero_inat_def one_inat_def
   213     by simp
   214 qed
   215 
   216 end
   217 
   218 lemma mult_iSuc: "iSuc m * n = n + m * n"
   219   unfolding iSuc_plus_1 by (simp add: algebra_simps)
   220 
   221 lemma mult_iSuc_right: "m * iSuc n = m + m * n"
   222   unfolding iSuc_plus_1 by (simp add: algebra_simps)
   223 
   224 lemma of_nat_eq_Fin: "of_nat n = Fin n"
   225   apply (induct n)
   226   apply (simp add: Fin_0)
   227   apply (simp add: plus_1_iSuc iSuc_Fin)
   228   done
   229 
   230 instance inat :: semiring_char_0 proof
   231   have "inj Fin" by (rule injI) simp
   232   then show "inj (\<lambda>n. of_nat n :: inat)" by (simp add: of_nat_eq_Fin)
   233 qed
   234 
   235 
   236 subsection {* Ordering *}
   237 
   238 instantiation inat :: linordered_ab_semigroup_add
   239 begin
   240 
   241 definition [nitpick_simp]:
   242   "m \<le> n = (case n of Fin n1 \<Rightarrow> (case m of Fin m1 \<Rightarrow> m1 \<le> n1 | \<infinity> \<Rightarrow> False)
   243     | \<infinity> \<Rightarrow> True)"
   244 
   245 definition [nitpick_simp]:
   246   "m < n = (case m of Fin m1 \<Rightarrow> (case n of Fin n1 \<Rightarrow> m1 < n1 | \<infinity> \<Rightarrow> True)
   247     | \<infinity> \<Rightarrow> False)"
   248 
   249 lemma inat_ord_simps [simp]:
   250   "Fin m \<le> Fin n \<longleftrightarrow> m \<le> n"
   251   "Fin m < Fin n \<longleftrightarrow> m < n"
   252   "q \<le> \<infinity>"
   253   "q < \<infinity> \<longleftrightarrow> q \<noteq> \<infinity>"
   254   "\<infinity> \<le> q \<longleftrightarrow> q = \<infinity>"
   255   "\<infinity> < q \<longleftrightarrow> False"
   256   by (simp_all add: less_eq_inat_def less_inat_def split: inat.splits)
   257 
   258 lemma inat_ord_code [code]:
   259   "Fin m \<le> Fin n \<longleftrightarrow> m \<le> n"
   260   "Fin m < Fin n \<longleftrightarrow> m < n"
   261   "q \<le> \<infinity> \<longleftrightarrow> True"
   262   "Fin m < \<infinity> \<longleftrightarrow> True"
   263   "\<infinity> \<le> Fin n \<longleftrightarrow> False"
   264   "\<infinity> < q \<longleftrightarrow> False"
   265   by simp_all
   266 
   267 instance by default
   268   (auto simp add: less_eq_inat_def less_inat_def plus_inat_def split: inat.splits)
   269 
   270 end
   271 
   272 instance inat :: ordered_comm_semiring
   273 proof
   274   fix a b c :: inat
   275   assume "a \<le> b" and "0 \<le> c"
   276   thus "c * a \<le> c * b"
   277     unfolding times_inat_def less_eq_inat_def zero_inat_def
   278     by (simp split: inat.splits)
   279 qed
   280 
   281 lemma inat_ord_number [simp]:
   282   "(number_of m \<Colon> inat) \<le> number_of n \<longleftrightarrow> (number_of m \<Colon> nat) \<le> number_of n"
   283   "(number_of m \<Colon> inat) < number_of n \<longleftrightarrow> (number_of m \<Colon> nat) < number_of n"
   284   by (simp_all add: number_of_inat_def)
   285 
   286 lemma i0_lb [simp]: "(0\<Colon>inat) \<le> n"
   287   by (simp add: zero_inat_def less_eq_inat_def split: inat.splits)
   288 
   289 lemma i0_neq [simp]: "n \<le> (0\<Colon>inat) \<longleftrightarrow> n = 0"
   290   by (simp add: zero_inat_def less_eq_inat_def split: inat.splits)
   291 
   292 lemma Infty_ileE [elim!]: "\<infinity> \<le> Fin m \<Longrightarrow> R"
   293   by (simp add: zero_inat_def less_eq_inat_def split: inat.splits)
   294 
   295 lemma Infty_ilessE [elim!]: "\<infinity> < Fin m \<Longrightarrow> R"
   296   by simp
   297 
   298 lemma not_ilessi0 [simp]: "\<not> n < (0\<Colon>inat)"
   299   by (simp add: zero_inat_def less_inat_def split: inat.splits)
   300 
   301 lemma i0_eq [simp]: "(0\<Colon>inat) < n \<longleftrightarrow> n \<noteq> 0"
   302   by (simp add: zero_inat_def less_inat_def split: inat.splits)
   303 
   304 lemma iSuc_ile_mono [simp]: "iSuc n \<le> iSuc m \<longleftrightarrow> n \<le> m"
   305   by (simp add: iSuc_def less_eq_inat_def split: inat.splits)
   306  
   307 lemma iSuc_mono [simp]: "iSuc n < iSuc m \<longleftrightarrow> n < m"
   308   by (simp add: iSuc_def less_inat_def split: inat.splits)
   309 
   310 lemma ile_iSuc [simp]: "n \<le> iSuc n"
   311   by (simp add: iSuc_def less_eq_inat_def split: inat.splits)
   312 
   313 lemma not_iSuc_ilei0 [simp]: "\<not> iSuc n \<le> 0"
   314   by (simp add: zero_inat_def iSuc_def less_eq_inat_def split: inat.splits)
   315 
   316 lemma i0_iless_iSuc [simp]: "0 < iSuc n"
   317   by (simp add: zero_inat_def iSuc_def less_inat_def split: inat.splits)
   318 
   319 lemma ileI1: "m < n \<Longrightarrow> iSuc m \<le> n"
   320   by (simp add: iSuc_def less_eq_inat_def less_inat_def split: inat.splits)
   321 
   322 lemma Suc_ile_eq: "Fin (Suc m) \<le> n \<longleftrightarrow> Fin m < n"
   323   by (cases n) auto
   324 
   325 lemma iless_Suc_eq [simp]: "Fin m < iSuc n \<longleftrightarrow> Fin m \<le> n"
   326   by (auto simp add: iSuc_def less_inat_def split: inat.splits)
   327 
   328 lemma min_inat_simps [simp]:
   329   "min (Fin m) (Fin n) = Fin (min m n)"
   330   "min q 0 = 0"
   331   "min 0 q = 0"
   332   "min q \<infinity> = q"
   333   "min \<infinity> q = q"
   334   by (auto simp add: min_def)
   335 
   336 lemma max_inat_simps [simp]:
   337   "max (Fin m) (Fin n) = Fin (max m n)"
   338   "max q 0 = q"
   339   "max 0 q = q"
   340   "max q \<infinity> = \<infinity>"
   341   "max \<infinity> q = \<infinity>"
   342   by (simp_all add: max_def)
   343 
   344 lemma Fin_ile: "n \<le> Fin m \<Longrightarrow> \<exists>k. n = Fin k"
   345   by (cases n) simp_all
   346 
   347 lemma Fin_iless: "n < Fin m \<Longrightarrow> \<exists>k. n = Fin k"
   348   by (cases n) simp_all
   349 
   350 lemma chain_incr: "\<forall>i. \<exists>j. Y i < Y j ==> \<exists>j. Fin k < Y j"
   351 apply (induct_tac k)
   352  apply (simp (no_asm) only: Fin_0)
   353  apply (fast intro: le_less_trans [OF i0_lb])
   354 apply (erule exE)
   355 apply (drule spec)
   356 apply (erule exE)
   357 apply (drule ileI1)
   358 apply (rule iSuc_Fin [THEN subst])
   359 apply (rule exI)
   360 apply (erule (1) le_less_trans)
   361 done
   362 
   363 instantiation inat :: "{bot, top}"
   364 begin
   365 
   366 definition bot_inat :: inat where
   367   "bot_inat = 0"
   368 
   369 definition top_inat :: inat where
   370   "top_inat = \<infinity>"
   371 
   372 instance proof
   373 qed (simp_all add: bot_inat_def top_inat_def)
   374 
   375 end
   376 
   377 
   378 subsection {* Well-ordering *}
   379 
   380 lemma less_FinE:
   381   "[| n < Fin m; !!k. n = Fin k ==> k < m ==> P |] ==> P"
   382 by (induct n) auto
   383 
   384 lemma less_InftyE:
   385   "[| n < Infty; !!k. n = Fin k ==> P |] ==> P"
   386 by (induct n) auto
   387 
   388 lemma inat_less_induct:
   389   assumes prem: "!!n. \<forall>m::inat. m < n --> P m ==> P n" shows "P n"
   390 proof -
   391   have P_Fin: "!!k. P (Fin k)"
   392     apply (rule nat_less_induct)
   393     apply (rule prem, clarify)
   394     apply (erule less_FinE, simp)
   395     done
   396   show ?thesis
   397   proof (induct n)
   398     fix nat
   399     show "P (Fin nat)" by (rule P_Fin)
   400   next
   401     show "P Infty"
   402       apply (rule prem, clarify)
   403       apply (erule less_InftyE)
   404       apply (simp add: P_Fin)
   405       done
   406   qed
   407 qed
   408 
   409 instance inat :: wellorder
   410 proof
   411   fix P and n
   412   assume hyp: "(\<And>n\<Colon>inat. (\<And>m\<Colon>inat. m < n \<Longrightarrow> P m) \<Longrightarrow> P n)"
   413   show "P n" by (blast intro: inat_less_induct hyp)
   414 qed
   415 
   416 
   417 subsection {* Traditional theorem names *}
   418 
   419 lemmas inat_defs = zero_inat_def one_inat_def number_of_inat_def iSuc_def
   420   plus_inat_def less_eq_inat_def less_inat_def
   421 
   422 lemmas inat_splits = inat.splits
   423 
   424 end