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".
18 include "nat/orders_op.ma".
19 include "nat/compare.ma".
21 let rec mod_aux p m n: nat \def
27 |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
29 definition mod : nat \to nat \to nat \def
33 | (S p) \Rightarrow mod_aux n n p].
35 let rec div_aux p m n : nat \def
41 |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
43 definition div : nat \to nat \to nat \def
47 | (S p) \Rightarrow div_aux n n p].
49 theorem le_mod_aux_m_m:
50 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
52 apply le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m).
53 simplify.apply le_O_n.
56 simplify.intro.assumption.
57 simplify.intro.apply H.
58 cut n1 \leq (S n) \to n1-(S m) \leq n.
59 apply Hcut.assumption.
61 simplify.apply le_O_n.
62 simplify.apply trans_le ? n2 n.
63 apply le_minus_m.apply le_S_S_to_le.assumption.
66 theorem lt_mod_m_m: \forall n,m. O < m \to (mod n m) < m.
67 intros 2.elim m.apply False_ind.
68 apply not_le_Sn_O O H.
69 simplify.apply le_S_S.apply le_mod_aux_m_m.
73 theorem div_aux_mod_aux: \forall p,n,m:nat.
74 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
76 simplify.elim leb n m.
77 simplify.apply refl_eq.
78 simplify.apply refl_eq.
81 simplify.intro.apply refl_eq.
84 elim (H (n1-(S m)) m).
85 change with (n1=(S m)+(n1-(S m))).
89 apply not_le_to_lt.exact H1.
92 theorem div_mod: \forall n,m:nat. O < m \to n=(div n m)*m+(mod n m).
93 intros 2.elim m.elim (not_le_Sn_O O H).
95 apply div_aux_mod_aux.
98 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
99 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
102 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
103 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
106 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to \lnot m=O.
107 intros 4.simplify.intros.elim H.absurd le (S r) O.
108 rewrite < H1.assumption.
112 theorem div_mod_spec_div_mod:
113 \forall n,m. O < m \to (div_mod_spec n m (div n m) (mod n m)).
115 apply div_mod_spec_intro.
116 apply lt_mod_m_m.assumption.
117 apply div_mod.assumption.
120 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
121 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
123 intros.elim H.elim H1.
124 apply nat_compare_elim q q1.intro.
126 cut eq nat ((q1-q)*b+r1) r.
127 cut b \leq (q1-q)*b+r1.
129 apply lt_to_not_le r b H2 Hcut2.
130 elim Hcut.assumption.
131 apply trans_le ? ((q1-q)*b) ?.
133 apply le_SO_minus.exact H6.
137 rewrite > distr_times_minus.
138 (* ATTENZIONE ALL' ORDINAMENTO DEI GOALS *)
139 rewrite > plus_minus ? ? ? ?.
144 apply eq_plus_to_le ? ? ? H3.
151 (* the following case is symmetric *)
154 cut eq nat ((q-q1)*b+r) r1.
155 cut b \leq (q-q1)*b+r.
157 apply lt_to_not_le r1 b H4 Hcut2.
158 elim Hcut.assumption.
159 apply trans_le ? ((q-q1)*b) ?.
161 apply le_SO_minus.exact H6.
165 rewrite > distr_times_minus.
166 rewrite > plus_minus ? ? ? ?.
171 apply eq_plus_to_le ? ? ? H5.