src/HOL/Library/NatPair.thy
author wenzelm
Thu May 06 14:14:18 2004 +0200 (2004-05-06)
changeset 14706 71590b7733b7
parent 14414 3fd75e96145d
child 15131 c69542757a4d
permissions -rw-r--r--
tuned document;
paulson@14127
     1
(*  Title:      HOL/Library/NatPair.thy
paulson@14127
     2
    ID:         $Id$
nipkow@14414
     3
    Author:     Stefan Richter
nipkow@14414
     4
    Copyright   2003 Technische Universitaet Muenchen
paulson@14127
     5
*)
paulson@14127
     6
wenzelm@14706
     7
header {* Pairs of Natural Numbers *}
paulson@14127
     8
paulson@14127
     9
theory NatPair = Main:
paulson@14127
    10
wenzelm@14706
    11
text{*
wenzelm@14706
    12
  An injective function from @{text "\<nat>\<twosuperior>"} to @{text
wenzelm@14706
    13
  \<nat>}.  Definition and proofs are from \cite[page
wenzelm@14706
    14
  85]{Oberschelp:1993}.
wenzelm@14706
    15
*}
paulson@14127
    16
wenzelm@14706
    17
constdefs
paulson@14127
    18
  nat2_to_nat:: "(nat * nat) \<Rightarrow> nat"
paulson@14127
    19
  "nat2_to_nat pair \<equiv> let (n,m) = pair in (n+m) * Suc (n+m) div 2 + n"
paulson@14127
    20
wenzelm@14706
    21
lemma dvd2_a_x_suc_a: "2 dvd a * (Suc a)"
wenzelm@14706
    22
proof (cases "2 dvd a")
paulson@14127
    23
  case True
paulson@14127
    24
  thus ?thesis by (rule dvd_mult2)
paulson@14127
    25
next
wenzelm@14706
    26
  case False
paulson@14127
    27
  hence "Suc (a mod 2) = 2" by (simp add: dvd_eq_mod_eq_0)
wenzelm@14706
    28
  hence "Suc a mod 2 = 0" by (simp add: mod_Suc)
wenzelm@14706
    29
  hence "2 dvd Suc a" by (simp only:dvd_eq_mod_eq_0)
paulson@14127
    30
  thus ?thesis by (rule dvd_mult)
paulson@14127
    31
qed
paulson@14127
    32
wenzelm@14706
    33
lemma
wenzelm@14706
    34
  assumes eq: "nat2_to_nat (u,v) = nat2_to_nat (x,y)"
paulson@14127
    35
  shows nat2_to_nat_help: "u+v \<le> x+y"
paulson@14127
    36
proof (rule classical)
paulson@14127
    37
  assume "\<not> ?thesis"
wenzelm@14706
    38
  hence contrapos: "x+y < u+v"
paulson@14127
    39
    by simp
wenzelm@14706
    40
  have "nat2_to_nat (x,y) < (x+y) * Suc (x+y) div 2 + Suc (x + y)"
paulson@14127
    41
    by (unfold nat2_to_nat_def) (simp add: Let_def)
wenzelm@14706
    42
  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2"
wenzelm@14706
    43
    by (simp only: div_mult_self1_is_m)
wenzelm@14706
    44
  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2
wenzelm@14706
    45
    + ((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2"
paulson@14127
    46
  proof -
wenzelm@14706
    47
    have "2 dvd (x+y)*Suc(x+y)"
paulson@14127
    48
      by (rule dvd2_a_x_suc_a)
wenzelm@14706
    49
    hence "(x+y)*Suc(x+y) mod 2 = 0"
paulson@14127
    50
      by (simp only: dvd_eq_mod_eq_0)
paulson@14127
    51
    also
wenzelm@14706
    52
    have "2 * Suc(x+y) mod 2 = 0"
paulson@14127
    53
      by (rule mod_mult_self1_is_0)
wenzelm@14706
    54
    ultimately have
wenzelm@14706
    55
      "((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2 = 0"
wenzelm@14706
    56
      by simp
wenzelm@14706
    57
    thus ?thesis
paulson@14127
    58
      by simp
wenzelm@14706
    59
  qed
wenzelm@14706
    60
  also have "\<dots> = ((x+y)*Suc(x+y) + 2*Suc(x+y)) div 2"
wenzelm@14706
    61
    by (rule div_add1_eq [symmetric])
wenzelm@14706
    62
  also have "\<dots> = ((x+y+2)*Suc(x+y)) div 2"
wenzelm@14706
    63
    by (simp only: add_mult_distrib [symmetric])
wenzelm@14706
    64
  also from contrapos have "\<dots> \<le> ((Suc(u+v))*(u+v)) div 2"
wenzelm@14706
    65
    by (simp only: mult_le_mono div_le_mono)
wenzelm@14706
    66
  also have "\<dots> \<le> nat2_to_nat (u,v)"
paulson@14127
    67
    by (unfold nat2_to_nat_def) (simp add: Let_def)
wenzelm@14706
    68
  finally show ?thesis
paulson@14127
    69
    by (simp only: eq)
paulson@14127
    70
qed
paulson@14127
    71
wenzelm@14706
    72
theorem nat2_to_nat_inj: "inj nat2_to_nat"
paulson@14127
    73
proof -
wenzelm@14706
    74
  {
wenzelm@14706
    75
    fix u v x y assume "nat2_to_nat (u,v) = nat2_to_nat (x,y)"
paulson@14127
    76
    hence "u+v \<le> x+y" by (rule nat2_to_nat_help)
wenzelm@14706
    77
    also from prems [symmetric] have "x+y \<le> u+v"
paulson@14127
    78
      by (rule nat2_to_nat_help)
paulson@14127
    79
    finally have eq: "u+v = x+y" .
wenzelm@14706
    80
    with prems have ux: "u=x"
paulson@14127
    81
      by (simp add: nat2_to_nat_def Let_def)
wenzelm@14706
    82
    with eq have vy: "v=y"
paulson@14127
    83
      by simp
wenzelm@14706
    84
    with ux have "(u,v) = (x,y)"
paulson@14127
    85
      by simp
paulson@14127
    86
  }
wenzelm@14706
    87
  hence "\<And>x y. nat2_to_nat x = nat2_to_nat y \<Longrightarrow> x=y"
paulson@14127
    88
    by fast
wenzelm@14706
    89
  thus ?thesis
paulson@14127
    90
    by (unfold inj_on_def) simp
paulson@14127
    91
qed
paulson@14127
    92
paulson@14127
    93
end