src/HOL/Parity.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66840 0d689d71dbdc
child 67051 e7e54a0b9197
permissions -rw-r--r--
more robust sorted_entries;
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@60758
     6
section \<open>Parity in rings and semirings\<close>
wenzelm@21256
     7
wenzelm@21256
     8
theory Parity
haftmann@66815
     9
  imports Euclidean_Division
wenzelm@21256
    10
begin
wenzelm@21256
    11
wenzelm@61799
    12
subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close>
haftmann@58678
    13
haftmann@66815
    14
class semiring_parity = linordered_semidom + unique_euclidean_semiring +
haftmann@66815
    15
  assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n"
haftmann@66839
    16
    and division_segment_of_nat [simp]: "division_segment (of_nat n) = 1"
haftmann@66839
    17
    and division_segment_euclidean_size [simp]: "division_segment a * of_nat (euclidean_size a) = a"
haftmann@66839
    18
begin
haftmann@66839
    19
haftmann@66839
    20
lemma division_segment_eq_iff:
haftmann@66839
    21
  "a = b" if "division_segment a = division_segment b"
haftmann@66839
    22
    and "euclidean_size a = euclidean_size b"
haftmann@66839
    23
  using that division_segment_euclidean_size [of a] by simp
haftmann@66839
    24
haftmann@66839
    25
lemma euclidean_size_of_nat [simp]:
haftmann@66839
    26
  "euclidean_size (of_nat n) = n"
haftmann@66839
    27
proof -
haftmann@66839
    28
  have "division_segment (of_nat n) * of_nat (euclidean_size (of_nat n)) = of_nat n"
haftmann@66839
    29
    by (fact division_segment_euclidean_size)
haftmann@66839
    30
  then show ?thesis by simp
haftmann@66839
    31
qed
haftmann@66815
    32
haftmann@66839
    33
lemma of_nat_euclidean_size:
haftmann@66839
    34
  "of_nat (euclidean_size a) = a div division_segment a"
haftmann@66839
    35
proof -
haftmann@66839
    36
  have "of_nat (euclidean_size a) = division_segment a * of_nat (euclidean_size a) div division_segment a"
haftmann@66839
    37
    by (subst nonzero_mult_div_cancel_left) simp_all
haftmann@66839
    38
  also have "\<dots> = a div division_segment a"
haftmann@66839
    39
    by simp
haftmann@66839
    40
  finally show ?thesis .
haftmann@66839
    41
qed
haftmann@66839
    42
haftmann@66839
    43
lemma division_segment_1 [simp]:
haftmann@66839
    44
  "division_segment 1 = 1"
haftmann@66839
    45
  using division_segment_of_nat [of 1] by simp
haftmann@66839
    46
haftmann@66839
    47
lemma division_segment_numeral [simp]:
haftmann@66839
    48
  "division_segment (numeral k) = 1"
haftmann@66839
    49
  using division_segment_of_nat [of "numeral k"] by simp
haftmann@66839
    50
haftmann@66839
    51
lemma euclidean_size_1 [simp]:
haftmann@66839
    52
  "euclidean_size 1 = 1"
haftmann@66839
    53
  using euclidean_size_of_nat [of 1] by simp
haftmann@66839
    54
haftmann@66839
    55
lemma euclidean_size_numeral [simp]:
haftmann@66839
    56
  "euclidean_size (numeral k) = numeral k"
haftmann@66839
    57
  using euclidean_size_of_nat [of "numeral k"] by simp
wenzelm@21256
    58
haftmann@66815
    59
lemma of_nat_dvd_iff:
haftmann@66815
    60
  "of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q")
haftmann@66815
    61
proof (cases "m = 0")
haftmann@66815
    62
  case True
haftmann@66815
    63
  then show ?thesis
haftmann@66815
    64
    by simp
haftmann@66815
    65
next
haftmann@66815
    66
  case False
haftmann@66815
    67
  show ?thesis
haftmann@66815
    68
  proof
haftmann@66815
    69
    assume ?Q
haftmann@66815
    70
    then show ?P
haftmann@66815
    71
      by (auto elim: dvd_class.dvdE)
haftmann@66815
    72
  next
haftmann@66815
    73
    assume ?P
haftmann@66815
    74
    with False have "of_nat n = of_nat n div of_nat m * of_nat m"
haftmann@66815
    75
      by simp
haftmann@66815
    76
    then have "of_nat n = of_nat (n div m * m)"
haftmann@66815
    77
      by (simp add: of_nat_div)
haftmann@66815
    78
    then have "n = n div m * m"
haftmann@66815
    79
      by (simp only: of_nat_eq_iff)
haftmann@66815
    80
    then have "n = m * (n div m)"
haftmann@66815
    81
      by (simp add: ac_simps)
haftmann@66815
    82
    then show ?Q ..
haftmann@66815
    83
  qed
haftmann@66815
    84
qed
haftmann@66815
    85
haftmann@66815
    86
lemma of_nat_mod:
haftmann@66815
    87
  "of_nat (m mod n) = of_nat m mod of_nat n"
haftmann@66815
    88
proof -
haftmann@66815
    89
  have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m"
haftmann@66815
    90
    by (simp add: div_mult_mod_eq)
haftmann@66815
    91
  also have "of_nat m = of_nat (m div n * n + m mod n)"
haftmann@66815
    92
    by simp
haftmann@66815
    93
  finally show ?thesis
haftmann@66815
    94
    by (simp only: of_nat_div of_nat_mult of_nat_add) simp
haftmann@66815
    95
qed
haftmann@66815
    96
haftmann@66815
    97
lemma one_div_two_eq_zero [simp]:
haftmann@66815
    98
  "1 div 2 = 0"
haftmann@66815
    99
proof -
haftmann@66815
   100
  from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0"
haftmann@66815
   101
    by (simp only:) simp
haftmann@66815
   102
  then show ?thesis
haftmann@66815
   103
    by simp
haftmann@66815
   104
qed
haftmann@66815
   105
haftmann@66815
   106
lemma one_mod_two_eq_one [simp]:
haftmann@66815
   107
  "1 mod 2 = 1"
haftmann@66815
   108
proof -
haftmann@66815
   109
  from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1"
haftmann@66815
   110
    by (simp only:) simp
haftmann@66815
   111
  then show ?thesis
haftmann@66815
   112
    by simp
haftmann@66815
   113
qed
haftmann@59816
   114
haftmann@58740
   115
abbreviation even :: "'a \<Rightarrow> bool"
wenzelm@63654
   116
  where "even a \<equiv> 2 dvd a"
haftmann@54228
   117
haftmann@58678
   118
abbreviation odd :: "'a \<Rightarrow> bool"
wenzelm@63654
   119
  where "odd a \<equiv> \<not> 2 dvd a"
haftmann@58678
   120
haftmann@66815
   121
lemma even_iff_mod_2_eq_zero:
haftmann@66815
   122
  "even a \<longleftrightarrow> a mod 2 = 0"
haftmann@66815
   123
  by (fact dvd_eq_mod_eq_0)
haftmann@66815
   124
haftmann@66815
   125
lemma odd_iff_mod_2_eq_one:
haftmann@66815
   126
  "odd a \<longleftrightarrow> a mod 2 = 1"
haftmann@66839
   127
proof
haftmann@66839
   128
  assume "a mod 2 = 1"
haftmann@66839
   129
  then show "odd a"
haftmann@66839
   130
    by auto
haftmann@66839
   131
next
haftmann@66839
   132
  assume "odd a"
haftmann@66839
   133
  have eucl: "euclidean_size (a mod 2) = 1"
haftmann@66839
   134
  proof (rule order_antisym)
haftmann@66839
   135
    show "euclidean_size (a mod 2) \<le> 1"
haftmann@66839
   136
      using mod_size_less [of 2 a] by simp
haftmann@66839
   137
    show "1 \<le> euclidean_size (a mod 2)"
haftmann@66840
   138
      using \<open>odd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0)
haftmann@66839
   139
  qed 
haftmann@66839
   140
  from \<open>odd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)"
haftmann@66839
   141
    by simp
haftmann@66839
   142
  then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)"
haftmann@66839
   143
    by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment)
haftmann@66839
   144
  then have "\<not> 2 dvd euclidean_size a"
haftmann@66839
   145
    using of_nat_dvd_iff [of 2] by simp
haftmann@66839
   146
  then have "euclidean_size a mod 2 = 1"
haftmann@66839
   147
    by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0)
haftmann@66839
   148
  then have "of_nat (euclidean_size a mod 2) = of_nat 1"
haftmann@66839
   149
    by simp
haftmann@66839
   150
  then have "of_nat (euclidean_size a) mod 2 = 1"
haftmann@66839
   151
    by (simp add: of_nat_mod)
haftmann@66839
   152
  from \<open>odd a\<close> eucl
haftmann@66839
   153
  show "a mod 2 = 1"
haftmann@66839
   154
    by (auto intro: division_segment_eq_iff simp add: division_segment_mod)
haftmann@66839
   155
qed
haftmann@58787
   156
haftmann@66815
   157
lemma parity_cases [case_names even odd]:
haftmann@66815
   158
  assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"
haftmann@66815
   159
  assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"
haftmann@66815
   160
  shows P
haftmann@66815
   161
  using assms by (cases "even a") (simp_all add: odd_iff_mod_2_eq_one)
haftmann@66815
   162
haftmann@66815
   163
lemma not_mod_2_eq_1_eq_0 [simp]:
haftmann@66815
   164
  "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
haftmann@66815
   165
  by (cases a rule: parity_cases) simp_all
haftmann@66815
   166
haftmann@66815
   167
lemma not_mod_2_eq_0_eq_1 [simp]:
haftmann@66815
   168
  "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
haftmann@66815
   169
  by (cases a rule: parity_cases) simp_all
haftmann@58787
   170
haftmann@58690
   171
lemma evenE [elim?]:
haftmann@58690
   172
  assumes "even a"
haftmann@58690
   173
  obtains b where "a = 2 * b"
haftmann@58740
   174
  using assms by (rule dvdE)
haftmann@58690
   175
haftmann@58681
   176
lemma oddE [elim?]:
haftmann@58680
   177
  assumes "odd a"
haftmann@58680
   178
  obtains b where "a = 2 * b + 1"
haftmann@58787
   179
proof -
haftmann@66815
   180
  have "a = 2 * (a div 2) + a mod 2"
haftmann@66815
   181
    by (simp add: mult_div_mod_eq)
haftmann@66815
   182
  with assms have "a = 2 * (a div 2) + 1"
haftmann@66815
   183
    by (simp add: odd_iff_mod_2_eq_one)
haftmann@66815
   184
  then show ?thesis ..
haftmann@66815
   185
qed
haftmann@66815
   186
haftmann@66815
   187
lemma mod_2_eq_odd:
haftmann@66815
   188
  "a mod 2 = of_bool (odd a)"
haftmann@66815
   189
  by (auto elim: oddE)
haftmann@66815
   190
haftmann@66815
   191
lemma one_mod_2_pow_eq [simp]:
haftmann@66815
   192
  "1 mod (2 ^ n) = of_bool (n > 0)"
haftmann@66815
   193
proof -
haftmann@66815
   194
  have "1 mod (2 ^ n) = (of_bool (n > 0) :: nat)"
haftmann@66815
   195
    by (induct n) (simp_all add: mod_mult2_eq)
haftmann@66815
   196
  then have "of_nat (1 mod (2 ^ n)) = of_bool (n > 0)"
haftmann@66815
   197
    by simp
haftmann@66815
   198
  then show ?thesis
haftmann@66815
   199
    by (simp add: of_nat_mod)
haftmann@66815
   200
qed
haftmann@66815
   201
haftmann@66815
   202
lemma even_of_nat [simp]:
haftmann@66815
   203
  "even (of_nat a) \<longleftrightarrow> even a"
haftmann@66815
   204
proof -
haftmann@66815
   205
  have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a"
haftmann@66815
   206
    by simp
haftmann@66815
   207
  also have "\<dots> \<longleftrightarrow> even a"
haftmann@66815
   208
    by (simp only: of_nat_dvd_iff)
haftmann@66815
   209
  finally show ?thesis .
haftmann@66815
   210
qed
haftmann@66815
   211
haftmann@66815
   212
lemma even_zero [simp]:
haftmann@66815
   213
  "even 0"
haftmann@66815
   214
  by (fact dvd_0_right)
haftmann@66815
   215
haftmann@66815
   216
lemma odd_one [simp]:
haftmann@66815
   217
  "odd 1"
haftmann@66815
   218
proof -
haftmann@66815
   219
  have "\<not> (2 :: nat) dvd 1"
haftmann@66815
   220
    by simp
haftmann@66815
   221
  then have "\<not> of_nat 2 dvd of_nat 1"
haftmann@66815
   222
    unfolding of_nat_dvd_iff by simp
haftmann@66815
   223
  then show ?thesis
haftmann@66815
   224
    by simp
haftmann@58787
   225
qed
wenzelm@63654
   226
haftmann@66815
   227
lemma odd_even_add:
haftmann@66815
   228
  "even (a + b)" if "odd a" and "odd b"
haftmann@66815
   229
proof -
haftmann@66815
   230
  from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1"
haftmann@66815
   231
    by (blast elim: oddE)
haftmann@66815
   232
  then have "a + b = 2 * c + 2 * d + (1 + 1)"
haftmann@66815
   233
    by (simp only: ac_simps)
haftmann@66815
   234
  also have "\<dots> = 2 * (c + d + 1)"
haftmann@66815
   235
    by (simp add: algebra_simps)
haftmann@66815
   236
  finally show ?thesis ..
haftmann@66815
   237
qed
haftmann@66815
   238
haftmann@66815
   239
lemma even_add [simp]:
haftmann@66815
   240
  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
haftmann@66815
   241
  by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)
haftmann@66815
   242
haftmann@66815
   243
lemma odd_add [simp]:
haftmann@66815
   244
  "odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"
haftmann@66815
   245
  by simp
haftmann@66815
   246
haftmann@66815
   247
lemma even_plus_one_iff [simp]:
haftmann@66815
   248
  "even (a + 1) \<longleftrightarrow> odd a"
haftmann@66815
   249
  by (auto simp add: dvd_add_right_iff intro: odd_even_add)
haftmann@66815
   250
haftmann@66815
   251
lemma even_mult_iff [simp]:
haftmann@66815
   252
  "even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q")
haftmann@66815
   253
proof
haftmann@66815
   254
  assume ?Q
haftmann@66815
   255
  then show ?P
haftmann@66815
   256
    by auto
haftmann@66815
   257
next
haftmann@66815
   258
  assume ?P
haftmann@66815
   259
  show ?Q
haftmann@66815
   260
  proof (rule ccontr)
haftmann@66815
   261
    assume "\<not> (even a \<or> even b)"
haftmann@66815
   262
    then have "odd a" and "odd b"
haftmann@66815
   263
      by auto
haftmann@66815
   264
    then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1"
haftmann@66815
   265
      by (blast elim: oddE)
haftmann@66815
   266
    then have "a * b = (2 * r + 1) * (2 * s + 1)"
haftmann@66815
   267
      by simp
haftmann@66815
   268
    also have "\<dots> = 2 * (2 * r * s + r + s) + 1"
haftmann@66815
   269
      by (simp add: algebra_simps)
haftmann@66815
   270
    finally have "odd (a * b)"
haftmann@66815
   271
      by simp
haftmann@66815
   272
    with \<open>?P\<close> show False
haftmann@66815
   273
      by auto
haftmann@66815
   274
  qed
haftmann@66815
   275
qed
haftmann@58678
   276
wenzelm@63654
   277
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
haftmann@58678
   278
proof -
haftmann@58678
   279
  have "even (2 * numeral n)"
haftmann@66815
   280
    unfolding even_mult_iff by simp
haftmann@58678
   281
  then have "even (numeral n + numeral n)"
haftmann@58678
   282
    unfolding mult_2 .
haftmann@58678
   283
  then show ?thesis
haftmann@58678
   284
    unfolding numeral.simps .
haftmann@58678
   285
qed
haftmann@58678
   286
wenzelm@63654
   287
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
haftmann@58678
   288
proof
haftmann@58678
   289
  assume "even (numeral (num.Bit1 n))"
haftmann@58678
   290
  then have "even (numeral n + numeral n + 1)"
haftmann@58678
   291
    unfolding numeral.simps .
haftmann@58678
   292
  then have "even (2 * numeral n + 1)"
haftmann@58678
   293
    unfolding mult_2 .
haftmann@58678
   294
  then have "2 dvd numeral n * 2 + 1"
haftmann@58740
   295
    by (simp add: ac_simps)
wenzelm@63654
   296
  then have "2 dvd 1"
wenzelm@63654
   297
    using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp
haftmann@58678
   298
  then show False by simp
haftmann@58678
   299
qed
haftmann@58678
   300
wenzelm@63654
   301
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"
haftmann@58680
   302
  by (induct n) auto
haftmann@58680
   303
haftmann@66815
   304
lemma even_succ_div_two [simp]:
haftmann@66815
   305
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
haftmann@66815
   306
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
haftmann@66815
   307
haftmann@66815
   308
lemma odd_succ_div_two [simp]:
haftmann@66815
   309
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
haftmann@66815
   310
  by (auto elim!: oddE simp add: add.assoc)
haftmann@66815
   311
haftmann@66815
   312
lemma even_two_times_div_two:
haftmann@66815
   313
  "even a \<Longrightarrow> 2 * (a div 2) = a"
haftmann@66815
   314
  by (fact dvd_mult_div_cancel)
haftmann@66815
   315
haftmann@66815
   316
lemma odd_two_times_div_two_succ [simp]:
haftmann@66815
   317
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
haftmann@66815
   318
  using mult_div_mod_eq [of 2 a]
haftmann@66815
   319
  by (simp add: even_iff_mod_2_eq_zero)
haftmann@66815
   320
haftmann@58678
   321
end
haftmann@58678
   322
haftmann@59816
   323
class ring_parity = ring + semiring_parity
haftmann@58679
   324
begin
haftmann@58679
   325
haftmann@59816
   326
subclass comm_ring_1 ..
haftmann@59816
   327
haftmann@66815
   328
lemma even_minus [simp]:
haftmann@66815
   329
  "even (- a) \<longleftrightarrow> even a"
haftmann@58740
   330
  by (fact dvd_minus_iff)
haftmann@58679
   331
haftmann@66815
   332
lemma even_diff [simp]:
haftmann@66815
   333
  "even (a - b) \<longleftrightarrow> even (a + b)"
haftmann@58680
   334
  using even_add [of a "- b"] by simp
haftmann@58680
   335
haftmann@58679
   336
end
haftmann@58679
   337
haftmann@66808
   338
haftmann@66815
   339
subsection \<open>Instance for @{typ nat}\<close>
haftmann@66808
   340
haftmann@66815
   341
instance nat :: semiring_parity
haftmann@66815
   342
  by standard (simp_all add: dvd_eq_mod_eq_0)
haftmann@66808
   343
haftmann@66815
   344
lemma even_Suc_Suc_iff [simp]:
haftmann@66815
   345
  "even (Suc (Suc n)) \<longleftrightarrow> even n"
haftmann@58787
   346
  using dvd_add_triv_right_iff [of 2 n] by simp
haftmann@58687
   347
haftmann@66815
   348
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"
haftmann@66815
   349
  using even_plus_one_iff [of n] by simp
haftmann@58787
   350
haftmann@66815
   351
lemma even_diff_nat [simp]:
haftmann@66815
   352
  "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat
haftmann@58787
   353
proof (cases "n \<le> m")
haftmann@58787
   354
  case True
haftmann@58787
   355
  then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
haftmann@66815
   356
  moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp
haftmann@66815
   357
  ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)
haftmann@58787
   358
  then show ?thesis by auto
haftmann@58787
   359
next
haftmann@58787
   360
  case False
haftmann@58787
   361
  then show ?thesis by simp
wenzelm@63654
   362
qed
wenzelm@63654
   363
haftmann@66815
   364
lemma odd_pos:
haftmann@66815
   365
  "odd n \<Longrightarrow> 0 < n" for n :: nat
haftmann@58690
   366
  by (auto elim: oddE)
haftmann@60343
   367
haftmann@66815
   368
lemma Suc_double_not_eq_double:
haftmann@66815
   369
  "Suc (2 * m) \<noteq> 2 * n"
haftmann@62597
   370
proof
haftmann@62597
   371
  assume "Suc (2 * m) = 2 * n"
haftmann@62597
   372
  moreover have "odd (Suc (2 * m))" and "even (2 * n)"
haftmann@62597
   373
    by simp_all
haftmann@62597
   374
  ultimately show False by simp
haftmann@62597
   375
qed
haftmann@62597
   376
haftmann@66815
   377
lemma double_not_eq_Suc_double:
haftmann@66815
   378
  "2 * m \<noteq> Suc (2 * n)"
haftmann@62597
   379
  using Suc_double_not_eq_double [of n m] by simp
haftmann@62597
   380
haftmann@66815
   381
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
haftmann@66815
   382
  by (auto elim: oddE)
haftmann@60343
   383
haftmann@66815
   384
lemma even_Suc_div_two [simp]:
haftmann@66815
   385
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
haftmann@66815
   386
  using even_succ_div_two [of n] by simp
haftmann@60343
   387
haftmann@66815
   388
lemma odd_Suc_div_two [simp]:
haftmann@66815
   389
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
haftmann@66815
   390
  using odd_succ_div_two [of n] by simp
haftmann@60343
   391
haftmann@66815
   392
lemma odd_two_times_div_two_nat [simp]:
haftmann@66815
   393
  assumes "odd n"
haftmann@66815
   394
  shows "2 * (n div 2) = n - (1 :: nat)"
haftmann@66815
   395
proof -
haftmann@66815
   396
  from assms have "2 * (n div 2) + 1 = n"
haftmann@66815
   397
    by (rule odd_two_times_div_two_succ)
haftmann@66815
   398
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
haftmann@58787
   399
    by simp
haftmann@66815
   400
  then show ?thesis
haftmann@66815
   401
    by simp
haftmann@58787
   402
qed
haftmann@58680
   403
haftmann@66815
   404
lemma parity_induct [case_names zero even odd]:
haftmann@66815
   405
  assumes zero: "P 0"
haftmann@66815
   406
  assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"
haftmann@66815
   407
  assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
haftmann@66815
   408
  shows "P n"
haftmann@66815
   409
proof (induct n rule: less_induct)
haftmann@66815
   410
  case (less n)
haftmann@66815
   411
  show "P n"
haftmann@66815
   412
  proof (cases "n = 0")
haftmann@66815
   413
    case True with zero show ?thesis by simp
haftmann@66815
   414
  next
haftmann@66815
   415
    case False
haftmann@66815
   416
    with less have hyp: "P (n div 2)" by simp
haftmann@66815
   417
    show ?thesis
haftmann@66815
   418
    proof (cases "even n")
haftmann@66815
   419
      case True
haftmann@66815
   420
      with hyp even [of "n div 2"] show ?thesis
haftmann@66815
   421
        by simp
haftmann@66815
   422
    next
haftmann@66815
   423
      case False
haftmann@66815
   424
      with hyp odd [of "n div 2"] show ?thesis
haftmann@66815
   425
        by simp
haftmann@66815
   426
    qed
haftmann@66815
   427
  qed
haftmann@66815
   428
qed
haftmann@58687
   429
haftmann@58687
   430
wenzelm@60758
   431
subsection \<open>Parity and powers\<close>
haftmann@58689
   432
eberlm@61531
   433
context ring_1
haftmann@58689
   434
begin
haftmann@58689
   435
wenzelm@63654
   436
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
haftmann@58690
   437
  by (auto elim: evenE)
haftmann@58689
   438
wenzelm@63654
   439
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
haftmann@58690
   440
  by (auto elim: oddE)
haftmann@58690
   441
haftmann@66815
   442
lemma uminus_power_if:
haftmann@66815
   443
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
haftmann@66815
   444
  by auto
haftmann@66815
   445
wenzelm@63654
   446
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
haftmann@58690
   447
  by simp
haftmann@58689
   448
wenzelm@63654
   449
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
haftmann@58690
   450
  by simp
haftmann@58689
   451
bulwahn@66582
   452
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)"
bulwahn@66582
   453
  by (cases "even (n + k)") auto
bulwahn@66582
   454
wenzelm@63654
   455
end
haftmann@58689
   456
haftmann@58689
   457
context linordered_idom
haftmann@58689
   458
begin
haftmann@58689
   459
wenzelm@63654
   460
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
haftmann@58690
   461
  by (auto elim: evenE)
haftmann@58689
   462
wenzelm@63654
   463
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
haftmann@58689
   464
  by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
haftmann@58689
   465
wenzelm@63654
   466
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
haftmann@58787
   467
  by (auto simp add: zero_le_even_power zero_le_odd_power)
wenzelm@63654
   468
wenzelm@63654
   469
lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
haftmann@58689
   470
proof -
haftmann@58689
   471
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
haftmann@58787
   472
    unfolding power_eq_0_iff [of a n, symmetric] by blast
haftmann@58689
   473
  show ?thesis
wenzelm@63654
   474
    unfolding less_le zero_le_power_eq by auto
haftmann@58689
   475
qed
haftmann@58689
   476
wenzelm@63654
   477
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
haftmann@58689
   478
  unfolding not_le [symmetric] zero_le_power_eq by auto
haftmann@58689
   479
wenzelm@63654
   480
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)"
wenzelm@63654
   481
  unfolding not_less [symmetric] zero_less_power_eq by auto
wenzelm@63654
   482
wenzelm@63654
   483
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
haftmann@58689
   484
  using power_abs [of a n] by (simp add: zero_le_even_power)
haftmann@58689
   485
haftmann@58689
   486
lemma power_mono_even:
haftmann@58689
   487
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
haftmann@58689
   488
  shows "a ^ n \<le> b ^ n"
haftmann@58689
   489
proof -
haftmann@58689
   490
  have "0 \<le> \<bar>a\<bar>" by auto
wenzelm@63654
   491
  with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"
wenzelm@63654
   492
    by (rule power_mono)
wenzelm@63654
   493
  with \<open>even n\<close> show ?thesis
wenzelm@63654
   494
    by (simp add: power_even_abs)
haftmann@58689
   495
qed
haftmann@58689
   496
haftmann@58689
   497
lemma power_mono_odd:
haftmann@58689
   498
  assumes "odd n" and "a \<le> b"
haftmann@58689
   499
  shows "a ^ n \<le> b ^ n"
haftmann@58689
   500
proof (cases "b < 0")
wenzelm@63654
   501
  case True
wenzelm@63654
   502
  with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto
wenzelm@63654
   503
  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
wenzelm@60758
   504
  with \<open>odd n\<close> show ?thesis by simp
haftmann@58689
   505
next
wenzelm@63654
   506
  case False
wenzelm@63654
   507
  then have "0 \<le> b" by auto
haftmann@58689
   508
  show ?thesis
haftmann@58689
   509
  proof (cases "a < 0")
wenzelm@63654
   510
    case True
wenzelm@63654
   511
    then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto
wenzelm@60758
   512
    then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto
wenzelm@63654
   513
    moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto
haftmann@58689
   514
    ultimately show ?thesis by auto
haftmann@58689
   515
  next
wenzelm@63654
   516
    case False
wenzelm@63654
   517
    then have "0 \<le> a" by auto
wenzelm@63654
   518
    with \<open>a \<le> b\<close> show ?thesis
wenzelm@63654
   519
      using power_mono by auto
haftmann@58689
   520
  qed
haftmann@58689
   521
qed
hoelzl@62083
   522
wenzelm@60758
   523
text \<open>Simplify, when the exponent is a numeral\<close>
haftmann@58689
   524
haftmann@58689
   525
lemma zero_le_power_eq_numeral [simp]:
haftmann@58689
   526
  "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"
haftmann@58689
   527
  by (fact zero_le_power_eq)
haftmann@58689
   528
haftmann@58689
   529
lemma zero_less_power_eq_numeral [simp]:
wenzelm@63654
   530
  "0 < a ^ numeral w \<longleftrightarrow>
wenzelm@63654
   531
    numeral w = (0 :: nat) \<or>
wenzelm@63654
   532
    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
wenzelm@63654
   533
    odd (numeral w :: nat) \<and> 0 < a"
haftmann@58689
   534
  by (fact zero_less_power_eq)
haftmann@58689
   535
haftmann@58689
   536
lemma power_le_zero_eq_numeral [simp]:
wenzelm@63654
   537
  "a ^ numeral w \<le> 0 \<longleftrightarrow>
wenzelm@63654
   538
    (0 :: nat) < numeral w \<and>
wenzelm@63654
   539
    (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
haftmann@58689
   540
  by (fact power_le_zero_eq)
haftmann@58689
   541
haftmann@58689
   542
lemma power_less_zero_eq_numeral [simp]:
haftmann@58689
   543
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
haftmann@58689
   544
  by (fact power_less_zero_eq)
haftmann@58689
   545
haftmann@58689
   546
lemma power_even_abs_numeral [simp]:
haftmann@58689
   547
  "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"
haftmann@58689
   548
  by (fact power_even_abs)
haftmann@58689
   549
haftmann@58689
   550
end
haftmann@58689
   551
haftmann@66816
   552
haftmann@66816
   553
subsection \<open>Instance for @{typ int}\<close>
haftmann@66816
   554
haftmann@66816
   555
instance int :: ring_parity
haftmann@66839
   556
  by standard (simp_all add: dvd_eq_mod_eq_0 divide_int_def division_segment_int_def)
haftmann@66816
   557
haftmann@66816
   558
lemma even_diff_iff [simp]:
haftmann@66816
   559
  "even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int
haftmann@66816
   560
  using dvd_add_times_triv_right_iff [of 2 "k - l" l] by (simp add: mult_2_right)
haftmann@66816
   561
haftmann@66816
   562
lemma even_abs_add_iff [simp]:
haftmann@66816
   563
  "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int
haftmann@66816
   564
  by (cases "k \<ge> 0") (simp_all add: ac_simps)
haftmann@66816
   565
haftmann@66816
   566
lemma even_add_abs_iff [simp]:
haftmann@66816
   567
  "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int
haftmann@66816
   568
  using even_abs_add_iff [of l k] by (simp add: ac_simps)
haftmann@66816
   569
haftmann@66816
   570
lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
haftmann@66816
   571
  by (simp add: even_of_nat [of "nat k", where ?'a = int, symmetric])
haftmann@66816
   572
haftmann@58770
   573
end