src/HOL/Parity.thy
author haftmann
Thu Oct 23 14:04:05 2014 +0200 (2014-10-23)
changeset 58770 ae5e9b4f8daf
parent 58769 70fff47875cd
child 58771 0997ea62e868
permissions -rw-r--r--
downshift of theory Parity in the hierarchy
wenzelm@41959
     1
(*  Title:      HOL/Parity.thy
wenzelm@41959
     2
    Author:     Jeremy Avigad
wenzelm@41959
     3
    Author:     Jacques D. Fleuriot
wenzelm@21256
     4
*)
wenzelm@21256
     5
wenzelm@21256
     6
header {* Even and Odd for int and nat *}
wenzelm@21256
     7
wenzelm@21256
     8
theory Parity
haftmann@58770
     9
imports Presburger
wenzelm@21256
    10
begin
wenzelm@21256
    11
haftmann@58678
    12
subsection {* Preliminaries about divisibility on @{typ nat} and @{typ int} *}
haftmann@58678
    13
haftmann@58678
    14
lemma two_dvd_Suc_Suc_iff [simp]:
haftmann@58678
    15
  "2 dvd Suc (Suc n) \<longleftrightarrow> 2 dvd n"
haftmann@58678
    16
  using dvd_add_triv_right_iff [of 2 n] by simp
haftmann@58678
    17
haftmann@58678
    18
lemma two_dvd_Suc_iff:
haftmann@58678
    19
  "2 dvd Suc n \<longleftrightarrow> \<not> 2 dvd n"
haftmann@58678
    20
  by (induct n) auto
haftmann@58678
    21
haftmann@58687
    22
lemma two_dvd_diff_nat_iff:
haftmann@58687
    23
  fixes m n :: nat
haftmann@58687
    24
  shows "2 dvd m - n \<longleftrightarrow> m < n \<or> 2 dvd m + n"
haftmann@58687
    25
proof (cases "n \<le> m")
haftmann@58687
    26
  case True
haftmann@58770
    27
  then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
haftmann@58687
    28
  moreover have "2 dvd m - n \<longleftrightarrow> 2 dvd m - n + n * 2" by simp
haftmann@58687
    29
  ultimately have "2 dvd m - n \<longleftrightarrow> 2 dvd m + n" by (simp only:)
haftmann@58687
    30
  then show ?thesis by auto
haftmann@58687
    31
next
haftmann@58687
    32
  case False
haftmann@58687
    33
  then show ?thesis by simp
haftmann@58687
    34
qed 
haftmann@58687
    35
  
haftmann@58678
    36
lemma two_dvd_diff_iff:
haftmann@58678
    37
  fixes k l :: int
haftmann@58678
    38
  shows "2 dvd k - l \<longleftrightarrow> 2 dvd k + l"
haftmann@58678
    39
  using dvd_add_times_triv_right_iff [of 2 "k - l" l] by (simp add: ac_simps)
haftmann@58678
    40
haftmann@58678
    41
lemma two_dvd_abs_add_iff:
haftmann@58678
    42
  fixes k l :: int
haftmann@58678
    43
  shows "2 dvd \<bar>k\<bar> + l \<longleftrightarrow> 2 dvd k + l"
haftmann@58678
    44
  by (cases "k \<ge> 0") (simp_all add: two_dvd_diff_iff ac_simps)
haftmann@58678
    45
haftmann@58678
    46
lemma two_dvd_add_abs_iff:
haftmann@58678
    47
  fixes k l :: int
haftmann@58678
    48
  shows "2 dvd k + \<bar>l\<bar> \<longleftrightarrow> 2 dvd k + l"
haftmann@58678
    49
  using two_dvd_abs_add_iff [of l k] by (simp add: ac_simps)
haftmann@58678
    50
haftmann@58678
    51
haftmann@58678
    52
subsection {* Ring structures with parity *}
haftmann@58678
    53
haftmann@58678
    54
class semiring_parity = semiring_dvd + semiring_numeral +
haftmann@58678
    55
  assumes two_not_dvd_one [simp]: "\<not> 2 dvd 1"
haftmann@58678
    56
  assumes not_dvd_not_dvd_dvd_add: "\<not> 2 dvd a \<Longrightarrow> \<not> 2 dvd b \<Longrightarrow> 2 dvd a + b"
haftmann@58678
    57
  assumes two_is_prime: "2 dvd a * b \<Longrightarrow> 2 dvd a \<or> 2 dvd b"
haftmann@58680
    58
  assumes not_dvd_ex_decrement: "\<not> 2 dvd a \<Longrightarrow> \<exists>b. a = b + 1"
haftmann@58678
    59
begin
haftmann@58678
    60
haftmann@58678
    61
lemma two_dvd_plus_one_iff [simp]:
haftmann@58678
    62
  "2 dvd a + 1 \<longleftrightarrow> \<not> 2 dvd a"
haftmann@58678
    63
  by (auto simp add: dvd_add_right_iff intro: not_dvd_not_dvd_dvd_add)
haftmann@58678
    64
haftmann@58680
    65
lemma not_two_dvdE [elim?]:
haftmann@58680
    66
  assumes "\<not> 2 dvd a"
haftmann@58680
    67
  obtains b where "a = 2 * b + 1"
haftmann@58680
    68
proof -
haftmann@58680
    69
  from assms obtain b where *: "a = b + 1"
haftmann@58680
    70
    by (blast dest: not_dvd_ex_decrement)
haftmann@58680
    71
  with assms have "2 dvd b + 2" by simp
haftmann@58680
    72
  then have "2 dvd b" by simp
haftmann@58680
    73
  then obtain c where "b = 2 * c" ..
haftmann@58680
    74
  with * have "a = 2 * c + 1" by simp
haftmann@58680
    75
  with that show thesis .
haftmann@58680
    76
qed
haftmann@58680
    77
haftmann@58678
    78
end
haftmann@58678
    79
haftmann@58678
    80
instance nat :: semiring_parity
haftmann@58678
    81
proof
haftmann@58678
    82
  show "\<not> (2 :: nat) dvd 1"
haftmann@58678
    83
    by (rule notI, erule dvdE) simp
haftmann@58678
    84
next
haftmann@58678
    85
  fix m n :: nat
haftmann@58678
    86
  assume "\<not> 2 dvd m"
haftmann@58678
    87
  moreover assume "\<not> 2 dvd n"
haftmann@58678
    88
  ultimately have *: "2 dvd Suc m \<and> 2 dvd Suc n"
haftmann@58678
    89
    by (simp add: two_dvd_Suc_iff)
haftmann@58678
    90
  then have "2 dvd Suc m + Suc n"
haftmann@58678
    91
    by (blast intro: dvd_add)
haftmann@58678
    92
  also have "Suc m + Suc n = m + n + 2"
haftmann@58678
    93
    by simp
haftmann@58678
    94
  finally show "2 dvd m + n"
haftmann@58678
    95
    using dvd_add_triv_right_iff [of 2 "m + n"] by simp
haftmann@58678
    96
next
haftmann@58678
    97
  fix m n :: nat
haftmann@58678
    98
  assume *: "2 dvd m * n"
haftmann@58678
    99
  show "2 dvd m \<or> 2 dvd n"
haftmann@58678
   100
  proof (rule disjCI)
haftmann@58678
   101
    assume "\<not> 2 dvd n"
haftmann@58678
   102
    then have "2 dvd Suc n" by (simp add: two_dvd_Suc_iff)
haftmann@58678
   103
    then obtain r where "Suc n = 2 * r" ..
haftmann@58678
   104
    moreover from * obtain s where "m * n = 2 * s" ..
haftmann@58678
   105
    then have "2 * s + m = m * Suc n" by simp
haftmann@58770
   106
    ultimately have " 2 * s + m = 2 * (m * r)" by (simp add: algebra_simps)
haftmann@58678
   107
    then have "m = 2 * (m * r - s)" by simp
haftmann@58678
   108
    then show "2 dvd m" ..
haftmann@58678
   109
  qed
haftmann@58680
   110
next
haftmann@58680
   111
  fix n :: nat
haftmann@58680
   112
  assume "\<not> 2 dvd n"
haftmann@58680
   113
  then show "\<exists>m. n = m + 1"
haftmann@58680
   114
    by (cases n) simp_all
haftmann@58678
   115
qed
haftmann@58678
   116
haftmann@58678
   117
class ring_parity = comm_ring_1 + semiring_parity
haftmann@58678
   118
haftmann@58678
   119
instance int :: ring_parity
haftmann@58678
   120
proof
haftmann@58678
   121
  show "\<not> (2 :: int) dvd 1" by (simp add: dvd_int_unfold_dvd_nat)
haftmann@58678
   122
  fix k l :: int
haftmann@58678
   123
  assume "\<not> 2 dvd k"
haftmann@58678
   124
  moreover assume "\<not> 2 dvd l"
haftmann@58678
   125
  ultimately have "2 dvd nat \<bar>k\<bar> + nat \<bar>l\<bar>" 
haftmann@58678
   126
    by (auto simp add: dvd_int_unfold_dvd_nat intro: not_dvd_not_dvd_dvd_add)
haftmann@58678
   127
  then have "2 dvd \<bar>k\<bar> + \<bar>l\<bar>"
haftmann@58678
   128
    by (simp add: dvd_int_unfold_dvd_nat nat_add_distrib)
haftmann@58678
   129
  then show "2 dvd k + l"
haftmann@58678
   130
    by (simp add: two_dvd_abs_add_iff two_dvd_add_abs_iff)
haftmann@58680
   131
next
haftmann@58680
   132
  fix k l :: int
haftmann@58680
   133
  assume "2 dvd k * l"
haftmann@58680
   134
  then show "2 dvd k \<or> 2 dvd l"
haftmann@58680
   135
    by (simp add: dvd_int_unfold_dvd_nat two_is_prime nat_abs_mult_distrib)
haftmann@58680
   136
next
haftmann@58680
   137
  fix k :: int
haftmann@58680
   138
  have "k = (k - 1) + 1" by simp
haftmann@58680
   139
  then show "\<exists>l. k = l + 1" ..
haftmann@58680
   140
qed
haftmann@58678
   141
haftmann@58678
   142
context semiring_div_parity
haftmann@58678
   143
begin
haftmann@58678
   144
haftmann@58678
   145
subclass semiring_parity
haftmann@58678
   146
proof (unfold_locales, unfold dvd_eq_mod_eq_0 not_mod_2_eq_0_eq_1)
haftmann@58678
   147
  fix a b c
haftmann@58678
   148
  show "(c * a + b) mod a = 0 \<longleftrightarrow> b mod a = 0"
haftmann@58678
   149
    by simp
haftmann@58678
   150
next
haftmann@58678
   151
  fix a b c
haftmann@58678
   152
  assume "(b + c) mod a = 0"
haftmann@58678
   153
  with mod_add_eq [of b c a]
haftmann@58678
   154
  have "(b mod a + c mod a) mod a = 0"
haftmann@58678
   155
    by simp
haftmann@58678
   156
  moreover assume "b mod a = 0"
haftmann@58678
   157
  ultimately show "c mod a = 0"
haftmann@58678
   158
    by simp
haftmann@58678
   159
next
haftmann@58678
   160
  show "1 mod 2 = 1"
haftmann@58678
   161
    by (fact one_mod_two_eq_one)
haftmann@58678
   162
next
haftmann@58678
   163
  fix a b
haftmann@58678
   164
  assume "a mod 2 = 1"
haftmann@58678
   165
  moreover assume "b mod 2 = 1"
haftmann@58678
   166
  ultimately show "(a + b) mod 2 = 0"
haftmann@58678
   167
    using mod_add_eq [of a b 2] by simp
haftmann@58678
   168
next
haftmann@58678
   169
  fix a b
haftmann@58678
   170
  assume "(a * b) mod 2 = 0"
haftmann@58678
   171
  then have "(a mod 2) * (b mod 2) = 0"
haftmann@58678
   172
    by (cases "a mod 2 = 0") (simp_all add: mod_mult_eq [of a b 2])
haftmann@58678
   173
  then show "a mod 2 = 0 \<or> b mod 2 = 0"
haftmann@58678
   174
    by (rule divisors_zero)
haftmann@58680
   175
next
haftmann@58680
   176
  fix a
haftmann@58680
   177
  assume "a mod 2 = 1"
haftmann@58680
   178
  then have "a = a div 2 * 2 + 1" using mod_div_equality [of a 2] by simp
haftmann@58680
   179
  then show "\<exists>b. a = b + 1" ..
haftmann@58678
   180
qed
haftmann@58678
   181
haftmann@58678
   182
end
haftmann@58678
   183
haftmann@58678
   184
haftmann@58678
   185
subsection {* Dedicated @{text even}/@{text odd} predicate *}
haftmann@58678
   186
haftmann@58680
   187
subsubsection {* Properties *}
haftmann@58680
   188
haftmann@58678
   189
context semiring_parity
haftmann@54227
   190
begin
wenzelm@21256
   191
haftmann@58740
   192
abbreviation even :: "'a \<Rightarrow> bool"
haftmann@54228
   193
where
haftmann@58740
   194
  "even a \<equiv> 2 dvd a"
haftmann@54228
   195
haftmann@58678
   196
abbreviation odd :: "'a \<Rightarrow> bool"
haftmann@58678
   197
where
haftmann@58740
   198
  "odd a \<equiv> \<not> 2 dvd a"
haftmann@58678
   199
haftmann@58690
   200
lemma evenE [elim?]:
haftmann@58690
   201
  assumes "even a"
haftmann@58690
   202
  obtains b where "a = 2 * b"
haftmann@58740
   203
  using assms by (rule dvdE)
haftmann@58690
   204
haftmann@58681
   205
lemma oddE [elim?]:
haftmann@58680
   206
  assumes "odd a"
haftmann@58680
   207
  obtains b where "a = 2 * b + 1"
haftmann@58740
   208
  using assms by (rule not_two_dvdE)
haftmann@58680
   209
  
haftmann@58770
   210
lemma even_times_iff [simp]:
haftmann@58678
   211
  "even (a * b) \<longleftrightarrow> even a \<or> even b"
haftmann@58740
   212
  by (auto simp add: dest: two_is_prime)
haftmann@58678
   213
haftmann@58678
   214
lemma even_zero [simp]:
haftmann@58678
   215
  "even 0"
haftmann@58740
   216
  by simp
haftmann@58678
   217
haftmann@58678
   218
lemma odd_one [simp]:
haftmann@58678
   219
  "odd 1"
haftmann@58740
   220
  by simp
haftmann@58678
   221
haftmann@58678
   222
lemma even_numeral [simp]:
haftmann@58678
   223
  "even (numeral (Num.Bit0 n))"
haftmann@58678
   224
proof -
haftmann@58678
   225
  have "even (2 * numeral n)"
haftmann@58740
   226
    unfolding even_times_iff by simp
haftmann@58678
   227
  then have "even (numeral n + numeral n)"
haftmann@58678
   228
    unfolding mult_2 .
haftmann@58678
   229
  then show ?thesis
haftmann@58678
   230
    unfolding numeral.simps .
haftmann@58678
   231
qed
haftmann@58678
   232
haftmann@58678
   233
lemma odd_numeral [simp]:
haftmann@58678
   234
  "odd (numeral (Num.Bit1 n))"
haftmann@58678
   235
proof
haftmann@58678
   236
  assume "even (numeral (num.Bit1 n))"
haftmann@58678
   237
  then have "even (numeral n + numeral n + 1)"
haftmann@58678
   238
    unfolding numeral.simps .
haftmann@58678
   239
  then have "even (2 * numeral n + 1)"
haftmann@58678
   240
    unfolding mult_2 .
haftmann@58678
   241
  then have "2 dvd numeral n * 2 + 1"
haftmann@58740
   242
    by (simp add: ac_simps)
haftmann@58678
   243
  with dvd_add_times_triv_left_iff [of 2 "numeral n" 1]
haftmann@58678
   244
    have "2 dvd 1"
haftmann@58678
   245
    by simp
haftmann@58678
   246
  then show False by simp
haftmann@58678
   247
qed
haftmann@58678
   248
haftmann@58680
   249
lemma even_add [simp]:
haftmann@58680
   250
  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
haftmann@58740
   251
  by (auto simp add: dvd_add_right_iff dvd_add_left_iff not_dvd_not_dvd_dvd_add)
haftmann@58680
   252
haftmann@58680
   253
lemma odd_add [simp]:
haftmann@58680
   254
  "odd (a + b) \<longleftrightarrow> (\<not> (odd a \<longleftrightarrow> odd b))"
haftmann@58680
   255
  by simp
haftmann@58680
   256
haftmann@58770
   257
lemma even_power [simp]:
haftmann@58680
   258
  "even (a ^ n) \<longleftrightarrow> even a \<and> n \<noteq> 0"
haftmann@58680
   259
  by (induct n) auto
haftmann@58680
   260
haftmann@58678
   261
end
haftmann@58678
   262
haftmann@58679
   263
context ring_parity
haftmann@58679
   264
begin
haftmann@58679
   265
haftmann@58770
   266
lemma even_minus [simp]:
haftmann@58679
   267
  "even (- a) \<longleftrightarrow> even a"
haftmann@58740
   268
  by (fact dvd_minus_iff)
haftmann@58679
   269
haftmann@58680
   270
lemma even_diff [simp]:
haftmann@58680
   271
  "even (a - b) \<longleftrightarrow> even (a + b)"
haftmann@58680
   272
  using even_add [of a "- b"] by simp
haftmann@58680
   273
haftmann@58679
   274
end
haftmann@58679
   275
haftmann@58710
   276
haftmann@58710
   277
subsubsection {* Parity and division *}
haftmann@58710
   278
haftmann@58678
   279
context semiring_div_parity
haftmann@58678
   280
begin
haftmann@58645
   281
haftmann@58710
   282
lemma one_div_two_eq_zero [simp]: -- \<open>FIXME move\<close>
haftmann@58710
   283
  "1 div 2 = 0"
haftmann@58710
   284
proof (cases "2 = 0")
haftmann@58710
   285
  case True then show ?thesis by simp
haftmann@58710
   286
next
haftmann@58710
   287
  case False
haftmann@58710
   288
  from mod_div_equality have "1 div 2 * 2 + 1 mod 2 = 1" .
haftmann@58710
   289
  with one_mod_two_eq_one have "1 div 2 * 2 + 1 = 1" by simp
haftmann@58710
   290
  then have "1 div 2 * 2 = 0" by (simp add: ac_simps add_left_imp_eq)
haftmann@58710
   291
  then have "1 div 2 = 0 \<or> 2 = 0" by (rule divisors_zero)
haftmann@58710
   292
  with False show ?thesis by auto
haftmann@58710
   293
qed
haftmann@58710
   294
haftmann@58711
   295
lemma even_iff_mod_2_eq_zero:
haftmann@58645
   296
  "even a \<longleftrightarrow> a mod 2 = 0"
haftmann@58740
   297
  by (fact dvd_eq_mod_eq_0)
haftmann@54228
   298
haftmann@58710
   299
lemma even_succ_div_two [simp]:
haftmann@58710
   300
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
haftmann@58710
   301
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
haftmann@58710
   302
haftmann@58710
   303
lemma odd_succ_div_two [simp]:
haftmann@58710
   304
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
haftmann@58710
   305
  by (auto elim!: oddE simp add: zero_not_eq_two [symmetric] add.assoc)
haftmann@58710
   306
haftmann@58710
   307
lemma even_two_times_div_two:
haftmann@58710
   308
  "even a \<Longrightarrow> 2 * (a div 2) = a"
haftmann@58740
   309
  by (fact dvd_mult_div_cancel)
haftmann@58710
   310
haftmann@58710
   311
lemma odd_two_times_div_two_succ:
haftmann@58710
   312
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
haftmann@58710
   313
  using mod_div_equality2 [of 2 a] by (simp add: even_iff_mod_2_eq_zero)
haftmann@58710
   314
  
haftmann@54227
   315
end
haftmann@54227
   316
haftmann@58680
   317
haftmann@58687
   318
subsubsection {* Particularities for @{typ nat} and @{typ int} *}
haftmann@58687
   319
haftmann@58770
   320
lemma even_Suc [simp]:
haftmann@58687
   321
  "even (Suc n) = odd n"
haftmann@58740
   322
  by (fact two_dvd_Suc_iff)
haftmann@58687
   323
haftmann@58689
   324
lemma odd_pos: 
haftmann@58689
   325
  "odd (n :: nat) \<Longrightarrow> 0 < n"
haftmann@58690
   326
  by (auto elim: oddE)
haftmann@58689
   327
  
haftmann@58687
   328
lemma even_diff_nat [simp]:
haftmann@58687
   329
  fixes m n :: nat
haftmann@58687
   330
  shows "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)"
haftmann@58740
   331
  by (fact two_dvd_diff_nat_iff)
haftmann@58680
   332
haftmann@58679
   333
lemma even_int_iff:
haftmann@58679
   334
  "even (int n) \<longleftrightarrow> even n"
haftmann@58740
   335
  by (simp add: dvd_int_iff)
haftmann@33318
   336
haftmann@58687
   337
lemma even_nat_iff:
haftmann@58687
   338
  "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
haftmann@58687
   339
  by (simp add: even_int_iff [symmetric])
haftmann@58687
   340
haftmann@58710
   341
lemma even_num_iff:
haftmann@58710
   342
  "0 < n \<Longrightarrow> even n = odd (n - 1 :: nat)"
haftmann@58710
   343
  by simp
haftmann@58687
   344
haftmann@58710
   345
lemma even_Suc_div_two [simp]:
haftmann@58710
   346
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
haftmann@58710
   347
  using even_succ_div_two [of n] by simp
haftmann@58710
   348
  
haftmann@58710
   349
lemma odd_Suc_div_two [simp]:
haftmann@58710
   350
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
haftmann@58710
   351
  using odd_succ_div_two [of n] by simp
haftmann@58710
   352
haftmann@58710
   353
lemma odd_two_times_div_two_Suc:
haftmann@58710
   354
  "odd n \<Longrightarrow> Suc (2 * (n div 2)) = n"
haftmann@58710
   355
  using odd_two_times_div_two_succ [of n] by simp
haftmann@58769
   356
haftmann@58769
   357
lemma parity_induct [case_names zero even odd]:
haftmann@58769
   358
  assumes zero: "P 0"
haftmann@58769
   359
  assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"
haftmann@58769
   360
  assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
haftmann@58769
   361
  shows "P n"
haftmann@58769
   362
proof (induct n rule: less_induct)
haftmann@58769
   363
  case (less n)
haftmann@58769
   364
  show "P n"
haftmann@58769
   365
  proof (cases "n = 0")
haftmann@58769
   366
    case True with zero show ?thesis by simp
haftmann@58769
   367
  next
haftmann@58769
   368
    case False
haftmann@58769
   369
    with less have hyp: "P (n div 2)" by simp
haftmann@58769
   370
    show ?thesis
haftmann@58769
   371
    proof (cases "even n")
haftmann@58769
   372
      case True
haftmann@58769
   373
      with hyp even [of "n div 2"] show ?thesis
haftmann@58769
   374
        by (simp add: dvd_mult_div_cancel)
haftmann@58769
   375
    next
haftmann@58769
   376
      case False
haftmann@58769
   377
      with hyp odd [of "n div 2"] show ?thesis 
haftmann@58769
   378
        by (simp add: odd_two_times_div_two_Suc)
haftmann@58769
   379
    qed
haftmann@58769
   380
  qed
haftmann@58769
   381
qed
haftmann@58710
   382
  
haftmann@58710
   383
text {* Parity and powers *}
haftmann@58689
   384
haftmann@58689
   385
context comm_ring_1
haftmann@58689
   386
begin
haftmann@58689
   387
haftmann@58689
   388
lemma power_minus_even [simp]:
haftmann@58689
   389
  "even n \<Longrightarrow> (- a) ^ n = a ^ n"
haftmann@58690
   390
  by (auto elim: evenE)
haftmann@58689
   391
haftmann@58689
   392
lemma power_minus_odd [simp]:
haftmann@58689
   393
  "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
haftmann@58690
   394
  by (auto elim: oddE)
haftmann@58690
   395
haftmann@58690
   396
lemma neg_power_if:
haftmann@58690
   397
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
haftmann@58690
   398
  by simp
haftmann@58689
   399
haftmann@58689
   400
lemma neg_one_even_power [simp]:
haftmann@58689
   401
  "even n \<Longrightarrow> (- 1) ^ n = 1"
haftmann@58690
   402
  by simp
haftmann@58689
   403
haftmann@58689
   404
lemma neg_one_odd_power [simp]:
haftmann@58689
   405
  "odd n \<Longrightarrow> (- 1) ^ n = - 1"
haftmann@58690
   406
  by simp
haftmann@58689
   407
haftmann@58689
   408
end  
haftmann@58689
   409
haftmann@58689
   410
lemma zero_less_power_nat_eq_numeral [simp]: -- \<open>FIXME move\<close>
haftmann@58689
   411
  "0 < (n :: nat) ^ numeral w \<longleftrightarrow> 0 < n \<or> numeral w = (0 :: nat)"
haftmann@58689
   412
  by (fact nat_zero_less_power_iff)
haftmann@58689
   413
haftmann@58689
   414
context linordered_idom
haftmann@58689
   415
begin
haftmann@58689
   416
haftmann@58689
   417
lemma power_eq_0_iff' [simp]: -- \<open>FIXME move\<close>
haftmann@58689
   418
  "a ^ n = 0 \<longleftrightarrow> a = 0 \<and> n > 0"
haftmann@58689
   419
  by (induct n) auto
haftmann@58689
   420
haftmann@58689
   421
lemma power2_less_eq_zero_iff [simp]: -- \<open>FIXME move\<close>
haftmann@58689
   422
  "a\<^sup>2 \<le> 0 \<longleftrightarrow> a = 0"
haftmann@58689
   423
proof (cases "a = 0")
haftmann@58689
   424
  case True then show ?thesis by simp
haftmann@58689
   425
next
haftmann@58689
   426
  case False then have "a < 0 \<or> a > 0" by auto
haftmann@58689
   427
  then have "a\<^sup>2 > 0" by auto
haftmann@58689
   428
  then have "\<not> a\<^sup>2 \<le> 0" by (simp add: not_le)
haftmann@58689
   429
  with False show ?thesis by simp
haftmann@58689
   430
qed
haftmann@58689
   431
haftmann@58689
   432
lemma zero_le_even_power:
haftmann@58689
   433
  "even n \<Longrightarrow> 0 \<le> a ^ n"
haftmann@58690
   434
  by (auto elim: evenE)
haftmann@58689
   435
haftmann@58689
   436
lemma zero_le_odd_power:
haftmann@58689
   437
  "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
haftmann@58689
   438
  by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
haftmann@58689
   439
haftmann@58770
   440
lemma zero_le_power_iff: -- \<open>FIXME cf. @{text zero_le_power_eq}\<close>
haftmann@58689
   441
  "0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a \<or> even n"
haftmann@58689
   442
proof (cases "even n")
haftmann@58689
   443
  case True
haftmann@58689
   444
  then obtain k where "n = 2 * k" ..
haftmann@58690
   445
  then show ?thesis by simp
haftmann@58689
   446
next
haftmann@58689
   447
  case False
haftmann@58689
   448
  then obtain k where "n = 2 * k + 1" ..
haftmann@58689
   449
  moreover have "a ^ (2 * k) \<le> 0 \<Longrightarrow> a = 0"
haftmann@58689
   450
    by (induct k) (auto simp add: zero_le_mult_iff mult_le_0_iff)
haftmann@58689
   451
  ultimately show ?thesis
haftmann@58689
   452
    by (auto simp add: zero_le_mult_iff zero_le_even_power)
haftmann@58689
   453
qed
haftmann@58689
   454
haftmann@58770
   455
lemma zero_le_power_eq:
haftmann@58689
   456
  "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
haftmann@58689
   457
  using zero_le_power_iff [of a n] by auto
haftmann@58689
   458
haftmann@58770
   459
lemma zero_less_power_eq:
haftmann@58689
   460
  "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
haftmann@58689
   461
proof -
haftmann@58689
   462
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
haftmann@58689
   463
    unfolding power_eq_0_iff' [of a n, symmetric] by blast
haftmann@58689
   464
  show ?thesis
haftmann@58710
   465
  unfolding less_le zero_le_power_eq by auto
haftmann@58689
   466
qed
haftmann@58689
   467
haftmann@58770
   468
lemma power_less_zero_eq:
haftmann@58689
   469
  "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
haftmann@58689
   470
  unfolding not_le [symmetric] zero_le_power_eq by auto
haftmann@58689
   471
  
haftmann@58770
   472
lemma power_le_zero_eq:
haftmann@58689
   473
  "a ^ n \<le> 0 \<longleftrightarrow> n > 0 \<and> (odd n \<and> a \<le> 0 \<or> even n \<and> a = 0)"
haftmann@58689
   474
  unfolding not_less [symmetric] zero_less_power_eq by auto 
haftmann@58689
   475
haftmann@58689
   476
lemma power_even_abs:
haftmann@58689
   477
  "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
haftmann@58689
   478
  using power_abs [of a n] by (simp add: zero_le_even_power)
haftmann@58689
   479
haftmann@58689
   480
lemma power_mono_even:
haftmann@58689
   481
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
haftmann@58689
   482
  shows "a ^ n \<le> b ^ n"
haftmann@58689
   483
proof -
haftmann@58689
   484
  have "0 \<le> \<bar>a\<bar>" by auto
haftmann@58689
   485
  with `\<bar>a\<bar> \<le> \<bar>b\<bar>`
haftmann@58689
   486
  have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n" by (rule power_mono)
haftmann@58689
   487
  with `even n` show ?thesis by (simp add: power_even_abs)  
haftmann@58689
   488
qed
haftmann@58689
   489
haftmann@58689
   490
lemma power_mono_odd:
haftmann@58689
   491
  assumes "odd n" and "a \<le> b"
haftmann@58689
   492
  shows "a ^ n \<le> b ^ n"
haftmann@58689
   493
proof (cases "b < 0")
haftmann@58689
   494
  case True with `a \<le> b` have "- b \<le> - a" and "0 \<le> - b" by auto
haftmann@58689
   495
  hence "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
haftmann@58689
   496
  with `odd n` show ?thesis by simp
haftmann@58689
   497
next
haftmann@58689
   498
  case False then have "0 \<le> b" by auto
haftmann@58689
   499
  show ?thesis
haftmann@58689
   500
  proof (cases "a < 0")
haftmann@58689
   501
    case True then have "n \<noteq> 0" and "a \<le> 0" using `odd n` [THEN odd_pos] by auto
haftmann@58689
   502
    then have "a ^ n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
haftmann@58689
   503
    moreover
haftmann@58689
   504
    from `0 \<le> b` have "0 \<le> b ^ n" by auto
haftmann@58689
   505
    ultimately show ?thesis by auto
haftmann@58689
   506
  next
haftmann@58689
   507
    case False then have "0 \<le> a" by auto
haftmann@58689
   508
    with `a \<le> b` show ?thesis using power_mono by auto
haftmann@58689
   509
  qed
haftmann@58689
   510
qed
haftmann@58689
   511
 
haftmann@58689
   512
text {* Simplify, when the exponent is a numeral *}
haftmann@58689
   513
haftmann@58689
   514
lemma zero_le_power_eq_numeral [simp]:
haftmann@58689
   515
  "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"
haftmann@58689
   516
  by (fact zero_le_power_eq)
haftmann@58689
   517
haftmann@58689
   518
lemma zero_less_power_eq_numeral [simp]:
haftmann@58689
   519
  "0 < a ^ numeral w \<longleftrightarrow> numeral w = (0 :: nat)
haftmann@58689
   520
    \<or> even (numeral w :: nat) \<and> a \<noteq> 0 \<or> odd (numeral w :: nat) \<and> 0 < a"
haftmann@58689
   521
  by (fact zero_less_power_eq)
haftmann@58689
   522
haftmann@58689
   523
lemma power_le_zero_eq_numeral [simp]:
haftmann@58689
   524
  "a ^ numeral w \<le> 0 \<longleftrightarrow> (0 :: nat) < numeral w
haftmann@58689
   525
    \<and> (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
haftmann@58689
   526
  by (fact power_le_zero_eq)
haftmann@58689
   527
haftmann@58689
   528
lemma power_less_zero_eq_numeral [simp]:
haftmann@58689
   529
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
haftmann@58689
   530
  by (fact power_less_zero_eq)
haftmann@58689
   531
haftmann@58689
   532
lemma power_eq_0_iff_numeral [simp]:
haftmann@58689
   533
  "a ^ numeral w = (0 :: nat) \<longleftrightarrow> a = 0 \<and> numeral w \<noteq> (0 :: nat)"
haftmann@58689
   534
  by (fact power_eq_0_iff)
haftmann@58689
   535
haftmann@58689
   536
lemma power_even_abs_numeral [simp]:
haftmann@58689
   537
  "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"
haftmann@58689
   538
  by (fact power_even_abs)
haftmann@58689
   539
haftmann@58689
   540
end
haftmann@58689
   541
haftmann@58689
   542
haftmann@58687
   543
subsubsection {* Tools setup *}
haftmann@58687
   544
haftmann@58679
   545
declare transfer_morphism_int_nat [transfer add return:
haftmann@58679
   546
  even_int_iff
haftmann@33318
   547
]
wenzelm@21256
   548
haftmann@58770
   549
context semiring_parity
haftmann@58770
   550
begin
haftmann@58770
   551
haftmann@58770
   552
declare even_times_iff [presburger, algebra]
haftmann@58770
   553
haftmann@58770
   554
declare even_power [presburger]
haftmann@58770
   555
haftmann@58679
   556
lemma [presburger]:
haftmann@58680
   557
  "even (a + b) \<longleftrightarrow> even a \<and> even b \<or> odd a \<and> odd b"
haftmann@58680
   558
  by auto
wenzelm@21256
   559
haftmann@58770
   560
end
haftmann@58770
   561
haftmann@58770
   562
context ring_parity
haftmann@58770
   563
begin
haftmann@58770
   564
haftmann@58770
   565
declare even_minus [presburger, algebra]
haftmann@58770
   566
haftmann@58770
   567
end
haftmann@58770
   568
haftmann@58770
   569
context linordered_idom
haftmann@58770
   570
begin
haftmann@58770
   571
haftmann@58770
   572
declare zero_le_power_iff [presburger]
haftmann@58770
   573
haftmann@58770
   574
declare zero_le_power_eq [presburger]
haftmann@58770
   575
haftmann@58770
   576
declare zero_less_power_eq [presburger]
haftmann@58770
   577
haftmann@58770
   578
declare power_less_zero_eq [presburger]
haftmann@58770
   579
  
haftmann@58770
   580
declare power_le_zero_eq [presburger]
haftmann@58770
   581
haftmann@58770
   582
end
haftmann@58770
   583
haftmann@58770
   584
declare even_Suc [presburger, algebra]
haftmann@58770
   585
haftmann@58770
   586
lemma [presburger]:
haftmann@58770
   587
  "Suc n div Suc (Suc 0) = n div Suc (Suc 0) \<longleftrightarrow> even n"
haftmann@58770
   588
  by presburger
haftmann@58770
   589
haftmann@58687
   590
lemma [presburger, algebra]:
haftmann@58687
   591
  fixes m n :: nat
haftmann@58687
   592
  shows "even (m - n) \<longleftrightarrow> m < n \<or> even m \<and> even n \<or> odd m \<and> odd n"
haftmann@58687
   593
  by auto
haftmann@58687
   594
haftmann@58687
   595
lemma [presburger, algebra]:
haftmann@58687
   596
  fixes m n :: nat
haftmann@58687
   597
  shows "even (m ^ n) \<longleftrightarrow> even m \<and> 0 < n"
haftmann@58687
   598
  by simp
haftmann@58687
   599
haftmann@58687
   600
lemma [presburger]:
haftmann@58687
   601
  fixes k :: int
haftmann@58687
   602
  shows "(k + 1) div 2 = k div 2 \<longleftrightarrow> even k"
haftmann@58687
   603
  by presburger
haftmann@58687
   604
haftmann@58687
   605
lemma [presburger]:
haftmann@58687
   606
  fixes k :: int
haftmann@58687
   607
  shows "(k + 1) div 2 = k div 2 + 1 \<longleftrightarrow> odd k"
haftmann@58687
   608
  by presburger
haftmann@58770
   609
haftmann@58687
   610
lemma [presburger]:
haftmann@58770
   611
  "even n \<longleftrightarrow> even (int n)"
haftmann@58770
   612
  using even_int_iff [of n] by simp
haftmann@58770
   613
  
haftmann@58770
   614
haftmann@58770
   615
subsubsection {* Nice facts about division by @{term 4} *}  
haftmann@58770
   616
haftmann@58770
   617
lemma even_even_mod_4_iff:
haftmann@58770
   618
  "even (n::nat) \<longleftrightarrow> even (n mod 4)"
haftmann@58687
   619
  by presburger
haftmann@58687
   620
haftmann@58770
   621
lemma odd_mod_4_div_2:
haftmann@58770
   622
  "n mod 4 = (3::nat) \<Longrightarrow> odd ((n - 1) div 2)"
haftmann@58770
   623
  by presburger
haftmann@58770
   624
haftmann@58770
   625
lemma even_mod_4_div_2:
haftmann@58770
   626
  "n mod 4 = (1::nat) \<Longrightarrow> even ((n - 1) div 2)"
haftmann@58770
   627
  by presburger
haftmann@58770
   628
  
wenzelm@21256
   629
end
haftmann@54227
   630