Debian Codegraph @

theories/Corelib/Init/Peano.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** The type [nat] of Peano natural numbers (built from [O] and [S])
12 is defined in [Datatypes.v] *)
13
14 (** This module defines the following operations on natural numbers :
15 - predecessor [pred]
16 - addition [plus]
17 - multiplication [mult]
18 - less or equal order [le]
19 - less [lt]
20 - greater or equal [ge]
21 - greater [gt]
22
23 It states various lemmas and theorems about natural numbers,
24 including Peano's axioms of arithmetic (in Rocq, these are provable).
25 Case analysis on [nat] and induction on [nat * nat] are provided too
26 *)
27
28 Require Import Notations.
29 Require Import Ltac.
30 Require Import Datatypes.
31 Require Import Logic.
32 Require Corelib.Init.Nat.
33
34 Open Scope nat_scope.
35 Local Notation "0" := O.
36
37 Definition eq_S := f_equal S.
38 Definition f_equal_nat := f_equal (A:=nat).
39
40 #[global]
41 Hint Resolve f_equal_nat: core.
42
43 (** The predecessor function *)
44
45 Abbreviation pred := Nat.pred (only parsing).
46
47 Definition f_equal_pred := f_equal pred.
48
49 Theorem pred_Sn : forall n:nat, n = pred (S n).
50 Proof.
51 simpl; reflexivity.
52 Qed.
53
54 (** Injectivity of successor *)
55
56 Definition eq_add_S n m (H: S n = S m): n = m := f_equal pred H.
57 #[global]
58 Hint Immediate eq_add_S: core.
59
60 Theorem not_eq_S : forall n m:nat, n <> m -> S n <> S m.
61 Proof.
62 red; auto.
63 Qed.
64 #[global]
65 Hint Resolve not_eq_S: core.
66
67 Definition IsSucc (n:nat) : Prop :=
68 match n with
69 | O => False
70 | S p => True
71 end.
72
73 (** Zero is not the successor of a number *)
74
75 Theorem O_S : forall n:nat, 0 <> S n.
76 Proof.
77 discriminate.
78 Qed.
79 #[global]
80 Hint Resolve O_S: core.
81
82 Theorem n_Sn : forall n:nat, n <> S n.
83 Proof.
84 intro n; induction n; auto.
85 Qed.
86 #[global]
87 Hint Resolve n_Sn: core.
88
89 (** Addition *)
90
91 Abbreviation plus := Nat.add (only parsing).
92 Infix "+" := Nat.add : nat_scope.
93
94 Definition f_equal2_plus := f_equal2 plus.
95 Definition f_equal2_nat := f_equal2 (A1:=nat) (A2:=nat).
96 #[global]
97 Hint Resolve f_equal2_nat: core.
98
99 Lemma plus_n_O : forall n:nat, n = n + 0.
100 Proof.
101 intro n; induction n; simpl; auto.
102 Qed.
103
104 #[global]
105 Remove Hints eq_refl : core.
106 #[global]
107 Hint Resolve plus_n_O eq_refl: core. (* We want eq_refl to have higher priority than plus_n_O *)
108
109 Lemma plus_O_n : forall n:nat, 0 + n = n.
110 Proof.
111 reflexivity.
112 Qed.
113
114 Lemma plus_n_Sm : forall n m:nat, S (n + m) = n + S m.
115 Proof.
116 intros n m; induction n; simpl; auto.
117 Qed.
118 #[global]
119 Hint Resolve plus_n_Sm: core.
120
121 Lemma plus_Sn_m : forall n m:nat, S n + m = S (n + m).
122 Proof.
123 reflexivity.
124 Qed.
125
126 (** Standard associated names *)
127
128 Abbreviation plus_0_r_reverse := plus_n_O (only parsing).
129 Abbreviation plus_succ_r_reverse := plus_n_Sm (only parsing).
130
131 (** Multiplication *)
132
133 Abbreviation mult := Nat.mul (only parsing).
134 Infix "*" := Nat.mul : nat_scope.
135
136 Definition f_equal2_mult := f_equal2 mult.
137 #[global]
138 Hint Resolve f_equal2_mult: core.
139
140 Lemma mult_n_O : forall n:nat, 0 = n * 0.
141 Proof.
142 intro n; induction n; simpl; auto.
143 Qed.
144 #[global]
145 Hint Resolve mult_n_O: core.
146
147 Lemma mult_n_Sm : forall n m:nat, n * m + n = n * S m.
148 Proof.
149 intros n m; induction n as [| p H]; simpl; auto.
150 destruct H; rewrite <- plus_n_Sm; apply eq_S.
151 pattern m at 1 3; elim m; simpl; auto.
152 Qed.
153 #[global]
154 Hint Resolve mult_n_Sm: core.
155
156 (** Standard associated names *)
157
158 Abbreviation mult_0_r_reverse := mult_n_O (only parsing).
159 Abbreviation mult_succ_r_reverse := mult_n_Sm (only parsing).
160
161 (** Truncated subtraction: [m-n] is [0] if [n>=m] *)
162
163 Abbreviation minus := Nat.sub (only parsing).
164 Infix "-" := Nat.sub : nat_scope.
165
166 (** Definition of the usual orders, the basic properties of [le] and [lt]
167 can be found in files Le and Lt *)
168
169 Inductive le (n:nat) : nat -> Prop :=
170 | le_n : n <= n
171 | le_S : forall m:nat, n <= m -> n <= S m
172
173 where "n <= m" := (le n m) : nat_scope.
174
175 Register le_n as num.nat.le_n.
176
177 #[global]
178 Hint Constructors le: core.
179 (*i equivalent to : "Hints Resolve le_n le_S : core." i*)
180
181 Definition lt (n m:nat) := S n <= m.
182 #[global]
183 Hint Unfold lt: core.
184
185 Infix "<" := lt : nat_scope.
186
187 Definition ge (n m:nat) := m <= n.
188 #[global]
189 Hint Unfold ge: core.
190
191 Infix ">=" := ge : nat_scope.
192
193 Definition gt (n m:nat) := m < n.
194 #[global]
195 Hint Unfold gt: core.
196
197 Infix ">" := gt : nat_scope.
198
199 Notation "x <= y <= z" := (x <= y /\ y <= z) : nat_scope.
200 Notation "x <= y < z" := (x <= y /\ y < z) : nat_scope.
201 Notation "x < y < z" := (x < y /\ y < z) : nat_scope.
202 Notation "x < y <= z" := (x < y /\ y <= z) : nat_scope.
203
204 Register le as num.nat.le.
205 Register lt as num.nat.lt.
206 Register ge as num.nat.ge.
207 Register gt as num.nat.gt.
208
209 Theorem le_pred : forall n m, n <= m -> pred n <= pred m.
210 Proof.
211 induction 1 as [|m _]; auto. destruct m; simpl; auto.
212 Qed.
213
214 Theorem le_S_n : forall n m, S n <= S m -> n <= m.
215 Proof.
216 intros n m. exact (le_pred (S n) (S m)).
217 Qed.
218
219 Theorem le_0_n : forall n, 0 <= n.
220 Proof.
221 intro n; induction n; constructor; trivial.
222 Qed.
223
224 Theorem le_n_S : forall n m, n <= m -> S n <= S m.
225 Proof.
226 induction 1; constructor; trivial.
227 Qed.
228
229 (** Case analysis *)
230
231 Theorem nat_case :
232 forall (n:nat) (P:nat -> Prop), P 0 -> (forall m:nat, P (S m)) -> P n.
233 Proof.
234 intros n P IH0 IHS; case n; auto.
235 Qed.
236
237 (** Principle of double induction *)
238
239 Theorem nat_double_ind :
240 forall R:nat -> nat -> Prop,
241 (forall n:nat, R 0 n) ->
242 (forall n:nat, R (S n) 0) ->
243 (forall n m:nat, R n m -> R (S n) (S m)) -> forall n m:nat, R n m.
244 Proof.
245 intros R ? ? ? n.
246 induction n; auto.
247 intro m; destruct m; auto.
248 Qed.
249
250 (** Maximum and minimum : definitions and specifications *)
251
252 Abbreviation max := Nat.max (only parsing).
253 Abbreviation min := Nat.min (only parsing).
254
255 Lemma max_l n m : m <= n -> Nat.max n m = n.
256 Proof.
257 revert m; induction n as [|n IHn]; intro m; destruct m; simpl; trivial.
258 - inversion 1.
259 - intros. apply f_equal, IHn, le_S_n; trivial.
260 Qed.
261
262 Lemma max_r n m : n <= m -> Nat.max n m = m.
263 Proof.
264 revert m; induction n as [|n IHn]; intro m; destruct m; simpl; trivial.
265 - inversion 1.
266 - intros. apply f_equal, IHn, le_S_n; trivial.
267 Qed.
268
269 Lemma min_l n m : n <= m -> Nat.min n m = n.
270 Proof.
271 revert m; induction n as [|n IHn]; intro m; destruct m; simpl; trivial.
272 - inversion 1.
273 - intros. apply f_equal, IHn, le_S_n; trivial.
274 Qed.
275
276 Lemma min_r n m : m <= n -> Nat.min n m = m.
277 Proof.
278 revert m; induction n as [|n IHn]; intro m; destruct m; simpl; trivial.
279 - inversion 1.
280 - intros. apply f_equal, IHn, le_S_n; trivial.
281 Qed.
282
283
284 Lemma nat_rect_succ_r {A} (f: A -> A) (x:A) n :
285 nat_rect (fun _ => A) x (fun _ => f) (S n) = nat_rect (fun _ => A) (f x) (fun _ => f) n.
286 Proof.
287 induction n as [|n IHn]; intros; simpl; rewrite <- ?IHn; trivial.
288 Qed.
289
290 Theorem nat_rect_plus :
291 forall (n m:nat) {A} (f:A -> A) (x:A),
292 nat_rect (fun _ => A) x (fun _ => f) (n + m) =
293 nat_rect (fun _ => A) (nat_rect (fun _ => A) x (fun _ => f) m) (fun _ => f) n.
294 Proof.
295 intro n; induction n as [|n IHn]; intros; simpl; rewrite ?IHn; trivial.
296 Qed.