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