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 (minus 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 (minus 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. (le n p) \to (le (mod_aux p n m) m).
52 apply le_n_O_elim n H (\lambda n.(le (mod_aux O n m) m)).
53 simplify.apply le_O_n.
56 simplify.intro.assumption.
57 simplify.intro.apply H.
58 cut (le n1 (S n)) \to (le (minus n1 (S m)) 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. (lt O m) \to (lt (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 (eq nat n (plus (times (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 (minus n1 (S m)) m).
85 change with (eq nat n1 (plus (S m) (minus n1 (S m)))).
89 apply not_le_to_lt.exact H1.
92 theorem div_mod: \forall n,m:nat.
93 (lt O m) \to (eq nat n (plus (times (div n m) m) (mod n m))).
94 intros 2.elim m.elim (not_le_Sn_O O H).
96 apply div_aux_mod_aux.
99 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
101 (lt r m) \to (eq nat n (plus (times q m) r)) \to (div_mod_spec n m q r).
104 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
105 \lambda n,m,q,r:nat.(And (lt r m) (eq nat n (plus (times q m) r))).
108 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).
109 intros 4.simplify.intros.elim H.absurd le (S r) O.
110 rewrite < H1.assumption.
114 theorem div_mod_spec_div_mod:
115 \forall n,m. (lt O m) \to (div_mod_spec n m (div n m) (mod n m)).
117 apply div_mod_spec_intro.
118 apply lt_mod_m_m.assumption.
119 apply div_mod.assumption.
122 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
123 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
125 intros.elim H.elim H1.
126 apply nat_compare_elim q q1.intro.
128 cut eq nat (plus (times (minus q1 q) b) r1) r.
129 cut le b (plus (times (minus q1 q) b) r1).
131 apply lt_to_not_le r b H2 Hcut2.
132 elim Hcut.assumption.
133 apply trans_le ? (times (minus q1 q) b) ?.
135 apply le_SO_minus.exact H6.
139 rewrite > distr_times_minus.
140 (* ATTENZIONE ALL' ORDINAMENTO DEI GOALS *)
141 rewrite > plus_minus ? ? ? ?.
146 apply eq_plus_to_le ? ? ? H3.
153 (* the following case is symmetric *)
156 cut eq nat (plus (times (minus q q1) b) r) r1.
157 cut le b (plus (times (minus q q1) b) r).
159 apply lt_to_not_le r1 b H4 Hcut2.
160 elim Hcut.assumption.
161 apply trans_le ? (times (minus q q1) b) ?.
163 apply le_SO_minus.exact H6.
167 rewrite > distr_times_minus.
168 rewrite > plus_minus ? ? ? ?.
173 apply eq_plus_to_le ? ? ? H5.