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