author | nipkow |
Mon, 30 May 2022 20:58:45 +0200 | |
changeset 75496 | 99b37c391433 |
parent 75455 | 91c16c5ad3e9 |
child 80732 | 3eda814762fc |
permissions | -rw-r--r-- |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
1 |
(* Title: HOL/Library/Log_Nat.thy |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
2 |
Author: Johannes Hölzl, Fabian Immler |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
3 |
Copyright 2012 TU München |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
4 |
*) |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
5 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
6 |
section \<open>Logarithm of Natural Numbers\<close> |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
7 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
8 |
theory Log_Nat |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
9 |
imports Complex_Main |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
10 |
begin |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
11 |
|
70349 | 12 |
subsection \<open>Preliminaries\<close> |
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 |
||
29 |
subsection \<open>Floorlog\<close> |
|
30 |
||
31 |
definition floorlog :: "nat \<Rightarrow> nat \<Rightarrow> nat" |
|
32 |
where "floorlog b a = (if a > 0 \<and> b > 1 then nat \<lfloor>log b a\<rfloor> + 1 else 0)" |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
33 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
34 |
lemma floorlog_mono: "x \<le> y \<Longrightarrow> floorlog b x \<le> floorlog b y" |
70349 | 35 |
by (auto simp: floorlog_def floor_mono nat_mono) |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
36 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
37 |
lemma floorlog_bounds: |
70349 | 38 |
"b ^ (floorlog b x - 1) \<le> x \<and> x < b ^ (floorlog b x)" if "x > 0" "b > 1" |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
39 |
proof |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
40 |
show "b ^ (floorlog b x - 1) \<le> x" |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
41 |
proof - |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
42 |
have "b ^ nat \<lfloor>log b x\<rfloor> = b powr \<lfloor>log b x\<rfloor>" |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
43 |
using powr_realpow[symmetric, of b "nat \<lfloor>log b x\<rfloor>"] \<open>x > 0\<close> \<open>b > 1\<close> |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
44 |
by simp |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
45 |
also have "\<dots> \<le> b powr log b x" using \<open>b > 1\<close> by simp |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
46 |
also have "\<dots> = real_of_int x" using \<open>0 < x\<close> \<open>b > 1\<close> by simp |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
47 |
finally have "b ^ nat \<lfloor>log b x\<rfloor> \<le> real_of_int x" by simp |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
48 |
then show ?thesis |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
49 |
using \<open>0 < x\<close> \<open>b > 1\<close> of_nat_le_iff |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
50 |
by (fastforce simp add: floorlog_def) |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
51 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
52 |
show "x < b ^ (floorlog b x)" |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
53 |
proof - |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
54 |
have "x \<le> b powr (log b x)" using \<open>x > 0\<close> \<open>b > 1\<close> by simp |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
55 |
also have "\<dots> < b powr (\<lfloor>log b x\<rfloor> + 1)" |
70349 | 56 |
using that by (intro powr_less_mono) auto |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
57 |
also have "\<dots> = b ^ nat (\<lfloor>log b (real_of_int x)\<rfloor> + 1)" |
70349 | 58 |
using that by (simp flip: powr_realpow) |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
59 |
finally |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
60 |
have "x < b ^ nat (\<lfloor>log b (int x)\<rfloor> + 1)" |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
61 |
by (rule of_nat_less_imp_less) |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
62 |
then show ?thesis |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
63 |
using \<open>x > 0\<close> \<open>b > 1\<close> by (simp add: floorlog_def nat_add_distrib) |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
64 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
65 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
66 |
|
70349 | 67 |
lemma floorlog_power [simp]: |
68 |
"floorlog b (a * b ^ c) = floorlog b a + c" if "a > 0" "b > 1" |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
69 |
proof - |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
70 |
have "\<lfloor>log b a + real c\<rfloor> = \<lfloor>log b a\<rfloor> + c" by arith |
70349 | 71 |
then show ?thesis using that |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
72 |
by (auto simp: floorlog_def log_mult powr_realpow[symmetric] nat_add_distrib) |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
73 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
74 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
75 |
lemma floor_log_add_eqI: |
70349 | 76 |
"\<lfloor>log b (a + r)\<rfloor> = \<lfloor>log b a\<rfloor>" if "b > 1" "a \<ge> 1" "0 \<le> r" "r < 1" |
77 |
for a b :: nat and r :: real |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
78 |
proof (rule floor_eq2) |
70349 | 79 |
have "log b a \<le> log b (a + r)" using that by force |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
80 |
then show "\<lfloor>log b a\<rfloor> \<le> log b (a + r)" by arith |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
81 |
next |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
82 |
define l::int where "l = int b ^ (nat \<lfloor>log b a\<rfloor> + 1)" |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
83 |
have l_def_real: "l = b powr (\<lfloor>log b a\<rfloor> + 1)" |
70349 | 84 |
using that by (simp add: l_def powr_add powr_real_of_int) |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
85 |
have "a < l" |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
86 |
proof - |
70349 | 87 |
have "a = b powr (log b a)" using that by simp |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
88 |
also have "\<dots> < b powr floor ((log b a) + 1)" |
70349 | 89 |
using that(1) by auto |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
90 |
also have "\<dots> = l" |
70349 | 91 |
using that by (simp add: l_def powr_real_of_int powr_add) |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
92 |
finally show ?thesis by simp |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
93 |
qed |
70349 | 94 |
then have "a + r < l" using that by simp |
95 |
then have "log b (a + r) < log b l" using that by simp |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
96 |
also have "\<dots> = real_of_int \<lfloor>log b a\<rfloor> + 1" |
70349 | 97 |
using that by (simp add: l_def_real) |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
98 |
finally show "log b (a + r) < real_of_int \<lfloor>log b a\<rfloor> + 1" . |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
99 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
100 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
101 |
lemma floor_log_div: |
70349 | 102 |
"\<lfloor>log b x\<rfloor> = \<lfloor>log b (x div b)\<rfloor> + 1" if "b > 1" "x > 0" "x div b > 0" |
103 |
for b x :: nat |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
104 |
proof- |
70349 | 105 |
have "\<lfloor>log b x\<rfloor> = \<lfloor>log b (x / b * b)\<rfloor>" using that by simp |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
106 |
also have "\<dots> = \<lfloor>log b (x / b) + log b b\<rfloor>" |
70349 | 107 |
using that by (subst log_mult) auto |
108 |
also have "\<dots> = \<lfloor>log b (x / b)\<rfloor> + 1" using that by simp |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
109 |
also have "\<lfloor>log b (x / b)\<rfloor> = \<lfloor>log b (x div b + (x / b - x div b))\<rfloor>" by simp |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
110 |
also have "\<dots> = \<lfloor>log b (x div b)\<rfloor>" |
70349 | 111 |
using that real_of_nat_div4 divide_nat_diff_div_nat_less_one |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
112 |
by (intro floor_log_add_eqI) auto |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
113 |
finally show ?thesis . |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
114 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
115 |
|
70349 | 116 |
lemma compute_floorlog [code]: |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
117 |
"floorlog b x = (if x > 0 \<and> b > 1 then floorlog b (x div b) + 1 else 0)" |
70349 | 118 |
by (auto simp: floorlog_def floor_log_div[of b x] div_eq_0_iff nat_add_distrib |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
119 |
intro!: floor_eq2) |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
120 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
121 |
lemma floor_log_eq_if: |
70349 | 122 |
"\<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" |
123 |
for b x y :: nat |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
124 |
proof - |
70349 | 125 |
have "y > 0" using that by (auto intro: ccontr) |
126 |
thus ?thesis using that by (simp add: floor_log_div) |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
127 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
128 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
129 |
lemma floorlog_eq_if: |
70349 | 130 |
"floorlog b x = floorlog b y" if "x div b = y div b" "b > 1" "x > 0" "x div b \<ge> 1" |
131 |
for b x y :: nat |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
132 |
proof - |
70349 | 133 |
have "y > 0" using that by (auto intro: ccontr) |
134 |
then show ?thesis using that |
|
135 |
by (auto simp add: floorlog_def eq_nat_nat_iff intro: floor_log_eq_if) |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
136 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
137 |
|
70349 | 138 |
lemma floorlog_leD: |
139 |
"floorlog b x \<le> w \<Longrightarrow> b > 1 \<Longrightarrow> x < b ^ w" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
140 |
by (metis floorlog_bounds leD linorder_neqE_nat order.strict_trans power_strict_increasing_iff |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
141 |
zero_less_one zero_less_power) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
142 |
|
70349 | 143 |
lemma floorlog_leI: |
144 |
"x < b ^ w \<Longrightarrow> 0 \<le> w \<Longrightarrow> b > 1 \<Longrightarrow> floorlog b x \<le> w" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
145 |
by (drule less_imp_of_nat_less[where 'a=real]) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
146 |
(auto simp: floorlog_def Suc_le_eq nat_less_iff floor_less_iff log_of_power_less) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
147 |
|
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
148 |
lemma floorlog_eq_zero_iff: |
70349 | 149 |
"floorlog b x = 0 \<longleftrightarrow> b \<le> 1 \<or> x \<le> 0" |
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
150 |
by (auto simp: floorlog_def) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
151 |
|
70349 | 152 |
lemma floorlog_le_iff: |
153 |
"floorlog b x \<le> w \<longleftrightarrow> b \<le> 1 \<or> b > 1 \<and> 0 \<le> w \<and> x < b ^ w" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
154 |
using floorlog_leD[of b x w] floorlog_leI[of x b w] |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
155 |
by (auto simp: floorlog_eq_zero_iff[THEN iffD2]) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
156 |
|
70349 | 157 |
lemma floorlog_ge_SucI: |
158 |
"Suc w \<le> floorlog b x" if "b ^ w \<le> x" "b > 1" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
159 |
using that le_log_of_power[of b w x] power_not_zero |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
160 |
by (force simp: floorlog_def Suc_le_eq powr_realpow not_less Suc_nat_eq_nat_zadd1 |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
161 |
zless_nat_eq_int_zless int_add_floor less_floor_iff |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
162 |
simp del: floor_add2) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
163 |
|
70349 | 164 |
lemma floorlog_geI: |
165 |
"w \<le> floorlog b x" if "b ^ (w - 1) \<le> x" "b > 1" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
166 |
using floorlog_ge_SucI[of b "w - 1" x] that |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
167 |
by auto |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
168 |
|
70349 | 169 |
lemma floorlog_geD: |
170 |
"b ^ (w - 1) \<le> x" if "w \<le> floorlog b x" "w > 0" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
171 |
proof - |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
172 |
have "b > 1" "0 < x" |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
173 |
using that by (auto simp: floorlog_def split: if_splits) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
174 |
have "b ^ (w - 1) \<le> x" if "b ^ w \<le> x" |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
175 |
proof - |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
176 |
have "b ^ (w - 1) \<le> b ^ w" |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
177 |
using \<open>b > 1\<close> |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
178 |
by (auto intro!: power_increasing) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
179 |
also note that |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
180 |
finally show ?thesis . |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
181 |
qed |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
182 |
moreover have "b ^ nat \<lfloor>log (real b) (real x)\<rfloor> \<le> x" (is "?l \<le> _") |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
183 |
proof - |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
184 |
have "0 \<le> log (real b) (real x)" |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
185 |
using \<open>b > 1\<close> \<open>0 < x\<close> |
75455
91c16c5ad3e9
tidied auto / simp with null arguments
paulson <lp15@cam.ac.uk>
parents:
70350
diff
changeset
|
186 |
by auto |
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
187 |
then have "?l \<le> b powr log (real b) (real x)" |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
188 |
using \<open>b > 1\<close> |
68406 | 189 |
by (auto simp flip: powr_realpow intro!: powr_mono of_nat_floor) |
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
190 |
also have "\<dots> = x" using \<open>b > 1\<close> \<open>0 < x\<close> |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
191 |
by auto |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
192 |
finally show ?thesis |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
193 |
unfolding of_nat_le_iff . |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
194 |
qed |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
195 |
ultimately show ?thesis |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
196 |
using that |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
197 |
by (auto simp: floorlog_def le_nat_iff le_floor_iff le_log_iff powr_realpow |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
198 |
split: if_splits elim!: le_SucE) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
199 |
qed |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
200 |
|
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
201 |
|
70349 | 202 |
subsection \<open>Bitlen\<close> |
203 |
||
204 |
definition bitlen :: "int \<Rightarrow> int" |
|
205 |
where "bitlen a = floorlog 2 (nat a)" |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
206 |
|
70349 | 207 |
lemma bitlen_alt_def: |
208 |
"bitlen a = (if a > 0 then \<lfloor>log 2 a\<rfloor> + 1 else 0)" |
|
209 |
by (simp add: bitlen_def floorlog_def) |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
210 |
|
70349 | 211 |
lemma bitlen_zero [simp]: |
212 |
"bitlen 0 = 0" |
|
67573 | 213 |
by (auto simp: bitlen_def floorlog_def) |
214 |
||
70349 | 215 |
lemma bitlen_nonneg: |
216 |
"0 \<le> bitlen x" |
|
67573 | 217 |
by (simp add: bitlen_def) |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
218 |
|
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
219 |
lemma bitlen_bounds: |
70349 | 220 |
"2 ^ nat (bitlen x - 1) \<le> x \<and> x < 2 ^ nat (bitlen x)" if "x > 0" |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
221 |
proof - |
70349 | 222 |
from that have "bitlen x \<ge> 1" by (auto simp: bitlen_alt_def) |
223 |
with that floorlog_bounds[of "nat x" 2] show ?thesis |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
224 |
by (auto simp add: bitlen_def le_nat_iff nat_less_iff nat_diff_distrib) |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
225 |
qed |
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
226 |
|
70349 | 227 |
lemma bitlen_pow2 [simp]: |
228 |
"bitlen (b * 2 ^ c) = bitlen b + c" if "b > 0" |
|
229 |
using that by (simp add: bitlen_def nat_mult_distrib nat_power_eq) |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
230 |
|
70349 | 231 |
lemma compute_bitlen [code]: |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
232 |
"bitlen x = (if x > 0 then bitlen (x div 2) + 1 else 0)" |
70349 | 233 |
by (simp add: bitlen_def nat_div_distrib compute_floorlog) |
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
234 |
|
70349 | 235 |
lemma bitlen_eq_zero_iff: |
236 |
"bitlen x = 0 \<longleftrightarrow> x \<le> 0" |
|
237 |
by (auto simp add: bitlen_alt_def) |
|
63664 | 238 |
(metis compute_bitlen add.commute bitlen_alt_def bitlen_nonneg less_add_same_cancel2 |
239 |
not_less zero_less_one) |
|
240 |
||
241 |
lemma bitlen_div: |
|
70349 | 242 |
"1 \<le> real_of_int m / 2^nat (bitlen m - 1)" |
243 |
and "real_of_int m / 2^nat (bitlen m - 1) < 2" if "0 < m" |
|
63664 | 244 |
proof - |
245 |
let ?B = "2^nat (bitlen m - 1)" |
|
246 |
||
247 |
have "?B \<le> m" using bitlen_bounds[OF \<open>0 <m\<close>] .. |
|
248 |
then have "1 * ?B \<le> real_of_int m" |
|
249 |
unfolding of_int_le_iff[symmetric] by auto |
|
250 |
then show "1 \<le> real_of_int m / ?B" by auto |
|
251 |
||
70349 | 252 |
from that have "0 \<le> bitlen m - 1" by (auto simp: bitlen_alt_def) |
63664 | 253 |
|
70349 | 254 |
have "m < 2^nat(bitlen m)" using bitlen_bounds[OF that] .. |
255 |
also from that have "\<dots> = 2^nat(bitlen m - 1 + 1)" |
|
63664 | 256 |
by (auto simp: bitlen_def) |
257 |
also have "\<dots> = ?B * 2" |
|
258 |
unfolding nat_add_distrib[OF \<open>0 \<le> bitlen m - 1\<close> zero_le_one] by auto |
|
259 |
finally have "real_of_int m < 2 * ?B" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
260 |
by (metis (full_types) mult.commute power.simps(2) of_int_less_numeral_power_cancel_iff) |
63664 | 261 |
then have "real_of_int m / ?B < 2 * ?B / ?B" |
262 |
by (rule divide_strict_right_mono) auto |
|
263 |
then show "real_of_int m / ?B < 2" by auto |
|
264 |
qed |
|
265 |
||
70349 | 266 |
lemma bitlen_le_iff_floorlog: |
267 |
"bitlen x \<le> w \<longleftrightarrow> w \<ge> 0 \<and> floorlog 2 (nat x) \<le> nat w" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
268 |
by (auto simp: bitlen_def) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
269 |
|
70349 | 270 |
lemma bitlen_le_iff_power: |
271 |
"bitlen x \<le> w \<longleftrightarrow> w \<ge> 0 \<and> x < 2 ^ nat w" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
272 |
by (auto simp: bitlen_le_iff_floorlog floorlog_le_iff) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
273 |
|
70349 | 274 |
lemma less_power_nat_iff_bitlen: |
275 |
"x < 2 ^ w \<longleftrightarrow> bitlen (int x) \<le> w" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
276 |
using bitlen_le_iff_power[of x w] |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
277 |
by auto |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
278 |
|
70349 | 279 |
lemma bitlen_ge_iff_power: |
280 |
"w \<le> bitlen x \<longleftrightarrow> w \<le> 0 \<or> 2 ^ (nat w - 1) \<le> x" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
281 |
unfolding bitlen_def |
68406 | 282 |
by (auto simp flip: nat_le_iff intro: floorlog_geI dest: floorlog_geD) |
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
283 |
|
70349 | 284 |
lemma bitlen_twopow_add_eq: |
285 |
"bitlen (2 ^ w + b) = w + 1" if "0 \<le> b" "b < 2 ^ w" |
|
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
286 |
by (auto simp: that nat_add_distrib bitlen_le_iff_power bitlen_ge_iff_power intro!: antisym) |
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
63664
diff
changeset
|
287 |
|
63663
28d1deca302e
Extracted floorlog and bitlen to separate theory Log_Nat
nipkow
parents:
diff
changeset
|
288 |
end |