72000
|
1 |
(* Author: Jeremy Dawson, NICTA
|
|
2 |
*)
|
|
3 |
|
|
4 |
section \<open>Operation variant for setting and unsetting bits\<close>
|
|
5 |
|
|
6 |
theory Misc_set_bit
|
|
7 |
imports Word Misc_msb
|
|
8 |
begin
|
|
9 |
|
|
10 |
class set_bit = ring_bit_operations +
|
|
11 |
fixes set_bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool \<Rightarrow> 'a\<close>
|
|
12 |
assumes set_bit_eq: \<open>set_bit a n b = (if b then Bit_Operations.set_bit else unset_bit) n a\<close>
|
|
13 |
|
|
14 |
instantiation int :: set_bit
|
|
15 |
begin
|
|
16 |
|
|
17 |
definition set_bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool \<Rightarrow> int\<close>
|
|
18 |
where \<open>set_bit i n b = bin_sc n b i\<close>
|
|
19 |
|
|
20 |
instance
|
|
21 |
by standard (simp add: set_bit_int_def bin_sc_eq)
|
|
22 |
|
|
23 |
end
|
|
24 |
|
|
25 |
lemma int_set_bit_0 [simp]: fixes x :: int shows
|
|
26 |
"set_bit x 0 b = of_bool b + 2 * (x div 2)"
|
|
27 |
by (auto simp add: set_bit_int_def intro: bin_rl_eqI)
|
|
28 |
|
|
29 |
lemma int_set_bit_Suc: fixes x :: int shows
|
|
30 |
"set_bit x (Suc n) b = of_bool (odd x) + 2 * set_bit (x div 2) n b"
|
|
31 |
by (auto simp add: set_bit_int_def intro: bin_rl_eqI)
|
|
32 |
|
|
33 |
lemma bin_last_set_bit:
|
|
34 |
"bin_last (set_bit x n b) = (if n > 0 then bin_last x else b)"
|
|
35 |
by (cases n) (simp_all add: int_set_bit_Suc)
|
|
36 |
|
|
37 |
lemma bin_rest_set_bit:
|
|
38 |
"bin_rest (set_bit x n b) = (if n > 0 then set_bit (x div 2) (n - 1) b else x div 2)"
|
|
39 |
by (cases n) (simp_all add: int_set_bit_Suc)
|
|
40 |
|
|
41 |
lemma int_set_bit_numeral: fixes x :: int shows
|
|
42 |
"set_bit x (numeral w) b = of_bool (odd x) + 2 * set_bit (x div 2) (pred_numeral w) b"
|
|
43 |
by (simp add: set_bit_int_def)
|
|
44 |
|
|
45 |
lemmas int_set_bit_numerals [simp] =
|
|
46 |
int_set_bit_numeral[where x="numeral w'"]
|
|
47 |
int_set_bit_numeral[where x="- numeral w'"]
|
|
48 |
int_set_bit_numeral[where x="Numeral1"]
|
|
49 |
int_set_bit_numeral[where x="1"]
|
|
50 |
int_set_bit_numeral[where x="0"]
|
|
51 |
int_set_bit_Suc[where x="numeral w'"]
|
|
52 |
int_set_bit_Suc[where x="- numeral w'"]
|
|
53 |
int_set_bit_Suc[where x="Numeral1"]
|
|
54 |
int_set_bit_Suc[where x="1"]
|
|
55 |
int_set_bit_Suc[where x="0"]
|
|
56 |
for w'
|
|
57 |
|
|
58 |
lemma msb_set_bit [simp]: "msb (set_bit (x :: int) n b) \<longleftrightarrow> msb x"
|
|
59 |
by(simp add: msb_conv_bin_sign set_bit_int_def)
|
|
60 |
|
|
61 |
instantiation word :: (len) set_bit
|
|
62 |
begin
|
|
63 |
|
|
64 |
definition set_bit_word :: \<open>'a word \<Rightarrow> nat \<Rightarrow> bool \<Rightarrow> 'a word\<close>
|
|
65 |
where word_set_bit_def: \<open>set_bit a n x = word_of_int (bin_sc n x (uint a))\<close>
|
|
66 |
|
|
67 |
instance
|
|
68 |
apply standard
|
|
69 |
apply (simp add: word_set_bit_def bin_sc_eq Bit_Operations.set_bit_def unset_bit_def)
|
|
70 |
apply transfer
|
|
71 |
apply simp
|
|
72 |
done
|
|
73 |
|
|
74 |
end
|
|
75 |
|
|
76 |
lemma set_bit_unfold:
|
|
77 |
\<open>set_bit w n b = (if b then Bit_Operations.set_bit n w else unset_bit n w)\<close>
|
|
78 |
for w :: \<open>'a::len word\<close>
|
|
79 |
by (simp add: set_bit_eq)
|
|
80 |
|
|
81 |
lemma bit_set_bit_word_iff:
|
|
82 |
\<open>bit (set_bit w m b) n \<longleftrightarrow> (if m = n then n < LENGTH('a) \<and> b else bit w n)\<close>
|
|
83 |
for w :: \<open>'a::len word\<close>
|
|
84 |
by (auto simp add: set_bit_unfold bit_unset_bit_iff bit_set_bit_iff exp_eq_zero_iff not_le bit_imp_le_length)
|
|
85 |
|
|
86 |
lemma word_set_nth [simp]: "set_bit w n (test_bit w n) = w"
|
|
87 |
for w :: "'a::len word"
|
|
88 |
by (auto simp: word_test_bit_def word_set_bit_def)
|
|
89 |
|
|
90 |
lemma test_bit_set: "(set_bit w n x) !! n \<longleftrightarrow> n < size w \<and> x"
|
|
91 |
for w :: "'a::len word"
|
|
92 |
by (auto simp: word_size word_test_bit_def word_set_bit_def word_ubin.eq_norm nth_bintr)
|
|
93 |
|
|
94 |
lemma test_bit_set_gen:
|
|
95 |
"test_bit (set_bit w n x) m = (if m = n then n < size w \<and> x else test_bit w m)"
|
|
96 |
for w :: "'a::len word"
|
|
97 |
apply (unfold word_size word_test_bit_def word_set_bit_def)
|
|
98 |
apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
|
|
99 |
apply (auto elim!: test_bit_size [unfolded word_size]
|
|
100 |
simp add: word_test_bit_def [symmetric])
|
|
101 |
done
|
|
102 |
|
|
103 |
lemma word_set_set_same [simp]: "set_bit (set_bit w n x) n y = set_bit w n y"
|
|
104 |
for w :: "'a::len word"
|
|
105 |
by (rule word_eqI) (simp add : test_bit_set_gen word_size)
|
|
106 |
|
|
107 |
lemma word_set_set_diff:
|
|
108 |
fixes w :: "'a::len word"
|
|
109 |
assumes "m \<noteq> n"
|
|
110 |
shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x"
|
|
111 |
by (rule word_eqI) (auto simp: test_bit_set_gen word_size assms)
|
|
112 |
|
|
113 |
lemma set_bit_word_of_int: "set_bit (word_of_int x) n b = word_of_int (bin_sc n b x)"
|
|
114 |
unfolding word_set_bit_def
|
|
115 |
by (rule word_eqI)(simp add: word_size bin_nth_sc_gen word_ubin.eq_norm nth_bintr)
|
|
116 |
|
|
117 |
lemma word_set_numeral [simp]:
|
|
118 |
"set_bit (numeral bin::'a::len word) n b =
|
|
119 |
word_of_int (bin_sc n b (numeral bin))"
|
|
120 |
unfolding word_numeral_alt by (rule set_bit_word_of_int)
|
|
121 |
|
|
122 |
lemma word_set_neg_numeral [simp]:
|
|
123 |
"set_bit (- numeral bin::'a::len word) n b =
|
|
124 |
word_of_int (bin_sc n b (- numeral bin))"
|
|
125 |
unfolding word_neg_numeral_alt by (rule set_bit_word_of_int)
|
|
126 |
|
|
127 |
lemma word_set_bit_0 [simp]: "set_bit 0 n b = word_of_int (bin_sc n b 0)"
|
|
128 |
unfolding word_0_wi by (rule set_bit_word_of_int)
|
|
129 |
|
|
130 |
lemma word_set_bit_1 [simp]: "set_bit 1 n b = word_of_int (bin_sc n b 1)"
|
|
131 |
unfolding word_1_wi by (rule set_bit_word_of_int)
|
|
132 |
|
|
133 |
lemma word_set_nth_iff: "set_bit w n b = w \<longleftrightarrow> w !! n = b \<or> n \<ge> size w"
|
|
134 |
for w :: "'a::len word"
|
|
135 |
apply (rule iffI)
|
|
136 |
apply (rule disjCI)
|
|
137 |
apply (drule word_eqD)
|
|
138 |
apply (erule sym [THEN trans])
|
|
139 |
apply (simp add: test_bit_set)
|
|
140 |
apply (erule disjE)
|
|
141 |
apply clarsimp
|
|
142 |
apply (rule word_eqI)
|
|
143 |
apply (clarsimp simp add : test_bit_set_gen)
|
|
144 |
apply (drule test_bit_size)
|
|
145 |
apply force
|
|
146 |
done
|
|
147 |
|
|
148 |
lemma word_clr_le: "w \<ge> set_bit w n False"
|
|
149 |
for w :: "'a::len word"
|
|
150 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
|
|
151 |
apply (rule order_trans)
|
|
152 |
apply (rule bintr_bin_clr_le)
|
|
153 |
apply simp
|
|
154 |
done
|
|
155 |
|
|
156 |
lemma word_set_ge: "w \<le> set_bit w n True"
|
|
157 |
for w :: "'a::len word"
|
|
158 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
|
|
159 |
apply (rule order_trans [OF _ bintr_bin_set_ge])
|
|
160 |
apply simp
|
|
161 |
done
|
|
162 |
|
|
163 |
lemma set_bit_beyond:
|
|
164 |
"size x \<le> n \<Longrightarrow> set_bit x n b = x" for x :: "'a :: len word"
|
|
165 |
by (auto intro: word_eqI simp add: test_bit_set_gen word_size)
|
|
166 |
|
|
167 |
end
|