src/HOL/Power.thy
 author nipkow Wed Jan 09 19:23:36 2008 +0100 (2008-01-09) changeset 25874 14819a95cf75 parent 25836 f7771e4f7064 child 28131 3130d7b3149d permissions -rw-r--r--
1 (*  Title:      HOL/Power.thy
2     ID:         \$Id\$
3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
4     Copyright   1997  University of Cambridge
6 *)
10 theory Power
11 imports Nat
12 begin
14 class power = type +
15   fixes power :: "'a \<Rightarrow> nat \<Rightarrow> 'a"            (infixr "^" 80)
17 subsection{*Powers for Arbitrary Monoids*}
19 class recpower = monoid_mult + power +
20   assumes power_0 [simp]: "a ^ 0       = 1"
21   assumes power_Suc:      "a ^ Suc n = a * (a ^ n)"
23 lemma power_0_Suc [simp]: "(0::'a::{recpower,semiring_0}) ^ (Suc n) = 0"
26 text{*It looks plausible as a simprule, but its effect can be strange.*}
27 lemma power_0_left: "0^n = (if n=0 then 1 else (0::'a::{recpower,semiring_0}))"
28   by (induct n) simp_all
30 lemma power_one [simp]: "1^n = (1::'a::recpower)"
31   by (induct n) (simp_all add: power_Suc)
33 lemma power_one_right [simp]: "(a::'a::recpower) ^ 1 = a"
36 lemma power_commutes: "(a::'a::recpower) ^ n * a = a * a ^ n"
37   by (induct n) (simp_all add: power_Suc mult_assoc)
39 lemma power_add: "(a::'a::recpower) ^ (m+n) = (a^m) * (a^n)"
40   by (induct m) (simp_all add: power_Suc mult_ac)
42 lemma power_mult: "(a::'a::recpower) ^ (m*n) = (a^m) ^ n"
45 lemma power_mult_distrib: "((a::'a::{recpower,comm_monoid_mult}) * b) ^ n = (a^n) * (b^n)"
46   by (induct n) (simp_all add: power_Suc mult_ac)
48 lemma zero_less_power[simp]:
49      "0 < (a::'a::{ordered_semidom,recpower}) ==> 0 < a^n"
50 apply (induct "n")
51 apply (simp_all add: power_Suc zero_less_one mult_pos_pos)
52 done
54 lemma zero_le_power[simp]:
55      "0 \<le> (a::'a::{ordered_semidom,recpower}) ==> 0 \<le> a^n"
57 apply (erule disjE)
58 apply (simp_all add: zero_less_one power_0_left)
59 done
61 lemma one_le_power[simp]:
62      "1 \<le> (a::'a::{ordered_semidom,recpower}) ==> 1 \<le> a^n"
63 apply (induct "n")
65 apply (rule order_trans [OF _ mult_mono [of 1 _ 1]])
66 apply (simp_all add: zero_le_one order_trans [OF zero_le_one])
67 done
69 lemma gt1_imp_ge0: "1 < a ==> 0 \<le> (a::'a::ordered_semidom)"
70   by (simp add: order_trans [OF zero_le_one order_less_imp_le])
72 lemma power_gt1_lemma:
73   assumes gt1: "1 < (a::'a::{ordered_semidom,recpower})"
74   shows "1 < a * a^n"
75 proof -
76   have "1*1 < a*1" using gt1 by simp
77   also have "\<dots> \<le> a * a^n" using gt1
78     by (simp only: mult_mono gt1_imp_ge0 one_le_power order_less_imp_le
79         zero_le_one order_refl)
80   finally show ?thesis by simp
81 qed
83 lemma one_less_power[simp]:
84   "\<lbrakk>1 < (a::'a::{ordered_semidom,recpower}); 0 < n\<rbrakk> \<Longrightarrow> 1 < a ^ n"
85 by (cases n, simp_all add: power_gt1_lemma power_Suc)
87 lemma power_gt1:
88      "1 < (a::'a::{ordered_semidom,recpower}) ==> 1 < a ^ (Suc n)"
89 by (simp add: power_gt1_lemma power_Suc)
91 lemma power_le_imp_le_exp:
92   assumes gt1: "(1::'a::{recpower,ordered_semidom}) < a"
93   shows "!!n. a^m \<le> a^n ==> m \<le> n"
94 proof (induct m)
95   case 0
96   show ?case by simp
97 next
98   case (Suc m)
99   show ?case
100   proof (cases n)
101     case 0
102     from prems have "a * a^m \<le> 1" by (simp add: power_Suc)
103     with gt1 show ?thesis
104       by (force simp only: power_gt1_lemma
105           linorder_not_less [symmetric])
106   next
107     case (Suc n)
108     from prems show ?thesis
109       by (force dest: mult_left_le_imp_le
110           simp add: power_Suc order_less_trans [OF zero_less_one gt1])
111   qed
112 qed
114 text{*Surely we can strengthen this? It holds for @{text "0<a<1"} too.*}
115 lemma power_inject_exp [simp]:
116      "1 < (a::'a::{ordered_semidom,recpower}) ==> (a^m = a^n) = (m=n)"
117   by (force simp add: order_antisym power_le_imp_le_exp)
119 text{*Can relax the first premise to @{term "0<a"} in the case of the
120 natural numbers.*}
121 lemma power_less_imp_less_exp:
122      "[| (1::'a::{recpower,ordered_semidom}) < a; a^m < a^n |] ==> m < n"
123 by (simp add: order_less_le [of m n] order_less_le [of "a^m" "a^n"]
124               power_le_imp_le_exp)
127 lemma power_mono:
128      "[|a \<le> b; (0::'a::{recpower,ordered_semidom}) \<le> a|] ==> a^n \<le> b^n"
129 apply (induct "n")
131 apply (auto intro: mult_mono order_trans [of 0 a b])
132 done
134 lemma power_strict_mono [rule_format]:
135      "[|a < b; (0::'a::{recpower,ordered_semidom}) \<le> a|]
136       ==> 0 < n --> a^n < b^n"
137 apply (induct "n")
138 apply (auto simp add: mult_strict_mono power_Suc
139                       order_le_less_trans [of 0 a b])
140 done
142 lemma power_eq_0_iff [simp]:
143   "(a^n = 0) = (a = (0::'a::{ring_1_no_zero_divisors,recpower}) & n>0)"
144 apply (induct "n")
145 apply (auto simp add: power_Suc zero_neq_one [THEN not_sym])
146 done
148 lemma field_power_not_zero:
149   "a \<noteq> (0::'a::{ring_1_no_zero_divisors,recpower}) ==> a^n \<noteq> 0"
150 by force
152 lemma nonzero_power_inverse:
153   fixes a :: "'a::{division_ring,recpower}"
154   shows "a \<noteq> 0 ==> inverse (a ^ n) = (inverse a) ^ n"
155 apply (induct "n")
156 apply (auto simp add: power_Suc nonzero_inverse_mult_distrib power_commutes)
157 done (* TODO: reorient or rename to nonzero_inverse_power *)
159 text{*Perhaps these should be simprules.*}
160 lemma power_inverse:
161   fixes a :: "'a::{division_ring,division_by_zero,recpower}"
162   shows "inverse (a ^ n) = (inverse a) ^ n"
163 apply (cases "a = 0")
166 done (* TODO: reorient or rename to inverse_power *)
168 lemma power_one_over: "1 / (a::'a::{field,division_by_zero,recpower})^n =
169     (1 / a)^n"
171 apply (rule power_inverse)
172 done
174 lemma nonzero_power_divide:
175     "b \<noteq> 0 ==> (a/b) ^ n = ((a::'a::{field,recpower}) ^ n) / (b ^ n)"
176 by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
178 lemma power_divide:
179     "(a/b) ^ n = ((a::'a::{field,division_by_zero,recpower}) ^ n / b ^ n)"
180 apply (case_tac "b=0", simp add: power_0_left)
181 apply (rule nonzero_power_divide)
182 apply assumption
183 done
185 lemma power_abs: "abs(a ^ n) = abs(a::'a::{ordered_idom,recpower}) ^ n"
186 apply (induct "n")
187 apply (auto simp add: power_Suc abs_mult)
188 done
190 lemma zero_less_power_abs_iff [simp,noatp]:
191      "(0 < (abs a)^n) = (a \<noteq> (0::'a::{ordered_idom,recpower}) | n=0)"
192 proof (induct "n")
193   case 0
194     show ?case by (simp add: zero_less_one)
195 next
196   case (Suc n)
197     show ?case by (auto simp add: prems power_Suc zero_less_mult_iff
198       abs_zero)
199 qed
201 lemma zero_le_power_abs [simp]:
202      "(0::'a::{ordered_idom,recpower}) \<le> (abs a)^n"
203 by (rule zero_le_power [OF abs_ge_zero])
205 lemma power_minus: "(-a) ^ n = (- 1)^n * (a::'a::{comm_ring_1,recpower}) ^ n"
206 proof -
207   have "-a = (- 1) * a"  by (simp add: minus_mult_left [symmetric])
208   thus ?thesis by (simp only: power_mult_distrib)
209 qed
211 text{*Lemma for @{text power_strict_decreasing}*}
212 lemma power_Suc_less:
213      "[|(0::'a::{ordered_semidom,recpower}) < a; a < 1|]
214       ==> a * a^n < a^n"
215 apply (induct n)
216 apply (auto simp add: power_Suc mult_strict_left_mono)
217 done
219 lemma power_strict_decreasing:
220      "[|n < N; 0 < a; a < (1::'a::{ordered_semidom,recpower})|]
221       ==> a^N < a^n"
222 apply (erule rev_mp)
223 apply (induct "N")
224 apply (auto simp add: power_Suc power_Suc_less less_Suc_eq)
225 apply (rename_tac m)
226 apply (subgoal_tac "a * a^m < 1 * a^n", simp)
227 apply (rule mult_strict_mono)
228 apply (auto simp add: zero_less_one order_less_imp_le)
229 done
231 text{*Proof resembles that of @{text power_strict_decreasing}*}
232 lemma power_decreasing:
233      "[|n \<le> N; 0 \<le> a; a \<le> (1::'a::{ordered_semidom,recpower})|]
234       ==> a^N \<le> a^n"
235 apply (erule rev_mp)
236 apply (induct "N")
237 apply (auto simp add: power_Suc  le_Suc_eq)
238 apply (rename_tac m)
239 apply (subgoal_tac "a * a^m \<le> 1 * a^n", simp)
240 apply (rule mult_mono)
241 apply (auto simp add: zero_le_one)
242 done
244 lemma power_Suc_less_one:
245      "[| 0 < a; a < (1::'a::{ordered_semidom,recpower}) |] ==> a ^ Suc n < 1"
246 apply (insert power_strict_decreasing [of 0 "Suc n" a], simp)
247 done
249 text{*Proof again resembles that of @{text power_strict_decreasing}*}
250 lemma power_increasing:
251      "[|n \<le> N; (1::'a::{ordered_semidom,recpower}) \<le> a|] ==> a^n \<le> a^N"
252 apply (erule rev_mp)
253 apply (induct "N")
254 apply (auto simp add: power_Suc le_Suc_eq)
255 apply (rename_tac m)
256 apply (subgoal_tac "1 * a^n \<le> a * a^m", simp)
257 apply (rule mult_mono)
258 apply (auto simp add: order_trans [OF zero_le_one])
259 done
261 text{*Lemma for @{text power_strict_increasing}*}
262 lemma power_less_power_Suc:
263      "(1::'a::{ordered_semidom,recpower}) < a ==> a^n < a * a^n"
264 apply (induct n)
265 apply (auto simp add: power_Suc mult_strict_left_mono order_less_trans [OF zero_less_one])
266 done
268 lemma power_strict_increasing:
269      "[|n < N; (1::'a::{ordered_semidom,recpower}) < a|] ==> a^n < a^N"
270 apply (erule rev_mp)
271 apply (induct "N")
272 apply (auto simp add: power_less_power_Suc power_Suc less_Suc_eq)
273 apply (rename_tac m)
274 apply (subgoal_tac "1 * a^n < a * a^m", simp)
275 apply (rule mult_strict_mono)
276 apply (auto simp add: order_less_trans [OF zero_less_one] order_less_imp_le)
277 done
279 lemma power_increasing_iff [simp]:
280   "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x \<le> b ^ y) = (x \<le> y)"
281 by (blast intro: power_le_imp_le_exp power_increasing order_less_imp_le)
283 lemma power_strict_increasing_iff [simp]:
284   "1 < (b::'a::{ordered_semidom,recpower}) ==> (b ^ x < b ^ y) = (x < y)"
285 by (blast intro: power_less_imp_less_exp power_strict_increasing)
287 lemma power_le_imp_le_base:
288 assumes le: "a ^ Suc n \<le> b ^ Suc n"
289     and ynonneg: "(0::'a::{ordered_semidom,recpower}) \<le> b"
290 shows "a \<le> b"
291 proof (rule ccontr)
292   assume "~ a \<le> b"
293   then have "b < a" by (simp only: linorder_not_le)
294   then have "b ^ Suc n < a ^ Suc n"
295     by (simp only: prems power_strict_mono)
296   from le and this show "False"
297     by (simp add: linorder_not_less [symmetric])
298 qed
300 lemma power_less_imp_less_base:
301   fixes a b :: "'a::{ordered_semidom,recpower}"
302   assumes less: "a ^ n < b ^ n"
303   assumes nonneg: "0 \<le> b"
304   shows "a < b"
305 proof (rule contrapos_pp [OF less])
306   assume "~ a < b"
307   hence "b \<le> a" by (simp only: linorder_not_less)
308   hence "b ^ n \<le> a ^ n" using nonneg by (rule power_mono)
309   thus "~ a ^ n < b ^ n" by (simp only: linorder_not_less)
310 qed
312 lemma power_inject_base:
313      "[| a ^ Suc n = b ^ Suc n; 0 \<le> a; 0 \<le> b |]
314       ==> a = (b::'a::{ordered_semidom,recpower})"
315 by (blast intro: power_le_imp_le_base order_antisym order_eq_refl sym)
317 lemma power_eq_imp_eq_base:
318   fixes a b :: "'a::{ordered_semidom,recpower}"
319   shows "\<lbrakk>a ^ n = b ^ n; 0 \<le> a; 0 \<le> b; 0 < n\<rbrakk> \<Longrightarrow> a = b"
320 by (cases n, simp_all, rule power_inject_base)
323 subsection{*Exponentiation for the Natural Numbers*}
325 instantiation nat :: recpower
326 begin
328 primrec power_nat where
329   "p ^ 0 = (1\<Colon>nat)"
330   | "p ^ (Suc n) = (p\<Colon>nat) * (p ^ n)"
332 instance proof
333   fix z n :: nat
334   show "z^0 = 1" by simp
335   show "z^(Suc n) = z * (z^n)" by simp
336 qed
338 end
340 lemma of_nat_power:
341   "of_nat (m ^ n) = (of_nat m::'a::{semiring_1,recpower}) ^ n"
342 by (induct n, simp_all add: power_Suc of_nat_mult)
344 lemma nat_one_le_power [simp]: "1 \<le> i ==> Suc 0 \<le> i^n"
345 by (insert one_le_power [of i n], simp)
347 lemma nat_zero_less_power_iff [simp]: "(x^n > 0) = (x > (0::nat) | n=0)"
348 by (induct "n", auto)
350 text{*Valid for the naturals, but what if @{text"0<i<1"}?
351 Premises cannot be weakened: consider the case where @{term "i=0"},
352 @{term "m=1"} and @{term "n=0"}.*}
353 lemma nat_power_less_imp_less:
354   assumes nonneg: "0 < (i\<Colon>nat)"
355   assumes less: "i^m < i^n"
356   shows "m < n"
357 proof (cases "i = 1")
358   case True with less power_one [where 'a = nat] show ?thesis by simp
359 next
360   case False with nonneg have "1 < i" by auto
361   from power_strict_increasing_iff [OF this] less show ?thesis ..
362 qed
364 lemma power_diff:
365   assumes nz: "a ~= 0"
366   shows "n <= m ==> (a::'a::{recpower, field}) ^ (m-n) = (a^m) / (a^n)"
367   by (induct m n rule: diff_induct)
368     (simp_all add: power_Suc nonzero_mult_divide_cancel_left nz)
371 text{*ML bindings for the general exponentiation theorems*}
372 ML
373 {*
374 val power_0 = thm"power_0";
375 val power_Suc = thm"power_Suc";
376 val power_0_Suc = thm"power_0_Suc";
377 val power_0_left = thm"power_0_left";
378 val power_one = thm"power_one";
379 val power_one_right = thm"power_one_right";
381 val power_mult = thm"power_mult";
382 val power_mult_distrib = thm"power_mult_distrib";
383 val zero_less_power = thm"zero_less_power";
384 val zero_le_power = thm"zero_le_power";
385 val one_le_power = thm"one_le_power";
386 val gt1_imp_ge0 = thm"gt1_imp_ge0";
387 val power_gt1_lemma = thm"power_gt1_lemma";
388 val power_gt1 = thm"power_gt1";
389 val power_le_imp_le_exp = thm"power_le_imp_le_exp";
390 val power_inject_exp = thm"power_inject_exp";
391 val power_less_imp_less_exp = thm"power_less_imp_less_exp";
392 val power_mono = thm"power_mono";
393 val power_strict_mono = thm"power_strict_mono";
394 val power_eq_0_iff = thm"power_eq_0_iff";
395 val field_power_eq_0_iff = thm"power_eq_0_iff";
396 val field_power_not_zero = thm"field_power_not_zero";
397 val power_inverse = thm"power_inverse";
398 val nonzero_power_divide = thm"nonzero_power_divide";
399 val power_divide = thm"power_divide";
400 val power_abs = thm"power_abs";
401 val zero_less_power_abs_iff = thm"zero_less_power_abs_iff";
402 val zero_le_power_abs = thm "zero_le_power_abs";
403 val power_minus = thm"power_minus";
404 val power_Suc_less = thm"power_Suc_less";
405 val power_strict_decreasing = thm"power_strict_decreasing";
406 val power_decreasing = thm"power_decreasing";
407 val power_Suc_less_one = thm"power_Suc_less_one";
408 val power_increasing = thm"power_increasing";
409 val power_strict_increasing = thm"power_strict_increasing";
410 val power_le_imp_le_base = thm"power_le_imp_le_base";
411 val power_inject_base = thm"power_inject_base";
412 *}
414 text{*ML bindings for the remaining theorems*}
415 ML
416 {*
417 val nat_one_le_power = thm"nat_one_le_power";
418 val nat_power_less_imp_less = thm"nat_power_less_imp_less";
419 val nat_zero_less_power_iff = thm"nat_zero_less_power_iff";
420 *}
422 end