1 | include "Infrastructure/Universes.ma". |
---|
2 | include "Infrastructure/Equality.ma". |
---|
3 | include "Infrastructure/Connectives.ma". |
---|
4 | |
---|
5 | include "Common/Nat/Nat.ma". |
---|
6 | include "Common/Nat/Addition.ma". |
---|
7 | include "Common/Nat/Multiplication.ma". |
---|
8 | include "Common/Nat/Division_Modulus.ma". |
---|
9 | include "Common/Nat/Order.ma". |
---|
10 | |
---|
11 | include "Programming/Datatypes/Bool.ma". |
---|
12 | |
---|
13 | include "Programming/Datatypes/Listlike/Vector/BitVector.ma". |
---|
14 | include "Programming/Datatypes/Listlike/List/List.ma". |
---|
15 | |
---|
16 | ndefinition eight ≝ exponential (S(S(Z))) (S(S(S(Z)))). |
---|
17 | ndefinition sixteen ≝ eight + eight. |
---|
18 | ndefinition one_hundred_and_twenty_eight ≝ sixteen × eight. |
---|
19 | ndefinition two_hundred_and_fifty_six ≝ |
---|
20 | one_hundred_and_twenty_eight + one_hundred_and_twenty_eight. |
---|
21 | |
---|
22 | ndefinition add_n_with_carry: |
---|
23 | ∀n: Nat. ∀b, c: BitVector n. ∀carry: Bool. Cartesian (BitVector n) (List Bool) ≝ |
---|
24 | λn: Nat. |
---|
25 | λb: BitVector n. |
---|
26 | λc: BitVector n. |
---|
27 | λcarry: Bool. |
---|
28 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
29 | let c_as_nat ≝ nat_of_bitvector n c in |
---|
30 | let carry_as_nat ≝ nat_of_bool carry in |
---|
31 | let result_old ≝ b_as_nat + c_as_nat + carry_as_nat in |
---|
32 | let ac_flag ≝ ((modulus b_as_nat ((S (S Z)) × n)) + |
---|
33 | (modulus c_as_nat ((S (S Z)) × n)) + |
---|
34 | c_as_nat) ≥ ((S (S Z)) × n) in |
---|
35 | let bit_xxx ≝ (((modulus b_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
36 | (modulus c_as_nat ((S (S Z))^(n - (S Z)))) + |
---|
37 | c_as_nat) ≥ ((S (S Z))^(n - (S Z)))) in |
---|
38 | let result ≝ modulus result_old ((S (S Z))^n) in |
---|
39 | let cy_flag ≝ (result_old ≥ ((S (S Z))^n)) in |
---|
40 | let ov_flag ≝ exclusive_disjunction cy_flag bit_xxx in |
---|
41 | ? (mk_Cartesian (BitVector n) ? (bitvector_of_nat n result) |
---|
42 | (cy_flag :: ac_flag :: ov_flag :: Empty Bool)). |
---|
43 | //. |
---|
44 | nqed. |
---|
45 | |
---|
46 | (* |
---|
47 | ndefinition sub_8_with_carry ≝ |
---|
48 | λb: BitVector eight. |
---|
49 | λc: BitVector eight. |
---|
50 | λcarry: Bool. |
---|
51 | let b_as_nat ≝ nat_of_bitvector eight b in |
---|
52 | let c_as_nat ≝ nat_of_bitvector eight c in |
---|
53 | let carry_as_nat ≝ nat_of_bool carry in |
---|
54 | let result_old_1 ≝ subtraction_underflow b_as_nat c_as_nat in |
---|
55 | match result_old_1 with |
---|
56 | [ Nothing ⇒ |
---|
57 | let ac_flag ≝ True in |
---|
58 | |
---|
59 | | Just result_old_1' ⇒ |
---|
60 | ] |
---|
61 | *) |
---|
62 | |
---|
63 | ndefinition add_8_with_carry ≝ add_n_with_carry eight. |
---|
64 | ndefinition add_16_with_carry ≝ add_n_with_carry sixteen. |
---|
65 | |
---|
66 | ndefinition increment ≝ |
---|
67 | λn: Nat. |
---|
68 | λb: BitVector n. |
---|
69 | let b_as_nat ≝ (nat_of_bitvector n b) + (S Z) in |
---|
70 | let overflow ≝ b_as_nat ≥ (S (S Z))^n in |
---|
71 | match overflow with |
---|
72 | [ True ⇒ bitvector_of_nat n Z |
---|
73 | | False ⇒ bitvector_of_nat n b_as_nat |
---|
74 | ]. |
---|
75 | |
---|
76 | ndefinition decrement ≝ |
---|
77 | λn: Nat. |
---|
78 | λb: BitVector n. |
---|
79 | let b_as_nat ≝ nat_of_bitvector n b in |
---|
80 | match b_as_nat with |
---|
81 | [ Z ⇒ max n |
---|
82 | | S o ⇒ bitvector_of_nat n o |
---|
83 | ]. |
---|
84 | |
---|
85 | alias symbol "greater_than_or_equal" (instance 1) = "Nat greater than or equal prop". |
---|
86 | |
---|
87 | ndefinition bitvector_of_bool: |
---|
88 | ∀n: Nat. ∀b: Bool. BitVector n ≝ |
---|
89 | λn: Nat. |
---|
90 | λb: Bool. |
---|
91 | ? (pad (n - (S Z)) (S Z) (Cons Bool ? b (Empty Bool))). |
---|
92 | //. |
---|
93 | nqed. |
---|