Debian Codegraph @

src/Util/ZUtil/Z2Nat.v

raw
1 From Coq Require Import ZArith Lia.
2
3 Local Open Scope Z_scope.
4 Module Z2Nat.
5 Definition inj_nonpos n : n <= 0 -> Z.to_nat n = 0%nat.
6 Proof.
7 destruct n; try reflexivity; lia.
8 Qed.
9 End Z2Nat.
10
11 Module Z.
12 Lemma pos_pow_nat_pos : forall x n,
13 Z.pos x ^ Z.of_nat n > 0.
14 Proof. intros; apply Z.lt_gt, Z.pow_pos_nonneg; lia. Qed.
15
16 Lemma pow_Z2N_Zpow : forall a n, 0 <= a ->
17 ((Z.to_nat a) ^ n = Z.to_nat (a ^ Z.of_nat n)%Z)%nat.
18 Proof.
19 intros a n H; induction n as [|n IHn]; try reflexivity.
20 rewrite Nat2Z.inj_succ.
21 rewrite Nat.pow_succ_r by apply le_0_n.
22 rewrite Z.pow_succ_r by apply Zle_0_nat.
23 rewrite IHn.
24 rewrite Z2Nat.inj_mul; auto using Z.pow_nonneg.
25 Qed.
26
27 Lemma pow_Zpow : forall a n : nat, Z.of_nat (a ^ n) = Z.of_nat a ^ Z.of_nat n.
28 Proof with auto using Zle_0_nat, Z.pow_nonneg.
29 intros; apply Z2Nat.inj...
30 rewrite <- pow_Z2N_Zpow, !Nat2Z.id...
31 Qed.
32 #[global]
33 Hint Rewrite pow_Zpow : push_Zof_nat.
34 #[global]
35 Hint Rewrite <- pow_Zpow : pull_Zof_nat.
36
37 Lemma Zpow_sub_1_nat_pow a v
38 : (Z.pos a^Z.of_nat v - 1 = Z.of_nat (Z.to_nat (Z.pos a)^v - 1))%Z.
39 Proof.
40 rewrite <- (Z2Nat.id (Z.pos a)) at 1 by lia.
41 change 2%Z with (Z.of_nat 2); change 1%Z with (Z.of_nat 1);
42 autorewrite with pull_Zof_nat.
43 rewrite Nat2Z.inj_sub
44 by (change 1%nat with (Z.to_nat (Z.pos a)^0)%nat; apply Nat.pow_le_mono_r; simpl; lia).
45 reflexivity.
46 Qed.
47 #[global]
48 Hint Rewrite Zpow_sub_1_nat_pow : pull_Zof_nat.
49 #[global]
50 Hint Rewrite <- Zpow_sub_1_nat_pow : push_Zof_nat.
51 End Z.