src/HOL/Parity.thy
author wenzelm
Sat, 17 Mar 2018 20:32:39 +0100
changeset 67895 cd00999d2d30
parent 67828 655d03493d0f
child 67905 fe0f4eeceeb7
permissions -rw-r--r--
more position information;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 36840
diff changeset
     1
(*  Title:      HOL/Parity.thy
b460124855b8 tuned headers;
wenzelm
parents: 36840
diff changeset
     2
    Author:     Jeremy Avigad
b460124855b8 tuned headers;
wenzelm
parents: 36840
diff changeset
     3
    Author:     Jacques D. Fleuriot
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     4
*)
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     5
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
     6
section \<open>Parity in rings and semirings\<close>
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     7
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
     8
theory Parity
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
     9
  imports Euclidean_Division
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
    10
begin
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
    11
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61531
diff changeset
    12
subsection \<open>Ring structures with parity and \<open>even\<close>/\<open>odd\<close> predicates\<close>
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
    13
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    14
class semiring_parity = linordered_semidom + unique_euclidean_semiring +
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    15
  assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n"
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    16
    and division_segment_of_nat [simp]: "division_segment (of_nat n) = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    17
    and division_segment_euclidean_size [simp]: "division_segment a * of_nat (euclidean_size a) = a"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    18
begin
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    19
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    20
lemma division_segment_eq_iff:
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    21
  "a = b" if "division_segment a = division_segment b"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    22
    and "euclidean_size a = euclidean_size b"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    23
  using that division_segment_euclidean_size [of a] by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    24
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    25
lemma euclidean_size_of_nat [simp]:
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    26
  "euclidean_size (of_nat n) = n"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    27
proof -
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    28
  have "division_segment (of_nat n) * of_nat (euclidean_size (of_nat n)) = of_nat n"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    29
    by (fact division_segment_euclidean_size)
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    30
  then show ?thesis by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    31
qed
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    32
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    33
lemma of_nat_euclidean_size:
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    34
  "of_nat (euclidean_size a) = a div division_segment a"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    35
proof -
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    36
  have "of_nat (euclidean_size a) = division_segment a * of_nat (euclidean_size a) div division_segment a"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    37
    by (subst nonzero_mult_div_cancel_left) simp_all
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    38
  also have "\<dots> = a div division_segment a"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    39
    by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    40
  finally show ?thesis .
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    41
qed
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    42
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    43
lemma division_segment_1 [simp]:
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    44
  "division_segment 1 = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    45
  using division_segment_of_nat [of 1] by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    46
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    47
lemma division_segment_numeral [simp]:
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    48
  "division_segment (numeral k) = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    49
  using division_segment_of_nat [of "numeral k"] by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    50
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    51
lemma euclidean_size_1 [simp]:
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    52
  "euclidean_size 1 = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    53
  using euclidean_size_of_nat [of 1] by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    54
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    55
lemma euclidean_size_numeral [simp]:
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    56
  "euclidean_size (numeral k) = numeral k"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    57
  using euclidean_size_of_nat [of "numeral k"] by simp
21256
47195501ecf7 moved theories Parity, GCD, Binomial to Library;
wenzelm
parents:
diff changeset
    58
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    59
lemma of_nat_dvd_iff:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    60
  "of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    61
proof (cases "m = 0")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    62
  case True
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    63
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    64
    by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    65
next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    66
  case False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    67
  show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    68
  proof
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    69
    assume ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    70
    then show ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    71
      by (auto elim: dvd_class.dvdE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    72
  next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    73
    assume ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    74
    with False have "of_nat n = of_nat n div of_nat m * of_nat m"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    75
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    76
    then have "of_nat n = of_nat (n div m * m)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    77
      by (simp add: of_nat_div)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    78
    then have "n = n div m * m"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    79
      by (simp only: of_nat_eq_iff)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    80
    then have "n = m * (n div m)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    81
      by (simp add: ac_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    82
    then show ?Q ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    83
  qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    84
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    85
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    86
lemma of_nat_mod:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    87
  "of_nat (m mod n) = of_nat m mod of_nat n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    88
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    89
  have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    90
    by (simp add: div_mult_mod_eq)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    91
  also have "of_nat m = of_nat (m div n * n + m mod n)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    92
    by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    93
  finally show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    94
    by (simp only: of_nat_div of_nat_mult of_nat_add) simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    95
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    96
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    97
lemma one_div_two_eq_zero [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    98
  "1 div 2 = 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    99
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   100
  from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   101
    by (simp only:) simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   102
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   103
    by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   104
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   105
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   106
lemma one_mod_two_eq_one [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   107
  "1 mod 2 = 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   108
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   109
  from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   110
    by (simp only:) simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   111
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   112
    by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   113
qed
59816
034b13f4efae distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents: 58889
diff changeset
   114
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
   115
abbreviation even :: "'a \<Rightarrow> bool"
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   116
  where "even a \<equiv> 2 dvd a"
54228
229282d53781 purely algebraic foundation for even/odd
haftmann
parents: 54227
diff changeset
   117
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   118
abbreviation odd :: "'a \<Rightarrow> bool"
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   119
  where "odd a \<equiv> \<not> 2 dvd a"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   120
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   121
lemma even_iff_mod_2_eq_zero:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   122
  "even a \<longleftrightarrow> a mod 2 = 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   123
  by (fact dvd_eq_mod_eq_0)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   124
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   125
lemma odd_iff_mod_2_eq_one:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   126
  "odd a \<longleftrightarrow> a mod 2 = 1"
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   127
proof
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   128
  assume "a mod 2 = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   129
  then show "odd a"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   130
    by auto
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   131
next
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   132
  assume "odd a"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   133
  have eucl: "euclidean_size (a mod 2) = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   134
  proof (rule order_antisym)
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   135
    show "euclidean_size (a mod 2) \<le> 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   136
      using mod_size_less [of 2 a] by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   137
    show "1 \<le> euclidean_size (a mod 2)"
66840
0d689d71dbdc canonical multiplicative euclidean size
haftmann
parents: 66839
diff changeset
   138
      using \<open>odd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0)
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   139
  qed 
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   140
  from \<open>odd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   141
    by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   142
  then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   143
    by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment)
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   144
  then have "\<not> 2 dvd euclidean_size a"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   145
    using of_nat_dvd_iff [of 2] by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   146
  then have "euclidean_size a mod 2 = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   147
    by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0)
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   148
  then have "of_nat (euclidean_size a mod 2) = of_nat 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   149
    by simp
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   150
  then have "of_nat (euclidean_size a) mod 2 = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   151
    by (simp add: of_nat_mod)
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   152
  from \<open>odd a\<close> eucl
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   153
  show "a mod 2 = 1"
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   154
    by (auto intro: division_segment_eq_iff simp add: division_segment_mod)
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   155
qed
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   156
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   157
lemma parity_cases [case_names even odd]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   158
  assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   159
  assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   160
  shows P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   161
  using assms by (cases "even a") (simp_all add: odd_iff_mod_2_eq_one)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   162
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   163
lemma not_mod_2_eq_1_eq_0 [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   164
  "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   165
  by (cases a rule: parity_cases) simp_all
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   166
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   167
lemma not_mod_2_eq_0_eq_1 [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   168
  "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   169
  by (cases a rule: parity_cases) simp_all
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   170
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   171
lemma evenE [elim?]:
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   172
  assumes "even a"
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   173
  obtains b where "a = 2 * b"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
   174
  using assms by (rule dvdE)
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   175
58681
a478a0742a8e legacy cleanup
haftmann
parents: 58680
diff changeset
   176
lemma oddE [elim?]:
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   177
  assumes "odd a"
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   178
  obtains b where "a = 2 * b + 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   179
proof -
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   180
  have "a = 2 * (a div 2) + a mod 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   181
    by (simp add: mult_div_mod_eq)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   182
  with assms have "a = 2 * (a div 2) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   183
    by (simp add: odd_iff_mod_2_eq_one)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   184
  then show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   185
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   186
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   187
lemma mod_2_eq_odd:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   188
  "a mod 2 = of_bool (odd a)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   189
  by (auto elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   190
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   191
lemma of_bool_odd_eq_mod_2:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   192
  "of_bool (odd a) = a mod 2"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   193
  by (simp add: mod_2_eq_odd)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   194
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   195
lemma one_mod_2_pow_eq [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   196
  "1 mod (2 ^ n) = of_bool (n > 0)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   197
proof -
67083
6b2c0681ef28 new simp rule
haftmann
parents: 67051
diff changeset
   198
  have "1 mod (2 ^ n) = of_nat (1 mod (2 ^ n))"
6b2c0681ef28 new simp rule
haftmann
parents: 67051
diff changeset
   199
    using of_nat_mod [of 1 "2 ^ n"] by simp
6b2c0681ef28 new simp rule
haftmann
parents: 67051
diff changeset
   200
  also have "\<dots> = of_bool (n > 0)"
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   201
    by simp
67083
6b2c0681ef28 new simp rule
haftmann
parents: 67051
diff changeset
   202
  finally show ?thesis .
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   203
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   204
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   205
lemma one_div_2_pow_eq [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   206
  "1 div (2 ^ n) = of_bool (n = 0)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   207
  using div_mult_mod_eq [of 1 "2 ^ n"] by auto
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   208
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   209
lemma even_of_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   210
  "even (of_nat a) \<longleftrightarrow> even a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   211
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   212
  have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   213
    by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   214
  also have "\<dots> \<longleftrightarrow> even a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   215
    by (simp only: of_nat_dvd_iff)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   216
  finally show ?thesis .
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   217
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   218
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   219
lemma even_zero [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   220
  "even 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   221
  by (fact dvd_0_right)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   222
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   223
lemma odd_one [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   224
  "odd 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   225
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   226
  have "\<not> (2 :: nat) dvd 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   227
    by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   228
  then have "\<not> of_nat 2 dvd of_nat 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   229
    unfolding of_nat_dvd_iff by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   230
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   231
    by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   232
qed
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   233
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   234
lemma odd_even_add:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   235
  "even (a + b)" if "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   236
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   237
  from that obtain c d where "a = 2 * c + 1" and "b = 2 * d + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   238
    by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   239
  then have "a + b = 2 * c + 2 * d + (1 + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   240
    by (simp only: ac_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   241
  also have "\<dots> = 2 * (c + d + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   242
    by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   243
  finally show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   244
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   245
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   246
lemma even_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   247
  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   248
  by (auto simp add: dvd_add_right_iff dvd_add_left_iff odd_even_add)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   249
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   250
lemma odd_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   251
  "odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   252
  by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   253
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   254
lemma even_plus_one_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   255
  "even (a + 1) \<longleftrightarrow> odd a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   256
  by (auto simp add: dvd_add_right_iff intro: odd_even_add)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   257
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   258
lemma even_mult_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   259
  "even (a * b) \<longleftrightarrow> even a \<or> even b" (is "?P \<longleftrightarrow> ?Q")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   260
proof
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   261
  assume ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   262
  then show ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   263
    by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   264
next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   265
  assume ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   266
  show ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   267
  proof (rule ccontr)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   268
    assume "\<not> (even a \<or> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   269
    then have "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   270
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   271
    then obtain r s where "a = 2 * r + 1" and "b = 2 * s + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   272
      by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   273
    then have "a * b = (2 * r + 1) * (2 * s + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   274
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   275
    also have "\<dots> = 2 * (2 * r * s + r + s) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   276
      by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   277
    finally have "odd (a * b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   278
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   279
    with \<open>?P\<close> show False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   280
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   281
  qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   282
qed
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   283
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   284
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   285
proof -
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   286
  have "even (2 * numeral n)"
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   287
    unfolding even_mult_iff by simp
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   288
  then have "even (numeral n + numeral n)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   289
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   290
  then show ?thesis
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   291
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   292
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   293
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   294
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   295
proof
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   296
  assume "even (numeral (num.Bit1 n))"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   297
  then have "even (numeral n + numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   298
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   299
  then have "even (2 * numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   300
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   301
  then have "2 dvd numeral n * 2 + 1"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
   302
    by (simp add: ac_simps)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   303
  then have "2 dvd 1"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   304
    using dvd_add_times_triv_left_iff [of 2 "numeral n" 1] by simp
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   305
  then show False by simp
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   306
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   307
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   308
lemma even_power [simp]: "even (a ^ n) \<longleftrightarrow> even a \<and> n > 0"
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   309
  by (induct n) auto
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   310
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   311
lemma even_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   312
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   313
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   314
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   315
lemma odd_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   316
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   317
  by (auto elim!: oddE simp add: add.assoc)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   318
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   319
lemma even_two_times_div_two:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   320
  "even a \<Longrightarrow> 2 * (a div 2) = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   321
  by (fact dvd_mult_div_cancel)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   322
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   323
lemma odd_two_times_div_two_succ [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   324
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   325
  using mult_div_mod_eq [of 2 a]
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   326
  by (simp add: even_iff_mod_2_eq_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   327
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   328
lemma coprime_left_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   329
  "coprime 2 a \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   330
proof
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   331
  assume "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   332
  show "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   333
  proof (rule coprimeI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   334
    fix b
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   335
    assume "b dvd 2" "b dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   336
    then have "b dvd a mod 2"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   337
      by (auto intro: dvd_mod)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   338
    with \<open>odd a\<close> show "is_unit b"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   339
      by (simp add: mod_2_eq_odd)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   340
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   341
next
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   342
  assume "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   343
  show "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   344
  proof (rule notI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   345
    assume "even a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   346
    then obtain b where "a = 2 * b" ..
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   347
    with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   348
      by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   349
    moreover have "\<not> coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   350
      by (rule not_coprimeI [of 2]) simp_all
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   351
    ultimately show False
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   352
      by blast
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   353
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   354
qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   355
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   356
lemma coprime_right_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   357
  "coprime a 2 \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   358
  using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   359
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   360
lemma div_mult2_eq':
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   361
  "a div (of_nat m * of_nat n) = a div of_nat m div of_nat n"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   362
proof (cases a "of_nat m * of_nat n" rule: divmod_cases)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   363
  case (divides q)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   364
  then show ?thesis
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   365
    using nonzero_mult_div_cancel_right [of "of_nat m" "q * of_nat n"]
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   366
    by (simp add: ac_simps)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   367
next
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   368
  case (remainder q r)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   369
  then have "division_segment r = 1"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   370
    using division_segment_of_nat [of "m * n"] by simp
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   371
  with division_segment_euclidean_size [of r]
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   372
  have "of_nat (euclidean_size r) = r"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   373
    by simp
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   374
  then have "r div of_nat m = of_nat (euclidean_size r) div of_nat m"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   375
    by simp
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   376
  also have "\<dots> = of_nat (euclidean_size r div m)"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   377
    by (simp add: of_nat_div)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   378
  finally have "r div of_nat m = of_nat (euclidean_size r div m)"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   379
    .
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   380
  with remainder(1-3) have "r div of_nat m div of_nat n = 0"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   381
    by (auto intro!: div_eqI [of _ "of_nat (euclidean_size r div m)"])
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   382
      (simp add: division_segment_mult euclidean_size_mult ac_simps less_mult_imp_div_less )
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   383
  with remainder(1)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   384
  have "q = (r div of_nat m + q * of_nat n * of_nat m div of_nat m) div of_nat n"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   385
    by simp
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   386
  with remainder(5-7) show ?thesis
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   387
    using div_plus_div_distrib_dvd_right [of "of_nat m" "q * (of_nat m * of_nat n)" r]
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   388
    by (simp add: ac_simps)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   389
next
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   390
  case by0
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   391
  then show ?thesis
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   392
    by auto
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   393
qed
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   394
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   395
lemma mod_mult2_eq':
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   396
  "a mod (of_nat m * of_nat n) = of_nat m * (a div of_nat m mod of_nat n) + a mod of_nat m"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   397
proof -
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   398
  have "a div (of_nat m * of_nat n) * (of_nat m * of_nat n) + a mod (of_nat m * of_nat n) = a div of_nat m div of_nat n * of_nat n * of_nat m + (a div of_nat m mod of_nat n * of_nat m + a mod of_nat m)"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   399
    by (simp add: combine_common_factor div_mult_mod_eq)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   400
  moreover have "a div of_nat m div of_nat n * of_nat n * of_nat m = of_nat n * of_nat m * (a div of_nat m div of_nat n)"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   401
    by (simp add: ac_simps)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   402
  ultimately show ?thesis
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   403
    by (simp add: div_mult2_eq' mult_commute)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   404
qed
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   405
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   406
end
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   407
59816
034b13f4efae distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents: 58889
diff changeset
   408
class ring_parity = ring + semiring_parity
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   409
begin
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   410
59816
034b13f4efae distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents: 58889
diff changeset
   411
subclass comm_ring_1 ..
034b13f4efae distributivity of partial minus establishes desired properties of dvd in semirings
haftmann
parents: 58889
diff changeset
   412
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   413
lemma even_minus:
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   414
  "even (- a) \<longleftrightarrow> even a"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
   415
  by (fact dvd_minus_iff)
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   416
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   417
lemma even_diff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   418
  "even (a - b) \<longleftrightarrow> even (a + b)"
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   419
  using even_add [of a "- b"] by simp
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   420
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   421
end
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   422
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   423
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   424
subsection \<open>Instance for @{typ nat}\<close>
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   425
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   426
instance nat :: semiring_parity
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   427
  by standard (simp_all add: dvd_eq_mod_eq_0)
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   428
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   429
lemma even_Suc_Suc_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   430
  "even (Suc (Suc n)) \<longleftrightarrow> even n"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   431
  using dvd_add_triv_right_iff [of 2 n] by simp
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   432
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   433
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   434
  using even_plus_one_iff [of n] by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   435
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   436
lemma even_diff_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   437
  "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   438
proof (cases "n \<le> m")
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   439
  case True
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   440
  then have "m - n + n * 2 = m + n" by (simp add: mult_2_right)
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   441
  moreover have "even (m - n) \<longleftrightarrow> even (m - n + n * 2)" by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   442
  ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   443
  then show ?thesis by auto
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   444
next
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   445
  case False
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   446
  then show ?thesis by simp
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   447
qed
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   448
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   449
lemma odd_pos:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   450
  "odd n \<Longrightarrow> 0 < n" for n :: nat
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   451
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   452
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   453
lemma Suc_double_not_eq_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   454
  "Suc (2 * m) \<noteq> 2 * n"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   455
proof
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   456
  assume "Suc (2 * m) = 2 * n"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   457
  moreover have "odd (Suc (2 * m))" and "even (2 * n)"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   458
    by simp_all
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   459
  ultimately show False by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   460
qed
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   461
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   462
lemma double_not_eq_Suc_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   463
  "2 * m \<noteq> Suc (2 * n)"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   464
  using Suc_double_not_eq_double [of n m] by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   465
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   466
lemma odd_Suc_minus_one [simp]: "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   467
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   468
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   469
lemma even_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   470
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   471
  using even_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   472
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   473
lemma odd_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   474
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   475
  using odd_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   476
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   477
lemma odd_two_times_div_two_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   478
  assumes "odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   479
  shows "2 * (n div 2) = n - (1 :: nat)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   480
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   481
  from assms have "2 * (n div 2) + 1 = n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   482
    by (rule odd_two_times_div_two_succ)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   483
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   484
    by simp
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   485
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   486
    by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   487
qed
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   488
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   489
lemma parity_induct [case_names zero even odd]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   490
  assumes zero: "P 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   491
  assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   492
  assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   493
  shows "P n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   494
proof (induct n rule: less_induct)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   495
  case (less n)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   496
  show "P n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   497
  proof (cases "n = 0")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   498
    case True with zero show ?thesis by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   499
  next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   500
    case False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   501
    with less have hyp: "P (n div 2)" by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   502
    show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   503
    proof (cases "even n")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   504
      case True
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   505
      with hyp even [of "n div 2"] show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   506
        by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   507
    next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   508
      case False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   509
      with hyp odd [of "n div 2"] show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   510
        by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   511
    qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   512
  qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   513
qed
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   514
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   515
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   516
subsection \<open>Parity and powers\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   517
61531
ab2e862263e7 Rounding function, uniform limits, cotangent, binomial identities
eberlm
parents: 60867
diff changeset
   518
context ring_1
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   519
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   520
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   521
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   522
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   523
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   524
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   525
  by (auto elim: oddE)
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   526
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   527
lemma uminus_power_if:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   528
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   529
  by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   530
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   531
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   532
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   533
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   534
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   535
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   536
66582
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   537
lemma neg_one_power_add_eq_neg_one_power_diff: "k \<le> n \<Longrightarrow> (- 1) ^ (n + k) = (- 1) ^ (n - k)"
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   538
  by (cases "even (n + k)") auto
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   539
67371
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   540
lemma minus_one_power_iff: "(- 1) ^ n = (if even n then 1 else - 1)"
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   541
  by (induct n) auto
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   542
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   543
end
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   544
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   545
context linordered_idom
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   546
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   547
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   548
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   549
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   550
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   551
lemma zero_le_odd_power: "odd n \<Longrightarrow> 0 \<le> a ^ n \<longleftrightarrow> 0 \<le> a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   552
  by (auto simp add: power_even_eq zero_le_mult_iff elim: oddE)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   553
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   554
lemma zero_le_power_eq: "0 \<le> a ^ n \<longleftrightarrow> even n \<or> odd n \<and> 0 \<le> a"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   555
  by (auto simp add: zero_le_even_power zero_le_odd_power)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   556
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   557
lemma zero_less_power_eq: "0 < a ^ n \<longleftrightarrow> n = 0 \<or> even n \<and> a \<noteq> 0 \<or> odd n \<and> 0 < a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   558
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   559
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   560
    unfolding power_eq_0_iff [of a n, symmetric] by blast
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   561
  show ?thesis
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   562
    unfolding less_le zero_le_power_eq by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   563
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   564
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   565
lemma power_less_zero_eq [simp]: "a ^ n < 0 \<longleftrightarrow> odd n \<and> a < 0"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   566
  unfolding not_le [symmetric] zero_le_power_eq by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   567
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   568
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)"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   569
  unfolding not_less [symmetric] zero_less_power_eq by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   570
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   571
lemma power_even_abs: "even n \<Longrightarrow> \<bar>a\<bar> ^ n = a ^ n"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   572
  using power_abs [of a n] by (simp add: zero_le_even_power)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   573
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   574
lemma power_mono_even:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   575
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   576
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   577
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   578
  have "0 \<le> \<bar>a\<bar>" by auto
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   579
  with \<open>\<bar>a\<bar> \<le> \<bar>b\<bar>\<close> have "\<bar>a\<bar> ^ n \<le> \<bar>b\<bar> ^ n"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   580
    by (rule power_mono)
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   581
  with \<open>even n\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   582
    by (simp add: power_even_abs)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   583
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   584
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   585
lemma power_mono_odd:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   586
  assumes "odd n" and "a \<le> b"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   587
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   588
proof (cases "b < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   589
  case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   590
  with \<open>a \<le> b\<close> have "- b \<le> - a" and "0 \<le> - b" by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   591
  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   592
  with \<open>odd n\<close> show ?thesis by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   593
next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   594
  case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   595
  then have "0 \<le> b" by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   596
  show ?thesis
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   597
  proof (cases "a < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   598
    case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   599
    then have "n \<noteq> 0" and "a \<le> 0" using \<open>odd n\<close> [THEN odd_pos] by auto
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   600
    then have "a ^ n \<le> 0" unfolding power_le_zero_eq using \<open>odd n\<close> by auto
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   601
    moreover from \<open>0 \<le> b\<close> have "0 \<le> b ^ n" by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   602
    ultimately show ?thesis by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   603
  next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   604
    case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   605
    then have "0 \<le> a" by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   606
    with \<open>a \<le> b\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   607
      using power_mono by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   608
  qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   609
qed
62083
7582b39f51ed add the proof of the central limit theorem
hoelzl
parents: 61799
diff changeset
   610
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   611
text \<open>Simplify, when the exponent is a numeral\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   612
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   613
lemma zero_le_power_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   614
  "0 \<le> a ^ numeral w \<longleftrightarrow> even (numeral w :: nat) \<or> odd (numeral w :: nat) \<and> 0 \<le> a"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   615
  by (fact zero_le_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   616
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   617
lemma zero_less_power_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   618
  "0 < a ^ numeral w \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   619
    numeral w = (0 :: nat) \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   620
    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   621
    odd (numeral w :: nat) \<and> 0 < a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   622
  by (fact zero_less_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   623
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   624
lemma power_le_zero_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   625
  "a ^ numeral w \<le> 0 \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   626
    (0 :: nat) < numeral w \<and>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   627
    (odd (numeral w :: nat) \<and> a \<le> 0 \<or> even (numeral w :: nat) \<and> a = 0)"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   628
  by (fact power_le_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   629
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   630
lemma power_less_zero_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   631
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   632
  by (fact power_less_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   633
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   634
lemma power_even_abs_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   635
  "even (numeral w :: nat) \<Longrightarrow> \<bar>a\<bar> ^ numeral w = a ^ numeral w"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   636
  by (fact power_even_abs)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   637
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   638
end
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   639
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   640
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   641
subsection \<open>Instance for @{typ int}\<close>
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   642
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   643
instance int :: ring_parity
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   644
  by standard (simp_all add: dvd_eq_mod_eq_0 divide_int_def division_segment_int_def)
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   645
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   646
lemma even_diff_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   647
  "even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   648
  by (fact even_diff)
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   649
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   650
lemma even_abs_add_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   651
  "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   652
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   653
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   654
lemma even_add_abs_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   655
  "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   656
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   657
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   658
lemma even_nat_iff: "0 \<le> k \<Longrightarrow> even (nat k) \<longleftrightarrow> even k"
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   659
  by (simp add: even_of_nat [of "nat k", where ?'a = int, symmetric])
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   660
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   661
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   662
subsection \<open>Abstract bit operations\<close>
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   663
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   664
context semiring_parity
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   665
begin
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   666
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   667
text \<open>The primary purpose of the following operations is
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   668
  to avoid ad-hoc simplification of concrete expressions @{term "2 ^ n"}\<close>
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   669
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   670
definition bit_push :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   671
  where bit_push_eq_mult: "bit_push n a = a * 2 ^ n"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   672
 
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   673
definition bit_take :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   674
  where bit_take_eq_mod: "bit_take n a = a mod of_nat (2 ^ n)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   675
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   676
definition bit_drop :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   677
  where bit_drop_eq_div: "bit_drop n a = a div of_nat (2 ^ n)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   678
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   679
lemma bit_ident:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   680
  "bit_push n (bit_drop n a) + bit_take n a = a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   681
  using div_mult_mod_eq by (simp add: bit_push_eq_mult bit_take_eq_mod bit_drop_eq_div)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   682
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   683
lemma bit_take_bit_take [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   684
  "bit_take n (bit_take n a) = bit_take n a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   685
  by (simp add: bit_take_eq_mod)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   686
  
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   687
lemma bit_take_0 [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   688
  "bit_take 0 a = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   689
  by (simp add: bit_take_eq_mod)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   690
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   691
lemma bit_take_Suc [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   692
  "bit_take (Suc n) a = bit_take n (a div 2) * 2 + of_bool (odd a)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   693
proof -
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   694
  have "1 + 2 * (a div 2) mod (2 * 2 ^ n) = (a div 2 * 2 + a mod 2) mod (2 * 2 ^ n)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   695
    if "odd a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   696
    using that mod_mult2_eq' [of "1 + 2 * (a div 2)" 2 "2 ^ n"]
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   697
    by (simp add: ac_simps odd_iff_mod_2_eq_one mult_mod_right)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   698
  also have "\<dots> = a mod (2 * 2 ^ n)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   699
    by (simp only: div_mult_mod_eq)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   700
  finally show ?thesis
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   701
    by (simp add: bit_take_eq_mod algebra_simps mult_mod_right)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   702
qed
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   703
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   704
lemma bit_take_of_0 [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   705
  "bit_take n 0 = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   706
  by (simp add: bit_take_eq_mod)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   707
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   708
lemma bit_take_plus:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   709
  "bit_take n (bit_take n a + bit_take n b) = bit_take n (a + b)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   710
  by (simp add: bit_take_eq_mod mod_simps)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   711
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   712
lemma bit_take_of_1_eq_0_iff [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   713
  "bit_take n 1 = 0 \<longleftrightarrow> n = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   714
  by (simp add: bit_take_eq_mod)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   715
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   716
lemma bit_push_eq_0_iff [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   717
  "bit_push n a = 0 \<longleftrightarrow> a = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   718
  by (simp add: bit_push_eq_mult)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   719
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   720
lemma bit_drop_0 [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   721
  "bit_drop 0 = id"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   722
  by (simp add: fun_eq_iff bit_drop_eq_div)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   723
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   724
lemma bit_drop_of_0 [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   725
  "bit_drop n 0 = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   726
  by (simp add: bit_drop_eq_div)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   727
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   728
lemma bit_drop_Suc [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   729
  "bit_drop (Suc n) a = bit_drop n (a div 2)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   730
proof (cases "even a")
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   731
  case True
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   732
  then obtain b where "a = 2 * b" ..
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   733
  moreover have "bit_drop (Suc n) (2 * b) = bit_drop n b"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   734
    by (simp add: bit_drop_eq_div)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   735
  ultimately show ?thesis
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   736
    by simp
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   737
next
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   738
  case False
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   739
  then obtain b where "a = 2 * b + 1" ..
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   740
  moreover have "bit_drop (Suc n) (2 * b + 1) = bit_drop n b"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   741
    using div_mult2_eq' [of "1 + b * 2" 2 "2 ^ n"]
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   742
    by (auto simp add: bit_drop_eq_div ac_simps)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   743
  ultimately show ?thesis
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   744
    by simp
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   745
qed
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   746
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   747
lemma bit_drop_half:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   748
  "bit_drop n (a div 2) = bit_drop n a div 2"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   749
  by (induction n arbitrary: a) simp_all
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   750
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   751
lemma bit_drop_of_bool [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   752
  "bit_drop n (of_bool d) = of_bool (n = 0 \<and> d)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   753
  by (cases n) simp_all
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   754
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   755
lemma even_bit_take_eq [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   756
  "even (bit_take n a) \<longleftrightarrow> n = 0 \<or> even a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   757
  by (cases n) (simp_all add: bit_take_eq_mod dvd_mod_iff)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   758
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   759
lemma bit_push_0_id [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   760
  "bit_push 0 = id"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   761
  by (simp add: fun_eq_iff bit_push_eq_mult)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   762
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   763
lemma bit_push_of_0 [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   764
  "bit_push n 0 = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   765
  by (simp add: bit_push_eq_mult)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   766
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   767
lemma bit_push_of_1:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   768
  "bit_push n 1 = 2 ^ n"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   769
  by (simp add: bit_push_eq_mult)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   770
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   771
lemma bit_push_Suc [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   772
  "bit_push (Suc n) a = bit_push n (a * 2)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   773
  by (simp add: bit_push_eq_mult ac_simps)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   774
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   775
lemma bit_push_double:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   776
  "bit_push n (a * 2) = bit_push n a * 2"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   777
  by (simp add: bit_push_eq_mult ac_simps)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   778
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   779
lemma bit_drop_bit_take [simp]:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   780
  "bit_drop n (bit_take n a) = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   781
  by (simp add: bit_drop_eq_div bit_take_eq_mod)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   782
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   783
lemma bit_take_bit_drop_commute:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   784
  "bit_drop m (bit_take n a) = bit_take (n - m) (bit_drop m a)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   785
  for m n :: nat
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   786
proof (cases "n \<ge> m")
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   787
  case True
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   788
  moreover define q where "q = n - m"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   789
  ultimately have "n - m = q" and "n = m + q"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   790
    by simp_all
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   791
  moreover have "bit_drop m (bit_take (m + q) a) = bit_take q (bit_drop m a)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   792
    using mod_mult2_eq' [of a "2 ^ m" "2 ^ q"]
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   793
    by (simp add: bit_drop_eq_div bit_take_eq_mod power_add)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   794
  ultimately show ?thesis
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   795
    by simp
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   796
next
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   797
  case False
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   798
  moreover define q where "q = m - n"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   799
  ultimately have "m - n = q" and "m = n + q"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   800
    by simp_all
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   801
  moreover have "bit_drop (n + q) (bit_take n a) = 0"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   802
    using div_mult2_eq' [of "a mod 2 ^ n" "2 ^ n" "2 ^ q"]
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   803
    by (simp add: bit_drop_eq_div bit_take_eq_mod power_add div_mult2_eq)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   804
  ultimately show ?thesis
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   805
    by simp
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   806
qed
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   807
58770
ae5e9b4f8daf downshift of theory Parity in the hierarchy
haftmann
parents: 58769
diff changeset
   808
end
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   809
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   810
end