X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fsoftware%2Fmatita%2Flibrary%2Fnat%2Fdiv_and_mod.ma;h=e31f2d678cd739a075e5e33b5847998157f5d4fc;hb=0137a346eaaf9ae7a0b23c7a3b4c6628073b7dfb;hp=e9831f82ad1ec5cc01decf9e920f9e80518c3f64;hpb=55b82bd235d82ff7f0a40d980effe1efde1f5073;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 e9831f82a..e31f2d678 100644 --- a/helm/software/matita/library/nat/div_and_mod.ma +++ b/helm/software/matita/library/nat/div_and_mod.ma @@ -12,8 +12,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 @@ -27,11 +26,10 @@ match (leb m n) with definition mod : nat \to nat \to nat \def \lambda n,m. match m with -[O \Rightarrow m +[O \Rightarrow n | (S p) \Rightarrow mod_aux n n p]. -interpretation "natural remainder" 'module x y = - (cic:/matita/nat/div_and_mod/mod.con x y). +interpretation "natural remainder" 'module x y = (mod x y). let rec div_aux p m n : nat \def match (leb m n) with @@ -47,8 +45,7 @@ match m with [O \Rightarrow S n | (S p) \Rightarrow div_aux n n p]. -interpretation "natural divide" 'divide x y = - (cic:/matita/nat/div_and_mod/div.con x y). +interpretation "natural divide" 'divide x y = (div x y). theorem le_mod_aux_m_m: \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m. @@ -99,6 +96,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). @@ -140,11 +147,14 @@ apply le_plus_n. rewrite < sym_times. rewrite > distr_times_minus. rewrite > plus_minus. +lapply(plus_to_minus ??? H3); demodulate. reflexivity. +(* rewrite > sym_times. rewrite < H5. -rewrite < sym_times. +rewrite < sym_times. apply plus_to_minus. apply H3. +*) apply le_times_r. apply lt_to_le. apply H6. @@ -188,26 +198,56 @@ qed. theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O. intros.constructor 1. -unfold lt.apply le_S_S.apply le_O_n. -rewrite < plus_n_O.rewrite < sym_times.reflexivity. +unfold lt.apply le_S_S.apply le_O_n. demodulate. reflexivity. +(*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. intros. apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O). apply div_mod_spec_div_mod.assumption. -constructor 1.assumption. -rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity. +constructor 1.assumption. demodulate. reflexivity. (* +rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.*) qed. theorem eq_div_O: \forall n,m. n < m \to n / m = O. @@ -223,8 +263,8 @@ theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O. intros. apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O). apply div_mod_spec_div_mod.assumption. -constructor 1.assumption. -rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity. +constructor 1.assumption. demodulate. reflexivity.(* +rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.*) qed. theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to @@ -252,6 +292,50 @@ 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 or_div_mod: \forall n,q. O < q \to +((S (n \mod q)=q) \land S n = (S (div n q)) * q \lor +((S (n \mod q) sym_plus. + rewrite < H1 in ⊢ (? ? ? (? ? %)). + rewrite < plus_n_Sm. + apply eq_f. + apply div_mod. + 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). @@ -266,7 +350,7 @@ variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def injective_times_r. theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m). -change with (\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q). +simplify. intros 4. apply (lt_O_n_elim n H).intros. apply (inj_times_r m).assumption. @@ -276,11 +360,11 @@ variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q \def lt_O_to_injective_times_r. theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)). -change with (\forall n,p,q:nat.p*(S n) = q*(S n) \to p=q). +simplify. intros. -apply (inj_times_r n p q). +apply (inj_times_r n x y). rewrite < sym_times. -rewrite < (sym_times q). +rewrite < (sym_times y). assumption. qed. @@ -288,7 +372,7 @@ variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def injective_times_l. theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n). -change with (\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q). +simplify. intros 4. apply (lt_O_n_elim n H).intros. apply (inj_times_l m).assumption. @@ -296,3 +380,19 @@ qed. variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q \def lt_O_to_injective_times_l. + + +(* n_divides computes the pair (div,mod) *) + +(* p is just an upper bound, acc is an accumulator *) +let rec n_divides_aux p n m acc \def + match n \mod m with + [ O \Rightarrow + match p with + [ O \Rightarrow pair nat nat acc n + | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)] + | (S a) \Rightarrow pair nat nat acc n]. + +(* 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. +