1 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
2 | (* BitVector.ma: Fixed length bitvectors, and common operations on them. *) |
---|
3 | (* Most functions are specialised versions of those found in *) |
---|
4 | (* Vector.ma as a courtesy, or boolean functions lifted into *) |
---|
5 | (* BitVector variants. *) |
---|
6 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
7 | |
---|
8 | include "arithmetics/nat.ma". |
---|
9 | |
---|
10 | include "ASM/FoldStuff.ma". |
---|
11 | include "ASM/Vector.ma". |
---|
12 | include "ASM/String.ma". |
---|
13 | |
---|
14 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
15 | (* Common synonyms. *) |
---|
16 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
17 | |
---|
18 | definition BitVector ≝ λn: nat. Vector bool n. |
---|
19 | definition Bit ≝ bool. |
---|
20 | definition Nibble ≝ BitVector 4. |
---|
21 | definition Byte7 ≝ BitVector 7. |
---|
22 | definition Byte ≝ BitVector 8. |
---|
23 | definition Word ≝ BitVector 16. |
---|
24 | definition Word11 ≝ BitVector 11. |
---|
25 | |
---|
26 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
27 | (* Inversion *) |
---|
28 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
29 | |
---|
30 | lemma BitVector_O: ∀v:BitVector 0. v ≃ VEmpty bool. |
---|
31 | #v lapply (refl … 0) cases v in ⊢ (??%? → ?%%??); // |
---|
32 | #n #hd #tl #abs @⊥ destruct (abs) |
---|
33 | qed. |
---|
34 | |
---|
35 | lemma BitVector_Sn: ∀n.∀v:BitVector (S n). |
---|
36 | ∃hd.∃tl.v ≃ VCons bool n hd tl. |
---|
37 | #n #v lapply (refl … (S n)) cases v in ⊢ (??%? → ??(λ_.??(λ_.?%%??))); |
---|
38 | [ #abs @⊥ destruct (abs) |
---|
39 | | #m #hd #tl #EQ <(injective_S … EQ) %[@hd] %[@tl] // ] |
---|
40 | qed. |
---|
41 | |
---|
42 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
43 | (* Lookup. *) |
---|
44 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
45 | |
---|
46 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
47 | (* Creating bitvectors from scratch. *) |
---|
48 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
49 | |
---|
50 | definition zero: ∀n:nat. BitVector n ≝ |
---|
51 | λn: nat. replicate bool n false. |
---|
52 | |
---|
53 | definition maximum: ∀n:nat. BitVector n ≝ |
---|
54 | λn: nat. replicate bool n true. |
---|
55 | |
---|
56 | definition pad ≝ |
---|
57 | λm, n: nat. |
---|
58 | λb: BitVector n. pad_vector ? false m n b. |
---|
59 | |
---|
60 | let rec nat_to_bv (n : nat) (k : nat) on n : BitVector n ≝ |
---|
61 | match n with |
---|
62 | [ O ⇒ VEmpty ? |
---|
63 | | S n' ⇒ |
---|
64 | eqb (k mod 2) 1 ::: nat_to_bv n' (k ÷ 2) |
---|
65 | ]. |
---|
66 | |
---|
67 | let rec bv_to_nat (n : nat) (b : BitVector n) on b : nat ≝ |
---|
68 | match b with |
---|
69 | [ VEmpty ⇒ 0 |
---|
70 | | VCons n' x b' ⇒ (if x then 1 else 0) + bv_to_nat n' b' * 2]. |
---|
71 | |
---|
72 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
73 | (* Other manipulations. *) |
---|
74 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
75 | |
---|
76 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
77 | (* Logical operations. *) |
---|
78 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
79 | |
---|
80 | definition conjunction_bv: ∀n. ∀b, c: BitVector n. BitVector n ≝ |
---|
81 | λn: nat. |
---|
82 | λb: BitVector n. |
---|
83 | λc: BitVector n. |
---|
84 | zip_with ? ? ? n (andb) b c. |
---|
85 | |
---|
86 | interpretation "BitVector conjunction" 'conjunction b c = (conjunction_bv ? b c). |
---|
87 | |
---|
88 | definition inclusive_disjunction_bv ≝ |
---|
89 | λn: nat. |
---|
90 | λb: BitVector n. |
---|
91 | λc: BitVector n. |
---|
92 | zip_with ? ? ? n (orb) b c. |
---|
93 | |
---|
94 | interpretation "BitVector inclusive disjunction" |
---|
95 | 'inclusive_disjunction b c = (inclusive_disjunction_bv ? b c). |
---|
96 | |
---|
97 | definition exclusive_disjunction_bv ≝ |
---|
98 | λn: nat. |
---|
99 | λb: BitVector n. |
---|
100 | λc: BitVector n. |
---|
101 | zip_with ? ? ? n xorb b c. |
---|
102 | |
---|
103 | interpretation "BitVector exclusive disjunction" |
---|
104 | 'exclusive_disjunction b c = (xorb b c). |
---|
105 | |
---|
106 | definition negation_bv ≝ |
---|
107 | λn: nat. |
---|
108 | λb: BitVector n. |
---|
109 | map bool bool n (notb) b. |
---|
110 | |
---|
111 | interpretation "BitVector negation" 'negation b c = (negation_bv ? b c). |
---|
112 | |
---|
113 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
114 | (* Rotates and shifts. *) |
---|
115 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
116 | |
---|
117 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
118 | (* Conversions to and from lists and natural numbers. *) |
---|
119 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
120 | |
---|
121 | definition eq_b ≝ |
---|
122 | λb, c: bool. |
---|
123 | if b then |
---|
124 | c |
---|
125 | else |
---|
126 | notb c. |
---|
127 | |
---|
128 | lemma eq_b_eq: |
---|
129 | ∀b, c. |
---|
130 | eq_b b c = true → b = c. |
---|
131 | #b #c |
---|
132 | cases b |
---|
133 | cases c |
---|
134 | normalize // |
---|
135 | qed. |
---|
136 | |
---|
137 | definition eq_bv ≝ |
---|
138 | λn: nat. |
---|
139 | λb, c: BitVector n. |
---|
140 | eq_v bool n eq_b b c. |
---|
141 | |
---|
142 | lemma eq_bv_elim: ∀P:bool → Type[0]. ∀n. ∀x,y. |
---|
143 | (x = y → P true) → |
---|
144 | (x ≠ y → P false) → |
---|
145 | P (eq_bv n x y). |
---|
146 | #P #n #x #y #Ht #Hf whd in ⊢ (?%); @(eq_v_elim … Ht Hf) |
---|
147 | #Q * *; normalize /3/ |
---|
148 | qed. |
---|
149 | |
---|
150 | lemma eq_bv_true: ∀n,v. eq_bv n v v = true. |
---|
151 | @eq_v_true * @refl |
---|
152 | qed. |
---|
153 | |
---|
154 | lemma eq_bv_false: ∀n,v,v'. v ≠ v' → eq_bv n v v' = false. |
---|
155 | #n #v #v' #NE @eq_v_false [ * * #H try @refl normalize in H; destruct | @NE ] |
---|
156 | qed. |
---|
157 | |
---|
158 | lemma eq_bv_refl: |
---|
159 | ∀n,v. eq_bv n v v = true. |
---|
160 | #n #v |
---|
161 | elim v |
---|
162 | [ // |
---|
163 | | #n #hd #tl #ih |
---|
164 | normalize |
---|
165 | cases hd |
---|
166 | [ normalize |
---|
167 | @ ih |
---|
168 | | normalize |
---|
169 | @ ih |
---|
170 | ] |
---|
171 | ] |
---|
172 | qed. |
---|
173 | |
---|
174 | lemma eq_bv_sym: ∀n,v1,v2. eq_bv n v1 v2 = eq_bv n v2 v1. |
---|
175 | #n #v1 #v2 @(eq_bv_elim … v1 v2) [// | #H >eq_bv_false /2/] |
---|
176 | qed. |
---|
177 | |
---|
178 | lemma eq_eq_bv: |
---|
179 | ∀n, v, q. |
---|
180 | v = q → eq_bv n v q = true. |
---|
181 | #n #v |
---|
182 | elim v |
---|
183 | [ #q #h <h normalize % |
---|
184 | | #n #hd #tl #ih #q #h >h // |
---|
185 | ] |
---|
186 | qed. |
---|
187 | |
---|
188 | lemma eq_bv_eq: |
---|
189 | ∀n, v, q. |
---|
190 | eq_bv n v q = true → v = q. |
---|
191 | #n #v #q generalize in match v; |
---|
192 | elim q |
---|
193 | [ #v #h @BitVector_O |
---|
194 | | #n #hd #tl #ih #v' #h |
---|
195 | cases (BitVector_Sn ? v') |
---|
196 | #hd' * #tl' #jmeq >jmeq in h; |
---|
197 | #new_h |
---|
198 | change with ((andb ? ?) = ?) in new_h; |
---|
199 | cases(conjunction_true … new_h) |
---|
200 | #eq_heads #eq_tails |
---|
201 | whd in eq_heads:(??(??(%))?); |
---|
202 | cases(eq_b_eq … eq_heads) |
---|
203 | whd in eq_tails:(??(?????(%))?); |
---|
204 | change with (eq_bv ??? = ?) in eq_tails; |
---|
205 | <(ih tl') // |
---|
206 | ] |
---|
207 | qed. |
---|
208 | |
---|
209 | axiom bitvector_of_string: |
---|
210 | ∀n: nat. |
---|
211 | ∀s: String. |
---|
212 | BitVector n. |
---|
213 | |
---|
214 | axiom string_of_bitvector: |
---|
215 | ∀n: nat. |
---|
216 | ∀b: BitVector n. |
---|
217 | String. |
---|