| author | wenzelm |
| Wed, 23 Jan 2008 22:57:09 +0100 | |
| changeset 25947 | 1f2f4d941e9e |
| parent 25942 | a52309ac4a4d |
| child 26062 | 16f334d7156a |
| permissions | -rw-r--r-- |
| 3366 | 1 |
(* Title: HOL/Divides.thy |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
|
6865
5577ffe4c2f1
now div and mod are overloaded; dvd is polymorphic
paulson
parents:
3366
diff
changeset
|
4 |
Copyright 1999 University of Cambridge |
| 18154 | 5 |
*) |
| 3366 | 6 |
|
| 18154 | 7 |
header {* The division operators div, mod and the divides relation "dvd" *}
|
| 3366 | 8 |
|
| 15131 | 9 |
theory Divides |
| 24268 | 10 |
imports Power |
| 22993 | 11 |
uses "~~/src/Provers/Arith/cancel_div_mod.ML" |
| 15131 | 12 |
begin |
| 3366 | 13 |
|
| 25942 | 14 |
subsection {* Syntactic division operations *}
|
15 |
||
| 24993 | 16 |
class div = times + |
| 25062 | 17 |
fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70) |
18 |
fixes mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70) |
|
| 25942 | 19 |
begin |
| 21408 | 20 |
|
| 25942 | 21 |
definition |
22 |
dvd :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "dvd" 50) |
|
23 |
where |
|
24 |
[code func del]: "m dvd n \<longleftrightarrow> (\<exists>k. n = m * k)" |
|
25 |
||
26 |
end |
|
27 |
||
28 |
subsection {* Abstract divisibility in commutative semirings. *}
|
|
29 |
||
30 |
class semiring_div = comm_semiring_1_cancel + div + |
|
31 |
assumes mod_div_equality: "a div b * b + a mod b = a" |
|
32 |
and div_by_0: "a div 0 = 0" |
|
33 |
and mult_div: "b \<noteq> 0 \<Longrightarrow> a * b div b = a" |
|
34 |
begin |
|
35 |
||
36 |
lemma div_by_1: "a div 1 = a" |
|
37 |
using mult_div [of one a] zero_neq_one by simp |
|
38 |
||
39 |
lemma mod_by_1: "a mod 1 = 0" |
|
40 |
proof - |
|
41 |
from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp |
|
42 |
then have "a + a mod 1 = a + 0" by simp |
|
43 |
then show ?thesis by (rule add_left_imp_eq) |
|
44 |
qed |
|
45 |
||
46 |
lemma mod_by_0: "a mod 0 = a" |
|
47 |
using mod_div_equality [of a zero] by simp |
|
48 |
||
49 |
lemma mult_mod: "a * b mod b = 0" |
|
50 |
proof (cases "b = 0") |
|
51 |
case True then show ?thesis by (simp add: mod_by_0) |
|
52 |
next |
|
53 |
case False with mult_div have abb: "a * b div b = a" . |
|
54 |
from mod_div_equality have "a * b div b * b + a * b mod b = a * b" . |
|
55 |
with abb have "a * b + a * b mod b = a * b + 0" by simp |
|
56 |
then show ?thesis by (rule add_left_imp_eq) |
|
57 |
qed |
|
58 |
||
59 |
lemma mod_self: "a mod a = 0" |
|
60 |
using mult_mod [of one] by simp |
|
61 |
||
62 |
lemma div_self: "a \<noteq> 0 \<Longrightarrow> a div a = 1" |
|
63 |
using mult_div [of _ one] by simp |
|
64 |
||
65 |
lemma div_0: "0 div a = 0" |
|
66 |
proof (cases "a = 0") |
|
67 |
case True then show ?thesis by (simp add: div_by_0) |
|
68 |
next |
|
69 |
case False with mult_div have "0 * a div a = 0" . |
|
70 |
then show ?thesis by simp |
|
71 |
qed |
|
72 |
||
73 |
lemma mod_0: "0 mod a = 0" |
|
74 |
using mod_div_equality [of zero a] div_0 by simp |
|
75 |
||
76 |
lemma dvd_def_mod [code func]: "a dvd b \<longleftrightarrow> b mod a = 0" |
|
77 |
proof |
|
78 |
assume "b mod a = 0" |
|
79 |
with mod_div_equality [of b a] have "b div a * a = b" by simp |
|
80 |
then have "b = a * (b div a)" unfolding mult_commute .. |
|
81 |
then have "\<exists>c. b = a * c" .. |
|
82 |
then show "a dvd b" unfolding dvd_def . |
|
83 |
next |
|
84 |
assume "a dvd b" |
|
85 |
then have "\<exists>c. b = a * c" unfolding dvd_def . |
|
86 |
then obtain c where "b = a * c" .. |
|
87 |
then have "b mod a = a * c mod a" by simp |
|
88 |
then have "b mod a = c * a mod a" by (simp add: mult_commute) |
|
89 |
then show "b mod a = 0" by (simp add: mult_mod) |
|
90 |
qed |
|
91 |
||
92 |
lemma dvd_refl: "a dvd a" |
|
93 |
unfolding dvd_def_mod mod_self .. |
|
94 |
||
95 |
lemma dvd_trans: |
|
96 |
assumes "a dvd b" and "b dvd c" |
|
97 |
shows "a dvd c" |
|
98 |
proof - |
|
99 |
from assms obtain v where "b = a * v" unfolding dvd_def by auto |
|
100 |
moreover from assms obtain w where "c = b * w" unfolding dvd_def by auto |
|
101 |
ultimately have "c = a * (v * w)" by (simp add: mult_assoc) |
|
102 |
then show ?thesis unfolding dvd_def .. |
|
103 |
qed |
|
104 |
||
105 |
lemma one_dvd: "1 dvd a" |
|
106 |
unfolding dvd_def by simp |
|
107 |
||
108 |
lemma dvd_0: "a dvd 0" |
|
109 |
unfolding dvd_def proof |
|
110 |
show "0 = a * 0" by simp |
|
111 |
qed |
|
112 |
||
113 |
end |
|
114 |
||
115 |
||
116 |
subsection {* Division on the natural numbers *}
|
|
117 |
||
118 |
instantiation nat :: semiring_div |
|
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25162
diff
changeset
|
119 |
begin |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25162
diff
changeset
|
120 |
|
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25162
diff
changeset
|
121 |
definition |
| 22993 | 122 |
div_def: "m div n == wfrec (pred_nat^+) |
123 |
(%f j. if j<n | n=0 then 0 else Suc (f (j-n))) m" |
|
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25162
diff
changeset
|
124 |
|
| 25942 | 125 |
lemma div_eq: "(%m. m div n) = wfrec (pred_nat^+) |
126 |
(%f j. if j<n | n=0 then 0 else Suc (f (j-n)))" |
|
127 |
by (simp add: div_def) |
|
128 |
||
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25162
diff
changeset
|
129 |
definition |
|
22261
9e185f78e7d4
Adapted to changes in Transitive_Closure theory.
berghofe
parents:
21911
diff
changeset
|
130 |
mod_def: "m mod n == wfrec (pred_nat^+) |
|
25571
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25162
diff
changeset
|
131 |
(%f j. if j<n | n=0 then j else f (j-n)) m" |
|
c9e39eafc7a0
instantiation target rather than legacy instance
haftmann
parents:
25162
diff
changeset
|
132 |
|
| 22718 | 133 |
lemma mod_eq: "(%m. m mod n) = |
|
22261
9e185f78e7d4
Adapted to changes in Transitive_Closure theory.
berghofe
parents:
21911
diff
changeset
|
134 |
wfrec (pred_nat^+) (%f j. if j<n | n=0 then j else f (j-n))" |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
135 |
by (simp add: mod_def) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
136 |
|
| 25942 | 137 |
lemmas wf_less_trans = def_wfrec [THEN trans, |
138 |
OF eq_reflection wf_pred_nat [THEN wf_trancl], standard] |
|
139 |
||
140 |
lemma div_less [simp]: "m < n \<Longrightarrow> m div n = (0\<Colon>nat)" |
|
141 |
by (rule div_eq [THEN wf_less_trans]) simp |
|
142 |
||
143 |
lemma le_div_geq: "0 < n \<Longrightarrow> n \<le> m \<Longrightarrow> m div n = Suc ((m - n) div n)" |
|
144 |
by (rule div_eq [THEN wf_less_trans]) (simp add: cut_apply less_eq) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
145 |
|
| 25942 | 146 |
lemma DIVISION_BY_ZERO_MOD [simp]: "a mod 0 = (a\<Colon>nat)" |
147 |
by (rule mod_eq [THEN wf_less_trans]) simp |
|
148 |
||
149 |
lemma mod_less [simp]: "m < n \<Longrightarrow> m mod n = (m\<Colon>nat)" |
|
150 |
by (rule mod_eq [THEN wf_less_trans]) simp |
|
151 |
||
152 |
lemma le_mod_geq: "(n\<Colon>nat) \<le> m \<Longrightarrow> m mod n = (m - n) mod n" |
|
153 |
by (cases "n = 0", simp, rule mod_eq [THEN wf_less_trans]) |
|
154 |
(simp add: cut_apply less_eq) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
155 |
|
| 25942 | 156 |
lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)" |
157 |
by (simp add: le_mod_geq) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
158 |
|
| 25942 | 159 |
instance proof |
160 |
fix n m :: nat |
|
161 |
show "(m div n) * n + m mod n = m" |
|
162 |
apply (cases "n = 0", simp) |
|
| 25947 | 163 |
apply (induct m rule: less_induct) |
| 25942 | 164 |
apply (subst mod_if) |
165 |
apply (simp add: add_assoc add_diff_inverse le_div_geq) |
|
166 |
done |
|
167 |
next |
|
168 |
fix n :: nat |
|
169 |
show "n div 0 = 0" |
|
| 25947 | 170 |
by (rule div_eq [THEN wf_less_trans]) simp |
| 25942 | 171 |
next |
172 |
fix n m :: nat |
|
173 |
assume "n \<noteq> 0" |
|
174 |
then show "m * n div n = m" |
|
175 |
by (induct m) (simp_all add: le_div_geq) |
|
176 |
qed |
|
177 |
||
178 |
end |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
179 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
180 |
|
| 25942 | 181 |
subsubsection{*Simproc for Cancelling Div and Mod*}
|
182 |
||
183 |
lemmas mod_div_equality = semiring_div_class.times_div_mod_plus_zero_one.mod_div_equality [of "m\<Colon>nat" n, standard] |
|
184 |
||
185 |
lemma mod_div_equality2: "n * (m div n) + m mod n = (m::nat)" |
|
186 |
unfolding mult_commute [of n] |
|
187 |
by (rule mod_div_equality) |
|
188 |
||
189 |
lemma div_mod_equality: "((m div n)*n + m mod n) + k = (m::nat) + k" |
|
190 |
by (simp add: mod_div_equality) |
|
191 |
||
192 |
lemma div_mod_equality2: "(n*(m div n) + m mod n) + k = (m::nat) + k" |
|
193 |
by (simp add: mod_div_equality2) |
|
194 |
||
195 |
ML {*
|
|
196 |
structure CancelDivModData = |
|
197 |
struct |
|
198 |
||
199 |
val div_name = @{const_name Divides.div};
|
|
200 |
val mod_name = @{const_name Divides.mod};
|
|
201 |
val mk_binop = HOLogic.mk_binop; |
|
202 |
val mk_sum = NatArithUtils.mk_sum; |
|
203 |
val dest_sum = NatArithUtils.dest_sum; |
|
204 |
||
205 |
(*logic*) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
206 |
|
| 25942 | 207 |
val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}]
|
208 |
||
209 |
val trans = trans |
|
210 |
||
211 |
val prove_eq_sums = |
|
212 |
let val simps = @{thm add_0} :: @{thm add_0_right} :: @{thms add_ac}
|
|
213 |
in NatArithUtils.prove_conv all_tac (NatArithUtils.simp_all_tac simps) end; |
|
214 |
||
215 |
end; |
|
216 |
||
217 |
structure CancelDivMod = CancelDivModFun(CancelDivModData); |
|
218 |
||
219 |
val cancel_div_mod_proc = NatArithUtils.prep_simproc |
|
220 |
("cancel_div_mod", ["(m::nat) + n"], K CancelDivMod.proc);
|
|
221 |
||
222 |
Addsimprocs[cancel_div_mod_proc]; |
|
223 |
*} |
|
224 |
||
225 |
||
226 |
subsubsection {* Remainder *}
|
|
227 |
||
228 |
lemmas DIVISION_BY_ZERO_MOD [simp] = mod_by_0 [of "a\<Colon>nat", standard] |
|
229 |
||
230 |
lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)" |
|
231 |
by (induct m) (simp_all add: le_div_geq) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
232 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
233 |
lemma mod_geq: "~ m < (n::nat) ==> m mod n = (m-n) mod n" |
| 25942 | 234 |
by (simp add: le_mod_geq linorder_not_less) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
235 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
236 |
lemma mod_1 [simp]: "m mod Suc 0 = 0" |
| 22718 | 237 |
by (induct m) (simp_all add: mod_geq) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
238 |
|
| 25942 | 239 |
lemmas mod_self [simp] = semiring_div_class.mod_self [of "n\<Colon>nat", standard] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
240 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
241 |
lemma mod_add_self2 [simp]: "(m+n) mod n = m mod (n::nat)" |
| 22718 | 242 |
apply (subgoal_tac "(n + m) mod n = (n+m-n) mod n") |
243 |
apply (simp add: add_commute) |
|
| 25942 | 244 |
apply (subst le_mod_geq [symmetric], simp_all) |
| 22718 | 245 |
done |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
246 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
247 |
lemma mod_add_self1 [simp]: "(n+m) mod n = m mod (n::nat)" |
| 22718 | 248 |
by (simp add: add_commute mod_add_self2) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
249 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
250 |
lemma mod_mult_self1 [simp]: "(m + k*n) mod n = m mod (n::nat)" |
| 22718 | 251 |
by (induct k) (simp_all add: add_left_commute [of _ n]) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
252 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
253 |
lemma mod_mult_self2 [simp]: "(m + n*k) mod n = m mod (n::nat)" |
| 22718 | 254 |
by (simp add: mult_commute mod_mult_self1) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
255 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
256 |
lemma mod_mult_distrib: "(m mod n) * (k::nat) = (m*k) mod (n*k)" |
| 22718 | 257 |
apply (cases "n = 0", simp) |
258 |
apply (cases "k = 0", simp) |
|
259 |
apply (induct m rule: nat_less_induct) |
|
260 |
apply (subst mod_if, simp) |
|
261 |
apply (simp add: mod_geq diff_mult_distrib) |
|
262 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
263 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
264 |
lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)" |
| 22718 | 265 |
by (simp add: mult_commute [of k] mod_mult_distrib) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
266 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
267 |
lemma mod_mult_self_is_0 [simp]: "(m*n) mod n = (0::nat)" |
| 22718 | 268 |
apply (cases "n = 0", simp) |
269 |
apply (induct m, simp) |
|
270 |
apply (rename_tac k) |
|
271 |
apply (cut_tac m = "k * n" and n = n in mod_add_self2) |
|
272 |
apply (simp add: add_commute) |
|
273 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
274 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
275 |
lemma mod_mult_self1_is_0 [simp]: "(n*m) mod n = (0::nat)" |
| 22718 | 276 |
by (simp add: mult_commute mod_mult_self_is_0) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
277 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
278 |
|
| 25942 | 279 |
subsubsection{*Quotient*}
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
280 |
|
| 25942 | 281 |
lemmas DIVISION_BY_ZERO_DIV [simp] = div_by_0 [of "a\<Colon>nat", standard] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
282 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
283 |
lemma div_geq: "[| 0<n; ~m<n |] ==> m div n = Suc((m-n) div n)" |
| 25942 | 284 |
by (simp add: le_div_geq linorder_not_less) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
285 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
286 |
lemma div_if: "0<n ==> m div n = (if m<n then 0 else Suc((m-n) div n))" |
| 22718 | 287 |
by (simp add: div_geq) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
288 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
289 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
290 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
291 |
(* a simple rearrangement of mod_div_equality: *) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
292 |
lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)" |
| 22718 | 293 |
by (cut_tac m = m and n = n in mod_div_equality2, arith) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
294 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
295 |
lemma mod_less_divisor [simp]: "0<n ==> m mod n < (n::nat)" |
| 22718 | 296 |
apply (induct m rule: nat_less_induct) |
297 |
apply (rename_tac m) |
|
298 |
apply (case_tac "m<n", simp) |
|
299 |
txt{*case @{term "n \<le> m"}*}
|
|
300 |
apply (simp add: mod_geq) |
|
301 |
done |
|
| 15439 | 302 |
|
303 |
lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)" |
|
| 22718 | 304 |
apply (drule mod_less_divisor [where m = m]) |
305 |
apply simp |
|
306 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
307 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
308 |
lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)" |
| 22718 | 309 |
by (simp add: mult_commute div_mult_self_is_m) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
310 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
311 |
(*mod_mult_distrib2 above is the counterpart for remainder*) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
312 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
313 |
|
| 25942 | 314 |
subsubsection {* Proving advancedfacts about Quotient and Remainder *}
|
315 |
||
316 |
definition |
|
317 |
quorem :: "(nat*nat) * (nat*nat) => bool" where |
|
318 |
(*This definition helps prove the harder properties of div and mod. |
|
319 |
It is copied from IntDiv.thy; should it be overloaded?*) |
|
320 |
"quorem = (%((a,b), (q,r)). |
|
321 |
a = b*q + r & |
|
322 |
(if 0<b then 0\<le>r & r<b else b<r & r \<le>0))" |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
323 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
324 |
lemma unique_quotient_lemma: |
| 22718 | 325 |
"[| b*q' + r' \<le> b*q + r; x < b; r < b |] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
326 |
==> q' \<le> (q::nat)" |
| 22718 | 327 |
apply (rule leI) |
328 |
apply (subst less_iff_Suc_add) |
|
329 |
apply (auto simp add: add_mult_distrib2) |
|
330 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
331 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
332 |
lemma unique_quotient: |
| 22718 | 333 |
"[| quorem ((a,b), (q,r)); quorem ((a,b), (q',r')); 0 < b |] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
334 |
==> q = q'" |
| 22718 | 335 |
apply (simp add: split_ifs quorem_def) |
336 |
apply (blast intro: order_antisym |
|
337 |
dest: order_eq_refl [THEN unique_quotient_lemma] sym) |
|
338 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
339 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
340 |
lemma unique_remainder: |
| 22718 | 341 |
"[| quorem ((a,b), (q,r)); quorem ((a,b), (q',r')); 0 < b |] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
342 |
==> r = r'" |
| 22718 | 343 |
apply (subgoal_tac "q = q'") |
344 |
prefer 2 apply (blast intro: unique_quotient) |
|
345 |
apply (simp add: quorem_def) |
|
346 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
347 |
|
| 25162 | 348 |
lemma quorem_div_mod: "b > 0 ==> quorem ((a, b), (a div b, a mod b))" |
349 |
unfolding quorem_def by simp |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
350 |
|
| 25162 | 351 |
lemma quorem_div: "[| quorem((a,b),(q,r)); b > 0 |] ==> a div b = q" |
352 |
by (simp add: quorem_div_mod [THEN unique_quotient]) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
353 |
|
| 25162 | 354 |
lemma quorem_mod: "[| quorem((a,b),(q,r)); b > 0 |] ==> a mod b = r" |
355 |
by (simp add: quorem_div_mod [THEN unique_remainder]) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
356 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
357 |
(** A dividend of zero **) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
358 |
|
| 25942 | 359 |
lemmas div_0 [simp] = semiring_div_class.div_0 [of "n\<Colon>nat", standard] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
360 |
|
| 25942 | 361 |
lemmas mod_0 [simp] = semiring_div_class.mod_0 [of "n\<Colon>nat", standard] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
362 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
363 |
(** proving (a*b) div c = a * (b div c) + a * (b mod c) **) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
364 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
365 |
lemma quorem_mult1_eq: |
| 25162 | 366 |
"[| quorem((b,c),(q,r)); c > 0 |] |
367 |
==> quorem ((a*b, c), (a*q + a*r div c, a*r mod c))" |
|
368 |
by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
369 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
370 |
lemma div_mult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::nat)" |
|
25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
371 |
apply (cases "c = 0", simp) |
| 25942 | 372 |
thm DIVISION_BY_ZERO_DIV |
|
25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
373 |
apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_div]) |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
374 |
done |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
375 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
376 |
lemma mod_mult1_eq: "(a*b) mod c = a*(b mod c) mod (c::nat)" |
|
25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
377 |
apply (cases "c = 0", simp) |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
378 |
apply (blast intro: quorem_div_mod [THEN quorem_mult1_eq, THEN quorem_mod]) |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
379 |
done |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
380 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
381 |
lemma mod_mult1_eq': "(a*b) mod (c::nat) = ((a mod c) * b) mod c" |
| 22718 | 382 |
apply (rule trans) |
383 |
apply (rule_tac s = "b*a mod c" in trans) |
|
384 |
apply (rule_tac [2] mod_mult1_eq) |
|
385 |
apply (simp_all add: mult_commute) |
|
386 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
387 |
|
| 25162 | 388 |
lemma mod_mult_distrib_mod: |
389 |
"(a*b) mod (c::nat) = ((a mod c) * (b mod c)) mod c" |
|
390 |
apply (rule mod_mult1_eq' [THEN trans]) |
|
391 |
apply (rule mod_mult1_eq) |
|
392 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
393 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
394 |
(** proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) **) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
395 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
396 |
lemma quorem_add1_eq: |
| 25162 | 397 |
"[| quorem((a,c),(aq,ar)); quorem((b,c),(bq,br)); c > 0 |] |
398 |
==> quorem ((a+b, c), (aq + bq + (ar+br) div c, (ar+br) mod c))" |
|
399 |
by (auto simp add: split_ifs mult_ac quorem_def add_mult_distrib2) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
400 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
401 |
(*NOT suitable for rewriting: the RHS has an instance of the LHS*) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
402 |
lemma div_add1_eq: |
|
25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
403 |
"(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)" |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
404 |
apply (cases "c = 0", simp) |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
405 |
apply (blast intro: quorem_add1_eq [THEN quorem_div] quorem_div_mod) |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
406 |
done |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
407 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
408 |
lemma mod_add1_eq: "(a+b) mod (c::nat) = (a mod c + b mod c) mod c" |
|
25134
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
409 |
apply (cases "c = 0", simp) |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
410 |
apply (blast intro: quorem_div_mod quorem_add1_eq [THEN quorem_mod]) |
|
3d4953e88449
Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents:
25112
diff
changeset
|
411 |
done |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
412 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
413 |
|
| 25942 | 414 |
subsubsection {* Proving @{prop "a div (b*c) = (a div b) div c"} *}
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
415 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
416 |
(** first, a lemma to bound the remainder **) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
417 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
418 |
lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c" |
| 22718 | 419 |
apply (cut_tac m = q and n = c in mod_less_divisor) |
420 |
apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto) |
|
421 |
apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst) |
|
422 |
apply (simp add: add_mult_distrib2) |
|
423 |
done |
|
|
10559
d3fd54fc659b
many new div and mod properties (borrowed from Integ/IntDiv)
paulson
parents:
10214
diff
changeset
|
424 |
|
| 22718 | 425 |
lemma quorem_mult2_eq: "[| quorem ((a,b), (q,r)); 0 < b; 0 < c |] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
426 |
==> quorem ((a, b*c), (q div c, b*(q mod c) + r))" |
| 22718 | 427 |
by (auto simp add: mult_ac quorem_def add_mult_distrib2 [symmetric] mod_lemma) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
428 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
429 |
lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)" |
| 22718 | 430 |
apply (cases "b = 0", simp) |
431 |
apply (cases "c = 0", simp) |
|
432 |
apply (force simp add: quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_div]) |
|
433 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
434 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
435 |
lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)" |
| 22718 | 436 |
apply (cases "b = 0", simp) |
437 |
apply (cases "c = 0", simp) |
|
438 |
apply (auto simp add: mult_commute quorem_div_mod [THEN quorem_mult2_eq, THEN quorem_mod]) |
|
439 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
440 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
441 |
|
| 25942 | 442 |
subsubsection{*Cancellation of Common Factors in Division*}
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
443 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
444 |
lemma div_mult_mult_lemma: |
| 22718 | 445 |
"[| (0::nat) < b; 0 < c |] ==> (c*a) div (c*b) = a div b" |
446 |
by (auto simp add: div_mult2_eq) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
447 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
448 |
lemma div_mult_mult1 [simp]: "(0::nat) < c ==> (c*a) div (c*b) = a div b" |
| 22718 | 449 |
apply (cases "b = 0") |
450 |
apply (auto simp add: linorder_neq_iff [of b] div_mult_mult_lemma) |
|
451 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
452 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
453 |
lemma div_mult_mult2 [simp]: "(0::nat) < c ==> (a*c) div (b*c) = a div b" |
| 22718 | 454 |
apply (drule div_mult_mult1) |
455 |
apply (auto simp add: mult_commute) |
|
456 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
457 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
458 |
|
| 25942 | 459 |
subsubsection{*Further Facts about Quotient and Remainder*}
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
460 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
461 |
lemma div_1 [simp]: "m div Suc 0 = m" |
| 22718 | 462 |
by (induct m) (simp_all add: div_geq) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
463 |
|
| 25942 | 464 |
lemmas div_self [simp] = semiring_div_class.div_self [of "n\<Colon>nat", standard] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
465 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
466 |
lemma div_add_self2: "0<n ==> (m+n) div n = Suc (m div n)" |
| 22718 | 467 |
apply (subgoal_tac "(n + m) div n = Suc ((n+m-n) div n) ") |
468 |
apply (simp add: add_commute) |
|
469 |
apply (subst div_geq [symmetric], simp_all) |
|
470 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
471 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
472 |
lemma div_add_self1: "0<n ==> (n+m) div n = Suc (m div n)" |
| 22718 | 473 |
by (simp add: add_commute div_add_self2) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
474 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
475 |
lemma div_mult_self1 [simp]: "!!n::nat. 0<n ==> (m + k*n) div n = k + m div n" |
| 22718 | 476 |
apply (subst div_add1_eq) |
477 |
apply (subst div_mult1_eq, simp) |
|
478 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
479 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
480 |
lemma div_mult_self2 [simp]: "0<n ==> (m + n*k) div n = k + m div (n::nat)" |
| 22718 | 481 |
by (simp add: mult_commute div_mult_self1) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
482 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
483 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
484 |
(* Monotonicity of div in first argument *) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
485 |
lemma div_le_mono [rule_format (no_asm)]: |
| 22718 | 486 |
"\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)" |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
487 |
apply (case_tac "k=0", simp) |
| 15251 | 488 |
apply (induct "n" rule: nat_less_induct, clarify) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
489 |
apply (case_tac "n<k") |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
490 |
(* 1 case n<k *) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
491 |
apply simp |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
492 |
(* 2 case n >= k *) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
493 |
apply (case_tac "m<k") |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
494 |
(* 2.1 case m<k *) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
495 |
apply simp |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
496 |
(* 2.2 case m>=k *) |
| 15439 | 497 |
apply (simp add: div_geq diff_le_mono) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
498 |
done |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
499 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
500 |
(* Antimonotonicity of div in second argument *) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
501 |
lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)" |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
502 |
apply (subgoal_tac "0<n") |
| 22718 | 503 |
prefer 2 apply simp |
| 15251 | 504 |
apply (induct_tac k rule: nat_less_induct) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
505 |
apply (rename_tac "k") |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
506 |
apply (case_tac "k<n", simp) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
507 |
apply (subgoal_tac "~ (k<m) ") |
| 22718 | 508 |
prefer 2 apply simp |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
509 |
apply (simp add: div_geq) |
| 15251 | 510 |
apply (subgoal_tac "(k-n) div n \<le> (k-m) div n") |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
511 |
prefer 2 |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
512 |
apply (blast intro: div_le_mono diff_le_mono2) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
513 |
apply (rule le_trans, simp) |
| 15439 | 514 |
apply (simp) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
515 |
done |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
516 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
517 |
lemma div_le_dividend [simp]: "m div n \<le> (m::nat)" |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
518 |
apply (case_tac "n=0", simp) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
519 |
apply (subgoal_tac "m div n \<le> m div 1", simp) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
520 |
apply (rule div_le_mono2) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
521 |
apply (simp_all (no_asm_simp)) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
522 |
done |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
523 |
|
| 22718 | 524 |
(* Similar for "less than" *) |
| 17085 | 525 |
lemma div_less_dividend [rule_format]: |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
526 |
"!!n::nat. 1<n ==> 0 < m --> m div n < m" |
| 15251 | 527 |
apply (induct_tac m rule: nat_less_induct) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
528 |
apply (rename_tac "m") |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
529 |
apply (case_tac "m<n", simp) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
530 |
apply (subgoal_tac "0<n") |
| 22718 | 531 |
prefer 2 apply simp |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
532 |
apply (simp add: div_geq) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
533 |
apply (case_tac "n<m") |
| 15251 | 534 |
apply (subgoal_tac "(m-n) div n < (m-n) ") |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
535 |
apply (rule impI less_trans_Suc)+ |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
536 |
apply assumption |
| 15439 | 537 |
apply (simp_all) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
538 |
done |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
539 |
|
| 17085 | 540 |
declare div_less_dividend [simp] |
541 |
||
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
542 |
text{*A fact for the mutilated chess board*}
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
543 |
lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))" |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
544 |
apply (case_tac "n=0", simp) |
| 15251 | 545 |
apply (induct "m" rule: nat_less_induct) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
546 |
apply (case_tac "Suc (na) <n") |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
547 |
(* case Suc(na) < n *) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
548 |
apply (frule lessI [THEN less_trans], simp add: less_not_refl3) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
549 |
(* case n \<le> Suc(na) *) |
| 16796 | 550 |
apply (simp add: linorder_not_less le_Suc_eq mod_geq) |
| 15439 | 551 |
apply (auto simp add: Suc_diff_le le_mod_geq) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
552 |
done |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
553 |
|
| 14437 | 554 |
lemma nat_mod_div_trivial [simp]: "m mod n div n = (0 :: nat)" |
| 22718 | 555 |
by (cases "n = 0") auto |
| 14437 | 556 |
|
557 |
lemma nat_mod_mod_trivial [simp]: "m mod n mod n = (m mod n :: nat)" |
|
| 22718 | 558 |
by (cases "n = 0") auto |
| 14437 | 559 |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
560 |
|
| 25942 | 561 |
subsubsection{*The Divides Relation*}
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
562 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
563 |
lemma dvdI [intro?]: "n = m * k ==> m dvd n" |
| 22718 | 564 |
unfolding dvd_def by blast |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
565 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
566 |
lemma dvdE [elim?]: "!!P. [|m dvd n; !!k. n = m*k ==> P|] ==> P" |
| 22718 | 567 |
unfolding dvd_def by blast |
| 13152 | 568 |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
569 |
lemma dvd_0_right [iff]: "m dvd (0::nat)" |
| 22718 | 570 |
unfolding dvd_def by (blast intro: mult_0_right [symmetric]) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
571 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
572 |
lemma dvd_0_left: "0 dvd m ==> m = (0::nat)" |
| 22718 | 573 |
by (force simp add: dvd_def) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
574 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
575 |
lemma dvd_0_left_iff [iff]: "(0 dvd (m::nat)) = (m = 0)" |
| 22718 | 576 |
by (blast intro: dvd_0_left) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
577 |
|
|
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24268
diff
changeset
|
578 |
declare dvd_0_left_iff [noatp] |
|
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24268
diff
changeset
|
579 |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
580 |
lemma dvd_1_left [iff]: "Suc 0 dvd k" |
| 22718 | 581 |
unfolding dvd_def by simp |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
582 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
583 |
lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)" |
| 22718 | 584 |
by (simp add: dvd_def) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
585 |
|
| 25942 | 586 |
lemmas dvd_refl [simp] = semiring_div_class.dvd_refl [of "m\<Colon>nat", standard] |
587 |
lemmas dvd_trans [trans] = semiring_div_class.dvd_trans [of "m\<Colon>nat" n p, standard] |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
588 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
589 |
lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)" |
| 22718 | 590 |
unfolding dvd_def |
591 |
by (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
592 |
|
|
23684
8c508c4dc53b
introduced (auxiliary) class dvd_mod for more convenient code generation
haftmann
parents:
23162
diff
changeset
|
593 |
text {* @{term "op dvd"} is a partial order *}
|
|
8c508c4dc53b
introduced (auxiliary) class dvd_mod for more convenient code generation
haftmann
parents:
23162
diff
changeset
|
594 |
|
| 25942 | 595 |
interpretation dvd: order ["op dvd" "\<lambda>n m \<Colon> nat. n dvd m \<and> n \<noteq> m"] |
|
23684
8c508c4dc53b
introduced (auxiliary) class dvd_mod for more convenient code generation
haftmann
parents:
23162
diff
changeset
|
596 |
by unfold_locales (auto intro: dvd_trans dvd_anti_sym) |
|
8c508c4dc53b
introduced (auxiliary) class dvd_mod for more convenient code generation
haftmann
parents:
23162
diff
changeset
|
597 |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
598 |
lemma dvd_add: "[| k dvd m; k dvd n |] ==> k dvd (m+n :: nat)" |
| 22718 | 599 |
unfolding dvd_def |
600 |
by (blast intro: add_mult_distrib2 [symmetric]) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
601 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
602 |
lemma dvd_diff: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)" |
| 22718 | 603 |
unfolding dvd_def |
604 |
by (blast intro: diff_mult_distrib2 [symmetric]) |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
605 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
606 |
lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)" |
| 22718 | 607 |
apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst]) |
608 |
apply (blast intro: dvd_add) |
|
609 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
610 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
611 |
lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)" |
| 22718 | 612 |
by (drule_tac m = m in dvd_diff, auto) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
613 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
614 |
lemma dvd_mult: "k dvd n ==> k dvd (m*n :: nat)" |
| 22718 | 615 |
unfolding dvd_def by (blast intro: mult_left_commute) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
616 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
617 |
lemma dvd_mult2: "k dvd m ==> k dvd (m*n :: nat)" |
| 22718 | 618 |
apply (subst mult_commute) |
619 |
apply (erule dvd_mult) |
|
620 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
621 |
|
|
17084
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16796
diff
changeset
|
622 |
lemma dvd_triv_right [iff]: "k dvd (m*k :: nat)" |
| 22718 | 623 |
by (rule dvd_refl [THEN dvd_mult]) |
|
17084
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16796
diff
changeset
|
624 |
|
|
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16796
diff
changeset
|
625 |
lemma dvd_triv_left [iff]: "k dvd (k*m :: nat)" |
| 22718 | 626 |
by (rule dvd_refl [THEN dvd_mult2]) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
627 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
628 |
lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))" |
| 22718 | 629 |
apply (rule iffI) |
630 |
apply (erule_tac [2] dvd_add) |
|
631 |
apply (rule_tac [2] dvd_refl) |
|
632 |
apply (subgoal_tac "n = (n+k) -k") |
|
633 |
prefer 2 apply simp |
|
634 |
apply (erule ssubst) |
|
635 |
apply (erule dvd_diff) |
|
636 |
apply (rule dvd_refl) |
|
637 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
638 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
639 |
lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n" |
| 22718 | 640 |
unfolding dvd_def |
641 |
apply (case_tac "n = 0", auto) |
|
642 |
apply (blast intro: mod_mult_distrib2 [symmetric]) |
|
643 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
644 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
645 |
lemma dvd_mod_imp_dvd: "[| (k::nat) dvd m mod n; k dvd n |] ==> k dvd m" |
| 22718 | 646 |
apply (subgoal_tac "k dvd (m div n) *n + m mod n") |
647 |
apply (simp add: mod_div_equality) |
|
648 |
apply (simp only: dvd_add dvd_mult) |
|
649 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
650 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
651 |
lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)" |
| 22718 | 652 |
by (blast intro: dvd_mod_imp_dvd dvd_mod) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
653 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
654 |
lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n" |
| 22718 | 655 |
unfolding dvd_def |
656 |
apply (erule exE) |
|
657 |
apply (simp add: mult_ac) |
|
658 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
659 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
660 |
lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))" |
| 22718 | 661 |
apply auto |
662 |
apply (subgoal_tac "m*n dvd m*1") |
|
663 |
apply (drule dvd_mult_cancel, auto) |
|
664 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
665 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
666 |
lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))" |
| 22718 | 667 |
apply (subst mult_commute) |
668 |
apply (erule dvd_mult_cancel1) |
|
669 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
670 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
671 |
lemma mult_dvd_mono: "[| i dvd m; j dvd n|] ==> i*j dvd (m*n :: nat)" |
| 22718 | 672 |
apply (unfold dvd_def, clarify) |
673 |
apply (rule_tac x = "k*ka" in exI) |
|
674 |
apply (simp add: mult_ac) |
|
675 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
676 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
677 |
lemma dvd_mult_left: "(i*j :: nat) dvd k ==> i dvd k" |
| 22718 | 678 |
by (simp add: dvd_def mult_assoc, blast) |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
679 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
680 |
lemma dvd_mult_right: "(i*j :: nat) dvd k ==> j dvd k" |
| 22718 | 681 |
apply (unfold dvd_def, clarify) |
682 |
apply (rule_tac x = "i*k" in exI) |
|
683 |
apply (simp add: mult_ac) |
|
684 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
685 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
686 |
lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)" |
| 22718 | 687 |
apply (unfold dvd_def, clarify) |
688 |
apply (simp_all (no_asm_use) add: zero_less_mult_iff) |
|
689 |
apply (erule conjE) |
|
690 |
apply (rule le_trans) |
|
691 |
apply (rule_tac [2] le_refl [THEN mult_le_mono]) |
|
692 |
apply (erule_tac [2] Suc_leI, simp) |
|
693 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
694 |
|
| 25942 | 695 |
lemmas dvd_eq_mod_eq_0 = dvd_def_mod [of "k\<Colon>nat" n, standard] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
696 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
697 |
lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)" |
| 22718 | 698 |
apply (subgoal_tac "m mod n = 0") |
699 |
apply (simp add: mult_div_cancel) |
|
700 |
apply (simp only: dvd_eq_mod_eq_0) |
|
701 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
702 |
|
| 21408 | 703 |
lemma le_imp_power_dvd: "!!i::nat. m \<le> n ==> i^m dvd i^n" |
| 22718 | 704 |
apply (unfold dvd_def) |
705 |
apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst]) |
|
706 |
apply (simp add: power_add) |
|
707 |
done |
|
| 21408 | 708 |
|
| 25162 | 709 |
lemma nat_zero_less_power_iff [simp]: "(x^n > 0) = (x > (0::nat) | n=0)" |
| 22718 | 710 |
by (induct n) auto |
| 21408 | 711 |
|
712 |
lemma power_le_dvd [rule_format]: "k^j dvd n --> i\<le>j --> k^i dvd (n::nat)" |
|
| 22718 | 713 |
apply (induct j) |
714 |
apply (simp_all add: le_Suc_eq) |
|
715 |
apply (blast dest!: dvd_mult_right) |
|
716 |
done |
|
| 21408 | 717 |
|
718 |
lemma power_dvd_imp_le: "[|i^m dvd i^n; (1::nat) < i|] ==> m \<le> n" |
|
| 22718 | 719 |
apply (rule power_le_imp_le_exp, assumption) |
720 |
apply (erule dvd_imp_le, simp) |
|
721 |
done |
|
| 21408 | 722 |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
723 |
lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)" |
| 22718 | 724 |
by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def) |
|
17084
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16796
diff
changeset
|
725 |
|
| 22718 | 726 |
lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1] |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
727 |
|
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
728 |
(*Loses information, namely we also have r<d provided d is nonzero*) |
|
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
729 |
lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d" |
| 22718 | 730 |
apply (cut_tac m = m in mod_div_equality) |
731 |
apply (simp only: add_ac) |
|
732 |
apply (blast intro: sym) |
|
733 |
done |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
734 |
|
| 14131 | 735 |
|
| 13152 | 736 |
lemma split_div: |
|
13189
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
737 |
"P(n div k :: nat) = |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
738 |
((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
739 |
(is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))") |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
740 |
proof |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
741 |
assume P: ?P |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
742 |
show ?Q |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
743 |
proof (cases) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
744 |
assume "k = 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
745 |
with P show ?Q by(simp add:DIVISION_BY_ZERO_DIV) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
746 |
next |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
747 |
assume not0: "k \<noteq> 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
748 |
thus ?Q |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
749 |
proof (simp, intro allI impI) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
750 |
fix i j |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
751 |
assume n: "n = k*i + j" and j: "j < k" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
752 |
show "P i" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
753 |
proof (cases) |
| 22718 | 754 |
assume "i = 0" |
755 |
with n j P show "P i" by simp |
|
|
13189
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
756 |
next |
| 22718 | 757 |
assume "i \<noteq> 0" |
758 |
with not0 n j P show "P i" by(simp add:add_ac) |
|
|
13189
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
759 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
760 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
761 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
762 |
next |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
763 |
assume Q: ?Q |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
764 |
show ?P |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
765 |
proof (cases) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
766 |
assume "k = 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
767 |
with Q show ?P by(simp add:DIVISION_BY_ZERO_DIV) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
768 |
next |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
769 |
assume not0: "k \<noteq> 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
770 |
with Q have R: ?R by simp |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
771 |
from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"] |
| 13517 | 772 |
show ?P by simp |
|
13189
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
773 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
774 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
775 |
|
| 13882 | 776 |
lemma split_div_lemma: |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
777 |
"0 < n \<Longrightarrow> (n * q \<le> m \<and> m < n * (Suc q)) = (q = ((m::nat) div n))" |
| 25162 | 778 |
apply (rule iffI) |
779 |
apply (rule_tac a=m and r = "m - n * q" and r' = "m mod n" in unique_quotient) |
|
780 |
prefer 3; apply assumption |
|
781 |
apply (simp_all add: quorem_def) |
|
782 |
apply arith |
|
783 |
apply (rule conjI) |
|
784 |
apply (rule_tac P="%x. n * (m div n) \<le> x" in |
|
| 13882 | 785 |
subst [OF mod_div_equality [of _ n]]) |
| 25162 | 786 |
apply (simp only: add: mult_ac) |
787 |
apply (rule_tac P="%x. x < n + n * (m div n)" in |
|
| 13882 | 788 |
subst [OF mod_div_equality [of _ n]]) |
| 25162 | 789 |
apply (simp only: add: mult_ac add_ac) |
790 |
apply (rule add_less_mono1, simp) |
|
791 |
done |
|
| 13882 | 792 |
|
793 |
theorem split_div': |
|
794 |
"P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or> |
|
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
795 |
(\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))" |
| 13882 | 796 |
apply (case_tac "0 < n") |
797 |
apply (simp only: add: split_div_lemma) |
|
798 |
apply (simp_all add: DIVISION_BY_ZERO_DIV) |
|
799 |
done |
|
800 |
||
|
13189
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
801 |
lemma split_mod: |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
802 |
"P(n mod k :: nat) = |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
803 |
((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
804 |
(is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))") |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
805 |
proof |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
806 |
assume P: ?P |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
807 |
show ?Q |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
808 |
proof (cases) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
809 |
assume "k = 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
810 |
with P show ?Q by(simp add:DIVISION_BY_ZERO_MOD) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
811 |
next |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
812 |
assume not0: "k \<noteq> 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
813 |
thus ?Q |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
814 |
proof (simp, intro allI impI) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
815 |
fix i j |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
816 |
assume "n = k*i + j" "j < k" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
817 |
thus "P j" using not0 P by(simp add:add_ac mult_ac) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
818 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
819 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
820 |
next |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
821 |
assume Q: ?Q |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
822 |
show ?P |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
823 |
proof (cases) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
824 |
assume "k = 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
825 |
with Q show ?P by(simp add:DIVISION_BY_ZERO_MOD) |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
826 |
next |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
827 |
assume not0: "k \<noteq> 0" |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
828 |
with Q have R: ?R by simp |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
829 |
from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"] |
| 13517 | 830 |
show ?P by simp |
|
13189
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
831 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
832 |
qed |
|
81ed5c6de890
Now arith can deal with div/mod arbitrary nat numerals.
nipkow
parents:
13152
diff
changeset
|
833 |
|
| 13882 | 834 |
theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n" |
835 |
apply (rule_tac P="%x. m mod n = x - (m div n) * n" in |
|
836 |
subst [OF mod_div_equality [of _ n]]) |
|
837 |
apply arith |
|
838 |
done |
|
839 |
||
| 22800 | 840 |
lemma div_mod_equality': |
841 |
fixes m n :: nat |
|
842 |
shows "m div n * n = m - m mod n" |
|
843 |
proof - |
|
844 |
have "m mod n \<le> m mod n" .. |
|
845 |
from div_mod_equality have |
|
846 |
"m div n * n + m mod n - m mod n = m - m mod n" by simp |
|
847 |
with diff_add_assoc [OF `m mod n \<le> m mod n`, of "m div n * n"] have |
|
848 |
"m div n * n + (m mod n - m mod n) = m - m mod n" |
|
849 |
by simp |
|
850 |
then show ?thesis by simp |
|
851 |
qed |
|
852 |
||
853 |
||
| 25942 | 854 |
subsubsection {*An ``induction'' law for modulus arithmetic.*}
|
| 14640 | 855 |
|
856 |
lemma mod_induct_0: |
|
857 |
assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)" |
|
858 |
and base: "P i" and i: "i<p" |
|
859 |
shows "P 0" |
|
860 |
proof (rule ccontr) |
|
861 |
assume contra: "\<not>(P 0)" |
|
862 |
from i have p: "0<p" by simp |
|
863 |
have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k") |
|
864 |
proof |
|
865 |
fix k |
|
866 |
show "?A k" |
|
867 |
proof (induct k) |
|
868 |
show "?A 0" by simp -- "by contradiction" |
|
869 |
next |
|
870 |
fix n |
|
871 |
assume ih: "?A n" |
|
872 |
show "?A (Suc n)" |
|
873 |
proof (clarsimp) |
|
| 22718 | 874 |
assume y: "P (p - Suc n)" |
875 |
have n: "Suc n < p" |
|
876 |
proof (rule ccontr) |
|
877 |
assume "\<not>(Suc n < p)" |
|
878 |
hence "p - Suc n = 0" |
|
879 |
by simp |
|
880 |
with y contra show "False" |
|
881 |
by simp |
|
882 |
qed |
|
883 |
hence n2: "Suc (p - Suc n) = p-n" by arith |
|
884 |
from p have "p - Suc n < p" by arith |
|
885 |
with y step have z: "P ((Suc (p - Suc n)) mod p)" |
|
886 |
by blast |
|
887 |
show "False" |
|
888 |
proof (cases "n=0") |
|
889 |
case True |
|
890 |
with z n2 contra show ?thesis by simp |
|
891 |
next |
|
892 |
case False |
|
893 |
with p have "p-n < p" by arith |
|
894 |
with z n2 False ih show ?thesis by simp |
|
895 |
qed |
|
| 14640 | 896 |
qed |
897 |
qed |
|
898 |
qed |
|
899 |
moreover |
|
900 |
from i obtain k where "0<k \<and> i+k=p" |
|
901 |
by (blast dest: less_imp_add_positive) |
|
902 |
hence "0<k \<and> i=p-k" by auto |
|
903 |
moreover |
|
904 |
note base |
|
905 |
ultimately |
|
906 |
show "False" by blast |
|
907 |
qed |
|
908 |
||
909 |
lemma mod_induct: |
|
910 |
assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)" |
|
911 |
and base: "P i" and i: "i<p" and j: "j<p" |
|
912 |
shows "P j" |
|
913 |
proof - |
|
914 |
have "\<forall>j<p. P j" |
|
915 |
proof |
|
916 |
fix j |
|
917 |
show "j<p \<longrightarrow> P j" (is "?A j") |
|
918 |
proof (induct j) |
|
919 |
from step base i show "?A 0" |
|
| 22718 | 920 |
by (auto elim: mod_induct_0) |
| 14640 | 921 |
next |
922 |
fix k |
|
923 |
assume ih: "?A k" |
|
924 |
show "?A (Suc k)" |
|
925 |
proof |
|
| 22718 | 926 |
assume suc: "Suc k < p" |
927 |
hence k: "k<p" by simp |
|
928 |
with ih have "P k" .. |
|
929 |
with step k have "P (Suc k mod p)" |
|
930 |
by blast |
|
931 |
moreover |
|
932 |
from suc have "Suc k mod p = Suc k" |
|
933 |
by simp |
|
934 |
ultimately |
|
935 |
show "P (Suc k)" by simp |
|
| 14640 | 936 |
qed |
937 |
qed |
|
938 |
qed |
|
939 |
with j show ?thesis by blast |
|
940 |
qed |
|
941 |
||
942 |
||
|
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18154
diff
changeset
|
943 |
lemma mod_add_left_eq: "((a::nat) + b) mod c = (a mod c + b) mod c" |
|
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18154
diff
changeset
|
944 |
apply (rule trans [symmetric]) |
| 22718 | 945 |
apply (rule mod_add1_eq, simp) |
|
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18154
diff
changeset
|
946 |
apply (rule mod_add1_eq [symmetric]) |
|
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18154
diff
changeset
|
947 |
done |
|
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18154
diff
changeset
|
948 |
|
|
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18154
diff
changeset
|
949 |
lemma mod_add_right_eq: "(a+b) mod (c::nat) = (a + (b mod c)) mod c" |
| 22718 | 950 |
apply (rule trans [symmetric]) |
951 |
apply (rule mod_add1_eq, simp) |
|
952 |
apply (rule mod_add1_eq [symmetric]) |
|
953 |
done |
|
|
18202
46af82efd311
presburger method updated to deal better with mod and div, tweo lemmas added to Divides.thy
chaieb
parents:
18154
diff
changeset
|
954 |
|
| 22800 | 955 |
lemma mod_div_decomp: |
956 |
fixes n k :: nat |
|
957 |
obtains m q where "m = n div k" and "q = n mod k" |
|
958 |
and "n = m * k + q" |
|
959 |
proof - |
|
960 |
from mod_div_equality have "n = n div k * k + n mod k" by auto |
|
961 |
moreover have "n div k = n div k" .. |
|
962 |
moreover have "n mod k = n mod k" .. |
|
963 |
note that ultimately show thesis by blast |
|
964 |
qed |
|
965 |
||
| 20589 | 966 |
|
| 25942 | 967 |
subsubsection {* Code generation for div, mod and dvd on nat *}
|
| 20589 | 968 |
|
| 22845 | 969 |
definition [code func del]: |
| 20589 | 970 |
"divmod (m\<Colon>nat) n = (m div n, m mod n)" |
971 |
||
| 22718 | 972 |
lemma divmod_zero [code]: "divmod m 0 = (0, m)" |
| 20589 | 973 |
unfolding divmod_def by simp |
974 |
||
975 |
lemma divmod_succ [code]: |
|
976 |
"divmod m (Suc k) = (if m < Suc k then (0, m) else |
|
977 |
let |
|
978 |
(p, q) = divmod (m - Suc k) (Suc k) |
|
| 22718 | 979 |
in (Suc p, q))" |
| 20589 | 980 |
unfolding divmod_def Let_def split_def |
981 |
by (auto intro: div_geq mod_geq) |
|
982 |
||
| 22718 | 983 |
lemma div_divmod [code]: "m div n = fst (divmod m n)" |
| 20589 | 984 |
unfolding divmod_def by simp |
985 |
||
| 22718 | 986 |
lemma mod_divmod [code]: "m mod n = snd (divmod m n)" |
| 20589 | 987 |
unfolding divmod_def by simp |
988 |
||
| 21191 | 989 |
code_modulename SML |
| 23017 | 990 |
Divides Nat |
| 20640 | 991 |
|
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21408
diff
changeset
|
992 |
code_modulename OCaml |
| 23017 | 993 |
Divides Nat |
994 |
||
995 |
code_modulename Haskell |
|
996 |
Divides Nat |
|
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21408
diff
changeset
|
997 |
|
|
23684
8c508c4dc53b
introduced (auxiliary) class dvd_mod for more convenient code generation
haftmann
parents:
23162
diff
changeset
|
998 |
hide (open) const divmod |
|
14267
b963e9cee2a0
More refinements to Ring_and_Field and numerics. Conversion of Divides_lemmas
paulson
parents:
14208
diff
changeset
|
999 |
|
| 3366 | 1000 |
end |