Skip to content

Commit 6f55010

Browse files
committed
Subtraction in NatInt
This is the revival of an old PR in former repository. We add two axioms in NatInt to restrict the models to exactly the integers and the natural numbers (with `pred 0 = 0`). This allows us to prove lemmas such as `sub_succ` and then prove many properties of `sub` which are shared between the natural numbers and the integers. The Natural and Integer parts of Numbers are modified in consequence. The result should be completely compatible except for `mul_sub_distr_l` which had different variable names in Integers and Natural (we chose to keep it as it was in Integers). We also remove references to old NZAxiomsSig modules. This needs more polishing, so still a draft: - the case of `mul_sub_distr_l` should be decided - removing some `Private_` lemmas is possible (e.g. with `Let`), this would result in a cleaner PR - check exactly the before/after for a user importing PeanoNat, etc
1 parent c33eae9 commit 6f55010

File tree

15 files changed

+515
-489
lines changed

15 files changed

+515
-489
lines changed

theories/Numbers/Cyclic/Abstract/NZCyclic.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(************************************************************************)
2-
(* * The Rocq Prover / The Rocq Development Team *)
2+
(* * The Coq Proof Assistant / The Coq Development Team *)
33
(* v * Copyright INRIA, CNRS and contributors *)
44
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
55
(* \VV/ **************************************************************)
@@ -25,7 +25,7 @@ From Stdlib Require Import Lia.
2525
a power of 2.
2626
*)
2727

28-
Module NZCyclicAxiomsMod (Import Cyclic : CyclicType) <: NZAxiomsSig.
28+
Module NZCyclicAxiomsMod (Import Cyclic : CyclicType) <: NZBasicFunsSig.
2929

3030
Local Open Scope Z_scope.
3131

theories/Numbers/Integer/Abstract/ZAdd.v

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(************************************************************************)
2-
(* * The Rocq Prover / The Rocq Development Team *)
2+
(* * The Coq Proof Assistant / The Coq Development Team *)
33
(* v * Copyright INRIA, CNRS and contributors *)
44
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
55
(* \VV/ **************************************************************)
@@ -66,13 +66,6 @@ rewrite <- (succ_pred n) at 2.
6666
rewrite opp_succ. now rewrite succ_pred.
6767
Qed.
6868

69-
Theorem sub_diag n : n - n == 0.
70-
Proof.
71-
nzinduct n.
72-
- now nzsimpl.
73-
- intro n. rewrite sub_succ_r, sub_succ_l; now rewrite pred_succ.
74-
Qed.
75-
7669
Theorem add_opp_diag_l n : - n + n == 0.
7770
Proof.
7871
now rewrite add_comm, add_opp_r, sub_diag.
@@ -134,12 +127,6 @@ Proof.
134127
symmetry; apply eq_opp_l.
135128
Qed.
136129

137-
Theorem sub_add_distr n m p : n - (m + p) == (n - m) - p.
138-
Proof.
139-
rewrite <- add_opp_r, opp_add_distr, add_assoc.
140-
now rewrite 2 add_opp_r.
141-
Qed.
142-
143130
Theorem sub_sub_distr n m p : n - (m - p) == (n - m) + p.
144131
Proof.
145132
rewrite <- add_opp_r, opp_sub_distr, add_assoc.
@@ -230,16 +217,6 @@ Qed.
230217
terms. The name includes the first operator and the position of
231218
the term being canceled. *)
232219

233-
Theorem add_simpl_l n m : n + m - n == m.
234-
Proof.
235-
now rewrite add_sub_swap, sub_diag, add_0_l.
236-
Qed.
237-
238-
Theorem add_simpl_r n m : n + m - m == n.
239-
Proof.
240-
now rewrite <- add_sub_assoc, sub_diag, add_0_r.
241-
Qed.
242-
243220
Theorem sub_simpl_l n m : - n - m + n == - m.
244221
Proof.
245222
now rewrite <- add_sub_swap, add_opp_diag_l, sub_0_l.
@@ -258,27 +235,6 @@ Qed.
258235
(** Now we have two sums or differences; the name includes the two
259236
operators and the position of the terms being canceled *)
260237

261-
Theorem add_add_simpl_l_l n m p : (n + m) - (n + p) == m - p.
262-
Proof.
263-
now rewrite (add_comm n m), <- add_sub_assoc,
264-
sub_add_distr, sub_diag, sub_0_l, add_opp_r.
265-
Qed.
266-
267-
Theorem add_add_simpl_l_r n m p : (n + m) - (p + n) == m - p.
268-
Proof.
269-
rewrite (add_comm p n); apply add_add_simpl_l_l.
270-
Qed.
271-
272-
Theorem add_add_simpl_r_l n m p : (n + m) - (m + p) == n - p.
273-
Proof.
274-
rewrite (add_comm n m); apply add_add_simpl_l_l.
275-
Qed.
276-
277-
Theorem add_add_simpl_r_r n m p : (n + m) - (p + m) == n - p.
278-
Proof.
279-
rewrite (add_comm p m); apply add_add_simpl_r_l.
280-
Qed.
281-
282238
Theorem sub_add_simpl_r_l n m p : (n - m) + (m + p) == n + p.
283239
Proof.
284240
now rewrite <- sub_sub_distr, sub_add_distr, sub_diag,
@@ -293,3 +249,4 @@ Qed.
293249
(** Of course, there are many other variants *)
294250

295251
End ZAddProp.
252+

theories/Numbers/Integer/Abstract/ZAddOrder.v

+4-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(************************************************************************)
2-
(* * The Rocq Prover / The Rocq Development Team *)
2+
(* * The Coq Proof Assistant / The Coq Development Team *)
33
(* v * Copyright INRIA, CNRS and contributors *)
44
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
55
(* \VV/ **************************************************************)
@@ -40,11 +40,6 @@ Qed.
4040

4141
(** Sub and order *)
4242

43-
Theorem lt_0_sub : forall n m, 0 < m - n <-> n < m.
44-
Proof.
45-
intros n m. now rewrite (add_lt_mono_r _ _ n), add_0_l, sub_simpl_r.
46-
Qed.
47-
4843
Notation sub_pos := lt_0_sub (only parsing).
4944

5045
Theorem le_0_sub : forall n m, 0 <= m - n <-> n <= m.
@@ -151,39 +146,24 @@ apply le_lt_trans with (m - p);
151146
[now apply sub_le_mono_r | now apply sub_lt_mono_l].
152147
Qed.
153148

154-
Theorem le_lt_sub_lt : forall n m p q, n <= m -> p - n < q - m -> p < q.
155-
Proof.
156-
intros n m p q H1 H2. apply (le_lt_add_lt (- m) (- n));
157-
[now apply -> opp_le_mono | now rewrite 2 add_opp_r].
158-
Qed.
159-
160149
Theorem lt_le_sub_lt : forall n m p q, n < m -> p - n <= q - m -> p < q.
161150
Proof.
162151
intros n m p q H1 H2. apply (lt_le_add_lt (- m) (- n));
163152
[now apply -> opp_lt_mono | now rewrite 2 add_opp_r].
164153
Qed.
165154

155+
(* TODO: fix name *)
166156
Theorem le_le_sub_lt : forall n m p q, n <= m -> p - n <= q - m -> p <= q.
167157
Proof.
168158
intros n m p q H1 H2. apply (le_le_add_le (- m) (- n));
169159
[now apply -> opp_le_mono | now rewrite 2 add_opp_r].
170160
Qed.
171161

172-
Theorem lt_add_lt_sub_r : forall n m p, n + p < m <-> n < m - p.
173-
Proof.
174-
intros n m p. now rewrite (sub_lt_mono_r _ _ p), add_simpl_r.
175-
Qed.
176-
177162
Theorem le_add_le_sub_r : forall n m p, n + p <= m <-> n <= m - p.
178163
Proof.
179164
intros n m p. now rewrite (sub_le_mono_r _ _ p), add_simpl_r.
180165
Qed.
181166

182-
Theorem lt_add_lt_sub_l : forall n m p, n + p < m <-> p < m - n.
183-
Proof.
184-
intros n m p. rewrite add_comm; apply lt_add_lt_sub_r.
185-
Qed.
186-
187167
Theorem le_add_le_sub_l : forall n m p, n + p <= m <-> p <= m - n.
188168
Proof.
189169
intros n m p. rewrite add_comm; apply le_add_le_sub_r.
@@ -194,21 +174,11 @@ Proof.
194174
intros n m p. now rewrite (add_lt_mono_r _ _ p), sub_simpl_r.
195175
Qed.
196176

197-
Theorem le_sub_le_add_r : forall n m p, n - p <= m <-> n <= m + p.
198-
Proof.
199-
intros n m p. now rewrite (add_le_mono_r _ _ p), sub_simpl_r.
200-
Qed.
201-
202177
Theorem lt_sub_lt_add_l : forall n m p, n - m < p <-> n < m + p.
203178
Proof.
204179
intros n m p. rewrite add_comm; apply lt_sub_lt_add_r.
205180
Qed.
206181

207-
Theorem le_sub_le_add_l : forall n m p, n - m <= p <-> n <= m + p.
208-
Proof.
209-
intros n m p. rewrite add_comm; apply le_sub_le_add_r.
210-
Qed.
211-
212182
Theorem lt_sub_lt_add : forall n m p q, n - m < p - q <-> n + q < m + p.
213183
Proof.
214184
intros n m p q. now rewrite lt_sub_lt_add_l, add_sub_assoc, <- lt_add_lt_sub_r.
@@ -283,3 +253,5 @@ End PosNeg.
283253
Ltac zero_pos_neg n := induction_maker n ltac:(apply zero_pos_neg).
284254

285255
End ZAddOrderProp.
256+
257+

theories/Numbers/Integer/Abstract/ZAxioms.v

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(************************************************************************)
2-
(* * The Rocq Prover / The Rocq Development Team *)
2+
(* * The Coq Proof Assistant / The Coq Development Team *)
33
(* v * Copyright INRIA, CNRS and contributors *)
44
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
55
(* \VV/ **************************************************************)
@@ -16,7 +16,7 @@ From Stdlib Require Import Bool NZParity NZPow NZSqrt NZLog NZGcd NZDiv NZBits.
1616
(** We obtain integers by postulating that successor of predecessor
1717
is identity. *)
1818

19-
Module Type ZAxiom (Import Z : NZAxiomsSig').
19+
Module Type ZAxiom (Import Z : NZBasicFunsSig').
2020
Axiom succ_pred : forall n, S (P n) == n.
2121
End ZAxiom.
2222

@@ -34,14 +34,14 @@ End OppNotation.
3434

3535
Module Type Opp' (T:Typ) := Opp T <+ OppNotation T.
3636

37-
Module Type IsOpp (Import Z : NZAxiomsSig')(Import O : Opp' Z).
37+
Module Type IsOpp (Import Z : NZBasicFunsSig')(Import O : Opp' Z).
3838
#[global]
3939
Declare Instance opp_wd : Proper (eq==>eq) opp.
4040
Axiom opp_0 : - 0 == 0.
4141
Axiom opp_succ : forall n, - (S n) == P (- n).
4242
End IsOpp.
4343

44-
Module Type OppCstNotation (Import A : NZAxiomsSig)(Import B : Opp A).
44+
Module Type OppCstNotation (Import A : NZBasicFunsSig)(Import B : Opp A).
4545
Notation "- 1" := (opp one).
4646
Notation "- 2" := (opp two).
4747
End OppCstNotation.

theories/Numbers/Integer/Abstract/ZBase.v

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(************************************************************************)
2-
(* * The Rocq Prover / The Rocq Development Team *)
2+
(* * The Coq Proof Assistant / The Coq Development Team *)
33
(* v * Copyright INRIA, CNRS and contributors *)
44
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
55
(* \VV/ **************************************************************)
@@ -17,6 +17,14 @@ From Stdlib Require Import NZMulOrder.
1717
Module ZBaseProp (Import Z : ZAxiomsMiniSig').
1818
Include NZMulOrderProp Z.
1919

20+
Lemma Private_succ_pred n : n ~= 0 -> S (P n) == n.
21+
Proof. intros _; exact (succ_pred _). Qed.
22+
23+
Lemma le_pred_l n : P n <= n.
24+
Proof. rewrite <-(succ_pred n), pred_succ; exact (le_succ_diag_r _). Qed.
25+
26+
Include NZAddOrder.NatIntAddOrderProp Z.
27+
2028
(* Theorems that are true for integers but not for natural numbers *)
2129

2230
Theorem pred_inj : forall n m, P n == P m -> n == m.
@@ -35,3 +43,4 @@ Proof.
3543
Qed.
3644

3745
End ZBaseProp.
46+

theories/Numbers/Integer/Abstract/ZLt.v

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(************************************************************************)
2-
(* * The Rocq Prover / The Rocq Development Team *)
2+
(* * The Coq Proof Assistant / The Coq Development Team *)
33
(* v * Copyright INRIA, CNRS and contributors *)
44
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
55
(* \VV/ **************************************************************)
@@ -47,11 +47,6 @@ Proof.
4747
intro n; rewrite <- (succ_pred n) at 2; apply lt_succ_diag_r.
4848
Qed.
4949

50-
Theorem le_pred_l : forall n, P n <= n.
51-
Proof.
52-
intro; apply lt_le_incl; apply lt_pred_l.
53-
Qed.
54-
5550
Theorem lt_le_pred : forall n m, n < m <-> n <= P m.
5651
Proof.
5752
intros n m; rewrite <- (succ_pred m); rewrite pred_succ. apply lt_succ_r.
@@ -132,3 +127,4 @@ setoid_replace (P 0) with (-1) in H2.
132127
Qed.
133128

134129
End ZOrderProp.
130+

theories/Numbers/Integer/Abstract/ZMul.v

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(************************************************************************)
2-
(* * The Rocq Prover / The Rocq Development Team *)
2+
(* * The Coq Proof Assistant / The Coq Development Team *)
33
(* v * Copyright INRIA, CNRS and contributors *)
44
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
55
(* \VV/ **************************************************************)
@@ -27,18 +27,6 @@ Include ZAddProp Z.
2727
(** Theorems that are either not valid on N or have different proofs
2828
on N and Z *)
2929

30-
Theorem mul_pred_r : forall n m, n * (P m) == n * m - n.
31-
Proof.
32-
intros n m.
33-
rewrite <- (succ_pred m) at 2.
34-
now rewrite mul_succ_r, <- add_sub_assoc, sub_diag, add_0_r.
35-
Qed.
36-
37-
Theorem mul_pred_l : forall n m, (P n) * m == n * m - m.
38-
Proof.
39-
intros n m; rewrite (mul_comm (P n) m), (mul_comm n m). apply mul_pred_r.
40-
Qed.
41-
4230
Theorem mul_opp_l : forall n m, (- n) * m == - (n * m).
4331
Proof.
4432
intros n m. apply add_move_0_r.
@@ -60,16 +48,6 @@ Proof.
6048
intros n m. now rewrite mul_opp_l, <- mul_opp_r.
6149
Qed.
6250

63-
Theorem mul_sub_distr_l : forall n m p, n * (m - p) == n * m - n * p.
64-
Proof.
65-
intros n m p. do 2 rewrite <- add_opp_r. rewrite mul_add_distr_l.
66-
now rewrite mul_opp_r.
67-
Qed.
51+
End ZMulProp.
6852

69-
Theorem mul_sub_distr_r : forall n m p, (n - m) * p == n * p - m * p.
70-
Proof.
71-
intros n m p; rewrite (mul_comm (n - m) p), (mul_comm n p), (mul_comm m p);
72-
now apply mul_sub_distr_l.
73-
Qed.
7453

75-
End ZMulProp.

0 commit comments

Comments
 (0)