src/HOL/Parity.thy
author Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
Mon, 12 Oct 2020 18:59:44 +0200
changeset 72458 b44e894796d5
parent 72281 beeadb35e357
child 72512 83b5911c0164
permissions -rw-r--r--
add reconstruction for the SMT solver veriT * * * Improved veriT reconstruction
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
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   909
lemma bit_exp_iff:
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   910
  \<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
   911
  by (auto simp add: bit_iff_odd exp_div_exp_eq)
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
   912
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   913
lemma bit_1_iff:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   914
  \<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   915
  using bit_exp_iff [of 0 n] by simp
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   916
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   917
lemma bit_2_iff:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   918
  \<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   919
  using bit_exp_iff [of 1 n] by auto
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
   920
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   921
lemma even_bit_succ_iff:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   922
  \<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
   923
  using that by (cases \<open>n = 0\<close>) (simp_all add: bit_iff_odd)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   924
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   925
lemma odd_bit_iff_bit_pred:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   926
  \<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
   927
proof -
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   928
  from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> ..
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   929
  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
   930
    using even_bit_succ_iff by simp
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   931
  ultimately show ?thesis by (simp add: ac_simps)
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   932
qed
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   933
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   934
lemma bit_double_iff:
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   935
  \<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
   936
  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
   937
  by (cases n, auto simp add: bit_iff_odd ac_simps)
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   938
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   939
lemma bit_eq_rec:
71441
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   940
  \<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
   941
proof
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   942
  assume ?P
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   943
  then show ?Q
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   944
    by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   945
next
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   946
  assume ?Q
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   947
  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
   948
    by simp_all
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   949
  show ?P
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   950
  proof (rule bit_eqI)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   951
    fix n
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   952
    show \<open>bit a n \<longleftrightarrow> bit b n\<close>
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   953
    proof (cases n)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   954
      case 0
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   955
      with \<open>even a \<longleftrightarrow> even b\<close> show ?thesis
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   956
        by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   957
    next
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   958
      case (Suc n)
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   959
      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
   960
        by simp
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   961
      ultimately show ?thesis
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
   962
        by (simp add: bit_Suc)
71441
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   963
    qed
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   964
  qed
4e66867fd63f tuned proof
haftmann
parents: 71426
diff changeset
   965
qed
71426
745e518d3d0b easy abstraction over pointwise bit operations
haftmann
parents: 71424
diff changeset
   966
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
   967
lemma bit_mod_2_iff [simp]:
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
   968
  \<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
   969
  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
   970
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   971
lemma bit_mask_iff:
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   972
  \<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
   973
  by (simp add: bit_iff_odd even_mask_div_iff not_le)
71418
bd9d27ccb3a3 more theorems
haftmann
parents: 71413
diff changeset
   974
71757
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   975
lemma bit_Numeral1_iff [simp]:
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   976
  \<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close>
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   977
  by (simp add: bit_rec)
02c50bba9304 bit on numerals
haftmann
parents: 71755
diff changeset
   978
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   979
lemma exp_add_not_zero_imp:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   980
  \<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
   981
proof -
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   982
  have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   983
  proof (rule notI)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   984
    assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   985
    then have \<open>2 ^ (m + n) = 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   986
      by (rule disjE) (simp_all add: power_add)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   987
    with that show False ..
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   988
  qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   989
  then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   990
    by simp_all
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   991
qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
   992
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   993
lemma bit_disjunctive_add_iff:
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   994
  \<open>bit (a + b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   995
  if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   996
proof (cases \<open>2 ^ n = 0\<close>)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   997
  case True
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   998
  then show ?thesis
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
   999
    by (simp add: exp_eq_0_imp_not_bit)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1000
next
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1001
  case False
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1002
  with that show ?thesis proof (induction n arbitrary: a b)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1003
    case 0
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1004
    from "0.prems"(1) [of 0] show ?case
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1005
      by auto
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1006
  next
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1007
    case (Suc n)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1008
    from Suc.prems(1) [of 0] have even: \<open>even a \<or> even b\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1009
      by auto
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1010
    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
  1011
      using Suc.prems(1) [of \<open>Suc n\<close>] by (simp add: bit_Suc)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1012
    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
  1013
      by (auto simp add: mult_2)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1014
    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
  1015
      using div_mult_mod_eq [of a 2] div_mult_mod_eq [of b 2] by simp
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1016
    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
  1017
      using even by (auto simp add: algebra_simps mod2_eq_if)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1018
    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
  1019
      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
  1020
    also have \<open>\<dots> \<longleftrightarrow> bit (a div 2) n \<or> bit (b div 2) n\<close>
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1021
      using bit \<open>2 ^ n \<noteq> 0\<close> by (rule Suc.IH)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1022
    finally show ?case
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1023
      by (simp add: bit_Suc)
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1024
  qed
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1025
qed
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  1026
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1027
lemma
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1028
  exp_add_not_zero_imp_left: \<open>2 ^ m \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1029
  and exp_add_not_zero_imp_right: \<open>2 ^ n \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1030
  if \<open>2 ^ (m + n) \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1031
proof -
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1032
  have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1033
  proof (rule notI)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1034
    assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1035
    then have \<open>2 ^ (m + n) = 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1036
      by (rule disjE) (simp_all add: power_add)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1037
    with that show False ..
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1038
  qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1039
  then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1040
    by simp_all
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1041
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1042
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1043
lemma exp_not_zero_imp_exp_diff_not_zero:
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1044
  \<open>2 ^ (n - m) \<noteq> 0\<close> if \<open>2 ^ n \<noteq> 0\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1045
proof (cases \<open>m \<le> n\<close>)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1046
  case True
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1047
  moreover define q where \<open>q = n - m\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1048
  ultimately have \<open>n = m + q\<close>
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1049
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1050
  with that show ?thesis
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1051
    by (simp add: exp_add_not_zero_imp_right)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1052
next
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1053
  case False
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1054
  with that show ?thesis
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1055
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1056
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1057
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1058
end
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1059
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1060
lemma nat_bit_induct [case_names zero even odd]:
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1061
  "P n" if zero: "P 0"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1062
    and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1063
    and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1064
proof (induction n rule: less_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1065
  case (less n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1066
  show "P n"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1067
  proof (cases "n = 0")
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1068
    case True with zero show ?thesis by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1069
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1070
    case False
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1071
    with less have hyp: "P (n div 2)" by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1072
    show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1073
    proof (cases "even n")
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1074
      case True
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1075
      then have "n \<noteq> 1"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1076
        by auto
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1077
      with \<open>n \<noteq> 0\<close> have "n div 2 > 0"
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1078
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1079
      with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1080
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1081
    next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1082
      case False
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1083
      with hyp odd [of "n div 2"] show ?thesis
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1084
        by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1085
    qed
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1086
  qed
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
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1089
instantiation nat :: semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1090
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1091
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1092
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
  1093
  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
  1094
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1095
instance
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1096
proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1097
  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
  1098
    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
  1099
    for P and n :: nat
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1100
  proof (induction n rule: nat_bit_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1101
    case zero
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1102
    from stable [of 0] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1103
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1104
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1105
    case (even n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1106
    with rec [of n False] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1107
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1108
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1109
    case (odd n)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1110
    with rec [of n True] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1111
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1112
  qed
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1113
  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
  1114
    for q m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1115
    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
  1116
    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
  1117
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1118
  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
  1119
    for q m n :: nat
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1120
    using that
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1121
    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
  1122
    apply (simp add: mult.commute)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1123
    done
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1124
  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
  1125
    for m n :: nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1126
    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
  1127
  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
  1128
    for m n q r :: nat
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1129
    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
  1130
    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
  1131
    done
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1132
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
  1133
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1134
end
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1135
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1136
lemma int_bit_induct [case_names zero minus even odd]:
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1137
  "P k" if zero_int: "P 0"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1138
    and minus_int: "P (- 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1139
    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
  1140
    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
  1141
proof (cases "k \<ge> 0")
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1142
  case True
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1143
  define n where "n = nat k"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1144
  with True have "k = int n"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1145
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1146
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1147
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1148
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1149
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1150
      by (simp add: zero_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1151
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1152
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1153
    have "P (int n * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1154
      by (rule even_int) (use even in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1155
    with even show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1156
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1157
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1158
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1159
    have "P (1 + (int n * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1160
      by (rule odd_int) (use odd in simp_all)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1161
    with odd show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1162
      by (simp add: ac_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1163
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1164
next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1165
  case False
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1166
  define n where "n = nat (- k - 1)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1167
  with False have "k = - int n - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1168
    by simp
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1169
  then show "P k"
70353
7aa64296b9b0 even more appropriate fact name
haftmann
parents: 70341
diff changeset
  1170
  proof (induction n arbitrary: k rule: nat_bit_induct)
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1171
    case zero
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1172
    then show ?case
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1173
      by (simp add: minus_int)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1174
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1175
    case (even n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1176
    have "P (1 + (- int (Suc n) * 2))"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1177
      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
  1178
    also have "\<dots> = - int (2 * n) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1179
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1180
    finally show ?case
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1181
      using even.prems by simp
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1182
  next
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1183
    case (odd n)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1184
    have "P (- int (Suc n) * 2)"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1185
      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
  1186
    also have "\<dots> = - int (Suc (2 * n)) - 1"
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1187
      by (simp add: algebra_simps)
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1188
    finally show ?case
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1189
      using odd.prems by simp
70338
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1190
  qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1191
qed
c832d431636b slightly more stringent ordering of theorems
haftmann
parents: 70226
diff changeset
  1192
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1193
context semiring_bits
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1194
begin
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1195
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1196
lemma bit_of_bool_iff:
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1197
  \<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
  1198
	by (simp add: bit_1_iff)
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72262
diff changeset
  1199
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1200
lemma even_of_nat_iff:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1201
  \<open>even (of_nat n) \<longleftrightarrow> even n\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1202
  by (induction n rule: nat_bit_induct) simp_all
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1203
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1204
lemma bit_of_nat_iff:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1205
  \<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
  1206
proof (cases \<open>(2::'a) ^ n = 0\<close>)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1207
  case True
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1208
  then show ?thesis
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1209
    by (simp add: exp_eq_0_imp_not_bit)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1210
next
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1211
  case False
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1212
  then have \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1213
  proof (induction m arbitrary: n rule: nat_bit_induct)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1214
    case zero
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1215
    then show ?case
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1216
      by simp
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1217
  next
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1218
    case (even m)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1219
    then show ?case
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1220
      by (cases n)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1221
        (auto simp add: bit_double_iff Parity.bit_double_iff dest: mult_not_zero)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1222
  next
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1223
    case (odd m)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1224
    then show ?case
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1225
      by (cases n)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1226
         (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
  1227
  qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1228
  with False show ?thesis
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1229
    by simp
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1230
qed
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1231
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1232
end
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1233
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1234
instantiation int :: semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1235
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1236
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1237
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
  1238
  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
  1239
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1240
instance
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1241
proof
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1242
  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
  1243
    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
  1244
    for P and k :: int
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1245
  proof (induction k rule: int_bit_induct)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1246
    case zero
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1247
    from stable [of 0] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1248
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1249
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1250
    case minus
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1251
    from stable [of \<open>- 1\<close>] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1252
      by simp
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1253
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1254
    case (even k)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1255
    with rec [of k False] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1256
      by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1257
  next
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1258
    case (odd k)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1259
    with rec [of k True] show ?case
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1260
      by (simp add: ac_simps)
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1261
  qed
71182
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1262
  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
  1263
    for m n :: nat
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1264
  proof (cases \<open>m < n\<close>)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1265
    case True
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1266
    then have \<open>n = m + (n - m)\<close>
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1267
      by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1268
    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
  1269
      by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1270
    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
  1271
      by (simp add: power_add)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1272
    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
  1273
      by (simp add: zdiv_zmult2_eq)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1274
    finally show ?thesis using \<open>m < n\<close> by simp
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1275
  next
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1276
    case False
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1277
    then show ?thesis
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1278
      by (simp add: power_diff)
410935efbf5c characterization of singleton bit operation
haftmann
parents: 71181
diff changeset
  1279
  qed
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1280
  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
  1281
    for m n :: nat and k :: int
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1282
    using mod_exp_eq [of \<open>nat k\<close> m n]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1283
    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
  1284
     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
  1285
    apply (simp only: flip: mult.left_commute [of \<open>2 ^ m\<close>])
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1286
    apply (subst zmod_zmult2_eq) apply simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1287
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1288
  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
  1289
    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
  1290
    using that
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1291
    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
  1292
    apply (simp add: ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1293
    done
71413
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1294
  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
  1295
    for m n :: nat
65ffe9e910d4 more specific class assumptions
haftmann
parents: 71412
diff changeset
  1296
    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
  1297
  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
  1298
    for m n :: nat and k l :: int
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1299
    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
  1300
    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
  1301
    done
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1302
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
  1303
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1304
end
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1305
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1306
class semiring_bit_shifts = semiring_bits +
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1307
  fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1308
  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
  1309
  fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1310
  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
  1311
  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
  1312
  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
  1313
begin
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1314
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1315
text \<open>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1316
  Logically, \<^const>\<open>push_bit\<close>,
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1317
  \<^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
  1318
  as separate operations makes proofs easier, otherwise proof automation
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1319
  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
  1320
  algebraic relationships between those operations.
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1321
  Having
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1322
  them as definitional class operations
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1323
  takes into account that specific instances of these can be implemented
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1324
  differently wrt. code generation.
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1325
\<close>
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1326
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1327
lemma bit_iff_odd_drop_bit:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1328
  \<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
  1329
  by (simp add: bit_iff_odd drop_bit_eq_div)
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1330
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1331
lemma even_drop_bit_iff_not_bit:
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1332
  \<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close>
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1333
  by (simp add: bit_iff_odd_drop_bit)
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1334
71423
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1335
lemma div_push_bit_of_1_eq_drop_bit:
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1336
  \<open>a div push_bit n 1 = drop_bit n a\<close>
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1337
  by (simp add: push_bit_eq_mult drop_bit_eq_div)
7ae4dcf332ae more lemmas
haftmann
parents: 71418
diff changeset
  1338
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1339
lemma bits_ident:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1340
  "push_bit n (drop_bit n a) + take_bit n a = a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1341
  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
  1342
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1343
lemma push_bit_push_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1344
  "push_bit m (push_bit n a) = push_bit (m + n) a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1345
  by (simp add: push_bit_eq_mult power_add ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1346
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1347
lemma push_bit_0_id [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1348
  "push_bit 0 = id"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1349
  by (simp add: fun_eq_iff push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1350
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1351
lemma push_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1352
  "push_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1353
  by (simp add: push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1354
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1355
lemma push_bit_of_1:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1356
  "push_bit n 1 = 2 ^ n"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1357
  by (simp add: push_bit_eq_mult)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1358
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1359
lemma push_bit_Suc [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1360
  "push_bit (Suc n) a = push_bit n (a * 2)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1361
  by (simp add: push_bit_eq_mult ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1362
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1363
lemma push_bit_double:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1364
  "push_bit n (a * 2) = push_bit n a * 2"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1365
  by (simp add: push_bit_eq_mult ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1366
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1367
lemma push_bit_add:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1368
  "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
  1369
  by (simp add: push_bit_eq_mult algebra_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1370
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1371
lemma push_bit_numeral [simp]:
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1372
  \<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
  1373
  by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0)
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1374
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1375
lemma take_bit_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1376
  "take_bit 0 a = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1377
  by (simp add: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1378
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1379
lemma take_bit_Suc:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
  1380
  \<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
  1381
proof -
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1382
  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
  1383
    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
  1384
      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
  1385
    by (auto simp add: take_bit_eq_mod ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1386
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1387
    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
  1388
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1389
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1390
lemma take_bit_rec:
71822
67cc2319104f prefer _ mod 2 over of_bool (odd _)
haftmann
parents: 71804
diff changeset
  1391
  \<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
  1392
  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
  1393
71759
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1394
lemma take_bit_Suc_0 [simp]:
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1395
  \<open>take_bit (Suc 0) a = a mod 2\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1396
  by (simp add: take_bit_eq_mod)
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1397
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1398
lemma take_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1399
  "take_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1400
  by (simp add: take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1401
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1402
lemma take_bit_of_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1403
  "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
  1404
  by (cases n) (simp_all add: take_bit_Suc)
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1405
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1406
lemma drop_bit_of_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1407
  "drop_bit n 0 = 0"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1408
  by (simp add: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1409
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1410
lemma drop_bit_of_1 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1411
  "drop_bit n 1 = of_bool (n = 0)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1412
  by (simp add: drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1413
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1414
lemma drop_bit_0 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1415
  "drop_bit 0 = id"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1416
  by (simp add: fun_eq_iff drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1417
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1418
lemma drop_bit_Suc:
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1419
  "drop_bit (Suc n) a = drop_bit n (a div 2)"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1420
  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
  1421
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1422
lemma drop_bit_rec:
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1423
  "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
  1424
  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
  1425
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1426
lemma drop_bit_half:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1427
  "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
  1428
  by (induction n arbitrary: a) (simp_all add: drop_bit_Suc)
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1429
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1430
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
  1431
  "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
  1432
  by (cases n) simp_all
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1433
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1434
lemma even_take_bit_eq [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1435
  \<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
  1436
  by (simp add: take_bit_rec [of n a])
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1437
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1438
lemma take_bit_take_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1439
  "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
  1440
  by (simp add: take_bit_eq_mod mod_exp_eq ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1441
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1442
lemma drop_bit_drop_bit [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1443
  "drop_bit m (drop_bit n a) = drop_bit (m + n) a"
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1444
  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
  1445
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1446
lemma push_bit_take_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1447
  "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
  1448
  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
  1449
  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
  1450
  done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1451
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1452
lemma take_bit_push_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1453
  "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
  1454
proof (cases "m \<le> n")
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1455
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1456
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1457
    apply (simp add:)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1458
    apply (simp_all add: push_bit_eq_mult take_bit_eq_mod)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1459
    apply (auto dest!: le_Suc_ex simp add: power_add ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1460
    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
  1461
    apply (simp add: ac_simps)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1462
    done
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1463
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1464
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1465
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1466
    using push_bit_take_bit [of n "m - n" a]
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1467
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1468
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1469
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1470
lemma take_bit_drop_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1471
  "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
  1472
  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
  1473
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1474
lemma drop_bit_take_bit:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1475
  "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
  1476
proof (cases "m \<le> n")
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1477
  case True
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1478
  then show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1479
    using take_bit_drop_bit [of "n - m" m a] by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1480
next
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1481
  case False
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1482
  then obtain q where \<open>m = n + q\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1483
    by (auto simp add: not_le dest: less_imp_Suc_add)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1484
  then have \<open>drop_bit m (take_bit n a) = 0\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1485
    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
  1486
    by (simp add: take_bit_eq_mod drop_bit_eq_div)
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1487
  with False show ?thesis
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1488
    by simp
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1489
qed
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1490
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1491
lemma even_push_bit_iff [simp]:
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1492
  \<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
  1493
  by (simp add: push_bit_eq_mult) auto
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1494
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1495
lemma bit_push_bit_iff:
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71966
diff changeset
  1496
  \<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
  1497
  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
  1498
71181
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1499
lemma bit_drop_bit_eq:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1500
  \<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
  1501
  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
  1502
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1503
lemma bit_take_bit_iff:
8331063570d6 bit accessor and fundamental properties
haftmann
parents: 71157
diff changeset
  1504
  \<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
  1505
  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
  1506
71535
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1507
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
  1508
  \<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
  1509
  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
  1510
  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
  1511
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1512
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
  1513
  \<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
  1514
    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
  1515
proof (rule bit_eqI)
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1516
  fix m
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1517
  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
  1518
  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
  1519
    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
  1520
qed
b612edee9b0c more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents: 71441
diff changeset
  1521
71958
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1522
lemma exp_dvdE:
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1523
  assumes \<open>2 ^ n dvd a\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1524
  obtains b where \<open>a = push_bit n b\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1525
proof -
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1526
  from assms obtain b where \<open>a = 2 ^ n * b\<close> ..
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1527
  then have \<open>a = push_bit n b\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1528
    by (simp add: push_bit_eq_mult ac_simps)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1529
  with that show thesis .
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1530
qed
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1531
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1532
lemma take_bit_eq_0_iff:
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1533
  \<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
  1534
proof
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1535
  assume ?P
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1536
  then show ?Q
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1537
	  by (simp add: take_bit_eq_mod mod_0_imp_dvd)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1538
next
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1539
  assume ?Q
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1540
  then obtain b where \<open>a = push_bit n b\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1541
    by (rule exp_dvdE)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1542
  then show ?P
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1543
    by (simp add: take_bit_push_bit)
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1544
qed
4320875eb8a1 more lemmas
haftmann
parents: 71853
diff changeset
  1545
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1546
lemma take_bit_tightened:
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1547
  \<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
  1548
proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1549
  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
  1550
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1551
  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
  1552
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1553
  with that show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1554
    by (simp add: min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1555
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72026
diff changeset
  1556
70341
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1557
end
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1558
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1559
instantiation nat :: semiring_bit_shifts
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1560
begin
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1561
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1562
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1563
  where \<open>push_bit_nat n m = m * 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1564
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1565
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1566
  where \<open>drop_bit_nat n m = m div 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1567
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1568
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
  1569
  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
  1570
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1571
instance
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1572
  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
  1573
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1574
end
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1575
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1576
context semiring_bit_shifts
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1577
begin
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1578
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1579
lemma push_bit_of_nat:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1580
  \<open>push_bit n (of_nat m) = of_nat (push_bit n m)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1581
  by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1582
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1583
lemma of_nat_push_bit:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1584
  \<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1585
  by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1586
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1587
lemma take_bit_of_nat:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1588
  \<open>take_bit n (of_nat m) = of_nat (take_bit n m)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1589
  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
  1590
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1591
lemma of_nat_take_bit:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1592
  \<open>of_nat (take_bit n m) = take_bit n (of_nat m)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1593
  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
  1594
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1595
end
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1596
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1597
instantiation int :: semiring_bit_shifts
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1598
begin
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1599
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1600
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1601
  where \<open>push_bit_int n k = k * 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1602
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1603
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1604
  where \<open>drop_bit_int n k = k div 2 ^ n\<close>
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1605
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1606
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
  1607
  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
  1608
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1609
instance
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1610
  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
  1611
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1612
end
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1613
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1614
lemma bit_push_bit_iff_nat:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1615
  \<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
  1616
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1617
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1618
lemma bit_push_bit_iff_int:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1619
  \<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
  1620
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1621
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1622
lemma take_bit_nat_less_exp [simp]:
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1623
  \<open>take_bit n m < 2 ^ n\<close> for n m ::nat 
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1624
  by (simp add: take_bit_eq_mod)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1625
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1626
lemma take_bit_nonnegative [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1627
  \<open>take_bit n k \<ge> 0\<close> for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1628
  by (simp add: take_bit_eq_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1629
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1630
lemma not_take_bit_negative [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1631
  \<open>\<not> take_bit n k < 0\<close> for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1632
  by (simp add: not_less)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1633
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1634
lemma take_bit_int_less_exp [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1635
  \<open>take_bit n k < 2 ^ n\<close> for k :: int
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1636
  by (simp add: take_bit_eq_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1637
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1638
lemma take_bit_nat_eq_self_iff:
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1639
  \<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
  1640
  for n m :: nat
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1641
proof
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1642
  assume ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1643
  moreover note take_bit_nat_less_exp [of n m]
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1644
  ultimately show ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1645
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1646
next
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1647
  assume ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1648
  then show ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1649
    by (simp add: take_bit_eq_mod)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1650
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1651
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1652
lemma take_bit_nat_eq_self:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1653
  \<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
  1654
  using that by (simp add: take_bit_nat_eq_self_iff)
72261
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1655
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1656
lemma take_bit_int_eq_self_iff:
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1657
  \<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
  1658
  for k :: int
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1659
proof
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1660
  assume ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1661
  moreover note take_bit_int_less_exp [of n k] take_bit_nonnegative [of n k]
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1662
  ultimately show ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1663
    by simp
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1664
next
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1665
  assume ?Q
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1666
  then show ?P
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1667
    by (simp add: take_bit_eq_mod)
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1668
qed
5193570b739a more lemmas
haftmann
parents: 72239
diff changeset
  1669
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1670
lemma take_bit_int_eq_self:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1671
  \<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
  1672
  using that by (simp add: take_bit_int_eq_self_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1673
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1674
lemma take_bit_nat_less_eq_self [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1675
  \<open>take_bit n m \<le> m\<close> for n m :: nat
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1676
  by (simp add: take_bit_eq_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1677
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1678
lemma take_bit_nat_less_self_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1679
  \<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
  1680
  for m n :: nat
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1681
proof
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1682
  assume ?P
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1683
  then have \<open>take_bit n m \<noteq> m\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1684
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1685
  then show \<open>?Q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1686
    by (simp add: take_bit_nat_eq_self_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1687
next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1688
  have \<open>take_bit n m < 2 ^ n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1689
    by (fact take_bit_nat_less_exp)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1690
  also assume ?Q
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1691
  finally show ?P .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1692
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1693
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1694
class unique_euclidean_semiring_with_bit_shifts =
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1695
  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
  1696
begin
972c0c744e7c generalized type classes for parity to cover word types also, which contain zero divisors
haftmann
parents: 70340
diff changeset
  1697
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1698
lemma take_bit_of_exp [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1699
  \<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
  1700
  by (simp add: take_bit_eq_mod exp_mod_exp)
67960
ac66cbe795e5 more bit operation conversions
haftmann
parents: 67908
diff changeset
  1701
71138
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1702
lemma take_bit_of_2 [simp]:
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1703
  \<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close>
9de7f1067520 strengthened type class for bit operations
haftmann
parents: 71094
diff changeset
  1704
  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
  1705
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1706
lemma take_bit_of_mask:
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1707
  \<open>take_bit m (2 ^ n - 1) = 2 ^ min m n - 1\<close>
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1708
  by (simp add: take_bit_eq_mod mask_mod_exp)
71408
554385d4cf59 more theorems
haftmann
parents: 71195
diff changeset
  1709
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1710
lemma push_bit_eq_0_iff [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1711
  "push_bit n a = 0 \<longleftrightarrow> a = 0"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1712
  by (simp add: push_bit_eq_mult)
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1713
67961
9c31678d2139 even more on bit operations
haftmann
parents: 67960
diff changeset
  1714
lemma take_bit_add:
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1715
  "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
  1716
  by (simp add: take_bit_eq_mod mod_simps)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1717
67907
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1718
lemma take_bit_of_1_eq_0_iff [simp]:
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1719
  "take_bit n 1 = 0 \<longleftrightarrow> n = 0"
02a14c1cb917 prefer convention to place operation name before type name
haftmann
parents: 67906
diff changeset
  1720
  by (simp add: take_bit_eq_mod)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1721
72026
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1722
lemma take_bit_Suc_1 [simp]:
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1723
  \<open>take_bit (Suc n) 1 = 1\<close>
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1724
  by (simp add: take_bit_Suc)
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1725
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1726
lemma take_bit_Suc_bit0 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1727
  \<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
  1728
  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
  1729
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1730
lemma take_bit_Suc_bit1 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1731
  \<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
  1732
  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
  1733
72026
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1734
lemma take_bit_numeral_1 [simp]:
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1735
  \<open>take_bit (numeral l) 1 = 1\<close>
5689f0db4508 more simp rules for concrete numerical values
haftmann
parents: 72024
diff changeset
  1736
  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
  1737
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1738
lemma take_bit_numeral_bit0 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1739
  \<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
  1740
  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
  1741
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1742
lemma take_bit_numeral_bit1 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1743
  \<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
  1744
  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
  1745
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1746
lemma drop_bit_Suc_bit0 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1747
  \<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
  1748
  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
  1749
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1750
lemma drop_bit_Suc_bit1 [simp]:
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1751
  \<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
  1752
  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
  1753
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1754
lemma drop_bit_numeral_bit0 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1755
  \<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
  1756
  by (simp add: drop_bit_rec numeral_Bit0_div_2)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1757
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1758
lemma drop_bit_numeral_bit1 [simp]:
71799
e00712b4e2c2 numeral rules for take_bit / drop_bit on int
haftmann
parents: 71759
diff changeset
  1759
  \<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
  1760
  by (simp add: drop_bit_rec numeral_Bit1_div_2)
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1761
68010
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1762
lemma drop_bit_of_nat:
3f223b9a0066 algebraic embeddings for bit operations
haftmann
parents: 67988
diff changeset
  1763
  "drop_bit n (of_nat m) = of_nat (drop_bit n m)"
68389
1c84a8c513af proper white space;
wenzelm
parents: 68157
diff changeset
  1764
  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
  1765
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1766
lemma bit_of_nat_iff_bit [simp]:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1767
  \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1768
proof -
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1769
  have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1770
    by simp
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1771
  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
  1772
    by (simp add: of_nat_div)
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1773
  finally show ?thesis
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1774
    by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1775
qed
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1776
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1777
lemma of_nat_drop_bit:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1778
  \<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close>
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1779
  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
  1780
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1781
lemma bit_push_bit_iff_of_nat_iff:
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1782
  \<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
  1783
  by (auto simp add: bit_push_bit_iff)
71412
96d126844adc more theorems
haftmann
parents: 71408
diff changeset
  1784
58770
ae5e9b4f8daf downshift of theory Parity in the hierarchy
haftmann
parents: 58769
diff changeset
  1785
end
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1786
71094
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1787
instance nat :: unique_euclidean_semiring_with_bit_shifts ..
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1788
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1789
instance int :: unique_euclidean_semiring_with_bit_shifts ..
a197532693a5 bit shifts as class operations
haftmann
parents: 70973
diff changeset
  1790
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1791
lemma bit_nat_iff:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1792
  \<open>bit (nat k) n \<longleftrightarrow> k \<ge> 0 \<and> bit k n\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1793
proof (cases \<open>k \<ge> 0\<close>)
71804
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1794
  case True
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1795
  moreover define m where \<open>m = nat k\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1796
  ultimately have \<open>k = int m\<close>
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1797
    by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1798
  then show ?thesis
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1799
    by (auto intro: ccontr)
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1800
next
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1801
  case False
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1802
  then show ?thesis
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1803
    by simp
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1804
qed
6fd70ed18199 simplified construction of binary bit operations
haftmann
parents: 71802
diff changeset
  1805
72227
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1806
lemma push_bit_nat_eq:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1807
  \<open>push_bit n (nat k) = nat (push_bit n k)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1808
  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
  1809
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1810
lemma drop_bit_nat_eq:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1811
  \<open>drop_bit n (nat k) = nat (drop_bit n k)\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1812
  apply (cases \<open>k \<ge> 0\<close>)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1813
   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
  1814
  apply (simp add: divide_int_def)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1815
  done
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1816
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1817
lemma take_bit_nat_eq:
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1818
  \<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
  1819
  using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
0f3d24dc197f more on conversions
haftmann
parents: 72187
diff changeset
  1820
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1821
lemma nat_take_bit_eq:
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1822
  \<open>nat (take_bit n k) = take_bit n (nat k)\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1823
  if \<open>k \<ge> 0\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1824
  using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1825
71802
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1826
lemma not_exp_less_eq_0_int [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1827
  \<open>\<not> 2 ^ n \<le> (0::int)\<close>
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1828
  by (simp add: power_le_zero_eq)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1829
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1830
lemma half_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1831
  \<open>k div 2 \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1832
proof (cases \<open>k \<ge> 0\<close>)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1833
  case True
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1834
  then show ?thesis
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1835
    by (auto simp add: divide_int_def sgn_1_pos)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1836
next
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1837
  case False
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1838
  then show ?thesis
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1839
    apply (auto simp add: divide_int_def not_le elim!: evenE)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1840
    apply (simp only: minus_mult_right)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1841
    apply (subst nat_mult_distrib)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1842
     apply simp_all
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1843
    done
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1844
qed
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1845
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1846
lemma half_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1847
  \<open>k div 2 < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1848
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1849
67988
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1850
lemma push_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1851
  "push_bit n (Suc 0) = 2 ^ n"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1852
  using push_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1853
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1854
lemma take_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1855
  "take_bit n (Suc 0) = of_bool (0 < n)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1856
  using take_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1857
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1858
lemma drop_bit_of_Suc_0 [simp]:
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1859
  "drop_bit n (Suc 0) = of_bool (n = 0)"
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1860
  using drop_bit_of_1 [where ?'a = nat] by simp
01c651412081 explicit simp rules for computing abstract bit operations
haftmann
parents: 67961
diff changeset
  1861
70911
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1862
lemma push_bit_minus_one:
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1863
  "push_bit n (- 1 :: int) = - (2 ^ n)"
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1864
  by (simp add: push_bit_eq_mult)
38298c04c12e more lemmas
haftmann
parents: 70365
diff changeset
  1865
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1866
lemma minus_1_div_exp_eq_int:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1867
  \<open>- 1 div (2 :: int) ^ n = - 1\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1868
  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
  1869
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1870
lemma drop_bit_minus_one [simp]:
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1871
  \<open>drop_bit n (- 1 :: int) = - 1\<close>
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1872
  by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1873
72023
08348e364739 more thms
haftmann
parents: 71991
diff changeset
  1874
lemma take_bit_Suc_from_most:
08348e364739 more thms
haftmann
parents: 71991
diff changeset
  1875
  \<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
  1876
  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
  1877
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1878
lemma take_bit_minus:
71966
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1879
  \<open>take_bit n (- take_bit n k) = take_bit n (- k)\<close>
71195
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1880
    for k :: int
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1881
  by (simp add: take_bit_eq_mod mod_minus_eq)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1882
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1883
lemma take_bit_diff:
71966
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1884
  \<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
  1885
    for k l :: int
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1886
  by (simp add: take_bit_eq_mod mod_diff_eq)
d50a718ccf35 tuned material
haftmann
parents: 71182
diff changeset
  1887
72187
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1888
lemma bit_imp_take_bit_positive:
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1889
  \<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
  1890
proof (rule ccontr)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1891
  assume \<open>\<not> 0 < take_bit m k\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1892
  then have \<open>take_bit m k = 0\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1893
    by (auto simp add: not_less intro: order_antisym)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1894
  then have \<open>bit (take_bit m k) n = bit 0 n\<close>
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1895
    by simp
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1896
  with that show False
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1897
    by (simp add: bit_take_bit_iff)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1898
qed
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1899
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1900
lemma take_bit_mult:
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1901
  \<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
  1902
  for k l :: int
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1903
  by (simp add: take_bit_eq_mod mod_mult_eq)
e4aecb0c7296 more lemmas
haftmann
parents: 72079
diff changeset
  1904
71966
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1905
lemma (in ring_1) of_nat_nat_take_bit_eq [simp]:
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1906
  \<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
  1907
  by simp
e18e9ac8c205 simp rules for conversions
haftmann
parents: 71965
diff changeset
  1908
71759
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1909
lemma take_bit_minus_small_eq:
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1910
  \<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
  1911
proof -
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1912
  define m where \<open>m = nat k\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1913
  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
  1914
    by simp_all
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1915
  have \<open>(2 ^ n - m) mod 2 ^ n = 2 ^ n - m\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1916
    using \<open>0 < m\<close> by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1917
  then have \<open>int ((2 ^ n - m) mod 2 ^ n) = int (2 ^ n - m)\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1918
    by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1919
  then have \<open>(2 ^ n - int m) mod 2 ^ n = 2 ^ n - int m\<close>
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1920
    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
  1921
  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
  1922
    by simp
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1923
  then show ?thesis
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1924
    by (simp add: take_bit_eq_mod)
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1925
qed
816e52bbfa60 more theorems
haftmann
parents: 71757
diff changeset
  1926
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1927
lemma drop_bit_push_bit_int:
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1928
  \<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
  1929
  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
  1930
    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
  1931
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1932
lemma push_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1933
  \<open>push_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1934
  by (simp add: push_bit_eq_mult zero_le_mult_iff)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1935
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1936
lemma push_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1937
  \<open>push_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1938
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1939
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1940
lemma drop_bit_nonnegative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1941
  \<open>drop_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1942
  by (induction n) (simp_all add: drop_bit_Suc drop_bit_half)
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1943
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1944
lemma drop_bit_negative_int_iff [simp]:
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1945
  \<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int
ab3cecb836b5 more rules
haftmann
parents: 71799
diff changeset
  1946
  by (subst Not_eq_iff [symmetric]) (simp add: not_less)
71424
e83fe2c31088 rule concerning bit (push_bit ...)
haftmann
parents: 71423
diff changeset
  1947
71853
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
  1948
code_identifier
30d92e668b52 tuned module name space for generated code
haftmann
parents: 71837
diff changeset
  1949
  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
  1950
67816
2249b27ab1dd abstract algebraic bit operations
haftmann
parents: 67371
diff changeset
  1951
end