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