X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fmatita%2Flibrary%2Fnat%2Fdiv_and_mod.ma;h=26e3dcc829a604fa0b1453fe249928a2dac154dd;hb=5c6b8eec9db4119a87eb4fd4055f1ac31a713d90;hp=619ba68151a48183109400077fc9e1d034d6919a;hpb=7bbce6bc163892cfd99cfcda65db42001b86789f;p=helm.git diff --git a/helm/matita/library/nat/div_and_mod.ma b/helm/matita/library/nat/div_and_mod.ma index 619ba6815..26e3dcc82 100644 --- a/helm/matita/library/nat/div_and_mod.ma +++ b/helm/matita/library/nat/div_and_mod.ma @@ -1,5 +1,5 @@ (**************************************************************************) -(* ___ *) +(* ___ *) (* ||M|| *) (* ||A|| A project by Andrea Asperti *) (* ||T|| *) @@ -15,8 +15,6 @@ set "baseuri" "cic:/matita/nat/div_and_mod". include "nat/minus.ma". -include "nat/le_arith.ma". -include "nat/compare.ma". let rec mod_aux p m n: nat \def match (leb m n) with @@ -32,6 +30,9 @@ match m with [O \Rightarrow m | (S p) \Rightarrow mod_aux n n p]. +interpretation "natural remainder" 'module x y = + (cic:/matita/nat/div_and_mod/mod.con x y). + let rec div_aux p m n : nat \def match (leb m n) with [ true \Rightarrow O @@ -46,6 +47,9 @@ 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). + theorem le_mod_aux_m_m: \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m. intro.elim p. @@ -63,7 +67,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. O < m \to (mod n m) < m. +theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod 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. @@ -89,7 +93,7 @@ change with m < n1. apply not_le_to_lt.exact H1. qed. -theorem div_mod: \forall n,m:nat. O < m \to n=(div n m)*m+(mod n m). +theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m). intros 2.elim m.elim (not_le_Sn_O O H). simplify. apply div_aux_mod_aux. @@ -103,14 +107,14 @@ definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def \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 \lnot m=O. +theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq 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. 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 (n / m) (n \mod m)). intros. apply div_mod_spec_intro. apply lt_mod_m_m.assumption. @@ -128,20 +132,18 @@ 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 ? ((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. apply plus_to_minus. -apply eq_plus_to_le ? ? ? H3. apply H3. apply le_times_r. apply lt_to_le. @@ -156,39 +158,93 @@ 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 ? ((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. apply plus_to_minus. -apply eq_plus_to_le ? ? ? H5. apply H5. apply le_times_r. apply lt_to_le. apply H6. 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. -theorem div_times: \forall n,m:nat. div ((S n)*m) (S n) = m. +(* 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. 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 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. +qed. + +theorem eq_div_O: \forall n,m. n < m \to n / m = O. +intros. +apply div_mod_spec_to_eq n m (n/m) (n \mod m) O n. +apply div_mod_spec_div_mod. +apply le_to_lt_to_lt O n m. +apply le_O_n.assumption. +constructor 1.assumption.reflexivity. +qed. + +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. +qed. + +theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to +((S n) \mod m) = S (n \mod m). +intros. +apply div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod 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.O \mod 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. @@ -201,6 +257,16 @@ 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. @@ -212,3 +278,13 @@ 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.