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