13156
|
1 |
(* Title: HOL/Divides.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
Copyright 1993 University of Cambridge
|
|
5 |
|
|
6 |
The division operators div, mod and the divides relation "dvd"
|
|
7 |
*)
|
|
8 |
|
|
9 |
(* ML legacy bindings *)
|
|
10 |
|
|
11 |
val div_def = thm "div_def";
|
|
12 |
val mod_def = thm "mod_def";
|
|
13 |
val dvd_def = thm "dvd_def";
|
|
14 |
val quorem_def = thm "quorem_def";
|
|
15 |
|
|
16 |
structure Divides =
|
|
17 |
struct
|
|
18 |
val div_def = div_def
|
|
19 |
val mod_def = mod_def
|
|
20 |
val dvd_def = dvd_def
|
|
21 |
val quorem_def = quorem_def
|
|
22 |
end;
|
|
23 |
|
|
24 |
(** Less-then properties **)
|
|
25 |
|
|
26 |
bind_thm ("wf_less_trans", [eq_reflection, wf_pred_nat RS wf_trancl] MRS
|
|
27 |
def_wfrec RS trans);
|
|
28 |
|
|
29 |
Goal "(%m. m mod n) = wfrec (trancl pred_nat) \
|
|
30 |
\ (%f j. if j<n | n=0 then j else f (j-n))";
|
|
31 |
by (simp_tac (simpset() addsimps [mod_def]) 1);
|
|
32 |
qed "mod_eq";
|
|
33 |
|
|
34 |
Goal "(%m. m div n) = wfrec (trancl pred_nat) \
|
|
35 |
\ (%f j. if j<n | n=0 then 0 else Suc (f (j-n)))";
|
|
36 |
by (simp_tac (simpset() addsimps [div_def]) 1);
|
|
37 |
qed "div_eq";
|
|
38 |
|
|
39 |
|
|
40 |
(** Aribtrary definitions for division by zero. Useful to simplify
|
|
41 |
certain equations **)
|
|
42 |
|
|
43 |
Goal "a div 0 = (0::nat)";
|
|
44 |
by (rtac (div_eq RS wf_less_trans) 1);
|
|
45 |
by (Asm_simp_tac 1);
|
|
46 |
qed "DIVISION_BY_ZERO_DIV"; (*NOT for adding to default simpset*)
|
|
47 |
|
|
48 |
Goal "a mod 0 = (a::nat)";
|
|
49 |
by (rtac (mod_eq RS wf_less_trans) 1);
|
|
50 |
by (Asm_simp_tac 1);
|
|
51 |
qed "DIVISION_BY_ZERO_MOD"; (*NOT for adding to default simpset*)
|
|
52 |
|
|
53 |
fun div_undefined_case_tac s i =
|
|
54 |
case_tac s i THEN
|
|
55 |
Full_simp_tac (i+1) THEN
|
|
56 |
asm_simp_tac (simpset() addsimps [DIVISION_BY_ZERO_DIV,
|
|
57 |
DIVISION_BY_ZERO_MOD]) i;
|
|
58 |
|
|
59 |
(*** Remainder ***)
|
|
60 |
|
|
61 |
Goal "m<n ==> m mod n = (m::nat)";
|
|
62 |
by (rtac (mod_eq RS wf_less_trans) 1);
|
|
63 |
by (Asm_simp_tac 1);
|
|
64 |
qed "mod_less";
|
|
65 |
Addsimps [mod_less];
|
|
66 |
|
|
67 |
Goal "~ m < (n::nat) ==> m mod n = (m-n) mod n";
|
|
68 |
by (div_undefined_case_tac "n=0" 1);
|
|
69 |
by (rtac (mod_eq RS wf_less_trans) 1);
|
|
70 |
by (asm_simp_tac (simpset() addsimps [diff_less, cut_apply, less_eq]) 1);
|
|
71 |
qed "mod_geq";
|
|
72 |
|
|
73 |
(*Avoids the ugly ~m<n above*)
|
|
74 |
Goal "(n::nat) <= m ==> m mod n = (m-n) mod n";
|
|
75 |
by (asm_simp_tac (simpset() addsimps [mod_geq, not_less_iff_le]) 1);
|
|
76 |
qed "le_mod_geq";
|
|
77 |
|
|
78 |
Goal "m mod (n::nat) = (if m<n then m else (m-n) mod n)";
|
|
79 |
by (asm_simp_tac (simpset() addsimps [mod_geq]) 1);
|
|
80 |
qed "mod_if";
|
|
81 |
|
|
82 |
Goal "m mod Suc 0 = 0";
|
|
83 |
by (induct_tac "m" 1);
|
|
84 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [mod_geq])));
|
|
85 |
qed "mod_1";
|
|
86 |
Addsimps [mod_1];
|
|
87 |
|
|
88 |
Goal "n mod n = (0::nat)";
|
|
89 |
by (div_undefined_case_tac "n=0" 1);
|
|
90 |
by (asm_simp_tac (simpset() addsimps [mod_geq]) 1);
|
|
91 |
qed "mod_self";
|
|
92 |
Addsimps [mod_self];
|
|
93 |
|
|
94 |
Goal "(m+n) mod n = m mod (n::nat)";
|
|
95 |
by (subgoal_tac "(n + m) mod n = (n+m-n) mod n" 1);
|
|
96 |
by (stac (mod_geq RS sym) 2);
|
|
97 |
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [add_commute])));
|
|
98 |
qed "mod_add_self2";
|
|
99 |
|
|
100 |
Goal "(n+m) mod n = m mod (n::nat)";
|
|
101 |
by (asm_simp_tac (simpset() addsimps [add_commute, mod_add_self2]) 1);
|
|
102 |
qed "mod_add_self1";
|
|
103 |
|
|
104 |
Addsimps [mod_add_self1, mod_add_self2];
|
|
105 |
|
|
106 |
Goal "(m + k*n) mod n = m mod (n::nat)";
|
|
107 |
by (induct_tac "k" 1);
|
|
108 |
by (ALLGOALS
|
|
109 |
(asm_simp_tac
|
|
110 |
(simpset() addsimps [read_instantiate [("y","n")] add_left_commute])));
|
|
111 |
qed "mod_mult_self1";
|
|
112 |
|
|
113 |
Goal "(m + n*k) mod n = m mod (n::nat)";
|
|
114 |
by (asm_simp_tac (simpset() addsimps [mult_commute, mod_mult_self1]) 1);
|
|
115 |
qed "mod_mult_self2";
|
|
116 |
|
|
117 |
Addsimps [mod_mult_self1, mod_mult_self2];
|
|
118 |
|
|
119 |
Goal "(m mod n) * (k::nat) = (m*k) mod (n*k)";
|
|
120 |
by (div_undefined_case_tac "n=0" 1);
|
|
121 |
by (div_undefined_case_tac "k=0" 1);
|
|
122 |
by (induct_thm_tac nat_less_induct "m" 1);
|
|
123 |
by (stac mod_if 1);
|
|
124 |
by (Asm_simp_tac 1);
|
|
125 |
by (asm_simp_tac (simpset() addsimps [mod_geq,
|
|
126 |
diff_less, diff_mult_distrib]) 1);
|
|
127 |
qed "mod_mult_distrib";
|
|
128 |
|
|
129 |
Goal "(k::nat) * (m mod n) = (k*m) mod (k*n)";
|
|
130 |
by (asm_simp_tac
|
|
131 |
(simpset() addsimps [read_instantiate [("m","k")] mult_commute,
|
|
132 |
mod_mult_distrib]) 1);
|
|
133 |
qed "mod_mult_distrib2";
|
|
134 |
|
|
135 |
Goal "(m*n) mod n = (0::nat)";
|
|
136 |
by (div_undefined_case_tac "n=0" 1);
|
|
137 |
by (induct_tac "m" 1);
|
|
138 |
by (Asm_simp_tac 1);
|
|
139 |
by (rename_tac "k" 1);
|
|
140 |
by (cut_inst_tac [("m","k*n"),("n","n")] mod_add_self2 1);
|
|
141 |
by (asm_full_simp_tac (simpset() addsimps [add_commute]) 1);
|
|
142 |
qed "mod_mult_self_is_0";
|
|
143 |
|
|
144 |
Goal "(n*m) mod n = (0::nat)";
|
|
145 |
by (simp_tac (simpset() addsimps [mult_commute, mod_mult_self_is_0]) 1);
|
|
146 |
qed "mod_mult_self1_is_0";
|
|
147 |
Addsimps [mod_mult_self_is_0, mod_mult_self1_is_0];
|
|
148 |
|
|
149 |
|
|
150 |
(*** Quotient ***)
|
|
151 |
|
|
152 |
Goal "m<n ==> m div n = (0::nat)";
|
|
153 |
by (rtac (div_eq RS wf_less_trans) 1);
|
|
154 |
by (Asm_simp_tac 1);
|
|
155 |
qed "div_less";
|
|
156 |
Addsimps [div_less];
|
|
157 |
|
|
158 |
Goal "[| 0<n; ~m<n |] ==> m div n = Suc((m-n) div n)";
|
|
159 |
by (rtac (div_eq RS wf_less_trans) 1);
|
|
160 |
by (asm_simp_tac (simpset() addsimps [diff_less, cut_apply, less_eq]) 1);
|
|
161 |
qed "div_geq";
|
|
162 |
|
|
163 |
(*Avoids the ugly ~m<n above*)
|
|
164 |
Goal "[| 0<n; n<=m |] ==> m div n = Suc((m-n) div n)";
|
|
165 |
by (asm_simp_tac (simpset() addsimps [div_geq, not_less_iff_le]) 1);
|
|
166 |
qed "le_div_geq";
|
|
167 |
|
|
168 |
Goal "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))";
|
|
169 |
by (asm_simp_tac (simpset() addsimps [div_geq]) 1);
|
|
170 |
qed "div_if";
|
|
171 |
|
|
172 |
|
|
173 |
(*Main Result about quotient and remainder.*)
|
|
174 |
Goal "(m div n)*n + m mod n = (m::nat)";
|
|
175 |
by (div_undefined_case_tac "n=0" 1);
|
|
176 |
by (induct_thm_tac nat_less_induct "m" 1);
|
|
177 |
by (stac mod_if 1);
|
|
178 |
by (ALLGOALS (asm_simp_tac
|
|
179 |
(simpset() addsimps [add_assoc, div_geq,
|
|
180 |
add_diff_inverse, diff_less])));
|
|
181 |
qed "mod_div_equality";
|
|
182 |
|
|
183 |
(* a simple rearrangement of mod_div_equality: *)
|
|
184 |
Goal "(n::nat) * (m div n) = m - (m mod n)";
|
|
185 |
by (cut_inst_tac [("m","m"),("n","n")] mod_div_equality 1);
|
|
186 |
by (full_simp_tac (simpset() addsimps mult_ac) 1);
|
|
187 |
by (arith_tac 1);
|
|
188 |
qed "mult_div_cancel";
|
|
189 |
|
|
190 |
Goal "0<n ==> m mod n < (n::nat)";
|
|
191 |
by (induct_thm_tac nat_less_induct "m" 1);
|
|
192 |
by (case_tac "na<n" 1);
|
|
193 |
(*case n le na*)
|
|
194 |
by (asm_full_simp_tac (simpset() addsimps [mod_geq, diff_less]) 2);
|
|
195 |
(*case na<n*)
|
|
196 |
by (Asm_simp_tac 1);
|
|
197 |
qed "mod_less_divisor";
|
|
198 |
Addsimps [mod_less_divisor];
|
|
199 |
|
|
200 |
(*** More division laws ***)
|
|
201 |
|
|
202 |
Goal "0<n ==> (m*n) div n = (m::nat)";
|
|
203 |
by (cut_inst_tac [("m", "m*n"),("n","n")] mod_div_equality 1);
|
|
204 |
by Auto_tac;
|
|
205 |
qed "div_mult_self_is_m";
|
|
206 |
|
|
207 |
Goal "0<n ==> (n*m) div n = (m::nat)";
|
|
208 |
by (asm_simp_tac (simpset() addsimps [mult_commute, div_mult_self_is_m]) 1);
|
|
209 |
qed "div_mult_self1_is_m";
|
|
210 |
Addsimps [div_mult_self_is_m, div_mult_self1_is_m];
|
|
211 |
|
|
212 |
(*mod_mult_distrib2 above is the counterpart for remainder*)
|
|
213 |
|
|
214 |
|
|
215 |
(*** Proving facts about div and mod using quorem ***)
|
|
216 |
|
|
217 |
Goal "[| b*q' + r' <= b*q + r; 0 < b; r < b |] \
|
|
218 |
\ ==> q' <= (q::nat)";
|
|
219 |
by (rtac leI 1);
|
|
220 |
by (stac less_iff_Suc_add 1);
|
|
221 |
by (auto_tac (claset(), simpset() addsimps [add_mult_distrib2]));
|
|
222 |
qed "unique_quotient_lemma";
|
|
223 |
|
|
224 |
Goal "[| quorem ((a,b), (q,r)); quorem ((a,b), (q',r')); 0 < b |] \
|
|
225 |
\ ==> q = q'";
|
|
226 |
by (asm_full_simp_tac
|
|
227 |
(simpset() addsimps split_ifs @ [Divides.quorem_def]) 1);
|
|
228 |
by Auto_tac;
|
|
229 |
by (REPEAT
|
|
230 |
(blast_tac (claset() addIs [order_antisym]
|
|
231 |
addDs [order_eq_refl RS unique_quotient_lemma,
|
|
232 |
sym]) 1));
|
|
233 |
qed "unique_quotient";
|
|
234 |
|
|
235 |
Goal "[| quorem ((a,b), (q,r)); quorem ((a,b), (q',r')); 0 < b |] \
|
|
236 |
\ ==> r = r'";
|
|
237 |
by (subgoal_tac "q = q'" 1);
|
|
238 |
by (blast_tac (claset() addIs [unique_quotient]) 2);
|
|
239 |
by (asm_full_simp_tac (simpset() addsimps [Divides.quorem_def]) 1);
|
|
240 |
qed "unique_remainder";
|
|
241 |
|
|
242 |
Goal "0 < b ==> quorem ((a, b), (a div b, a mod b))";
|
|
243 |
by (cut_inst_tac [("m","a"),("n","b")] mod_div_equality 1);
|
|
244 |
by (auto_tac
|
|
245 |
(claset() addEs [sym],
|
|
246 |
simpset() addsimps mult_ac@[Divides.quorem_def]));
|
|
247 |
qed "quorem_div_mod";
|
|
248 |
|
|
249 |
Goal "[| quorem((a,b),(q,r)); 0 < b |] ==> a div b = q";
|
|
250 |
by (asm_simp_tac (simpset() addsimps [quorem_div_mod RS unique_quotient]) 1);
|
|
251 |
qed "quorem_div";
|
|
252 |
|
|
253 |
Goal "[| quorem((a,b),(q,r)); 0 < b |] ==> a mod b = r";
|
|
254 |
by (asm_simp_tac (simpset() addsimps [quorem_div_mod RS unique_remainder]) 1);
|
|
255 |
qed "quorem_mod";
|
|
256 |
|
|
257 |
(** A dividend of zero **)
|
|
258 |
|
|
259 |
Goal "0 div m = (0::nat)";
|
|
260 |
by (div_undefined_case_tac "m=0" 1);
|
|
261 |
by (Asm_simp_tac 1);
|
|
262 |
qed "div_0";
|
|
263 |
|
|
264 |
Goal "0 mod m = (0::nat)";
|
|
265 |
by (div_undefined_case_tac "m=0" 1);
|
|
266 |
by (Asm_simp_tac 1);
|
|
267 |
qed "mod_0";
|
|
268 |
Addsimps [div_0, mod_0];
|
|
269 |
|
|
270 |
(** proving (a*b) div c = a * (b div c) + a * (b mod c) **)
|
|
271 |
|
|
272 |
Goal "[| quorem((b,c),(q,r)); 0 < c |] \
|
|
273 |
\ ==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))";
|
|
274 |
by (cut_inst_tac [("m", "a*r"), ("n","c")] mod_div_equality 1);
|
|
275 |
by (auto_tac
|
|
276 |
(claset(),
|
|
277 |
simpset() addsimps split_ifs@mult_ac@
|
|
278 |
[Divides.quorem_def, add_mult_distrib2]));
|
|
279 |
val lemma = result();
|
|
280 |
|
|
281 |
Goal "(a*b) div c = a*(b div c) + a*(b mod c) div (c::nat)";
|
|
282 |
by (div_undefined_case_tac "c = 0" 1);
|
|
283 |
by (blast_tac (claset() addIs [quorem_div_mod RS lemma RS quorem_div]) 1);
|
|
284 |
qed "div_mult1_eq";
|
|
285 |
|
|
286 |
Goal "(a*b) mod c = a*(b mod c) mod (c::nat)";
|
|
287 |
by (div_undefined_case_tac "c = 0" 1);
|
|
288 |
by (blast_tac (claset() addIs [quorem_div_mod RS lemma RS quorem_mod]) 1);
|
|
289 |
qed "mod_mult1_eq";
|
|
290 |
|
|
291 |
Goal "(a*b) mod (c::nat) = ((a mod c) * b) mod c";
|
|
292 |
by (rtac trans 1);
|
|
293 |
by (res_inst_tac [("s","b*a mod c")] trans 1);
|
|
294 |
by (rtac mod_mult1_eq 2);
|
|
295 |
by (ALLGOALS (simp_tac (simpset() addsimps [mult_commute])));
|
|
296 |
qed "mod_mult1_eq'";
|
|
297 |
|
|
298 |
Goal "(a*b) mod (c::nat) = ((a mod c) * (b mod c)) mod c";
|
|
299 |
by (rtac (mod_mult1_eq' RS trans) 1);
|
|
300 |
by (rtac mod_mult1_eq 1);
|
|
301 |
qed "mod_mult_distrib_mod";
|
|
302 |
|
|
303 |
(** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **)
|
|
304 |
|
|
305 |
Goal "[| quorem((a,c),(aq,ar)); quorem((b,c),(bq,br)); 0 < c |] \
|
|
306 |
\ ==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))";
|
|
307 |
by (cut_inst_tac [("m", "ar+br"), ("n","c")] mod_div_equality 1);
|
|
308 |
by (auto_tac
|
|
309 |
(claset(),
|
|
310 |
simpset() addsimps split_ifs@mult_ac@
|
|
311 |
[Divides.quorem_def, add_mult_distrib2]));
|
|
312 |
val lemma = result();
|
|
313 |
|
|
314 |
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
|
|
315 |
Goal "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)";
|
|
316 |
by (div_undefined_case_tac "c = 0" 1);
|
|
317 |
by (blast_tac (claset() addIs [[quorem_div_mod,quorem_div_mod]
|
|
318 |
MRS lemma RS quorem_div]) 1);
|
|
319 |
qed "div_add1_eq";
|
|
320 |
|
|
321 |
Goal "(a+b) mod (c::nat) = (a mod c + b mod c) mod c";
|
|
322 |
by (div_undefined_case_tac "c = 0" 1);
|
|
323 |
by (blast_tac (claset() addIs [[quorem_div_mod,quorem_div_mod]
|
|
324 |
MRS lemma RS quorem_mod]) 1);
|
|
325 |
qed "mod_add1_eq";
|
|
326 |
|
|
327 |
|
|
328 |
(*** proving a div (b*c) = (a div b) div c ***)
|
|
329 |
|
|
330 |
(** first, a lemma to bound the remainder **)
|
|
331 |
|
|
332 |
Goal "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c";
|
|
333 |
by (cut_inst_tac [("m","q"),("n","c")] mod_less_divisor 1);
|
|
334 |
by (dres_inst_tac [("m","q mod c")] less_imp_Suc_add 2);
|
|
335 |
by Auto_tac;
|
|
336 |
by (eres_inst_tac [("P","%x. ?lhs < ?rhs x")] ssubst 1);
|
|
337 |
by (asm_simp_tac (simpset() addsimps [add_mult_distrib2]) 1);
|
|
338 |
val mod_lemma = result();
|
|
339 |
|
|
340 |
Goal "[| quorem ((a,b), (q,r)); 0 < b; 0 < c |] \
|
|
341 |
\ ==> quorem ((a, b*c), (q div c, b*(q mod c) + r))";
|
|
342 |
by (cut_inst_tac [("m", "q"), ("n","c")] mod_div_equality 1);
|
|
343 |
by (auto_tac
|
|
344 |
(claset(),
|
|
345 |
simpset() addsimps mult_ac@
|
|
346 |
[Divides.quorem_def, add_mult_distrib2 RS sym,
|
|
347 |
mod_lemma]));
|
|
348 |
val lemma = result();
|
|
349 |
|
|
350 |
Goal "a div (b*c) = (a div b) div (c::nat)";
|
|
351 |
by (div_undefined_case_tac "b=0" 1);
|
|
352 |
by (div_undefined_case_tac "c=0" 1);
|
|
353 |
by (force_tac (claset(),
|
|
354 |
simpset() addsimps [quorem_div_mod RS lemma RS quorem_div]) 1);
|
|
355 |
qed "div_mult2_eq";
|
|
356 |
|
|
357 |
Goal "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)";
|
|
358 |
by (div_undefined_case_tac "b=0" 1);
|
|
359 |
by (div_undefined_case_tac "c=0" 1);
|
|
360 |
by (cut_inst_tac [("m", "a"), ("n","b")] mod_div_equality 1);
|
|
361 |
by (auto_tac (claset(),
|
|
362 |
simpset() addsimps [mult_commute,
|
|
363 |
quorem_div_mod RS lemma RS quorem_mod]));
|
|
364 |
qed "mod_mult2_eq";
|
|
365 |
|
|
366 |
|
|
367 |
(*** Cancellation of common factors in "div" ***)
|
|
368 |
|
|
369 |
Goal "[| (0::nat) < b; 0 < c |] ==> (c*a) div (c*b) = a div b";
|
|
370 |
by (stac div_mult2_eq 1);
|
|
371 |
by Auto_tac;
|
|
372 |
val lemma1 = result();
|
|
373 |
|
|
374 |
Goal "(0::nat) < c ==> (c*a) div (c*b) = a div b";
|
|
375 |
by (div_undefined_case_tac "b = 0" 1);
|
|
376 |
by (auto_tac
|
|
377 |
(claset(),
|
|
378 |
simpset() addsimps [read_instantiate [("x", "b")] linorder_neq_iff,
|
|
379 |
lemma1, lemma2]));
|
|
380 |
qed "div_mult_mult1";
|
|
381 |
|
|
382 |
Goal "(0::nat) < c ==> (a*c) div (b*c) = a div b";
|
|
383 |
by (dtac div_mult_mult1 1);
|
|
384 |
by (auto_tac (claset(), simpset() addsimps [mult_commute]));
|
|
385 |
qed "div_mult_mult2";
|
|
386 |
|
|
387 |
Addsimps [div_mult_mult1, div_mult_mult2];
|
|
388 |
|
|
389 |
|
|
390 |
(*** Distribution of factors over "mod"
|
|
391 |
|
|
392 |
Could prove these as in Integ/IntDiv.ML, but we already have
|
|
393 |
mod_mult_distrib and mod_mult_distrib2 above!
|
|
394 |
|
|
395 |
Goal "(c*a) mod (c*b) = (c::nat) * (a mod b)";
|
|
396 |
qed "mod_mult_mult1";
|
|
397 |
|
|
398 |
Goal "(a*c) mod (b*c) = (a mod b) * (c::nat)";
|
|
399 |
qed "mod_mult_mult2";
|
|
400 |
***)
|
|
401 |
|
|
402 |
(*** Further facts about div and mod ***)
|
|
403 |
|
|
404 |
Goal "m div Suc 0 = m";
|
|
405 |
by (induct_tac "m" 1);
|
|
406 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [div_geq])));
|
|
407 |
qed "div_1";
|
|
408 |
Addsimps [div_1];
|
|
409 |
|
|
410 |
Goal "0<n ==> n div n = (1::nat)";
|
|
411 |
by (asm_simp_tac (simpset() addsimps [div_geq]) 1);
|
|
412 |
qed "div_self";
|
|
413 |
Addsimps [div_self];
|
|
414 |
|
|
415 |
Goal "0<n ==> (m+n) div n = Suc (m div n)";
|
|
416 |
by (subgoal_tac "(n + m) div n = Suc ((n+m-n) div n)" 1);
|
|
417 |
by (stac (div_geq RS sym) 2);
|
|
418 |
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [add_commute])));
|
|
419 |
qed "div_add_self2";
|
|
420 |
|
|
421 |
Goal "0<n ==> (n+m) div n = Suc (m div n)";
|
|
422 |
by (asm_simp_tac (simpset() addsimps [add_commute, div_add_self2]) 1);
|
|
423 |
qed "div_add_self1";
|
|
424 |
|
|
425 |
Goal "!!n::nat. 0<n ==> (m + k*n) div n = k + m div n";
|
|
426 |
by (stac div_add1_eq 1);
|
|
427 |
by (stac div_mult1_eq 1);
|
|
428 |
by (Asm_simp_tac 1);
|
|
429 |
qed "div_mult_self1";
|
|
430 |
|
|
431 |
Goal "0<n ==> (m + n*k) div n = k + m div (n::nat)";
|
|
432 |
by (asm_simp_tac (simpset() addsimps [mult_commute, div_mult_self1]) 1);
|
|
433 |
qed "div_mult_self2";
|
|
434 |
|
|
435 |
Addsimps [div_mult_self1, div_mult_self2];
|
|
436 |
|
|
437 |
(* Monotonicity of div in first argument *)
|
|
438 |
Goal "ALL m::nat. m <= n --> (m div k) <= (n div k)";
|
|
439 |
by (div_undefined_case_tac "k=0" 1);
|
|
440 |
by (induct_thm_tac nat_less_induct "n" 1);
|
|
441 |
by (Clarify_tac 1);
|
|
442 |
by (case_tac "n<k" 1);
|
|
443 |
(* 1 case n<k *)
|
|
444 |
by (Asm_simp_tac 1);
|
|
445 |
(* 2 case n >= k *)
|
|
446 |
by (case_tac "m<k" 1);
|
|
447 |
(* 2.1 case m<k *)
|
|
448 |
by (Asm_simp_tac 1);
|
|
449 |
(* 2.2 case m>=k *)
|
|
450 |
by (asm_simp_tac (simpset() addsimps [div_geq, diff_less, diff_le_mono]) 1);
|
|
451 |
qed_spec_mp "div_le_mono";
|
|
452 |
|
|
453 |
(* Antimonotonicity of div in second argument *)
|
|
454 |
Goal "!!m::nat. [| 0<m; m<=n |] ==> (k div n) <= (k div m)";
|
|
455 |
by (subgoal_tac "0<n" 1);
|
|
456 |
by (Asm_simp_tac 2);
|
|
457 |
by (induct_thm_tac nat_less_induct "k" 1);
|
|
458 |
by (rename_tac "k" 1);
|
|
459 |
by (case_tac "k<n" 1);
|
|
460 |
by (Asm_simp_tac 1);
|
|
461 |
by (subgoal_tac "~(k<m)" 1);
|
|
462 |
by (Asm_simp_tac 2);
|
|
463 |
by (asm_simp_tac (simpset() addsimps [div_geq]) 1);
|
|
464 |
by (subgoal_tac "(k-n) div n <= (k-m) div n" 1);
|
|
465 |
by (REPEAT (ares_tac [div_le_mono,diff_le_mono2] 2));
|
|
466 |
by (rtac le_trans 1);
|
|
467 |
by (Asm_simp_tac 1);
|
|
468 |
by (asm_simp_tac (simpset() addsimps [diff_less]) 1);
|
|
469 |
qed "div_le_mono2";
|
|
470 |
|
|
471 |
Goal "m div n <= (m::nat)";
|
|
472 |
by (div_undefined_case_tac "n=0" 1);
|
|
473 |
by (subgoal_tac "m div n <= m div 1" 1);
|
|
474 |
by (Asm_full_simp_tac 1);
|
|
475 |
by (rtac div_le_mono2 1);
|
|
476 |
by (ALLGOALS Asm_simp_tac);
|
|
477 |
qed "div_le_dividend";
|
|
478 |
Addsimps [div_le_dividend];
|
|
479 |
|
|
480 |
(* Similar for "less than" *)
|
|
481 |
Goal "!!n::nat. 1<n ==> (0 < m) --> (m div n < m)";
|
|
482 |
by (induct_thm_tac nat_less_induct "m" 1);
|
|
483 |
by (rename_tac "m" 1);
|
|
484 |
by (case_tac "m<n" 1);
|
|
485 |
by (Asm_full_simp_tac 1);
|
|
486 |
by (subgoal_tac "0<n" 1);
|
|
487 |
by (Asm_simp_tac 2);
|
|
488 |
by (asm_full_simp_tac (simpset() addsimps [div_geq]) 1);
|
|
489 |
by (case_tac "n<m" 1);
|
|
490 |
by (subgoal_tac "(m-n) div n < (m-n)" 1);
|
|
491 |
by (REPEAT (ares_tac [impI,less_trans_Suc] 1));
|
|
492 |
by (asm_full_simp_tac (simpset() addsimps [diff_less]) 1);
|
|
493 |
by (asm_full_simp_tac (simpset() addsimps [diff_less]) 1);
|
|
494 |
(* case n=m *)
|
|
495 |
by (subgoal_tac "m=n" 1);
|
|
496 |
by (Asm_simp_tac 2);
|
|
497 |
by (Asm_simp_tac 1);
|
|
498 |
qed_spec_mp "div_less_dividend";
|
|
499 |
Addsimps [div_less_dividend];
|
|
500 |
|
|
501 |
(*** Further facts about mod (mainly for the mutilated chess board ***)
|
|
502 |
|
|
503 |
Goal "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))";
|
|
504 |
by (div_undefined_case_tac "n=0" 1);
|
|
505 |
by (induct_thm_tac nat_less_induct "m" 1);
|
|
506 |
by (case_tac "Suc(na)<n" 1);
|
|
507 |
(* case Suc(na) < n *)
|
|
508 |
by (forward_tac [lessI RS less_trans] 1
|
|
509 |
THEN asm_simp_tac (simpset() addsimps [less_not_refl3]) 1);
|
|
510 |
(* case n <= Suc(na) *)
|
|
511 |
by (asm_full_simp_tac (simpset() addsimps [not_less_iff_le, le_Suc_eq,
|
|
512 |
mod_geq]) 1);
|
|
513 |
by (auto_tac (claset(),
|
|
514 |
simpset() addsimps [Suc_diff_le, diff_less, le_mod_geq]));
|
|
515 |
qed "mod_Suc";
|
|
516 |
|
|
517 |
|
|
518 |
(************************************************)
|
|
519 |
(** Divides Relation **)
|
|
520 |
(************************************************)
|
|
521 |
|
|
522 |
Goalw [dvd_def] "n = m * k ==> m dvd n";
|
|
523 |
by (Blast_tac 1);
|
|
524 |
qed "dvdI";
|
|
525 |
|
|
526 |
Goalw [dvd_def] "!!P. [|m dvd n; !!k. n = m*k ==> P|] ==> P";
|
|
527 |
by (Blast_tac 1);
|
|
528 |
qed "dvdE";
|
|
529 |
|
|
530 |
Goalw [dvd_def] "m dvd (0::nat)";
|
|
531 |
by (blast_tac (claset() addIs [mult_0_right RS sym]) 1);
|
|
532 |
qed "dvd_0_right";
|
|
533 |
AddIffs [dvd_0_right];
|
|
534 |
|
|
535 |
Goalw [dvd_def] "0 dvd m ==> m = (0::nat)";
|
|
536 |
by Auto_tac;
|
|
537 |
qed "dvd_0_left";
|
|
538 |
|
|
539 |
Goal "(0 dvd (m::nat)) = (m = 0)";
|
|
540 |
by (blast_tac (claset() addIs [dvd_0_left]) 1);
|
|
541 |
qed "dvd_0_left_iff";
|
|
542 |
AddIffs [dvd_0_left_iff];
|
|
543 |
|
|
544 |
Goalw [dvd_def] "Suc 0 dvd k";
|
|
545 |
by (Simp_tac 1);
|
|
546 |
qed "dvd_1_left";
|
|
547 |
AddIffs [dvd_1_left];
|
|
548 |
|
|
549 |
Goal "(m dvd Suc 0) = (m = Suc 0)";
|
|
550 |
by (simp_tac (simpset() addsimps [dvd_def]) 1);
|
|
551 |
qed "dvd_1_iff_1";
|
|
552 |
Addsimps [dvd_1_iff_1];
|
|
553 |
|
|
554 |
Goalw [dvd_def] "m dvd (m::nat)";
|
|
555 |
by (blast_tac (claset() addIs [mult_1_right RS sym]) 1);
|
|
556 |
qed "dvd_refl";
|
|
557 |
Addsimps [dvd_refl];
|
|
558 |
|
|
559 |
Goalw [dvd_def] "[| m dvd n; n dvd p |] ==> m dvd (p::nat)";
|
|
560 |
by (blast_tac (claset() addIs [mult_assoc] ) 1);
|
|
561 |
qed "dvd_trans";
|
|
562 |
|
|
563 |
Goalw [dvd_def] "[| m dvd n; n dvd m |] ==> m = (n::nat)";
|
|
564 |
by (force_tac (claset() addDs [mult_eq_self_implies_10],
|
|
565 |
simpset() addsimps [mult_assoc, mult_eq_1_iff]) 1);
|
|
566 |
qed "dvd_anti_sym";
|
|
567 |
|
|
568 |
Goalw [dvd_def] "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)";
|
|
569 |
by (blast_tac (claset() addIs [add_mult_distrib2 RS sym]) 1);
|
|
570 |
qed "dvd_add";
|
|
571 |
|
|
572 |
Goalw [dvd_def] "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)";
|
|
573 |
by (blast_tac (claset() addIs [diff_mult_distrib2 RS sym]) 1);
|
|
574 |
qed "dvd_diff";
|
|
575 |
|
|
576 |
Goal "[| k dvd m-n; k dvd n; n<=m |] ==> k dvd (m::nat)";
|
|
577 |
by (etac (not_less_iff_le RS iffD2 RS add_diff_inverse RS subst) 1);
|
|
578 |
by (blast_tac (claset() addIs [dvd_add]) 1);
|
|
579 |
qed "dvd_diffD";
|
|
580 |
|
|
581 |
Goal "[| k dvd m-n; k dvd m; n<=m |] ==> k dvd (n::nat)";
|
|
582 |
by (dres_inst_tac [("m","m")] dvd_diff 1);
|
|
583 |
by Auto_tac;
|
|
584 |
qed "dvd_diffD1";
|
|
585 |
|
|
586 |
Goalw [dvd_def] "k dvd n ==> k dvd (m*n :: nat)";
|
|
587 |
by (blast_tac (claset() addIs [mult_left_commute]) 1);
|
|
588 |
qed "dvd_mult";
|
|
589 |
|
|
590 |
Goal "k dvd m ==> k dvd (m*n :: nat)";
|
|
591 |
by (stac mult_commute 1);
|
|
592 |
by (etac dvd_mult 1);
|
|
593 |
qed "dvd_mult2";
|
|
594 |
|
|
595 |
(* k dvd (m*k) *)
|
|
596 |
AddIffs [dvd_refl RS dvd_mult, dvd_refl RS dvd_mult2];
|
|
597 |
|
|
598 |
Goal "(k dvd n + k) = (k dvd (n::nat))";
|
|
599 |
by (rtac iffI 1);
|
|
600 |
by (etac dvd_add 2);
|
|
601 |
by (rtac dvd_refl 2);
|
|
602 |
by (subgoal_tac "n = (n+k)-k" 1);
|
|
603 |
by (Simp_tac 2);
|
|
604 |
by (etac ssubst 1);
|
|
605 |
by (etac dvd_diff 1);
|
|
606 |
by (rtac dvd_refl 1);
|
|
607 |
qed "dvd_reduce";
|
|
608 |
|
|
609 |
Goalw [dvd_def] "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n";
|
|
610 |
by (div_undefined_case_tac "n=0" 1);
|
|
611 |
by Auto_tac;
|
|
612 |
by (blast_tac (claset() addIs [mod_mult_distrib2 RS sym]) 1);
|
|
613 |
qed "dvd_mod";
|
|
614 |
|
|
615 |
Goal "[| (k::nat) dvd m mod n; k dvd n |] ==> k dvd m";
|
|
616 |
by (subgoal_tac "k dvd (m div n)*n + m mod n" 1);
|
|
617 |
by (asm_simp_tac (simpset() addsimps [dvd_add, dvd_mult]) 2);
|
|
618 |
by (asm_full_simp_tac (simpset() addsimps [mod_div_equality]) 1);
|
|
619 |
qed "dvd_mod_imp_dvd";
|
|
620 |
|
|
621 |
Goal "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)";
|
|
622 |
by (blast_tac (claset() addIs [dvd_mod_imp_dvd, dvd_mod]) 1);
|
|
623 |
qed "dvd_mod_iff";
|
|
624 |
|
|
625 |
Goalw [dvd_def] "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n";
|
|
626 |
by (etac exE 1);
|
|
627 |
by (asm_full_simp_tac (simpset() addsimps mult_ac) 1);
|
|
628 |
qed "dvd_mult_cancel";
|
|
629 |
|
|
630 |
Goal "0<m ==> (m*n dvd m) = (n = (1::nat))";
|
|
631 |
by Auto_tac;
|
|
632 |
by (subgoal_tac "m*n dvd m*1" 1);
|
|
633 |
by (dtac dvd_mult_cancel 1);
|
|
634 |
by Auto_tac;
|
|
635 |
qed "dvd_mult_cancel1";
|
|
636 |
|
|
637 |
Goal "0<m ==> (n*m dvd m) = (n = (1::nat))";
|
|
638 |
by (stac mult_commute 1);
|
|
639 |
by (etac dvd_mult_cancel1 1);
|
|
640 |
qed "dvd_mult_cancel2";
|
|
641 |
|
|
642 |
Goalw [dvd_def] "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)";
|
|
643 |
by (Clarify_tac 1);
|
|
644 |
by (res_inst_tac [("x","k*ka")] exI 1);
|
|
645 |
by (asm_simp_tac (simpset() addsimps mult_ac) 1);
|
|
646 |
qed "mult_dvd_mono";
|
|
647 |
|
|
648 |
Goalw [dvd_def] "(i*j :: nat) dvd k ==> i dvd k";
|
|
649 |
by (full_simp_tac (simpset() addsimps [mult_assoc]) 1);
|
|
650 |
by (Blast_tac 1);
|
|
651 |
qed "dvd_mult_left";
|
|
652 |
|
|
653 |
Goalw [dvd_def] "(i*j :: nat) dvd k ==> j dvd k";
|
|
654 |
by (Clarify_tac 1);
|
|
655 |
by (res_inst_tac [("x","i*k")] exI 1);
|
|
656 |
by (simp_tac (simpset() addsimps mult_ac) 1);
|
|
657 |
qed "dvd_mult_right";
|
|
658 |
|
|
659 |
Goalw [dvd_def] "[| k dvd n; 0 < n |] ==> k <= (n::nat)";
|
|
660 |
by (Clarify_tac 1);
|
|
661 |
by (ALLGOALS (full_simp_tac (simpset() addsimps [zero_less_mult_iff])));
|
|
662 |
by (etac conjE 1);
|
|
663 |
by (rtac le_trans 1);
|
|
664 |
by (rtac (le_refl RS mult_le_mono) 2);
|
|
665 |
by (etac Suc_leI 2);
|
|
666 |
by (Simp_tac 1);
|
|
667 |
qed "dvd_imp_le";
|
|
668 |
|
|
669 |
Goalw [dvd_def] "!!k::nat. (k dvd n) = (n mod k = 0)";
|
|
670 |
by (div_undefined_case_tac "k=0" 1);
|
|
671 |
by Safe_tac;
|
|
672 |
by (asm_simp_tac (simpset() addsimps [mult_commute]) 1);
|
|
673 |
by (res_inst_tac [("t","n"),("n1","k")] (mod_div_equality RS subst) 1);
|
|
674 |
by (stac mult_commute 1);
|
|
675 |
by (Asm_simp_tac 1);
|
|
676 |
qed "dvd_eq_mod_eq_0";
|
|
677 |
|
|
678 |
Goal "n dvd m ==> n * (m div n) = (m::nat)";
|
|
679 |
by (subgoal_tac "m mod n = 0" 1);
|
|
680 |
by (asm_full_simp_tac (simpset() addsimps [mult_div_cancel]) 1);
|
|
681 |
by (asm_full_simp_tac (HOL_basic_ss addsimps [dvd_eq_mod_eq_0]) 1);
|
|
682 |
qed "dvd_mult_div_cancel";
|
|
683 |
|
|
684 |
Goal "(m mod d = 0) = (EX q::nat. m = d*q)";
|
|
685 |
by (auto_tac (claset(),
|
|
686 |
simpset() addsimps [dvd_eq_mod_eq_0 RS sym, dvd_def]));
|
|
687 |
qed "mod_eq_0_iff";
|
|
688 |
AddSDs [mod_eq_0_iff RS iffD1];
|
|
689 |
|
|
690 |
(*Loses information, namely we also have r<d provided d is nonzero*)
|
|
691 |
Goal "(m mod d = r) ==> EX q::nat. m = r + q*d";
|
|
692 |
by (cut_inst_tac [("m","m")] mod_div_equality 1);
|
|
693 |
by (full_simp_tac (simpset() addsimps add_ac) 1);
|
|
694 |
by (blast_tac (claset() addIs [sym]) 1);
|
|
695 |
qed "mod_eqD";
|
|
696 |
|