src/HOL/Parity.thy
author haftmann
Sun, 16 Jun 2019 16:40:57 +0000
changeset 70353 7aa64296b9b0
parent 70341 972c0c744e7c
child 70365 4df0628e8545
permissions -rw-r--r--
even more appropriate fact name
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
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    14
class semiring_parity = comm_semiring_1 + semiring_modulo +
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    15
  assumes even_iff_mod_2_eq_zero: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    16
    and odd_iff_mod_2_eq_one: "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    17
    and odd_one [simp]: "\<not> 2 dvd 1"
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    18
begin
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
    19
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
    20
abbreviation even :: "'a \<Rightarrow> bool"
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
    21
  where "even a \<equiv> 2 dvd a"
54228
229282d53781 purely algebraic foundation for even/odd
haftmann
parents: 54227
diff changeset
    22
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
    23
abbreviation odd :: "'a \<Rightarrow> bool"
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
    24
  where "odd a \<equiv> \<not> 2 dvd a"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
    25
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    26
lemma parity_cases [case_names even odd]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    27
  assumes "even a \<Longrightarrow> a mod 2 = 0 \<Longrightarrow> P"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    28
  assumes "odd a \<Longrightarrow> a mod 2 = 1 \<Longrightarrow> P"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    29
  shows P
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    30
  using assms by (cases "even a")
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    31
    (simp_all add: even_iff_mod_2_eq_zero [symmetric] odd_iff_mod_2_eq_one [symmetric])
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    32
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    33
lemma not_mod_2_eq_0_eq_1 [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    34
  "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    35
  by (cases a rule: parity_cases) simp_all
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    36
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    37
lemma not_mod_2_eq_1_eq_0 [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    38
  "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    39
  by (cases a rule: parity_cases) simp_all
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    40
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    41
lemma mod2_eq_if: "a mod 2 = (if 2 dvd a then 0 else 1)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    42
  by (simp add: even_iff_mod_2_eq_zero odd_iff_mod_2_eq_one)
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
    43
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    44
lemma evenE [elim?]:
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    45
  assumes "even a"
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    46
  obtains b where "a = 2 * b"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
    47
  using assms by (rule dvdE)
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    48
58681
a478a0742a8e legacy cleanup
haftmann
parents: 58680
diff changeset
    49
lemma oddE [elim?]:
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
    50
  assumes "odd a"
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
    51
  obtains b where "a = 2 * b + 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
    52
proof -
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    53
  have "a = 2 * (a div 2) + a mod 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    54
    by (simp add: mult_div_mod_eq)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    55
  with assms have "a = 2 * (a div 2) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    56
    by (simp add: odd_iff_mod_2_eq_one)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    57
  then show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    58
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    59
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    60
lemma mod_2_eq_odd:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    61
  "a mod 2 = of_bool (odd a)"
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    62
  by (auto elim: oddE simp add: even_iff_mod_2_eq_zero)
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    63
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    64
lemma of_bool_odd_eq_mod_2:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    65
  "of_bool (odd a) = a mod 2"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    66
  by (simp add: mod_2_eq_odd)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    67
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    68
lemma even_zero [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    69
  "even 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    70
  by (fact dvd_0_right)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    71
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    72
lemma odd_even_add:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    73
  "even (a + b)" if "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    74
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    75
  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
    76
    by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    77
  then have "a + b = 2 * c + 2 * d + (1 + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    78
    by (simp only: ac_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    79
  also have "\<dots> = 2 * (c + d + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    80
    by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    81
  finally show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    82
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    83
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    84
lemma even_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    85
  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    86
  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
    87
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    88
lemma odd_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    89
  "odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    90
  by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    91
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    92
lemma even_plus_one_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    93
  "even (a + 1) \<longleftrightarrow> odd a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    94
  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
    95
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    96
lemma even_mult_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    97
  "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
    98
proof
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    99
  assume ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   100
  then show ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   101
    by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   102
next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   103
  assume ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   104
  show ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   105
  proof (rule ccontr)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   106
    assume "\<not> (even a \<or> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   107
    then have "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   108
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   109
    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
   110
      by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   111
    then have "a * b = (2 * r + 1) * (2 * s + 1)"
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
    also have "\<dots> = 2 * (2 * r * s + r + s) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   114
      by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   115
    finally have "odd (a * b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   116
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   117
    with \<open>?P\<close> show False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   118
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   119
  qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   120
qed
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   121
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   122
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   123
proof -
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   124
  have "even (2 * numeral n)"
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   125
    unfolding even_mult_iff by simp
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   126
  then have "even (numeral n + numeral n)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   127
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   128
  then show ?thesis
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   129
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   130
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   131
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   132
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   133
proof
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   134
  assume "even (numeral (num.Bit1 n))"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   135
  then have "even (numeral n + numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   136
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   137
  then have "even (2 * numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   138
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   139
  then have "2 dvd numeral n * 2 + 1"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
   140
    by (simp add: ac_simps)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   141
  then have "2 dvd 1"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   142
    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
   143
  then show False by simp
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   144
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   145
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   146
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
   147
  by (induct n) auto
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   148
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   149
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   150
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   151
class ring_parity = ring + semiring_parity
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   152
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   153
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   154
subclass comm_ring_1 ..
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   155
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   156
lemma even_minus:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   157
  "even (- a) \<longleftrightarrow> even a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   158
  by (fact dvd_minus_iff)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   159
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   160
lemma even_diff [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   161
  "even (a - b) \<longleftrightarrow> even (a + b)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   162
  using even_add [of a "- b"] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   163
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   164
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   165
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   166
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   167
subsection \<open>Special case: euclidean rings containing the natural numbers\<close>
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   168
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   169
class unique_euclidean_semiring_with_nat = semidom + semiring_char_0 + unique_euclidean_semiring +
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   170
  assumes of_nat_div: "of_nat (m div n) = of_nat m div of_nat n"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   171
    and division_segment_of_nat [simp]: "division_segment (of_nat n) = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   172
    and division_segment_euclidean_size [simp]: "division_segment a * of_nat (euclidean_size a) = a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   173
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   174
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   175
lemma division_segment_eq_iff:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   176
  "a = b" if "division_segment a = division_segment b"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   177
    and "euclidean_size a = euclidean_size b"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   178
  using that division_segment_euclidean_size [of a] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   179
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   180
lemma euclidean_size_of_nat [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   181
  "euclidean_size (of_nat n) = n"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   182
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   183
  have "division_segment (of_nat n) * of_nat (euclidean_size (of_nat n)) = of_nat n"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   184
    by (fact division_segment_euclidean_size)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   185
  then show ?thesis by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   186
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   187
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   188
lemma of_nat_euclidean_size:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   189
  "of_nat (euclidean_size a) = a div division_segment a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   190
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   191
  have "of_nat (euclidean_size a) = division_segment a * of_nat (euclidean_size a) div division_segment a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   192
    by (subst nonzero_mult_div_cancel_left) simp_all
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   193
  also have "\<dots> = a div division_segment a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   194
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   195
  finally show ?thesis .
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   196
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   197
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   198
lemma division_segment_1 [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   199
  "division_segment 1 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   200
  using division_segment_of_nat [of 1] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   201
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   202
lemma division_segment_numeral [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   203
  "division_segment (numeral k) = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   204
  using division_segment_of_nat [of "numeral k"] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   205
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   206
lemma euclidean_size_1 [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   207
  "euclidean_size 1 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   208
  using euclidean_size_of_nat [of 1] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   209
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   210
lemma euclidean_size_numeral [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   211
  "euclidean_size (numeral k) = numeral k"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   212
  using euclidean_size_of_nat [of "numeral k"] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   213
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   214
lemma of_nat_dvd_iff:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   215
  "of_nat m dvd of_nat n \<longleftrightarrow> m dvd n" (is "?P \<longleftrightarrow> ?Q")
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   216
proof (cases "m = 0")
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   217
  case True
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   218
  then show ?thesis
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   219
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   220
next
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   221
  case False
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   222
  show ?thesis
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   223
  proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   224
    assume ?Q
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   225
    then show ?P
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   226
      by auto
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   227
  next
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   228
    assume ?P
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   229
    with False have "of_nat n = of_nat n div of_nat m * of_nat m"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   230
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   231
    then have "of_nat n = of_nat (n div m * m)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   232
      by (simp add: of_nat_div)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   233
    then have "n = n div m * m"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   234
      by (simp only: of_nat_eq_iff)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   235
    then have "n = m * (n div m)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   236
      by (simp add: ac_simps)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   237
    then show ?Q ..
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   238
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   239
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   240
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   241
lemma of_nat_mod:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   242
  "of_nat (m mod n) = of_nat m mod of_nat n"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   243
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   244
  have "of_nat m div of_nat n * of_nat n + of_nat m mod of_nat n = of_nat m"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   245
    by (simp add: div_mult_mod_eq)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   246
  also have "of_nat m = of_nat (m div n * n + m mod n)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   247
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   248
  finally show ?thesis
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   249
    by (simp only: of_nat_div of_nat_mult of_nat_add) simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   250
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   251
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   252
lemma one_div_two_eq_zero [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   253
  "1 div 2 = 0"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   254
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   255
  from of_nat_div [symmetric] have "of_nat 1 div of_nat 2 = of_nat 0"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   256
    by (simp only:) simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   257
  then show ?thesis
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   258
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   259
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   260
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   261
lemma one_mod_two_eq_one [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   262
  "1 mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   263
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   264
  from of_nat_mod [symmetric] have "of_nat 1 mod of_nat 2 = of_nat 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   265
    by (simp only:) simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   266
  then show ?thesis
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   267
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   268
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   269
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   270
subclass semiring_parity
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   271
proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   272
  show "2 dvd a \<longleftrightarrow> a mod 2 = 0" for a
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   273
    by (fact dvd_eq_mod_eq_0)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   274
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" for a
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   275
  proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   276
    assume "a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   277
    then show "\<not> 2 dvd a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   278
      by auto
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   279
  next
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   280
    assume "\<not> 2 dvd a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   281
    have eucl: "euclidean_size (a mod 2) = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   282
    proof (rule order_antisym)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   283
      show "euclidean_size (a mod 2) \<le> 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   284
        using mod_size_less [of 2 a] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   285
      show "1 \<le> euclidean_size (a mod 2)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   286
        using \<open>\<not> 2 dvd a\<close> by (simp add: Suc_le_eq dvd_eq_mod_eq_0)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   287
    qed 
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   288
    from \<open>\<not> 2 dvd a\<close> have "\<not> of_nat 2 dvd division_segment a * of_nat (euclidean_size a)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   289
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   290
    then have "\<not> of_nat 2 dvd of_nat (euclidean_size a)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   291
      by (auto simp only: dvd_mult_unit_iff' is_unit_division_segment)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   292
    then have "\<not> 2 dvd euclidean_size a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   293
      using of_nat_dvd_iff [of 2] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   294
    then have "euclidean_size a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   295
      by (simp add: semidom_modulo_class.dvd_eq_mod_eq_0)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   296
    then have "of_nat (euclidean_size a mod 2) = of_nat 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   297
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   298
    then have "of_nat (euclidean_size a) mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   299
      by (simp add: of_nat_mod)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   300
    from \<open>\<not> 2 dvd a\<close> eucl
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   301
    show "a mod 2 = 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   302
      by (auto intro: division_segment_eq_iff simp add: division_segment_mod)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   303
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   304
  show "\<not> is_unit 2"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   305
  proof (rule notI)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   306
    assume "is_unit 2"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   307
    then have "of_nat 2 dvd of_nat 1"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   308
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   309
    then have "is_unit (2::nat)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   310
      by (simp only: of_nat_dvd_iff)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   311
    then show False
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   312
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   313
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   314
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   315
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   316
lemma even_of_nat [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   317
  "even (of_nat a) \<longleftrightarrow> even a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   318
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   319
  have "even (of_nat a) \<longleftrightarrow> of_nat 2 dvd of_nat a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   320
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   321
  also have "\<dots> \<longleftrightarrow> even a"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   322
    by (simp only: of_nat_dvd_iff)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   323
  finally show ?thesis .
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   324
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   325
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   326
lemma one_mod_2_pow_eq [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   327
  "1 mod (2 ^ n) = of_bool (n > 0)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   328
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   329
  have "1 mod (2 ^ n) = of_nat (1 mod (2 ^ n))"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   330
    using of_nat_mod [of 1 "2 ^ n"] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   331
  also have "\<dots> = of_bool (n > 0)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   332
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   333
  finally show ?thesis .
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   334
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   335
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   336
lemma one_div_2_pow_eq [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   337
  "1 div (2 ^ n) = of_bool (n = 0)"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   338
  using div_mult_mod_eq [of 1 "2 ^ n"] by auto
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   339
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   340
lemma even_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   341
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   342
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   343
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   344
lemma odd_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   345
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   346
  by (auto elim!: oddE simp add: add.assoc)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   347
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   348
lemma even_two_times_div_two:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   349
  "even a \<Longrightarrow> 2 * (a div 2) = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   350
  by (fact dvd_mult_div_cancel)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   351
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   352
lemma odd_two_times_div_two_succ [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   353
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   354
  using mult_div_mod_eq [of 2 a]
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   355
  by (simp add: even_iff_mod_2_eq_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   356
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   357
lemma coprime_left_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   358
  "coprime 2 a \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   359
proof
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   360
  assume "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   361
  show "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   362
  proof (rule coprimeI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   363
    fix b
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   364
    assume "b dvd 2" "b dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   365
    then have "b dvd a mod 2"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   366
      by (auto intro: dvd_mod)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   367
    with \<open>odd a\<close> show "is_unit b"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   368
      by (simp add: mod_2_eq_odd)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   369
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   370
next
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   371
  assume "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   372
  show "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   373
  proof (rule notI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   374
    assume "even a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   375
    then obtain b where "a = 2 * b" ..
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   376
    with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   377
      by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   378
    moreover have "\<not> coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   379
      by (rule not_coprimeI [of 2]) simp_all
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   380
    ultimately show False
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   381
      by blast
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   382
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   383
qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   384
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   385
lemma coprime_right_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   386
  "coprime a 2 \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   387
  using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   388
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   389
lemma div_mult2_eq':
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   390
  "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
   391
proof (cases a "of_nat m * of_nat n" rule: divmod_cases)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   392
  case (divides q)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   393
  then show ?thesis
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   394
    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
   395
    by (simp add: ac_simps)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   396
next
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   397
  case (remainder q r)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   398
  then have "division_segment r = 1"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   399
    using division_segment_of_nat [of "m * n"] by simp
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   400
  with division_segment_euclidean_size [of r]
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   401
  have "of_nat (euclidean_size r) = r"
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   402
    by simp
67908
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   403
  have "a mod (of_nat m * of_nat n) div (of_nat m * of_nat n) = 0"
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   404
    by simp
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   405
  with remainder(6) have "r div (of_nat m * of_nat n) = 0"
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   406
    by simp
67908
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   407
  with \<open>of_nat (euclidean_size r) = r\<close>
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   408
  have "of_nat (euclidean_size r) div (of_nat m * of_nat n) = 0"
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   409
    by simp
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   410
  then have "of_nat (euclidean_size r div (m * n)) = 0"
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   411
    by (simp add: of_nat_div)
67908
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   412
  then have "of_nat (euclidean_size r div m div n) = 0"
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   413
    by (simp add: div_mult2_eq)
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   414
  with \<open>of_nat (euclidean_size r) = r\<close> have "r div of_nat m div of_nat n = 0"
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   415
    by (simp add: of_nat_div)
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   416
  with remainder(1)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   417
  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
   418
    by simp
67908
537f891d8f14 tuned proof
haftmann
parents: 67907
diff changeset
   419
  with remainder(5) remainder(7) show ?thesis
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   420
    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
   421
    by (simp add: ac_simps)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   422
next
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   423
  case by0
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   424
  then show ?thesis
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   425
    by auto
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   426
qed
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   427
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   428
lemma mod_mult2_eq':
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   429
  "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
   430
proof -
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   431
  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
   432
    by (simp add: combine_common_factor div_mult_mod_eq)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   433
  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
   434
    by (simp add: ac_simps)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   435
  ultimately show ?thesis
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   436
    by (simp add: div_mult2_eq' mult_commute)
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   437
qed
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   438
68028
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   439
lemma div_mult2_numeral_eq:
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   440
  "a div numeral k div numeral l = a div numeral (k * l)" (is "?A = ?B")
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   441
proof -
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   442
  have "?A = a div of_nat (numeral k) div of_nat (numeral l)"
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   443
    by simp
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   444
  also have "\<dots> = a div (of_nat (numeral k) * of_nat (numeral l))"
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   445
    by (fact div_mult2_eq' [symmetric])
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   446
  also have "\<dots> = ?B"
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   447
    by simp
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   448
  finally show ?thesis .
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   449
qed
1f9f973eed2a proper datatype for 8-bit characters
haftmann
parents: 68010
diff changeset
   450
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   451
end
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   452
70340
7383930fc946 slightly more specialized name for type class
haftmann
parents: 70339
diff changeset
   453
class unique_euclidean_ring_with_nat = ring + unique_euclidean_semiring_with_nat
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   454
begin
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   455
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   456
subclass ring_parity ..
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   457
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   458
lemma minus_1_mod_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   459
  "- 1 mod 2 = 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   460
  by (simp add: mod_2_eq_odd)
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   461
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   462
lemma minus_1_div_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   463
  "- 1 div 2 = - 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   464
proof -
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   465
  from div_mult_mod_eq [of "- 1" 2]
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   466
  have "- 1 div 2 * 2 = - 1 * 2"
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   467
    using add_implies_diff by fastforce
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   468
  then show ?thesis
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   469
    using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   470
qed
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   471
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   472
end
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   473
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   474
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   475
subsection \<open>Instance for \<^typ>\<open>nat\<close>\<close>
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   476
70340
7383930fc946 slightly more specialized name for type class
haftmann
parents: 70339
diff changeset
   477
instance nat :: unique_euclidean_semiring_with_nat
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   478
  by standard (simp_all add: dvd_eq_mod_eq_0)
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   479
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   480
lemma even_Suc_Suc_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   481
  "even (Suc (Suc n)) \<longleftrightarrow> even n"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   482
  using dvd_add_triv_right_iff [of 2 n] by simp
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   483
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   484
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   485
  using even_plus_one_iff [of n] by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   486
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   487
lemma even_diff_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   488
  "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   489
proof (cases "n \<le> m")
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   490
  case True
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   491
  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
   492
  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
   493
  ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   494
  then show ?thesis by auto
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   495
next
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   496
  case False
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   497
  then show ?thesis by simp
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   498
qed
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   499
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   500
lemma odd_pos:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   501
  "odd n \<Longrightarrow> 0 < n" for n :: nat
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   502
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   503
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   504
lemma Suc_double_not_eq_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   505
  "Suc (2 * m) \<noteq> 2 * n"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   506
proof
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   507
  assume "Suc (2 * m) = 2 * n"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   508
  moreover have "odd (Suc (2 * m))" and "even (2 * n)"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   509
    by simp_all
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   510
  ultimately show False by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   511
qed
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   512
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   513
lemma double_not_eq_Suc_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   514
  "2 * m \<noteq> Suc (2 * n)"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   515
  using Suc_double_not_eq_double [of n m] by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   516
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   517
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
   518
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   519
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   520
lemma even_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   521
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   522
  using even_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   523
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   524
lemma odd_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   525
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   526
  using odd_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   527
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   528
lemma odd_two_times_div_two_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   529
  assumes "odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   530
  shows "2 * (n div 2) = n - (1 :: nat)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   531
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   532
  from assms have "2 * (n div 2) + 1 = n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   533
    by (rule odd_two_times_div_two_succ)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   534
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   535
    by simp
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   536
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   537
    by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   538
qed
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   539
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   540
lemma not_mod2_eq_Suc_0_eq_0 [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   541
  "n mod 2 \<noteq> Suc 0 \<longleftrightarrow> n mod 2 = 0"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   542
  using not_mod_2_eq_1_eq_0 [of n] by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   543
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
   544
lemma nat_bit_induct [case_names zero even odd]:
70226
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   545
  "P n" if zero: "P 0"
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   546
    and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)"
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   547
    and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   548
proof (induction n rule: less_induct)
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   549
  case (less n)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   550
  show "P n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   551
  proof (cases "n = 0")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   552
    case True with zero show ?thesis by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   553
  next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   554
    case False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   555
    with less have hyp: "P (n div 2)" by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   556
    show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   557
    proof (cases "even n")
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   558
      case True
70226
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   559
      then have "n \<noteq> 1"
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   560
        by auto
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   561
      with \<open>n \<noteq> 0\<close> have "n div 2 > 0"
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   562
        by simp
accbd801fefa more lemmas
haftmann
parents: 69593
diff changeset
   563
      with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   564
        by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   565
    next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   566
      case False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   567
      with hyp odd [of "n div 2"] show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   568
        by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   569
    qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   570
  qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   571
qed
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   572
69502
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   573
lemma odd_card_imp_not_empty:
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   574
  \<open>A \<noteq> {}\<close> if \<open>odd (card A)\<close>
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   575
  using that by auto
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   576
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   577
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   578
subsection \<open>Parity and powers\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   579
61531
ab2e862263e7 Rounding function, uniform limits, cotangent, binomial identities
eberlm
parents: 60867
diff changeset
   580
context ring_1
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   581
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   582
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   583
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   584
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   585
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   586
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   587
  by (auto elim: oddE)
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   588
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   589
lemma uminus_power_if:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   590
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   591
  by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   592
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   593
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   594
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   595
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   596
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   597
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   598
66582
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   599
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
   600
  by (cases "even (n + k)") auto
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   601
67371
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   602
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
   603
  by (induct n) auto
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   604
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   605
end
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   606
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   607
context linordered_idom
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   608
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   609
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   610
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   611
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   612
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   613
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
   614
  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
   615
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   616
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
   617
  by (auto simp add: zero_le_even_power zero_le_odd_power)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   618
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   619
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
   620
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   621
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   622
    unfolding power_eq_0_iff [of a n, symmetric] by blast
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   623
  show ?thesis
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   624
    unfolding less_le zero_le_power_eq by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   625
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   626
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   627
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
   628
  unfolding not_le [symmetric] zero_le_power_eq by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   629
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   630
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
   631
  unfolding not_less [symmetric] zero_less_power_eq by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   632
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   633
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
   634
  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
   635
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   636
lemma power_mono_even:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   637
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   638
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   639
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   640
  have "0 \<le> \<bar>a\<bar>" by auto
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   641
  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
   642
    by (rule power_mono)
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   643
  with \<open>even n\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   644
    by (simp add: power_even_abs)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   645
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   646
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   647
lemma power_mono_odd:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   648
  assumes "odd n" and "a \<le> b"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   649
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   650
proof (cases "b < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   651
  case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   652
  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
   653
  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   654
  with \<open>odd n\<close> show ?thesis by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   655
next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   656
  case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   657
  then have "0 \<le> b" by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   658
  show ?thesis
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   659
  proof (cases "a < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   660
    case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   661
    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
   662
    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
   663
    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
   664
    ultimately show ?thesis by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   665
  next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   666
    case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   667
    then have "0 \<le> a" by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   668
    with \<open>a \<le> b\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   669
      using power_mono by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   670
  qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   671
qed
62083
7582b39f51ed add the proof of the central limit theorem
hoelzl
parents: 61799
diff changeset
   672
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   673
text \<open>Simplify, when the exponent is a numeral\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   674
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   675
lemma zero_le_power_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   676
  "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
   677
  by (fact zero_le_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   678
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   679
lemma zero_less_power_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   680
  "0 < a ^ numeral w \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   681
    numeral w = (0 :: nat) \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   682
    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   683
    odd (numeral w :: nat) \<and> 0 < a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   684
  by (fact zero_less_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   685
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   686
lemma power_le_zero_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   687
  "a ^ numeral w \<le> 0 \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   688
    (0 :: nat) < numeral w \<and>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   689
    (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
   690
  by (fact power_le_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   691
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   692
lemma power_less_zero_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   693
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   694
  by (fact power_less_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   695
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   696
lemma power_even_abs_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   697
  "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
   698
  by (fact power_even_abs)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   699
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   700
end
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   701
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   702
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   703
subsection \<open>Instance for \<^typ>\<open>int\<close>\<close>
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   704
70340
7383930fc946 slightly more specialized name for type class
haftmann
parents: 70339
diff changeset
   705
instance int :: unique_euclidean_ring_with_nat
66839
909ba5ed93dd clarified parity
haftmann
parents: 66816
diff changeset
   706
  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
   707
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   708
lemma even_diff_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   709
  "even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   710
  by (fact even_diff)
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   711
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   712
lemma even_abs_add_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   713
  "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   714
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   715
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   716
lemma even_add_abs_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   717
  "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   718
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   719
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   720
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
   721
  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
   722
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
   723
lemma int_bit_induct [case_names zero minus even odd]:
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   724
  "P k" if zero_int: "P 0"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   725
    and minus_int: "P (- 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   726
    and even_int: "\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   727
    and odd_int: "\<And>k. P k \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> P (1 + (k * 2))" for k :: int
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   728
proof (cases "k \<ge> 0")
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   729
  case True
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   730
  define n where "n = nat k"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   731
  with True have "k = int n"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   732
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   733
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
   734
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   735
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   736
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   737
      by (simp add: zero_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   738
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   739
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   740
    have "P (int n * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   741
      by (rule even_int) (use even in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   742
    with even show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   743
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   744
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   745
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   746
    have "P (1 + (int n * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   747
      by (rule odd_int) (use odd in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   748
    with odd show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   749
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   750
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   751
next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   752
  case False
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   753
  define n where "n = nat (- k - 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   754
  with False have "k = - int n - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   755
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   756
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
   757
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   758
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   759
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   760
      by (simp add: minus_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   761
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   762
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   763
    have "P (1 + (- int (Suc n) * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   764
      by (rule odd_int) (use even in \<open>simp_all add: algebra_simps\<close>)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   765
    also have "\<dots> = - int (2 * n) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   766
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   767
    finally show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   768
      using even by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   769
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   770
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   771
    have "P (- int (Suc n) * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   772
      by (rule even_int) (use odd in \<open>simp_all add: algebra_simps\<close>)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   773
    also have "\<dots> = - int (Suc (2 * n)) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   774
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   775
    finally show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   776
      using odd by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   777
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   778
qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
   779
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   780
67828
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   781
subsection \<open>Abstract bit operations\<close>
655d03493d0f eliminiated superfluous class semiring_bits
haftmann
parents: 67816
diff changeset
   782
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   783
context semiring_parity
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   784
begin
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   785
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   786
text \<open>The primary purpose of the following operations is
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   787
  to avoid ad-hoc simplification of concrete expressions \<^term>\<open>2 ^ n\<close>\<close>
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   788
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   789
definition push_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   790
  where push_bit_eq_mult: "push_bit n a = a * 2 ^ n"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   791
 
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   792
definition take_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   793
  where take_bit_eq_mod: "take_bit n a = a mod 2 ^ n"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   794
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   795
definition drop_bit :: "nat \<Rightarrow> 'a \<Rightarrow> 'a"
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   796
  where drop_bit_eq_div: "drop_bit n a = a div 2 ^ n"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   797
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   798
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   799
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   800
context unique_euclidean_semiring_with_nat
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   801
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   802
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   803
lemma bit_ident:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   804
  "push_bit n (drop_bit n a) + take_bit n a = a"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   805
  using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   806
67960
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   807
lemma push_bit_push_bit [simp]:
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   808
  "push_bit m (push_bit n a) = push_bit (m + n) a"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   809
  by (simp add: push_bit_eq_mult power_add ac_simps)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   810
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   811
lemma take_bit_take_bit [simp]:
67960
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   812
  "take_bit m (take_bit n a) = take_bit (min m n) a"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   813
proof (cases "m \<le> n")
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   814
  case True
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   815
  then show ?thesis
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   816
    by (simp add: take_bit_eq_mod not_le min_def mod_mod_cancel le_imp_power_dvd)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   817
next
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   818
  case False
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   819
  then have "n < m" and "min m n = n"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   820
    by simp_all
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   821
  then have "2 ^ m = of_nat (2 ^ n) * of_nat (2 ^ (m - n))"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   822
    by (simp add: power_add [symmetric])
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   823
  then have "a mod 2 ^ n mod 2 ^ m = a mod 2 ^ n mod (of_nat (2 ^ n) * of_nat (2 ^ (m - n)))"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   824
    by simp
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   825
  also have "\<dots> = of_nat (2 ^ n) * (a mod 2 ^ n div of_nat (2 ^ n) mod of_nat (2 ^ (m - n))) + a mod 2 ^ n mod of_nat (2 ^ n)"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   826
    by (simp only: mod_mult2_eq')
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   827
  finally show ?thesis
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   828
    using \<open>min m n = n\<close> by (simp add: take_bit_eq_mod)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   829
qed
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   830
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   831
lemma drop_bit_drop_bit [simp]:
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   832
  "drop_bit m (drop_bit n a) = drop_bit (m + n) a"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   833
proof -
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   834
  have "a div (2 ^ m * 2 ^ n) = a div (of_nat (2 ^ n) * of_nat (2 ^ m))"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   835
    by (simp add: ac_simps)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   836
  also have "\<dots> = a div of_nat (2 ^ n) div of_nat (2 ^ m)"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   837
    by (simp only: div_mult2_eq')
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   838
  finally show ?thesis
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   839
    by (simp add: drop_bit_eq_div power_add)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   840
qed
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   841
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   842
lemma push_bit_take_bit:
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   843
  "push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   844
  by (simp add: push_bit_eq_mult take_bit_eq_mod power_add mult_mod_right ac_simps)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   845
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   846
lemma take_bit_push_bit:
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   847
  "take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   848
proof (cases "m \<le> n")
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   849
  case True
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   850
  then show ?thesis
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   851
    by (simp_all add: push_bit_eq_mult take_bit_eq_mod mod_eq_0_iff_dvd dvd_power_le)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   852
next
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   853
  case False
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   854
  then show ?thesis
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   855
    using push_bit_take_bit [of n "m - n" a]
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   856
    by simp
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   857
qed
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   858
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   859
lemma take_bit_drop_bit:
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   860
  "take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   861
  using mod_mult2_eq' [of a "2 ^ n" "2 ^ m"]
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   862
  by (simp add: drop_bit_eq_div take_bit_eq_mod power_add ac_simps)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   863
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   864
lemma drop_bit_take_bit:
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   865
  "drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   866
proof (cases "m \<le> n")
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   867
  case True
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   868
  then show ?thesis
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   869
    using take_bit_drop_bit [of "n - m" m a] by simp
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   870
next
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   871
  case False
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   872
  then have "a mod 2 ^ n div 2 ^ m = a mod 2 ^ n div 2 ^ (n + (m - n))"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   873
    by simp
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   874
  also have "\<dots> = a mod 2 ^ n div (2 ^ n * 2 ^ (m - n))"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   875
    by (simp add: power_add)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   876
  also have "\<dots> = a mod 2 ^ n div (of_nat (2 ^ n) * of_nat (2 ^ (m - n)))"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   877
    by simp
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   878
  also have "\<dots> = a mod 2 ^ n div of_nat (2 ^ n) div of_nat (2 ^ (m - n))"
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   879
    by (simp only: div_mult2_eq')
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   880
  finally show ?thesis
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   881
    using False by (simp add: take_bit_eq_mod drop_bit_eq_div)
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   882
qed
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
   883
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   884
lemma push_bit_0_id [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   885
  "push_bit 0 = id"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   886
  by (simp add: fun_eq_iff push_bit_eq_mult)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   887
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   888
lemma push_bit_of_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   889
  "push_bit n 0 = 0"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   890
  by (simp add: push_bit_eq_mult)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   891
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   892
lemma push_bit_of_1:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   893
  "push_bit n 1 = 2 ^ n"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   894
  by (simp add: push_bit_eq_mult)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   895
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   896
lemma push_bit_Suc [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   897
  "push_bit (Suc n) a = push_bit n (a * 2)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   898
  by (simp add: push_bit_eq_mult ac_simps)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   899
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   900
lemma push_bit_double:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   901
  "push_bit n (a * 2) = push_bit n a * 2"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   902
  by (simp add: push_bit_eq_mult ac_simps)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   903
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   904
lemma push_bit_eq_0_iff [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   905
  "push_bit n a = 0 \<longleftrightarrow> a = 0"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   906
  by (simp add: push_bit_eq_mult)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   907
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   908
lemma push_bit_add:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   909
  "push_bit n (a + b) = push_bit n a + push_bit n b"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   910
  by (simp add: push_bit_eq_mult algebra_simps)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   911
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   912
lemma push_bit_numeral [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   913
  "push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   914
  by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric]) (simp add: ac_simps)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   915
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   916
lemma push_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   917
  "push_bit n (of_nat m) = of_nat (push_bit n m)"
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   918
  by (simp add: push_bit_eq_mult Parity.push_bit_eq_mult)
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   919
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   920
lemma take_bit_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   921
  "take_bit 0 a = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   922
  by (simp add: take_bit_eq_mod)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   923
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   924
lemma take_bit_Suc [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   925
  "take_bit (Suc n) a = take_bit n (a div 2) * 2 + of_bool (odd a)"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   926
proof -
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   927
  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
   928
    if "odd a"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   929
    using that mod_mult2_eq' [of "1 + 2 * (a div 2)" 2 "2 ^ n"]
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   930
    by (simp add: ac_simps odd_iff_mod_2_eq_one mult_mod_right)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   931
  also have "\<dots> = a mod (2 * 2 ^ n)"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   932
    by (simp only: div_mult_mod_eq)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   933
  finally show ?thesis
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   934
    by (simp add: take_bit_eq_mod algebra_simps mult_mod_right)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   935
qed
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   936
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   937
lemma take_bit_of_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   938
  "take_bit n 0 = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   939
  by (simp add: take_bit_eq_mod)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   940
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   941
lemma take_bit_of_1 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   942
  "take_bit n 1 = of_bool (n > 0)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   943
  by (simp add: take_bit_eq_mod)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   944
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
   945
lemma take_bit_add:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   946
  "take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   947
  by (simp add: take_bit_eq_mod mod_simps)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   948
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
   949
lemma take_bit_eq_0_iff:
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
   950
  "take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a"
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
   951
  by (simp add: take_bit_eq_mod mod_eq_0_iff_dvd)
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
   952
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   953
lemma take_bit_of_1_eq_0_iff [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   954
  "take_bit n 1 = 0 \<longleftrightarrow> n = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   955
  by (simp add: take_bit_eq_mod)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   956
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   957
lemma even_take_bit_eq [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   958
  "even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   959
  by (cases n) (simp_all add: take_bit_eq_mod dvd_mod_iff)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   960
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   961
lemma take_bit_numeral_bit0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   962
  "take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   963
  by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric] take_bit_Suc
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   964
    ac_simps even_mult_iff nonzero_mult_div_cancel_right [OF numeral_neq_zero]) simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   965
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   966
lemma take_bit_numeral_bit1 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   967
  "take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   968
  by (simp only: numeral_eq_Suc power_Suc numeral_Bit1 [of k] mult_2 [symmetric] take_bit_Suc
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   969
    ac_simps even_add even_mult_iff div_mult_self1 [OF numeral_neq_zero]) (simp add: ac_simps)
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
   970
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   971
lemma take_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   972
  "take_bit n (of_nat m) = of_nat (take_bit n m)"
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   973
  by (simp add: take_bit_eq_mod Parity.take_bit_eq_mod of_nat_mod [of m "2 ^ n"])
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
   974
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   975
lemma drop_bit_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   976
  "drop_bit 0 = id"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   977
  by (simp add: fun_eq_iff drop_bit_eq_div)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   978
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   979
lemma drop_bit_of_0 [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   980
  "drop_bit n 0 = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   981
  by (simp add: drop_bit_eq_div)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   982
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   983
lemma drop_bit_of_1 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   984
  "drop_bit n 1 = of_bool (n = 0)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   985
  by (simp add: drop_bit_eq_div)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
   986
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   987
lemma drop_bit_Suc [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   988
  "drop_bit (Suc n) a = drop_bit n (a div 2)"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   989
proof (cases "even a")
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   990
  case True
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   991
  then obtain b where "a = 2 * b" ..
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   992
  moreover have "drop_bit (Suc n) (2 * b) = drop_bit n b"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   993
    by (simp add: drop_bit_eq_div)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   994
  ultimately show ?thesis
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   995
    by simp
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   996
next
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   997
  case False
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   998
  then obtain b where "a = 2 * b + 1" ..
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
   999
  moreover have "drop_bit (Suc n) (2 * b + 1) = drop_bit n b"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1000
    using div_mult2_eq' [of "1 + b * 2" 2 "2 ^ n"]
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1001
    by (auto simp add: drop_bit_eq_div ac_simps)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1002
  ultimately show ?thesis
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1003
    by simp
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1004
qed
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1005
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1006
lemma drop_bit_half:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1007
  "drop_bit n (a div 2) = drop_bit n a div 2"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1008
  by (induction n arbitrary: a) simp_all
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1009
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1010
lemma drop_bit_of_bool [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1011
  "drop_bit n (of_bool d) = of_bool (n = 0 \<and> d)"
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1012
  by (cases n) simp_all
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1013
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1014
lemma drop_bit_numeral_bit0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1015
  "drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1016
  by (simp only: numeral_eq_Suc power_Suc numeral_Bit0 [of k] mult_2 [symmetric] drop_bit_Suc
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1017
    nonzero_mult_div_cancel_left [OF numeral_neq_zero])
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1018
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1019
lemma drop_bit_numeral_bit1 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1020
  "drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1021
  by (simp only: numeral_eq_Suc power_Suc numeral_Bit1 [of k] mult_2 [symmetric] drop_bit_Suc
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1022
    div_mult_self4 [OF numeral_neq_zero]) simp
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1023
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1024
lemma drop_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1025
  "drop_bit n (of_nat m) = of_nat (drop_bit n m)"
68389
1c84a8c513af proper white space;
wenzelm
parents: 68157
diff changeset
  1026
  by (simp add: drop_bit_eq_div Parity.drop_bit_eq_div of_nat_div [of m "2 ^ n"])
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1027
58770
ae5e9b4f8daf downshift of theory Parity in the hierarchy
haftmann
parents: 58769
diff changeset
  1028
end
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1029
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1030
lemma push_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1031
  "push_bit n (Suc 0) = 2 ^ n"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1032
  using push_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1033
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1034
lemma take_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1035
  "take_bit n (Suc 0) = of_bool (0 < n)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1036
  using take_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1037
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1038
lemma drop_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1039
  "drop_bit n (Suc 0) = of_bool (n = 0)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1040
  using drop_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1041
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1042
end