src/HOL/Parity.thy
author haftmann
Sun, 15 Nov 2020 10:13:03 +0000
changeset 72611 c7bc3e70a8c7
parent 72512 83b5911c0164
child 72792 26492b600d78
permissions -rw-r--r--
official collection for bit projection simplifications
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
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   175
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   176
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   177
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
   178
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   179
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   180
subclass comm_ring_1 ..
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   181
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   182
lemma even_minus:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   183
  "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
   184
  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
   185
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   186
lemma even_diff [simp]:
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   187
  "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
   188
  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
   189
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   190
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   191
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   192
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   193
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
   194
71157
8bdf3c36011c tuned theory structure
haftmann
parents: 71138
diff changeset
   195
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
   196
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   197
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   198
subclass semiring_parity
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   199
proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   200
  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
   201
    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
   202
  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
   203
  proof
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   204
    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
   205
    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
   206
      by auto
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   207
  next
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   208
    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
   209
    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
   210
    proof (rule order_antisym)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   211
      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
   212
        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
   213
      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
   214
        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
   215
    qed 
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   216
    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
   217
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   218
    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
   219
      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
   220
    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
   221
      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
   222
    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
   223
      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
   224
    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
   225
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   226
    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
   227
      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
   228
    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
   229
    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
   230
      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
   231
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   232
  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
   233
  proof (rule notI)
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   234
    assume "is_unit 2"
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 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
   236
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   237
    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
   238
      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
   239
    then show False
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   240
      by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   241
  qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   242
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   243
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   244
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
   245
  "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
   246
proof -
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   247
  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
   248
    by simp
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   249
  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
   250
    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
   251
  finally show ?thesis .
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   252
qed
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   253
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   254
lemma even_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   255
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   256
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   257
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   258
lemma odd_succ_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   259
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   260
  by (auto elim!: oddE simp add: add.assoc)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   261
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   262
lemma even_two_times_div_two:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   263
  "even a \<Longrightarrow> 2 * (a div 2) = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   264
  by (fact dvd_mult_div_cancel)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   265
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   266
lemma odd_two_times_div_two_succ [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   267
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   268
  using mult_div_mod_eq [of 2 a]
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   269
  by (simp add: even_iff_mod_2_eq_zero)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   270
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   271
lemma coprime_left_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   272
  "coprime 2 a \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   273
proof
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   274
  assume "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   275
  show "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   276
  proof (rule coprimeI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   277
    fix b
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   278
    assume "b dvd 2" "b dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   279
    then have "b dvd a mod 2"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   280
      by (auto intro: dvd_mod)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   281
    with \<open>odd a\<close> show "is_unit b"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   282
      by (simp add: mod_2_eq_odd)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   283
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   284
next
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   285
  assume "coprime 2 a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   286
  show "odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   287
  proof (rule notI)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   288
    assume "even a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   289
    then obtain b where "a = 2 * b" ..
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   290
    with \<open>coprime 2 a\<close> have "coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   291
      by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   292
    moreover have "\<not> coprime 2 (2 * b)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   293
      by (rule not_coprimeI [of 2]) simp_all
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   294
    ultimately show False
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   295
      by blast
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   296
  qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   297
qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   298
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   299
lemma coprime_right_2_iff_odd [simp]:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   300
  "coprime a 2 \<longleftrightarrow> odd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   301
  using coprime_left_2_iff_odd [of a] by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66840
diff changeset
   302
58678
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   303
end
398e05aa84d4 purely algebraic characterization of even and odd
haftmann
parents: 58645
diff changeset
   304
71157
8bdf3c36011c tuned theory structure
haftmann
parents: 71138
diff changeset
   305
context unique_euclidean_ring_with_nat
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   306
begin
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   307
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   308
subclass ring_parity ..
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   309
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   310
lemma minus_1_mod_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   311
  "- 1 mod 2 = 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   312
  by (simp add: mod_2_eq_odd)
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   313
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   314
lemma minus_1_div_2_eq [simp]:
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   315
  "- 1 div 2 = - 1"
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   316
proof -
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   317
  from div_mult_mod_eq [of "- 1" 2]
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   318
  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
   319
    using add_implies_diff by fastforce
67906
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   320
  then show ?thesis
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   321
    using mult_right_cancel [of 2 "- 1 div 2" "- 1"] by simp
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   322
qed
9cc32b18c785 more lemmas
haftmann
parents: 67905
diff changeset
   323
58679
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   324
end
33c90658448a more algebraic deductions for facts on even/odd
haftmann
parents: 58678
diff changeset
   325
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   326
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   327
subsection \<open>Instance for \<^typ>\<open>nat\<close>\<close>
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   328
70340
7383930fc946 slightly more specialized name for type class
haftmann
parents: 70339
diff changeset
   329
instance nat :: unique_euclidean_semiring_with_nat
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   330
  by standard (simp_all add: dvd_eq_mod_eq_0)
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66582
diff changeset
   331
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   332
lemma even_Suc_Suc_iff [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   333
  "even (Suc (Suc n)) \<longleftrightarrow> even n"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   334
  using dvd_add_triv_right_iff [of 2 n] by simp
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   335
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   336
lemma even_Suc [simp]: "even (Suc n) \<longleftrightarrow> odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   337
  using even_plus_one_iff [of n] by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   338
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   339
lemma even_diff_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   340
  "even (m - n) \<longleftrightarrow> m < n \<or> even (m + n)" for m n :: nat
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   341
proof (cases "n \<le> m")
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   342
  case True
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   343
  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
   344
  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
   345
  ultimately have "even (m - n) \<longleftrightarrow> even (m + n)" by (simp only:)
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   346
  then show ?thesis by auto
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   347
next
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   348
  case False
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   349
  then show ?thesis by simp
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   350
qed
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   351
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   352
lemma odd_pos:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   353
  "odd n \<Longrightarrow> 0 < n" for n :: nat
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   354
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   355
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   356
lemma Suc_double_not_eq_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   357
  "Suc (2 * m) \<noteq> 2 * n"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   358
proof
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   359
  assume "Suc (2 * m) = 2 * n"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   360
  moreover have "odd (Suc (2 * m))" and "even (2 * n)"
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   361
    by simp_all
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   362
  ultimately show False by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   363
qed
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   364
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   365
lemma double_not_eq_Suc_double:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   366
  "2 * m \<noteq> Suc (2 * n)"
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   367
  using Suc_double_not_eq_double [of n m] by simp
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 62083
diff changeset
   368
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   369
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
   370
  by (auto elim: oddE)
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   371
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   372
lemma even_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   373
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   374
  using even_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   375
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   376
lemma odd_Suc_div_two [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   377
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   378
  using odd_succ_div_two [of n] by simp
60343
063698416239 correct sort constraints for abbreviations in type classes
haftmann
parents: 59816
diff changeset
   379
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   380
lemma odd_two_times_div_two_nat [simp]:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   381
  assumes "odd n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   382
  shows "2 * (n div 2) = n - (1 :: nat)"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   383
proof -
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   384
  from assms have "2 * (n div 2) + 1 = n"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   385
    by (rule odd_two_times_div_two_succ)
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   386
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   387
    by simp
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   388
  then show ?thesis
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   389
    by simp
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   390
qed
58680
6b2fa479945f more algebraic deductions for facts on even/odd
haftmann
parents: 58679
diff changeset
   391
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
   392
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
   393
  "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
   394
  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
   395
69502
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   396
lemma odd_card_imp_not_empty:
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   397
  \<open>A \<noteq> {}\<close> if \<open>odd (card A)\<close>
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   398
  using that by auto
0cf906072e20 more rules
haftmann
parents: 69198
diff changeset
   399
70365
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   400
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
   401
  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
   402
  shows "P n"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   403
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
   404
  case (less n)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   405
  show ?case
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   406
  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
   407
    case True
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   408
    then show ?thesis
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   409
      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
   410
  next
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   411
    case False
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   412
    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
   413
      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
   414
    then have "k<n"
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   415
      by simp
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   416
    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
   417
      by blast
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   418
    then show ?thesis
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   419
      by (simp add: k)
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   420
  qed
4df0628e8545 a few new lemmas and a bit of tidying
paulson <lp15@cam.ac.uk>
parents: 70353
diff changeset
   421
qed
58687
5469874b0228 even more cleanup
haftmann
parents: 58681
diff changeset
   422
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   423
lemma mask_eq_sum_exp_nat:
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   424
  \<open>2 ^ n - Suc 0 = (\<Sum>m\<in>{q. q < n}. 2 ^ m)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   425
  using mask_eq_sum_exp [where ?'a = nat] by simp
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   426
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   427
context semiring_parity
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   428
begin
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   429
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   430
lemma even_sum_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   431
  \<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
   432
using that proof (induction A)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   433
  case empty
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   434
  then show ?case
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   435
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   436
next
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   437
  case (insert a A)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   438
  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
   439
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   440
  ultimately show ?case
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   441
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   442
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   443
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   444
lemma even_prod_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   445
  \<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
   446
  using that by (induction A) simp_all
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   447
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   448
lemma even_mask_iff [simp]:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   449
  \<open>even (2 ^ n - 1) \<longleftrightarrow> n = 0\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   450
proof (cases \<open>n = 0\<close>)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   451
  case True
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   452
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   453
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   454
next
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   455
  case False
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   456
  then have \<open>{a. a = 0 \<and> a < n} = {0}\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   457
    by auto
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   458
  then show ?thesis
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   459
    by (auto simp add: mask_eq_sum_exp even_sum_iff)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   460
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   461
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   462
end
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
   463
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   464
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   465
subsection \<open>Parity and powers\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   466
61531
ab2e862263e7 Rounding function, uniform limits, cotangent, binomial identities
eberlm
parents: 60867
diff changeset
   467
context ring_1
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   468
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   469
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   470
lemma power_minus_even [simp]: "even n \<Longrightarrow> (- a) ^ n = a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   471
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   472
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   473
lemma power_minus_odd [simp]: "odd n \<Longrightarrow> (- a) ^ n = - (a ^ n)"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   474
  by (auto elim: oddE)
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   475
66815
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   476
lemma uminus_power_if:
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   477
  "(- a) ^ n = (if even n then a ^ n else - (a ^ n))"
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   478
  by auto
93c6632ddf44 one uniform type class for parity structures
haftmann
parents: 66808
diff changeset
   479
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   480
lemma neg_one_even_power [simp]: "even n \<Longrightarrow> (- 1) ^ n = 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   481
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   482
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   483
lemma neg_one_odd_power [simp]: "odd n \<Longrightarrow> (- 1) ^ n = - 1"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   484
  by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   485
66582
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   486
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
   487
  by (cases "even (n + k)") auto
2b49d4888cb8 another fact on (- 1) ^ _
bulwahn
parents: 64785
diff changeset
   488
67371
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   489
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
   490
  by (induct n) auto
2d9cf74943e1 moved in some material from Euler-MacLaurin
paulson <lp15@cam.ac.uk>
parents: 67083
diff changeset
   491
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   492
end
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   493
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   494
context linordered_idom
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   495
begin
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   496
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   497
lemma zero_le_even_power: "even n \<Longrightarrow> 0 \<le> a ^ n"
58690
5c5c14844738 standard elimination rule for even
haftmann
parents: 58689
diff changeset
   498
  by (auto elim: evenE)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   499
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   500
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
   501
  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
   502
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   503
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
   504
  by (auto simp add: zero_le_even_power zero_le_odd_power)
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   505
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   506
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
   507
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   508
  have [simp]: "0 = a ^ n \<longleftrightarrow> a = 0 \<and> n > 0"
58787
af9eb5e566dd eliminated redundancies;
haftmann
parents: 58778
diff changeset
   509
    unfolding power_eq_0_iff [of a n, symmetric] by blast
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   510
  show ?thesis
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   511
    unfolding less_le zero_le_power_eq by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   512
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   513
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   514
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
   515
  unfolding not_le [symmetric] zero_le_power_eq by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   516
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   517
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
   518
  unfolding not_less [symmetric] zero_less_power_eq by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   519
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   520
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
   521
  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
   522
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   523
lemma power_mono_even:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   524
  assumes "even n" and "\<bar>a\<bar> \<le> \<bar>b\<bar>"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   525
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   526
proof -
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   527
  have "0 \<le> \<bar>a\<bar>" by auto
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   528
  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
   529
    by (rule power_mono)
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   530
  with \<open>even n\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   531
    by (simp add: power_even_abs)
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   532
qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   533
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   534
lemma power_mono_odd:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   535
  assumes "odd n" and "a \<le> b"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   536
  shows "a ^ n \<le> b ^ n"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   537
proof (cases "b < 0")
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   538
  case True
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   539
  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
   540
  then have "(- b) ^ n \<le> (- a) ^ n" by (rule power_mono)
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   541
  with \<open>odd n\<close> show ?thesis by simp
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   542
next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   543
  case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   544
  then have "0 \<le> b" by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   545
  show ?thesis
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   546
  proof (cases "a < 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
    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
   549
    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
   550
    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
   551
    ultimately show ?thesis by auto
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   552
  next
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   553
    case False
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   554
    then have "0 \<le> a" by auto
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   555
    with \<open>a \<le> b\<close> show ?thesis
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   556
      using power_mono by auto
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   557
  qed
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   558
qed
62083
7582b39f51ed add the proof of the central limit theorem
hoelzl
parents: 61799
diff changeset
   559
60758
d8d85a8172b5 isabelle update_cartouches;
wenzelm
parents: 60562
diff changeset
   560
text \<open>Simplify, when the exponent is a numeral\<close>
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   561
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   562
lemma zero_le_power_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   563
  "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
   564
  by (fact zero_le_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   565
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   566
lemma zero_less_power_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   567
  "0 < a ^ numeral w \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   568
    numeral w = (0 :: nat) \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   569
    even (numeral w :: nat) \<and> a \<noteq> 0 \<or>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   570
    odd (numeral w :: nat) \<and> 0 < a"
58689
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   571
  by (fact zero_less_power_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   572
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   573
lemma power_le_zero_eq_numeral [simp]:
63654
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   574
  "a ^ numeral w \<le> 0 \<longleftrightarrow>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   575
    (0 :: nat) < numeral w \<and>
f90e3926e627 misc tuning and modernization;
wenzelm
parents: 62597
diff changeset
   576
    (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
   577
  by (fact power_le_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   578
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   579
lemma power_less_zero_eq_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   580
  "a ^ numeral w < 0 \<longleftrightarrow> odd (numeral w :: nat) \<and> a < 0"
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   581
  by (fact power_less_zero_eq)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   582
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   583
lemma power_even_abs_numeral [simp]:
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   584
  "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
   585
  by (fact power_even_abs)
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   586
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   587
end
ee5bf401cfa7 tuned facts on even and power
haftmann
parents: 58688
diff changeset
   588
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   589
context unique_euclidean_semiring_with_nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   590
begin
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   591
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   592
lemma even_mask_div_iff':
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   593
  \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   594
proof -
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   595
  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
   596
    by (simp only: of_nat_div) (simp add: of_nat_diff)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   597
  also have \<open>\<dots> \<longleftrightarrow> even ((2 ^ m - Suc 0) div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   598
    by simp
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   599
  also have \<open>\<dots> \<longleftrightarrow> m \<le> n\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   600
  proof (cases \<open>m \<le> n\<close>)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   601
    case True
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   602
    then show ?thesis
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   603
      by (simp add: Suc_le_lessD)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   604
  next
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   605
    case False
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   606
    then obtain r where r: \<open>m = n + Suc r\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   607
      using less_imp_Suc_add by fastforce
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   608
    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
   609
      by (auto simp add: dvd_power_iff_le)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   610
    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
   611
      by (auto simp add: dvd_power_iff_le)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   612
    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
   613
      by auto
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   614
    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
   615
      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
   616
    ultimately have \<open>odd (sum ((^) (2::nat)) {q. q < m} div 2 ^ n)\<close>
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   617
      by (subst euclidean_semiring_cancel_class.sum_div_partition) simp_all
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   618
    with False show ?thesis
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   619
      by (simp add: mask_eq_sum_exp_nat)
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   620
  qed
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   621
  finally show ?thesis .
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   622
qed
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   623
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   624
end
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
   625
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   626
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69502
diff changeset
   627
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
   628
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   629
lemma even_diff_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   630
  "even (k - l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   631
  by (fact even_diff)
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   632
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   633
lemma even_abs_add_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   634
  "even (\<bar>k\<bar> + l) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   635
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   636
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   637
lemma even_add_abs_iff:
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   638
  "even (k + \<bar>l\<bar>) \<longleftrightarrow> even (k + l)" for k l :: int
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
   639
  by simp
66816
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   640
212a3334e7da more fundamental definition of div and mod on int
haftmann
parents: 66815
diff changeset
   641
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
   642
  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
   643
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   644
lemma zdiv_zmult2_eq:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   645
  \<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
   646
proof (cases \<open>b \<ge> 0\<close>)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   647
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   648
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   649
    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
   650
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   651
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   652
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   653
    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
   654
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   655
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   656
lemma zmod_zmult2_eq:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   657
  \<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
   658
proof (cases \<open>b \<ge> 0\<close>)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   659
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   660
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   661
    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
   662
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   663
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   664
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   665
    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
   666
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   667
71837
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   668
context
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   669
  assumes "SORT_CONSTRAINT('a::division_ring)"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   670
begin
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   671
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   672
lemma power_int_minus_left:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   673
  "power_int (-a :: 'a) n = (if even n then power_int a n else -power_int a n)"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   674
  by (auto simp: power_int_def minus_one_power_iff even_nat_iff)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   675
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   676
lemma power_int_minus_left_even [simp]: "even n \<Longrightarrow> power_int (-a :: 'a) n = power_int a n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   677
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   678
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   679
lemma power_int_minus_left_odd [simp]: "odd n \<Longrightarrow> power_int (-a :: 'a) n = -power_int a n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   680
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   681
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   682
lemma power_int_minus_left_distrib:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   683
  "NO_MATCH (-1) x \<Longrightarrow> power_int (-a :: 'a) n = power_int (-1) n * power_int a n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   684
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   685
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   686
lemma power_int_minus_one_minus: "power_int (-1 :: 'a) (-n) = power_int (-1) n"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   687
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   688
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   689
lemma power_int_minus_one_diff_commute: "power_int (-1 :: 'a) (a - b) = power_int (-1) (b - a)"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   690
  by (subst power_int_minus_one_minus [symmetric]) auto
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   691
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   692
lemma power_int_minus_one_mult_self [simp]:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   693
  "power_int (-1 :: 'a) m * power_int (-1) m = 1"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   694
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   695
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   696
lemma power_int_minus_one_mult_self' [simp]:
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   697
  "power_int (-1 :: 'a) m * (power_int (-1) m * b) = b"
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   698
  by (simp add: power_int_minus_left)
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   699
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   700
end
dca11678c495 new constant power_int in HOL
Manuel Eberl <eberlm@in.tum.de>
parents: 71822
diff changeset
   701
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   702
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   703
subsection \<open>Abstract bit structures\<close>
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   704
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   705
class semiring_bits = semiring_parity +
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   706
  assumes bits_induct [case_names stable rec]:
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   707
    \<open>(\<And>a. a div 2 = a \<Longrightarrow> P a)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
   708
     \<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
   709
        \<Longrightarrow> P a\<close>
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   710
  assumes bits_div_0 [simp]: \<open>0 div a = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   711
    and bits_div_by_1 [simp]: \<open>a div 1 = a\<close>
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   712
    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
   713
    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
   714
    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
   715
    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
   716
    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
   717
    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
   718
    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
   719
    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
   720
    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>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   721
  fixes bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   722
  assumes bit_iff_odd: \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close>
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   723
begin
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   724
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   725
text \<open>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   726
  Having \<^const>\<open>bit\<close> as definitional class operation
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   727
  takes into account that specific instances can be implemented
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   728
  differently wrt. code generation.
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   729
\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   730
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   731
lemma bits_div_by_0 [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   732
  \<open>a div 0 = 0\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   733
  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
   734
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   735
lemma bits_1_div_2 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   736
  \<open>1 div 2 = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   737
  using even_succ_div_2 [of 0] by simp
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_1_div_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   740
  \<open>1 div 2 ^ n = of_bool (n = 0)\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   741
  using div_exp_eq [of 1 1] by (cases n) simp_all
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 even_succ_div_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   744
  \<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
   745
proof (cases n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   746
  case 0
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   747
  with that show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   748
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   749
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   750
  case (Suc n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   751
  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
   752
  proof (induction n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   753
    case 0
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   754
    then show ?case
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   755
      by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   756
  next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   757
    case (Suc n)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   758
    then show ?case
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   759
      using div_exp_eq [of _ 1 \<open>Suc n\<close>, symmetric]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   760
      by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   761
  qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   762
  with Suc show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   763
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   764
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   765
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   766
lemma even_succ_mod_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   767
  \<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
   768
  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
   769
  apply simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   770
  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
   771
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   772
lemma bits_mod_by_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   773
  \<open>a mod 1 = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   774
  using div_mult_mod_eq [of a 1] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   775
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   776
lemma bits_mod_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   777
  \<open>0 mod a = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   778
  using div_mult_mod_eq [of 0 a] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   779
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   780
lemma bits_one_mod_two_eq_one [simp]:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   781
  \<open>1 mod 2 = 1\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   782
  by (simp add: mod2_eq_if)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
   783
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   784
lemma bit_0 [simp]:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   785
  \<open>bit a 0 \<longleftrightarrow> odd a\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   786
  by (simp add: bit_iff_odd)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   787
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   788
lemma bit_Suc:
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   789
  \<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   790
  using div_exp_eq [of a 1 n] by (simp add: bit_iff_odd)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   791
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   792
lemma bit_rec:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   793
  \<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
   794
  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
   795
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   796
lemma bit_0_eq [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   797
  \<open>bit 0 = bot\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   798
  by (simp add: fun_eq_iff bit_iff_odd)
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   799
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   800
context
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   801
  fixes a
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   802
  assumes stable: \<open>a div 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   803
begin
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   804
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
   805
lemma bits_stable_imp_add_self:
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   806
  \<open>a + a mod 2 = 0\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   807
proof -
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   808
  have \<open>a div 2 * 2 + a mod 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   809
    by (fact div_mult_mod_eq)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   810
  then have \<open>a * 2 + a mod 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   811
    by (simp add: stable)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   812
  then show ?thesis
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   813
    by (simp add: mult_2_right ac_simps)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   814
qed
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   815
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   816
lemma stable_imp_bit_iff_odd:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   817
  \<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
   818
  by (induction n) (simp_all add: stable bit_Suc)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   819
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   820
end
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   821
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   822
lemma bit_iff_idd_imp_stable:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   823
  \<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
   824
using that proof (induction a rule: bits_induct)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   825
  case (stable a)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   826
  then show ?case
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   827
    by simp
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   828
next
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   829
  case (rec a b)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   830
  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
   831
    by (simp add: rec.hyps bit_Suc)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   832
  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
   833
    by simp
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   834
  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
   835
    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
   836
  then have \<open>a div 2 = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   837
    by (rule rec.IH)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   838
  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
   839
    by (simp add: ac_simps)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   840
  also have \<open>\<dots> = a\<close>
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   841
    using mult_div_mod_eq [of 2 a]
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   842
    by (simp add: of_bool_odd_eq_mod_2)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   843
  finally show ?case
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   844
    using \<open>a div 2 = a\<close> by (simp add: hyp)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   845
qed
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   846
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   847
lemma exp_eq_0_imp_not_bit:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   848
  \<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   849
  using that by (simp add: bit_iff_odd)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   850
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   851
lemma bit_eqI:
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   852
  \<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
   853
proof -
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   854
  have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   855
  proof (cases \<open>2 ^ n = 0\<close>)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   856
    case True
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   857
    then show ?thesis
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   858
      by (simp add: exp_eq_0_imp_not_bit)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   859
  next
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   860
    case False
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   861
    then show ?thesis
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   862
      by (rule that)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   863
  qed
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   864
  then show ?thesis proof (induction a arbitrary: b rule: bits_induct)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   865
    case (stable a)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   866
    from stable(2) [of 0] have **: \<open>even b \<longleftrightarrow> even a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   867
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   868
    have \<open>b div 2 = b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   869
    proof (rule bit_iff_idd_imp_stable)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   870
      fix n
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   871
      from stable have *: \<open>bit b n \<longleftrightarrow> bit a n\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   872
        by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   873
      also have \<open>bit a n \<longleftrightarrow> odd a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   874
        using stable by (simp add: stable_imp_bit_iff_odd)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   875
      finally show \<open>bit b n \<longleftrightarrow> odd b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   876
        by (simp add: **)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   877
    qed
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   878
    from ** have \<open>a mod 2 = b mod 2\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   879
      by (simp add: mod2_eq_if)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   880
    then have \<open>a mod 2 + (a + b) = b mod 2 + (a + b)\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   881
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   882
    then have \<open>a + a mod 2 + b = b + b mod 2 + a\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   883
      by (simp add: ac_simps)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   884
    with \<open>a div 2 = a\<close> \<open>b div 2 = b\<close> show ?case
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   885
      by (simp add: bits_stable_imp_add_self)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   886
  next
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   887
    case (rec a p)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   888
    from rec.prems [of 0] have [simp]: \<open>p = odd b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   889
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   890
    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
   891
      using rec.prems [of \<open>Suc n\<close>] by (simp add: bit_Suc)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   892
    then have \<open>a = b div 2\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   893
      by (rule rec.IH)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   894
    then have \<open>2 * a = 2 * (b div 2)\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   895
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   896
    then have \<open>b mod 2 + 2 * a = b mod 2 + 2 * (b div 2)\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   897
      by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   898
    also have \<open>\<dots> = b\<close>
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   899
      by (fact mod_mult_div_eq)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   900
    finally show ?case
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   901
      by (auto simp add: mod2_eq_if)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   902
  qed
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   903
qed
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   904
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   905
lemma bit_eq_iff:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   906
  \<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
   907
  by (auto intro: bit_eqI)
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
   908
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
   909
named_theorems bit_simps \<open>Simplification rules for \<^const>\<open>bit\<close>\<close>
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
   910
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
   911
lemma bit_exp_iff [bit_simps]:
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   912
  \<open>bit (2 ^ m) n \<longleftrightarrow> 2 ^ m \<noteq> 0 \<and> m = n\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   913
  by (auto simp add: bit_iff_odd exp_div_exp_eq)
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   914
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
   915
lemma bit_1_iff [bit_simps]:
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   916
  \<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   917
  using bit_exp_iff [of 0 n] by simp
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   918
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
   919
lemma bit_2_iff [bit_simps]:
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   920
  \<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   921
  using bit_exp_iff [of 1 n] by auto
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   922
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   923
lemma even_bit_succ_iff:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   924
  \<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   925
  using that by (cases \<open>n = 0\<close>) (simp_all add: bit_iff_odd)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   926
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   927
lemma odd_bit_iff_bit_pred:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   928
  \<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
   929
proof -
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   930
  from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> ..
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   931
  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
   932
    using even_bit_succ_iff by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   933
  ultimately show ?thesis by (simp add: ac_simps)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   934
qed
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   935
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
   936
lemma bit_double_iff [bit_simps]:
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   937
  \<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
   938
  using even_mult_exp_div_exp_iff [of a 1 n]
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   939
  by (cases n, auto simp add: bit_iff_odd ac_simps)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   940
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   941
lemma bit_eq_rec:
71441
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   942
  \<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
   943
proof
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   944
  assume ?P
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   945
  then show ?Q
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   946
    by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   947
next
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   948
  assume ?Q
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   949
  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
   950
    by simp_all
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   951
  show ?P
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   952
  proof (rule bit_eqI)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   953
    fix n
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   954
    show \<open>bit a n \<longleftrightarrow> bit b n\<close>
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   955
    proof (cases n)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   956
      case 0
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   957
      with \<open>even a \<longleftrightarrow> even b\<close> show ?thesis
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   958
        by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   959
    next
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   960
      case (Suc n)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   961
      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
   962
        by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   963
      ultimately show ?thesis
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   964
        by (simp add: bit_Suc)
71441
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   965
    qed
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   966
  qed
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   967
qed
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   968
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
   969
lemma bit_mod_2_iff [simp]:
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
   970
  \<open>bit (a mod 2) n \<longleftrightarrow> n = 0 \<and> odd a\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   971
  by (cases a rule: parity_cases) (simp_all add: bit_iff_odd)
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
   972
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   973
lemma bit_mask_iff:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   974
  \<open>bit (2 ^ m - 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   975
  by (simp add: bit_iff_odd even_mask_div_iff not_le)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   976
71757
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   977
lemma bit_Numeral1_iff [simp]:
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   978
  \<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close>
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   979
  by (simp add: bit_rec)
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   980
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   981
lemma exp_add_not_zero_imp:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   982
  \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> if \<open>2 ^ (m + n) \<noteq> 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   983
proof -
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   984
  have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   985
  proof (rule notI)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   986
    assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   987
    then have \<open>2 ^ (m + n) = 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   988
      by (rule disjE) (simp_all add: power_add)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   989
    with that show False ..
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   990
  qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   991
  then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   992
    by simp_all
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   993
qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   994
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   995
lemma bit_disjunctive_add_iff:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   996
  \<open>bit (a + b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   997
  if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   998
proof (cases \<open>2 ^ n = 0\<close>)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   999
  case True
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1000
  then show ?thesis
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1001
    by (simp add: exp_eq_0_imp_not_bit)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1002
next
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1003
  case False
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1004
  with that show ?thesis proof (induction n arbitrary: a b)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1005
    case 0
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1006
    from "0.prems"(1) [of 0] show ?case
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1007
      by auto
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1008
  next
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1009
    case (Suc n)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1010
    from Suc.prems(1) [of 0] have even: \<open>even a \<or> even b\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1011
      by auto
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1012
    have bit: \<open>\<not> bit (a div 2) n \<or> \<not> bit (b div 2) n\<close> for n
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1013
      using Suc.prems(1) [of \<open>Suc n\<close>] by (simp add: bit_Suc)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1014
    from Suc.prems(2) have \<open>2 * 2 ^ n \<noteq> 0\<close> \<open>2 ^ n \<noteq> 0\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1015
      by (auto simp add: mult_2)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1016
    have \<open>a + b = (a div 2 * 2 + a mod 2) + (b div 2 * 2 + b mod 2)\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1017
      using div_mult_mod_eq [of a 2] div_mult_mod_eq [of b 2] by simp
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1018
    also have \<open>\<dots> = of_bool (odd a \<or> odd b) + 2 * (a div 2 + b div 2)\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1019
      using even by (auto simp add: algebra_simps mod2_eq_if)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1020
    finally have \<open>bit ((a + b) div 2) n \<longleftrightarrow> bit (a div 2 + b div 2) n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1021
      using \<open>2 * 2 ^ n \<noteq> 0\<close> by simp (simp flip: bit_Suc add: bit_double_iff)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1022
    also have \<open>\<dots> \<longleftrightarrow> bit (a div 2) n \<or> bit (b div 2) n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1023
      using bit \<open>2 ^ n \<noteq> 0\<close> by (rule Suc.IH)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1024
    finally show ?case
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1025
      by (simp add: bit_Suc)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1026
  qed
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1027
qed
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1028
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1029
lemma
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1030
  exp_add_not_zero_imp_left: \<open>2 ^ m \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1031
  and exp_add_not_zero_imp_right: \<open>2 ^ n \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1032
  if \<open>2 ^ (m + n) \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1033
proof -
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1034
  have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1035
  proof (rule notI)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1036
    assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1037
    then have \<open>2 ^ (m + n) = 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1038
      by (rule disjE) (simp_all add: power_add)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1039
    with that show False ..
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1040
  qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1041
  then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1042
    by simp_all
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1043
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1044
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1045
lemma exp_not_zero_imp_exp_diff_not_zero:
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1046
  \<open>2 ^ (n - m) \<noteq> 0\<close> if \<open>2 ^ n \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1047
proof (cases \<open>m \<le> n\<close>)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1048
  case True
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1049
  moreover define q where \<open>q = n - m\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1050
  ultimately have \<open>n = m + q\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1051
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1052
  with that show ?thesis
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1053
    by (simp add: exp_add_not_zero_imp_right)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1054
next
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1055
  case False
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1056
  with that show ?thesis
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1057
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1058
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1059
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1060
end
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1061
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1062
lemma nat_bit_induct [case_names zero even odd]:
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1063
  "P n" if zero: "P 0"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1064
    and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1065
    and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1066
proof (induction n rule: less_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1067
  case (less n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1068
  show "P n"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1069
  proof (cases "n = 0")
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1070
    case True with zero show ?thesis by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1071
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1072
    case False
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1073
    with less have hyp: "P (n div 2)" by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1074
    show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1075
    proof (cases "even n")
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1076
      case True
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1077
      then have "n \<noteq> 1"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1078
        by auto
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1079
      with \<open>n \<noteq> 0\<close> have "n div 2 > 0"
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
      with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1082
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1083
    next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1084
      case False
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1085
      with hyp odd [of "n div 2"] show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1086
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1087
    qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1088
  qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1089
qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1090
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1091
instantiation nat :: semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1092
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1093
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1094
definition bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> bool\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1095
  where \<open>bit_nat m n \<longleftrightarrow> odd (m div 2 ^ n)\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1096
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1097
instance
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1098
proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1099
  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
  1100
    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
  1101
    for P and n :: nat
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1102
  proof (induction n rule: nat_bit_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1103
    case zero
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1104
    from stable [of 0] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1105
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1106
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1107
    case (even n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1108
    with rec [of n False] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1109
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1110
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1111
    case (odd n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1112
    with rec [of n True] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1113
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1114
  qed
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1115
  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
  1116
    for q m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1117
    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
  1118
    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
  1119
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1120
  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
  1121
    for q m n :: nat
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: 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
  1124
    apply (simp add: mult.commute)
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::nat)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::nat) \<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 = nat, of m n] by simp
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1129
  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
  1130
    for m n q r :: nat
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 (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
  1133
    done
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1134
qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff bit_nat_def)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1135
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1136
end
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1137
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1138
lemma int_bit_induct [case_names zero minus even odd]:
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1139
  "P k" if zero_int: "P 0"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1140
    and minus_int: "P (- 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1141
    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
  1142
    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
  1143
proof (cases "k \<ge> 0")
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1144
  case True
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1145
  define n where "n = nat k"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1146
  with True have "k = int n"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1147
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1148
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1149
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1150
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1151
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1152
      by (simp add: zero_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1153
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1154
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1155
    have "P (int n * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1156
      by (rule even_int) (use even in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1157
    with even show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1158
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1159
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1160
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1161
    have "P (1 + (int n * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1162
      by (rule odd_int) (use odd in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1163
    with odd show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1164
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1165
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1166
next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1167
  case False
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1168
  define n where "n = nat (- k - 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1169
  with False have "k = - int n - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1170
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1171
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1172
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1173
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1174
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1175
      by (simp add: minus_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1176
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1177
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1178
    have "P (1 + (- int (Suc n) * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1179
      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
  1180
    also have "\<dots> = - int (2 * n) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1181
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1182
    finally show ?case
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1183
      using even.prems by simp
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1184
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1185
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1186
    have "P (- int (Suc n) * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1187
      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
  1188
    also have "\<dots> = - int (Suc (2 * n)) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1189
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1190
    finally show ?case
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1191
      using odd.prems by simp
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1192
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1193
qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1194
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1195
context semiring_bits
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1196
begin
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1197
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1198
lemma bit_of_bool_iff [bit_simps]:
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1199
  \<open>bit (of_bool b) n \<longleftrightarrow> b \<and> n = 0\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1200
	by (simp add: bit_1_iff)
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1201
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1202
lemma even_of_nat_iff:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1203
  \<open>even (of_nat n) \<longleftrightarrow> even n\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1204
  by (induction n rule: nat_bit_induct) simp_all
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1205
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1206
lemma bit_of_nat_iff [bit_simps]:
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1207
  \<open>bit (of_nat m) n \<longleftrightarrow> (2::'a) ^ n \<noteq> 0 \<and> bit m n\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1208
proof (cases \<open>(2::'a) ^ n = 0\<close>)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1209
  case True
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1210
  then show ?thesis
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1211
    by (simp add: exp_eq_0_imp_not_bit)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1212
next
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1213
  case False
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1214
  then have \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1215
  proof (induction m arbitrary: n rule: nat_bit_induct)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1216
    case zero
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1217
    then show ?case
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1218
      by simp
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1219
  next
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1220
    case (even m)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1221
    then show ?case
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1222
      by (cases n)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1223
        (auto simp add: bit_double_iff Parity.bit_double_iff dest: mult_not_zero)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1224
  next
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1225
    case (odd m)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1226
    then show ?case
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1227
      by (cases n)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1228
         (auto simp add: bit_double_iff even_bit_succ_iff Parity.bit_Suc dest: mult_not_zero)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1229
  qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1230
  with False show ?thesis
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1231
    by simp
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1232
qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1233
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1234
end
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1235
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1236
instantiation int :: semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1237
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1238
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1239
definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1240
  where \<open>bit_int k n \<longleftrightarrow> odd (k div 2 ^ n)\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1241
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1242
instance
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1243
proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1244
  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
  1245
    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
  1246
    for P and k :: int
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1247
  proof (induction k rule: int_bit_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1248
    case zero
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1249
    from stable [of 0] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1250
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1251
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1252
    case minus
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1253
    from stable [of \<open>- 1\<close>] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1254
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1255
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1256
    case (even k)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1257
    with rec [of k False] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1258
      by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1259
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1260
    case (odd k)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1261
    with rec [of k True] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1262
      by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1263
  qed
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1264
  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
  1265
    for m n :: nat
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1266
  proof (cases \<open>m < n\<close>)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1267
    case True
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1268
    then have \<open>n = m + (n - m)\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1269
      by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1270
    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
  1271
      by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1272
    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
  1273
      by (simp add: power_add)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1274
    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
  1275
      by (simp add: zdiv_zmult2_eq)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1276
    finally show ?thesis using \<open>m < n\<close> by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1277
  next
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1278
    case False
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1279
    then show ?thesis
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1280
      by (simp add: power_diff)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1281
  qed
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1282
  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
  1283
    for m n :: nat and k :: int
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1284
    using mod_exp_eq [of \<open>nat k\<close> m n]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1285
    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
  1286
     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
  1287
    apply (simp only: flip: mult.left_commute [of \<open>2 ^ m\<close>])
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1288
    apply (subst zmod_zmult2_eq) apply simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1289
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1290
  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
  1291
    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
  1292
    using that
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1293
    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
  1294
    apply (simp add: ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1295
    done
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1296
  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
  1297
    for m n :: nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1298
    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
  1299
  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
  1300
    for m n :: nat and k l :: int
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1301
    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
  1302
    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
  1303
    done
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1304
qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le bit_int_def)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1305
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1306
end
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1307
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1308
class semiring_bit_shifts = semiring_bits +
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1309
  fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1310
  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
  1311
  fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1312
  assumes drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1313
  fixes take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1314
  assumes 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
  1315
begin
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1316
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1317
text \<open>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1318
  Logically, \<^const>\<open>push_bit\<close>,
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1319
  \<^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
  1320
  as separate operations makes proofs easier, otherwise proof automation
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1321
  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
  1322
  algebraic relationships between those operations.
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1323
  Having
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1324
  them as definitional class operations
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1325
  takes into account that specific instances of these can be implemented
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1326
  differently wrt. code generation.
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1327
\<close>
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1328
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1329
lemma bit_iff_odd_drop_bit:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1330
  \<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1331
  by (simp add: bit_iff_odd drop_bit_eq_div)
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1332
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1333
lemma even_drop_bit_iff_not_bit:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1334
  \<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1335
  by (simp add: bit_iff_odd_drop_bit)
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1336
71423
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1337
lemma div_push_bit_of_1_eq_drop_bit:
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1338
  \<open>a div push_bit n 1 = drop_bit n a\<close>
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1339
  by (simp add: push_bit_eq_mult drop_bit_eq_div)
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1340
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1341
lemma bits_ident:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1342
  "push_bit n (drop_bit n a) + take_bit n a = a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1343
  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
  1344
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1345
lemma push_bit_push_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1346
  "push_bit m (push_bit n a) = push_bit (m + n) a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1347
  by (simp add: push_bit_eq_mult power_add ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1348
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1349
lemma push_bit_0_id [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1350
  "push_bit 0 = id"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1351
  by (simp add: fun_eq_iff push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1352
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1353
lemma push_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1354
  "push_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1355
  by (simp add: push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1356
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1357
lemma push_bit_of_1:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1358
  "push_bit n 1 = 2 ^ n"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1359
  by (simp add: push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1360
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1361
lemma push_bit_Suc [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1362
  "push_bit (Suc n) a = push_bit n (a * 2)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1363
  by (simp add: push_bit_eq_mult ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1364
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1365
lemma push_bit_double:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1366
  "push_bit n (a * 2) = push_bit n a * 2"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1367
  by (simp add: push_bit_eq_mult ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1368
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1369
lemma push_bit_add:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1370
  "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
  1371
  by (simp add: push_bit_eq_mult algebra_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1372
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1373
lemma push_bit_numeral [simp]:
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1374
  \<open>push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))\<close>
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1375
  by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0)
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1376
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1377
lemma take_bit_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1378
  "take_bit 0 a = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1379
  by (simp add: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1380
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1381
lemma take_bit_Suc:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
  1382
  \<open>take_bit (Suc n) a = take_bit n (a div 2) * 2 + a mod 2\<close>
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1383
proof -
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1384
  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
  1385
    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
  1386
      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
  1387
    by (auto simp add: take_bit_eq_mod ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1388
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1389
    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
  1390
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1391
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1392
lemma take_bit_rec:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
  1393
  \<open>take_bit n a = (if n = 0 then 0 else take_bit (n - 1) (a div 2) * 2 + a mod 2)\<close>
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1394
  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
  1395
71759
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1396
lemma take_bit_Suc_0 [simp]:
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1397
  \<open>take_bit (Suc 0) a = a mod 2\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1398
  by (simp add: take_bit_eq_mod)
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1399
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1400
lemma take_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1401
  "take_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1402
  by (simp add: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1403
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1404
lemma take_bit_of_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1405
  "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
  1406
  by (cases n) (simp_all add: take_bit_Suc)
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1407
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1408
lemma drop_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1409
  "drop_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1410
  by (simp add: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1411
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1412
lemma drop_bit_of_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1413
  "drop_bit n 1 = of_bool (n = 0)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1414
  by (simp add: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1415
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1416
lemma drop_bit_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1417
  "drop_bit 0 = id"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1418
  by (simp add: fun_eq_iff drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1419
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1420
lemma drop_bit_Suc:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1421
  "drop_bit (Suc n) a = drop_bit n (a div 2)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1422
  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
  1423
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1424
lemma drop_bit_rec:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1425
  "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
  1426
  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
  1427
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1428
lemma drop_bit_half:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1429
  "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
  1430
  by (induction n arbitrary: a) (simp_all add: drop_bit_Suc)
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1431
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1432
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
  1433
  "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
  1434
  by (cases n) simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1435
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1436
lemma even_take_bit_eq [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1437
  \<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
  1438
  by (simp add: take_bit_rec [of n a])
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1439
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1440
lemma take_bit_take_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1441
  "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
  1442
  by (simp add: take_bit_eq_mod mod_exp_eq ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1443
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1444
lemma drop_bit_drop_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1445
  "drop_bit m (drop_bit n a) = drop_bit (m + n) a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1446
  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
  1447
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1448
lemma push_bit_take_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1449
  "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
  1450
  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
  1451
  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
  1452
  done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1453
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1454
lemma take_bit_push_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1455
  "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
  1456
proof (cases "m \<le> n")
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1457
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1458
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1459
    apply (simp add:)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1460
    apply (simp_all add: push_bit_eq_mult take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1461
    apply (auto dest!: le_Suc_ex simp add: power_add ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1462
    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
  1463
    apply (simp add: ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1464
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1465
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1466
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1467
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1468
    using push_bit_take_bit [of n "m - n" a]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1469
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1470
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1471
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1472
lemma take_bit_drop_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1473
  "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
  1474
  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
  1475
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1476
lemma drop_bit_take_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1477
  "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
  1478
proof (cases "m \<le> n")
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1479
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1480
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1481
    using take_bit_drop_bit [of "n - m" m a] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1482
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1483
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1484
  then obtain q where \<open>m = n + q\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1485
    by (auto simp add: not_le dest: less_imp_Suc_add)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1486
  then have \<open>drop_bit m (take_bit n a) = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1487
    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
  1488
    by (simp add: take_bit_eq_mod drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1489
  with False show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1490
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1491
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1492
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1493
lemma even_push_bit_iff [simp]:
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1494
  \<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
  1495
  by (simp add: push_bit_eq_mult) auto
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1496
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1497
lemma bit_push_bit_iff [bit_simps]:
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1498
  \<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> 2 ^ n \<noteq> 0 \<and> bit a (n - m)\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1499
  by (auto simp add: bit_iff_odd push_bit_eq_mult even_mult_exp_div_exp_iff)
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1500
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1501
lemma bit_drop_bit_eq [bit_simps]:
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1502
  \<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1503
  by (simp add: bit_iff_odd fun_eq_iff ac_simps flip: drop_bit_eq_div)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1504
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1505
lemma bit_take_bit_iff [bit_simps]:
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1506
  \<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close>
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1507
  by (simp add: bit_iff_odd drop_bit_take_bit not_le flip: drop_bit_eq_div)
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1508
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1509
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
  1510
  \<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
  1511
  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
  1512
  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
  1513
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1514
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
  1515
  \<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
  1516
    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
  1517
proof (rule bit_eqI)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1518
  fix m
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1519
  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
  1520
  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
  1521
    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
  1522
qed
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1523
71958
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1524
lemma exp_dvdE:
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1525
  assumes \<open>2 ^ n dvd a\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1526
  obtains b where \<open>a = push_bit n b\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1527
proof -
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1528
  from assms obtain b where \<open>a = 2 ^ n * b\<close> ..
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1529
  then have \<open>a = push_bit n b\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1530
    by (simp add: push_bit_eq_mult ac_simps)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1531
  with that show thesis .
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1532
qed
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1533
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1534
lemma take_bit_eq_0_iff:
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1535
  \<open>take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1536
proof
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1537
  assume ?P
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1538
  then show ?Q
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1539
	  by (simp add: take_bit_eq_mod mod_0_imp_dvd)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1540
next
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1541
  assume ?Q
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1542
  then obtain b where \<open>a = push_bit n b\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1543
    by (rule exp_dvdE)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1544
  then show ?P
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1545
    by (simp add: take_bit_push_bit)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1546
qed
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1547
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1548
lemma take_bit_tightened:
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1549
  \<open>take_bit m a = take_bit m b\<close> if \<open>take_bit n a = take_bit n b\<close> and \<open>m \<le> n\<close> 
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1550
proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1551
  from that have \<open>take_bit m (take_bit n a) = take_bit m (take_bit n b)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1552
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1553
  then have \<open>take_bit (min m n) a = take_bit (min m n) b\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1554
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1555
  with that show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1556
    by (simp add: min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1557
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1558
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1559
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1560
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1561
instantiation nat :: semiring_bit_shifts
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1562
begin
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1563
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1564
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1565
  where \<open>push_bit_nat n m = m * 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1566
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1567
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1568
  where \<open>drop_bit_nat n m = m div 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1569
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1570
definition take_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1571
  where \<open>take_bit_nat n m = m mod 2 ^ n\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1572
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1573
instance
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1574
  by standard (simp_all add: push_bit_nat_def drop_bit_nat_def take_bit_nat_def)
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1575
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1576
end
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1577
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1578
context semiring_bit_shifts
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1579
begin
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1580
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1581
lemma push_bit_of_nat:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1582
  \<open>push_bit n (of_nat m) = of_nat (push_bit n m)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1583
  by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1584
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1585
lemma of_nat_push_bit:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1586
  \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1587
  by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1588
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1589
lemma take_bit_of_nat:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1590
  \<open>take_bit n (of_nat m) = of_nat (take_bit n m)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1591
  by (rule bit_eqI) (simp add: bit_take_bit_iff Parity.bit_take_bit_iff bit_of_nat_iff)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1592
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1593
lemma of_nat_take_bit:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1594
  \<open>of_nat (take_bit n m) = take_bit n (of_nat m)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1595
  by (rule bit_eqI) (simp add: bit_take_bit_iff Parity.bit_take_bit_iff bit_of_nat_iff)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1596
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1597
end
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1598
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1599
instantiation int :: semiring_bit_shifts
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1600
begin
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1601
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1602
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1603
  where \<open>push_bit_int n k = k * 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1604
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1605
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1606
  where \<open>drop_bit_int n k = k div 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1607
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1608
definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1609
  where \<open>take_bit_int n k = k mod 2 ^ n\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1610
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1611
instance
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1612
  by standard (simp_all add: push_bit_int_def drop_bit_int_def take_bit_int_def)
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1613
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1614
end
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1615
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1616
lemma bit_push_bit_iff_nat:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1617
  \<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
  1618
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1619
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1620
lemma bit_push_bit_iff_int:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1621
  \<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
  1622
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1623
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1624
lemma take_bit_nat_less_exp [simp]:
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1625
  \<open>take_bit n m < 2 ^ n\<close> for n m ::nat 
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1626
  by (simp add: take_bit_eq_mod)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1627
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1628
lemma take_bit_nonnegative [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1629
  \<open>take_bit n k \<ge> 0\<close> for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1630
  by (simp add: take_bit_eq_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1631
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1632
lemma not_take_bit_negative [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1633
  \<open>\<not> take_bit n k < 0\<close> for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1634
  by (simp add: not_less)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1635
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1636
lemma take_bit_int_less_exp [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1637
  \<open>take_bit n k < 2 ^ n\<close> for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1638
  by (simp add: take_bit_eq_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1639
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1640
lemma take_bit_nat_eq_self_iff:
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1641
  \<open>take_bit n m = m \<longleftrightarrow> m < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1642
  for n m :: nat
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1643
proof
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1644
  assume ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1645
  moreover note take_bit_nat_less_exp [of n m]
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1646
  ultimately show ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1647
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1648
next
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1649
  assume ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1650
  then show ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1651
    by (simp add: take_bit_eq_mod)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1652
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1653
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1654
lemma take_bit_nat_eq_self:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1655
  \<open>take_bit n m = m\<close> if \<open>m < 2 ^ n\<close> for m n :: nat
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1656
  using that by (simp add: take_bit_nat_eq_self_iff)
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1657
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1658
lemma take_bit_int_eq_self_iff:
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1659
  \<open>take_bit n k = k \<longleftrightarrow> 0 \<le> k \<and> k < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1660
  for k :: int
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1661
proof
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1662
  assume ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1663
  moreover note take_bit_int_less_exp [of n k] take_bit_nonnegative [of n k]
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1664
  ultimately show ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1665
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1666
next
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1667
  assume ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1668
  then show ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1669
    by (simp add: take_bit_eq_mod)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1670
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1671
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1672
lemma take_bit_int_eq_self:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1673
  \<open>take_bit n k = k\<close> if \<open>0 \<le> k\<close> \<open>k < 2 ^ n\<close> for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1674
  using that by (simp add: take_bit_int_eq_self_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1675
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1676
lemma take_bit_nat_less_eq_self [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1677
  \<open>take_bit n m \<le> m\<close> for n m :: nat
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1678
  by (simp add: take_bit_eq_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1679
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1680
lemma take_bit_nat_less_self_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1681
  \<open>take_bit n m < m \<longleftrightarrow> 2 ^ n \<le> m\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1682
  for m n :: nat
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1683
proof
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1684
  assume ?P
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1685
  then have \<open>take_bit n m \<noteq> m\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1686
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1687
  then show \<open>?Q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1688
    by (simp add: take_bit_nat_eq_self_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1689
next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1690
  have \<open>take_bit n m < 2 ^ n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1691
    by (fact take_bit_nat_less_exp)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1692
  also assume ?Q
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1693
  finally show ?P .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1694
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1695
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1696
class unique_euclidean_semiring_with_bit_shifts =
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1697
  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
  1698
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1699
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1700
lemma take_bit_of_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1701
  \<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
  1702
  by (simp add: take_bit_eq_mod exp_mod_exp)
67960
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
  1703
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1704
lemma take_bit_of_2 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1705
  \<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1706
  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
  1707
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1708
lemma take_bit_of_mask:
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1709
  \<open>take_bit m (2 ^ n - 1) = 2 ^ min m n - 1\<close>
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1710
  by (simp add: take_bit_eq_mod mask_mod_exp)
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1711
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1712
lemma push_bit_eq_0_iff [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1713
  "push_bit n a = 0 \<longleftrightarrow> a = 0"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1714
  by (simp add: push_bit_eq_mult)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1715
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1716
lemma take_bit_add:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1717
  "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
  1718
  by (simp add: take_bit_eq_mod mod_simps)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1719
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1720
lemma take_bit_of_1_eq_0_iff [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1721
  "take_bit n 1 = 0 \<longleftrightarrow> n = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1722
  by (simp add: take_bit_eq_mod)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1723
72026
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1724
lemma take_bit_Suc_1 [simp]:
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1725
  \<open>take_bit (Suc n) 1 = 1\<close>
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1726
  by (simp add: take_bit_Suc)
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1727
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1728
lemma take_bit_Suc_bit0 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1729
  \<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
  1730
  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
  1731
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1732
lemma take_bit_Suc_bit1 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1733
  \<open>take_bit (Suc n) (numeral (Num.Bit1 k)) = take_bit n (numeral k) * 2 + 1\<close>
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
  1734
  by (simp add: take_bit_Suc numeral_Bit1_div_2 mod_2_eq_odd)
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1735
72026
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1736
lemma take_bit_numeral_1 [simp]:
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1737
  \<open>take_bit (numeral l) 1 = 1\<close>
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1738
  by (simp add: take_bit_rec [of \<open>numeral l\<close> 1])
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1739
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1740
lemma take_bit_numeral_bit0 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1741
  \<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
  1742
  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
  1743
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1744
lemma take_bit_numeral_bit1 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1745
  \<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close>
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
  1746
  by (simp add: take_bit_rec numeral_Bit1_div_2 mod_2_eq_odd)
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1747
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1748
lemma drop_bit_Suc_bit0 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1749
  \<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
  1750
  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
  1751
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1752
lemma drop_bit_Suc_bit1 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1753
  \<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
  1754
  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
  1755
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1756
lemma drop_bit_numeral_bit0 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1757
  \<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
  1758
  by (simp add: drop_bit_rec numeral_Bit0_div_2)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1759
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1760
lemma drop_bit_numeral_bit1 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1761
  \<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
  1762
  by (simp add: drop_bit_rec numeral_Bit1_div_2)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1763
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1764
lemma drop_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1765
  "drop_bit n (of_nat m) = of_nat (drop_bit n m)"
68389
1c84a8c513af proper white space;
wenzelm
parents: 68157
diff changeset
  1766
  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
  1767
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1768
lemma bit_of_nat_iff_bit [bit_simps]:
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1769
  \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1770
proof -
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1771
  have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1772
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1773
  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
  1774
    by (simp add: of_nat_div)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1775
  finally show ?thesis
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1776
    by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1777
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1778
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1779
lemma of_nat_drop_bit:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1780
  \<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1781
  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
  1782
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1783
lemma bit_push_bit_iff_of_nat_iff [bit_simps]:
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1784
  \<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
  1785
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1786
58770
ae5e9b4f8daf downshift of theory Parity in the hierarchy
haftmann
parents: 58769
diff changeset
  1787
end
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1788
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1789
instance nat :: unique_euclidean_semiring_with_bit_shifts ..
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1790
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1791
instance int :: unique_euclidean_semiring_with_bit_shifts ..
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1792
72512
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1793
lemma bit_not_int_iff':
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1794
  \<open>bit (- k - 1) n \<longleftrightarrow> \<not> bit k n\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1795
  for k :: int
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1796
proof (induction n arbitrary: k)
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1797
  case 0
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1798
  show ?case
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1799
    by simp
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1800
next
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1801
  case (Suc n)
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1802
  have \<open>- k - 1 = - (k + 2) + 1\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1803
    by simp
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1804
  also have \<open>(- (k + 2) + 1) div 2 = - (k div 2) - 1\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1805
  proof (cases \<open>even k\<close>)
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1806
    case True
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1807
    then have \<open>- k div 2 = - (k div 2)\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1808
      by rule (simp flip: mult_minus_right)
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1809
    with True show ?thesis
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1810
      by simp
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1811
  next
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1812
    case False
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1813
    have \<open>4 = 2 * (2::int)\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1814
      by simp
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1815
    also have \<open>2 * 2 div 2 = (2::int)\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1816
      by (simp only: nonzero_mult_div_cancel_left)
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1817
    finally have *: \<open>4 div 2 = (2::int)\<close> .
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1818
    from False obtain l where k: \<open>k = 2 * l + 1\<close> ..
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1819
    then have \<open>- k - 2 = 2 * - (l + 2) + 1\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1820
      by simp
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1821
    then have \<open>(- k - 2) div 2 + 1 = - (k div 2) - 1\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1822
      by (simp flip: mult_minus_right add: *) (simp add: k)
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1823
    with False show ?thesis
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1824
      by simp
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1825
  qed
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1826
  finally have \<open>(- k - 1) div 2 = - (k div 2) - 1\<close> .
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1827
  with Suc show ?case
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1828
    by (simp add: bit_Suc)
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1829
qed
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1830
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1831
lemma bit_minus_int_iff [bit_simps]:
72512
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1832
  \<open>bit (- k) n \<longleftrightarrow> \<not> bit (k - 1) n\<close>
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1833
  for k :: int
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1834
  using bit_not_int_iff' [of \<open>k - 1\<close>] by simp
83b5911c0164 more lemmas
haftmann
parents: 72281
diff changeset
  1835
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1836
lemma bit_nat_iff [bit_simps]:
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1837
  \<open>bit (nat k) n \<longleftrightarrow> k \<ge> 0 \<and> bit k n\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1838
proof (cases \<open>k \<ge> 0\<close>)
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1839
  case True
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1840
  moreover define m where \<open>m = nat k\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1841
  ultimately have \<open>k = int m\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1842
    by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1843
  then show ?thesis
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72512
diff changeset
  1844
    by (simp add: bit_simps)
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1845
next
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1846
  case False
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1847
  then show ?thesis
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1848
    by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1849
qed
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1850
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1851
lemma push_bit_nat_eq:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1852
  \<open>push_bit n (nat k) = nat (push_bit n k)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1853
  by (cases \<open>k \<ge> 0\<close>) (simp_all add: push_bit_eq_mult nat_mult_distrib not_le mult_nonneg_nonpos2)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1854
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1855
lemma drop_bit_nat_eq:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1856
  \<open>drop_bit n (nat k) = nat (drop_bit n k)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1857
  apply (cases \<open>k \<ge> 0\<close>)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1858
   apply (simp_all add: drop_bit_eq_div nat_div_distrib nat_power_eq not_le)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1859
  apply (simp add: divide_int_def)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1860
  done
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1861
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1862
lemma take_bit_nat_eq:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1863
  \<open>take_bit n (nat k) = nat (take_bit n k)\<close> if \<open>k \<ge> 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1864
  using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1865
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1866
lemma nat_take_bit_eq:
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1867
  \<open>nat (take_bit n k) = take_bit n (nat k)\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1868
  if \<open>k \<ge> 0\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1869
  using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1870
71802
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1871
lemma not_exp_less_eq_0_int [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1872
  \<open>\<not> 2 ^ n \<le> (0::int)\<close>
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1873
  by (simp add: power_le_zero_eq)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1874
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1875
lemma half_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1876
  \<open>k div 2 \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1877
proof (cases \<open>k \<ge> 0\<close>)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1878
  case True
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1879
  then show ?thesis
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1880
    by (auto simp add: divide_int_def sgn_1_pos)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1881
next
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1882
  case False
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1883
  then show ?thesis
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1884
    apply (auto simp add: divide_int_def not_le elim!: evenE)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1885
    apply (simp only: minus_mult_right)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1886
    apply (subst nat_mult_distrib)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1887
     apply simp_all
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1888
    done
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1889
qed
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1890
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1891
lemma half_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1892
  \<open>k div 2 < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1893
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1894
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1895
lemma push_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1896
  "push_bit n (Suc 0) = 2 ^ n"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1897
  using push_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1898
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1899
lemma take_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1900
  "take_bit n (Suc 0) = of_bool (0 < n)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1901
  using take_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1902
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1903
lemma drop_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1904
  "drop_bit n (Suc 0) = of_bool (n = 0)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1905
  using drop_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1906
70911
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1907
lemma push_bit_minus_one:
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1908
  "push_bit n (- 1 :: int) = - (2 ^ n)"
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1909
  by (simp add: push_bit_eq_mult)
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1910
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1911
lemma minus_1_div_exp_eq_int:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1912
  \<open>- 1 div (2 :: int) ^ n = - 1\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1913
  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
  1914
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1915
lemma drop_bit_minus_one [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1916
  \<open>drop_bit n (- 1 :: int) = - 1\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1917
  by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1918
72023
08348e364739 more thms
haftmann
parents: 71991
diff changeset
  1919
lemma take_bit_Suc_from_most:
08348e364739 more thms
haftmann
parents: 71991
diff changeset
  1920
  \<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close> for k :: int
08348e364739 more thms
haftmann
parents: 71991
diff changeset
  1921
  by (simp only: take_bit_eq_mod power_Suc2) (simp_all add: bit_iff_odd odd_iff_mod_2_eq_one zmod_zmult2_eq)
08348e364739 more thms
haftmann
parents: 71991
diff changeset
  1922
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1923
lemma take_bit_minus:
71966
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1924
  \<open>take_bit n (- take_bit n k) = take_bit n (- k)\<close>
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1925
    for k :: int
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1926
  by (simp add: take_bit_eq_mod mod_minus_eq)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1927
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1928
lemma take_bit_diff:
71966
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1929
  \<open>take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)\<close>
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1930
    for k l :: int
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1931
  by (simp add: take_bit_eq_mod mod_diff_eq)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1932
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1933
lemma bit_imp_take_bit_positive:
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1934
  \<open>0 < take_bit m k\<close> if \<open>n < m\<close> and \<open>bit k n\<close> for k :: int
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1935
proof (rule ccontr)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1936
  assume \<open>\<not> 0 < take_bit m k\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1937
  then have \<open>take_bit m k = 0\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1938
    by (auto simp add: not_less intro: order_antisym)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1939
  then have \<open>bit (take_bit m k) n = bit 0 n\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1940
    by simp
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1941
  with that show False
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1942
    by (simp add: bit_take_bit_iff)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1943
qed
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1944
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1945
lemma take_bit_mult:
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1946
  \<open>take_bit n (take_bit n k * take_bit n l) = take_bit n (k * l)\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1947
  for k l :: int
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1948
  by (simp add: take_bit_eq_mod mod_mult_eq)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1949
71966
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1950
lemma (in ring_1) of_nat_nat_take_bit_eq [simp]:
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1951
  \<open>of_nat (nat (take_bit n k)) = of_int (take_bit n k)\<close>
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1952
  by simp
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1953
71759
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1954
lemma take_bit_minus_small_eq:
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1955
  \<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
  1956
proof -
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1957
  define m where \<open>m = nat k\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1958
  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
  1959
    by simp_all
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1960
  have \<open>(2 ^ n - m) mod 2 ^ n = 2 ^ n - m\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1961
    using \<open>0 < m\<close> by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1962
  then have \<open>int ((2 ^ n - m) mod 2 ^ n) = int (2 ^ n - m)\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1963
    by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1964
  then have \<open>(2 ^ n - int m) mod 2 ^ n = 2 ^ n - int m\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1965
    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
  1966
  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
  1967
    by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1968
  then show ?thesis
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1969
    by (simp add: take_bit_eq_mod)
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1970
qed
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1971
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1972
lemma drop_bit_push_bit_int:
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1973
  \<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
  1974
  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
  1975
    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
  1976
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1977
lemma push_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1978
  \<open>push_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1979
  by (simp add: push_bit_eq_mult zero_le_mult_iff)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1980
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1981
lemma push_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1982
  \<open>push_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1983
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1984
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1985
lemma drop_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1986
  \<open>drop_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1987
  by (induction n) (simp_all add: drop_bit_Suc drop_bit_half)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1988
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1989
lemma drop_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1990
  \<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1991
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1992
71853
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
  1993
code_identifier
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
  1994
  code_module Parity \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
  1995
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1996
end