# source:Deliverables/D4.1/Matita/Arithmetic.ma@369

Last change on this file since 369 was 359, checked in by mulligan, 10 years ago

add_n_with_carry and sub_n_with_carry now both return bitvectors of length three for holding the flags instead of lists.

File size: 3.9 KB
Line
1include "Exponential.ma".
2include "BitVector.ma".
3
4ndefinition nat_of_bool ≝
5  λb: Bool.
6    match b with
7      [ false ⇒ Z
8      | true ⇒ S Z
9      ].
10
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
33ndefinition 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
58
59ndefinition 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
69ndefinition 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
78alias symbol "greater_than_or_equal" (instance 1) = "Nat greater than or equal prop".
79
80ndefinition 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/]
87nqed.
88
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