1 | include "Universes.ma". |
---|
2 | include "Plogic/equality.ma". |
---|
3 | include "Connectives.ma". |
---|
4 | include "Nat.ma". |
---|
5 | include "Exponential.ma". |
---|
6 | include "Bool.ma". |
---|
7 | include "BitVector.ma". |
---|
8 | include "List.ma". |
---|
9 | |
---|
10 | ndefinition one ≝ S Z. |
---|
11 | ndefinition two ≝ (S(S(Z))). |
---|
12 | ndefinition three ≝ two + one. |
---|
13 | ndefinition four ≝ two + two. |
---|
14 | ndefinition five ≝ three + two. |
---|
15 | ndefinition six ≝ three + three. |
---|
16 | ndefinition seven ≝ three + four. |
---|
17 | ndefinition eight ≝ four + four. |
---|
18 | ndefinition nine ≝ five + four. |
---|
19 | ndefinition ten ≝ five + five. |
---|
20 | ndefinition eleven ≝ six + five. |
---|
21 | ndefinition twelve ≝ six + six. |
---|
22 | ndefinition thirteen ≝ seven + six. |
---|
23 | ndefinition fourteen ≝ seven + seven. |
---|
24 | ndefinition fifteen ≝ eight + seven. |
---|
25 | ndefinition sixteen ≝ eight + eight. |
---|
26 | ndefinition seventeen ≝ nine + eight. |
---|
27 | ndefinition eighteen ≝ nine + nine. |
---|
28 | ndefinition nineteen ≝ ten + nine. |
---|
29 | ndefinition one_hundred_and_twenty_eight ≝ sixteen * eight. |
---|
30 | ndefinition two_hundred_and_fifty_six ≝ |
---|
31 | one_hundred_and_twenty_eight + one_hundred_and_twenty_eight. |
---|
32 | |
---|
33 | ndefinition nat_of_bool ≝ |
---|
34 | λb: Bool. |
---|
35 | match b with |
---|
36 | [ false ⇒ Z |
---|
37 | | true ⇒ S Z |
---|
38 | ]. |
---|
39 | |
---|
40 | ndefinition add_n_with_carry: |
---|
41 | ∀n: Nat. ∀b, c: BitVector n. ∀carry: Bool. Cartesian (BitVector n) (List Bool) ≝ |
---|
42 | λn: Nat. |
---|
43 | λb: BitVector n. |
---|
44 | λc: BitVector n. |
---|
45 | λcarry: Bool. |
---|
46 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
47 | let c_as_nat ≝ nat_of_bitvector n c in |
---|
48 | let carry_as_nat ≝ nat_of_bool carry in |
---|
49 | let result_old ≝ b_as_nat + c_as_nat + carry_as_nat in |
---|
50 | let ac_flag ≝ ((modulus b_as_nat ((S (S Z)) * n)) + |
---|
51 | (modulus c_as_nat ((S (S Z)) * n)) + |
---|
52 | c_as_nat) ≥ ((S (S Z)) * n) in |
---|
53 | let bit_xxx ≝ (((modulus b_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
54 | (modulus c_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
55 | c_as_nat) ≥ ((S (S Z))^(n - (S Z)))) in |
---|
56 | let result ≝ modulus result_old ((S (S Z))^n) in |
---|
57 | let cy_flag ≝ (result_old ≥ ((S (S Z))^n)) in |
---|
58 | let ov_flag ≝ exclusive_disjunction cy_flag bit_xxx in |
---|
59 | ? (mk_Cartesian (BitVector n) ? (? (bitvector_of_nat n result)) |
---|
60 | (cy_flag :: ac_flag :: ov_flag :: Empty Bool)). |
---|
61 | #H; nassumption; |
---|
62 | nqed. |
---|
63 | |
---|
64 | naxiom less_than_or_equal_b: Nat → Nat → Bool. |
---|
65 | naxiom ior: Bool → Bool → Bool. |
---|
66 | naxiom neg: Bool → Bool. |
---|
67 | naxiom con: Bool → Bool → Bool. |
---|
68 | naxiom eq: Nat → Nat → Bool. |
---|
69 | |
---|
70 | ndefinition sub_8_with_carry: ∀b,c: BitVector eight. ∀carry: Bool. Cartesian (BitVector eight) (List Bool) ≝ |
---|
71 | λb: BitVector eight. |
---|
72 | λc: BitVector eight. |
---|
73 | λcarry: Bool. |
---|
74 | let b_as_nat ≝ nat_of_bitvector eight b in |
---|
75 | let c_as_nat ≝ nat_of_bitvector eight c in |
---|
76 | let carry_as_nat ≝ nat_of_bool carry in |
---|
77 | let temporary ≝ minus (modulus b_as_nat sixteen) (modulus c_as_nat sixteen) in |
---|
78 | let ac_flag ≝ ior |
---|
79 | (neg (con (less_than_or_equal_b (modulus b_as_nat sixteen) (modulus c_as_nat sixteen)) (less_than_or_equal_b temporary carry_as_nat))) |
---|
80 | (con (con (eq b_as_nat Z) (eq c_as_nat Z)) (eq carry_as_nat Z)) in |
---|
81 | let bit_six ≝ ior |
---|
82 | (neg (con (less_than_or_equal_b (modulus b_as_nat one_hundred_and_twenty_eight) (modulus c_as_nat one_hundred_and_twenty_eight)) (less_than_or_equal_b temporary carry_as_nat))) |
---|
83 | (con (con (eq b_as_nat Z) (eq c_as_nat Z)) (eq carry_as_nat Z)) in |
---|
84 | let old_result_1 ≝ b_as_nat - c_as_nat in |
---|
85 | let old_result_2 ≝ old_result_1 - carry_as_nat in |
---|
86 | let ov_flag ≝ exclusive_disjunction carry bit_six in |
---|
87 | match less_than_or_equal_b b_as_nat c_as_nat with |
---|
88 | [ true ⇒ |
---|
89 | match less_than_or_equal_b old_result_1 carry_as_nat with |
---|
90 | [ true ⇒ |
---|
91 | let cy_flag ≝ false in |
---|
92 | mk_Cartesian … (bitvector_of_nat eight old_result_2) (cy_flag :: ac_flag :: ov_flag :: (Empty Bool)) |
---|
93 | | false ⇒ |
---|
94 | let cy_flag ≝ true in |
---|
95 | let new_result ≝ b_as_nat + two_hundred_and_fifty_six - c_as_nat - carry_as_nat in |
---|
96 | mk_Cartesian … (bitvector_of_nat eight new_result) (cy_flag :: ac_flag :: ov_flag :: (Empty Bool)) |
---|
97 | ] |
---|
98 | | false ⇒ |
---|
99 | let cy_flag ≝ true in |
---|
100 | let new_result ≝ b_as_nat + two_hundred_and_fifty_six - c_as_nat - carry_as_nat in |
---|
101 | mk_Cartesian … (bitvector_of_nat eight new_result) (cy_flag :: ac_flag :: ov_flag :: (Empty Bool)) |
---|
102 | ]. |
---|
103 | |
---|
104 | ndefinition add_8_with_carry ≝ add_n_with_carry eight. |
---|
105 | ndefinition add_16_with_carry ≝ add_n_with_carry sixteen. |
---|
106 | |
---|
107 | ndefinition increment ≝ |
---|
108 | λn: Nat. |
---|
109 | λb: BitVector n. |
---|
110 | let b_as_nat ≝ (nat_of_bitvector n b) + (S Z) in |
---|
111 | let overflow ≝ b_as_nat ≥ (S (S Z))^n in |
---|
112 | match overflow with |
---|
113 | [ false ⇒ bitvector_of_nat n b_as_nat |
---|
114 | | true ⇒ zero n |
---|
115 | ]. |
---|
116 | |
---|
117 | ndefinition decrement ≝ |
---|
118 | λn: Nat. |
---|
119 | λb: BitVector n. |
---|
120 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
121 | match b_as_nat with |
---|
122 | [ Z ⇒ max n |
---|
123 | | S o ⇒ bitvector_of_nat n o |
---|
124 | ]. |
---|
125 | |
---|
126 | alias symbol "greater_than_or_equal" (instance 1) = "Nat greater than or equal prop". |
---|
127 | |
---|
128 | ndefinition bitvector_of_bool: |
---|
129 | ∀n: Nat. ∀b: Bool. BitVector n ≝ |
---|
130 | λn: Nat. |
---|
131 | λb: Bool. |
---|
132 | ? (pad (n - (S Z)) (S Z) (Cons Bool ? b (Empty Bool))). |
---|
133 | nrewrite > (plus_minus_inverse_right n ?); |
---|
134 | #H; |
---|
135 | nassumption; |
---|
136 | nqed. |
---|