1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/nat/".
17 include "equality.ma".
22 inductive nat : Set \def
26 definition pred: nat \to nat \def
27 \lambda n:nat. match n with
29 | (S u) \Rightarrow u ].
31 theorem pred_Sn : \forall n:nat.
32 (eq nat n (pred (S n))).
36 theorem injective_S : \forall n,m:nat.
37 (eq nat (S n) (S m)) \to (eq nat n m).
41 apply f_equal. assumption.
44 theorem not_eq_S : \forall n,m:nat.
45 Not (eq nat n m) \to Not (eq nat (S n) (S m)).
46 intros. simplify.intros.
47 apply H.apply injective_S.assumption.
50 definition not_zero : nat \to Prop \def
54 | (S p) \Rightarrow True ].
56 theorem O_S : \forall n:nat. Not (eq nat O (S n)).
57 intros.simplify.intros.
58 cut (not_zero O).exact Hcut.rewrite > H.
62 theorem n_Sn : \forall n:nat. Not (eq nat n (S n)).
63 intros.elim n.apply O_S.apply not_eq_S.assumption.
69 | (S p) \Rightarrow S (plus p m) ].
71 theorem plus_n_O: \forall n:nat. eq nat n (plus n O).
72 intros.elim n.simplify.reflexivity.
73 simplify.apply f_equal.assumption.
76 theorem plus_n_Sm : \forall n,m:nat. eq nat (S (plus n m)) (plus n (S m)).
77 intros.elim n.simplify.reflexivity.
78 simplify.apply f_equal.assumption.
81 theorem sym_plus: \forall n,m:nat. eq nat (plus n m) (plus m n).
82 intros.elim n.simplify.apply plus_n_O.
83 simplify.rewrite > H.apply plus_n_Sm.
87 \forall n,m,p:nat. eq nat (plus (plus n m) p) (plus n (plus m p)).
88 intros.elim n.simplify.reflexivity.
89 simplify.apply f_equal.assumption.
92 let rec times n m \def
95 | (S p) \Rightarrow (plus m (times p m)) ].
97 theorem times_n_O: \forall n:nat. eq nat O (times n O).
98 intros.elim n.simplify.reflexivity.
103 \forall n,m:nat. eq nat (plus n (times n m)) (times n (S m)).
104 intros.elim n.simplify.reflexivity.
105 simplify.apply f_equal.rewrite < H.
106 transitivity (plus (plus e m) (times e m)).symmetry.
107 apply assoc_plus.transitivity (plus (plus m e) (times e m)).
109 apply sym_plus.reflexivity.apply assoc_plus.
113 \forall n,m:nat. eq nat (times n m) (times m n).
114 intros.elim n.simplify.apply times_n_O.
115 simplify.rewrite < sym_eq ? ? ? H.apply times_n_Sm.
118 let rec minus n m \def
124 | (S q) \Rightarrow minus p q ]].
127 \forall n:nat.\forall P:nat \to Prop.
128 P O \to (\forall m:nat. P (S m)) \to P n.
129 intros.elim n.assumption.apply H1.
132 theorem nat_double_ind :
133 \forall R:nat \to nat \to Prop.
134 (\forall n:nat. R O n) \to
135 (\forall n:nat. R (S n) O) \to
136 (\forall n,m:nat. R n m \to R (S n) (S m)) \to \forall n,m:nat. R n m.
137 intros 5.elim n.apply H.
138 apply nat_case m.apply H1.intros.apply H2. apply H3.
141 inductive le (n:nat) : nat \to Prop \def
143 | le_S : \forall m:nat. le n m \to le n (S m).
145 theorem trans_le: \forall n,m,p:nat. le n m \to le m p \to le n p.
148 apply le_S.assumption.
151 theorem le_n_S: \forall n,m:nat. le n m \to le (S n) (S m).
153 apply le_n.apply le_S.assumption.
156 theorem le_O_n : \forall n:nat. le O n.
157 intros.elim n.apply le_n.apply le_S. assumption.
160 theorem le_n_Sn : \forall n:nat. le n (S n).
161 intros. apply le_S.apply le_n.
164 theorem le_pred_n : \forall n:nat. le (pred n) n.
165 intros.elim n.simplify.apply le_n.simplify.
169 theorem not_zero_le : \forall n,m:nat. (le (S n) m ) \to not_zero m.
170 intros.elim H.exact I.exact I.
173 theorem le_Sn_O: \forall n:nat. Not (le (S n) O).
174 intros.simplify.intros.apply not_zero_le ? O H.
177 theorem le_n_O_eq : \forall n:nat. (le n O) \to (eq nat O n).
178 intros.cut (le n O) \to (eq nat O n).apply Hcut. assumption.
180 apply False_ind.apply (le_Sn_O ? H2).
183 theorem le_S_n : \forall n,m:nat. le (S n) (S m) \to le n m.
184 intros.change with le (pred (S n)) (pred (S m)).
185 elim H.apply le_n.apply trans_le ? (pred x).assumption.
189 theorem le_Sn_n : \forall n:nat. Not (le (S n) n).
190 intros.elim n.apply le_Sn_O.simplify.intros.
191 cut le (S e) e.apply H.assumption.apply le_S_n.assumption.
194 theorem le_antisym : \forall n,m:nat. (le n m) \to (le m n) \to (eq nat n m).
195 intros.cut (le n m) \to (le m n) \to (eq nat n m).exact Hcut H H1.
196 apply nat_double_ind (\lambda n,m.((le n m) \to (le m n) \to eq nat n m)).
198 apply le_n_O_eq.assumption.
199 intros.symmetry.apply le_n_O_eq.assumption.
200 intros.apply f_equal.apply H2.
201 apply le_S_n.assumption.
202 apply le_S_n.assumption.
210 [ O \Rightarrow false
211 | (S q) \Rightarrow leb p q]].
213 theorem le_dec: \forall n,m:nat. if_then_else (leb n m) (le n m) (Not (le n m)).
215 apply (nat_double_ind
216 (\lambda n,m:nat.if_then_else (leb n m) (le n m) (Not (le n m))) ? ? ? n m).
217 simplify.intros.apply le_O_n.
218 simplify.exact le_Sn_O.
219 intros 2.simplify.elim (leb n1 m1).
220 simplify.apply le_n_S.apply H.
221 simplify.intros.apply H.apply le_S_n.assumption.
224 let rec nat_compare n m: compare \def
229 | (S q) \Rightarrow LT ]
233 | (S q) \Rightarrow nat_compare p q]].
235 theorem nat_compare_invert: \forall n,m:nat.
236 eq compare (nat_compare n m) (compare_invert (nat_compare m n)).
238 apply nat_double_ind (\lambda n,m.eq compare (nat_compare n m) (compare_invert (nat_compare m n))).
239 intros.elim n1.simplify.reflexivity.
240 simplify.reflexivity.
241 intro.elim n1.simplify.reflexivity.
242 simplify.reflexivity.
243 intros.simplify.elim H.simplify.reflexivity.