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