]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/div_and_mod.ma
ocaml 3.09 transition
[helm.git] / helm / matita / library / nat / div_and_mod.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        Matita is distributed under the terms of the          *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/nat/div_and_mod".
16
17 include "nat/minus.ma".
18
19 let rec mod_aux p m n: nat \def
20 match (leb m n) with
21 [ true \Rightarrow m
22 | false \Rightarrow
23   match p with
24   [O \Rightarrow m
25   |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
26
27 definition mod : nat \to nat \to nat \def
28 \lambda n,m.
29 match m with 
30 [O \Rightarrow m
31 | (S p) \Rightarrow mod_aux n n p]. 
32
33 interpretation "natural remainder" 'module x y =
34   (cic:/matita/nat/div_and_mod/mod.con x y).
35
36 let rec div_aux p m n : nat \def
37 match (leb m n) with
38 [ true \Rightarrow O
39 | false \Rightarrow
40   match p with
41   [O \Rightarrow O
42   |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
43
44 definition div : nat \to nat \to nat \def
45 \lambda n,m.
46 match m with 
47 [O \Rightarrow S n
48 | (S p) \Rightarrow div_aux n n p]. 
49
50 interpretation "natural divide" 'divide x y =
51   (cic:/matita/nat/div_and_mod/div.con x y).
52
53 theorem le_mod_aux_m_m: 
54 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
55 intro.elim p.
56 apply (le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m)).
57 simplify.apply le_O_n.
58 simplify.
59 apply (leb_elim n1 m).
60 simplify.intro.assumption.
61 simplify.intro.apply H.
62 cut (n1 \leq (S n) \to n1-(S m) \leq n).
63 apply Hcut.assumption.
64 elim n1.
65 simplify.apply le_O_n.
66 simplify.apply (trans_le ? n2 n).
67 apply le_minus_m.apply le_S_S_to_le.assumption.
68 qed.
69
70 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
71 intros 2.elim m.apply False_ind.
72 apply (not_le_Sn_O O H).
73 simplify.unfold lt.apply le_S_S.apply le_mod_aux_m_m.
74 apply le_n.
75 qed.
76
77 theorem div_aux_mod_aux: \forall p,n,m:nat. 
78 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
79 intro.elim p.
80 simplify.elim (leb n m).
81 simplify.apply refl_eq.
82 simplify.apply refl_eq.
83 simplify.
84 apply (leb_elim n1 m).
85 simplify.intro.apply refl_eq.
86 simplify.intro.
87 rewrite > assoc_plus. 
88 elim (H (n1-(S m)) m).
89 change with (n1=(S m)+(n1-(S m))).
90 rewrite < sym_plus.
91 apply plus_minus_m_m.
92 change with (m < n1).
93 apply not_le_to_lt.exact H1.
94 qed.
95
96 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
97 intros 2.elim m.elim (not_le_Sn_O O H).
98 simplify.
99 apply div_aux_mod_aux.
100 qed.
101
102 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
103 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
104
105 (* 
106 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
107 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
108 *)
109
110 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
111 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
112 rewrite < H1.assumption.
113 exact (not_le_Sn_O r).
114 qed.
115
116 theorem div_mod_spec_div_mod: 
117 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
118 intros.
119 apply div_mod_spec_intro.
120 apply lt_mod_m_m.assumption.
121 apply div_mod.assumption.
122 qed. 
123
124 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
125 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to 
126 (eq nat q q1).
127 intros.elim H.elim H1.
128 apply (nat_compare_elim q q1).intro.
129 apply False_ind.
130 cut (eq nat ((q1-q)*b+r1) r).
131 cut (b \leq (q1-q)*b+r1).
132 cut (b \leq r).
133 apply (lt_to_not_le r b H2 Hcut2).
134 elim Hcut.assumption.
135 apply (trans_le ? ((q1-q)*b)).
136 apply le_times_n.
137 apply le_SO_minus.exact H6.
138 rewrite < sym_plus.
139 apply le_plus_n.
140 rewrite < sym_times.
141 rewrite > distr_times_minus.
142 rewrite > plus_minus.
143 rewrite > sym_times.
144 rewrite < H5.
145 rewrite < sym_times.
146 apply plus_to_minus.
147 apply H3.
148 apply le_times_r.
149 apply lt_to_le.
150 apply H6.
151 (* eq case *)
152 intros.assumption.
153 (* the following case is symmetric *)
154 intro.
155 apply False_ind.
156 cut (eq nat ((q-q1)*b+r) r1).
157 cut (b \leq (q-q1)*b+r).
158 cut (b \leq r1).
159 apply (lt_to_not_le r1 b H4 Hcut2).
160 elim Hcut.assumption.
161 apply (trans_le ? ((q-q1)*b)).
162 apply le_times_n.
163 apply le_SO_minus.exact H6.
164 rewrite < sym_plus.
165 apply le_plus_n.
166 rewrite < sym_times.
167 rewrite > distr_times_minus.
168 rewrite > plus_minus.
169 rewrite > sym_times.
170 rewrite < H3.
171 rewrite < sym_times.
172 apply plus_to_minus.
173 apply H5.
174 apply le_times_r.
175 apply lt_to_le.
176 apply H6.
177 qed.
178
179 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
180 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to 
181 (eq nat r r1).
182 intros.elim H.elim H1.
183 apply (inj_plus_r (q*b)).
184 rewrite < H3.
185 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
186 assumption.
187 qed.
188
189 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
190 intros.constructor 1.
191 unfold lt.apply le_S_S.apply le_O_n.
192 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
193 qed.
194
195 (* some properties of div and mod *)
196 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
197 intros.
198 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O).
199 goal 15. (* ?11 is closed with the following tactics *)
200 apply div_mod_spec_div_mod.
201 unfold lt.apply le_S_S.apply le_O_n.
202 apply div_mod_spec_times.
203 qed.
204
205 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
206 intros.
207 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
208 apply div_mod_spec_div_mod.assumption.
209 constructor 1.assumption.
210 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
211 qed.
212
213 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
214 intros.
215 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
216 apply div_mod_spec_div_mod.
217 apply (le_to_lt_to_lt O n m).
218 apply le_O_n.assumption.
219 constructor 1.assumption.reflexivity.
220 qed.
221
222 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
223 intros.
224 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
225 apply div_mod_spec_div_mod.assumption.
226 constructor 1.assumption.
227 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
228 qed.
229
230 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to 
231 ((S n) \mod m) = S (n \mod m).
232 intros.
233 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
234 apply div_mod_spec_div_mod.assumption.
235 constructor 1.assumption.rewrite < plus_n_Sm.
236 apply eq_f.
237 apply div_mod.
238 assumption.
239 qed.
240
241 theorem mod_O_n: \forall n:nat.O \mod n = O.
242 intro.elim n.simplify.reflexivity.
243 simplify.reflexivity.
244 qed.
245
246 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
247 intros.
248 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
249 apply div_mod_spec_div_mod.
250 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
251 constructor 1.
252 assumption.reflexivity.
253 qed.
254
255 (* injectivity *)
256 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
257 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
258 intros.
259 rewrite < (div_times n).
260 rewrite < (div_times n q).
261 apply eq_f2.assumption.
262 reflexivity.
263 qed.
264
265 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
266 injective_times_r.
267
268 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
269 change with (\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q).
270 intros 4.
271 apply (lt_O_n_elim n H).intros.
272 apply (inj_times_r m).assumption.
273 qed.
274
275 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
276 \def lt_O_to_injective_times_r.
277
278 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
279 change with (\forall n,p,q:nat.p*(S n) = q*(S n) \to p=q).
280 intros.
281 apply (inj_times_r n p q).
282 rewrite < sym_times.
283 rewrite < (sym_times q).
284 assumption.
285 qed.
286
287 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
288 injective_times_l.
289
290 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
291 change with (\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q).
292 intros 4.
293 apply (lt_O_n_elim n H).intros.
294 apply (inj_times_l m).assumption.
295 qed.
296
297 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
298 \def lt_O_to_injective_times_l.