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