src/HOL/Library/Nat_Infinity.thy
author huffman
Mon May 11 08:24:35 2009 -0700 (2009-05-11)
changeset 31094 7d6edb28bdbc
parent 31084 f4db921165ce
child 31998 2c7a24f74db9
permissions -rw-r--r--
removed redundant instance declarations inat :: linorder
     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 inline]: "1 = Fin 1"
    44 
    45 definition
    46   [code inline, 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
   129   [code del]: "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
   180   times_inat_def [code del]:
   181   "m * n = (case m of \<infinity> \<Rightarrow> if n = 0 then 0 else \<infinity> | Fin m \<Rightarrow>
   182     (case n of \<infinity> \<Rightarrow> if m = 0 then 0 else \<infinity> | Fin n \<Rightarrow> Fin (m * n)))"
   183 
   184 lemma times_inat_simps [simp, code]:
   185   "Fin m * Fin n = Fin (m * n)"
   186   "\<infinity> * \<infinity> = \<infinity>"
   187   "\<infinity> * Fin n = (if n = 0 then 0 else \<infinity>)"
   188   "Fin m * \<infinity> = (if m = 0 then 0 else \<infinity>)"
   189   unfolding times_inat_def zero_inat_def
   190   by (simp_all split: inat.split)
   191 
   192 instance proof
   193   fix a b c :: inat
   194   show "(a * b) * c = a * (b * c)"
   195     unfolding times_inat_def zero_inat_def
   196     by (simp split: inat.split)
   197   show "a * b = b * a"
   198     unfolding times_inat_def zero_inat_def
   199     by (simp split: inat.split)
   200   show "1 * a = a"
   201     unfolding times_inat_def zero_inat_def one_inat_def
   202     by (simp split: inat.split)
   203   show "(a + b) * c = a * c + b * c"
   204     unfolding times_inat_def zero_inat_def
   205     by (simp split: inat.split add: left_distrib)
   206   show "0 * a = 0"
   207     unfolding times_inat_def zero_inat_def
   208     by (simp split: inat.split)
   209   show "a * 0 = 0"
   210     unfolding times_inat_def zero_inat_def
   211     by (simp split: inat.split)
   212   show "(0::inat) \<noteq> 1"
   213     unfolding zero_inat_def one_inat_def
   214     by simp
   215 qed
   216 
   217 end
   218 
   219 lemma mult_iSuc: "iSuc m * n = n + m * n"
   220   unfolding iSuc_plus_1 by (simp add: algebra_simps)
   221 
   222 lemma mult_iSuc_right: "m * iSuc n = m + m * n"
   223   unfolding iSuc_plus_1 by (simp add: algebra_simps)
   224 
   225 lemma of_nat_eq_Fin: "of_nat n = Fin n"
   226   apply (induct n)
   227   apply (simp add: Fin_0)
   228   apply (simp add: plus_1_iSuc iSuc_Fin)
   229   done
   230 
   231 instance inat :: semiring_char_0
   232   by default (simp add: of_nat_eq_Fin)
   233 
   234 
   235 subsection {* Ordering *}
   236 
   237 instantiation inat :: ordered_ab_semigroup_add
   238 begin
   239 
   240 definition
   241   [code del]: "m \<le> n = (case n of Fin n1 \<Rightarrow> (case m of Fin m1 \<Rightarrow> m1 \<le> n1 | \<infinity> \<Rightarrow> False)
   242     | \<infinity> \<Rightarrow> True)"
   243 
   244 definition
   245   [code del]: "m < n = (case m of Fin m1 \<Rightarrow> (case n of Fin n1 \<Rightarrow> m1 < n1 | \<infinity> \<Rightarrow> True)
   246     | \<infinity> \<Rightarrow> False)"
   247 
   248 lemma inat_ord_simps [simp]:
   249   "Fin m \<le> Fin n \<longleftrightarrow> m \<le> n"
   250   "Fin m < Fin n \<longleftrightarrow> m < n"
   251   "q \<le> \<infinity>"
   252   "q < \<infinity> \<longleftrightarrow> q \<noteq> \<infinity>"
   253   "\<infinity> \<le> q \<longleftrightarrow> q = \<infinity>"
   254   "\<infinity> < q \<longleftrightarrow> False"
   255   by (simp_all add: less_eq_inat_def less_inat_def split: inat.splits)
   256 
   257 lemma inat_ord_code [code]:
   258   "Fin m \<le> Fin n \<longleftrightarrow> m \<le> n"
   259   "Fin m < Fin n \<longleftrightarrow> m < n"
   260   "q \<le> \<infinity> \<longleftrightarrow> True"
   261   "Fin m < \<infinity> \<longleftrightarrow> True"
   262   "\<infinity> \<le> Fin n \<longleftrightarrow> False"
   263   "\<infinity> < q \<longleftrightarrow> False"
   264   by simp_all
   265 
   266 instance by default
   267   (auto simp add: less_eq_inat_def less_inat_def plus_inat_def split: inat.splits)
   268 
   269 end
   270 
   271 instance inat :: pordered_comm_semiring
   272 proof
   273   fix a b c :: inat
   274   assume "a \<le> b" and "0 \<le> c"
   275   thus "c * a \<le> c * b"
   276     unfolding times_inat_def less_eq_inat_def zero_inat_def
   277     by (simp split: inat.splits)
   278 qed
   279 
   280 lemma inat_ord_number [simp]:
   281   "(number_of m \<Colon> inat) \<le> number_of n \<longleftrightarrow> (number_of m \<Colon> nat) \<le> number_of n"
   282   "(number_of m \<Colon> inat) < number_of n \<longleftrightarrow> (number_of m \<Colon> nat) < number_of n"
   283   by (simp_all add: number_of_inat_def)
   284 
   285 lemma i0_lb [simp]: "(0\<Colon>inat) \<le> n"
   286   by (simp add: zero_inat_def less_eq_inat_def split: inat.splits)
   287 
   288 lemma i0_neq [simp]: "n \<le> (0\<Colon>inat) \<longleftrightarrow> n = 0"
   289   by (simp add: zero_inat_def less_eq_inat_def split: inat.splits)
   290 
   291 lemma Infty_ileE [elim!]: "\<infinity> \<le> Fin m \<Longrightarrow> R"
   292   by (simp add: zero_inat_def less_eq_inat_def split: inat.splits)
   293 
   294 lemma Infty_ilessE [elim!]: "\<infinity> < Fin m \<Longrightarrow> R"
   295   by simp
   296 
   297 lemma not_ilessi0 [simp]: "\<not> n < (0\<Colon>inat)"
   298   by (simp add: zero_inat_def less_inat_def split: inat.splits)
   299 
   300 lemma i0_eq [simp]: "(0\<Colon>inat) < n \<longleftrightarrow> n \<noteq> 0"
   301   by (simp add: zero_inat_def less_inat_def split: inat.splits)
   302 
   303 lemma iSuc_ile_mono [simp]: "iSuc n \<le> iSuc m \<longleftrightarrow> n \<le> m"
   304   by (simp add: iSuc_def less_eq_inat_def split: inat.splits)
   305  
   306 lemma iSuc_mono [simp]: "iSuc n < iSuc m \<longleftrightarrow> n < m"
   307   by (simp add: iSuc_def less_inat_def split: inat.splits)
   308 
   309 lemma ile_iSuc [simp]: "n \<le> iSuc n"
   310   by (simp add: iSuc_def less_eq_inat_def split: inat.splits)
   311 
   312 lemma not_iSuc_ilei0 [simp]: "\<not> iSuc n \<le> 0"
   313   by (simp add: zero_inat_def iSuc_def less_eq_inat_def split: inat.splits)
   314 
   315 lemma i0_iless_iSuc [simp]: "0 < iSuc n"
   316   by (simp add: zero_inat_def iSuc_def less_inat_def split: inat.splits)
   317 
   318 lemma ileI1: "m < n \<Longrightarrow> iSuc m \<le> n"
   319   by (simp add: iSuc_def less_eq_inat_def less_inat_def split: inat.splits)
   320 
   321 lemma Suc_ile_eq: "Fin (Suc m) \<le> n \<longleftrightarrow> Fin m < n"
   322   by (cases n) auto
   323 
   324 lemma iless_Suc_eq [simp]: "Fin m < iSuc n \<longleftrightarrow> Fin m \<le> n"
   325   by (auto simp add: iSuc_def less_inat_def split: inat.splits)
   326 
   327 lemma min_inat_simps [simp]:
   328   "min (Fin m) (Fin n) = Fin (min m n)"
   329   "min q 0 = 0"
   330   "min 0 q = 0"
   331   "min q \<infinity> = q"
   332   "min \<infinity> q = q"
   333   by (auto simp add: min_def)
   334 
   335 lemma max_inat_simps [simp]:
   336   "max (Fin m) (Fin n) = Fin (max m n)"
   337   "max q 0 = q"
   338   "max 0 q = q"
   339   "max q \<infinity> = \<infinity>"
   340   "max \<infinity> q = \<infinity>"
   341   by (simp_all add: max_def)
   342 
   343 lemma Fin_ile: "n \<le> Fin m \<Longrightarrow> \<exists>k. n = Fin k"
   344   by (cases n) simp_all
   345 
   346 lemma Fin_iless: "n < Fin m \<Longrightarrow> \<exists>k. n = Fin k"
   347   by (cases n) simp_all
   348 
   349 lemma chain_incr: "\<forall>i. \<exists>j. Y i < Y j ==> \<exists>j. Fin k < Y j"
   350 apply (induct_tac k)
   351  apply (simp (no_asm) only: Fin_0)
   352  apply (fast intro: le_less_trans [OF i0_lb])
   353 apply (erule exE)
   354 apply (drule spec)
   355 apply (erule exE)
   356 apply (drule ileI1)
   357 apply (rule iSuc_Fin [THEN subst])
   358 apply (rule exI)
   359 apply (erule (1) le_less_trans)
   360 done
   361 
   362 instantiation inat :: "{bot, top}"
   363 begin
   364 
   365 definition bot_inat :: inat where
   366   "bot_inat = 0"
   367 
   368 definition top_inat :: inat where
   369   "top_inat = \<infinity>"
   370 
   371 instance proof
   372 qed (simp_all add: bot_inat_def top_inat_def)
   373 
   374 end
   375 
   376 
   377 subsection {* Well-ordering *}
   378 
   379 lemma less_FinE:
   380   "[| n < Fin m; !!k. n = Fin k ==> k < m ==> P |] ==> P"
   381 by (induct n) auto
   382 
   383 lemma less_InftyE:
   384   "[| n < Infty; !!k. n = Fin k ==> P |] ==> P"
   385 by (induct n) auto
   386 
   387 lemma inat_less_induct:
   388   assumes prem: "!!n. \<forall>m::inat. m < n --> P m ==> P n" shows "P n"
   389 proof -
   390   have P_Fin: "!!k. P (Fin k)"
   391     apply (rule nat_less_induct)
   392     apply (rule prem, clarify)
   393     apply (erule less_FinE, simp)
   394     done
   395   show ?thesis
   396   proof (induct n)
   397     fix nat
   398     show "P (Fin nat)" by (rule P_Fin)
   399   next
   400     show "P Infty"
   401       apply (rule prem, clarify)
   402       apply (erule less_InftyE)
   403       apply (simp add: P_Fin)
   404       done
   405   qed
   406 qed
   407 
   408 instance inat :: wellorder
   409 proof
   410   fix P and n
   411   assume hyp: "(\<And>n\<Colon>inat. (\<And>m\<Colon>inat. m < n \<Longrightarrow> P m) \<Longrightarrow> P n)"
   412   show "P n" by (blast intro: inat_less_induct hyp)
   413 qed
   414 
   415 
   416 subsection {* Traditional theorem names *}
   417 
   418 lemmas inat_defs = zero_inat_def one_inat_def number_of_inat_def iSuc_def
   419   plus_inat_def less_eq_inat_def less_inat_def
   420 
   421 lemmas inat_splits = inat.splits
   422 
   423 end