]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/div_and_mod.ma
* Obsolete debugging comments removed
[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 rewrite > plus_minus.
137 rewrite > sym_times.
138 rewrite < H5.
139 rewrite < sym_times.
140 apply plus_to_minus.
141 apply eq_plus_to_le ? ? ? H3.
142 apply H3.
143 apply le_times_r.
144 apply lt_to_le.
145 apply H6.
146 (* eq case *)
147 intros.assumption.
148 (* the following case is symmetric *)
149 intro.
150 apply False_ind.
151 cut eq nat ((q-q1)*b+r) r1.
152 cut b \leq (q-q1)*b+r.
153 cut b \leq r1.
154 apply lt_to_not_le r1 b H4 Hcut2.
155 elim Hcut.assumption.
156 apply trans_le ? ((q-q1)*b).
157 apply le_times_n.
158 apply le_SO_minus.exact H6.
159 rewrite < sym_plus.
160 apply le_plus_n.
161 rewrite < sym_times.
162 rewrite > distr_times_minus.
163 rewrite > plus_minus.
164 rewrite > sym_times.
165 rewrite < H3.
166 rewrite < sym_times.
167 apply plus_to_minus.
168 apply eq_plus_to_le ? ? ? H5.
169 apply H5.
170 apply le_times_r.
171 apply lt_to_le.
172 apply H6.
173 qed.
174
175 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
176 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to 
177 (eq nat r r1).
178 intros.elim H.elim H1.
179 apply inj_plus_r (q*b).
180 rewrite < H3.
181 rewrite > div_mod_spec_to_eq a b q r q1 r1 H H1.
182 assumption.
183 qed.
184
185 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
186 intros.constructor 1.
187 simplify.apply le_S_S.apply le_O_n.
188 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
189 qed.
190
191 (* some properties of div and mod *)
192 theorem div_times: \forall n,m:nat. div ((S n)*m) (S n) = m.
193 intros.
194 apply div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O.
195 goal 15. (* ?11 is closed with the following tactics *)
196 apply div_mod_spec_div_mod.
197 simplify.apply le_S_S.apply le_O_n.
198 apply div_mod_spec_times.
199 qed.
200
201 theorem div_n_n: \forall n:nat. O < n \to div n n = S O.
202 intros.
203 apply div_mod_spec_to_eq n n (div n n) (mod n n) (S O) O.
204 apply div_mod_spec_div_mod.assumption.
205 constructor 1.assumption.
206 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
207 qed.
208
209 theorem mod_n_n: \forall n:nat. O < n \to mod n n = O.
210 intros.
211 apply div_mod_spec_to_eq2 n n (div n n) (mod n n) (S O) O.
212 apply div_mod_spec_div_mod.assumption.
213 constructor 1.assumption.
214 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
215 qed.
216
217 theorem mod_S: \forall n,m:nat. O < m \to S (mod n m) < m \to 
218 (mod (S n) m) = S (mod n m).
219 intros.
220 apply div_mod_spec_to_eq2 (S n) m (div (S n) m) (mod (S n) m) (div n m) (S (mod n m)).
221 apply div_mod_spec_div_mod.assumption.
222 constructor 1.assumption.rewrite < plus_n_Sm.
223 apply eq_f.
224 apply div_mod.
225 assumption.
226 qed.
227
228 theorem mod_O_n: \forall n:nat.mod O n = O.
229 intro.elim n.simplify.reflexivity.
230 simplify.reflexivity.
231 qed.
232
233
234 (* injectivity *)
235 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
236 change with \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q.
237 intros.
238 rewrite < div_times n.
239 rewrite < div_times n q.
240 apply eq_f2.assumption.
241 reflexivity.
242 qed.
243
244 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
245 injective_times_r.
246
247 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
248 change with \forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q.
249 intros 4.
250 apply lt_O_n_elim n H.intros.
251 apply inj_times_r m.assumption.
252 qed.
253
254 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
255 \def lt_O_to_injective_times_r.
256
257 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
258 change with \forall n,p,q:nat.p*(S n) = q*(S n) \to p=q.
259 intros.
260 apply inj_times_r n p q.
261 rewrite < sym_times.
262 rewrite < sym_times q.
263 assumption.
264 qed.
265
266 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
267 injective_times_l.
268
269 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
270 change with \forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q.
271 intros 4.
272 apply lt_O_n_elim n H.intros.
273 apply inj_times_l m.assumption.
274 qed.
275
276 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
277 \def lt_O_to_injective_times_l.