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 ≝ ten * ten. |
---|
30 | ndefinition one_hundred_and_twenty_eight ≝ sixteen * eight. |
---|
31 | ndefinition one_hundred_and_twenty_nine ≝ one_hundred_and_twenty_eight + one. |
---|
32 | ndefinition one_hundred_and_thirty ≝ one_hundred_and_twenty_nine + one. |
---|
33 | ndefinition one_hundred_and_thirty_one ≝ one_hundred_and_thirty + one. |
---|
34 | ndefinition one_hundred_and_thirty_five ≝ one_hundred_and_twenty_eight + seven. |
---|
35 | ndefinition one_hundred_and_thirty_six ≝ one_hundred_and_thirty_five + one. |
---|
36 | ndefinition one_hundred_and_thirty_seven ≝ one_hundred_and_thirty_six + one. |
---|
37 | ndefinition one_hundred_and_thirty_eight ≝ one_hundred_and_twenty_eight + ten. |
---|
38 | ndefinition one_hundred_and_thirty_nine ≝ one_hundred_and_thirty_eight + one. |
---|
39 | ndefinition one_hundred_and_forty ≝ one_hundred_and_thirty_nine + one. |
---|
40 | ndefinition one_hundred_and_forty_one ≝ one_hundred_and_forty + one. |
---|
41 | ndefinition one_hundred_and_forty_four ≝ one_hundred_and_twenty_eight + sixteen. |
---|
42 | ndefinition one_hundred_and_fifty_two ≝ one_hundred_and_forty_four + eight. |
---|
43 | ndefinition one_hundred_and_fifty_three ≝ one_hundred_and_forty_four + nine. |
---|
44 | ndefinition one_hundred_and_sixty ≝ one_hundred_and_forty_four + sixteen. |
---|
45 | ndefinition one_hundred_and_sixty_eight ≝ one_hundred_and_sixty + eight. |
---|
46 | ndefinition one_hundred_and_seventy_six ≝ one_hundred_and_sixty + sixteen. |
---|
47 | ndefinition one_hundred_and_eighty_four ≝ one_hundred_and_seventy_six + eight. |
---|
48 | ndefinition two_hundred ≝ one_hundred + one_hundred. |
---|
49 | ndefinition two_hundred_and_two ≝ two_hundred + two. |
---|
50 | ndefinition two_hundred_and_three ≝ two_hundred_and_two + one. |
---|
51 | ndefinition two_hundred_and_four ≝ two_hundred_and_three + one. |
---|
52 | ndefinition two_hundred_and_five ≝ two_hundred_and_four + one. |
---|
53 | ndefinition two_hundred_and_eight ≝ two_hundred_and_five + three. |
---|
54 | ndefinition two_hundred_and_twenty_four ≝ two_hundred_and_eight + sixteen. |
---|
55 | ndefinition two_hundred_and_forty ≝ two_hundred_and_twenty_four + sixteen. |
---|
56 | ndefinition two_hundred_and_fifty_six ≝ |
---|
57 | one_hundred_and_twenty_eight + one_hundred_and_twenty_eight. |
---|
58 | |
---|
59 | ndefinition nat_of_bool ≝ |
---|
60 | λb: Bool. |
---|
61 | match b with |
---|
62 | [ false ⇒ Z |
---|
63 | | true ⇒ S Z |
---|
64 | ]. |
---|
65 | |
---|
66 | ndefinition add_n_with_carry: |
---|
67 | ∀n: Nat. ∀b, c: BitVector n. ∀carry: Bool. Cartesian (BitVector n) (List Bool) ≝ |
---|
68 | λn: Nat. |
---|
69 | λb: BitVector n. |
---|
70 | λc: BitVector n. |
---|
71 | λcarry: Bool. |
---|
72 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
73 | let c_as_nat ≝ nat_of_bitvector n c in |
---|
74 | let carry_as_nat ≝ nat_of_bool carry in |
---|
75 | let result_old ≝ b_as_nat + c_as_nat + carry_as_nat in |
---|
76 | let ac_flag ≝ ((modulus b_as_nat ((S (S Z)) * n)) + |
---|
77 | (modulus c_as_nat ((S (S Z)) * n)) + |
---|
78 | c_as_nat) ≥ ((S (S Z)) * n) in |
---|
79 | let bit_xxx ≝ (((modulus b_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
80 | (modulus c_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
81 | c_as_nat) ≥ ((S (S Z))^(n - (S Z)))) in |
---|
82 | let result ≝ modulus result_old ((S (S Z))^n) in |
---|
83 | let cy_flag ≝ (result_old ≥ ((S (S Z))^n)) in |
---|
84 | let ov_flag ≝ exclusive_disjunction cy_flag bit_xxx in |
---|
85 | ? (mk_Cartesian (BitVector n) ? (? (bitvector_of_nat n result)) |
---|
86 | (cy_flag :: ac_flag :: ov_flag :: Empty Bool)). |
---|
87 | #H; nassumption; |
---|
88 | nqed. |
---|
89 | |
---|
90 | ndefinition sub_8_with_carry: ∀b,c: BitVector eight. ∀carry: Bool. Cartesian (BitVector eight) (List Bool) ≝ |
---|
91 | λb: BitVector eight. |
---|
92 | λc: BitVector eight. |
---|
93 | λcarry: Bool. |
---|
94 | let b_as_nat ≝ nat_of_bitvector eight b in |
---|
95 | let c_as_nat ≝ nat_of_bitvector eight c in |
---|
96 | let carry_as_nat ≝ nat_of_bool carry in |
---|
97 | let temporary ≝ b_as_nat mod sixteen - c_as_nat mod sixteen in |
---|
98 | let ac_flag ≝ negation (conjunction ((b_as_nat mod sixteen) ≤ (c_as_nat mod sixteen)) (temporary ≤ carry_as_nat)) in |
---|
99 | 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 |
---|
100 | let old_result_1 ≝ b_as_nat - c_as_nat in |
---|
101 | let old_result_2 ≝ old_result_1 - carry_as_nat in |
---|
102 | let ov_flag ≝ exclusive_disjunction carry bit_six in |
---|
103 | match conjunction (b_as_nat ≤ c_as_nat) (old_result_1 ≤ carry_as_nat) with |
---|
104 | [ false ⇒ |
---|
105 | let cy_flag ≝ false in |
---|
106 | 〈 bitvector_of_nat eight old_result_2, [cy_flag ; ac_flag ; ov_flag ] 〉 |
---|
107 | | true ⇒ |
---|
108 | let cy_flag ≝ true in |
---|
109 | let new_result ≝ b_as_nat + two_hundred_and_fifty_six - c_as_nat - carry_as_nat in |
---|
110 | 〈 bitvector_of_nat eight new_result, [ cy_flag ; ac_flag ; ov_flag ] 〉 |
---|
111 | ]. |
---|
112 | |
---|
113 | ndefinition add_8_with_carry ≝ add_n_with_carry eight. |
---|
114 | ndefinition add_16_with_carry ≝ add_n_with_carry sixteen. |
---|
115 | |
---|
116 | ndefinition increment ≝ |
---|
117 | λn: Nat. |
---|
118 | λb: BitVector n. |
---|
119 | let b_as_nat ≝ (nat_of_bitvector n b) + (S Z) in |
---|
120 | let overflow ≝ b_as_nat ≥ (S (S Z))^n in |
---|
121 | match overflow with |
---|
122 | [ false ⇒ bitvector_of_nat n b_as_nat |
---|
123 | | true ⇒ zero n |
---|
124 | ]. |
---|
125 | |
---|
126 | ndefinition decrement ≝ |
---|
127 | λn: Nat. |
---|
128 | λb: BitVector n. |
---|
129 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
130 | match b_as_nat with |
---|
131 | [ Z ⇒ max n |
---|
132 | | S o ⇒ bitvector_of_nat n o |
---|
133 | ]. |
---|
134 | |
---|
135 | alias symbol "greater_than_or_equal" (instance 1) = "Nat greater than or equal prop". |
---|
136 | |
---|
137 | ndefinition bitvector_of_bool: |
---|
138 | ∀n: Nat. ∀b: Bool. BitVector n ≝ |
---|
139 | λn: Nat. |
---|
140 | λb: Bool. |
---|
141 | ? (pad (n - (S Z)) (S Z) (Cons Bool ? b (Empty Bool))). |
---|
142 | nrewrite > (plus_minus_inverse_right n ?); |
---|
143 | #H; |
---|
144 | nassumption; |
---|
145 | nqed. |
---|