src/HOL/Library/Nat_Bijection.thy
author paulson <lp15@cam.ac.uk>
Wed, 20 May 2020 15:00:25 +0100
changeset 71848 3c7852327787
parent 69593 3dda49e08b9d
child 74965 9469d9223689
permissions -rw-r--r--
A few new theorems, plus some tidying up
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     1
(*  Title:      HOL/Library/Nat_Bijection.thy
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
     3
    Author:     Florian Haftmann
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
     4
    Author:     Stefan Richter
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
     5
    Author:     Tobias Nipkow
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
     6
    Author:     Alexander Krauss
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
     7
*)
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
     8
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
     9
section \<open>Bijections between natural numbers and other types\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    10
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    11
theory Nat_Bijection
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    12
  imports Main
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    13
begin
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    14
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67399
diff changeset
    15
subsection \<open>Type \<^typ>\<open>nat \<times> nat\<close>\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    16
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    17
text \<open>Triangle numbers: 0, 1, 3, 6, 10, 15, ...\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    18
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    19
definition triangle :: "nat \<Rightarrow> nat"
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    20
  where "triangle n = (n * Suc n) div 2"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    21
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    22
lemma triangle_0 [simp]: "triangle 0 = 0"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    23
  by (simp add: triangle_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    24
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    25
lemma triangle_Suc [simp]: "triangle (Suc n) = triangle n + Suc n"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    26
  by (simp add: triangle_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    27
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    28
definition prod_encode :: "nat \<times> nat \<Rightarrow> nat"
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    29
  where "prod_encode = (\<lambda>(m, n). triangle (m + n) + m)"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    30
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67399
diff changeset
    31
text \<open>In this auxiliary function, \<^term>\<open>triangle k + m\<close> is an invariant.\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    32
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    33
fun prod_decode_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    34
  where "prod_decode_aux k m =
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    35
    (if m \<le> k then (m, k - m) else prod_decode_aux (Suc k) (m - Suc k))"
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    36
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    37
declare prod_decode_aux.simps [simp del]
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    38
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    39
definition prod_decode :: "nat \<Rightarrow> nat \<times> nat"
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    40
  where "prod_decode = prod_decode_aux 0"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    41
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    42
lemma prod_encode_prod_decode_aux: "prod_encode (prod_decode_aux k m) = triangle k + m"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    43
proof (induction k m rule: prod_decode_aux.induct)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    44
  case (1 k m)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    45
  then show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    46
    by (simp add: prod_encode_def prod_decode_aux.simps)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    47
qed
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    48
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    49
lemma prod_decode_inverse [simp]: "prod_encode (prod_decode n) = n"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    50
  by (simp add: prod_decode_def prod_encode_prod_decode_aux)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    51
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    52
lemma prod_decode_triangle_add: "prod_decode (triangle k + m) = prod_decode_aux k m"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    53
proof (induct k arbitrary: m)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    54
  case 0
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    55
  then show ?case 
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    56
    by (simp add: prod_decode_def)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    57
next
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    58
  case (Suc k)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    59
  then show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    60
    by (metis ab_semigroup_add_class.add_ac(1) add_diff_cancel_left' le_add1 not_less_eq_eq prod_decode_aux.simps triangle_Suc)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    61
qed
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    62
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    63
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    64
lemma prod_encode_inverse [simp]: "prod_decode (prod_encode x) = x"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    65
  unfolding prod_encode_def
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    66
proof (induct x)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    67
  case (Pair a b)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    68
  then show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    69
    by (simp add: prod_decode_triangle_add prod_decode_aux.simps)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
    70
qed
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    71
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    72
lemma inj_prod_encode: "inj_on prod_encode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    73
  by (rule inj_on_inverseI) (rule prod_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    74
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    75
lemma inj_prod_decode: "inj_on prod_decode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    76
  by (rule inj_on_inverseI) (rule prod_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    77
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    78
lemma surj_prod_encode: "surj prod_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    79
  by (rule surjI) (rule prod_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    80
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    81
lemma surj_prod_decode: "surj prod_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    82
  by (rule surjI) (rule prod_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    83
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    84
lemma bij_prod_encode: "bij prod_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    85
  by (rule bijI [OF inj_prod_encode surj_prod_encode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    86
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    87
lemma bij_prod_decode: "bij prod_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    88
  by (rule bijI [OF inj_prod_decode surj_prod_decode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    89
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    90
lemma prod_encode_eq: "prod_encode x = prod_encode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    91
  by (rule inj_prod_encode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    92
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    93
lemma prod_decode_eq: "prod_decode x = prod_decode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
    94
  by (rule inj_prod_decode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    95
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
    96
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
    97
text \<open>Ordering properties\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    98
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
    99
lemma le_prod_encode_1: "a \<le> prod_encode (a, b)"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   100
  by (simp add: prod_encode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   101
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   102
lemma le_prod_encode_2: "b \<le> prod_encode (a, b)"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   103
  by (induct b) (simp_all add: prod_encode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   104
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   105
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67399
diff changeset
   106
subsection \<open>Type \<^typ>\<open>nat + nat\<close>\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   107
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   108
definition sum_encode :: "nat + nat \<Rightarrow> nat"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   109
  where "sum_encode x = (case x of Inl a \<Rightarrow> 2 * a | Inr b \<Rightarrow> Suc (2 * b))"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   110
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   111
definition sum_decode :: "nat \<Rightarrow> nat + nat"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   112
  where "sum_decode n = (if even n then Inl (n div 2) else Inr (n div 2))"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   113
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   114
lemma sum_encode_inverse [simp]: "sum_decode (sum_encode x) = x"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   115
  by (induct x) (simp_all add: sum_decode_def sum_encode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   116
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   117
lemma sum_decode_inverse [simp]: "sum_encode (sum_decode n) = n"
58834
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   118
  by (simp add: even_two_times_div_two sum_decode_def sum_encode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   119
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   120
lemma inj_sum_encode: "inj_on sum_encode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   121
  by (rule inj_on_inverseI) (rule sum_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   122
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   123
lemma inj_sum_decode: "inj_on sum_decode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   124
  by (rule inj_on_inverseI) (rule sum_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   125
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   126
lemma surj_sum_encode: "surj sum_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   127
  by (rule surjI) (rule sum_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   128
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   129
lemma surj_sum_decode: "surj sum_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   130
  by (rule surjI) (rule sum_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   131
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   132
lemma bij_sum_encode: "bij sum_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   133
  by (rule bijI [OF inj_sum_encode surj_sum_encode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   134
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   135
lemma bij_sum_decode: "bij sum_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   136
  by (rule bijI [OF inj_sum_decode surj_sum_decode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   137
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   138
lemma sum_encode_eq: "sum_encode x = sum_encode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   139
  by (rule inj_sum_encode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   140
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   141
lemma sum_decode_eq: "sum_decode x = sum_decode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   142
  by (rule inj_sum_decode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   143
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   144
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67399
diff changeset
   145
subsection \<open>Type \<^typ>\<open>int\<close>\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   146
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   147
definition int_encode :: "int \<Rightarrow> nat"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   148
  where "int_encode i = sum_encode (if 0 \<le> i then Inl (nat i) else Inr (nat (- i - 1)))"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   149
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   150
definition int_decode :: "nat \<Rightarrow> int"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   151
  where "int_decode n = (case sum_decode n of Inl a \<Rightarrow> int a | Inr b \<Rightarrow> - int b - 1)"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   152
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   153
lemma int_encode_inverse [simp]: "int_decode (int_encode x) = x"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   154
  by (simp add: int_decode_def int_encode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   155
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   156
lemma int_decode_inverse [simp]: "int_encode (int_decode n) = n"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   157
  unfolding int_decode_def int_encode_def
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   158
  using sum_decode_inverse [of n] by (cases "sum_decode n") simp_all
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   159
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   160
lemma inj_int_encode: "inj_on int_encode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   161
  by (rule inj_on_inverseI) (rule int_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   162
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   163
lemma inj_int_decode: "inj_on int_decode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   164
  by (rule inj_on_inverseI) (rule int_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   165
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   166
lemma surj_int_encode: "surj int_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   167
  by (rule surjI) (rule int_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   168
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   169
lemma surj_int_decode: "surj int_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   170
  by (rule surjI) (rule int_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   171
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   172
lemma bij_int_encode: "bij int_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   173
  by (rule bijI [OF inj_int_encode surj_int_encode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   174
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   175
lemma bij_int_decode: "bij int_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   176
  by (rule bijI [OF inj_int_decode surj_int_decode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   177
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   178
lemma int_encode_eq: "int_encode x = int_encode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   179
  by (rule inj_int_encode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   180
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   181
lemma int_decode_eq: "int_decode x = int_decode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   182
  by (rule inj_int_decode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   183
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   184
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67399
diff changeset
   185
subsection \<open>Type \<^typ>\<open>nat list\<close>\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   186
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   187
fun list_encode :: "nat list \<Rightarrow> nat"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   188
  where
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   189
    "list_encode [] = 0"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   190
  | "list_encode (x # xs) = Suc (prod_encode (x, list_encode xs))"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   191
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   192
function list_decode :: "nat \<Rightarrow> nat list"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   193
  where
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   194
    "list_decode 0 = []"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   195
  | "list_decode (Suc n) = (case prod_decode n of (x, y) \<Rightarrow> x # list_decode y)"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   196
  by pat_completeness auto
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   197
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   198
termination list_decode
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   199
proof -
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   200
  have "\<And>n x y. (x, y) = prod_decode n \<Longrightarrow> y < Suc n"
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   201
    by (metis le_imp_less_Suc le_prod_encode_2 prod_decode_inverse)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   202
  then show ?thesis
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   203
    using "termination" by blast
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   204
qed
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   205
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   206
lemma list_encode_inverse [simp]: "list_decode (list_encode x) = x"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   207
  by (induct x rule: list_encode.induct) simp_all
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   208
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   209
lemma list_decode_inverse [simp]: "list_encode (list_decode n) = n"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   210
proof (induct n rule: list_decode.induct)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   211
  case (2 n)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   212
  then show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   213
    by (metis list_encode.simps(2) list_encode_inverse prod_decode_inverse surj_pair)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   214
qed auto
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   215
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   216
lemma inj_list_encode: "inj_on list_encode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   217
  by (rule inj_on_inverseI) (rule list_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   218
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   219
lemma inj_list_decode: "inj_on list_decode A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   220
  by (rule inj_on_inverseI) (rule list_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   221
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   222
lemma surj_list_encode: "surj list_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   223
  by (rule surjI) (rule list_decode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   224
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   225
lemma surj_list_decode: "surj list_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   226
  by (rule surjI) (rule list_encode_inverse)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   227
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   228
lemma bij_list_encode: "bij list_encode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   229
  by (rule bijI [OF inj_list_encode surj_list_encode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   230
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   231
lemma bij_list_decode: "bij list_decode"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   232
  by (rule bijI [OF inj_list_decode surj_list_decode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   233
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   234
lemma list_encode_eq: "list_encode x = list_encode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   235
  by (rule inj_list_encode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   236
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   237
lemma list_decode_eq: "list_decode x = list_decode y \<longleftrightarrow> x = y"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   238
  by (rule inj_list_decode [THEN inj_eq])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   239
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   240
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
   241
subsection \<open>Finite sets of naturals\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   242
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
   243
subsubsection \<open>Preliminaries\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   244
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   245
lemma finite_vimage_Suc_iff: "finite (Suc -` F) \<longleftrightarrow> finite F"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   246
proof 
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   247
  have "F \<subseteq> insert 0 (Suc ` Suc -` F)"
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   248
    using nat.nchotomy by force
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   249
  moreover
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   250
  assume "finite (Suc -` F)"
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   251
  then have "finite (insert 0 (Suc ` Suc -` F))"
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   252
    by blast
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   253
  ultimately show "finite F"
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   254
    using finite_subset by blast
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   255
qed (force intro: finite_vimageI inj_Suc)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   256
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   257
lemma vimage_Suc_insert_0: "Suc -` insert 0 A = Suc -` A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   258
  by auto
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   259
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   260
lemma vimage_Suc_insert_Suc: "Suc -` insert (Suc n) A = insert n (Suc -` A)"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   261
  by auto
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   262
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   263
lemma div2_even_ext_nat:
58834
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   264
  fixes x y :: nat
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   265
  assumes "x div 2 = y div 2"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   266
    and "even x \<longleftrightarrow> even y"
58834
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   267
  shows "x = y"
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   268
proof -
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
   269
  from \<open>even x \<longleftrightarrow> even y\<close> have "x mod 2 = y mod 2"
58834
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   270
    by (simp only: even_iff_mod_2_eq_zero) auto
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   271
  with assms have "x div 2 * 2 + x mod 2 = y div 2 * 2 + y mod 2"
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   272
    by simp
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   273
  then show ?thesis
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   274
    by simp
773b378d9313 more simp rules concerning dvd and even/odd
haftmann
parents: 58770
diff changeset
   275
qed
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   276
58710
7216a10d69ba augmented and tuned facts on even/odd and division
haftmann
parents: 57512
diff changeset
   277
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
   278
subsubsection \<open>From sets to naturals\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   279
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   280
definition set_encode :: "nat set \<Rightarrow> nat"
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 64267
diff changeset
   281
  where "set_encode = sum ((^) 2)"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   282
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   283
lemma set_encode_empty [simp]: "set_encode {} = 0"
59506
4af607652318 Not a simprule, as it complicates proofs
paulson <lp15@cam.ac.uk>
parents: 58881
diff changeset
   284
  by (simp add: set_encode_def)
4af607652318 Not a simprule, as it complicates proofs
paulson <lp15@cam.ac.uk>
parents: 58881
diff changeset
   285
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   286
lemma set_encode_inf: "\<not> finite A \<Longrightarrow> set_encode A = 0"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   287
  by (simp add: set_encode_def)
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   288
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   289
lemma set_encode_insert [simp]: "finite A \<Longrightarrow> n \<notin> A \<Longrightarrow> set_encode (insert n A) = 2^n + set_encode A"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   290
  by (simp add: set_encode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   291
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   292
lemma even_set_encode_iff: "finite A \<Longrightarrow> even (set_encode A) \<longleftrightarrow> 0 \<notin> A"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   293
  by (induct set: finite) (auto simp: set_encode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   294
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   295
lemma set_encode_vimage_Suc: "set_encode (Suc -` A) = set_encode A div 2"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   296
proof (induction A rule: infinite_finite_induct)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   297
  case (infinite A)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   298
  then show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   299
    by (simp add: finite_vimage_Suc_iff set_encode_inf)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   300
next
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   301
  case (insert x A)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   302
  show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   303
  proof (cases x)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   304
    case 0
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   305
    with insert show ?thesis
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   306
      by (simp add: even_set_encode_iff vimage_Suc_insert_0)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   307
  next
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   308
    case (Suc y)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   309
    with insert show ?thesis
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   310
      by (simp add: finite_vimageI add.commute vimage_Suc_insert_Suc)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   311
  qed   
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   312
qed auto
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   313
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   314
lemmas set_encode_div_2 = set_encode_vimage_Suc [symmetric]
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   315
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   316
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
   317
subsubsection \<open>From naturals to sets\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   318
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   319
definition set_decode :: "nat \<Rightarrow> nat set"
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   320
  where "set_decode x = {n. odd (x div 2 ^ n)}"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   321
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   322
lemma set_decode_0 [simp]: "0 \<in> set_decode x \<longleftrightarrow> odd x"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   323
  by (simp add: set_decode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   324
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   325
lemma set_decode_Suc [simp]: "Suc n \<in> set_decode x \<longleftrightarrow> n \<in> set_decode (x div 2)"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   326
  by (simp add: set_decode_def div_mult2_eq)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   327
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   328
lemma set_decode_zero [simp]: "set_decode 0 = {}"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   329
  by (simp add: set_decode_def)
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   330
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   331
lemma set_decode_div_2: "set_decode (x div 2) = Suc -` set_decode x"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   332
  by auto
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   333
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   334
lemma set_decode_plus_power_2:
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   335
  "n \<notin> set_decode z \<Longrightarrow> set_decode (2 ^ n + z) = insert n (set_decode z)"
60352
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59506
diff changeset
   336
proof (induct n arbitrary: z)
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   337
  case 0
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   338
  show ?case
60352
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59506
diff changeset
   339
  proof (rule set_eqI)
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   340
    show "q \<in> set_decode (2 ^ 0 + z) \<longleftrightarrow> q \<in> insert 0 (set_decode z)" for q
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   341
      by (induct q) (use 0 in simp_all)
60352
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59506
diff changeset
   342
  qed
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59506
diff changeset
   343
next
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   344
  case (Suc n)
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   345
  show ?case
60352
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59506
diff changeset
   346
  proof (rule set_eqI)
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   347
    show "q \<in> set_decode (2 ^ Suc n + z) \<longleftrightarrow> q \<in> insert (Suc n) (set_decode z)" for q
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   348
      by (induct q) (use Suc in simp_all)
60352
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59506
diff changeset
   349
  qed
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 59506
diff changeset
   350
qed
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   351
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   352
lemma finite_set_decode [simp]: "finite (set_decode n)"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   353
proof (induction n rule: less_induct)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   354
  case (less n)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   355
  show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   356
  proof (cases "n = 0")
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   357
    case False
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   358
    then show ?thesis
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   359
      using less.IH [of "n div 2"] finite_vimage_Suc_iff set_decode_div_2 by auto
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   360
  qed auto
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   361
qed
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   362
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   363
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60352
diff changeset
   364
subsubsection \<open>Proof of isomorphism\<close>
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   365
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   366
lemma set_decode_inverse [simp]: "set_encode (set_decode n) = n"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   367
proof (induction n rule: less_induct)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   368
  case (less n)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   369
  show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   370
  proof (cases "n = 0")
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   371
    case False
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   372
    then have "set_encode (set_decode (n div 2)) = n div 2"
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   373
      using less.IH by auto
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   374
    then show ?thesis
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   375
      by (metis div2_even_ext_nat even_set_encode_iff finite_set_decode set_decode_0 set_decode_div_2 set_encode_div_2)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   376
  qed auto
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   377
qed
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   378
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   379
lemma set_encode_inverse [simp]: "finite A \<Longrightarrow> set_decode (set_encode A) = A"
71848
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   380
proof (induction rule: finite_induct)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   381
  case (insert x A)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   382
  then show ?case
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   383
    by (simp add: set_decode_plus_power_2)
3c7852327787 A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents: 69593
diff changeset
   384
qed auto
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   385
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   386
lemma inj_on_set_encode: "inj_on set_encode (Collect finite)"
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   387
  by (rule inj_on_inverseI [where g = "set_decode"]) simp
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   388
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   389
lemma set_encode_eq: "finite A \<Longrightarrow> finite B \<Longrightarrow> set_encode A = set_encode B \<longleftrightarrow> A = B"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   390
  by (rule iffI) (simp_all add: inj_onD [OF inj_on_set_encode])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   391
62046
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   392
lemma subset_decode_imp_le:
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   393
  assumes "set_decode m \<subseteq> set_decode n"
2c9f68fbf047 tuned whitespace;
wenzelm
parents: 60500
diff changeset
   394
  shows "m \<le> n"
51414
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   395
proof -
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   396
  have "n = m + set_encode (set_decode n - set_decode m)"
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   397
  proof -
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   398
    obtain A B where
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   399
      "m = set_encode A" "finite A"
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   400
      "n = set_encode B" "finite B"
51414
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   401
      by (metis finite_set_decode set_decode_inverse)
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   402
  with assms show ?thesis
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63625
diff changeset
   403
    by auto (simp add: set_encode_def add.commute sum.subset_diff)
51414
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   404
  qed
63625
1e7c5bbea36d misc tuning and modernization;
wenzelm
parents: 62046
diff changeset
   405
  then show ?thesis
51414
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   406
    by (metis le_add1)
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   407
qed
587f493447d9 new lemma subset_decode_imp_le
paulson
parents: 41959
diff changeset
   408
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents:
diff changeset
   409
end