[475] | 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 | |
---|
[698] | 10 | include "ASM/Util.ma". |
---|
| 11 | include "ASM/Vector.ma". |
---|
| 12 | include "ASM/String.ma". |
---|
[475] | 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 | (* Lookup. *) |
---|
| 28 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 29 | |
---|
| 30 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 31 | (* Creating bitvectors from scratch. *) |
---|
| 32 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 33 | |
---|
| 34 | definition zero: ∀n:nat. BitVector n ≝ |
---|
| 35 | λn: nat. replicate bool n false. |
---|
| 36 | |
---|
| 37 | definition maximum: ∀n:nat. BitVector n ≝ |
---|
| 38 | λn: nat. replicate bool n true. |
---|
| 39 | |
---|
| 40 | definition pad ≝ |
---|
| 41 | λm, n: nat. |
---|
[744] | 42 | λb: BitVector n. pad_vector ? false m n b. |
---|
[475] | 43 | |
---|
| 44 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 45 | (* Other manipulations. *) |
---|
| 46 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 47 | |
---|
| 48 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 49 | (* Logical operations. *) |
---|
| 50 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 51 | |
---|
| 52 | definition conjunction_bv: ∀n. ∀b, c: BitVector n. BitVector n ≝ |
---|
| 53 | λn: nat. |
---|
| 54 | λb: BitVector n. |
---|
| 55 | λc: BitVector n. |
---|
| 56 | zip_with ? ? ? n (andb) b c. |
---|
| 57 | |
---|
| 58 | interpretation "BitVector conjunction" 'conjunction b c = (conjunction_bv ? b c). |
---|
| 59 | |
---|
| 60 | definition inclusive_disjunction_bv ≝ |
---|
| 61 | λn: nat. |
---|
| 62 | λb: BitVector n. |
---|
| 63 | λc: BitVector n. |
---|
| 64 | zip_with ? ? ? n (orb) b c. |
---|
| 65 | |
---|
| 66 | interpretation "BitVector inclusive disjunction" |
---|
| 67 | 'inclusive_disjunction b c = (inclusive_disjunction_bv ? b c). |
---|
| 68 | |
---|
| 69 | definition exclusive_disjunction_bv ≝ |
---|
| 70 | λn: nat. |
---|
| 71 | λb: BitVector n. |
---|
| 72 | λc: BitVector n. |
---|
| 73 | zip_with ? ? ? n (exclusive_disjunction) b c. |
---|
| 74 | |
---|
| 75 | interpretation "BitVector exclusive disjunction" |
---|
| 76 | 'exclusive_disjunction b c = (exclusive_disjunction ? b c). |
---|
| 77 | |
---|
| 78 | definition negation_bv ≝ |
---|
| 79 | λn: nat. |
---|
| 80 | λb: BitVector n. |
---|
| 81 | map bool bool n (notb) b. |
---|
| 82 | |
---|
| 83 | interpretation "BitVector negation" 'negation b c = (negation_bv ? b c). |
---|
| 84 | |
---|
| 85 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 86 | (* Rotates and shifts. *) |
---|
| 87 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 88 | |
---|
| 89 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 90 | (* Conversions to and from lists and natural numbers. *) |
---|
| 91 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) |
---|
| 92 | |
---|
| 93 | definition eq_bv ≝ |
---|
| 94 | λn: nat. |
---|
| 95 | λb, c: BitVector n. |
---|
| 96 | eq_v bool n (λd, e. |
---|
| 97 | if (d ∧ e) ∨ ((¬d) ∧ (¬e)) then |
---|
| 98 | true |
---|
| 99 | else |
---|
| 100 | false) b c. |
---|
| 101 | |
---|
[726] | 102 | lemma eq_bv_elim: ∀P:bool → Type[0]. ∀n. ∀x,y. |
---|
[697] | 103 | (x = y → P true) → |
---|
| 104 | (x ≠ y → P false) → |
---|
| 105 | P (eq_bv n x y). |
---|
| 106 | #P #n #x #y #Ht #Hf whd in ⊢ (?%) @(eq_v_elim … Ht Hf) |
---|
| 107 | #Q * *; normalize /3/ |
---|
| 108 | qed. |
---|
[475] | 109 | |
---|
| 110 | axiom bitvector_of_string: |
---|
| 111 | ∀n: nat. |
---|
| 112 | ∀s: String. |
---|
| 113 | BitVector n. |
---|
| 114 | |
---|
| 115 | axiom string_of_bitvector: |
---|
| 116 | ∀n: nat. |
---|
| 117 | ∀b: BitVector n. |
---|
| 118 | String. |
---|