src/HOL/Parity.thy
author haftmann
Sat, 25 Apr 2020 13:26:24 +0000
changeset 71802 ab3cecb836b5
parent 71799 e00712b4e2c2
child 71804 6fd70ed18199
permissions -rw-r--r--
more rules
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
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    33
lemma odd_of_bool_self [simp]:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    34
  \<open>odd (of_bool p) \<longleftrightarrow> p\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    35
  by (cases p) simp_all
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
    36
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
    37
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
    38
  "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
    39
  by (cases a rule: parity_cases) simp_all
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    40
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    41
lemma not_mod_2_eq_1_eq_0 [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    42
  "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    43
  by (cases a rule: parity_cases) simp_all
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    44
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    45
lemma evenE [elim?]:
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    46
  assumes "even a"
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    47
  obtains b where "a = 2 * b"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
    48
  using assms by (rule dvdE)
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
    49
58681
a478a0742a8e legacy cleanup
haftmann
parents: 58680
diff changeset
    50
lemma oddE [elim?]:
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
    51
  assumes "odd a"
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
    52
  obtains b where "a = 2 * b + 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
    53
proof -
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    54
  have "a = 2 * (a div 2) + a mod 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    55
    by (simp add: mult_div_mod_eq)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    56
  with assms have "a = 2 * (a div 2) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    57
    by (simp add: odd_iff_mod_2_eq_one)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    58
  then show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    59
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    60
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    61
lemma mod_2_eq_odd:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    62
  "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
    63
  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
    64
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    65
lemma of_bool_odd_eq_mod_2:
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    66
  "of_bool (odd a) = a mod 2"
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    67
  by (simp add: mod_2_eq_odd)
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
    68
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    69
lemma even_mod_2_iff [simp]:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    70
  \<open>even (a mod 2) \<longleftrightarrow> even a\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    71
  by (simp add: mod_2_eq_odd)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    72
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    73
lemma mod2_eq_if:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    74
  "a mod 2 = (if even a then 0 else 1)"
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    75
  by (simp add: mod_2_eq_odd)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
    76
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    77
lemma even_zero [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    78
  "even 0"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    79
  by (fact dvd_0_right)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    80
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    81
lemma odd_even_add:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    82
  "even (a + b)" if "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    83
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    84
  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
    85
    by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    86
  then have "a + b = 2 * c + 2 * d + (1 + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    87
    by (simp only: ac_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    88
  also have "\<dots> = 2 * (c + d + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    89
    by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    90
  finally show ?thesis ..
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    91
qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    92
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    93
lemma even_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    94
  "even (a + b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    95
  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
    96
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    97
lemma odd_add [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    98
  "odd (a + b) \<longleftrightarrow> \<not> (odd a \<longleftrightarrow> odd b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
    99
  by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   100
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   101
lemma even_plus_one_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   102
  "even (a + 1) \<longleftrightarrow> odd a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   103
  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
   104
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   105
lemma even_mult_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   106
  "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
   107
proof
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   108
  assume ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   109
  then show ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   110
    by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   111
next
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   112
  assume ?P
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   113
  show ?Q
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   114
  proof (rule ccontr)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   115
    assume "\<not> (even a \<or> even b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   116
    then have "odd a" and "odd b"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   117
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   118
    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
   119
      by (blast elim: oddE)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   120
    then have "a * b = (2 * r + 1) * (2 * s + 1)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   121
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   122
    also have "\<dots> = 2 * (2 * r * s + r + s) + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   123
      by (simp add: algebra_simps)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   124
    finally have "odd (a * b)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   125
      by simp
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   126
    with \<open>?P\<close> show False
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   127
      by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   128
  qed
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   129
qed
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   130
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   131
lemma even_numeral [simp]: "even (numeral (Num.Bit0 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   132
proof -
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   133
  have "even (2 * numeral n)"
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   134
    unfolding even_mult_iff by simp
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   135
  then have "even (numeral n + numeral n)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   136
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   137
  then show ?thesis
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   138
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   139
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   140
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   141
lemma odd_numeral [simp]: "odd (numeral (Num.Bit1 n))"
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   142
proof
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   143
  assume "even (numeral (num.Bit1 n))"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   144
  then have "even (numeral n + numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   145
    unfolding numeral.simps .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   146
  then have "even (2 * numeral n + 1)"
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   147
    unfolding mult_2 .
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   148
  then have "2 dvd numeral n * 2 + 1"
58740
cb9d84d3e7f2 turn even into an abbreviation
haftmann
parents: 58718
diff changeset
   149
    by (simp add: ac_simps)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   150
  then have "2 dvd 1"
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   151
    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
   152
  then show False by simp
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   153
qed
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   154
71755
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   155
lemma odd_numeral_BitM [simp]:
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   156
  \<open>odd (numeral (Num.BitM w))\<close>
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   157
  by (cases w) simp_all
318695613bb7 more complete rules on numerals
haftmann
parents: 71535
diff changeset
   158
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   159
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
   160
  by (induct n) auto
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   161
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   162
lemma mask_eq_sum_exp:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   163
  \<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   164
proof -
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   165
  have *: \<open>{q. q < Suc m} = insert m {q. q < m}\<close> for m
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   166
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   167
  have \<open>2 ^ n = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   168
    by (induction n) (simp_all add: ac_simps mult_2 *)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   169
  then have \<open>2 ^ n - 1 = (\<Sum>m\<in>{q. q < n}. 2 ^ m) + 1 - 1\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   170
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   171
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   172
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   173
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   174
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   175
lemma mask_eq_seq_sum:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   176
  \<open>2 ^ n - 1 = ((\<lambda>k. 1 + k * 2) ^^ n) 0\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   177
proof -
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   178
  have \<open>2 ^ n = ((\<lambda>k. 1 + k * 2) ^^ n) 0 + 1\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   179
    by (induction n) (simp_all add: ac_simps mult_2)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   180
  then show ?thesis
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   181
    by simp
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   182
qed
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   183
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   184
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   185
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   186
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
   187
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   188
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   189
subclass comm_ring_1 ..
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   190
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   191
lemma even_minus:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   192
  "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
   193
  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
   194
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   195
lemma even_diff [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   196
  "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
   197
  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
   198
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   199
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   200
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
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
   203
71157
8bdf3c36011c tuned theory structure
haftmann
parents: 71138
diff changeset
   204
context unique_euclidean_semiring_with_nat
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   205
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   206
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   207
subclass semiring_parity
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   208
proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   209
  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
   210
    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
   211
  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
   212
  proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   213
    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
   214
    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
   215
      by auto
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   216
  next
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   217
    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
   218
    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
   219
    proof (rule order_antisym)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   220
      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
   221
        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
   222
      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
   223
        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
   224
    qed 
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   225
    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
   226
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   227
    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
   228
      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
   229
    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
   230
      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
   231
    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
   232
      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
   233
    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
   234
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   235
    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
   236
      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
   237
    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
   238
    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
   239
      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
   240
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   241
  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
   242
  proof (rule notI)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   243
    assume "is_unit 2"
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   244
    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
   245
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   246
    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
   247
      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
   248
    then show False
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   249
      by 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
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   252
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   253
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
   254
  "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
   255
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   256
  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
   257
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   258
  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
   259
    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
   260
  finally show ?thesis .
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   261
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   262
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   263
lemma even_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   264
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   265
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   266
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   267
lemma odd_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   268
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   269
  by (auto elim!: oddE simp add: add.assoc)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   270
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   271
lemma even_two_times_div_two:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   272
  "even a \<Longrightarrow> 2 * (a div 2) = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   273
  by (fact dvd_mult_div_cancel)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   274
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   275
lemma odd_two_times_div_two_succ [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   276
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   277
  using mult_div_mod_eq [of 2 a]
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   278
  by (simp add: even_iff_mod_2_eq_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   279
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   280
lemma coprime_left_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   281
  "coprime 2 a \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   282
proof
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   283
  assume "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   284
  show "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   285
  proof (rule coprimeI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   286
    fix b
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   287
    assume "b dvd 2" "b dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   288
    then have "b dvd a mod 2"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   289
      by (auto intro: dvd_mod)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   290
    with \<open>odd a\<close> show "is_unit b"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   291
      by (simp add: mod_2_eq_odd)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   292
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   293
next
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   294
  assume "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   295
  show "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   296
  proof (rule notI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   297
    assume "even a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   298
    then obtain b where "a = 2 * b" ..
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   299
    with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   300
      by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   301
    moreover have "\<not> coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   302
      by (rule not_coprimeI [of 2]) simp_all
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   303
    ultimately show False
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   304
      by blast
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   305
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   306
qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   307
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   308
lemma coprime_right_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   309
  "coprime a 2 \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   310
  using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   311
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   312
end
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   313
71157
8bdf3c36011c tuned theory structure
haftmann
parents: 71138
diff changeset
   314
context unique_euclidean_ring_with_nat
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   315
begin
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   316
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   317
subclass ring_parity ..
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   318
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   319
lemma minus_1_mod_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   320
  "- 1 mod 2 = 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   321
  by (simp add: mod_2_eq_odd)
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   322
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   323
lemma minus_1_div_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   324
  "- 1 div 2 = - 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   325
proof -
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   326
  from div_mult_mod_eq [of "- 1" 2]
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   327
  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
   328
    using add_implies_diff by fastforce
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   329
  then show ?thesis
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   330
    using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   331
qed
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   332
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   333
end
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   334
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   335
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   336
subsection \<open>Instance for \<^typ>\<open>nat\<close>\<close>
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   337
70340
7383930fc946 slightly more specialized name for type class
haftmann
parents: 70339
diff changeset
   338
instance nat :: unique_euclidean_semiring_with_nat
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   339
  by standard (simp_all add: dvd_eq_mod_eq_0)
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   340
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   341
lemma even_Suc_Suc_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   342
  "even (Suc (Suc n)) \<longleftrightarrow> even n"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   343
  using dvd_add_triv_right_iff [of 2 n] by simp
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   344
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   345
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   346
  using even_plus_one_iff [of n] by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   347
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   348
lemma even_diff_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   349
  "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   350
proof (cases "n \<le> m")
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   351
  case True
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   352
  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
   353
  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
   354
  ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   355
  then show ?thesis by auto
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   356
next
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   357
  case False
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   358
  then show ?thesis by simp
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   359
qed
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   360
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   361
lemma odd_pos:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   362
  "odd n \<Longrightarrow> 0 < n" for n :: nat
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   363
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   364
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   365
lemma Suc_double_not_eq_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   366
  "Suc (2 * m) \<noteq> 2 * n"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   367
proof
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   368
  assume "Suc (2 * m) = 2 * n"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   369
  moreover have "odd (Suc (2 * m))" and "even (2 * n)"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   370
    by simp_all
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   371
  ultimately show False by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   372
qed
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   373
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   374
lemma double_not_eq_Suc_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   375
  "2 * m \<noteq> Suc (2 * n)"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   376
  using Suc_double_not_eq_double [of n m] by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   377
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   378
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
   379
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   380
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   381
lemma even_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   382
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   383
  using even_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   384
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   385
lemma odd_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   386
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   387
  using odd_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   388
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   389
lemma odd_two_times_div_two_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   390
  assumes "odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   391
  shows "2 * (n div 2) = n - (1 :: nat)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   392
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   393
  from assms have "2 * (n div 2) + 1 = n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   394
    by (rule odd_two_times_div_two_succ)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   395
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   396
    by simp
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   397
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   398
    by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   399
qed
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   400
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   401
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
   402
  "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
   403
  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
   404
69502
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   405
lemma odd_card_imp_not_empty:
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   406
  \<open>A \<noteq> {}\<close> if \<open>odd (card A)\<close>
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   407
  using that by auto
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   408
70365
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   409
lemma nat_induct2 [case_names 0 1 step]:
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   410
  assumes "P 0" "P 1" and step: "\<And>n::nat. P n \<Longrightarrow> P (n + 2)"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   411
  shows "P n"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   412
proof (induct n rule: less_induct)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   413
  case (less n)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   414
  show ?case
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   415
  proof (cases "n < Suc (Suc 0)")
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   416
    case True
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   417
    then show ?thesis
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   418
      using assms by (auto simp: less_Suc_eq)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   419
  next
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   420
    case False
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   421
    then obtain k where k: "n = Suc (Suc k)"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   422
      by (force simp: not_less nat_le_iff_add)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   423
    then have "k<n"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   424
      by simp
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   425
    with less assms have "P (k+2)"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   426
      by blast
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   427
    then show ?thesis
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   428
      by (simp add: k)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   429
  qed
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   430
qed
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   431
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   432
lemma mask_eq_sum_exp_nat:
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   433
  \<open>2 ^ n - Suc 0 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   434
  using mask_eq_sum_exp [where ?'a = nat] by simp
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   435
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   436
context semiring_parity
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   437
begin
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   438
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   439
lemma even_sum_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   440
  \<open>even (sum f A) \<longleftrightarrow> even (card {a\<in>A. odd (f a)})\<close> if \<open>finite A\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   441
using that proof (induction A)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   442
  case empty
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   443
  then show ?case
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   444
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   445
next
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   446
  case (insert a A)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   447
  moreover have \<open>{b \<in> insert a A. odd (f b)} = (if odd (f a) then {a} else {}) \<union> {b \<in> A. odd (f b)}\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   448
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   449
  ultimately show ?case
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   450
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   451
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   452
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   453
lemma even_prod_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   454
  \<open>even (prod f A) \<longleftrightarrow> (\<exists>a\<in>A. even (f a))\<close> if \<open>finite A\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   455
  using that by (induction A) simp_all
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   456
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   457
lemma even_mask_iff [simp]:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   458
  \<open>even (2 ^ n - 1) \<longleftrightarrow> n = 0\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   459
proof (cases \<open>n = 0\<close>)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   460
  case True
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   461
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   462
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   463
next
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   464
  case False
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   465
  then have \<open>{a. a = 0 \<and> a < n} = {0}\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   466
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   467
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   468
    by (auto simp add: mask_eq_sum_exp even_sum_iff)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   469
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   470
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   471
end
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   472
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   473
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   474
subsection \<open>Parity and powers\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   475
61531
ab2e862263e7 Rounding function, uniform limits, cotangent, binomial identities
eberlm
parents: 60867
diff changeset
   476
context ring_1
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   477
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   478
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   479
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   480
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   481
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   482
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   483
  by (auto elim: oddE)
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   484
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   485
lemma uminus_power_if:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   486
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   487
  by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   488
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   489
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   490
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   491
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   492
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   493
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   494
66582
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   495
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
   496
  by (cases "even (n + k)") auto
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   497
67371
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   498
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
   499
  by (induct n) auto
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   500
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   501
end
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   502
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   503
context linordered_idom
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   504
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   505
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   506
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   507
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   508
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   509
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
   510
  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
   511
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   512
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
   513
  by (auto simp add: zero_le_even_power zero_le_odd_power)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   514
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   515
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
   516
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   517
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   518
    unfolding power_eq_0_iff [of a n, symmetric] by blast
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   519
  show ?thesis
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   520
    unfolding less_le zero_le_power_eq by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   521
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   522
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   523
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
   524
  unfolding not_le [symmetric] zero_le_power_eq by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   525
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   526
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
   527
  unfolding not_less [symmetric] zero_less_power_eq by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   528
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   529
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
   530
  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
   531
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   532
lemma power_mono_even:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   533
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   534
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   535
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   536
  have "0 \<le> \<bar>a\<bar>" by auto
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   537
  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
   538
    by (rule power_mono)
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   539
  with \<open>even n\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   540
    by (simp add: power_even_abs)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   541
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   542
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   543
lemma power_mono_odd:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   544
  assumes "odd n" and "a \<le> b"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   545
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   546
proof (cases "b < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   547
  case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   548
  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
   549
  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   550
  with \<open>odd n\<close> show ?thesis by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   551
next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   552
  case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   553
  then have "0 \<le> b" by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   554
  show ?thesis
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   555
  proof (cases "a < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   556
    case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   557
    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
   558
    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
   559
    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
   560
    ultimately show ?thesis by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   561
  next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   562
    case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   563
    then have "0 \<le> a" by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   564
    with \<open>a \<le> b\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   565
      using power_mono by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   566
  qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   567
qed
62083
7582b39f51ed add the proof of the central limit theorem
hoelzl
parents: 61799
diff changeset
   568
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   569
text \<open>Simplify, when the exponent is a numeral\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   570
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   571
lemma zero_le_power_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   572
  "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
   573
  by (fact zero_le_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   574
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   575
lemma zero_less_power_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   576
  "0 < a ^ numeral w \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   577
    numeral w = (0 :: nat) \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   578
    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   579
    odd (numeral w :: nat) \<and> 0 < a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   580
  by (fact zero_less_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   581
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   582
lemma power_le_zero_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   583
  "a ^ numeral w \<le> 0 \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   584
    (0 :: nat) < numeral w \<and>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   585
    (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
   586
  by (fact power_le_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   587
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   588
lemma power_less_zero_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   589
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   590
  by (fact power_less_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   591
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   592
lemma power_even_abs_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   593
  "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
   594
  by (fact power_even_abs)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   595
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   596
end
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   597
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   598
context unique_euclidean_semiring_with_nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   599
begin
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   600
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   601
lemma even_mask_div_iff':
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   602
  \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   603
proof -
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   604
  have \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> even (of_nat ((2 ^ m - Suc 0) div 2 ^ n))\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   605
    by (simp only: of_nat_div) (simp add: of_nat_diff)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   606
  also have \<open>\<dots> \<longleftrightarrow> even ((2 ^ m - Suc 0) div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   607
    by simp
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   608
  also have \<open>\<dots> \<longleftrightarrow> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   609
  proof (cases \<open>m \<le> n\<close>)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   610
    case True
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   611
    then show ?thesis
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   612
      by (simp add: Suc_le_lessD)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   613
  next
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   614
    case False
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   615
    then obtain r where r: \<open>m = n + Suc r\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   616
      using less_imp_Suc_add by fastforce
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   617
    from r have \<open>{q. q < m} \<inter> {q. 2 ^ n dvd (2::nat) ^ q} = {q. n \<le> q \<and> q < m}\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   618
      by (auto simp add: dvd_power_iff_le)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   619
    moreover from r have \<open>{q. q < m} \<inter> {q. \<not> 2 ^ n dvd (2::nat) ^ q} = {q. q < n}\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   620
      by (auto simp add: dvd_power_iff_le)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   621
    moreover from False have \<open>{q. n \<le> q \<and> q < m \<and> q \<le> n} = {n}\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   622
      by auto
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   623
    then have \<open>odd ((\<Sum>a\<in>{q. n \<le> q \<and> q < m}. 2 ^ a div (2::nat) ^ n) + sum ((^) 2) {q. q < n} div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   624
      by (simp_all add: euclidean_semiring_cancel_class.power_diff_power_eq semiring_parity_class.even_sum_iff not_less mask_eq_sum_exp_nat [symmetric])
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   625
    ultimately have \<open>odd (sum ((^) (2::nat)) {q. q < m} div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   626
      by (subst euclidean_semiring_cancel_class.sum_div_partition) simp_all
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   627
    with False show ?thesis
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   628
      by (simp add: mask_eq_sum_exp_nat)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   629
  qed
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   630
  finally show ?thesis .
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   631
qed
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   632
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   633
end
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   634
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   635
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   636
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
   637
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   638
lemma even_diff_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   639
  "even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   640
  by (fact even_diff)
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   641
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   642
lemma even_abs_add_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   643
  "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   644
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   645
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   646
lemma even_add_abs_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   647
  "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   648
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   649
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   650
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
   651
  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
   652
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   653
lemma zdiv_zmult2_eq:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   654
  \<open>a div (b * c) = (a div b) div c\<close> if \<open>c \<ge> 0\<close> for a b c :: int
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   655
proof (cases \<open>b \<ge> 0\<close>)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   656
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   657
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   658
    using div_mult2_eq' [of a \<open>nat b\<close> \<open>nat c\<close>] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   659
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   660
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   661
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   662
    using div_mult2_eq' [of \<open>- a\<close> \<open>nat (- b)\<close> \<open>nat c\<close>] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   663
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   664
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   665
lemma zmod_zmult2_eq:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   666
  \<open>a mod (b * c) = b * (a div b mod c) + a mod b\<close> if \<open>c \<ge> 0\<close> for a b c :: int
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   667
proof (cases \<open>b \<ge> 0\<close>)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   668
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   669
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   670
    using mod_mult2_eq' [of a \<open>nat b\<close> \<open>nat c\<close>] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   671
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   672
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   673
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   674
    using mod_mult2_eq' [of \<open>- a\<close> \<open>nat (- b)\<close> \<open>nat c\<close>] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   675
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   676
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   677
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   678
subsection \<open>Abstract bit structures\<close>
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   679
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   680
class semiring_bits = semiring_parity +
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   681
  assumes bits_induct [case_names stable rec]:
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   682
    \<open>(\<And>a. a div 2 = a \<Longrightarrow> P a)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   683
     \<Longrightarrow> (\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a))
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   684
        \<Longrightarrow> P a\<close>
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   685
  assumes bits_div_0 [simp]: \<open>0 div a = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   686
    and bits_div_by_1 [simp]: \<open>a div 1 = a\<close>
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   687
    and bits_mod_div_trivial [simp]: \<open>a mod b div b = 0\<close>
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   688
    and even_succ_div_2 [simp]: \<open>even a \<Longrightarrow> (1 + a) div 2 = a div 2\<close>
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   689
    and even_mask_div_iff: \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> 2 ^ n = 0 \<or> m \<le> n\<close>
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   690
    and exp_div_exp_eq: \<open>2 ^ m div 2 ^ n = of_bool (2 ^ m \<noteq> 0 \<and> m \<ge> n) * 2 ^ (m - n)\<close>
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   691
    and div_exp_eq: \<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   692
    and mod_exp_eq: \<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   693
    and mult_exp_mod_exp_eq: \<open>m \<le> n \<Longrightarrow> (a * 2 ^ m) mod (2 ^ n) = (a mod 2 ^ (n - m)) * 2 ^ m\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   694
    and div_exp_mod_exp_eq: \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
   695
    and even_mult_exp_div_exp_iff: \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> m > n \<or> 2 ^ n = 0 \<or> (m \<le> n \<and> even (a div 2 ^ (n - m)))\<close>
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   696
begin
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   697
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   698
lemma bits_div_by_0 [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   699
  \<open>a div 0 = 0\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   700
  by (metis add_cancel_right_right bits_mod_div_trivial mod_mult_div_eq mult_not_zero)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   701
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   702
lemma bits_1_div_2 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   703
  \<open>1 div 2 = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   704
  using even_succ_div_2 [of 0] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   705
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   706
lemma bits_1_div_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   707
  \<open>1 div 2 ^ n = of_bool (n = 0)\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   708
  using div_exp_eq [of 1 1] by (cases n) simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   709
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   710
lemma even_succ_div_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   711
  \<open>(1 + a) div 2 ^ n = a div 2 ^ n\<close> if \<open>even a\<close> and \<open>n > 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   712
proof (cases n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   713
  case 0
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   714
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   715
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   716
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   717
  case (Suc n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   718
  with \<open>even a\<close> have \<open>(1 + a) div 2 ^ Suc n = a div 2 ^ Suc n\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   719
  proof (induction n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   720
    case 0
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   721
    then show ?case
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   722
      by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   723
  next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   724
    case (Suc n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   725
    then show ?case
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   726
      using div_exp_eq [of _ 1 \<open>Suc n\<close>, symmetric]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   727
      by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   728
  qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   729
  with Suc show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   730
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   731
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   732
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   733
lemma even_succ_mod_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   734
  \<open>(1 + a) mod 2 ^ n = 1 + (a mod 2 ^ n)\<close> if \<open>even a\<close> and \<open>n > 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   735
  using div_mult_mod_eq [of \<open>1 + a\<close> \<open>2 ^ n\<close>] that
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   736
  apply simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   737
  by (metis local.add.left_commute local.add_left_cancel local.div_mult_mod_eq)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   738
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   739
lemma bits_mod_by_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   740
  \<open>a mod 1 = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   741
  using div_mult_mod_eq [of a 1] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   742
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   743
lemma bits_mod_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   744
  \<open>0 mod a = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   745
  using div_mult_mod_eq [of 0 a] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   746
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   747
lemma bits_one_mod_two_eq_one [simp]:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   748
  \<open>1 mod 2 = 1\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   749
  by (simp add: mod2_eq_if)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   750
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   751
definition bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   752
  where \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   753
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   754
lemma bit_0 [simp]:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   755
  \<open>bit a 0 \<longleftrightarrow> odd a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   756
  by (simp add: bit_def)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   757
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   758
lemma bit_Suc:
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   759
  \<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   760
  using div_exp_eq [of a 1 n] by (simp add: bit_def)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   761
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   762
lemma bit_rec:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   763
  \<open>bit a n \<longleftrightarrow> (if n = 0 then odd a else bit (a div 2) (n - 1))\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   764
  by (cases n) (simp_all add: bit_Suc)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   765
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   766
lemma bit_0_eq [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   767
  \<open>bit 0 = bot\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   768
  by (simp add: fun_eq_iff bit_def)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   769
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   770
context
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   771
  fixes a
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   772
  assumes stable: \<open>a div 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   773
begin
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   774
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   775
lemma bits_stable_imp_add_self:
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   776
  \<open>a + a mod 2 = 0\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   777
proof -
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   778
  have \<open>a div 2 * 2 + a mod 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   779
    by (fact div_mult_mod_eq)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   780
  then have \<open>a * 2 + a mod 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   781
    by (simp add: stable)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   782
  then show ?thesis
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   783
    by (simp add: mult_2_right ac_simps)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   784
qed
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   785
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   786
lemma stable_imp_bit_iff_odd:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   787
  \<open>bit a n \<longleftrightarrow> odd a\<close>
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   788
  by (induction n) (simp_all add: stable bit_Suc)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   789
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   790
end
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   791
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   792
lemma bit_iff_idd_imp_stable:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   793
  \<open>a div 2 = a\<close> if \<open>\<And>n. bit a n \<longleftrightarrow> odd a\<close>
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   794
using that proof (induction a rule: bits_induct)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   795
  case (stable a)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   796
  then show ?case
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   797
    by simp
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   798
next
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   799
  case (rec a b)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   800
  from rec.prems [of 1] have [simp]: \<open>b = odd a\<close>
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   801
    by (simp add: rec.hyps bit_Suc)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   802
  from rec.hyps have hyp: \<open>(of_bool (odd a) + 2 * a) div 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   803
    by simp
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   804
  have \<open>bit a n \<longleftrightarrow> odd a\<close> for n
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   805
    using rec.prems [of \<open>Suc n\<close>] by (simp add: hyp bit_Suc)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   806
  then have \<open>a div 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   807
    by (rule rec.IH)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   808
  then have \<open>of_bool (odd a) + 2 * a = 2 * (a div 2) + of_bool (odd a)\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   809
    by (simp add: ac_simps)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   810
  also have \<open>\<dots> = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   811
    using mult_div_mod_eq [of 2 a]
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   812
    by (simp add: of_bool_odd_eq_mod_2)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   813
  finally show ?case
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   814
    using \<open>a div 2 = a\<close> by (simp add: hyp)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   815
qed
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   816
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   817
lemma exp_eq_0_imp_not_bit:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   818
  \<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   819
  using that by (simp add: bit_def)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   820
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   821
lemma bit_eqI:
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   822
  \<open>a = b\<close> if \<open>\<And>n. 2 ^ n \<noteq> 0 \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   823
proof -
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   824
  have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   825
  proof (cases \<open>2 ^ n = 0\<close>)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   826
    case True
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   827
    then show ?thesis
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   828
      by (simp add: exp_eq_0_imp_not_bit)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   829
  next
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   830
    case False
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   831
    then show ?thesis
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   832
      by (rule that)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   833
  qed
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   834
  then show ?thesis proof (induction a arbitrary: b rule: bits_induct)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   835
    case (stable a)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   836
    from stable(2) [of 0] have **: \<open>even b \<longleftrightarrow> even a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   837
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   838
    have \<open>b div 2 = b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   839
    proof (rule bit_iff_idd_imp_stable)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   840
      fix n
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   841
      from stable have *: \<open>bit b n \<longleftrightarrow> bit a n\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   842
        by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   843
      also have \<open>bit a n \<longleftrightarrow> odd a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   844
        using stable by (simp add: stable_imp_bit_iff_odd)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   845
      finally show \<open>bit b n \<longleftrightarrow> odd b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   846
        by (simp add: **)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   847
    qed
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   848
    from ** have \<open>a mod 2 = b mod 2\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   849
      by (simp add: mod2_eq_if)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   850
    then have \<open>a mod 2 + (a + b) = b mod 2 + (a + b)\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   851
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   852
    then have \<open>a + a mod 2 + b = b + b mod 2 + a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   853
      by (simp add: ac_simps)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   854
    with \<open>a div 2 = a\<close> \<open>b div 2 = b\<close> show ?case
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   855
      by (simp add: bits_stable_imp_add_self)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   856
  next
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   857
    case (rec a p)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   858
    from rec.prems [of 0] have [simp]: \<open>p = odd b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   859
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   860
    from rec.hyps have \<open>bit a n \<longleftrightarrow> bit (b div 2) n\<close> for n
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   861
      using rec.prems [of \<open>Suc n\<close>] by (simp add: bit_Suc)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   862
    then have \<open>a = b div 2\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   863
      by (rule rec.IH)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   864
    then have \<open>2 * a = 2 * (b div 2)\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   865
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   866
    then have \<open>b mod 2 + 2 * a = b mod 2 + 2 * (b div 2)\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   867
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   868
    also have \<open>\<dots> = b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   869
      by (fact mod_mult_div_eq)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   870
    finally show ?case
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   871
      by (auto simp add: mod2_eq_if)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   872
  qed
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   873
qed
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   874
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   875
lemma bit_eq_iff:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   876
  \<open>a = b \<longleftrightarrow> (\<forall>n. bit a n \<longleftrightarrow> bit b n)\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   877
  by (auto intro: bit_eqI)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   878
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   879
lemma bit_exp_iff:
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   880
  \<open>bit (2 ^ m) n \<longleftrightarrow> 2 ^ m \<noteq> 0 \<and> m = n\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   881
  by (auto simp add: bit_def exp_div_exp_eq)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   882
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   883
lemma bit_1_iff:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   884
  \<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   885
  using bit_exp_iff [of 0 n] by simp
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   886
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   887
lemma bit_2_iff:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   888
  \<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   889
  using bit_exp_iff [of 1 n] by auto
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   890
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   891
lemma even_bit_succ_iff:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   892
  \<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   893
  using that by (cases \<open>n = 0\<close>) (simp_all add: bit_def)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   894
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   895
lemma odd_bit_iff_bit_pred:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   896
  \<open>bit a n \<longleftrightarrow> bit (a - 1) n \<or> n = 0\<close> if \<open>odd a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   897
proof -
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   898
  from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> ..
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   899
  moreover have \<open>bit (2 * b) n \<or> n = 0 \<longleftrightarrow> bit (1 + 2 * b) n\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   900
    using even_bit_succ_iff by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   901
  ultimately show ?thesis by (simp add: ac_simps)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   902
qed
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   903
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   904
lemma bit_double_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   905
  \<open>bit (2 * a) n \<longleftrightarrow> bit a (n - 1) \<and> n \<noteq> 0 \<and> 2 ^ n \<noteq> 0\<close>
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   906
  using even_mult_exp_div_exp_iff [of a 1 n]
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   907
  by (cases n, auto simp add: bit_def ac_simps)
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   908
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   909
lemma bit_eq_rec:
71441
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   910
  \<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> (is \<open>?P = ?Q\<close>)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   911
proof
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   912
  assume ?P
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   913
  then show ?Q
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   914
    by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   915
next
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   916
  assume ?Q
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   917
  then have \<open>even a \<longleftrightarrow> even b\<close> and \<open>a div 2 = b div 2\<close>
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   918
    by simp_all
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   919
  show ?P
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   920
  proof (rule bit_eqI)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   921
    fix n
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   922
    show \<open>bit a n \<longleftrightarrow> bit b n\<close>
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   923
    proof (cases n)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   924
      case 0
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   925
      with \<open>even a \<longleftrightarrow> even b\<close> show ?thesis
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   926
        by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   927
    next
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   928
      case (Suc n)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   929
      moreover from \<open>a div 2 = b div 2\<close> have \<open>bit (a div 2) n = bit (b div 2) n\<close>
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   930
        by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   931
      ultimately show ?thesis
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   932
        by (simp add: bit_Suc)
71441
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   933
    qed
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   934
  qed
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   935
qed
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   936
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   937
lemma bit_mask_iff:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   938
  \<open>bit (2 ^ m - 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   939
  by (simp add: bit_def even_mask_div_iff not_le)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   940
71757
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   941
lemma bit_Numeral1_iff [simp]:
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   942
  \<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close>
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   943
  by (simp add: bit_rec)
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   944
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   945
end
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   946
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   947
lemma nat_bit_induct [case_names zero even odd]:
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   948
  "P n" if zero: "P 0"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   949
    and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   950
    and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   951
proof (induction n rule: less_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   952
  case (less n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   953
  show "P n"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   954
  proof (cases "n = 0")
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   955
    case True with zero show ?thesis by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   956
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   957
    case False
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   958
    with less have hyp: "P (n div 2)" by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   959
    show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   960
    proof (cases "even n")
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   961
      case True
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   962
      then have "n \<noteq> 1"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   963
        by auto
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   964
      with \<open>n \<noteq> 0\<close> have "n div 2 > 0"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   965
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   966
      with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   967
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   968
    next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   969
      case False
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   970
      with hyp odd [of "n div 2"] show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   971
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   972
    qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   973
  qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   974
qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   975
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   976
instance nat :: semiring_bits
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   977
proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   978
  show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   979
    and rec: \<open>\<And>n b. P n \<Longrightarrow> (of_bool b + 2 * n) div 2 = n \<Longrightarrow> P (of_bool b + 2 * n)\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   980
    for P and n :: nat
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   981
  proof (induction n rule: nat_bit_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   982
    case zero
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   983
    from stable [of 0] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   984
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   985
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   986
    case (even n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   987
    with rec [of n False] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   988
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   989
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   990
    case (odd n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   991
    with rec [of n True] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   992
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   993
  qed
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   994
  show \<open>q mod 2 ^ m mod 2 ^ n = q mod 2 ^ min m n\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   995
    for q m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   996
    apply (auto simp add: less_iff_Suc_add power_add mod_mod_cancel split: split_min_lin)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   997
    apply (metis div_mult2_eq mod_div_trivial mod_eq_self_iff_div_eq_0 mod_mult_self2_is_0 power_commutes)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   998
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   999
  show \<open>(q * 2 ^ m) mod (2 ^ n) = (q mod 2 ^ (n - m)) * 2 ^ m\<close> if \<open>m \<le> n\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1000
    for q m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1001
    using that
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1002
    apply (auto simp add: mod_mod_cancel div_mult2_eq power_add mod_mult2_eq le_iff_add split: split_min_lin)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1003
    apply (simp add: mult.commute)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1004
    done
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1005
  show \<open>even ((2 ^ m - (1::nat)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::nat) \<or> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1006
    for m n :: nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1007
    using even_mask_div_iff' [where ?'a = nat, of m n] by simp
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1008
  show \<open>even (q * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::nat) ^ n = 0 \<or> m \<le> n \<and> even (q div 2 ^ (n - m))\<close>
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1009
    for m n q r :: nat
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1010
    apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1011
    apply (metis (full_types) dvd_mult dvd_mult_imp_div dvd_power_iff_le not_less not_less_eq order_refl power_Suc)
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1012
    done
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1013
qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff)
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1014
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1015
lemma int_bit_induct [case_names zero minus even odd]:
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1016
  "P k" if zero_int: "P 0"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1017
    and minus_int: "P (- 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1018
    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
  1019
    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
  1020
proof (cases "k \<ge> 0")
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1021
  case True
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1022
  define n where "n = nat k"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1023
  with True have "k = int n"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1024
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1025
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1026
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1027
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1028
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1029
      by (simp add: zero_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1030
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1031
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1032
    have "P (int n * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1033
      by (rule even_int) (use even in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1034
    with even show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1035
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1036
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1037
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1038
    have "P (1 + (int n * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1039
      by (rule odd_int) (use odd in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1040
    with odd show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1041
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1042
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1043
next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1044
  case False
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1045
  define n where "n = nat (- k - 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1046
  with False have "k = - int n - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1047
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1048
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1049
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1050
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1051
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1052
      by (simp add: minus_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1053
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1054
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1055
    have "P (1 + (- int (Suc n) * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1056
      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
  1057
    also have "\<dots> = - int (2 * n) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1058
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1059
    finally show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1060
      using even by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1061
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1062
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1063
    have "P (- int (Suc n) * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1064
      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
  1065
    also have "\<dots> = - int (Suc (2 * n)) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1066
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1067
    finally show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1068
      using odd by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1069
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1070
qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1071
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1072
instance int :: semiring_bits
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1073
proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1074
  show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1075
    and rec: \<open>\<And>k b. P k \<Longrightarrow> (of_bool b + 2 * k) div 2 = k \<Longrightarrow> P (of_bool b + 2 * k)\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1076
    for P and k :: int
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1077
  proof (induction k rule: int_bit_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1078
    case zero
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1079
    from stable [of 0] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1080
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1081
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1082
    case minus
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1083
    from stable [of \<open>- 1\<close>] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1084
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1085
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1086
    case (even k)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1087
    with rec [of k False] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1088
      by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1089
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1090
    case (odd k)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1091
    with rec [of k True] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1092
      by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1093
  qed
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1094
  show \<open>(2::int) ^ m div 2 ^ n = of_bool ((2::int) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1095
    for m n :: nat
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1096
  proof (cases \<open>m < n\<close>)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1097
    case True
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1098
    then have \<open>n = m + (n - m)\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1099
      by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1100
    then have \<open>(2::int) ^ m div 2 ^ n = (2::int) ^ m div 2 ^ (m + (n - m))\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1101
      by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1102
    also have \<open>\<dots> = (2::int) ^ m div (2 ^ m * 2 ^ (n - m))\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1103
      by (simp add: power_add)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1104
    also have \<open>\<dots> = (2::int) ^ m div 2 ^ m div 2 ^ (n - m)\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1105
      by (simp add: zdiv_zmult2_eq)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1106
    finally show ?thesis using \<open>m < n\<close> by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1107
  next
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1108
    case False
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1109
    then show ?thesis
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1110
      by (simp add: power_diff)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1111
  qed
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1112
  show \<open>k mod 2 ^ m mod 2 ^ n = k mod 2 ^ min m n\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1113
    for m n :: nat and k :: int
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1114
    using mod_exp_eq [of \<open>nat k\<close> m n]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1115
    apply (auto simp add: mod_mod_cancel zdiv_zmult2_eq power_add zmod_zmult2_eq le_iff_add split: split_min_lin)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1116
     apply (auto simp add: less_iff_Suc_add mod_mod_cancel power_add)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1117
    apply (simp only: flip: mult.left_commute [of \<open>2 ^ m\<close>])
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1118
    apply (subst zmod_zmult2_eq) apply simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1119
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1120
  show \<open>(k * 2 ^ m) mod (2 ^ n) = (k mod 2 ^ (n - m)) * 2 ^ m\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1121
    if \<open>m \<le> n\<close> for m n :: nat and k :: int
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1122
    using that
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1123
    apply (auto simp add: power_add zmod_zmult2_eq le_iff_add split: split_min_lin)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1124
    apply (simp add: ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1125
    done
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1126
  show \<open>even ((2 ^ m - (1::int)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::int) \<or> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1127
    for m n :: nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1128
    using even_mask_div_iff' [where ?'a = int, of m n] by simp
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1129
  show \<open>even (k * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::int) ^ n = 0 \<or> m \<le> n \<and> even (k div 2 ^ (n - m))\<close>
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1130
    for m n :: nat and k l :: int
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1131
    apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex)
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1132
    apply (metis Suc_leI dvd_mult dvd_mult_imp_div dvd_power_le dvd_refl power.simps(2))
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1133
    done
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1134
qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1135
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1136
class semiring_bit_shifts = semiring_bits +
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1137
  fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1138
  assumes push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1139
  fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1140
  assumes drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close>
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1141
begin
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1142
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1143
definition take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1144
  where take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ n\<close>
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1145
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1146
text \<open>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1147
  Logically, \<^const>\<open>push_bit\<close>,
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1148
  \<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1149
  as separate operations makes proofs easier, otherwise proof automation
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1150
  would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1151
  algebraic relationships between those operations.
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1152
  Having
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1153
  \<^const>\<open>push_bit\<close> and \<^const>\<open>drop_bit\<close> as definitional class operations
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1154
  takes into account that specific instances of these can be implemented
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1155
  differently wrt. code generation.
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1156
\<close>
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1157
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1158
lemma bit_iff_odd_drop_bit:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1159
  \<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1160
  by (simp add: bit_def drop_bit_eq_div)
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1161
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1162
lemma even_drop_bit_iff_not_bit:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1163
  \<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1164
  by (simp add: bit_iff_odd_drop_bit)
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1165
71423
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1166
lemma div_push_bit_of_1_eq_drop_bit:
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1167
  \<open>a div push_bit n 1 = drop_bit n a\<close>
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1168
  by (simp add: push_bit_eq_mult drop_bit_eq_div)
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1169
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1170
lemma bits_ident:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1171
  "push_bit n (drop_bit n a) + take_bit n a = a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1172
  using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1173
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1174
lemma push_bit_push_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1175
  "push_bit m (push_bit n a) = push_bit (m + n) a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1176
  by (simp add: push_bit_eq_mult power_add ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1177
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1178
lemma push_bit_0_id [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1179
  "push_bit 0 = id"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1180
  by (simp add: fun_eq_iff push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1181
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1182
lemma push_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1183
  "push_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1184
  by (simp add: push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1185
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1186
lemma push_bit_of_1:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1187
  "push_bit n 1 = 2 ^ n"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1188
  by (simp add: push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1189
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1190
lemma push_bit_Suc [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1191
  "push_bit (Suc n) a = push_bit n (a * 2)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1192
  by (simp add: push_bit_eq_mult ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1193
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1194
lemma push_bit_double:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1195
  "push_bit n (a * 2) = push_bit n a * 2"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1196
  by (simp add: push_bit_eq_mult ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1197
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1198
lemma push_bit_add:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1199
  "push_bit n (a + b) = push_bit n a + push_bit n b"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1200
  by (simp add: push_bit_eq_mult algebra_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1201
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1202
lemma take_bit_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1203
  "take_bit 0 a = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1204
  by (simp add: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1205
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1206
lemma take_bit_Suc:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1207
  \<open>take_bit (Suc n) a = take_bit n (a div 2) * 2 + of_bool (odd a)\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1208
proof -
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1209
  have \<open>take_bit (Suc n) (a div 2 * 2 + of_bool (odd a)) = take_bit n (a div 2) * 2 + of_bool (odd a)\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1210
    using even_succ_mod_exp [of \<open>2 * (a div 2)\<close> \<open>Suc n\<close>]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1211
      mult_exp_mod_exp_eq [of 1 \<open>Suc n\<close> \<open>a div 2\<close>]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1212
    by (auto simp add: take_bit_eq_mod ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1213
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1214
    using div_mult_mod_eq [of a 2] by (simp add: mod_2_eq_odd)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1215
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1216
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1217
lemma take_bit_rec:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1218
  \<open>take_bit n a = (if n = 0 then 0 else take_bit (n - 1) (a div 2) * 2 + of_bool (odd a))\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1219
  by (cases n) (simp_all add: take_bit_Suc)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1220
71759
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1221
lemma take_bit_Suc_0 [simp]:
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1222
  \<open>take_bit (Suc 0) a = a mod 2\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1223
  by (simp add: take_bit_eq_mod)
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1224
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1225
lemma take_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1226
  "take_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1227
  by (simp add: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1228
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1229
lemma take_bit_of_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1230
  "take_bit n 1 = of_bool (n > 0)"
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1231
  by (cases n) (simp_all add: take_bit_Suc)
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1232
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1233
lemma drop_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1234
  "drop_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1235
  by (simp add: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1236
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1237
lemma drop_bit_of_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1238
  "drop_bit n 1 = of_bool (n = 0)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1239
  by (simp add: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1240
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1241
lemma drop_bit_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1242
  "drop_bit 0 = id"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1243
  by (simp add: fun_eq_iff drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1244
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1245
lemma drop_bit_Suc:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1246
  "drop_bit (Suc n) a = drop_bit n (a div 2)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1247
  using div_exp_eq [of a 1] by (simp add: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1248
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1249
lemma drop_bit_rec:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1250
  "drop_bit n a = (if n = 0 then a else drop_bit (n - 1) (a div 2))"
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1251
  by (cases n) (simp_all add: drop_bit_Suc)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1252
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1253
lemma drop_bit_half:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1254
  "drop_bit n (a div 2) = drop_bit n a div 2"
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1255
  by (induction n arbitrary: a) (simp_all add: drop_bit_Suc)
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1256
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1257
lemma drop_bit_of_bool [simp]:
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1258
  "drop_bit n (of_bool b) = of_bool (n = 0 \<and> b)"
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1259
  by (cases n) simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1260
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1261
lemma take_bit_eq_0_imp_dvd:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1262
  "take_bit n a = 0 \<Longrightarrow> 2 ^ n dvd a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1263
  by (simp add: take_bit_eq_mod mod_0_imp_dvd)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1264
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1265
lemma even_take_bit_eq [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1266
  \<open>even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a\<close>
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1267
  by (simp add: take_bit_rec [of n a])
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1268
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1269
lemma take_bit_take_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1270
  "take_bit m (take_bit n a) = take_bit (min m n) a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1271
  by (simp add: take_bit_eq_mod mod_exp_eq ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1272
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1273
lemma drop_bit_drop_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1274
  "drop_bit m (drop_bit n a) = drop_bit (m + n) a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1275
  by (simp add: drop_bit_eq_div power_add div_exp_eq ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1276
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1277
lemma push_bit_take_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1278
  "push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1279
  apply (simp add: push_bit_eq_mult take_bit_eq_mod power_add ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1280
  using mult_exp_mod_exp_eq [of m \<open>m + n\<close> a] apply (simp add: ac_simps power_add)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1281
  done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1282
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1283
lemma take_bit_push_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1284
  "take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1285
proof (cases "m \<le> n")
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1286
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1287
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1288
    apply (simp add:)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1289
    apply (simp_all add: push_bit_eq_mult take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1290
    apply (auto dest!: le_Suc_ex simp add: power_add ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1291
    using mult_exp_mod_exp_eq [of m m \<open>a * 2 ^ n\<close> for n]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1292
    apply (simp add: ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1293
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1294
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1295
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1296
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1297
    using push_bit_take_bit [of n "m - n" a]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1298
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1299
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1300
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1301
lemma take_bit_drop_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1302
  "take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1303
  by (simp add: drop_bit_eq_div take_bit_eq_mod ac_simps div_exp_mod_exp_eq)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1304
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1305
lemma drop_bit_take_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1306
  "drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1307
proof (cases "m \<le> n")
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1308
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1309
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1310
    using take_bit_drop_bit [of "n - m" m a] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1311
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1312
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1313
  then obtain q where \<open>m = n + q\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1314
    by (auto simp add: not_le dest: less_imp_Suc_add)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1315
  then have \<open>drop_bit m (take_bit n a) = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1316
    using div_exp_eq [of \<open>a mod 2 ^ n\<close> n q]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1317
    by (simp add: take_bit_eq_mod drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1318
  with False show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1319
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1320
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1321
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1322
lemma even_push_bit_iff [simp]:
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1323
  \<open>even (push_bit n a) \<longleftrightarrow> n \<noteq> 0 \<or> even a\<close>
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1324
  by (simp add: push_bit_eq_mult) auto
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1325
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1326
lemma bit_push_bit_iff:
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1327
  \<open>bit (push_bit m a) n \<longleftrightarrow> n \<ge> m \<and> 2 ^ n \<noteq> 0 \<and> (n < m \<or> bit a (n - m))\<close>
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1328
  by (auto simp add: bit_def push_bit_eq_mult even_mult_exp_div_exp_iff)
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1329
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1330
lemma bit_drop_bit_eq:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1331
  \<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1332
  by (simp add: bit_def fun_eq_iff ac_simps flip: drop_bit_eq_div)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1333
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1334
lemma bit_take_bit_iff:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1335
  \<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1336
  by (simp add: bit_def drop_bit_take_bit not_le flip: drop_bit_eq_div)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1337
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1338
lemma stable_imp_drop_bit_eq:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1339
  \<open>drop_bit n a = a\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1340
  if \<open>a div 2 = a\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1341
  by (induction n) (simp_all add: that drop_bit_Suc)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1342
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1343
lemma stable_imp_take_bit_eq:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1344
  \<open>take_bit n a = (if even a then 0 else 2 ^ n - 1)\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1345
    if \<open>a div 2 = a\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1346
proof (rule bit_eqI)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1347
  fix m
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1348
  assume \<open>2 ^ m \<noteq> 0\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1349
  with that show \<open>bit (take_bit n a) m \<longleftrightarrow> bit (if even a then 0 else 2 ^ n - 1) m\<close>
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1350
    by (simp add: bit_take_bit_iff bit_mask_iff stable_imp_bit_iff_odd)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1351
qed
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1352
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1353
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1354
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1355
instantiation nat :: semiring_bit_shifts
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1356
begin
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1357
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1358
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1359
  where \<open>push_bit_nat n m = m * 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1360
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1361
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1362
  where \<open>drop_bit_nat n m = m div 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1363
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1364
instance proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1365
  show \<open>push_bit n m = m * 2 ^ n\<close> for n m :: nat
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1366
    by (simp add: push_bit_nat_def)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1367
  show \<open>drop_bit n m = m div 2 ^ n\<close> for n m :: nat
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1368
    by (simp add: drop_bit_nat_def)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1369
qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1370
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1371
end
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1372
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1373
instantiation int :: semiring_bit_shifts
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1374
begin
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1375
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1376
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1377
  where \<open>push_bit_int n k = k * 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1378
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1379
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1380
  where \<open>drop_bit_int n k = k div 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1381
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1382
instance proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1383
  show \<open>push_bit n k = k * 2 ^ n\<close> for n :: nat and k :: int
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1384
    by (simp add: push_bit_int_def)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1385
  show \<open>drop_bit n k = k div 2 ^ n\<close> for n :: nat and k :: int
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1386
    by (simp add: drop_bit_int_def)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1387
qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1388
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1389
end
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1390
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1391
lemma bit_push_bit_iff_nat:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1392
  \<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1393
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1394
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1395
lemma bit_push_bit_iff_int:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1396
  \<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1397
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1398
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1399
class unique_euclidean_semiring_with_bit_shifts =
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1400
  unique_euclidean_semiring_with_nat + semiring_bit_shifts
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1401
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1402
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1403
lemma take_bit_of_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1404
  \<open>take_bit m (2 ^ n) = of_bool (n < m) * 2 ^ n\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1405
  by (simp add: take_bit_eq_mod exp_mod_exp)
67960
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
  1406
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1407
lemma take_bit_of_2 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1408
  \<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1409
  using take_bit_of_exp [of n 1] by simp
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1410
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1411
lemma take_bit_of_mask:
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1412
  \<open>take_bit m (2 ^ n - 1) = 2 ^ min m n - 1\<close>
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1413
  by (simp add: take_bit_eq_mod mask_mod_exp)
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1414
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1415
lemma push_bit_eq_0_iff [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1416
  "push_bit n a = 0 \<longleftrightarrow> a = 0"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1417
  by (simp add: push_bit_eq_mult)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1418
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1419
lemma push_bit_numeral [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1420
  "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
  1421
  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
  1422
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1423
lemma push_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1424
  "push_bit n (of_nat m) = of_nat (push_bit n m)"
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1425
  by (simp add: push_bit_eq_mult Parity.push_bit_eq_mult)
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1426
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1427
lemma take_bit_add:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1428
  "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
  1429
  by (simp add: take_bit_eq_mod mod_simps)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1430
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1431
lemma take_bit_eq_0_iff:
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1432
  "take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a"
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1433
  by (simp add: take_bit_eq_mod mod_eq_0_iff_dvd)
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1434
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1435
lemma take_bit_of_1_eq_0_iff [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1436
  "take_bit n 1 = 0 \<longleftrightarrow> n = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1437
  by (simp add: take_bit_eq_mod)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1438
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1439
lemma take_bit_Suc_bit0 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1440
  \<open>take_bit (Suc n) (numeral (Num.Bit0 k)) = take_bit n (numeral k) * 2\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1441
  by (simp add: take_bit_Suc numeral_Bit0_div_2)
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1442
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1443
lemma take_bit_Suc_bit1 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1444
  \<open>take_bit (Suc n) (numeral (Num.Bit1 k)) = take_bit n (numeral k) * 2 + 1\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1445
  by (simp add: take_bit_Suc numeral_Bit1_div_2)
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1446
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1447
lemma take_bit_numeral_bit0 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1448
  \<open>take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1449
  by (simp add: take_bit_rec numeral_Bit0_div_2)
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1450
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1451
lemma take_bit_numeral_bit1 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1452
  \<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1453
  by (simp add: take_bit_rec numeral_Bit1_div_2)
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1454
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1455
lemma take_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1456
  "take_bit n (of_nat m) = of_nat (take_bit n m)"
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1457
  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
  1458
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1459
lemma drop_bit_Suc_bit0 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1460
  \<open>drop_bit (Suc n) (numeral (Num.Bit0 k)) = drop_bit n (numeral k)\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1461
  by (simp add: drop_bit_Suc numeral_Bit0_div_2)
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1462
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1463
lemma drop_bit_Suc_bit1 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1464
  \<open>drop_bit (Suc n) (numeral (Num.Bit1 k)) = drop_bit n (numeral k)\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1465
  by (simp add: drop_bit_Suc numeral_Bit1_div_2)
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1466
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1467
lemma drop_bit_numeral_bit0 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1468
  \<open>drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1469
  by (simp add: drop_bit_rec numeral_Bit0_div_2)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1470
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1471
lemma drop_bit_numeral_bit1 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1472
  \<open>drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)\<close>
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1473
  by (simp add: drop_bit_rec numeral_Bit1_div_2)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1474
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1475
lemma drop_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1476
  "drop_bit n (of_nat m) = of_nat (drop_bit n m)"
68389
1c84a8c513af proper white space;
wenzelm
parents: 68157
diff changeset
  1477
  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
  1478
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1479
lemma bit_of_nat_iff_bit [simp]:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1480
  \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1481
proof -
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1482
  have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1483
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1484
  also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1485
    by (simp add: of_nat_div)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1486
  finally show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1487
    by (simp add: bit_def semiring_bits_class.bit_def)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1488
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1489
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1490
lemma of_nat_push_bit:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1491
  \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1492
  by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1493
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1494
lemma of_nat_drop_bit:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1495
  \<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1496
  by (simp add: drop_bit_eq_div semiring_bit_shifts_class.drop_bit_eq_div of_nat_div)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1497
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1498
lemma of_nat_take_bit:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1499
  \<open>of_nat (take_bit m n) = take_bit m (of_nat n)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1500
  by (simp add: take_bit_eq_mod semiring_bit_shifts_class.take_bit_eq_mod of_nat_mod)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1501
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1502
lemma bit_push_bit_iff_of_nat_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1503
  \<open>bit (push_bit m (of_nat r)) n \<longleftrightarrow> m \<le> n \<and> bit (of_nat r) (n - m)\<close>
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1504
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1505
58770
ae5e9b4f8daf downshift of theory Parity in the hierarchy
haftmann
parents: 58769
diff changeset
  1506
end
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1507
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1508
instance nat :: unique_euclidean_semiring_with_bit_shifts ..
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1509
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1510
instance int :: unique_euclidean_semiring_with_bit_shifts ..
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1511
71802
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1512
lemma not_exp_less_eq_0_int [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1513
  \<open>\<not> 2 ^ n \<le> (0::int)\<close>
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1514
  by (simp add: power_le_zero_eq)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1515
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1516
lemma half_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1517
  \<open>k div 2 \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1518
proof (cases \<open>k \<ge> 0\<close>)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1519
  case True
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1520
  then show ?thesis
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1521
    by (auto simp add: divide_int_def sgn_1_pos)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1522
next
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1523
  case False
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1524
  then show ?thesis
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1525
    apply (auto simp add: divide_int_def not_le elim!: evenE)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1526
    apply (simp only: minus_mult_right)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1527
    apply (subst nat_mult_distrib)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1528
     apply simp_all
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1529
    done
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1530
qed
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1531
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1532
lemma half_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1533
  \<open>k div 2 < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1534
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1535
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1536
lemma push_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1537
  "push_bit n (Suc 0) = 2 ^ n"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1538
  using push_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1539
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1540
lemma take_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1541
  "take_bit n (Suc 0) = of_bool (0 < n)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1542
  using take_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1543
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1544
lemma drop_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1545
  "drop_bit n (Suc 0) = of_bool (n = 0)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1546
  using drop_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1547
70973
a7a52ba0717d more lemmas
haftmann
parents: 70911
diff changeset
  1548
lemma take_bit_eq_self:
a7a52ba0717d more lemmas
haftmann
parents: 70911
diff changeset
  1549
  \<open>take_bit n m = m\<close> if \<open>m < 2 ^ n\<close> for n m :: nat
a7a52ba0717d more lemmas
haftmann
parents: 70911
diff changeset
  1550
  using that by (simp add: take_bit_eq_mod)
a7a52ba0717d more lemmas
haftmann
parents: 70911
diff changeset
  1551
70911
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1552
lemma push_bit_minus_one:
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1553
  "push_bit n (- 1 :: int) = - (2 ^ n)"
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1554
  by (simp add: push_bit_eq_mult)
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1555
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1556
lemma minus_1_div_exp_eq_int:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1557
  \<open>- 1 div (2 :: int) ^ n = - 1\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1558
  by (induction n) (use div_exp_eq [symmetric, of \<open>- 1 :: int\<close> 1] in \<open>simp_all add: ac_simps\<close>)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1559
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1560
lemma drop_bit_minus_one [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1561
  \<open>drop_bit n (- 1 :: int) = - 1\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1562
  by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1563
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1564
lemma take_bit_minus:
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1565
  "take_bit n (- (take_bit n k)) = take_bit n (- k)"
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1566
    for k :: int
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1567
  by (simp add: take_bit_eq_mod mod_minus_eq)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1568
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1569
lemma take_bit_diff:
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1570
  "take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)"
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1571
    for k l :: int
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1572
  by (simp add: take_bit_eq_mod mod_diff_eq)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1573
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1574
lemma take_bit_nonnegative [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1575
  "take_bit n k \<ge> 0"
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1576
    for k :: int
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1577
  by (simp add: take_bit_eq_mod)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1578
71759
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1579
lemma take_bit_minus_small_eq:
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1580
  \<open>take_bit n (- k) = 2 ^ n - k\<close> if \<open>0 < k\<close> \<open>k \<le> 2 ^ n\<close> for k :: int
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1581
proof -
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1582
  define m where \<open>m = nat k\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1583
  with that have \<open>k = int m\<close> and \<open>0 < m\<close> and \<open>m \<le> 2 ^ n\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1584
    by simp_all
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1585
  have \<open>(2 ^ n - m) mod 2 ^ n = 2 ^ n - m\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1586
    using \<open>0 < m\<close> by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1587
  then have \<open>int ((2 ^ n - m) mod 2 ^ n) = int (2 ^ n - m)\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1588
    by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1589
  then have \<open>(2 ^ n - int m) mod 2 ^ n = 2 ^ n - int m\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1590
    using \<open>m \<le> 2 ^ n\<close> by (simp only: of_nat_mod of_nat_diff) simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1591
  with \<open>k = int m\<close> have \<open>(2 ^ n - k) mod 2 ^ n = 2 ^ n - k\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1592
    by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1593
  then show ?thesis
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1594
    by (simp add: take_bit_eq_mod)
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1595
qed
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1596
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1597
lemma drop_bit_push_bit_int:
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1598
  \<open>drop_bit m (push_bit n k) = drop_bit (m - n) (push_bit (n - m) k)\<close> for k :: int
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1599
  by (cases \<open>m \<le> n\<close>) (auto simp add: mult.left_commute [of _ \<open>2 ^ n\<close>] mult.commute [of _ \<open>2 ^ n\<close>] mult.assoc
71802
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1600
    mult.commute [of k] drop_bit_eq_div push_bit_eq_mult not_le power_add dest!: le_Suc_ex less_imp_Suc_add)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1601
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1602
lemma push_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1603
  \<open>push_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1604
  by (simp add: push_bit_eq_mult zero_le_mult_iff)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1605
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1606
lemma push_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1607
  \<open>push_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1608
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1609
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1610
lemma drop_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1611
  \<open>drop_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1612
  by (induction n) (simp_all add: drop_bit_Suc drop_bit_half)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1613
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1614
lemma drop_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1615
  \<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1616
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1617
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1618
end