
wenzelm@41959: (*  Title:      HOL/Library/Nat_Bijection.thy
huffman@35700:     Author:     Brian Huffman
huffman@35700:     Author:     Florian Haftmann
huffman@35700:     Author:     Stefan Richter
huffman@35700:     Author:     Tobias Nipkow
huffman@35700:     Author:     Alexander Krauss
huffman@35700: *)
huffman@35700: 
huffman@35700: header {* Bijections between natural numbers and other types *}
huffman@35700: 
huffman@35700: theory Nat_Bijection
huffman@35700: imports Main Parity
huffman@35700: begin
huffman@35700: 
huffman@35700: subsection {* Type @{typ "nat \<times> nat"} *}
huffman@35700: 
huffman@35700: text "Triangle numbers: 0, 1, 3, 6, 10, 15, ..."
huffman@35700: 
huffman@35700: definition
huffman@35700:   triangle :: "nat \<Rightarrow> nat"
huffman@35700: where
huffman@35700:   "triangle n = n * Suc n div 2"
huffman@35700: 
huffman@35700: lemma triangle_0 [simp]: "triangle 0 = 0"
huffman@35700: unfolding triangle_def by simp
huffman@35700: 
huffman@35700: lemma triangle_Suc [simp]: "triangle (Suc n) = triangle n + Suc n"
huffman@35700: unfolding triangle_def by simp
huffman@35700: 
huffman@35700: definition
huffman@35700:   prod_encode :: "nat \<times> nat \<Rightarrow> nat"
huffman@35700: where
huffman@35700:   "prod_encode = (\<lambda>(m, n). triangle (m + n) + m)"
huffman@35700: 
huffman@35700: text {* In this auxiliary function, @{term "triangle k + m"} is an invariant. *}
huffman@35700: 
huffman@35700: fun
huffman@35700:   prod_decode_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat"
huffman@35700: where
huffman@35700:   "prod_decode_aux k m =
huffman@35700:     (if m \<le> k then (m, k - m) else prod_decode_aux (Suc k) (m - Suc k))"
huffman@35700: 
huffman@35700: declare prod_decode_aux.simps [simp del]
huffman@35700: 
huffman@35700: definition
huffman@35700:   prod_decode :: "nat \<Rightarrow> nat \<times> nat"
huffman@35700: where
huffman@35700:   "prod_decode = prod_decode_aux 0"
huffman@35700: 
huffman@35700: lemma prod_encode_prod_decode_aux:
huffman@35700:   "prod_encode (prod_decode_aux k m) = triangle k + m"
huffman@35700: apply (induct k m rule: prod_decode_aux.induct)
huffman@35700: apply (subst prod_decode_aux.simps)
huffman@35700: apply (simp add: prod_encode_def)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma prod_decode_inverse [simp]: "prod_encode (prod_decode n) = n"
huffman@35700: unfolding prod_decode_def by (simp add: prod_encode_prod_decode_aux)
huffman@35700: 
huffman@35700: lemma prod_decode_triangle_add:
huffman@35700:   "prod_decode (triangle k + m) = prod_decode_aux k m"
huffman@35700: apply (induct k arbitrary: m)
huffman@35700: apply (simp add: prod_decode_def)
huffman@35700: apply (simp only: triangle_Suc add_assoc)
huffman@35700: apply (subst prod_decode_aux.simps, simp)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma prod_encode_inverse [simp]: "prod_decode (prod_encode x) = x"
huffman@35700: unfolding prod_encode_def
huffman@35700: apply (induct x)
huffman@35700: apply (simp add: prod_decode_triangle_add)
huffman@35700: apply (subst prod_decode_aux.simps, simp)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma inj_prod_encode: "inj_on prod_encode A"
huffman@35700: by (rule inj_on_inverseI, rule prod_encode_inverse)
huffman@35700: 
huffman@35700: lemma inj_prod_decode: "inj_on prod_decode A"
huffman@35700: by (rule inj_on_inverseI, rule prod_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_prod_encode: "surj prod_encode"
huffman@35700: by (rule surjI, rule prod_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_prod_decode: "surj prod_decode"
huffman@35700: by (rule surjI, rule prod_encode_inverse)
huffman@35700: 
huffman@35700: lemma bij_prod_encode: "bij prod_encode"
huffman@35700: by (rule bijI [OF inj_prod_encode surj_prod_encode])
huffman@35700: 
huffman@35700: lemma bij_prod_decode: "bij prod_decode"
huffman@35700: by (rule bijI [OF inj_prod_decode surj_prod_decode])
huffman@35700: 
huffman@35700: lemma prod_encode_eq: "prod_encode x = prod_encode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_prod_encode [THEN inj_eq])
huffman@35700: 
huffman@35700: lemma prod_decode_eq: "prod_decode x = prod_decode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_prod_decode [THEN inj_eq])
huffman@35700: 
huffman@35700: text {* Ordering properties *}
huffman@35700: 
huffman@35700: lemma le_prod_encode_1: "a \<le> prod_encode (a, b)"
huffman@35700: unfolding prod_encode_def by simp
huffman@35700: 
huffman@35700: lemma le_prod_encode_2: "b \<le> prod_encode (a, b)"
huffman@35700: unfolding prod_encode_def by (induct b, simp_all)
huffman@35700: 
huffman@35700: 
huffman@35700: subsection {* Type @{typ "nat + nat"} *}
huffman@35700: 
huffman@35700: definition
huffman@35700:   sum_encode  :: "nat + nat \<Rightarrow> nat"
huffman@35700: where
huffman@35700:   "sum_encode x = (case x of Inl a \<Rightarrow> 2 * a | Inr b \<Rightarrow> Suc (2 * b))"
huffman@35700: 
huffman@35700: definition
huffman@35700:   sum_decode  :: "nat \<Rightarrow> nat + nat"
huffman@35700: where
huffman@35700:   "sum_decode n = (if even n then Inl (n div 2) else Inr (n div 2))"
huffman@35700: 
huffman@35700: lemma sum_encode_inverse [simp]: "sum_decode (sum_encode x) = x"
huffman@35700: unfolding sum_decode_def sum_encode_def
huffman@35700: by (induct x) simp_all
huffman@35700: 
huffman@35700: lemma sum_decode_inverse [simp]: "sum_encode (sum_decode n) = n"
huffman@35700: unfolding sum_decode_def sum_encode_def numeral_2_eq_2
huffman@35700: by (simp add: even_nat_div_two_times_two odd_nat_div_two_times_two_plus_one
huffman@35700:          del: mult_Suc)
huffman@35700: 
huffman@35700: lemma inj_sum_encode: "inj_on sum_encode A"
huffman@35700: by (rule inj_on_inverseI, rule sum_encode_inverse)
huffman@35700: 
huffman@35700: lemma inj_sum_decode: "inj_on sum_decode A"
huffman@35700: by (rule inj_on_inverseI, rule sum_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_sum_encode: "surj sum_encode"
huffman@35700: by (rule surjI, rule sum_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_sum_decode: "surj sum_decode"
huffman@35700: by (rule surjI, rule sum_encode_inverse)
huffman@35700: 
huffman@35700: lemma bij_sum_encode: "bij sum_encode"
huffman@35700: by (rule bijI [OF inj_sum_encode surj_sum_encode])
huffman@35700: 
huffman@35700: lemma bij_sum_decode: "bij sum_decode"
huffman@35700: by (rule bijI [OF inj_sum_decode surj_sum_decode])
huffman@35700: 
huffman@35700: lemma sum_encode_eq: "sum_encode x = sum_encode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_sum_encode [THEN inj_eq])
huffman@35700: 
huffman@35700: lemma sum_decode_eq: "sum_decode x = sum_decode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_sum_decode [THEN inj_eq])
huffman@35700: 
huffman@35700: 
huffman@35700: subsection {* Type @{typ "int"} *}
huffman@35700: 
huffman@35700: definition
huffman@35700:   int_encode :: "int \<Rightarrow> nat"
huffman@35700: where
huffman@35700:   "int_encode i = sum_encode (if 0 \<le> i then Inl (nat i) else Inr (nat (- i - 1)))"
huffman@35700: 
huffman@35700: definition
huffman@35700:   int_decode :: "nat \<Rightarrow> int"
huffman@35700: where
huffman@35700:   "int_decode n = (case sum_decode n of Inl a \<Rightarrow> int a | Inr b \<Rightarrow> - int b - 1)"
huffman@35700: 
huffman@35700: lemma int_encode_inverse [simp]: "int_decode (int_encode x) = x"
huffman@35700: unfolding int_decode_def int_encode_def by simp
huffman@35700: 
huffman@35700: lemma int_decode_inverse [simp]: "int_encode (int_decode n) = n"
huffman@35700: unfolding int_decode_def int_encode_def using sum_decode_inverse [of n]
huffman@35700: by (cases "sum_decode n", simp_all)
huffman@35700: 
huffman@35700: lemma inj_int_encode: "inj_on int_encode A"
huffman@35700: by (rule inj_on_inverseI, rule int_encode_inverse)
huffman@35700: 
huffman@35700: lemma inj_int_decode: "inj_on int_decode A"
huffman@35700: by (rule inj_on_inverseI, rule int_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_int_encode: "surj int_encode"
huffman@35700: by (rule surjI, rule int_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_int_decode: "surj int_decode"
huffman@35700: by (rule surjI, rule int_encode_inverse)
huffman@35700: 
huffman@35700: lemma bij_int_encode: "bij int_encode"
huffman@35700: by (rule bijI [OF inj_int_encode surj_int_encode])
huffman@35700: 
huffman@35700: lemma bij_int_decode: "bij int_decode"
huffman@35700: by (rule bijI [OF inj_int_decode surj_int_decode])
huffman@35700: 
huffman@35700: lemma int_encode_eq: "int_encode x = int_encode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_int_encode [THEN inj_eq])
huffman@35700: 
huffman@35700: lemma int_decode_eq: "int_decode x = int_decode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_int_decode [THEN inj_eq])
huffman@35700: 
huffman@35700: 
huffman@35700: subsection {* Type @{typ "nat list"} *}
huffman@35700: 
huffman@35700: fun
huffman@35700:   list_encode :: "nat list \<Rightarrow> nat"
huffman@35700: where
huffman@35700:   "list_encode [] = 0"
huffman@35700: | "list_encode (x # xs) = Suc (prod_encode (x, list_encode xs))"
huffman@35700: 
huffman@35700: function
huffman@35700:   list_decode :: "nat \<Rightarrow> nat list"
huffman@35700: where
huffman@35700:   "list_decode 0 = []"
huffman@35700: | "list_decode (Suc n) = (case prod_decode n of (x, y) \<Rightarrow> x # list_decode y)"
huffman@35700: by pat_completeness auto
huffman@35700: 
huffman@35700: termination list_decode
huffman@35700: apply (relation "measure id", simp_all)
huffman@35700: apply (drule arg_cong [where f="prod_encode"])
haftmann@37591: apply (drule sym)
huffman@35700: apply (simp add: le_imp_less_Suc le_prod_encode_2)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma list_encode_inverse [simp]: "list_decode (list_encode x) = x"
huffman@35700: by (induct x rule: list_encode.induct) simp_all
huffman@35700: 
huffman@35700: lemma list_decode_inverse [simp]: "list_encode (list_decode n) = n"
huffman@35700: apply (induct n rule: list_decode.induct, simp)
huffman@35700: apply (simp split: prod.split)
huffman@35700: apply (simp add: prod_decode_eq [symmetric])
huffman@35700: done
huffman@35700: 
huffman@35700: lemma inj_list_encode: "inj_on list_encode A"
huffman@35700: by (rule inj_on_inverseI, rule list_encode_inverse)
huffman@35700: 
huffman@35700: lemma inj_list_decode: "inj_on list_decode A"
huffman@35700: by (rule inj_on_inverseI, rule list_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_list_encode: "surj list_encode"
huffman@35700: by (rule surjI, rule list_decode_inverse)
huffman@35700: 
huffman@35700: lemma surj_list_decode: "surj list_decode"
huffman@35700: by (rule surjI, rule list_encode_inverse)
huffman@35700: 
huffman@35700: lemma bij_list_encode: "bij list_encode"
huffman@35700: by (rule bijI [OF inj_list_encode surj_list_encode])
huffman@35700: 
huffman@35700: lemma bij_list_decode: "bij list_decode"
huffman@35700: by (rule bijI [OF inj_list_decode surj_list_decode])
huffman@35700: 
huffman@35700: lemma list_encode_eq: "list_encode x = list_encode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_list_encode [THEN inj_eq])
huffman@35700: 
huffman@35700: lemma list_decode_eq: "list_decode x = list_decode y \<longleftrightarrow> x = y"
huffman@35700: by (rule inj_list_decode [THEN inj_eq])
huffman@35700: 
huffman@35700: 
huffman@35700: subsection {* Finite sets of naturals *}
huffman@35700: 
huffman@35700: subsubsection {* Preliminaries *}
huffman@35700: 
huffman@35700: lemma finite_vimage_Suc_iff: "finite (Suc -` F) \<longleftrightarrow> finite F"
huffman@35700: apply (safe intro!: finite_vimageI inj_Suc)
huffman@35700: apply (rule finite_subset [where B="insert 0 (Suc ` Suc -` F)"])
huffman@35700: apply (rule subsetI, case_tac x, simp, simp)
huffman@35700: apply (rule finite_insert [THEN iffD2])
huffman@35700: apply (erule finite_imageI)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma vimage_Suc_insert_0: "Suc -` insert 0 A = Suc -` A"
huffman@35700: by auto
huffman@35700: 
huffman@35700: lemma vimage_Suc_insert_Suc:
huffman@35700:   "Suc -` insert (Suc n) A = insert n (Suc -` A)"
huffman@35700: by auto
huffman@35700: 
huffman@35700: lemma even_nat_Suc_div_2: "even x \<Longrightarrow> Suc x div 2 = x div 2"
huffman@35700: by (simp only: numeral_2_eq_2 even_nat_plus_one_div_two)
huffman@35700: 
huffman@35700: lemma div2_even_ext_nat:
huffman@35700:   "\<lbrakk>x div 2 = y div 2; even x = even y\<rbrakk> \<Longrightarrow> (x::nat) = y"
huffman@35700: apply (rule mod_div_equality [of x 2, THEN subst])
huffman@35700: apply (rule mod_div_equality [of y 2, THEN subst])
huffman@35700: apply (case_tac "even x")
huffman@35700: apply (simp add: numeral_2_eq_2 even_nat_equiv_def)
huffman@35700: apply (simp add: numeral_2_eq_2 odd_nat_equiv_def)
huffman@35700: done
huffman@35700: 
huffman@35700: subsubsection {* From sets to naturals *}
huffman@35700: 
huffman@35700: definition
huffman@35700:   set_encode :: "nat set \<Rightarrow> nat"
huffman@35700: where
huffman@35700:   "set_encode = setsum (op ^ 2)"
huffman@35700: 
huffman@35700: lemma set_encode_empty [simp]: "set_encode {} = 0"
huffman@35700: by (simp add: set_encode_def)
huffman@35700: 
huffman@35700: lemma set_encode_insert [simp]:
huffman@35700:   "\<lbrakk>finite A; n \<notin> A\<rbrakk> \<Longrightarrow> set_encode (insert n A) = 2^n + set_encode A"
huffman@35700: by (simp add: set_encode_def)
huffman@35700: 
huffman@35700: lemma even_set_encode_iff: "finite A \<Longrightarrow> even (set_encode A) \<longleftrightarrow> 0 \<notin> A"
huffman@35700: unfolding set_encode_def by (induct set: finite, auto)
huffman@35700: 
huffman@35700: lemma set_encode_vimage_Suc: "set_encode (Suc -` A) = set_encode A div 2"
huffman@35700: apply (cases "finite A")
huffman@35700: apply (erule finite_induct, simp)
huffman@35700: apply (case_tac x)
huffman@35700: apply (simp add: even_nat_Suc_div_2 even_set_encode_iff vimage_Suc_insert_0)
huffman@35700: apply (simp add: finite_vimageI add_commute vimage_Suc_insert_Suc)
huffman@35700: apply (simp add: set_encode_def finite_vimage_Suc_iff)
huffman@35700: done
huffman@35700: 
huffman@35700: lemmas set_encode_div_2 = set_encode_vimage_Suc [symmetric]
huffman@35700: 
huffman@35700: subsubsection {* From naturals to sets *}
huffman@35700: 
huffman@35700: definition
huffman@35700:   set_decode :: "nat \<Rightarrow> nat set"
huffman@35700: where
huffman@35700:   "set_decode x = {n. odd (x div 2 ^ n)}"
huffman@35700: 
huffman@35700: lemma set_decode_0 [simp]: "0 \<in> set_decode x \<longleftrightarrow> odd x"
huffman@35700: by (simp add: set_decode_def)
huffman@35700: 
huffman@35700: lemma set_decode_Suc [simp]:
huffman@35700:   "Suc n \<in> set_decode x \<longleftrightarrow> n \<in> set_decode (x div 2)"
huffman@35700: by (simp add: set_decode_def div_mult2_eq)
huffman@35700: 
huffman@35700: lemma set_decode_zero [simp]: "set_decode 0 = {}"
huffman@35700: by (simp add: set_decode_def)
huffman@35700: 
huffman@35700: lemma set_decode_div_2: "set_decode (x div 2) = Suc -` set_decode x"
huffman@35700: by auto
huffman@35700: 
huffman@35700: lemma set_decode_plus_power_2:
huffman@35700:   "n \<notin> set_decode z \<Longrightarrow> set_decode (2 ^ n + z) = insert n (set_decode z)"
huffman@35700:  apply (induct n arbitrary: z, simp_all)
nipkow@39302:   apply (rule set_eqI, induct_tac x, simp, simp add: even_nat_Suc_div_2)
nipkow@39302:  apply (rule set_eqI, induct_tac x, simp, simp add: add_commute)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma finite_set_decode [simp]: "finite (set_decode n)"
huffman@35700: apply (induct n rule: nat_less_induct)
huffman@35700: apply (case_tac "n = 0", simp)
huffman@35700: apply (drule_tac x="n div 2" in spec, simp)
huffman@35700: apply (simp add: set_decode_div_2)
huffman@35700: apply (simp add: finite_vimage_Suc_iff)
huffman@35700: done
huffman@35700: 
huffman@35700: subsubsection {* Proof of isomorphism *}
huffman@35700: 
huffman@35700: lemma set_decode_inverse [simp]: "set_encode (set_decode n) = n"
huffman@35700: apply (induct n rule: nat_less_induct)
huffman@35700: apply (case_tac "n = 0", simp)
huffman@35700: apply (drule_tac x="n div 2" in spec, simp)
huffman@35700: apply (simp add: set_decode_div_2 set_encode_vimage_Suc)
huffman@35700: apply (erule div2_even_ext_nat)
huffman@35700: apply (simp add: even_set_encode_iff)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma set_encode_inverse [simp]: "finite A \<Longrightarrow> set_decode (set_encode A) = A"
huffman@35700: apply (erule finite_induct, simp_all)
huffman@35700: apply (simp add: set_decode_plus_power_2)
huffman@35700: done
huffman@35700: 
huffman@35700: lemma inj_on_set_encode: "inj_on set_encode (Collect finite)"
huffman@35700: by (rule inj_on_inverseI [where g="set_decode"], simp)
huffman@35700: 
huffman@35700: lemma set_encode_eq:
huffman@35700:   "\<lbrakk>finite A; finite B\<rbrakk> \<Longrightarrow> set_encode A = set_encode B \<longleftrightarrow> A = B"
huffman@35700: by (rule iffI, simp add: inj_onD [OF inj_on_set_encode], simp)
huffman@35700: 
huffman@35700: end




