Debian Codegraph @

src/Util/NUtil/WithoutReferenceToZ.v

raw
1 (** NUtil that doesn't depend on ZUtil stuff *)
2 (** Should probably come up with a better organization of this stuff *)
3 From Coq Require Import NArith PeanoNat.
4 Require Import Crypto.Util.NatUtil Crypto.Util.Decidable.
5
6 Module N.
7 Lemma size_le a b : (a <= b -> N.size a <= N.size b)%N.
8 Proof.
9 destruct (dec (a=0)%N), (dec (b=0)%N); subst; auto using N.le_0_l.
10 { destruct a; auto. }
11 { rewrite !N.size_log2 by assumption.
12 rewrite <-N.succ_le_mono.
13 apply N.log2_le_mono. }
14 Qed.
15
16 Lemma N_le_1_l : forall p, (1 <= N.pos p)%N.
17 Proof.
18 destruct p; cbv; congruence.
19 Qed.
20
21 Lemma Pos_land_upper_bound_l : forall a b, (Pos.land a b <= N.pos a)%N.
22 Proof.
23 induction a as [a IHa|a IHa|]; destruct b as [b|b|]; try solve [cbv; congruence];
24 simpl; specialize (IHa b); case_eq (Pos.land a b); intro p; simpl;
25 try (apply N_le_1_l || apply N.le_0_l); intro land_eq;
26 rewrite land_eq in *; unfold N.le, N.compare in *;
27 rewrite ?Pos.compare_xI_xI, ?Pos.compare_xO_xI, ?Pos.compare_xO_xO;
28 try assumption.
29 destruct (p ?=a)%positive; cbv; congruence.
30 Qed.
31
32 Lemma le_to_nat a b : (a <= b)%N <-> (N.to_nat a <= N.to_nat b)%nat.
33 Proof.
34 rewrite <-N.lt_succ_r.
35 rewrite <-Nat.lt_succ_r.
36 rewrite <-Nnat.N2Nat.inj_succ.
37 rewrite <-NatUtil.Nat2N_inj_lt.
38 rewrite !Nnat.N2Nat.id.
39 reflexivity.
40 Qed.
41
42 Lemma size_nat_equiv : forall n, N.size_nat n = N.to_nat (N.size n).
43 Proof.
44 destruct n as [|p]; auto; simpl; induction p as [p IHp|p IHp|]; simpl; auto; rewrite IHp, Pnat.Pos2Nat.inj_succ; reflexivity.
45 Qed.
46
47 Lemma size_nat_le a b : (a <= b)%N -> (N.size_nat a <= N.size_nat b)%nat.
48 Proof.
49 rewrite !size_nat_equiv.
50 rewrite <-le_to_nat.
51 apply size_le.
52 Qed.
53 End N.