1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / Matita is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/nat/div_and_mod".
17 include "nat/minus.ma".
19 let rec mod_aux p m n: nat \def
25 |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
27 definition mod : nat \to nat \to nat \def
31 | (S p) \Rightarrow mod_aux n n p].
33 let rec div_aux p m n : nat \def
39 |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
41 definition div : nat \to nat \to nat \def
45 | (S p) \Rightarrow div_aux n n p].
47 theorem le_mod_aux_m_m:
48 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
50 apply le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m).
51 simplify.apply le_O_n.
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.
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.
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.
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)).
74 simplify.elim leb n m.
75 simplify.apply refl_eq.
76 simplify.apply refl_eq.
79 simplify.intro.apply refl_eq.
82 elim (H (n1-(S m)) m).
83 change with (n1=(S m)+(n1-(S m))).
87 apply not_le_to_lt.exact H1.
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).
93 apply div_aux_mod_aux.
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).
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).
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.
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)).
113 apply div_mod_spec_intro.
114 apply lt_mod_m_m.assumption.
115 apply div_mod.assumption.
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
121 intros.elim H.elim H1.
122 apply nat_compare_elim q q1.intro.
124 cut eq nat ((q1-q)*b+r1) r.
125 cut b \leq (q1-q)*b+r1.
127 apply lt_to_not_le r b H2 Hcut2.
128 elim Hcut.assumption.
129 apply trans_le ? ((q1-q)*b) ?.
131 apply le_SO_minus.exact H6.
135 rewrite > distr_times_minus.
136 (* ATTENZIONE ALL' ORDINAMENTO DEI GOALS *)
137 rewrite > plus_minus ? ? ? ?.
142 apply eq_plus_to_le ? ? ? H3.
149 (* the following case is symmetric *)
152 cut eq nat ((q-q1)*b+r) r1.
153 cut b \leq (q-q1)*b+r.
155 apply lt_to_not_le r1 b H4 Hcut2.
156 elim Hcut.assumption.
157 apply trans_le ? ((q-q1)*b) ?.
159 apply le_SO_minus.exact H6.
163 rewrite > distr_times_minus.
164 rewrite > plus_minus ? ? ? ?.
169 apply eq_plus_to_le ? ? ? H5.
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
179 intros.elim H.elim H1.
180 apply inj_plus_r (q*b).
182 rewrite > div_mod_spec_to_eq a b q r q1 r1 H H1.
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.
192 (* some properties of div and mod *)
193 theorem div_times: \forall n,m:nat. div ((S n)*m) (S n) = m.
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.
202 theorem div_n_n: \forall n:nat. O < n \to div n n = S O.
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.
210 theorem mod_n_n: \forall n:nat. O < n \to mod n n = O.
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.
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).
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.
229 theorem mod_O_n: \forall n:nat.mod O n = O.
230 intro.elim n.simplify.reflexivity.
231 simplify.reflexivity.
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.
239 rewrite < div_times n.
240 rewrite < div_times n q.
241 apply eq_f2.assumption.
245 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
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.
251 apply lt_O_n_elim n H.intros.
252 apply inj_times_r m.assumption.
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.
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.
261 apply inj_times_r n p q.
263 rewrite < sym_times q.
267 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
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.
273 apply lt_O_n_elim n H.intros.
274 apply inj_times_l m.assumption.
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.