author | haftmann |
Sat, 08 Aug 2015 10:51:33 +0200 | |
changeset 60868 | dd18c33c001e |
parent 60867 | 86e7560e07d0 |
child 60930 | dd8ab7252ba2 |
permissions | -rw-r--r-- |
3366 | 1 |
(* Title: HOL/Divides.thy |
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
6865
5577ffe4c2f1
now div and mod are overloaded; dvd is polymorphic
paulson
parents:
3366
diff
changeset
|
3 |
Copyright 1999 University of Cambridge |
18154 | 4 |
*) |
3366 | 5 |
|
60758 | 6 |
section \<open>The division operators div and mod\<close> |
3366 | 7 |
|
15131 | 8 |
theory Divides |
58778
e29cae8eab1f
even further downshift of theory Parity in the hierarchy
haftmann
parents:
58710
diff
changeset
|
9 |
imports Parity |
15131 | 10 |
begin |
3366 | 11 |
|
60758 | 12 |
subsection \<open>Abstract division in commutative semirings.\<close> |
25942 | 13 |
|
60352
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
haftmann
parents:
59833
diff
changeset
|
14 |
class div = dvd + divide + |
d46de31a50c4
separate class for division operator, with particular syntax added in more specific classes
haftmann
parents:
59833
diff
changeset
|
15 |
fixes mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70) |
25942 | 16 |
|
59833
ab828c2c5d67
clarified no_zero_devisors: makes only sense in a semiring;
haftmann
parents:
59816
diff
changeset
|
17 |
class semiring_div = semidom + div + |
25942 | 18 |
assumes mod_div_equality: "a div b * b + a mod b = a" |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
19 |
and div_by_0 [simp]: "a div 0 = 0" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
20 |
and div_0 [simp]: "0 div a = 0" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
21 |
and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b" |
30930 | 22 |
and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b" |
25942 | 23 |
begin |
24 |
||
60517
f16e4fb20652
separate class for notions specific for integral (semi)domains, in contrast to fields where these are trivial
haftmann
parents:
60516
diff
changeset
|
25 |
subclass algebraic_semidom |
60353
838025c6e278
implicit partial divison operation in integral domains
haftmann
parents:
60352
diff
changeset
|
26 |
proof |
838025c6e278
implicit partial divison operation in integral domains
haftmann
parents:
60352
diff
changeset
|
27 |
fix b a |
838025c6e278
implicit partial divison operation in integral domains
haftmann
parents:
60352
diff
changeset
|
28 |
assume "b \<noteq> 0" |
838025c6e278
implicit partial divison operation in integral domains
haftmann
parents:
60352
diff
changeset
|
29 |
then show "a * b div b = a" |
838025c6e278
implicit partial divison operation in integral domains
haftmann
parents:
60352
diff
changeset
|
30 |
using div_mult_self1 [of b 0 a] by (simp add: ac_simps) |
838025c6e278
implicit partial divison operation in integral domains
haftmann
parents:
60352
diff
changeset
|
31 |
qed simp |
58953 | 32 |
|
60867 | 33 |
lemma div_by_1: |
34 |
"a div 1 = a" |
|
35 |
by (fact divide_1) |
|
36 |
||
37 |
lemma div_mult_self1_is_id: |
|
38 |
"b \<noteq> 0 \<Longrightarrow> b * a div b = a" |
|
39 |
by (fact nonzero_mult_divide_cancel_left) |
|
40 |
||
41 |
lemma div_mult_self2_is_id: |
|
42 |
"b \<noteq> 0 \<Longrightarrow> a * b div b = a" |
|
43 |
by (fact nonzero_mult_divide_cancel_right) |
|
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
59000
diff
changeset
|
44 |
|
60758 | 45 |
text \<open>@{const divide} and @{const mod}\<close> |
26100
fbc60cd02ae2
using only an relation predicate to construct div and mod
haftmann
parents:
26072
diff
changeset
|
46 |
|
26062 | 47 |
lemma mod_div_equality2: "b * (a div b) + a mod b = a" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
48 |
unfolding mult.commute [of b] |
26062 | 49 |
by (rule mod_div_equality) |
50 |
||
29403
fe17df4e4ab3
generalize some div/mod lemmas; remove type-specific proofs
huffman
parents:
29252
diff
changeset
|
51 |
lemma mod_div_equality': "a mod b + a div b * b = a" |
fe17df4e4ab3
generalize some div/mod lemmas; remove type-specific proofs
huffman
parents:
29252
diff
changeset
|
52 |
using mod_div_equality [of a b] |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
53 |
by (simp only: ac_simps) |
29403
fe17df4e4ab3
generalize some div/mod lemmas; remove type-specific proofs
huffman
parents:
29252
diff
changeset
|
54 |
|
26062 | 55 |
lemma div_mod_equality: "((a div b) * b + a mod b) + c = a + c" |
30934 | 56 |
by (simp add: mod_div_equality) |
26062 | 57 |
|
58 |
lemma div_mod_equality2: "(b * (a div b) + a mod b) + c = a + c" |
|
30934 | 59 |
by (simp add: mod_div_equality2) |
26062 | 60 |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
61 |
lemma mod_by_0 [simp]: "a mod 0 = a" |
30934 | 62 |
using mod_div_equality [of a zero] by simp |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
63 |
|
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
64 |
lemma mod_0 [simp]: "0 mod a = 0" |
30934 | 65 |
using mod_div_equality [of zero a] div_0 by simp |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
66 |
|
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
67 |
lemma div_mult_self2 [simp]: |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
68 |
assumes "b \<noteq> 0" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
69 |
shows "(a + b * c) div b = c + a div b" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
70 |
using assms div_mult_self1 [of b a c] by (simp add: mult.commute) |
26100
fbc60cd02ae2
using only an relation predicate to construct div and mod
haftmann
parents:
26072
diff
changeset
|
71 |
|
54221 | 72 |
lemma div_mult_self3 [simp]: |
73 |
assumes "b \<noteq> 0" |
|
74 |
shows "(c * b + a) div b = c + a div b" |
|
75 |
using assms by (simp add: add.commute) |
|
76 |
||
77 |
lemma div_mult_self4 [simp]: |
|
78 |
assumes "b \<noteq> 0" |
|
79 |
shows "(b * c + a) div b = c + a div b" |
|
80 |
using assms by (simp add: add.commute) |
|
81 |
||
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
82 |
lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
83 |
proof (cases "b = 0") |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
84 |
case True then show ?thesis by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
85 |
next |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
86 |
case False |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
87 |
have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
88 |
by (simp add: mod_div_equality) |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
89 |
also from False div_mult_self1 [of b a c] have |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
90 |
"\<dots> = (c + a div b) * b + (a + c * b) mod b" |
29667 | 91 |
by (simp add: algebra_simps) |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
92 |
finally have "a = a div b * b + (a + c * b) mod b" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
93 |
by (simp add: add.commute [of a] add.assoc distrib_right) |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
94 |
then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
95 |
by (simp add: mod_div_equality) |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
96 |
then show ?thesis by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
97 |
qed |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
98 |
|
60562
24af00b010cf
Amalgamation of the class comm_semiring_1_diff_distrib into comm_semiring_1_cancel. Moving axiom le_add_diff_inverse2 from semiring_numeral_div to linordered_semidom.
paulson <lp15@cam.ac.uk>
parents:
60517
diff
changeset
|
99 |
lemma mod_mult_self2 [simp]: |
54221 | 100 |
"(a + b * c) mod b = a mod b" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
101 |
by (simp add: mult.commute [of b]) |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
102 |
|
54221 | 103 |
lemma mod_mult_self3 [simp]: |
104 |
"(c * b + a) mod b = a mod b" |
|
105 |
by (simp add: add.commute) |
|
106 |
||
107 |
lemma mod_mult_self4 [simp]: |
|
108 |
"(b * c + a) mod b = a mod b" |
|
109 |
by (simp add: add.commute) |
|
110 |
||
60867 | 111 |
lemma mod_mult_self1_is_0 [simp]: |
112 |
"b * a mod b = 0" |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
113 |
using mod_mult_self2 [of 0 b a] by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
114 |
|
60867 | 115 |
lemma mod_mult_self2_is_0 [simp]: |
116 |
"a * b mod b = 0" |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
117 |
using mod_mult_self1 [of 0 a b] by simp |
26062 | 118 |
|
60867 | 119 |
lemma mod_by_1 [simp]: |
120 |
"a mod 1 = 0" |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
121 |
proof - |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
122 |
from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
123 |
then have "a + a mod 1 = a + 0" by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
124 |
then show ?thesis by (rule add_left_imp_eq) |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
125 |
qed |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
126 |
|
60867 | 127 |
lemma mod_self [simp]: |
128 |
"a mod a = 0" |
|
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
129 |
using mod_mult_self2_is_0 [of 1] by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
130 |
|
27676 | 131 |
lemma div_add_self1 [simp]: |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
132 |
assumes "b \<noteq> 0" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
133 |
shows "(b + a) div b = a div b + 1" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
134 |
using assms div_mult_self1 [of b a 1] by (simp add: add.commute) |
26062 | 135 |
|
27676 | 136 |
lemma div_add_self2 [simp]: |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
137 |
assumes "b \<noteq> 0" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
138 |
shows "(a + b) div b = a div b + 1" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
139 |
using assms div_add_self1 [of b a] by (simp add: add.commute) |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
140 |
|
27676 | 141 |
lemma mod_add_self1 [simp]: |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
142 |
"(b + a) mod b = a mod b" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57492
diff
changeset
|
143 |
using mod_mult_self1 [of a 1 b] by (simp add: add.commute) |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
144 |
|
27676 | 145 |
lemma mod_add_self2 [simp]: |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
146 |
"(a + b) mod b = a mod b" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
147 |
using mod_mult_self1 [of a 1 b] by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
148 |
|
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
149 |
lemma mod_div_decomp: |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
150 |
fixes a b |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
151 |
obtains q r where "q = a div b" and "r = a mod b" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
152 |
and "a = q * b + r" |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
153 |
proof - |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
154 |
from mod_div_equality have "a = a div b * b + a mod b" by simp |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
155 |
moreover have "a div b = a div b" .. |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27540
diff
changeset
|
156 |
moreover have "a mod b = a mod b" .. |
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
|