thys/NatBijection.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 10 May 2013 09:07:03 +0100
changeset 252 415e3082ccbc
child 254 0546ae452747
permissions -rw-r--r--
added first version of natbiject
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
252
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory NatBijection
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
imports Main Parity "~~/src/HOL/Library/Discrete"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
declare One_nat_def[simp del]
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
fun oddfactor :: "nat \<Rightarrow> nat" where
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
  [simp del]: "oddfactor n = (if n = 0 then 0 
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
                              else if even n then oddfactor (n div 2) else n)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
fun evenfactor :: "nat \<Rightarrow> nat" where
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  [simp del]: "evenfactor n = (if n = 0 then 0 
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
                               else if even n then 2 * evenfactor (n div 2) else 1)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
lemma oddfactor[simp]:
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  "oddfactor 0 = 0"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  "odd n \<Longrightarrow> oddfactor n = n"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  "even n \<Longrightarrow> oddfactor n = oddfactor (n div 2)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
by (simp_all add: oddfactor.simps)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
lemma oddfactor_odd:
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  "oddfactor n = 0 \<or> odd (oddfactor n)" 
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
apply(induct n rule: nat_less_induct)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
apply(case_tac "n = 0")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
apply(case_tac "odd n")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
apply(auto)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
lemma bigger: "oddfactor (Suc n) > 0"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
apply(induct n rule: nat_less_induct)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
apply(case_tac "n = 0")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
apply(case_tac "odd n")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
apply(auto)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
apply(drule_tac x="n div 2" in spec)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
apply(drule mp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
apply(auto)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
by (smt numeral_2_eq_2 odd_nat_plus_one_div_two)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
fun pencode :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  "pencode m n = (2 ^ m) * (2 * n + 1) - 1"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
fun pdecode2 :: "nat \<Rightarrow> nat" where
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  "pdecode2 z = (oddfactor (Suc z) - 1) div 2"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
fun pdecode1 :: "nat \<Rightarrow> nat" where
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  "pdecode1 z = Discrete.log ((Suc z) div (oddfactor (Suc z)))"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
lemma k:
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  "odd n \<Longrightarrow> oddfactor (2 ^ m * n) = n"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
apply(induct m)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
apply(simp_all)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
lemma ww: "\<exists>k. n = 2 ^ k * oddfactor n"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
apply(induct n rule: nat_less_induct)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
apply(case_tac "n=0")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
apply(case_tac "odd n")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
apply(rule_tac x="0" in exI)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
apply(drule_tac x="n div 2" in spec)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
apply(erule impE)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
apply(erule exE)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
apply(rule_tac x="Suc k" in exI)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
by (metis div_mult_self2_is_id even_mult_two_ex nat_mult_assoc nat_mult_commute zero_neq_numeral)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
lemma test: "x = y \<Longrightarrow> 2 * x = 2 * y"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
by simp
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
lemma m: "0 < n ==> 2 ^ Discrete.log (n div (oddfactor n)) = n div (oddfactor n)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
apply(induct n rule: nat_less_induct)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
apply(case_tac "n=0")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
apply(case_tac "odd n")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
apply(drule_tac x="n div 2" in spec)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
apply(drule mp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
apply(auto)[1]
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
apply(drule mp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
apply (metis One_nat_def Suc_lessI div_2_gt_zero odd_1_nat)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
apply(subst (asm) oddfactor(3)[symmetric])
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
apply(subst (asm) oddfactor(3)[symmetric])
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
apply(subgoal_tac "n div 2 div oddfactor n = n div (oddfactor n) div 2")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
prefer 2
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
apply (metis div_mult2_eq nat_mult_commute)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
apply(simp only: log_half)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
apply(case_tac "n div oddfactor n = 0")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
apply(simp del: oddfactor)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
apply(drule test)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
apply(subst (asm) power.simps(2)[symmetric])
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
apply(subgoal_tac "Suc (Discrete.log (n div oddfactor n) - 1) = Discrete.log (n div oddfactor n)")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
prefer 2
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
apply (smt log.simps Suc_1 div_less div_mult_self1_is_id log_half log_zero numeral_1_eq_Suc_0 numeral_One odd_1_nat odd_nat_plus_one_div_two one_less_numeral_iff power_one_right semiring_norm(76))
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
apply(simp only: One_nat_def)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
apply(subst dvd_mult_div_cancel)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
apply (smt Suc_1 div_by_0 div_mult_self2_is_id oddfactor_odd dvd_power even_Suc even_numeral_nat even_product_nat nat_even_iff_2_dvd power_0 ww)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
apply(simp (no_asm))
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
lemma m1: "n div oddfactor n * oddfactor n = n"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
apply(induct n rule: nat_less_induct)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
apply(case_tac "n=0")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
apply(case_tac "odd n")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
apply(drule_tac x="n div 2" in spec)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
apply(drule mp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
apply(auto)[1]
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
by (metis add_eq_if diff_add_inverse oddfactor(3) mod_eq_0_iff mult_div_cancel nat_mult_commute ww)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
lemma m2: "0 < n ==> 2 ^ Discrete.log (n div (oddfactor n)) * (oddfactor n) = n"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
apply(subst m)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
apply(subst m1)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
lemma y1:
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  "pdecode2 (pencode m n) = n"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
apply(simp del: One_nat_def)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
apply(subst k)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
apply(auto)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
lemma y2:
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
  "pdecode1 (pencode m n) = m"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
apply(simp only: pdecode1.simps pencode.simps)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
apply(subst Suc_diff_1)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
apply(auto)[1]
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
apply(subst Suc_diff_1)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
apply(auto)[1]
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
apply(subst k)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
apply(auto)[1]
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
by (metis Suc_eq_plus1 Suc_neq_Zero comm_semiring_1_class.normalizing_semiring_rules(7) div_mult_self1_is_id log_exp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
lemma yy: 
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
  "pencode (pdecode1 m) (pdecode2 m) = m"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
apply(induct m rule: nat_less_induct)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
apply(simp (no_asm))
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
apply(case_tac "n = 0")
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
apply(subst dvd_mult_div_cancel)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
using oddfactor_odd
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
apply -
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
apply(drule_tac x="Suc n" in meta_spec)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
apply(erule disjE)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
apply(auto)[1]
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
apply (metis One_nat_def even_num_iff nat_even_iff_2_dvd odd_pos)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
using bigger
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
apply -
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
apply(rotate_tac 3)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
apply(drule_tac x="n" in meta_spec)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
apply(simp del: pencode.simps pdecode2.simps pdecode1.simps One_nat_def add: One_nat_def[symmetric])
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
apply(subst m2)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
fun penc where
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
 "penc (m, n) = pencode m n"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
fun pdec where
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
 "pdec m = (pdecode1 m, pdecode2 m)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
lemma q1: "penc (pdec m) = m"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
apply(simp only: penc.simps pdec.simps)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
apply(rule yy)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
lemma q2: "pdec (penc m) = m"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
apply(simp only: penc.simps pdec.simps)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
apply(case_tac m)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
apply(simp only: penc.simps pdec.simps)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
apply(subst y1)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
apply(subst y2)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
lemma inj_penc: "inj_on penc A"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
apply(rule inj_on_inverseI)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
apply(rule q2)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
lemma inj_pdec: "inj_on pdec A"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
apply(rule inj_on_inverseI)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
apply(rule q1)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
lemma surj_penc: "surj penc"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
apply(rule surjI)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
apply(rule q1)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
lemma surj_pdec: "surj pdec"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
apply(rule surjI)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
apply(rule q2)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
done
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
lemma 
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
  "bij pdec"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
by(simp add: bij_def surj_pdec inj_pdec)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
lemma 
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
  "bij penc"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
by(simp add: bij_def surj_penc inj_penc)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
lemma "a \<le> penc (a, 0)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
apply(induct a)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
by (smt nat_one_le_power)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
lemma "penc(a, 0) \<le> penc (a, b)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
by (metis diff_le_mono le_add1 mult_2_right mult_le_mono1 nat_add_commute nat_mult_1 nat_mult_commute)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
lemma "b \<le> penc (a, b)"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
apply(simp)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
proof -
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
  have f1: "(1\<Colon>nat) + 1 = 2"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
    by (metis mult_2 nat_mult_1_right)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
  have f2: "\<And>x\<^isub>1 x\<^isub>2. (x\<^isub>1\<Colon>nat) \<le> x\<^isub>1 * x\<^isub>2 \<or> \<not> 1 \<le> x\<^isub>2"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
    by (metis mult_le_mono2 nat_mult_1_right)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
  have "1 + (b + b) \<le> 1 + b \<longrightarrow> b \<le> (1 + (b + b)) * (1 + 1) ^ a - 1"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
    by (metis le_add1 le_trans nat_add_left_cancel_le)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
  hence "(1 + (b + b)) * (1 + 1) ^ a \<le> 1 + b \<longrightarrow> b \<le> (1 + (b + b)) * (1 + 1) ^ a - 1"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
    using f2 by (metis le_add1 le_trans one_le_power)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
  hence "b \<le> 2 ^ a * (b + b + 1) - 1"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
    using f1 by (metis le_diff_conv nat_add_commute nat_le_linear nat_mult_commute)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
  thus "b \<le> 2 ^ a * (2 * b + 1) - 1"
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
    by (metis mult_2)
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
qed
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
415e3082ccbc added first version of natbiject
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
end