X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fmatita%2Flibrary%2Fnat%2Fdiv_and_mod.ma;h=73344c7c46b0cf1466b0aea949755ced792fc13a;hb=ab44166935d77276c04fcce50aa8281292776e29;hp=2b00a97abcf4c867d7b24bb5704beaa6d2ee562e;hpb=5e1fd9ee5ced5737c7fd4f25fca47feda1fda8e9;p=helm.git diff --git a/helm/matita/library/nat/div_and_mod.ma b/helm/matita/library/nat/div_and_mod.ma index 2b00a97ab..73344c7c4 100644 --- a/helm/matita/library/nat/div_and_mod.ma +++ b/helm/matita/library/nat/div_and_mod.ma @@ -15,8 +15,6 @@ set "baseuri" "cic:/matita/nat/div_and_mod". include "nat/minus.ma". -include "nat/orders_op.ma". -include "nat/compare.ma". let rec mod_aux p m n: nat \def match (leb m n) with @@ -24,7 +22,7 @@ match (leb m n) with | false \Rightarrow match p with [O \Rightarrow m - |(S q) \Rightarrow mod_aux q (minus m (S n)) n]]. + |(S q) \Rightarrow mod_aux q (m-(S n)) n]]. definition mod : nat \to nat \to nat \def \lambda n,m. @@ -38,7 +36,7 @@ match (leb m n) with | false \Rightarrow match p with [O \Rightarrow O - |(S q) \Rightarrow S (div_aux q (minus m (S n)) n)]]. + |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]]. definition div : nat \to nat \to nat \def \lambda n,m. @@ -47,15 +45,15 @@ match m with | (S p) \Rightarrow div_aux n n p]. theorem le_mod_aux_m_m: -\forall p,n,m. (le n p) \to (le (mod_aux p n m) m). +\forall p,n,m. n \leq p \to (mod_aux p n m) \leq m. intro.elim p. -apply le_n_O_elim n H (\lambda n.(le (mod_aux O n m) m)). +apply le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m). simplify.apply le_O_n. simplify. apply leb_elim n1 m. simplify.intro.assumption. simplify.intro.apply H. -cut (le n1 (S n)) \to (le (minus n1 (S m)) n). +cut n1 \leq (S n) \to n1-(S m) \leq n. apply Hcut.assumption. elim n1. simplify.apply le_O_n. @@ -63,7 +61,7 @@ simplify.apply trans_le ? n2 n. apply le_minus_m.apply le_S_S_to_le.assumption. qed. -theorem lt_mod_m_m: \forall n,m. (lt O m) \to (lt (mod n m) m). +theorem lt_mod_m_m: \forall n,m. O < m \to (mod n m) < m. intros 2.elim m.apply False_ind. apply not_le_Sn_O O H. simplify.apply le_S_S.apply le_mod_aux_m_m. @@ -71,7 +69,7 @@ apply le_n. qed. theorem div_aux_mod_aux: \forall p,n,m:nat. -(eq nat n (plus (times (div_aux p n m) (S m)) (mod_aux p n m) )). +(n=(div_aux p n m)*(S m) + (mod_aux p n m)). intro.elim p. simplify.elim leb n m. simplify.apply refl_eq. @@ -81,38 +79,36 @@ apply leb_elim n1 m. simplify.intro.apply refl_eq. simplify.intro. rewrite > assoc_plus. -elim (H (minus n1 (S m)) m). -change with (eq nat n1 (plus (S m) (minus n1 (S m)))). +elim (H (n1-(S m)) m). +change with (n1=(S m)+(n1-(S m))). rewrite < sym_plus. apply plus_minus_m_m. -change with lt m n1. +change with m < n1. apply not_le_to_lt.exact H1. qed. -theorem div_mod: \forall n,m:nat. -(lt O m) \to (eq nat n (plus (times (div n m) m) (mod n m))). +theorem div_mod: \forall n,m:nat. O < m \to n=(div n m)*m+(mod n m). intros 2.elim m.elim (not_le_Sn_O O H). simplify. apply div_aux_mod_aux. qed. inductive div_mod_spec (n,m,q,r:nat) : Prop \def -div_mod_spec_intro: -(lt r m) \to (eq nat n (plus (times q m) r)) \to (div_mod_spec n m q r). +div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r). (* definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def -\lambda n,m,q,r:nat.(And (lt r m) (eq nat n (plus (times q m) r))). +\lambda n,m,q,r:nat.r < m \land n=q*m+r). *) -theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to Not (eq nat m O). +theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to \lnot m=O. intros 4.simplify.intros.elim H.absurd le (S r) O. rewrite < H1.assumption. exact not_le_Sn_O r. qed. theorem div_mod_spec_div_mod: -\forall n,m. (lt O m) \to (div_mod_spec n m (div n m) (mod n m)). +\forall n,m. O < m \to (div_mod_spec n m (div n m) (mod n m)). intros. apply div_mod_spec_intro. apply lt_mod_m_m.assumption. @@ -125,20 +121,19 @@ theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1. intros.elim H.elim H1. apply nat_compare_elim q q1.intro. apply False_ind. -cut eq nat (plus (times (minus q1 q) b) r1) r. -cut le b (plus (times (minus q1 q) b) r1). -cut le b r. +cut eq nat ((q1-q)*b+r1) r. +cut b \leq (q1-q)*b+r1. +cut b \leq r. apply lt_to_not_le r b H2 Hcut2. elim Hcut.assumption. -apply trans_le ? (times (minus q1 q) b) ?. +apply trans_le ? ((q1-q)*b). apply le_times_n. apply le_SO_minus.exact H6. rewrite < sym_plus. apply le_plus_n. rewrite < sym_times. rewrite > distr_times_minus. -(* ATTENZIONE ALL' ORDINAMENTO DEI GOALS *) -rewrite > plus_minus ? ? ? ?. +rewrite > plus_minus. rewrite > sym_times. rewrite < H5. rewrite < sym_times. @@ -153,19 +148,19 @@ intros.assumption. (* the following case is symmetric *) intro. apply False_ind. -cut eq nat (plus (times (minus q q1) b) r) r1. -cut le b (plus (times (minus q q1) b) r). -cut le b r1. +cut eq nat ((q-q1)*b+r) r1. +cut b \leq (q-q1)*b+r. +cut b \leq r1. apply lt_to_not_le r1 b H4 Hcut2. elim Hcut.assumption. -apply trans_le ? (times (minus q q1) b) ?. +apply trans_le ? ((q-q1)*b). apply le_times_n. apply le_SO_minus.exact H6. rewrite < sym_plus. apply le_plus_n. rewrite < sym_times. rewrite > distr_times_minus. -rewrite > plus_minus ? ? ? ?. +rewrite > plus_minus. rewrite > sym_times. rewrite < H3. rewrite < sym_times. @@ -175,4 +170,108 @@ apply H5. apply le_times_r. apply lt_to_le. apply H6. -qed. \ No newline at end of file +qed. + +theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1. +(div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to +(eq nat r r1). +intros.elim H.elim H1. +apply inj_plus_r (q*b). +rewrite < H3. +rewrite > div_mod_spec_to_eq a b q r q1 r1 H H1. +assumption. +qed. + +theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O. +intros.constructor 1. +simplify.apply le_S_S.apply le_O_n. +rewrite < plus_n_O.rewrite < sym_times.reflexivity. +qed. + +(* some properties of div and mod *) +theorem div_times: \forall n,m:nat. div ((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. +simplify.apply le_S_S.apply le_O_n. +apply div_mod_spec_times. +qed. + +theorem div_n_n: \forall n:nat. O < n \to div n n = S O. +intros. +apply div_mod_spec_to_eq n n (div n n) (mod n n) (S O) O. +apply div_mod_spec_div_mod.assumption. +constructor 1.assumption. +rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity. +qed. + +theorem mod_n_n: \forall n:nat. O < n \to mod n n = O. +intros. +apply div_mod_spec_to_eq2 n n (div n n) (mod n n) (S O) O. +apply div_mod_spec_div_mod.assumption. +constructor 1.assumption. +rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity. +qed. + +theorem mod_S: \forall n,m:nat. O < m \to S (mod n m) < m \to +(mod (S n) m) = S (mod n m). +intros. +apply div_mod_spec_to_eq2 (S n) m (div (S n) m) (mod (S n) m) (div n m) (S (mod n m)). +apply div_mod_spec_div_mod.assumption. +constructor 1.assumption.rewrite < plus_n_Sm. +apply eq_f. +apply div_mod. +assumption. +qed. + +theorem mod_O_n: \forall n:nat.mod O n = O. +intro.elim n.simplify.reflexivity. +simplify.reflexivity. +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. +intros. +rewrite < div_times n. +rewrite < div_times n q. +apply eq_f2.assumption. +reflexivity. +qed. + +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. +intros 4. +apply lt_O_n_elim n H.intros. +apply inj_times_r m.assumption. +qed. + +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. +intros. +apply inj_times_r n p q. +rewrite < sym_times. +rewrite < sym_times q. +assumption. +qed. + +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. +intros 4. +apply lt_O_n_elim n H.intros. +apply inj_times_l m.assumption. +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.