]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/div_and_mod.ma
New naming policy for local variables.
[helm.git] / helm / matita / library / nat / div_and_mod.ma
1 (**************************************************************************)
2 (*       ___                                                                *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        Matita is distributed under the terms of the          *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/nat/div_and_mod".
16
17 include "nat/minus.ma".
18 include "nat/orders_op.ma".
19 include "nat/compare.ma".
20
21 let rec mod_aux p m n: nat \def
22 match (leb m n) with
23 [ true \Rightarrow m
24 | false \Rightarrow
25   match p with
26   [O \Rightarrow m
27   |(S q) \Rightarrow mod_aux q (minus m (S n)) n]].
28
29 definition mod : nat \to nat \to nat \def
30 \lambda n,m.
31 match m with 
32 [O \Rightarrow m
33 | (S p) \Rightarrow mod_aux n n p]. 
34
35 let rec div_aux p m n : nat \def
36 match (leb m n) with
37 [ true \Rightarrow O
38 | false \Rightarrow
39   match p with
40   [O \Rightarrow O
41   |(S q) \Rightarrow S (div_aux q (minus m (S n)) n)]].
42
43 definition div : nat \to nat \to nat \def
44 \lambda n,m.
45 match m with 
46 [O \Rightarrow S n
47 | (S p) \Rightarrow div_aux n n p]. 
48
49 theorem le_mod_aux_m_m: 
50 \forall p,n,m. (le n p) \to (le (mod_aux p n m) m).
51 intro.elim p.
52 apply le_n_O_elim n H (\lambda n.(le (mod_aux O n m) m)).
53 simplify.apply le_O_n.
54 simplify.
55 apply leb_elim n1 m.
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.
60 elim n1.
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.
64 qed.
65
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.
70 apply le_n.
71 qed.
72
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) )).
75 intro.elim p.
76 simplify.elim leb n m.
77 simplify.apply refl_eq.
78 simplify.apply refl_eq.
79 simplify.
80 apply leb_elim n1 m.
81 simplify.intro.apply refl_eq.
82 simplify.intro.
83 rewrite > assoc_plus. 
84 elim (H (minus n1 (S m)) m).
85 change with (eq nat n1 (plus (S m) (minus n1 (S m)))).
86 rewrite < sym_plus.
87 apply plus_minus_m_m.
88 change with lt m n1.
89 apply not_le_to_lt.exact H1.
90 qed.
91
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).
95 simplify.
96 apply div_aux_mod_aux.
97 qed.
98
99 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
100 div_mod_spec_intro: 
101 (lt r m) \to (eq nat n (plus (times q m) r)) \to (div_mod_spec n m q r).
102
103 (* 
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))).
106 *)
107
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.
111 exact not_le_Sn_O r.
112 qed.
113
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)).
116 intros.
117 apply div_mod_spec_intro.
118 apply lt_mod_m_m.assumption.
119 apply div_mod.assumption.
120 qed. 
121
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 
124 (eq nat q q1).
125 intros.elim H.elim H1.
126 apply nat_compare_elim q q1.intro.
127 apply False_ind.
128 cut eq nat (plus (times (minus q1 q) b) r1) r.
129 cut le b (plus (times (minus q1 q) b) r1).
130 cut le b r.
131 apply lt_to_not_le r b H2 Hcut2.
132 elim Hcut.assumption.
133 apply trans_le ? (times (minus q1 q) b) ?.
134 apply le_times_n.
135 apply le_SO_minus.exact H6.
136 rewrite < sym_plus.
137 apply le_plus_n.
138 rewrite < sym_times.
139 rewrite > distr_times_minus.
140 (* ATTENZIONE ALL' ORDINAMENTO DEI GOALS *)
141 rewrite > plus_minus ? ? ? ?.
142 rewrite > sym_times.
143 rewrite < H5.
144 rewrite < sym_times.
145 apply plus_to_minus.
146 apply eq_plus_to_le ? ? ? H3.
147 apply H3.
148 apply le_times_r.
149 apply lt_to_le.
150 apply H6.
151 (* eq case *)
152 intros.assumption.
153 (* the following case is symmetric *)
154 intro.
155 apply False_ind.
156 cut eq nat (plus (times (minus q q1) b) r) r1.
157 cut le b (plus (times (minus q q1) b) r).
158 cut le b r1.
159 apply lt_to_not_le r1 b H4 Hcut2.
160 elim Hcut.assumption.
161 apply trans_le ? (times (minus q q1) b) ?.
162 apply le_times_n.
163 apply le_SO_minus.exact H6.
164 rewrite < sym_plus.
165 apply le_plus_n.
166 rewrite < sym_times.
167 rewrite > distr_times_minus.
168 rewrite > plus_minus ? ? ? ?.
169 rewrite > sym_times.
170 rewrite < H3.
171 rewrite < sym_times.
172 apply plus_to_minus.
173 apply eq_plus_to_le ? ? ? H5.
174 apply H5.
175 apply le_times_r.
176 apply lt_to_le.
177 apply H6.
178 qed.