1 | include "Exponential.ma". |
---|
2 | include "BitVector.ma". |
---|
3 | |
---|
4 | ndefinition nat_of_bool ≝ |
---|
5 | λb: Bool. |
---|
6 | match b with |
---|
7 | [ false ⇒ Z |
---|
8 | | true ⇒ S Z |
---|
9 | ]. |
---|
10 | |
---|
11 | ndefinition add_n_with_carry: |
---|
12 | ∀n: Nat. ∀b, c: BitVector n. ∀carry: Bool. Cartesian (BitVector n) (List Bool) ≝ |
---|
13 | λn: Nat. |
---|
14 | λb: BitVector n. |
---|
15 | λc: BitVector n. |
---|
16 | λcarry: Bool. |
---|
17 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
18 | let c_as_nat ≝ nat_of_bitvector n c in |
---|
19 | let carry_as_nat ≝ nat_of_bool carry in |
---|
20 | let result_old ≝ b_as_nat + c_as_nat + carry_as_nat in |
---|
21 | let ac_flag ≝ ((modulus b_as_nat ((S (S Z)) * n)) + |
---|
22 | (modulus c_as_nat ((S (S Z)) * n)) + |
---|
23 | c_as_nat) ≳ ((S (S Z)) * n) in |
---|
24 | let bit_xxx ≝ (((modulus b_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
25 | (modulus c_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
26 | c_as_nat) ≳ ((S (S Z))^(n - (S Z)))) in |
---|
27 | let result ≝ modulus result_old ((S (S Z))^n) in |
---|
28 | let cy_flag ≝ (result_old ≳ ((S (S Z))^n)) in |
---|
29 | let ov_flag ≝ exclusive_disjunction cy_flag bit_xxx in |
---|
30 | ? (mk_Cartesian (BitVector n) ? (? (bitvector_of_nat n result)) |
---|
31 | (cy_flag :: ac_flag :: ov_flag :: Empty Bool)). |
---|
32 | #H; nassumption; |
---|
33 | nqed. |
---|
34 | |
---|
35 | ndefinition sub_8_with_carry: ∀b,c: BitVector eight. ∀carry: Bool. Cartesian (BitVector eight) (List Bool) ≝ |
---|
36 | λb: BitVector eight. |
---|
37 | λc: BitVector eight. |
---|
38 | λcarry: Bool. |
---|
39 | let b_as_nat ≝ nat_of_bitvector eight b in |
---|
40 | let c_as_nat ≝ nat_of_bitvector eight c in |
---|
41 | let carry_as_nat ≝ nat_of_bool carry in |
---|
42 | let temporary ≝ b_as_nat mod sixteen - c_as_nat mod sixteen in |
---|
43 | let ac_flag ≝ negation (conjunction ((b_as_nat mod sixteen) ≲ (c_as_nat mod sixteen)) (temporary ≲ carry_as_nat)) in |
---|
44 | let bit_six ≝ negation (conjunction ((b_as_nat mod one_hundred_and_twenty_eight) ≲ (c_as_nat mod one_hundred_and_twenty_eight)) (temporary ≲ carry_as_nat)) in |
---|
45 | let old_result_1 ≝ b_as_nat - c_as_nat in |
---|
46 | let old_result_2 ≝ old_result_1 - carry_as_nat in |
---|
47 | let ov_flag ≝ exclusive_disjunction carry bit_six in |
---|
48 | match conjunction (b_as_nat ≲ c_as_nat) (old_result_1 ≲ carry_as_nat) with |
---|
49 | [ false ⇒ |
---|
50 | let cy_flag ≝ false in |
---|
51 | 〈 bitvector_of_nat eight old_result_2, [cy_flag ; ac_flag ; ov_flag ] 〉 |
---|
52 | | true ⇒ |
---|
53 | let cy_flag ≝ true in |
---|
54 | let new_result ≝ b_as_nat + two_hundred_and_fifty_six - c_as_nat - carry_as_nat in |
---|
55 | 〈 bitvector_of_nat eight new_result, [ cy_flag ; ac_flag ; ov_flag ] 〉 |
---|
56 | ]. |
---|
57 | |
---|
58 | ndefinition add_8_with_carry ≝ add_n_with_carry eight. |
---|
59 | ndefinition add_16_with_carry ≝ add_n_with_carry sixteen. |
---|
60 | |
---|
61 | ndefinition increment ≝ |
---|
62 | λn: Nat. |
---|
63 | λb: BitVector n. |
---|
64 | let b_as_nat ≝ (nat_of_bitvector n b) + (S Z) in |
---|
65 | let overflow ≝ b_as_nat ≳ (S (S Z))^n in |
---|
66 | match overflow with |
---|
67 | [ false ⇒ bitvector_of_nat n b_as_nat |
---|
68 | | true ⇒ zero n |
---|
69 | ]. |
---|
70 | |
---|
71 | ndefinition decrement ≝ |
---|
72 | λn: Nat. |
---|
73 | λb: BitVector n. |
---|
74 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
75 | match b_as_nat with |
---|
76 | [ Z ⇒ max n |
---|
77 | | S o ⇒ bitvector_of_nat n o |
---|
78 | ]. |
---|
79 | |
---|
80 | alias symbol "greater_than_or_equal" (instance 1) = "Nat greater than or equal prop". |
---|
81 | |
---|
82 | ndefinition bitvector_of_bool: |
---|
83 | ∀n: Nat. ∀b: Bool. BitVector (S n) ≝ |
---|
84 | λn: Nat. |
---|
85 | λb: Bool. |
---|
86 | ? (pad (S n - (S Z)) (S Z) (Cons Bool ? b (Empty Bool))). |
---|
87 | nrewrite > plus_minus_inverse_right |
---|
88 | [ napply (λx.x) | /2/] |
---|
89 | nqed. |
---|
90 | |
---|
91 | ndefinition full_add ≝ |
---|
92 | λn: Nat. |
---|
93 | λb, c: BitVector n. |
---|
94 | λd: Bit. |
---|
95 | fold_right2_i ? ? ? ( |
---|
96 | λn. |
---|
97 | λb1, b2: Bool. |
---|
98 | λd: Bit × (BitVector n). |
---|
99 | let 〈c1,r〉 ≝ d in |
---|
100 | 〈inclusive_disjunction (conjunction b1 b2) |
---|
101 | (conjunction c1 (inclusive_disjunction b1 b2)), |
---|
102 | (exclusive_disjunction (exclusive_disjunction b1 b2) c1) ::: r〉) |
---|
103 | 〈d, [[ ]]〉 ? b c. |
---|
104 | |
---|
105 | ndefinition half_add ≝ |
---|
106 | λn: Nat. |
---|
107 | λb, c: BitVector n. |
---|
108 | full_add n b c false. |
---|
109 | |
---|