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 (**************************************************************************)
16 set "baseuri" "cic:/matita/nat/minus".
18 include "nat/le_arith.ma".
19 include "nat/compare.ma".
21 let rec minus n m \def
27 | (S q) \Rightarrow minus p q ]].
29 (*CSC: the URI must disappear: there is a bug now *)
30 interpretation "natural minus" 'minus x y = (cic:/matita/nat/minus/minus.con x y).
32 theorem minus_n_O: \forall n:nat.n=n-O.
33 intros.elim n.simplify.reflexivity.
37 theorem minus_n_n: \forall n:nat.O=n-n.
38 intros.elim n.simplify.
43 theorem minus_Sn_n: \forall n:nat. S O = (S n)-n.
49 theorem minus_Sn_m: \forall n,m:nat. m \leq n \to (S n)-m = S (n-m).
52 (\lambda n,m.m \leq n \to (S n)-m = S (n-m)).
53 intros.apply le_n_O_elim n1 H.
55 intros.simplify.reflexivity.
56 intros.rewrite < H.reflexivity.
57 apply le_S_S_to_le. assumption.
61 \forall n,m,p:nat. m \leq n \to (n-m)+p = (n+p)-m.
64 (\lambda n,m.\forall p:nat.m \leq n \to (n-m)+p = (n+p)-m).
65 intros.apply le_n_O_elim ? H.
66 simplify.rewrite < minus_n_O.reflexivity.
67 intros.simplify.reflexivity.
68 intros.simplify.apply H.apply le_S_S_to_le.assumption.
71 theorem plus_minus_m_m: \forall n,m:nat.
72 m \leq n \to n = (n-m)+m.
74 apply nat_elim2 (\lambda n,m.m \leq n \to n = (n-m)+m).
75 intros.apply le_n_O_elim n1 H.
77 intros.simplify.rewrite < plus_n_O.reflexivity.
78 intros.simplify.rewrite < sym_plus.simplify.
79 apply eq_f.rewrite < sym_plus.apply H.
80 apply le_S_S_to_le.assumption.
83 theorem minus_to_plus :\forall n,m,p:nat.m \leq n \to n-m = p \to
85 intros.apply trans_eq ? ? ((n-m)+m).
91 theorem plus_to_minus :\forall n,m,p:nat.
98 apply plus_minus_m_m.rewrite > H.
103 theorem minus_S_S : \forall n,m:nat.
104 eq nat (minus (S n) (S m)) (minus n m).
109 theorem minus_pred_pred : \forall n,m:nat. lt O n \to lt O m \to
110 eq nat (minus (pred n) (pred m)) (minus n m).
112 apply lt_O_n_elim n H.intro.
113 apply lt_O_n_elim m H1.intro.
114 simplify.reflexivity.
117 theorem eq_minus_n_m_O: \forall n,m:nat.
118 n \leq m \to n-m = O.
120 apply nat_elim2 (\lambda n,m.n \leq m \to n-m = O).
121 intros.simplify.reflexivity.
122 intros.apply False_ind.
126 simplify.apply H.apply le_S_S_to_le. apply H1.
129 theorem le_SO_minus: \forall n,m:nat.S n \leq m \to S O \leq m-n.
130 intros.elim H.elim minus_Sn_n n.apply le_n.
131 rewrite > minus_Sn_m.
132 apply le_S.assumption.
133 apply lt_to_le.assumption.
136 theorem minus_le_S_minus_S: \forall n,m:nat. m-n \leq S (m-(S n)).
137 intros.apply nat_elim2 (\lambda n,m.m-n \leq S (m-(S n))).
138 intro.elim n1.simplify.apply le_n_Sn.
139 simplify.rewrite < minus_n_O.apply le_n.
140 intros.simplify.apply le_n_Sn.
141 intros.simplify.apply H.
144 theorem lt_minus_S_n_to_le_minus_n : \forall n,m,p:nat. m-(S n) < p \to m-n \leq p.
145 intros 3.simplify.intro.
146 apply trans_le (m-n) (S (m-(S n))) p.
147 apply minus_le_S_minus_S.
151 theorem le_minus_m: \forall n,m:nat. n-m \leq n.
152 intros.apply nat_elim2 (\lambda m,n. n-m \leq n).
153 intros.rewrite < minus_n_O.apply le_n.
154 intros.simplify.apply le_n.
155 intros.simplify.apply le_S.assumption.
158 theorem lt_minus_m: \forall n,m:nat. O < n \to O < m \to n-m \lt n.
159 intros.apply lt_O_n_elim n H.intro.
160 apply lt_O_n_elim m H1.intro.
161 simplify.apply le_S_S.apply le_minus_m.
164 theorem minus_le_O_to_le: \forall n,m:nat. n-m \leq O \to n \leq m.
166 apply nat_elim2 (\lambda n,m:nat.n-m \leq O \to n \leq m).
168 simplify.intros. assumption.
169 simplify.intros.apply le_S_S.apply H.assumption.
173 theorem monotonic_le_minus_r:
174 \forall p,q,n:nat. q \leq p \to n-p \le n-q.
175 simplify.intros 2.apply nat_elim2
176 (\lambda p,q.\forall a.q \leq p \to a-p \leq a-q).
177 intros.apply le_n_O_elim n H.apply le_n.
178 intros.rewrite < minus_n_O.
180 intros.elim a.simplify.apply le_n.
181 simplify.apply H.apply le_S_S_to_le.assumption.
184 theorem le_minus_to_plus: \forall n,m,p. (le (n-m) p) \to (le n (p+m)).
185 intros 2.apply nat_elim2 (\lambda n,m.\forall p.(le (n-m) p) \to (le n (p+m))).
187 simplify.intros.rewrite < plus_n_O.assumption.
190 apply le_S_S.apply H.
194 theorem le_plus_to_minus: \forall n,m,p. (le n (p+m)) \to (le (n-m) p).
195 intros 2.apply nat_elim2 (\lambda n,m.\forall p.(le n (p+m)) \to (le (n-m) p)).
196 intros.simplify.apply le_O_n.
197 intros 2.rewrite < plus_n_O.intro.simplify.assumption.
198 intros.simplify.apply H.
199 apply le_S_S_to_le.rewrite > plus_n_Sm.assumption.
202 (* the converse of le_plus_to_minus does not hold *)
203 theorem le_plus_to_minus_r: \forall n,m,p. (le (n+m) p) \to (le n (p-m)).
204 intros 3.apply nat_elim2 (\lambda m,p.(le (n+m) p) \to (le n (p-m))).
205 intro.rewrite < plus_n_O.rewrite < minus_n_O.intro.assumption.
206 intro.intro.cut n=O.rewrite > Hcut.apply le_O_n.
207 apply sym_eq. apply le_n_O_to_eq.
208 apply trans_le ? (n+(S n1)).
210 apply le_plus_n.assumption.
212 apply H.apply le_S_S_to_le.
213 rewrite > plus_n_Sm.assumption.
217 theorem distributive_times_minus: distributive nat times minus.
220 apply (leb_elim z y).
221 intro.cut x*(y-z)+x*z = (x*y-x*z)+x*z.
222 apply inj_plus_l (x*z).assumption.
223 apply trans_eq nat ? (x*y).
224 rewrite < distr_times_plus.rewrite < plus_minus_m_m ? ? H.reflexivity.
225 rewrite < plus_minus_m_m.
227 apply le_times_r.assumption.
228 intro.rewrite > eq_minus_n_m_O.
229 rewrite > eq_minus_n_m_O (x*y).
230 rewrite < sym_times.simplify.reflexivity.
231 apply le_times_r.apply lt_to_le.apply not_le_to_lt.assumption.
232 apply lt_to_le.apply not_le_to_lt.assumption.
235 theorem distr_times_minus: \forall n,m,p:nat. n*(m-p) = n*m-n*p
236 \def distributive_times_minus.
238 theorem eq_minus_minus_minus_plus: \forall n,m,p:nat. (n-m)-p = n-(m+p).
240 cut m+p \le n \or m+p \nleq n.
242 symmetry.apply plus_to_minus.
243 rewrite > assoc_plus.rewrite > sym_plus p.rewrite < plus_minus_m_m.
244 rewrite > sym_plus.rewrite < plus_minus_m_m.
246 apply trans_le ? (m+p).
247 rewrite < sym_plus.apply le_plus_n.
249 apply le_plus_to_minus_r.rewrite > sym_plus.assumption.
250 rewrite > eq_minus_n_m_O n (m+p).
251 rewrite > eq_minus_n_m_O (n-m) p.
253 apply le_plus_to_minus.apply lt_to_le. rewrite < sym_plus.
254 apply not_le_to_lt. assumption.
255 apply lt_to_le.apply not_le_to_lt.assumption.
256 apply decidable_le (m+p) n.
259 theorem eq_plus_minus_minus_minus: \forall n,m,p:nat. p \le m \to m \le n \to
264 rewrite < assoc_plus.
265 rewrite < plus_minus_m_m.
267 rewrite < plus_minus_m_m.reflexivity.
268 assumption.assumption.