phanerozoic's picture
Put the organism first and move serialization, sizes and the Rocq map to appendices
5ee1fb5
Raw History Blame Contribute Delete
6.08 kB
(* The subtractor of Lemma 5.3: the carry-lookahead terms are the ripple carries of y + not x + 1,
and the sum bits spell (y - x) mod 2^A. *)
From Stdlib Require Import List Bool Arith Lia.
Import ListNotations.
From TH Require Import TwoHead Net PhysSem.
(* the value of the bits f 0 .. f (n - 1), least significant first *)
Definition bv (f : nat -> bool) (n : nat) : nat := bits2nat (rev (map f (seq 0 n))).
Lemma bv_S : forall f n, bv f (S n) = (if f n then 2 ^ n else 0) + bv f n.
Proof.
intros f n. unfold bv. rewrite seq_S, map_app, rev_app_distr. cbn [map rev app].
rewrite bits2nat_cons, length_rev, length_map, length_seq. reflexivity.
Qed.
Lemma bv_lt : forall f n, bv f n < 2 ^ n.
Proof.
intros f n. unfold bv. pose proof (bits2nat_lt (rev (map f (seq 0 n)))) as H.
rewrite length_rev, length_map, length_seq in H. exact H.
Qed.
Lemma testbit_bv : forall f n k, k < n -> Nat.testbit (bv f n) k = f k.
Proof.
intros f n k Hk. unfold bv.
pose proof (testbit_bits2nat (rev (map f (seq 0 n))) (n - 1 - k)) as T.
rewrite length_rev, length_map, length_seq in T. replace (n - 1 - (n - 1 - k)) with k in T by lia.
rewrite T by lia. rewrite rev_nth by (rewrite length_map, length_seq; lia).
rewrite length_map, length_seq. replace (n - S (n - 1 - k)) with k by lia.
rewrite nth_map_d with (d' := 0) by (rewrite length_seq; lia). rewrite seq_nth by lia. reflexivity.
Qed.
Lemma bv_testbit : forall z n, z < 2 ^ n -> bv (Nat.testbit z) n = z.
Proof.
intros z n Hz. apply (bits_eq_nat _ _ n); [apply bv_lt | exact Hz |].
intros i Hi. apply testbit_bv. exact Hi.
Qed.
Lemma bv_compl : forall f n, bv f n + bv (fun i => negb (f i)) n + 1 = 2 ^ n.
Proof.
intros f n. induction n as [|n IH]; [reflexivity|].
rewrite !bv_S. cbn [Nat.pow]. destruct (f n); cbn [negb]; lia.
Qed.
Section Sub.
Variables x y : nat.
Definition xb (i : nat) : bool := Nat.testbit x i.
Definition yb (i : nat) : bool := Nat.testbit y i.
Definition gb (i : nat) : bool := yb i && negb (xb i).
Definition pg (i : nat) : bool := negb (xb i) || yb i.
Fixpoint carry (k : nat) : bool := match k with O => true | S m => gb m || (pg m && carry m) end.
Definition sumbit (k : nat) : bool := xorb (xorb (yb k) (negb (xb k))) (carry k).
Lemma adder : forall n,
bv yb n + bv (fun i => negb (xb i)) n + 1 = bv sumbit n + (if carry n then 2 ^ n else 0).
Proof.
induction n as [|n IH]; [reflexivity|].
rewrite !bv_S. cbn [carry Nat.pow]. unfold sumbit, gb, pg in *.
destruct (yb n), (xb n), (carry n); cbn [negb xorb andb orb] in *; lia.
Qed.
Lemma sub_value : forall A, x < 2 ^ A -> y < 2 ^ A -> bv sumbit A = (y + 2 ^ A - x) mod 2 ^ A.
Proof.
intros A Hx Hy. pose proof (adder A) as H. pose proof (bv_compl xb A) as C.
change (bv yb A) with (bv (Nat.testbit y) A) in H. rewrite (bv_testbit y A Hy) in H.
change (bv xb A) with (bv (Nat.testbit x) A) in C. rewrite (bv_testbit x A Hx) in C.
pose proof (bv_lt sumbit A) as L.
assert (E : y + 2 ^ A - x = bv sumbit A + (if carry A then 1 else 0) * 2 ^ A)
by (destruct (carry A); lia).
rewrite E, Nat.Div0.mod_add, Nat.mod_small by exact L. reflexivity.
Qed.
Lemma sub_bits : forall A k, x < 2 ^ A -> y < 2 ^ A -> k < A ->
Nat.testbit ((y + 2 ^ A - x) mod 2 ^ A) k = sumbit k.
Proof. intros A k Hx Hy Hk. rewrite <- sub_value by assumption. apply testbit_bv. exact Hk. Qed.
Lemma existsb_and_r : forall (T : Type) (g : T -> bool) c l, existsb (fun a => g a && c) l = existsb g l && c.
Proof.
intros T g c l. induction l as [|a l IH]; [reflexivity|]. cbn [existsb]. rewrite IH.
destruct (g a), c, (existsb g l); reflexivity.
Qed.
Lemma forallb_seq_S : forall (f : nat -> bool) m n, forallb f (seq m (S n)) = forallb f (seq m n) && f (m + n).
Proof. intros f m n. rewrite seq_S, forallb_app. cbn [forallb]. rewrite andb_true_r. reflexivity. Qed.
(* the carry into bit k: all of 0 .. k-1 propagate, or some j generates and j+1 .. k-1 propagate *)
Lemma carry_closed : forall k,
carry k = forallb pg (seq 0 k) || existsb (fun j => gb j && forallb pg (seq (S j) (k - S j))) (seq 0 k).
Proof.
induction k as [|k IH]; [reflexivity|].
cbn [carry]. rewrite IH, forallb_seq_S, seq_S, existsb_app. cbn [existsb]. rewrite Nat.add_0_l.
replace (S k - S k) with 0 by lia. cbn [seq forallb]. rewrite andb_true_r, orb_false_r.
assert (E : existsb (fun j => gb j && forallb pg (seq (S j) (S k - S j))) (seq 0 k)
= existsb (fun j => gb j && forallb pg (seq (S j) (k - S j))) (seq 0 k) && pg k).
{ rewrite (existsb_ext_in _ _ (fun j => (gb j && forallb pg (seq (S j) (k - S j))) && pg k)).
- apply (existsb_and_r _ (fun j => gb j && forallb pg (seq (S j) (k - S j)))).
- intros j Hj. apply in_seq in Hj. replace (S k - S j) with (S (k - S j)) by lia. rewrite forallb_seq_S.
replace (S j + (k - S j)) with k by lia. rewrite andb_assoc. reflexivity. }
rewrite E. destruct (pg k), (gb k); cbn [andb orb]; rewrite ?andb_true_r, ?andb_false_r, ?orb_false_r,
?orb_true_r; reflexivity.
Qed.
End Sub.
Lemma testbit_top : forall r n, 1 <= n -> r < 2 ^ n -> Nat.testbit r (n - 1) = (2 ^ (n - 1) <=? r).
Proof.
intros r n Hn Hr. replace n with (S (n - 1)) in Hr by lia. rewrite Nat.pow_succ_r' in Hr.
apply Nat.b2n_inj. rewrite Nat.testbit_spec'.
destruct (Nat.leb_spec (2 ^ (n - 1)) r) as [H|H].
- assert (D : r / 2 ^ (n - 1) = 1).
{ symmetry. apply Nat.div_unique with (r - 2 ^ (n - 1)); lia. }
rewrite D. reflexivity.
- rewrite Nat.div_small by exact H. reflexivity.
Qed.
Lemma sub_zero : forall A x y, x < 2 ^ A -> y < 2 ^ A -> Nat.eqb ((y + 2 ^ A - x) mod 2 ^ A) 0 = Nat.eqb x y.
Proof.
intros A x y Hx Hy. pose proof (Nat.pow_nonzero 2 A ltac:(lia)) as Hn.
destruct (Nat.eqb_spec x y) as [->|E].
- replace (y + 2 ^ A - y) with (1 * 2 ^ A) by lia. rewrite Nat.Div0.mod_mul. reflexivity.
- apply Nat.eqb_neq. intro Z.
destruct (Nat.le_gt_cases x y) as [L|L].
+ replace (y + 2 ^ A - x) with ((y - x) + 1 * 2 ^ A) in Z by lia.
rewrite Nat.Div0.mod_add, Nat.mod_small in Z by lia. lia.
+ rewrite Nat.mod_small in Z by lia. lia.
Qed.