src/HOL/Library/Log_Nat.thy
changeset 70349 697450fd25c1
parent 68406 6beb45f6cf67
child 70350 571ae57313a4
equal deleted inserted replaced
70348:bde161c740ca 70349:697450fd25c1
     7 
     7 
     8 theory Log_Nat
     8 theory Log_Nat
     9 imports Complex_Main
     9 imports Complex_Main
    10 begin
    10 begin
    11 
    11 
    12 definition floorlog :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
    12 subsection \<open>Preliminaries\<close>
    13 "floorlog b a = (if a > 0 \<and> b > 1 then nat \<lfloor>log b a\<rfloor> + 1 else 0)"
    13 
       
    14 lemma divide_nat_diff_div_nat_less_one:
       
    15   "real x / real b - real (x div b) < 1" for x b :: nat
       
    16 proof (cases "b = 0")
       
    17   case True
       
    18   then show ?thesis
       
    19     by simp
       
    20 next
       
    21   case False
       
    22   then have "real (x div b) + real (x mod b) / real b - real (x div b) < 1"
       
    23     by (simp add: field_simps)
       
    24   then show ?thesis
       
    25     by (simp add: real_of_nat_div_aux [symmetric])
       
    26 qed
       
    27 
       
    28 lemma powr_eq_one_iff [simp]:
       
    29   "a powr x = 1 \<longleftrightarrow> x = 0" if "a > 1" for a x :: real
       
    30   using that by (auto simp: powr_def split: if_splits)
       
    31 
       
    32 
       
    33 subsection \<open>Floorlog\<close>
       
    34 
       
    35 definition floorlog :: "nat \<Rightarrow> nat \<Rightarrow> nat"
       
    36   where "floorlog b a = (if a > 0 \<and> b > 1 then nat \<lfloor>log b a\<rfloor> + 1 else 0)"
    14 
    37 
    15 lemma floorlog_mono: "x \<le> y \<Longrightarrow> floorlog b x \<le> floorlog b y"
    38 lemma floorlog_mono: "x \<le> y \<Longrightarrow> floorlog b x \<le> floorlog b y"
    16 by(auto simp: floorlog_def floor_mono nat_mono)
    39   by (auto simp: floorlog_def floor_mono nat_mono)
    17 
    40 
    18 lemma floorlog_bounds:
    41 lemma floorlog_bounds:
    19   assumes "x > 0" "b > 1"
    42   "b ^ (floorlog b x - 1) \<le> x \<and> x < b ^ (floorlog b x)" if "x > 0" "b > 1"
    20   shows "b ^ (floorlog b x - 1) \<le> x \<and> x < b ^ (floorlog b x)"
       
    21 proof
    43 proof
    22   show "b ^ (floorlog b x - 1) \<le> x"
    44   show "b ^ (floorlog b x - 1) \<le> x"
    23   proof -
    45   proof -
    24     have "b ^ nat \<lfloor>log b x\<rfloor> = b powr \<lfloor>log b x\<rfloor>"
    46     have "b ^ nat \<lfloor>log b x\<rfloor> = b powr \<lfloor>log b x\<rfloor>"
    25       using powr_realpow[symmetric, of b "nat \<lfloor>log b x\<rfloor>"] \<open>x > 0\<close> \<open>b > 1\<close>
    47       using powr_realpow[symmetric, of b "nat \<lfloor>log b x\<rfloor>"] \<open>x > 0\<close> \<open>b > 1\<close>
    33   qed
    55   qed
    34   show "x < b ^ (floorlog b x)"
    56   show "x < b ^ (floorlog b x)"
    35   proof -
    57   proof -
    36     have "x \<le> b powr (log b x)" using \<open>x > 0\<close> \<open>b > 1\<close> by simp
    58     have "x \<le> b powr (log b x)" using \<open>x > 0\<close> \<open>b > 1\<close> by simp
    37     also have "\<dots> < b powr (\<lfloor>log b x\<rfloor> + 1)"
    59     also have "\<dots> < b powr (\<lfloor>log b x\<rfloor> + 1)"
    38       using assms by (intro powr_less_mono) auto
    60       using that by (intro powr_less_mono) auto
    39     also have "\<dots> = b ^ nat (\<lfloor>log b (real_of_int x)\<rfloor> + 1)"
    61     also have "\<dots> = b ^ nat (\<lfloor>log b (real_of_int x)\<rfloor> + 1)"
    40       using assms by (simp flip: powr_realpow)
    62       using that by (simp flip: powr_realpow)
    41     finally
    63     finally
    42     have "x < b ^ nat (\<lfloor>log b (int x)\<rfloor> + 1)"
    64     have "x < b ^ nat (\<lfloor>log b (int x)\<rfloor> + 1)"
    43       by (rule of_nat_less_imp_less)
    65       by (rule of_nat_less_imp_less)
    44     then show ?thesis
    66     then show ?thesis
    45       using \<open>x > 0\<close> \<open>b > 1\<close> by (simp add: floorlog_def nat_add_distrib)
    67       using \<open>x > 0\<close> \<open>b > 1\<close> by (simp add: floorlog_def nat_add_distrib)
    46   qed
    68   qed
    47 qed
    69 qed
    48 
    70 
    49 lemma floorlog_power[simp]:
    71 lemma floorlog_power [simp]:
    50   assumes "a > 0" "b > 1"
    72   "floorlog b (a * b ^ c) = floorlog b a + c" if "a > 0" "b > 1"
    51   shows "floorlog b (a * b ^ c) = floorlog b a + c"
       
    52 proof -
    73 proof -
    53   have "\<lfloor>log b a + real c\<rfloor> = \<lfloor>log b a\<rfloor> + c" by arith
    74   have "\<lfloor>log b a + real c\<rfloor> = \<lfloor>log b a\<rfloor> + c" by arith
    54   then show ?thesis using assms
    75   then show ?thesis using that
    55     by (auto simp: floorlog_def log_mult powr_realpow[symmetric] nat_add_distrib)
    76     by (auto simp: floorlog_def log_mult powr_realpow[symmetric] nat_add_distrib)
    56 qed
    77 qed
    57 
    78 
    58 lemma floor_log_add_eqI:
    79 lemma floor_log_add_eqI:
    59   fixes a::nat and b::nat and r::real
    80   "\<lfloor>log b (a + r)\<rfloor> = \<lfloor>log b a\<rfloor>" if "b > 1" "a \<ge> 1" "0 \<le> r" "r < 1"
    60   assumes "b > 1" "a \<ge> 1" "0 \<le> r" "r < 1"
    81     for a b :: nat and r :: real
    61   shows "\<lfloor>log b (a + r)\<rfloor> = \<lfloor>log b a\<rfloor>"
       
    62 proof (rule floor_eq2)
    82 proof (rule floor_eq2)
    63   have "log b a \<le> log b (a + r)" using assms by force
    83   have "log b a \<le> log b (a + r)" using that by force
    64   then show "\<lfloor>log b a\<rfloor> \<le> log b (a + r)" by arith
    84   then show "\<lfloor>log b a\<rfloor> \<le> log b (a + r)" by arith
    65 next
    85 next
    66   define l::int where "l = int b ^ (nat \<lfloor>log b a\<rfloor> + 1)"
    86   define l::int where "l = int b ^ (nat \<lfloor>log b a\<rfloor> + 1)"
    67   have l_def_real: "l = b powr (\<lfloor>log b a\<rfloor> + 1)"
    87   have l_def_real: "l = b powr (\<lfloor>log b a\<rfloor> + 1)"
    68     using assms by (simp add: l_def powr_add powr_real_of_int)
    88     using that by (simp add: l_def powr_add powr_real_of_int)
    69   have "a < l"
    89   have "a < l"
    70   proof -
    90   proof -
    71     have "a = b powr (log b a)" using assms by simp
    91     have "a = b powr (log b a)" using that by simp
    72     also have "\<dots> < b powr floor ((log b a) + 1)"
    92     also have "\<dots> < b powr floor ((log b a) + 1)"
    73       using assms(1) by auto
    93       using that(1) by auto
    74     also have "\<dots> = l"
    94     also have "\<dots> = l"
    75       using assms by (simp add: l_def powr_real_of_int powr_add)
    95       using that by (simp add: l_def powr_real_of_int powr_add)
    76     finally show ?thesis by simp
    96     finally show ?thesis by simp
    77   qed
    97   qed
    78   then have "a + r < l" using assms by simp
    98   then have "a + r < l" using that by simp
    79   then have "log b (a + r) < log b l" using assms by simp
    99   then have "log b (a + r) < log b l" using that by simp
    80   also have "\<dots> = real_of_int \<lfloor>log b a\<rfloor> + 1"
   100   also have "\<dots> = real_of_int \<lfloor>log b a\<rfloor> + 1"
    81     using assms by (simp add: l_def_real)
   101     using that by (simp add: l_def_real)
    82   finally show "log b (a + r) < real_of_int \<lfloor>log b a\<rfloor> + 1" .
   102   finally show "log b (a + r) < real_of_int \<lfloor>log b a\<rfloor> + 1" .
    83 qed
   103 qed
    84 
   104 
    85 lemma divide_nat_diff_div_nat_less_one:
       
    86   fixes x b::nat shows "x / b - x div b < 1"
       
    87 proof -
       
    88   have "int 0 \<noteq> \<lfloor>1::real\<rfloor>" by simp
       
    89   thus ?thesis
       
    90     by (metis add_diff_cancel_left' floor_divide_of_nat_eq less_eq_real_def
       
    91         mod_div_trivial real_of_nat_div3 real_of_nat_div_aux)
       
    92 qed
       
    93 
       
    94 lemma floor_log_div:
   105 lemma floor_log_div:
    95   fixes b x :: nat assumes "b > 1" "x > 0" "x div b > 0"
   106   "\<lfloor>log b x\<rfloor> = \<lfloor>log b (x div b)\<rfloor> + 1" if "b > 1" "x > 0" "x div b > 0"
    96   shows "\<lfloor>log b x\<rfloor> = \<lfloor>log b (x div b)\<rfloor> + 1"
   107     for b x :: nat
    97 proof-
   108 proof-
    98   have "\<lfloor>log b x\<rfloor> = \<lfloor>log b (x / b * b)\<rfloor>" using assms by simp
   109   have "\<lfloor>log b x\<rfloor> = \<lfloor>log b (x / b * b)\<rfloor>" using that by simp
    99   also have "\<dots> = \<lfloor>log b (x / b) + log b b\<rfloor>"
   110   also have "\<dots> = \<lfloor>log b (x / b) + log b b\<rfloor>"
   100     using assms by (subst log_mult) auto
   111     using that by (subst log_mult) auto
   101   also have "\<dots> = \<lfloor>log b (x / b)\<rfloor> + 1" using assms by simp
   112   also have "\<dots> = \<lfloor>log b (x / b)\<rfloor> + 1" using that by simp
   102   also have "\<lfloor>log b (x / b)\<rfloor> = \<lfloor>log b (x div b + (x / b - x div b))\<rfloor>" by simp
   113   also have "\<lfloor>log b (x / b)\<rfloor> = \<lfloor>log b (x div b + (x / b - x div b))\<rfloor>" by simp
   103   also have "\<dots> = \<lfloor>log b (x div b)\<rfloor>"
   114   also have "\<dots> = \<lfloor>log b (x div b)\<rfloor>"
   104     using assms real_of_nat_div4 divide_nat_diff_div_nat_less_one
   115     using that real_of_nat_div4 divide_nat_diff_div_nat_less_one
   105     by (intro floor_log_add_eqI) auto
   116     by (intro floor_log_add_eqI) auto
   106   finally show ?thesis .
   117   finally show ?thesis .
   107 qed
   118 qed
   108 
   119 
   109 lemma compute_floorlog[code]:
   120 lemma compute_floorlog [code]:
   110   "floorlog b x = (if x > 0 \<and> b > 1 then floorlog b (x div b) + 1 else 0)"
   121   "floorlog b x = (if x > 0 \<and> b > 1 then floorlog b (x div b) + 1 else 0)"
   111 by (auto simp: floorlog_def floor_log_div[of b x] div_eq_0_iff nat_add_distrib
   122   by (auto simp: floorlog_def floor_log_div[of b x] div_eq_0_iff nat_add_distrib
   112     intro!: floor_eq2)
   123     intro!: floor_eq2)
   113 
   124 
   114 lemma floor_log_eq_if:
   125 lemma floor_log_eq_if:
   115   fixes b x y :: nat
   126   "\<lfloor>log b x\<rfloor> = \<lfloor>log b y\<rfloor>" if "x div b = y div b" "b > 1" "x > 0" "x div b \<ge> 1"
   116   assumes "x div b = y div b" "b > 1" "x > 0" "x div b \<ge> 1"
   127     for b x y :: nat
   117   shows "floor(log b x) = floor(log b y)"
   128 proof -
   118 proof -
   129   have "y > 0" using that by (auto intro: ccontr)
   119   have "y > 0" using assms by(auto intro: ccontr)
   130   thus ?thesis using that by (simp add: floor_log_div)
   120   thus ?thesis using assms by (simp add: floor_log_div)
       
   121 qed
   131 qed
   122 
   132 
   123 lemma floorlog_eq_if:
   133 lemma floorlog_eq_if:
   124   fixes b x y :: nat
   134   "floorlog b x = floorlog b y" if "x div b = y div b" "b > 1" "x > 0" "x div b \<ge> 1"
   125   assumes "x div b = y div b" "b > 1" "x > 0" "x div b \<ge> 1"
   135     for b x y :: nat
   126   shows "floorlog b x = floorlog b y"
   136 proof -
   127 proof -
   137   have "y > 0" using that by (auto intro: ccontr)
   128   have "y > 0" using assms by(auto intro: ccontr)
   138   then show ?thesis using that
   129   thus ?thesis using assms
   139     by (auto simp add: floorlog_def eq_nat_nat_iff intro: floor_log_eq_if)
   130     by(auto simp add: floorlog_def eq_nat_nat_iff intro: floor_log_eq_if)
   140 qed
   131 qed
   141 
   132 
   142 lemma floorlog_leD:
   133 
   143   "floorlog b x \<le> w \<Longrightarrow> b > 1 \<Longrightarrow> x < b ^ w"
   134 lemma powr_eq_one_iff[simp]: "a powr x = 1 \<longleftrightarrow> (x = 0)"
       
   135   if "a > 1"
       
   136   for a x::real
       
   137   using that
       
   138   by (auto simp: powr_def split: if_splits)
       
   139 
       
   140 lemma floorlog_leD: "floorlog b x \<le> w \<Longrightarrow> b > 1 \<Longrightarrow> x < b ^ w"
       
   141   by (metis floorlog_bounds leD linorder_neqE_nat order.strict_trans power_strict_increasing_iff
   144   by (metis floorlog_bounds leD linorder_neqE_nat order.strict_trans power_strict_increasing_iff
   142       zero_less_one zero_less_power)
   145       zero_less_one zero_less_power)
   143 
   146 
   144 lemma floorlog_leI: "x < b ^ w \<Longrightarrow> 0 \<le> w \<Longrightarrow> b > 1 \<Longrightarrow> floorlog b x \<le> w"
   147 lemma floorlog_leI:
       
   148   "x < b ^ w \<Longrightarrow> 0 \<le> w \<Longrightarrow> b > 1 \<Longrightarrow> floorlog b x \<le> w"
   145   by (drule less_imp_of_nat_less[where 'a=real])
   149   by (drule less_imp_of_nat_less[where 'a=real])
   146     (auto simp: floorlog_def Suc_le_eq nat_less_iff floor_less_iff log_of_power_less)
   150     (auto simp: floorlog_def Suc_le_eq nat_less_iff floor_less_iff log_of_power_less)
   147 
   151 
   148 lemma floorlog_eq_zero_iff:
   152 lemma floorlog_eq_zero_iff:
   149   "floorlog b x = 0 \<longleftrightarrow> (b \<le> 1 \<or> x \<le> 0)"
   153   "floorlog b x = 0 \<longleftrightarrow> b \<le> 1 \<or> x \<le> 0"
   150   by (auto simp: floorlog_def)
   154   by (auto simp: floorlog_def)
   151 
   155 
   152 lemma floorlog_le_iff: "floorlog b x \<le> w \<longleftrightarrow> b \<le> 1 \<or> b > 1 \<and> 0 \<le> w \<and> x < b ^ w"
   156 lemma floorlog_le_iff:
       
   157   "floorlog b x \<le> w \<longleftrightarrow> b \<le> 1 \<or> b > 1 \<and> 0 \<le> w \<and> x < b ^ w"
   153   using floorlog_leD[of b x w] floorlog_leI[of x b w]
   158   using floorlog_leD[of b x w] floorlog_leI[of x b w]
   154   by (auto simp: floorlog_eq_zero_iff[THEN iffD2])
   159   by (auto simp: floorlog_eq_zero_iff[THEN iffD2])
   155 
   160 
   156 lemma floorlog_ge_SucI: "Suc w \<le> floorlog b x" if "b ^ w \<le> x" "b > 1"
   161 lemma floorlog_ge_SucI:
       
   162   "Suc w \<le> floorlog b x" if "b ^ w \<le> x" "b > 1"
   157   using that le_log_of_power[of b w x] power_not_zero
   163   using that le_log_of_power[of b w x] power_not_zero
   158   by (force simp: floorlog_def Suc_le_eq powr_realpow not_less Suc_nat_eq_nat_zadd1
   164   by (force simp: floorlog_def Suc_le_eq powr_realpow not_less Suc_nat_eq_nat_zadd1
   159       zless_nat_eq_int_zless int_add_floor less_floor_iff
   165       zless_nat_eq_int_zless int_add_floor less_floor_iff
   160       simp del: floor_add2)
   166       simp del: floor_add2)
   161 
   167 
   162 lemma floorlog_geI: "w \<le> floorlog b x" if "b ^ (w - 1) \<le> x" "b > 1"
   168 lemma floorlog_geI:
       
   169   "w \<le> floorlog b x" if "b ^ (w - 1) \<le> x" "b > 1"
   163   using floorlog_ge_SucI[of b "w - 1" x] that
   170   using floorlog_ge_SucI[of b "w - 1" x] that
   164   by auto
   171   by auto
   165 
   172 
   166 lemma floorlog_geD: "b ^ (w - 1) \<le> x" if "w \<le> floorlog b x" "w > 0"
   173 lemma floorlog_geD:
       
   174   "b ^ (w - 1) \<le> x" if "w \<le> floorlog b x" "w > 0"
   167 proof -
   175 proof -
   168   have "b > 1" "0 < x"
   176   have "b > 1" "0 < x"
   169     using that by (auto simp: floorlog_def split: if_splits)
   177     using that by (auto simp: floorlog_def split: if_splits)
   170   have "b ^ (w - 1) \<le> x" if "b ^ w \<le> x"
   178   have "b ^ (w - 1) \<le> x" if "b ^ w \<le> x"
   171   proof -
   179   proof -
   193     by (auto simp: floorlog_def le_nat_iff le_floor_iff le_log_iff powr_realpow
   201     by (auto simp: floorlog_def le_nat_iff le_floor_iff le_log_iff powr_realpow
   194         split: if_splits elim!: le_SucE)
   202         split: if_splits elim!: le_SucE)
   195 qed
   203 qed
   196 
   204 
   197 
   205 
   198 definition bitlen :: "int \<Rightarrow> int" where "bitlen a = floorlog 2 (nat a)"
   206 subsection \<open>Bitlen\<close>
   199 
   207 
   200 lemma bitlen_alt_def: "bitlen a = (if a > 0 then \<lfloor>log 2 a\<rfloor> + 1 else 0)"
   208 definition bitlen :: "int \<Rightarrow> int"
   201 by (simp add: bitlen_def floorlog_def)
   209   where "bitlen a = floorlog 2 (nat a)"
   202 
   210 
   203 lemma bitlen_zero[simp]: "bitlen 0 = 0"
   211 lemma bitlen_alt_def:
       
   212   "bitlen a = (if a > 0 then \<lfloor>log 2 a\<rfloor> + 1 else 0)"
       
   213   by (simp add: bitlen_def floorlog_def)
       
   214 
       
   215 lemma bitlen_zero [simp]:
       
   216   "bitlen 0 = 0"
   204   by (auto simp: bitlen_def floorlog_def)
   217   by (auto simp: bitlen_def floorlog_def)
   205 
   218 
   206 lemma bitlen_nonneg: "0 \<le> bitlen x"
   219 lemma bitlen_nonneg:
       
   220   "0 \<le> bitlen x"
   207   by (simp add: bitlen_def)
   221   by (simp add: bitlen_def)
   208 
   222 
   209 lemma bitlen_bounds:
   223 lemma bitlen_bounds:
   210   assumes "x > 0"
   224   "2 ^ nat (bitlen x - 1) \<le> x \<and> x < 2 ^ nat (bitlen x)" if "x > 0"
   211   shows "2 ^ nat (bitlen x - 1) \<le> x \<and> x < 2 ^ nat (bitlen x)"
   225 proof -
   212 proof -
   226   from that have "bitlen x \<ge> 1" by (auto simp: bitlen_alt_def)
   213   from assms have "bitlen x \<ge> 1" by (auto simp: bitlen_alt_def)
   227   with that floorlog_bounds[of "nat x" 2] show ?thesis
   214   with assms floorlog_bounds[of "nat x" 2] show ?thesis
       
   215     by (auto simp add: bitlen_def le_nat_iff nat_less_iff nat_diff_distrib)
   228     by (auto simp add: bitlen_def le_nat_iff nat_less_iff nat_diff_distrib)
   216 qed
   229 qed
   217 
   230 
   218 lemma bitlen_pow2[simp]:
   231 lemma bitlen_pow2 [simp]:
   219   assumes "b > 0"
   232   "bitlen (b * 2 ^ c) = bitlen b + c" if "b > 0"
   220   shows "bitlen (b * 2 ^ c) = bitlen b + c"
   233   using that by (simp add: bitlen_def nat_mult_distrib nat_power_eq)
   221   using assms
   234 
   222   by (simp add: bitlen_def nat_mult_distrib nat_power_eq)
   235 lemma compute_bitlen [code]:
   223 
       
   224 lemma compute_bitlen[code]:
       
   225   "bitlen x = (if x > 0 then bitlen (x div 2) + 1 else 0)"
   236   "bitlen x = (if x > 0 then bitlen (x div 2) + 1 else 0)"
   226 by (simp add: bitlen_def nat_div_distrib compute_floorlog)
   237   by (simp add: bitlen_def nat_div_distrib compute_floorlog)
   227 
   238 
   228 lemma bitlen_eq_zero_iff: "bitlen x = 0 \<longleftrightarrow> x \<le> 0"
   239 lemma bitlen_eq_zero_iff:
   229 by (auto simp add: bitlen_alt_def)
   240   "bitlen x = 0 \<longleftrightarrow> x \<le> 0"
       
   241   by (auto simp add: bitlen_alt_def)
   230    (metis compute_bitlen add.commute bitlen_alt_def bitlen_nonneg less_add_same_cancel2
   242    (metis compute_bitlen add.commute bitlen_alt_def bitlen_nonneg less_add_same_cancel2
   231       not_less zero_less_one)
   243       not_less zero_less_one)
   232 
   244 
   233 lemma bitlen_div:
   245 lemma bitlen_div:
   234   assumes "0 < m"
   246   "1 \<le> real_of_int m / 2^nat (bitlen m - 1)"
   235   shows "1 \<le> real_of_int m / 2^nat (bitlen m - 1)"
   247     and "real_of_int m / 2^nat (bitlen m - 1) < 2" if "0 < m"
   236     and "real_of_int m / 2^nat (bitlen m - 1) < 2"
       
   237 proof -
   248 proof -
   238   let ?B = "2^nat (bitlen m - 1)"
   249   let ?B = "2^nat (bitlen m - 1)"
   239 
   250 
   240   have "?B \<le> m" using bitlen_bounds[OF \<open>0 <m\<close>] ..
   251   have "?B \<le> m" using bitlen_bounds[OF \<open>0 <m\<close>] ..
   241   then have "1 * ?B \<le> real_of_int m"
   252   then have "1 * ?B \<le> real_of_int m"
   242     unfolding of_int_le_iff[symmetric] by auto
   253     unfolding of_int_le_iff[symmetric] by auto
   243   then show "1 \<le> real_of_int m / ?B" by auto
   254   then show "1 \<le> real_of_int m / ?B" by auto
   244 
   255 
   245   from assms have "m \<noteq> 0" by auto
   256   from that have "0 \<le> bitlen m - 1" by (auto simp: bitlen_alt_def)
   246   from assms have "0 \<le> bitlen m - 1" by (auto simp: bitlen_alt_def)
   257 
   247 
   258   have "m < 2^nat(bitlen m)" using bitlen_bounds[OF that] ..
   248   have "m < 2^nat(bitlen m)" using bitlen_bounds[OF assms] ..
   259   also from that have "\<dots> = 2^nat(bitlen m - 1 + 1)"
   249   also from assms have "\<dots> = 2^nat(bitlen m - 1 + 1)"
       
   250     by (auto simp: bitlen_def)
   260     by (auto simp: bitlen_def)
   251   also have "\<dots> = ?B * 2"
   261   also have "\<dots> = ?B * 2"
   252     unfolding nat_add_distrib[OF \<open>0 \<le> bitlen m - 1\<close> zero_le_one] by auto
   262     unfolding nat_add_distrib[OF \<open>0 \<le> bitlen m - 1\<close> zero_le_one] by auto
   253   finally have "real_of_int m < 2 * ?B"
   263   finally have "real_of_int m < 2 * ?B"
   254     by (metis (full_types) mult.commute power.simps(2) of_int_less_numeral_power_cancel_iff)
   264     by (metis (full_types) mult.commute power.simps(2) of_int_less_numeral_power_cancel_iff)
   255   then have "real_of_int m / ?B < 2 * ?B / ?B"
   265   then have "real_of_int m / ?B < 2 * ?B / ?B"
   256     by (rule divide_strict_right_mono) auto
   266     by (rule divide_strict_right_mono) auto
   257   then show "real_of_int m / ?B < 2" by auto
   267   then show "real_of_int m / ?B < 2" by auto
   258 qed
   268 qed
   259 
   269 
   260 lemma bitlen_le_iff_floorlog: "bitlen x \<le> w \<longleftrightarrow> w \<ge> 0 \<and> floorlog 2 (nat x) \<le> nat w"
   270 lemma bitlen_le_iff_floorlog:
       
   271   "bitlen x \<le> w \<longleftrightarrow> w \<ge> 0 \<and> floorlog 2 (nat x) \<le> nat w"
   261   by (auto simp: bitlen_def)
   272   by (auto simp: bitlen_def)
   262 
   273 
   263 lemma bitlen_le_iff_power: "bitlen x \<le> w \<longleftrightarrow> w \<ge> 0 \<and> x < 2 ^ nat w"
   274 lemma bitlen_le_iff_power:
       
   275   "bitlen x \<le> w \<longleftrightarrow> w \<ge> 0 \<and> x < 2 ^ nat w"
   264   by (auto simp: bitlen_le_iff_floorlog floorlog_le_iff)
   276   by (auto simp: bitlen_le_iff_floorlog floorlog_le_iff)
   265 
   277 
   266 lemma less_power_nat_iff_bitlen: "x < 2 ^ w \<longleftrightarrow> bitlen (int x) \<le> w"
   278 lemma less_power_nat_iff_bitlen:
       
   279   "x < 2 ^ w \<longleftrightarrow> bitlen (int x) \<le> w"
   267   using bitlen_le_iff_power[of x w]
   280   using bitlen_le_iff_power[of x w]
   268   by auto
   281   by auto
   269 
   282 
   270 lemma bitlen_ge_iff_power: "w \<le> bitlen x \<longleftrightarrow> w \<le> 0 \<or> 2 ^ (nat w - 1) \<le> x"
   283 lemma bitlen_ge_iff_power:
       
   284   "w \<le> bitlen x \<longleftrightarrow> w \<le> 0 \<or> 2 ^ (nat w - 1) \<le> x"
   271   unfolding bitlen_def
   285   unfolding bitlen_def
   272   by (auto simp flip: nat_le_iff intro: floorlog_geI dest: floorlog_geD)
   286   by (auto simp flip: nat_le_iff intro: floorlog_geI dest: floorlog_geD)
   273 
   287 
   274 lemma bitlen_twopow_add_eq: "bitlen (2 ^ w + b) = w + 1" if "0 \<le> b" "b < 2 ^ w"
   288 lemma bitlen_twopow_add_eq:
       
   289   "bitlen (2 ^ w + b) = w + 1" if "0 \<le> b" "b < 2 ^ w"
   275   by (auto simp: that nat_add_distrib bitlen_le_iff_power bitlen_ge_iff_power intro!: antisym)
   290   by (auto simp: that nat_add_distrib bitlen_le_iff_power bitlen_ge_iff_power intro!: antisym)
   276 
   291 
   277 end
   292 end