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