X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fsoftware%2Fmatita%2Flibrary%2Fnat%2Fdiv_and_mod.ma;h=f7f2883d590a8118ae6a39a8c348a75dcb2ff565;hb=5f6a2cf7134fda213ffc721ee7eebf5be30d7800;hp=ccddcca3f14416507c5edabf52e985dbf7757a68;hpb=f275a4daa42f8ab455020ec5a7ce7bf69b460c37;p=helm.git diff --git a/helm/software/matita/library/nat/div_and_mod.ma b/helm/software/matita/library/nat/div_and_mod.ma index ccddcca3f..f7f2883d5 100644 --- a/helm/software/matita/library/nat/div_and_mod.ma +++ b/helm/software/matita/library/nat/div_and_mod.ma @@ -17,6 +17,7 @@ set "baseuri" "cic:/matita/nat/div_and_mod". include "datatypes/constructors.ma". include "nat/minus.ma". + let rec mod_aux p m n: nat \def match (leb m n) with [ true \Rightarrow m @@ -100,6 +101,16 @@ simplify. apply div_aux_mod_aux. qed. +theorem eq_times_div_minus_mod: +\forall a,b:nat. O \lt b \to +(a /b)*b = a - (a \mod b). +intros. +rewrite > (div_mod a b) in \vdash (? ? ? (? % ?)) +[ apply (minus_plus_m_m (times (div a b) b) (mod a b)) +| assumption +] +qed. + inductive div_mod_spec (n,m,q,r:nat) : Prop \def div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r). @@ -193,14 +204,43 @@ unfold lt.apply le_S_S.apply le_O_n. rewrite < plus_n_O.rewrite < sym_times.reflexivity. qed. +lemma div_plus_times: \forall m,q,r:nat. r < m \to (q*m+r)/ m = q. +intros. +apply (div_mod_spec_to_eq (q*m+r) m ? ((q*m+r) \mod m) ? r) + [apply div_mod_spec_div_mod. + apply (le_to_lt_to_lt ? r) + [apply le_O_n|assumption] + |apply div_mod_spec_intro[assumption|reflexivity] + ] +qed. + +lemma mod_plus_times: \forall m,q,r:nat. r < m \to (q*m+r) \mod m = r. +intros. +apply (div_mod_spec_to_eq2 (q*m+r) m ((q*m+r)/ m) ((q*m+r) \mod m) q r) + [apply div_mod_spec_div_mod. + apply (le_to_lt_to_lt ? r) + [apply le_O_n|assumption] + |apply div_mod_spec_intro[assumption|reflexivity] + ] +qed. (* some properties of div and mod *) theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m. intros. -apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O). -goal 15. (* ?11 is closed with the following tactics *) -apply div_mod_spec_div_mod. -unfold lt.apply le_S_S.apply le_O_n. -apply div_mod_spec_times. +apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O); +[2: apply div_mod_spec_div_mod. + unfold lt.apply le_S_S.apply le_O_n. +| skip +| apply div_mod_spec_times +] +qed. + +(*a simple variant of div_times theorem *) +theorem lt_O_to_div_times: \forall a,b:nat. O \lt b \to +a*b/b = a. +intros. +rewrite > sym_times. +rewrite > (S_pred b H). +apply div_times. qed. theorem div_n_n: \forall n:nat. O < n \to n / n = S O. @@ -253,6 +293,38 @@ constructor 1. assumption.reflexivity. qed. +theorem mod_SO: \forall n:nat. mod n (S O) = O. +intro. +apply sym_eq. +apply le_n_O_to_eq. +apply le_S_S_to_le. +apply lt_mod_m_m. +apply le_n. +qed. + +theorem div_SO: \forall n:nat. div n (S O) = n. +intro. +rewrite > (div_mod ? (S O)) in \vdash (? ? ? %) + [rewrite > mod_SO. + rewrite < plus_n_O. + apply times_n_SO + |apply le_n + ] +qed. + +theorem le_div: \forall n,m. O < n \to m/n \le m. +intros. +rewrite > (div_mod m n) in \vdash (? ? %) + [apply (trans_le ? (m/n*n)) + [rewrite > times_n_SO in \vdash (? % ?). + apply le_times + [apply le_n|assumption] + |apply le_plus_n_r + ] + |assumption + ] +qed. + (* injectivity *) theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m). change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q). @@ -311,3 +383,4 @@ let rec n_divides_aux p n m acc \def (* n_divides n m = if m divides n q times, with remainder r *) definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O. +