src/HOL/Parity.thy
author bulwahn
Sun Aug 27 06:27:01 2017 +0200 (23 months ago)
changeset 66582 2b49d4888cb8
parent 64785 ae0bbc8e45ad
child 66808 1907167b6038
permissions -rw-r--r--
another fact on (- 1) ^ _
     1 (*  Title:      HOL/Parity.thy
     2     Author:     Jeremy Avigad
     3     Author:     Jacques D. Fleuriot
     4 *)
     5 
     6 section \<open>Parity in rings and semirings\<close>
     7 
     8 theory Parity
     9   imports Nat_Transfer Euclidean_Division
    10 begin
    11 
    12 subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close>
    13 
    14 class semiring_parity = comm_semiring_1_cancel + numeral +
    15   assumes odd_one [simp]: "\<not> 2 dvd 1"
    16   assumes odd_even_add: "\<not> 2 dvd a \<Longrightarrow> \<not> 2 dvd b \<Longrightarrow> 2 dvd a + b"
    17   assumes even_multD: "2 dvd a * b \<Longrightarrow> 2 dvd a \<or> 2 dvd b"
    18   assumes odd_ex_decrement: "\<not> 2 dvd a \<Longrightarrow> \<exists>b. a = b + 1"
    19 begin
    20 
    21 subclass semiring_numeral ..
    22 
    23 abbreviation even :: "'a \<Rightarrow> bool"
    24   where "even a \<equiv> 2 dvd a"
    25 
    26 abbreviation odd :: "'a \<Rightarrow> bool"
    27   where "odd a \<equiv> \<not> 2 dvd a"
    28 
    29 lemma even_zero [simp]: "even 0"
    30   by (fact dvd_0_right)
    31 
    32 lemma even_plus_one_iff [simp]: "even (a + 1) \<longleftrightarrow> odd a"
    33   by (auto simp add: dvd_add_right_iff intro: odd_even_add)
    34 
    35 lemma evenE [elim?]:
    36   assumes "even a"
    37   obtains b where "a = 2 * b"
    38   using assms by (rule dvdE)
    39 
    40 lemma oddE [elim?]:
    41   assumes "odd a"
    42   obtains b where "a = 2 * b + 1"
    43 proof -
    44   from assms obtain b where *: "a = b + 1"
    45     by (blast dest: odd_ex_decrement)
    46   with assms have "even (b + 2)" by simp
    47   then have "even b" by simp
    48   then obtain c where "b = 2 * c" ..
    49   with * have "a = 2 * c + 1" by simp
    50   with that show thesis .
    51 qed
    52 
    53 lemma even_times_iff [simp]: "even (a * b) \<longleftrightarrow> even a \<or> even b"
    54   by (auto dest: even_multD)
    55 
    56 lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
    57 proof -
    58   have "even (2 * numeral n)"
    59     unfolding even_times_iff by simp
    60   then have "even (numeral n + numeral n)"
    61     unfolding mult_2 .
    62   then show ?thesis
    63     unfolding numeral.simps .
    64 qed
    65 
    66 lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
    67 proof
    68   assume "even (numeral (num.Bit1 n))"
    69   then have "even (numeral n + numeral n + 1)"
    70     unfolding numeral.simps .
    71   then have "even (2 * numeral n + 1)"
    72     unfolding mult_2 .
    73   then have "2 dvd numeral n * 2 + 1"
    74     by (simp add: ac_simps)
    75   then have "2 dvd 1"
    76     using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp
    77   then show False by simp
    78 qed
    79 
    80 lemma even_add [simp]: "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
    81   by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)
    82 
    83 lemma odd_add [simp]: "odd (a + b) \<longleftrightarrow> (\<not> (odd a \<longleftrightarrow> odd b))"
    84   by simp
    85 
    86 lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"
    87   by (induct n) auto
    88 
    89 end
    90 
    91 class ring_parity = ring + semiring_parity
    92 begin
    93 
    94 subclass comm_ring_1 ..
    95 
    96 lemma even_minus [simp]: "even (- a) \<longleftrightarrow> even a"
    97   by (fact dvd_minus_iff)
    98 
    99 lemma even_diff [simp]: "even (a - b) \<longleftrightarrow> even (a + b)"
   100   using even_add [of a "- b"] by simp
   101 
   102 end
   103 
   104 
   105 subsection \<open>Instances for @{typ nat} and @{typ int}\<close>
   106 
   107 lemma even_Suc_Suc_iff [simp]: "2 dvd Suc (Suc n) \<longleftrightarrow> 2 dvd n"
   108   using dvd_add_triv_right_iff [of 2 n] by simp
   109 
   110 lemma even_Suc [simp]: "2 dvd Suc n \<longleftrightarrow> \<not> 2 dvd n"
   111   by (induct n) auto
   112 
   113 lemma even_diff_nat [simp]: "2 dvd (m - n) \<longleftrightarrow> m < n \<or> 2 dvd (m + n)"
   114   for m n :: nat
   115 proof (cases "n \<le> m")
   116   case True
   117   then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
   118   moreover have "2 dvd (m - n) \<longleftrightarrow> 2 dvd (m - n + n * 2)" by simp
   119   ultimately have "2 dvd (m - n) \<longleftrightarrow> 2 dvd (m + n)" by (simp only:)
   120   then show ?thesis by auto
   121 next
   122   case False
   123   then show ?thesis by simp
   124 qed
   125 
   126 instance nat :: semiring_parity
   127 proof
   128   show "\<not> 2 dvd (1 :: nat)"
   129     by (rule notI, erule dvdE) simp
   130 next
   131   fix m n :: nat
   132   assume "\<not> 2 dvd m"
   133   moreover assume "\<not> 2 dvd n"
   134   ultimately have *: "2 dvd Suc m \<and> 2 dvd Suc n"
   135     by simp
   136   then have "2 dvd (Suc m + Suc n)"
   137     by (blast intro: dvd_add)
   138   also have "Suc m + Suc n = m + n + 2"
   139     by simp
   140   finally show "2 dvd (m + n)"
   141     using dvd_add_triv_right_iff [of 2 "m + n"] by simp
   142 next
   143   fix m n :: nat
   144   assume *: "2 dvd (m * n)"
   145   show "2 dvd m \<or> 2 dvd n"
   146   proof (rule disjCI)
   147     assume "\<not> 2 dvd n"
   148     then have "2 dvd (Suc n)" by simp
   149     then obtain r where "Suc n = 2 * r" ..
   150     moreover from * obtain s where "m * n = 2 * s" ..
   151     then have "2 * s + m = m * Suc n" by simp
   152     ultimately have " 2 * s + m = 2 * (m * r)"
   153       by (simp add: algebra_simps)
   154     then have "m = 2 * (m * r - s)" by simp
   155     then show "2 dvd m" ..
   156   qed
   157 next
   158   fix n :: nat
   159   assume "\<not> 2 dvd n"
   160   then show "\<exists>m. n = m + 1"
   161     by (cases n) simp_all
   162 qed
   163 
   164 lemma odd_pos: "odd n \<Longrightarrow> 0 < n"
   165   for n :: nat
   166   by (auto elim: oddE)
   167 
   168 lemma Suc_double_not_eq_double: "Suc (2 * m) \<noteq> 2 * n"
   169   for m n :: nat
   170 proof
   171   assume "Suc (2 * m) = 2 * n"
   172   moreover have "odd (Suc (2 * m))" and "even (2 * n)"
   173     by simp_all
   174   ultimately show False by simp
   175 qed
   176 
   177 lemma double_not_eq_Suc_double: "2 * m \<noteq> Suc (2 * n)"
   178   for m n :: nat
   179   using Suc_double_not_eq_double [of n m] by simp
   180 
   181 lemma even_diff_iff [simp]: "2 dvd (k - l) \<longleftrightarrow> 2 dvd (k + l)"
   182   for k l :: int
   183   using dvd_add_times_triv_right_iff [of 2 "k - l" l] by (simp add: mult_2_right)
   184 
   185 lemma even_abs_add_iff [simp]: "2 dvd (\<bar>k\<bar> + l) \<longleftrightarrow> 2 dvd (k + l)"
   186   for k l :: int
   187   by (cases "k \<ge> 0") (simp_all add: ac_simps)
   188 
   189 lemma even_add_abs_iff [simp]: "2 dvd (k + \<bar>l\<bar>) \<longleftrightarrow> 2 dvd (k + l)"
   190   for k l :: int
   191   using even_abs_add_iff [of l k] by (simp add: ac_simps)
   192 
   193 lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
   194   by (auto elim: oddE)
   195 
   196 instance int :: ring_parity
   197 proof
   198   show "\<not> 2 dvd (1 :: int)"
   199     by (simp add: dvd_int_unfold_dvd_nat)
   200 next
   201   fix k l :: int
   202   assume "\<not> 2 dvd k"
   203   moreover assume "\<not> 2 dvd l"
   204   ultimately have "2 dvd (nat \<bar>k\<bar> + nat \<bar>l\<bar>)"
   205     by (auto simp add: dvd_int_unfold_dvd_nat intro: odd_even_add)
   206   then have "2 dvd (\<bar>k\<bar> + \<bar>l\<bar>)"
   207     by (simp add: dvd_int_unfold_dvd_nat nat_add_distrib)
   208   then show "2 dvd (k + l)"
   209     by simp
   210 next
   211   fix k l :: int
   212   assume "2 dvd (k * l)"
   213   then show "2 dvd k \<or> 2 dvd l"
   214     by (simp add: dvd_int_unfold_dvd_nat even_multD nat_abs_mult_distrib)
   215 next
   216   fix k :: int
   217   have "k = (k - 1) + 1" by simp
   218   then show "\<exists>l. k = l + 1" ..
   219 qed
   220 
   221 lemma even_int_iff [simp]: "even (int n) \<longleftrightarrow> even n"
   222   by (simp add: dvd_int_iff)
   223 
   224 lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
   225   by (simp add: even_int_iff [symmetric])
   226 
   227 
   228 subsection \<open>Parity and powers\<close>
   229 
   230 context ring_1
   231 begin
   232 
   233 lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
   234   by (auto elim: evenE)
   235 
   236 lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
   237   by (auto elim: oddE)
   238 
   239 lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
   240   by simp
   241 
   242 lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
   243   by simp
   244 
   245 lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)"
   246   by (cases "even (n + k)") auto
   247 
   248 end
   249 
   250 context linordered_idom
   251 begin
   252 
   253 lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
   254   by (auto elim: evenE)
   255 
   256 lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
   257   by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
   258 
   259 lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
   260   by (auto simp add: zero_le_even_power zero_le_odd_power)
   261 
   262 lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
   263 proof -
   264   have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
   265     unfolding power_eq_0_iff [of a n, symmetric] by blast
   266   show ?thesis
   267     unfolding less_le zero_le_power_eq by auto
   268 qed
   269 
   270 lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
   271   unfolding not_le [symmetric] zero_le_power_eq by auto
   272 
   273 lemma power_le_zero_eq: "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"
   274   unfolding not_less [symmetric] zero_less_power_eq by auto
   275 
   276 lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
   277   using power_abs [of a n] by (simp add: zero_le_even_power)
   278 
   279 lemma power_mono_even:
   280   assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
   281   shows "a ^ n \<le> b ^ n"
   282 proof -
   283   have "0 \<le> \<bar>a\<bar>" by auto
   284   with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"
   285     by (rule power_mono)
   286   with \<open>even n\<close> show ?thesis
   287     by (simp add: power_even_abs)
   288 qed
   289 
   290 lemma power_mono_odd:
   291   assumes "odd n" and "a \<le> b"
   292   shows "a ^ n \<le> b ^ n"
   293 proof (cases "b < 0")
   294   case True
   295   with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto
   296   then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
   297   with \<open>odd n\<close> show ?thesis by simp
   298 next
   299   case False
   300   then have "0 \<le> b" by auto
   301   show ?thesis
   302   proof (cases "a < 0")
   303     case True
   304     then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto
   305     then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto
   306     moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto
   307     ultimately show ?thesis by auto
   308   next
   309     case False
   310     then have "0 \<le> a" by auto
   311     with \<open>a \<le> b\<close> show ?thesis
   312       using power_mono by auto
   313   qed
   314 qed
   315 
   316 lemma (in comm_ring_1) uminus_power_if: "(- x) ^ n = (if even n then x^n else - (x ^ n))"
   317   by auto
   318 
   319 text \<open>Simplify, when the exponent is a numeral\<close>
   320 
   321 lemma zero_le_power_eq_numeral [simp]:
   322   "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"
   323   by (fact zero_le_power_eq)
   324 
   325 lemma zero_less_power_eq_numeral [simp]:
   326   "0 < a ^ numeral w \<longleftrightarrow>
   327     numeral w = (0 :: nat) \<or>
   328     even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
   329     odd (numeral w :: nat) \<and> 0 < a"
   330   by (fact zero_less_power_eq)
   331 
   332 lemma power_le_zero_eq_numeral [simp]:
   333   "a ^ numeral w \<le> 0 \<longleftrightarrow>
   334     (0 :: nat) < numeral w \<and>
   335     (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
   336   by (fact power_le_zero_eq)
   337 
   338 lemma power_less_zero_eq_numeral [simp]:
   339   "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
   340   by (fact power_less_zero_eq)
   341 
   342 lemma power_even_abs_numeral [simp]:
   343   "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"
   344   by (fact power_even_abs)
   345 
   346 end
   347 
   348 
   349 subsubsection \<open>Tool setup\<close>
   350 
   351 declare transfer_morphism_int_nat [transfer add return: even_int_iff]
   352 
   353 end