-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArith.v
388 lines (303 loc) · 8.95 KB
/
Arith.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
(*******************************************************************************
Title: Arith.v
Authors: Jeremy Avigad, Chris Kapulkin, Peter LeFanu Lumsdaine
Date: 1 March 2013
This file contains some basic arithmetic, ported from Coq’s standard Peano.v.
(We cannot import that, since it is not all homotopy-compatible.)
Once the HoTT library contains its own development of arithmetic, this
file will be obsolete.
*******************************************************************************)
Require Import HoTT.
Require Import Auxiliary.
Open Scope nat_scope.
Hint Resolve (ap S): v62.
Hint Resolve (ap (A:=nat)): core.
(** Booleans *)
Inductive Bool : Type :=
| true : Bool
| false : Bool.
Definition is_true (b:Bool) : Type
:= match b with true => Unit | false => Empty end.
Definition true_neq_false : true = false -> Empty
:= (fun H => transport is_true H tt).
(** The predecessor function *)
Definition pred (n:nat) : nat := match n with
| O => n
| S u => u
end.
Hint Resolve (ap pred): v62.
Theorem pred_Sn : forall n:nat, n = pred (S n).
Proof.
simpl; reflexivity.
Defined.
(** Injectivity of successor *)
(* eq_add_S in Peano.v *)
Definition S_inj : forall {n m:nat}, S n = S m -> n = m.
Proof.
intros n m p. apply (ap pred p).
Defined.
Hint Resolve S_inj: core.
Definition is_succ (n:nat) : Type :=
match n with
| O => Empty
| S p => Unit
end.
Definition is_zero (n:nat) : Type :=
match n with
| O => Unit
| S p => Empty
end.
(** Zero is not the successor of a number *)
Theorem O_neq_S {n:nat} : 0 = S n -> Empty.
Proof.
intros p. apply (transport is_zero p). constructor.
Defined.
Hint Resolve O_neq_S: core.
Theorem n_Sn : forall n:nat, n = S n -> Empty.
Proof.
induction n; auto; apply O_neq_S.
Defined.
Hint Resolve n_Sn: core.
(** Addition *)
Fixpoint plus (n m:nat) : nat :=
match n with
| O => m
| S p => S (p + m)
end
where "n + m" := (plus n m) : nat_scope.
Hint Resolve (ap10 o (ap plus)): v62.
Lemma plus_n_O : forall n:nat, n = n + 0.
Proof.
induction n; simpl; auto.
Defined.
Hint Resolve plus_n_O: core.
Lemma plus_O_n : forall n:nat, 0 + n = n.
Proof.
auto.
Defined.
Lemma plus_n_Sm : forall n m:nat, S (n + m) = n + S m.
Proof.
intros n m; induction n; simpl; auto.
Defined.
Hint Resolve plus_n_Sm: core.
Lemma plus_Sn_m : forall n m:nat, S n + m = S (n + m).
Proof.
auto.
Defined.
(** Standard associated names *)
Notation plus_0_r_reverse := plus_n_O (compat "8.2").
Notation plus_succ_r_reverse := plus_n_Sm (compat "8.2").
(** Multiplication *)
Fixpoint mult (n m:nat) : nat :=
match n with
| O => 0
| S p => m + p * m
end
where "n * m" := (mult n m) : nat_scope.
Hint Resolve (ap10 o (ap mult)): core.
Lemma mult_n_O : forall n:nat, 0 = n * 0.
Proof.
induction n; simpl; auto.
Defined.
Hint Resolve mult_n_O: core.
Lemma mult_n_Sm : forall n m:nat, n * m + n = n * S m.
Proof.
intros; induction n as [| p H]; simpl; auto.
destruct H; rewrite <- plus_n_Sm; apply (ap S).
pattern m at 1 3; elim m; simpl; auto.
Defined.
Hint Resolve mult_n_Sm: core.
(** Standard associated names *)
Notation mult_0_r_reverse := mult_n_O (compat "8.2").
Notation mult_succ_r_reverse := mult_n_Sm (compat "8.2").
(** Truncated subtraction: [m-n] is [0] if [n>=m] *)
Fixpoint minus (n m:nat) : nat :=
match n, m with
| O, _ => n
| S k, O => n
| S k, S l => k - l
end
where "n - m" := (minus n m) : nat_scope.
(** Definition of the usual orders, the basic properties of [le] and [lt]
can be found in files Le and Lt *)
Inductive le (n:nat) : nat -> Type :=
| le_n : n <= n
| le_S : forall m:nat, n <= m -> n <= S m
where "n <= m" := (le n m) : nat_scope.
Hint Constructors le: core.
(*i equivalent to : "Hints Resolve le_n le_S : core." i*)
Definition lt (n m:nat) := S n <= m.
Hint Unfold lt: core.
Infix "<" := lt : nat_scope.
Definition ge (n m:nat) := m <= n.
Hint Unfold ge: core.
Infix ">=" := ge : nat_scope.
Definition gt (n m:nat) := m < n.
Hint Unfold gt: core.
Infix ">" := gt : nat_scope.
(* Awaiting definition of /\:
Notation "x <= y <= z" := (x <= y /\ y <= z) : nat_scope.
Notation "x <= y < z" := (x <= y /\ y < z) : nat_scope.
Notation "x < y < z" := (x < y /\ y < z) : nat_scope.
Notation "x < y <= z" := (x < y /\ y <= z) : nat_scope.
*)
Theorem S_preserves_le : forall n m, n <= m -> (S n) <= (S m).
Proof.
intros n m H; induction H; auto.
Defined.
(** le_pred in Peano.v *)
Theorem pred_preserves_le : forall n m, n <= m -> pred n <= pred m.
Proof.
induction 1; auto. destruct m; simpl; auto.
Defined.
(** le_S_n *)
Theorem S_reflects_le : forall {n m}, S n <= S m -> n <= m.
Proof.
intros n m. exact (pred_preserves_le (S n) (S m)).
Defined.
Theorem O_le_n n : O <= n.
Proof.
induction n; auto.
Defined.
Theorem n_le_O : forall {n:nat}, n <= 0 -> n = 0.
Proof.
assert (gen : forall n m, 0 = m -> n <= m -> n = m).
intros n. induction n as [ | n' IH].
intros; auto.
intros m p H. destruct H as [ | m' H'].
destruct (O_neq_S p).
destruct (O_neq_S p).
intros; apply gen; auto.
Defined.
Definition le_refl := le_n.
Theorem le_trans : forall {i j k:nat}, i <= j -> j <= k -> i <= k.
Proof.
intros i j k Hij Hjk. induction Hjk as [ | k' Hjk' IH].
auto.
auto.
Defined.
(** Case analysis *)
Theorem nat_case :
forall (n:nat) (P:nat -> Type), P 0 -> (forall m:nat, P (S m)) -> P n.
Proof.
induction n; auto.
Defined.
(** Principle of double induction *)
Theorem nat_double_ind :
forall R:nat -> nat -> Type,
(forall n:nat, R 0 n) ->
(forall n:nat, R (S n) 0) ->
(forall n m:nat, R n m -> R (S n) (S m)) -> forall n m:nat, R n m.
Proof.
induction n; auto.
destruct m; auto.
Defined.
(** max and min omitted *)
(** [n]th iteration of the function [f] *)
Fixpoint nat_iter (n:nat) {A} (f:A->A) (x:A) : A :=
match n with
| O => x
| S n' => f (nat_iter n' f x)
end.
Lemma nat_iter_succ_r n {A} (f:A->A) (x:A) :
nat_iter (S n) f x = nat_iter n f (f x).
Proof.
induction n; intros; simpl; rewrite <- ?IHn; trivial.
Defined.
Theorem nat_iter_plus :
forall (n m:nat) {A} (f:A -> A) (x:A),
nat_iter (n + m) f x = nat_iter n f (nat_iter m f x).
Proof.
induction n; intros; simpl; rewrite ?IHn; trivial.
Defined.
(** Preservation of invariants : if [f : A->A] preserves the invariant [Inv],
then the iterates of [f] also preserve it. *)
Theorem nat_iter_invariant :
forall (n:nat) {A} (f:A -> A) (P : A -> Type),
(forall x, P x -> P (f x)) ->
forall x, P x -> P (nat_iter n f x).
Proof.
induction n; simpl; trivial.
intros A f P Hf x Hx. apply Hf, IHn; trivial.
Defined.
(** Decidability
Note: there are several possible approaches to decidability.
Here we mix a few of them. *)
Fixpoint booleq_nat (n m:nat) : Bool :=
match n with
| O => match m with
| O => true
| S m' => false
end
| S n' => match m with
| O => false
| S m' => booleq_nat n' m'
end
end.
Fixpoint le_bool (n m:nat) : Bool :=
match n with
| O => true
| S n' => match m with
| O => false
| S m' => le_bool n' m'
end
end.
Definition le_bool__le {n m : nat} : (le_bool n m = true) -> n <= m.
Proof.
revert m. induction n as [ | n' IHn]; intros [ | m']; simpl.
auto.
intros; apply O_le_n.
intros H. destruct (true_neq_false (H^)).
intros H. apply S_preserves_le. apply IHn. assumption.
Defined.
Definition le__le_bool {n m : nat} : n <= m -> (le_bool n m = true).
Proof.
revert m. induction n as [ | n' IHn]; intros [ | m']; simpl.
auto.
auto.
intros H. destruct (O_neq_S (n_le_O H)^).
intros H. apply IHn. apply S_reflects_le. assumption.
Defined.
Definition Decide (A:Type) := (A + (A -> Empty))%type.
Definition eq_dec_nat : forall (n m:nat), Decide (n = m).
Proof.
intros n; induction n as [ | n' IHn];
intros m; destruct m as [ | m'].
(* O = O *) apply inl. auto.
(* O = S m' *) apply inr. apply O_neq_S.
(* S n' = 0 *) apply inr.
intros H. exact (O_neq_S H^).
(* Sn' = S m' *)
destruct (IHn m') as [ yes | no ].
(* yes *) apply inl; auto.
(* now *) apply inr.
intros H. apply no. apply (S_inj H).
Defined.
Definition le_dec : forall (n m:nat), Decide (n <= m).
Proof.
intros n; induction n as [ | n' IHn];
intros m; destruct m as [ | m'].
(* O = O *) apply inl. auto.
(* O = S m' *) apply inl. apply O_le_n.
(* S n' = 0 *) apply inr.
intros H. exact (O_neq_S (n_le_O H)^).
(* Sn' = S m' *)
destruct (IHn m') as [ yes | no ].
(* yes *) apply inl; apply S_preserves_le; auto.
(* now *) apply inr.
intros H. apply no. apply (S_reflects_le H).
Defined.
Theorem nat_is_hset : IsHSet nat.
Proof.
apply hset_decpaths. unfold DecidablePaths. apply eq_dec_nat.
Defined.
Definition nat_HSet : HSet
:= Build_HSet nat nat_is_hset.
(* TODO (mid): re-enable this as soon as the Canonical Structures issue is fixed.
Canonical Structure nat_HSet.
*)
(*
Local Variables:
coq-prog-name: "hoqtop"
End:
*)