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 rewrite > plus_minus.
141 apply eq_plus_to_le ? ? ? H3.
148 (* the following case is symmetric *)
151 cut eq nat ((q-q1)*b+r) r1.
152 cut b \leq (q-q1)*b+r.
154 apply lt_to_not_le r1 b H4 Hcut2.
155 elim Hcut.assumption.
156 apply trans_le ? ((q-q1)*b).
158 apply le_SO_minus.exact H6.
162 rewrite > distr_times_minus.
163 rewrite > plus_minus.
168 apply eq_plus_to_le ? ? ? H5.
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
178 intros.elim H.elim H1.
179 apply inj_plus_r (q*b).
181 rewrite > div_mod_spec_to_eq a b q r q1 r1 H H1.
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.
191 (* some properties of div and mod *)
192 theorem div_times: \forall n,m:nat. div ((S n)*m) (S n) = m.
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.
201 theorem div_n_n: \forall n:nat. O < n \to div n n = S O.
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.
209 theorem mod_n_n: \forall n:nat. O < n \to mod n n = O.
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.
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).
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.
228 theorem mod_O_n: \forall n:nat.mod O n = O.
229 intro.elim n.simplify.reflexivity.
230 simplify.reflexivity.
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.
238 rewrite < div_times n.
239 rewrite < div_times n q.
240 apply eq_f2.assumption.
244 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
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.
250 apply lt_O_n_elim n H.intros.
251 apply inj_times_r m.assumption.
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.
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.
260 apply inj_times_r n p q.
262 rewrite < sym_times q.
266 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
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.
272 apply lt_O_n_elim n H.intros.
273 apply inj_times_l m.assumption.
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.