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) (BitVector three) ≝ |
---|
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_of_nat n result) |
---|
31 | ([[ cy_flag ; ac_flag ; ov_flag ]]). |
---|
32 | |
---|
33 | ndefinition sub_8_with_carry: ∀b,c: BitVector eight. ∀carry: Bool. Cartesian (BitVector eight) (BitVector three) ≝ |
---|
34 | λb: BitVector eight. |
---|
35 | λc: BitVector eight. |
---|
36 | λcarry: Bool. |
---|
37 | let b_as_nat ≝ nat_of_bitvector eight b in |
---|
38 | let c_as_nat ≝ nat_of_bitvector eight c in |
---|
39 | let carry_as_nat ≝ nat_of_bool carry in |
---|
40 | let temporary ≝ b_as_nat mod sixteen - c_as_nat mod sixteen in |
---|
41 | let ac_flag ≝ negation (conjunction ((b_as_nat mod sixteen) ≲ (c_as_nat mod sixteen)) (temporary ≲ carry_as_nat)) in |
---|
42 | 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 |
---|
43 | let old_result_1 ≝ b_as_nat - c_as_nat in |
---|
44 | let old_result_2 ≝ old_result_1 - carry_as_nat in |
---|
45 | let ov_flag ≝ exclusive_disjunction carry bit_six in |
---|
46 | match conjunction (b_as_nat ≲ c_as_nat) (old_result_1 ≲ carry_as_nat) with |
---|
47 | [ false ⇒ |
---|
48 | let cy_flag ≝ false in |
---|
49 | 〈 bitvector_of_nat eight old_result_2, [[ cy_flag ; ac_flag ; ov_flag ]]〉 |
---|
50 | | true ⇒ |
---|
51 | let cy_flag ≝ true in |
---|
52 | let new_result ≝ b_as_nat + two_hundred_and_fifty_six - c_as_nat - carry_as_nat in |
---|
53 | 〈 bitvector_of_nat eight new_result, [[ cy_flag ; ac_flag ; ov_flag ]]〉 |
---|
54 | ]. |
---|
55 | |
---|
56 | ndefinition add_8_with_carry ≝ add_n_with_carry eight. |
---|
57 | ndefinition add_16_with_carry ≝ add_n_with_carry sixteen. |
---|
58 | |
---|
59 | ndefinition increment ≝ |
---|
60 | λn: Nat. |
---|
61 | λb: BitVector n. |
---|
62 | let b_as_nat ≝ (nat_of_bitvector n b) + (S Z) in |
---|
63 | let overflow ≝ b_as_nat ≳ (S (S Z))^n in |
---|
64 | match overflow with |
---|
65 | [ false ⇒ bitvector_of_nat n b_as_nat |
---|
66 | | true ⇒ zero n |
---|
67 | ]. |
---|
68 | |
---|
69 | ndefinition decrement ≝ |
---|
70 | λn: Nat. |
---|
71 | λb: BitVector n. |
---|
72 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
73 | match b_as_nat with |
---|
74 | [ Z ⇒ max n |
---|
75 | | S o ⇒ bitvector_of_nat n o |
---|
76 | ]. |
---|
77 | |
---|
78 | alias symbol "greater_than_or_equal" (instance 1) = "Nat greater than or equal prop". |
---|
79 | |
---|
80 | ndefinition bitvector_of_bool: |
---|
81 | ∀n: Nat. ∀b: Bool. BitVector (S n) ≝ |
---|
82 | λn: Nat. |
---|
83 | λb: Bool. |
---|
84 | ? (pad (S n - (S Z)) (S Z) (Cons Bool ? b (Empty Bool))). |
---|
85 | nrewrite > plus_minus_inverse_right |
---|
86 | [ napply (λx.x) | /2/] |
---|
87 | nqed. |
---|
88 | |
---|
89 | ndefinition full_add ≝ |
---|
90 | λn: Nat. |
---|
91 | λb, c: BitVector n. |
---|
92 | λd: Bit. |
---|
93 | fold_right2_i ? ? ? ( |
---|
94 | λn. |
---|
95 | λb1, b2: Bool. |
---|
96 | λd: Bit × (BitVector n). |
---|
97 | let 〈c1,r〉 ≝ d in |
---|
98 | 〈inclusive_disjunction (conjunction b1 b2) |
---|
99 | (conjunction c1 (inclusive_disjunction b1 b2)), |
---|
100 | (exclusive_disjunction (exclusive_disjunction b1 b2) c1) ::: r〉) |
---|
101 | 〈d, [[ ]]〉 ? b c. |
---|
102 | |
---|
103 | ndefinition half_add ≝ |
---|
104 | λn: Nat. |
---|
105 | λb, c: BitVector n. |
---|
106 | full_add n b c false. |
---|
107 | |
---|