src/HOL/Power.thy
author haftmann
Mon Nov 17 14:55:33 2014 +0100 (2014-11-17)
changeset 59009 348561aa3869
parent 58889 5b7a9633cfa8
child 59741 5b762cd73a8e
permissions -rw-r--r--
generalized lemmas (particularly concerning dvd) as far as appropriate
     1 (*  Title:      HOL/Power.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1997  University of Cambridge
     4 *)
     5 
     6 section {* Exponentiation *}
     7 
     8 theory Power
     9 imports Num Equiv_Relations
    10 begin
    11 
    12 subsection {* Powers for Arbitrary Monoids *}
    13 
    14 class power = one + times
    15 begin
    16 
    17 primrec power :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixr "^" 80) where
    18     power_0: "a ^ 0 = 1"
    19   | power_Suc: "a ^ Suc n = a * a ^ n"
    20 
    21 notation (latex output)
    22   power ("(_\<^bsup>_\<^esup>)" [1000] 1000)
    23 
    24 notation (HTML output)
    25   power ("(_\<^bsup>_\<^esup>)" [1000] 1000)
    26 
    27 text {* Special syntax for squares. *}
    28 
    29 abbreviation (xsymbols)
    30   power2 :: "'a \<Rightarrow> 'a"  ("(_\<^sup>2)" [1000] 999) where
    31   "x\<^sup>2 \<equiv> x ^ 2"
    32 
    33 notation (latex output)
    34   power2  ("(_\<^sup>2)" [1000] 999)
    35 
    36 notation (HTML output)
    37   power2  ("(_\<^sup>2)" [1000] 999)
    38 
    39 end
    40 
    41 context monoid_mult
    42 begin
    43 
    44 subclass power .
    45 
    46 lemma power_one [simp]:
    47   "1 ^ n = 1"
    48   by (induct n) simp_all
    49 
    50 lemma power_one_right [simp]:
    51   "a ^ 1 = a"
    52   by simp
    53 
    54 lemma power_commutes:
    55   "a ^ n * a = a * a ^ n"
    56   by (induct n) (simp_all add: mult.assoc)
    57 
    58 lemma power_Suc2:
    59   "a ^ Suc n = a ^ n * a"
    60   by (simp add: power_commutes)
    61 
    62 lemma power_add:
    63   "a ^ (m + n) = a ^ m * a ^ n"
    64   by (induct m) (simp_all add: algebra_simps)
    65 
    66 lemma power_mult:
    67   "a ^ (m * n) = (a ^ m) ^ n"
    68   by (induct n) (simp_all add: power_add)
    69 
    70 lemma power2_eq_square: "a\<^sup>2 = a * a"
    71   by (simp add: numeral_2_eq_2)
    72 
    73 lemma power3_eq_cube: "a ^ 3 = a * a * a"
    74   by (simp add: numeral_3_eq_3 mult.assoc)
    75 
    76 lemma power_even_eq:
    77   "a ^ (2 * n) = (a ^ n)\<^sup>2"
    78   by (subst mult.commute) (simp add: power_mult)
    79 
    80 lemma power_odd_eq:
    81   "a ^ Suc (2*n) = a * (a ^ n)\<^sup>2"
    82   by (simp add: power_even_eq)
    83 
    84 lemma power_numeral_even:
    85   "z ^ numeral (Num.Bit0 w) = (let w = z ^ (numeral w) in w * w)"
    86   unfolding numeral_Bit0 power_add Let_def ..
    87 
    88 lemma power_numeral_odd:
    89   "z ^ numeral (Num.Bit1 w) = (let w = z ^ (numeral w) in z * w * w)"
    90   unfolding numeral_Bit1 One_nat_def add_Suc_right add_0_right
    91   unfolding power_Suc power_add Let_def mult.assoc ..
    92 
    93 lemma funpow_times_power:
    94   "(times x ^^ f x) = times (x ^ f x)"
    95 proof (induct "f x" arbitrary: f)
    96   case 0 then show ?case by (simp add: fun_eq_iff)
    97 next
    98   case (Suc n)
    99   def g \<equiv> "\<lambda>x. f x - 1"
   100   with Suc have "n = g x" by simp
   101   with Suc have "times x ^^ g x = times (x ^ g x)" by simp
   102   moreover from Suc g_def have "f x = g x + 1" by simp
   103   ultimately show ?case by (simp add: power_add funpow_add fun_eq_iff mult.assoc)
   104 qed
   105 
   106 lemma power_commuting_commutes:
   107   assumes "x * y = y * x"
   108   shows "x ^ n * y = y * x ^n"
   109 proof (induct n)
   110   case (Suc n)
   111   have "x ^ Suc n * y = x ^ n * y * x"
   112     by (subst power_Suc2) (simp add: assms ac_simps)
   113   also have "\<dots> = y * x ^ Suc n"
   114     unfolding Suc power_Suc2
   115     by (simp add: ac_simps)
   116   finally show ?case .
   117 qed simp
   118 
   119 end
   120 
   121 context comm_monoid_mult
   122 begin
   123 
   124 lemma power_mult_distrib [field_simps]:
   125   "(a * b) ^ n = (a ^ n) * (b ^ n)"
   126   by (induct n) (simp_all add: ac_simps)
   127 
   128 end
   129 
   130 context semiring_numeral
   131 begin
   132 
   133 lemma numeral_sqr: "numeral (Num.sqr k) = numeral k * numeral k"
   134   by (simp only: sqr_conv_mult numeral_mult)
   135 
   136 lemma numeral_pow: "numeral (Num.pow k l) = numeral k ^ numeral l"
   137   by (induct l, simp_all only: numeral_class.numeral.simps pow.simps
   138     numeral_sqr numeral_mult power_add power_one_right)
   139 
   140 lemma power_numeral [simp]: "numeral k ^ numeral l = numeral (Num.pow k l)"
   141   by (rule numeral_pow [symmetric])
   142 
   143 end
   144 
   145 context semiring_1
   146 begin
   147 
   148 lemma of_nat_power:
   149   "of_nat (m ^ n) = of_nat m ^ n"
   150   by (induct n) (simp_all add: of_nat_mult)
   151 
   152 lemma zero_power:
   153   "0 < n \<Longrightarrow> 0 ^ n = 0"
   154   by (cases n) simp_all
   155 
   156 lemma power_zero_numeral [simp]:
   157   "0 ^ numeral k = 0"
   158   by (simp add: numeral_eq_Suc)
   159 
   160 lemma zero_power2: "0\<^sup>2 = 0" (* delete? *)
   161   by (rule power_zero_numeral)
   162 
   163 lemma one_power2: "1\<^sup>2 = 1" (* delete? *)
   164   by (rule power_one)
   165 
   166 end
   167 
   168 context comm_semiring_1
   169 begin
   170 
   171 text {* The divides relation *}
   172 
   173 lemma le_imp_power_dvd:
   174   assumes "m \<le> n" shows "a ^ m dvd a ^ n"
   175 proof
   176   have "a ^ n = a ^ (m + (n - m))"
   177     using `m \<le> n` by simp
   178   also have "\<dots> = a ^ m * a ^ (n - m)"
   179     by (rule power_add)
   180   finally show "a ^ n = a ^ m * a ^ (n - m)" .
   181 qed
   182 
   183 lemma power_le_dvd:
   184   "a ^ n dvd b \<Longrightarrow> m \<le> n \<Longrightarrow> a ^ m dvd b"
   185   by (rule dvd_trans [OF le_imp_power_dvd])
   186 
   187 lemma dvd_power_same:
   188   "x dvd y \<Longrightarrow> x ^ n dvd y ^ n"
   189   by (induct n) (auto simp add: mult_dvd_mono)
   190 
   191 lemma dvd_power_le:
   192   "x dvd y \<Longrightarrow> m \<ge> n \<Longrightarrow> x ^ n dvd y ^ m"
   193   by (rule power_le_dvd [OF dvd_power_same])
   194 
   195 lemma dvd_power [simp]:
   196   assumes "n > (0::nat) \<or> x = 1"
   197   shows "x dvd (x ^ n)"
   198 using assms proof
   199   assume "0 < n"
   200   then have "x ^ n = x ^ Suc (n - 1)" by simp
   201   then show "x dvd (x ^ n)" by simp
   202 next
   203   assume "x = 1"
   204   then show "x dvd (x ^ n)" by simp
   205 qed
   206 
   207 end
   208 
   209 context ring_1
   210 begin
   211 
   212 lemma power_minus:
   213   "(- a) ^ n = (- 1) ^ n * a ^ n"
   214 proof (induct n)
   215   case 0 show ?case by simp
   216 next
   217   case (Suc n) then show ?case
   218     by (simp del: power_Suc add: power_Suc2 mult.assoc)
   219 qed
   220 
   221 lemma power_minus_Bit0:
   222   "(- x) ^ numeral (Num.Bit0 k) = x ^ numeral (Num.Bit0 k)"
   223   by (induct k, simp_all only: numeral_class.numeral.simps power_add
   224     power_one_right mult_minus_left mult_minus_right minus_minus)
   225 
   226 lemma power_minus_Bit1:
   227   "(- x) ^ numeral (Num.Bit1 k) = - (x ^ numeral (Num.Bit1 k))"
   228   by (simp only: eval_nat_numeral(3) power_Suc power_minus_Bit0 mult_minus_left)
   229 
   230 lemma power2_minus [simp]:
   231   "(- a)\<^sup>2 = a\<^sup>2"
   232   by (rule power_minus_Bit0)
   233 
   234 lemma power_minus1_even [simp]:
   235   "(- 1) ^ (2*n) = 1"
   236 proof (induct n)
   237   case 0 show ?case by simp
   238 next
   239   case (Suc n) then show ?case by (simp add: power_add power2_eq_square)
   240 qed
   241 
   242 lemma power_minus1_odd:
   243   "(- 1) ^ Suc (2*n) = -1"
   244   by simp
   245 
   246 lemma power_minus_even [simp]:
   247   "(-a) ^ (2*n) = a ^ (2*n)"
   248   by (simp add: power_minus [of a])
   249 
   250 end
   251 
   252 lemma power_eq_0_nat_iff [simp]:
   253   fixes m n :: nat
   254   shows "m ^ n = 0 \<longleftrightarrow> m = 0 \<and> n > 0"
   255   by (induct n) auto
   256 
   257 context ring_1_no_zero_divisors
   258 begin
   259 
   260 lemma power_eq_0_iff [simp]:
   261   "a ^ n = 0 \<longleftrightarrow> a = 0 \<and> n > 0"
   262   by (induct n) auto
   263 
   264 lemma field_power_not_zero:
   265   "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
   266   by (induct n) auto
   267 
   268 lemma zero_eq_power2 [simp]:
   269   "a\<^sup>2 = 0 \<longleftrightarrow> a = 0"
   270   unfolding power2_eq_square by simp
   271 
   272 lemma power2_eq_1_iff:
   273   "a\<^sup>2 = 1 \<longleftrightarrow> a = 1 \<or> a = - 1"
   274   unfolding power2_eq_square by (rule square_eq_1_iff)
   275 
   276 end
   277 
   278 context idom
   279 begin
   280 
   281 lemma power2_eq_iff: "x\<^sup>2 = y\<^sup>2 \<longleftrightarrow> x = y \<or> x = - y"
   282   unfolding power2_eq_square by (rule square_eq_iff)
   283 
   284 end
   285 
   286 context division_ring
   287 begin
   288 
   289 text {* FIXME reorient or rename to @{text nonzero_inverse_power} *}
   290 lemma nonzero_power_inverse:
   291   "a \<noteq> 0 \<Longrightarrow> inverse (a ^ n) = (inverse a) ^ n"
   292   by (induct n)
   293     (simp_all add: nonzero_inverse_mult_distrib power_commutes field_power_not_zero)
   294 
   295 end
   296 
   297 context field
   298 begin
   299 
   300 lemma nonzero_power_divide:
   301   "b \<noteq> 0 \<Longrightarrow> (a / b) ^ n = a ^ n / b ^ n"
   302   by (simp add: divide_inverse power_mult_distrib nonzero_power_inverse)
   303 
   304 end
   305 
   306 
   307 subsection {* Exponentiation on ordered types *}
   308 
   309 context linordered_ring (* TODO: move *)
   310 begin
   311 
   312 lemma sum_squares_ge_zero:
   313   "0 \<le> x * x + y * y"
   314   by (intro add_nonneg_nonneg zero_le_square)
   315 
   316 lemma not_sum_squares_lt_zero:
   317   "\<not> x * x + y * y < 0"
   318   by (simp add: not_less sum_squares_ge_zero)
   319 
   320 end
   321 
   322 context linordered_semidom
   323 begin
   324 
   325 lemma zero_less_power [simp]:
   326   "0 < a \<Longrightarrow> 0 < a ^ n"
   327   by (induct n) simp_all
   328 
   329 lemma zero_le_power [simp]:
   330   "0 \<le> a \<Longrightarrow> 0 \<le> a ^ n"
   331   by (induct n) simp_all
   332 
   333 lemma power_mono:
   334   "a \<le> b \<Longrightarrow> 0 \<le> a \<Longrightarrow> a ^ n \<le> b ^ n"
   335   by (induct n) (auto intro: mult_mono order_trans [of 0 a b])
   336 
   337 lemma one_le_power [simp]: "1 \<le> a \<Longrightarrow> 1 \<le> a ^ n"
   338   using power_mono [of 1 a n] by simp
   339 
   340 lemma power_le_one: "\<lbrakk>0 \<le> a; a \<le> 1\<rbrakk> \<Longrightarrow> a ^ n \<le> 1"
   341   using power_mono [of a 1 n] by simp
   342 
   343 lemma power_gt1_lemma:
   344   assumes gt1: "1 < a"
   345   shows "1 < a * a ^ n"
   346 proof -
   347   from gt1 have "0 \<le> a"
   348     by (fact order_trans [OF zero_le_one less_imp_le])
   349   have "1 * 1 < a * 1" using gt1 by simp
   350   also have "\<dots> \<le> a * a ^ n" using gt1
   351     by (simp only: mult_mono `0 \<le> a` one_le_power order_less_imp_le
   352         zero_le_one order_refl)
   353   finally show ?thesis by simp
   354 qed
   355 
   356 lemma power_gt1:
   357   "1 < a \<Longrightarrow> 1 < a ^ Suc n"
   358   by (simp add: power_gt1_lemma)
   359 
   360 lemma one_less_power [simp]:
   361   "1 < a \<Longrightarrow> 0 < n \<Longrightarrow> 1 < a ^ n"
   362   by (cases n) (simp_all add: power_gt1_lemma)
   363 
   364 lemma power_le_imp_le_exp:
   365   assumes gt1: "1 < a"
   366   shows "a ^ m \<le> a ^ n \<Longrightarrow> m \<le> n"
   367 proof (induct m arbitrary: n)
   368   case 0
   369   show ?case by simp
   370 next
   371   case (Suc m)
   372   show ?case
   373   proof (cases n)
   374     case 0
   375     with Suc.prems Suc.hyps have "a * a ^ m \<le> 1" by simp
   376     with gt1 show ?thesis
   377       by (force simp only: power_gt1_lemma
   378           not_less [symmetric])
   379   next
   380     case (Suc n)
   381     with Suc.prems Suc.hyps show ?thesis
   382       by (force dest: mult_left_le_imp_le
   383           simp add: less_trans [OF zero_less_one gt1])
   384   qed
   385 qed
   386 
   387 text{*Surely we can strengthen this? It holds for @{text "0<a<1"} too.*}
   388 lemma power_inject_exp [simp]:
   389   "1 < a \<Longrightarrow> a ^ m = a ^ n \<longleftrightarrow> m = n"
   390   by (force simp add: order_antisym power_le_imp_le_exp)
   391 
   392 text{*Can relax the first premise to @{term "0<a"} in the case of the
   393 natural numbers.*}
   394 lemma power_less_imp_less_exp:
   395   "1 < a \<Longrightarrow> a ^ m < a ^ n \<Longrightarrow> m < n"
   396   by (simp add: order_less_le [of m n] less_le [of "a^m" "a^n"]
   397     power_le_imp_le_exp)
   398 
   399 lemma power_strict_mono [rule_format]:
   400   "a < b \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 < n \<longrightarrow> a ^ n < b ^ n"
   401   by (induct n)
   402    (auto simp add: mult_strict_mono le_less_trans [of 0 a b])
   403 
   404 text{*Lemma for @{text power_strict_decreasing}*}
   405 lemma power_Suc_less:
   406   "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a * a ^ n < a ^ n"
   407   by (induct n)
   408     (auto simp add: mult_strict_left_mono)
   409 
   410 lemma power_strict_decreasing [rule_format]:
   411   "n < N \<Longrightarrow> 0 < a \<Longrightarrow> a < 1 \<longrightarrow> a ^ N < a ^ n"
   412 proof (induct N)
   413   case 0 then show ?case by simp
   414 next
   415   case (Suc N) then show ?case 
   416   apply (auto simp add: power_Suc_less less_Suc_eq)
   417   apply (subgoal_tac "a * a^N < 1 * a^n")
   418   apply simp
   419   apply (rule mult_strict_mono) apply auto
   420   done
   421 qed
   422 
   423 text{*Proof resembles that of @{text power_strict_decreasing}*}
   424 lemma power_decreasing [rule_format]:
   425   "n \<le> N \<Longrightarrow> 0 \<le> a \<Longrightarrow> a \<le> 1 \<longrightarrow> a ^ N \<le> a ^ n"
   426 proof (induct N)
   427   case 0 then show ?case by simp
   428 next
   429   case (Suc N) then show ?case 
   430   apply (auto simp add: le_Suc_eq)
   431   apply (subgoal_tac "a * a^N \<le> 1 * a^n", simp)
   432   apply (rule mult_mono) apply auto
   433   done
   434 qed
   435 
   436 lemma power_Suc_less_one:
   437   "0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a ^ Suc n < 1"
   438   using power_strict_decreasing [of 0 "Suc n" a] by simp
   439 
   440 text{*Proof again resembles that of @{text power_strict_decreasing}*}
   441 lemma power_increasing [rule_format]:
   442   "n \<le> N \<Longrightarrow> 1 \<le> a \<Longrightarrow> a ^ n \<le> a ^ N"
   443 proof (induct N)
   444   case 0 then show ?case by simp
   445 next
   446   case (Suc N) then show ?case 
   447   apply (auto simp add: le_Suc_eq)
   448   apply (subgoal_tac "1 * a^n \<le> a * a^N", simp)
   449   apply (rule mult_mono) apply (auto simp add: order_trans [OF zero_le_one])
   450   done
   451 qed
   452 
   453 text{*Lemma for @{text power_strict_increasing}*}
   454 lemma power_less_power_Suc:
   455   "1 < a \<Longrightarrow> a ^ n < a * a ^ n"
   456   by (induct n) (auto simp add: mult_strict_left_mono less_trans [OF zero_less_one])
   457 
   458 lemma power_strict_increasing [rule_format]:
   459   "n < N \<Longrightarrow> 1 < a \<longrightarrow> a ^ n < a ^ N"
   460 proof (induct N)
   461   case 0 then show ?case by simp
   462 next
   463   case (Suc N) then show ?case 
   464   apply (auto simp add: power_less_power_Suc less_Suc_eq)
   465   apply (subgoal_tac "1 * a^n < a * a^N", simp)
   466   apply (rule mult_strict_mono) apply (auto simp add: less_trans [OF zero_less_one] less_imp_le)
   467   done
   468 qed
   469 
   470 lemma power_increasing_iff [simp]:
   471   "1 < b \<Longrightarrow> b ^ x \<le> b ^ y \<longleftrightarrow> x \<le> y"
   472   by (blast intro: power_le_imp_le_exp power_increasing less_imp_le)
   473 
   474 lemma power_strict_increasing_iff [simp]:
   475   "1 < b \<Longrightarrow> b ^ x < b ^ y \<longleftrightarrow> x < y"
   476 by (blast intro: power_less_imp_less_exp power_strict_increasing) 
   477 
   478 lemma power_le_imp_le_base:
   479   assumes le: "a ^ Suc n \<le> b ^ Suc n"
   480     and ynonneg: "0 \<le> b"
   481   shows "a \<le> b"
   482 proof (rule ccontr)
   483   assume "~ a \<le> b"
   484   then have "b < a" by (simp only: linorder_not_le)
   485   then have "b ^ Suc n < a ^ Suc n"
   486     by (simp only: assms power_strict_mono)
   487   from le and this show False
   488     by (simp add: linorder_not_less [symmetric])
   489 qed
   490 
   491 lemma power_less_imp_less_base:
   492   assumes less: "a ^ n < b ^ n"
   493   assumes nonneg: "0 \<le> b"
   494   shows "a < b"
   495 proof (rule contrapos_pp [OF less])
   496   assume "~ a < b"
   497   hence "b \<le> a" by (simp only: linorder_not_less)
   498   hence "b ^ n \<le> a ^ n" using nonneg by (rule power_mono)
   499   thus "\<not> a ^ n < b ^ n" by (simp only: linorder_not_less)
   500 qed
   501 
   502 lemma power_inject_base:
   503   "a ^ Suc n = b ^ Suc n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a = b"
   504 by (blast intro: power_le_imp_le_base antisym eq_refl sym)
   505 
   506 lemma power_eq_imp_eq_base:
   507   "a ^ n = b ^ n \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 < n \<Longrightarrow> a = b"
   508   by (cases n) (simp_all del: power_Suc, rule power_inject_base)
   509 
   510 lemma power2_le_imp_le:
   511   "x\<^sup>2 \<le> y\<^sup>2 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
   512   unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
   513 
   514 lemma power2_less_imp_less:
   515   "x\<^sup>2 < y\<^sup>2 \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
   516   by (rule power_less_imp_less_base)
   517 
   518 lemma power2_eq_imp_eq:
   519   "x\<^sup>2 = y\<^sup>2 \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
   520   unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base) simp
   521 
   522 end
   523 
   524 context linordered_ring_strict
   525 begin
   526 
   527 lemma sum_squares_eq_zero_iff:
   528   "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   529   by (simp add: add_nonneg_eq_0_iff)
   530 
   531 lemma sum_squares_le_zero_iff:
   532   "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   533   by (simp add: le_less not_sum_squares_lt_zero sum_squares_eq_zero_iff)
   534 
   535 lemma sum_squares_gt_zero_iff:
   536   "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   537   by (simp add: not_le [symmetric] sum_squares_le_zero_iff)
   538 
   539 end
   540 
   541 context linordered_idom
   542 begin
   543 
   544 lemma power_abs:
   545   "abs (a ^ n) = abs a ^ n"
   546   by (induct n) (auto simp add: abs_mult)
   547 
   548 lemma abs_power_minus [simp]:
   549   "abs ((-a) ^ n) = abs (a ^ n)"
   550   by (simp add: power_abs)
   551 
   552 lemma zero_less_power_abs_iff [simp]:
   553   "0 < abs a ^ n \<longleftrightarrow> a \<noteq> 0 \<or> n = 0"
   554 proof (induct n)
   555   case 0 show ?case by simp
   556 next
   557   case (Suc n) show ?case by (auto simp add: Suc zero_less_mult_iff)
   558 qed
   559 
   560 lemma zero_le_power_abs [simp]:
   561   "0 \<le> abs a ^ n"
   562   by (rule zero_le_power [OF abs_ge_zero])
   563 
   564 lemma zero_le_power2 [simp]:
   565   "0 \<le> a\<^sup>2"
   566   by (simp add: power2_eq_square)
   567 
   568 lemma zero_less_power2 [simp]:
   569   "0 < a\<^sup>2 \<longleftrightarrow> a \<noteq> 0"
   570   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
   571 
   572 lemma power2_less_0 [simp]:
   573   "\<not> a\<^sup>2 < 0"
   574   by (force simp add: power2_eq_square mult_less_0_iff)
   575 
   576 lemma power2_less_eq_zero_iff [simp]:
   577   "a\<^sup>2 \<le> 0 \<longleftrightarrow> a = 0"
   578   by (simp add: le_less)
   579 
   580 lemma abs_power2 [simp]:
   581   "abs (a\<^sup>2) = a\<^sup>2"
   582   by (simp add: power2_eq_square abs_mult abs_mult_self)
   583 
   584 lemma power2_abs [simp]:
   585   "(abs a)\<^sup>2 = a\<^sup>2"
   586   by (simp add: power2_eq_square abs_mult_self)
   587 
   588 lemma odd_power_less_zero:
   589   "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
   590 proof (induct n)
   591   case 0
   592   then show ?case by simp
   593 next
   594   case (Suc n)
   595   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
   596     by (simp add: ac_simps power_add power2_eq_square)
   597   thus ?case
   598     by (simp del: power_Suc add: Suc mult_less_0_iff mult_neg_neg)
   599 qed
   600 
   601 lemma odd_0_le_power_imp_0_le:
   602   "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
   603   using odd_power_less_zero [of a n]
   604     by (force simp add: linorder_not_less [symmetric]) 
   605 
   606 lemma zero_le_even_power'[simp]:
   607   "0 \<le> a ^ (2*n)"
   608 proof (induct n)
   609   case 0
   610     show ?case by simp
   611 next
   612   case (Suc n)
   613     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
   614       by (simp add: ac_simps power_add power2_eq_square)
   615     thus ?case
   616       by (simp add: Suc zero_le_mult_iff)
   617 qed
   618 
   619 lemma sum_power2_ge_zero:
   620   "0 \<le> x\<^sup>2 + y\<^sup>2"
   621   by (intro add_nonneg_nonneg zero_le_power2)
   622 
   623 lemma not_sum_power2_lt_zero:
   624   "\<not> x\<^sup>2 + y\<^sup>2 < 0"
   625   unfolding not_less by (rule sum_power2_ge_zero)
   626 
   627 lemma sum_power2_eq_zero_iff:
   628   "x\<^sup>2 + y\<^sup>2 = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   629   unfolding power2_eq_square by (simp add: add_nonneg_eq_0_iff)
   630 
   631 lemma sum_power2_le_zero_iff:
   632   "x\<^sup>2 + y\<^sup>2 \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
   633   by (simp add: le_less sum_power2_eq_zero_iff not_sum_power2_lt_zero)
   634 
   635 lemma sum_power2_gt_zero_iff:
   636   "0 < x\<^sup>2 + y\<^sup>2 \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
   637   unfolding not_le [symmetric] by (simp add: sum_power2_le_zero_iff)
   638 
   639 end
   640 
   641 
   642 subsection {* Miscellaneous rules *}
   643 
   644 lemma self_le_power:
   645   fixes x::"'a::linordered_semidom" 
   646   shows "1 \<le> x \<Longrightarrow> 0 < n \<Longrightarrow> x \<le> x ^ n"
   647   using power_increasing[of 1 n x] power_one_right[of x] by auto
   648 
   649 lemma power_eq_if: "p ^ m = (if m=0 then 1 else p * (p ^ (m - 1)))"
   650   unfolding One_nat_def by (cases m) simp_all
   651 
   652 lemma (in comm_semiring_1) power2_sum:
   653   "(x + y)\<^sup>2 = x\<^sup>2 + y\<^sup>2 + 2 * x * y"
   654   by (simp add: algebra_simps power2_eq_square mult_2_right)
   655 
   656 lemma (in comm_ring_1) power2_diff:
   657   "(x - y)\<^sup>2 = x\<^sup>2 + y\<^sup>2 - 2 * x * y"
   658   by (simp add: algebra_simps power2_eq_square mult_2_right)
   659 
   660 lemma power_0_Suc [simp]:
   661   "(0::'a::{power, semiring_0}) ^ Suc n = 0"
   662   by simp
   663 
   664 text{*It looks plausible as a simprule, but its effect can be strange.*}
   665 lemma power_0_left:
   666   "0 ^ n = (if n = 0 then 1 else (0::'a::{power, semiring_0}))"
   667   by (induct n) simp_all
   668 
   669 lemma (in field) power_diff:
   670   assumes nz: "a \<noteq> 0"
   671   shows "n \<le> m \<Longrightarrow> a ^ (m - n) = a ^ m / a ^ n"
   672   by (induct m n rule: diff_induct) (simp_all add: nz field_power_not_zero)
   673 
   674 text{*Perhaps these should be simprules.*}
   675 lemma power_inverse:
   676   fixes a :: "'a::division_ring_inverse_zero"
   677   shows "inverse (a ^ n) = inverse a ^ n"
   678 apply (cases "a = 0")
   679 apply (simp add: power_0_left)
   680 apply (simp add: nonzero_power_inverse)
   681 done (* TODO: reorient or rename to inverse_power *)
   682 
   683 lemma power_one_over:
   684   "1 / (a::'a::{field_inverse_zero, power}) ^ n =  (1 / a) ^ n"
   685   by (simp add: divide_inverse) (rule power_inverse)
   686 
   687 lemma power_divide [field_simps, divide_simps]:
   688   "(a / b) ^ n = (a::'a::field_inverse_zero) ^ n / b ^ n"
   689 apply (cases "b = 0")
   690 apply (simp add: power_0_left)
   691 apply (rule nonzero_power_divide)
   692 apply assumption
   693 done
   694 
   695 text {* Simprules for comparisons where common factors can be cancelled. *}
   696 
   697 lemmas zero_compare_simps =
   698     add_strict_increasing add_strict_increasing2 add_increasing
   699     zero_le_mult_iff zero_le_divide_iff 
   700     zero_less_mult_iff zero_less_divide_iff 
   701     mult_le_0_iff divide_le_0_iff 
   702     mult_less_0_iff divide_less_0_iff 
   703     zero_le_power2 power2_less_0
   704 
   705 
   706 subsection {* Exponentiation for the Natural Numbers *}
   707 
   708 lemma nat_one_le_power [simp]:
   709   "Suc 0 \<le> i \<Longrightarrow> Suc 0 \<le> i ^ n"
   710   by (rule one_le_power [of i n, unfolded One_nat_def])
   711 
   712 lemma nat_zero_less_power_iff [simp]:
   713   "x ^ n > 0 \<longleftrightarrow> x > (0::nat) \<or> n = 0"
   714   by (induct n) auto
   715 
   716 lemma nat_power_eq_Suc_0_iff [simp]: 
   717   "x ^ m = Suc 0 \<longleftrightarrow> m = 0 \<or> x = Suc 0"
   718   by (induct m) auto
   719 
   720 lemma power_Suc_0 [simp]:
   721   "Suc 0 ^ n = Suc 0"
   722   by simp
   723 
   724 text{*Valid for the naturals, but what if @{text"0<i<1"}?
   725 Premises cannot be weakened: consider the case where @{term "i=0"},
   726 @{term "m=1"} and @{term "n=0"}.*}
   727 lemma nat_power_less_imp_less:
   728   assumes nonneg: "0 < (i\<Colon>nat)"
   729   assumes less: "i ^ m < i ^ n"
   730   shows "m < n"
   731 proof (cases "i = 1")
   732   case True with less power_one [where 'a = nat] show ?thesis by simp
   733 next
   734   case False with nonneg have "1 < i" by auto
   735   from power_strict_increasing_iff [OF this] less show ?thesis ..
   736 qed
   737 
   738 lemma power_dvd_imp_le:
   739   "i ^ m dvd i ^ n \<Longrightarrow> (1::nat) < i \<Longrightarrow> m \<le> n"
   740   apply (rule power_le_imp_le_exp, assumption)
   741   apply (erule dvd_imp_le, simp)
   742   done
   743 
   744 lemma power2_nat_le_eq_le:
   745   fixes m n :: nat
   746   shows "m\<^sup>2 \<le> n\<^sup>2 \<longleftrightarrow> m \<le> n"
   747   by (auto intro: power2_le_imp_le power_mono)
   748 
   749 lemma power2_nat_le_imp_le:
   750   fixes m n :: nat
   751   assumes "m\<^sup>2 \<le> n"
   752   shows "m \<le> n"
   753 proof (cases m)
   754   case 0 then show ?thesis by simp
   755 next
   756   case (Suc k)
   757   show ?thesis
   758   proof (rule ccontr)
   759     assume "\<not> m \<le> n"
   760     then have "n < m" by simp
   761     with assms Suc show False
   762       by (auto simp add: algebra_simps) (simp add: power2_eq_square)
   763   qed
   764 qed
   765 
   766 subsubsection {* Cardinality of the Powerset *}
   767 
   768 lemma card_UNIV_bool [simp]: "card (UNIV :: bool set) = 2"
   769   unfolding UNIV_bool by simp
   770 
   771 lemma card_Pow: "finite A \<Longrightarrow> card (Pow A) = 2 ^ card A"
   772 proof (induct rule: finite_induct)
   773   case empty 
   774     show ?case by auto
   775 next
   776   case (insert x A)
   777   then have "inj_on (insert x) (Pow A)" 
   778     unfolding inj_on_def by (blast elim!: equalityE)
   779   then have "card (Pow A) + card (insert x ` Pow A) = 2 * 2 ^ card A" 
   780     by (simp add: mult_2 card_image Pow_insert insert.hyps)
   781   then show ?case using insert
   782     apply (simp add: Pow_insert)
   783     apply (subst card_Un_disjoint, auto)
   784     done
   785 qed
   786 
   787 
   788 subsubsection {* Generalized sum over a set *}
   789 
   790 lemma setsum_zero_power [simp]:
   791   fixes c :: "nat \<Rightarrow> 'a::division_ring"
   792   shows "(\<Sum>i\<in>A. c i * 0^i) = (if finite A \<and> 0 \<in> A then c 0 else 0)"
   793 apply (cases "finite A")
   794   by (induction A rule: finite_induct) auto
   795 
   796 lemma setsum_zero_power' [simp]:
   797   fixes c :: "nat \<Rightarrow> 'a::field"
   798   shows "(\<Sum>i\<in>A. c i * 0^i / d i) = (if finite A \<and> 0 \<in> A then c 0 / d 0 else 0)"
   799   using setsum_zero_power [of "\<lambda>i. c i / d i" A]
   800   by auto
   801 
   802 
   803 subsubsection {* Generalized product over a set *}
   804 
   805 lemma setprod_constant: "finite A ==> (\<Prod>x\<in> A. (y::'a::{comm_monoid_mult})) = y^(card A)"
   806 apply (erule finite_induct)
   807 apply auto
   808 done
   809 
   810 lemma setprod_power_distrib:
   811   fixes f :: "'a \<Rightarrow> 'b::comm_semiring_1"
   812   shows "setprod f A ^ n = setprod (\<lambda>x. (f x) ^ n) A"
   813 proof (cases "finite A") 
   814   case True then show ?thesis 
   815     by (induct A rule: finite_induct) (auto simp add: power_mult_distrib)
   816 next
   817   case False then show ?thesis 
   818     by simp
   819 qed
   820 
   821 lemma power_setsum:
   822   "c ^ (\<Sum>a\<in>A. f a) = (\<Prod>a\<in>A. c ^ f a)"
   823   by (induct A rule: infinite_finite_induct) (simp_all add: power_add)
   824 
   825 lemma setprod_gen_delta:
   826   assumes fS: "finite S"
   827   shows "setprod (\<lambda>k. if k=a then b k else c) S = (if a \<in> S then (b a ::'a::comm_monoid_mult) * c^ (card S - 1) else c^ card S)"
   828 proof-
   829   let ?f = "(\<lambda>k. if k=a then b k else c)"
   830   {assume a: "a \<notin> S"
   831     hence "\<forall> k\<in> S. ?f k = c" by simp
   832     hence ?thesis  using a setprod_constant[OF fS, of c] by simp }
   833   moreover 
   834   {assume a: "a \<in> S"
   835     let ?A = "S - {a}"
   836     let ?B = "{a}"
   837     have eq: "S = ?A \<union> ?B" using a by blast 
   838     have dj: "?A \<inter> ?B = {}" by simp
   839     from fS have fAB: "finite ?A" "finite ?B" by auto  
   840     have fA0:"setprod ?f ?A = setprod (\<lambda>i. c) ?A"
   841       apply (rule setprod.cong) by auto
   842     have cA: "card ?A = card S - 1" using fS a by auto
   843     have fA1: "setprod ?f ?A = c ^ card ?A"  unfolding fA0 apply (rule setprod_constant) using fS by auto
   844     have "setprod ?f ?A * setprod ?f ?B = setprod ?f S"
   845       using setprod.union_disjoint[OF fAB dj, of ?f, unfolded eq[symmetric]]
   846       by simp
   847     then have ?thesis using a cA
   848       by (simp add: fA1 field_simps cong add: setprod.cong cong del: if_weak_cong)}
   849   ultimately show ?thesis by blast
   850 qed
   851 
   852 subsection {* Code generator tweak *}
   853 
   854 lemma power_power_power [code]:
   855   "power = power.power (1::'a::{power}) (op *)"
   856   unfolding power_def power.power_def ..
   857 
   858 declare power.power.simps [code]
   859 
   860 code_identifier
   861   code_module Power \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
   862 
   863 end
   864