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