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