src/HOL/Library/NatPair.thy
changeset 14127 40a4768c8e0b
child 14414 3fd75e96145d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/NatPair.thy	Thu Jul 24 16:37:04 2003 +0200
     1.3 @@ -0,0 +1,93 @@
     1.4 +(*  Title:      HOL/Library/NatPair.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Christophe Tabacznyj and Lawrence C Paulson
     1.7 +    Copyright   1996  University of Cambridge
     1.8 +*)
     1.9 +
    1.10 +header {*
    1.11 +  \title{Pairs of Natural Numbers}
    1.12 +  \author{Stefan Richter}
    1.13 +*}
    1.14 +
    1.15 +theory NatPair = Main:
    1.16 +
    1.17 +text{*An injective function from $\mathbf{N}^2$ to
    1.18 +  $\mathbf{N}$.  Definition and proofs are from 
    1.19 +  Arnold Oberschelp.  Rekursionstheorie.  BI-Wissenschafts-Verlag, 1993
    1.20 +  (page 85).  *}
    1.21 +
    1.22 +constdefs 
    1.23 +  nat2_to_nat:: "(nat * nat) \<Rightarrow> nat"
    1.24 +  "nat2_to_nat pair \<equiv> let (n,m) = pair in (n+m) * Suc (n+m) div 2 + n"
    1.25 +
    1.26 +lemma dvd2_a_x_suc_a: "2 dvd a * (Suc a)" 
    1.27 +proof (cases "2 dvd a") 
    1.28 +  case True
    1.29 +  thus ?thesis by (rule dvd_mult2)
    1.30 +next
    1.31 +  case False 
    1.32 +  hence "Suc (a mod 2) = 2" by (simp add: dvd_eq_mod_eq_0)
    1.33 +  hence "Suc a mod 2 = 0" by (simp add: mod_Suc) 
    1.34 +  hence "2 dvd Suc a" by (simp only:dvd_eq_mod_eq_0) 
    1.35 +  thus ?thesis by (rule dvd_mult)
    1.36 +qed
    1.37 +
    1.38 +lemma assumes eq: "nat2_to_nat (u,v) = nat2_to_nat (x,y)" 
    1.39 +  shows nat2_to_nat_help: "u+v \<le> x+y"
    1.40 +proof (rule classical)
    1.41 +  assume "\<not> ?thesis"
    1.42 +  hence contrapos: "x+y < u+v" 
    1.43 +    by simp
    1.44 +  have "nat2_to_nat (x,y) < (x+y) * Suc (x+y) div 2 + Suc (x + y)" 
    1.45 +    by (unfold nat2_to_nat_def) (simp add: Let_def)
    1.46 +  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2" 
    1.47 +    by (simp only: div_mult_self1_is_m) 
    1.48 +  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2 
    1.49 +    + ((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2" 
    1.50 +  proof -
    1.51 +    have "2 dvd (x+y)*Suc(x+y)" 
    1.52 +      by (rule dvd2_a_x_suc_a)
    1.53 +    hence "(x+y)*Suc(x+y) mod 2 = 0" 
    1.54 +      by (simp only: dvd_eq_mod_eq_0)
    1.55 +    also
    1.56 +    have "2 * Suc(x+y) mod 2 = 0" 
    1.57 +      by (rule mod_mult_self1_is_0)
    1.58 +    ultimately have 
    1.59 +      "((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2 = 0" 
    1.60 +      by simp 
    1.61 +    thus ?thesis 
    1.62 +      by simp
    1.63 +  qed 
    1.64 +  also have "\<dots> = ((x+y)*Suc(x+y) + 2*Suc(x+y)) div 2" 
    1.65 +    by (rule div_add1_eq[THEN sym])
    1.66 +  also have "\<dots> = ((x+y+2)*Suc(x+y)) div 2" 
    1.67 +    by (simp only: add_mult_distrib[THEN sym])
    1.68 +  also from contrapos have "\<dots> \<le> ((Suc(u+v))*(u+v)) div 2" 
    1.69 +    by (simp only: mult_le_mono div_le_mono) 
    1.70 +  also have "\<dots> \<le> nat2_to_nat (u,v)" 
    1.71 +    by (unfold nat2_to_nat_def) (simp add: Let_def)
    1.72 +  finally show ?thesis 
    1.73 +    by (simp only: eq)
    1.74 +qed
    1.75 +
    1.76 +lemma nat2_to_nat_inj: "inj nat2_to_nat"
    1.77 +proof -
    1.78 +  {fix u v x y assume "nat2_to_nat (u,v) = nat2_to_nat (x,y)"
    1.79 +    hence "u+v \<le> x+y" by (rule nat2_to_nat_help)
    1.80 +    also from prems[THEN sym] have "x+y \<le> u+v" 
    1.81 +      by (rule nat2_to_nat_help)
    1.82 +    finally have eq: "u+v = x+y" .
    1.83 +    with prems have ux: "u=x" 
    1.84 +      by (simp add: nat2_to_nat_def Let_def)
    1.85 +    with eq have vy: "v=y" 
    1.86 +      by simp
    1.87 +    with ux have "(u,v) = (x,y)" 
    1.88 +      by simp
    1.89 +  }
    1.90 +  hence "\<And>x y. nat2_to_nat x = nat2_to_nat y \<Longrightarrow> x=y" 
    1.91 +    by fast
    1.92 +  thus ?thesis 
    1.93 +    by (unfold inj_on_def) simp
    1.94 +qed
    1.95 +
    1.96 +end