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