| author | wenzelm |
| Sat, 30 Dec 2006 16:08:04 +0100 | |
| changeset 21964 | df2e82888a66 |
| parent 21834 | 770ce948a59b |
| child 22187 | a2c4861363d5 |
| permissions | -rw-r--r-- |
| 20485 | 1 |
(* Title: HOL/Integ/Numeral.thy |
| 15013 | 2 |
ID: $Id$ |
| 20485 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
4 |
Copyright 1994 University of Cambridge |
|
| 15013 | 5 |
*) |
6 |
||
| 20485 | 7 |
header {* Arithmetic on Binary Integers *}
|
| 15013 | 8 |
|
| 15131 | 9 |
theory Numeral |
|
15620
8ccdc8bc66a2
replaced bool by a new datatype "bit" for binary numerals
paulson
parents:
15140
diff
changeset
|
10 |
imports IntDef Datatype |
| 16417 | 11 |
uses "../Tools/numeral_syntax.ML" |
| 15131 | 12 |
begin |
| 15013 | 13 |
|
| 20485 | 14 |
text {*
|
15 |
This formalization defines binary arithmetic in terms of the integers |
|
16 |
rather than using a datatype. This avoids multiple representations (leading |
|
17 |
zeroes, etc.) See @{text "ZF/Integ/twos-compl.ML"}, function @{text
|
|
18 |
int_of_binary}, for the numerical interpretation. |
|
| 15013 | 19 |
|
| 20485 | 20 |
The representation expects that @{text "(m mod 2)"} is 0 or 1,
|
21 |
even if m is negative; |
|
22 |
For instance, @{text "-5 div 2 = -3"} and @{text "-5 mod 2 = 1"}; thus
|
|
23 |
@{text "-5 = (-3)*2 + 1"}.
|
|
| 15013 | 24 |
*} |
25 |
||
| 20485 | 26 |
text{*
|
| 21834 | 27 |
This type avoids the use of type @{typ bool}, which would make
|
| 20485 | 28 |
all of the rewrite rules higher-order. |
29 |
*} |
|
| 15013 | 30 |
|
|
15620
8ccdc8bc66a2
replaced bool by a new datatype "bit" for binary numerals
paulson
parents:
15140
diff
changeset
|
31 |
datatype bit = B0 | B1 |
|
8ccdc8bc66a2
replaced bool by a new datatype "bit" for binary numerals
paulson
parents:
15140
diff
changeset
|
32 |
|
| 21779 | 33 |
definition |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21779
diff
changeset
|
34 |
Pls :: int where |
|
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21779
diff
changeset
|
35 |
"Pls = 0" |
| 21779 | 36 |
|
37 |
definition |
|
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21779
diff
changeset
|
38 |
Min :: int where |
|
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21779
diff
changeset
|
39 |
"Min = - 1" |
| 21779 | 40 |
|
41 |
definition |
|
42 |
Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where |
|
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21779
diff
changeset
|
43 |
"k BIT b = (case b of B0 \<Rightarrow> 0 | B1 \<Rightarrow> 1) + k + k" |
| 15013 | 44 |
|
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21779
diff
changeset
|
45 |
class number = -- {* for numeric types: nat, int, real, \dots *}
|
|
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
21779
diff
changeset
|
46 |
fixes number_of :: "int \<Rightarrow> 'a" |
| 15013 | 47 |
|
48 |
syntax |
|
| 20485 | 49 |
"_Numeral" :: "num_const \<Rightarrow> 'a" ("_")
|
| 15013 | 50 |
|
51 |
setup NumeralSyntax.setup |
|
52 |
||
| 19380 | 53 |
abbreviation |
| 20485 | 54 |
"Numeral0 \<equiv> number_of Pls" |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20900
diff
changeset
|
55 |
|
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20900
diff
changeset
|
56 |
abbreviation |
| 20485 | 57 |
"Numeral1 \<equiv> number_of (Pls BIT B1)" |
| 15013 | 58 |
|
| 20485 | 59 |
lemma Let_number_of [simp]: "Let (number_of v) f = f (number_of v)" |
| 15013 | 60 |
-- {* Unfold all @{text let}s involving constants *}
|
| 20485 | 61 |
unfolding Let_def .. |
62 |
||
63 |
lemma Let_0 [simp]: "Let 0 f = f 0" |
|
64 |
unfolding Let_def .. |
|
65 |
||
66 |
lemma Let_1 [simp]: "Let 1 f = f 1" |
|
67 |
unfolding Let_def .. |
|
| 15013 | 68 |
|
| 20485 | 69 |
definition |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20900
diff
changeset
|
70 |
succ :: "int \<Rightarrow> int" where |
| 20485 | 71 |
"succ k = k + 1" |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20900
diff
changeset
|
72 |
|
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20900
diff
changeset
|
73 |
definition |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20900
diff
changeset
|
74 |
pred :: "int \<Rightarrow> int" where |
| 20485 | 75 |
"pred k = k - 1" |
76 |
||
77 |
lemmas numeral_simps = |
|
78 |
succ_def pred_def Pls_def Min_def Bit_def |
|
| 15013 | 79 |
|
| 20485 | 80 |
text {* Removal of leading zeroes *}
|
81 |
||
| 20699 | 82 |
lemma Pls_0_eq [simp, code func]: |
| 20485 | 83 |
"Pls BIT B0 = Pls" |
84 |
unfolding numeral_simps by simp |
|
85 |
||
| 20699 | 86 |
lemma Min_1_eq [simp, code func]: |
| 20485 | 87 |
"Min BIT B1 = Min" |
88 |
unfolding numeral_simps by simp |
|
| 15013 | 89 |
|
90 |
||
| 20485 | 91 |
subsection {* The Functions @{term succ}, @{term pred} and @{term uminus} *}
|
| 15013 | 92 |
|
| 20485 | 93 |
lemma succ_Pls [simp]: |
94 |
"succ Pls = Pls BIT B1" |
|
95 |
unfolding numeral_simps by simp |
|
| 15013 | 96 |
|
| 20485 | 97 |
lemma succ_Min [simp]: |
98 |
"succ Min = Pls" |
|
99 |
unfolding numeral_simps by simp |
|
| 15013 | 100 |
|
| 20485 | 101 |
lemma succ_1 [simp]: |
102 |
"succ (k BIT B1) = succ k BIT B0" |
|
103 |
unfolding numeral_simps by simp |
|
| 15013 | 104 |
|
| 20485 | 105 |
lemma succ_0 [simp]: |
106 |
"succ (k BIT B0) = k BIT B1" |
|
107 |
unfolding numeral_simps by simp |
|
| 15013 | 108 |
|
| 20485 | 109 |
lemma pred_Pls [simp]: |
110 |
"pred Pls = Min" |
|
111 |
unfolding numeral_simps by simp |
|
| 15013 | 112 |
|
| 20485 | 113 |
lemma pred_Min [simp]: |
114 |
"pred Min = Min BIT B0" |
|
115 |
unfolding numeral_simps by simp |
|
| 15013 | 116 |
|
| 20485 | 117 |
lemma pred_1 [simp]: |
118 |
"pred (k BIT B1) = k BIT B0" |
|
119 |
unfolding numeral_simps by simp |
|
| 15013 | 120 |
|
| 20485 | 121 |
lemma pred_0 [simp]: |
122 |
"pred (k BIT B0) = pred k BIT B1" |
|
123 |
unfolding numeral_simps by simp |
|
| 15013 | 124 |
|
| 20485 | 125 |
lemma minus_Pls [simp]: |
126 |
"- Pls = Pls" |
|
127 |
unfolding numeral_simps by simp |
|
| 15013 | 128 |
|
| 20485 | 129 |
lemma minus_Min [simp]: |
130 |
"- Min = Pls BIT B1" |
|
131 |
unfolding numeral_simps by simp |
|
| 15013 | 132 |
|
| 20485 | 133 |
lemma minus_1 [simp]: |
134 |
"- (k BIT B1) = pred (- k) BIT B1" |
|
135 |
unfolding numeral_simps by simp |
|
| 15013 | 136 |
|
| 20485 | 137 |
lemma minus_0 [simp]: |
138 |
"- (k BIT B0) = (- k) BIT B0" |
|
139 |
unfolding numeral_simps by simp |
|
| 15013 | 140 |
|
141 |
||
| 20485 | 142 |
subsection {*
|
143 |
Binary Addition and Multiplication: @{term "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
|
|
144 |
and @{term "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"}
|
|
145 |
*} |
|
| 15013 | 146 |
|
| 20485 | 147 |
lemma add_Pls [simp]: |
148 |
"Pls + k = k" |
|
149 |
unfolding numeral_simps by simp |
|
| 15013 | 150 |
|
| 20485 | 151 |
lemma add_Min [simp]: |
152 |
"Min + k = pred k" |
|
153 |
unfolding numeral_simps by simp |
|
| 15013 | 154 |
|
| 20485 | 155 |
lemma add_BIT_11 [simp]: |
156 |
"(k BIT B1) + (l BIT B1) = (k + succ l) BIT B0" |
|
157 |
unfolding numeral_simps by simp |
|
| 15013 | 158 |
|
| 20485 | 159 |
lemma add_BIT_10 [simp]: |
160 |
"(k BIT B1) + (l BIT B0) = (k + l) BIT B1" |
|
161 |
unfolding numeral_simps by simp |
|
| 15013 | 162 |
|
| 20485 | 163 |
lemma add_BIT_0 [simp]: |
164 |
"(k BIT B0) + (l BIT b) = (k + l) BIT b" |
|
165 |
unfolding numeral_simps by simp |
|
| 15013 | 166 |
|
| 20485 | 167 |
lemma add_Pls_right [simp]: |
168 |
"k + Pls = k" |
|
169 |
unfolding numeral_simps by simp |
|
| 15013 | 170 |
|
| 20485 | 171 |
lemma add_Min_right [simp]: |
172 |
"k + Min = pred k" |
|
173 |
unfolding numeral_simps by simp |
|
| 15013 | 174 |
|
| 20485 | 175 |
lemma mult_Pls [simp]: |
176 |
"Pls * w = Pls" |
|
177 |
unfolding numeral_simps by simp |
|
| 15013 | 178 |
|
| 20485 | 179 |
lemma mult_Min [simp]: |
180 |
"Min * k = - k" |
|
181 |
unfolding numeral_simps by simp |
|
| 15013 | 182 |
|
| 20485 | 183 |
lemma mult_num1 [simp]: |
184 |
"(k BIT B1) * l = ((k * l) BIT B0) + l" |
|
185 |
unfolding numeral_simps int_distrib by simp |
|
| 15013 | 186 |
|
| 20485 | 187 |
lemma mult_num0 [simp]: |
188 |
"(k BIT B0) * l = (k * l) BIT B0" |
|
189 |
unfolding numeral_simps int_distrib by simp |
|
| 15013 | 190 |
|
191 |
||
192 |
||
| 20485 | 193 |
subsection {* Converting Numerals to Rings: @{term number_of} *}
|
| 15013 | 194 |
|
195 |
axclass number_ring \<subseteq> number, comm_ring_1 |
|
| 20485 | 196 |
number_of_eq: "number_of k = of_int k" |
| 15013 | 197 |
|
198 |
lemma number_of_succ: |
|
| 20485 | 199 |
"number_of (succ k) = (1 + number_of k ::'a::number_ring)" |
200 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 201 |
|
202 |
lemma number_of_pred: |
|
| 20485 | 203 |
"number_of (pred w) = (- 1 + number_of w ::'a::number_ring)" |
204 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 205 |
|
206 |
lemma number_of_minus: |
|
| 20485 | 207 |
"number_of (uminus w) = (- (number_of w)::'a::number_ring)" |
208 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 209 |
|
210 |
lemma number_of_add: |
|
| 20485 | 211 |
"number_of (v + w) = (number_of v + number_of w::'a::number_ring)" |
212 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 213 |
|
214 |
lemma number_of_mult: |
|
| 20485 | 215 |
"number_of (v * w) = (number_of v * number_of w::'a::number_ring)" |
216 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 217 |
|
| 20485 | 218 |
text {*
|
219 |
The correctness of shifting. |
|
220 |
But it doesn't seem to give a measurable speed-up. |
|
221 |
*} |
|
222 |
||
| 15013 | 223 |
lemma double_number_of_BIT: |
| 20485 | 224 |
"(1 + 1) * number_of w = (number_of (w BIT B0) ::'a::number_ring)" |
225 |
unfolding number_of_eq numeral_simps left_distrib by simp |
|
| 15013 | 226 |
|
| 20485 | 227 |
text {*
|
228 |
Converting numerals 0 and 1 to their abstract versions. |
|
229 |
*} |
|
230 |
||
231 |
lemma numeral_0_eq_0 [simp]: |
|
232 |
"Numeral0 = (0::'a::number_ring)" |
|
233 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 234 |
|
| 20485 | 235 |
lemma numeral_1_eq_1 [simp]: |
236 |
"Numeral1 = (1::'a::number_ring)" |
|
237 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 238 |
|
| 20485 | 239 |
text {*
|
240 |
Special-case simplification for small constants. |
|
241 |
*} |
|
| 15013 | 242 |
|
| 20485 | 243 |
text{*
|
244 |
Unary minus for the abstract constant 1. Cannot be inserted |
|
245 |
as a simprule until later: it is @{text number_of_Min} re-oriented!
|
|
246 |
*} |
|
| 15013 | 247 |
|
| 20485 | 248 |
lemma numeral_m1_eq_minus_1: |
249 |
"(-1::'a::number_ring) = - 1" |
|
250 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 251 |
|
| 20485 | 252 |
lemma mult_minus1 [simp]: |
253 |
"-1 * z = -(z::'a::number_ring)" |
|
254 |
unfolding number_of_eq numeral_simps by simp |
|
255 |
||
256 |
lemma mult_minus1_right [simp]: |
|
257 |
"z * -1 = -(z::'a::number_ring)" |
|
258 |
unfolding number_of_eq numeral_simps by simp |
|
| 15013 | 259 |
|
260 |
(*Negation of a coefficient*) |
|
261 |
lemma minus_number_of_mult [simp]: |
|
| 20485 | 262 |
"- (number_of w) * z = number_of (uminus w) * (z::'a::number_ring)" |
263 |
unfolding number_of_eq by simp |
|
264 |
||
265 |
text {* Subtraction *}
|
|
266 |
||
267 |
lemma diff_number_of_eq: |
|
268 |
"number_of v - number_of w = |
|
269 |
(number_of (v + uminus w)::'a::number_ring)" |
|
270 |
unfolding number_of_eq by simp |
|
| 15013 | 271 |
|
| 20485 | 272 |
lemma number_of_Pls: |
273 |
"number_of Pls = (0::'a::number_ring)" |
|
274 |
unfolding number_of_eq numeral_simps by simp |
|
275 |
||
276 |
lemma number_of_Min: |
|
277 |
"number_of Min = (- 1::'a::number_ring)" |
|
278 |
unfolding number_of_eq numeral_simps by simp |
|
279 |
||
280 |
lemma number_of_BIT: |
|
281 |
"number_of(w BIT x) = (case x of B0 => 0 | B1 => (1::'a::number_ring)) |
|
282 |
+ (number_of w) + (number_of w)" |
|
283 |
unfolding number_of_eq numeral_simps by (simp split: bit.split) |
|
| 15013 | 284 |
|
285 |
||
| 20485 | 286 |
subsection {* Equality of Binary Numbers *}
|
| 15013 | 287 |
|
| 20485 | 288 |
text {* First version by Norbert Voelker *}
|
| 15013 | 289 |
|
290 |
lemma eq_number_of_eq: |
|
291 |
"((number_of x::'a::number_ring) = number_of y) = |
|
| 20485 | 292 |
iszero (number_of (x + uminus y) :: 'a)" |
293 |
unfolding iszero_def number_of_add number_of_minus |
|
294 |
by (simp add: compare_rls) |
|
| 15013 | 295 |
|
| 20485 | 296 |
lemma iszero_number_of_Pls: |
297 |
"iszero ((number_of Pls)::'a::number_ring)" |
|
298 |
unfolding iszero_def numeral_0_eq_0 .. |
|
| 15013 | 299 |
|
| 20485 | 300 |
lemma nonzero_number_of_Min: |
301 |
"~ iszero ((number_of Min)::'a::number_ring)" |
|
302 |
unfolding iszero_def numeral_m1_eq_minus_1 by simp |
|
| 15013 | 303 |
|
304 |
||
| 20485 | 305 |
subsection {* Comparisons, for Ordered Rings *}
|
| 15013 | 306 |
|
| 20485 | 307 |
lemma double_eq_0_iff: |
308 |
"(a + a = 0) = (a = (0::'a::ordered_idom))" |
|
| 15013 | 309 |
proof - |
| 20485 | 310 |
have "a + a = (1 + 1) * a" unfolding left_distrib by simp |
| 15013 | 311 |
with zero_less_two [where 'a = 'a] |
312 |
show ?thesis by force |
|
313 |
qed |
|
314 |
||
315 |
lemma le_imp_0_less: |
|
| 20485 | 316 |
assumes le: "0 \<le> z" |
317 |
shows "(0::int) < 1 + z" |
|
| 15013 | 318 |
proof - |
319 |
have "0 \<le> z" . |
|
320 |
also have "... < z + 1" by (rule less_add_one) |
|
321 |
also have "... = 1 + z" by (simp add: add_ac) |
|
322 |
finally show "0 < 1 + z" . |
|
323 |
qed |
|
324 |
||
| 20485 | 325 |
lemma odd_nonzero: |
326 |
"1 + z + z \<noteq> (0::int)"; |
|
| 15013 | 327 |
proof (cases z rule: int_cases) |
328 |
case (nonneg n) |
|
329 |
have le: "0 \<le> z+z" by (simp add: nonneg add_increasing) |
|
330 |
thus ?thesis using le_imp_0_less [OF le] |
|
331 |
by (auto simp add: add_assoc) |
|
332 |
next |
|
333 |
case (neg n) |
|
334 |
show ?thesis |
|
335 |
proof |
|
336 |
assume eq: "1 + z + z = 0" |
|
337 |
have "0 < 1 + (int n + int n)" |
|
338 |
by (simp add: le_imp_0_less add_increasing) |
|
339 |
also have "... = - (1 + z + z)" |
|
340 |
by (simp add: neg add_assoc [symmetric]) |
|
341 |
also have "... = 0" by (simp add: eq) |
|
342 |
finally have "0<0" .. |
|
343 |
thus False by blast |
|
344 |
qed |
|
345 |
qed |
|
346 |
||
| 20485 | 347 |
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
|
| 15013 | 348 |
|
| 20485 | 349 |
lemma Ints_odd_nonzero: |
350 |
assumes in_Ints: "a \<in> Ints" |
|
351 |
shows "1 + a + a \<noteq> (0::'a::ordered_idom)" |
|
352 |
proof - |
|
353 |
from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] . |
|
| 15013 | 354 |
then obtain z where a: "a = of_int z" .. |
355 |
show ?thesis |
|
356 |
proof |
|
357 |
assume eq: "1 + a + a = 0" |
|
358 |
hence "of_int (1 + z + z) = (of_int 0 :: 'a)" by (simp add: a) |
|
359 |
hence "1 + z + z = 0" by (simp only: of_int_eq_iff) |
|
360 |
with odd_nonzero show False by blast |
|
361 |
qed |
|
362 |
qed |
|
363 |
||
| 20485 | 364 |
lemma Ints_number_of: |
365 |
"(number_of w :: 'a::number_ring) \<in> Ints" |
|
366 |
unfolding number_of_eq Ints_def by simp |
|
| 15013 | 367 |
|
368 |
lemma iszero_number_of_BIT: |
|
| 20485 | 369 |
"iszero (number_of (w BIT x)::'a) = |
370 |
(x = B0 \<and> iszero (number_of w::'a::{ordered_idom,number_ring}))"
|
|
371 |
by (simp add: iszero_def number_of_eq numeral_simps double_eq_0_iff |
|
372 |
Ints_odd_nonzero Ints_def split: bit.split) |
|
| 15013 | 373 |
|
374 |
lemma iszero_number_of_0: |
|
| 20485 | 375 |
"iszero (number_of (w BIT B0) :: 'a::{ordered_idom,number_ring}) =
|
376 |
iszero (number_of w :: 'a)" |
|
377 |
by (simp only: iszero_number_of_BIT simp_thms) |
|
| 15013 | 378 |
|
379 |
lemma iszero_number_of_1: |
|
| 20485 | 380 |
"~ iszero (number_of (w BIT B1)::'a::{ordered_idom,number_ring})"
|
381 |
by (simp add: iszero_number_of_BIT) |
|
| 15013 | 382 |
|
383 |
||
| 20485 | 384 |
subsection {* The Less-Than Relation *}
|
| 15013 | 385 |
|
386 |
lemma less_number_of_eq_neg: |
|
| 20485 | 387 |
"((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
|
388 |
= neg (number_of (x + uminus y) :: 'a)" |
|
| 15013 | 389 |
apply (subst less_iff_diff_less_0) |
390 |
apply (simp add: neg_def diff_minus number_of_add number_of_minus) |
|
391 |
done |
|
392 |
||
| 20485 | 393 |
text {*
|
394 |
If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
|
|
395 |
@{term Numeral0} IS @{term "number_of Pls"}
|
|
396 |
*} |
|
397 |
||
| 15013 | 398 |
lemma not_neg_number_of_Pls: |
| 20485 | 399 |
"~ neg (number_of Pls ::'a::{ordered_idom,number_ring})"
|
400 |
by (simp add: neg_def numeral_0_eq_0) |
|
| 15013 | 401 |
|
402 |
lemma neg_number_of_Min: |
|
| 20485 | 403 |
"neg (number_of Min ::'a::{ordered_idom,number_ring})"
|
404 |
by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1) |
|
| 15013 | 405 |
|
| 20485 | 406 |
lemma double_less_0_iff: |
407 |
"(a + a < 0) = (a < (0::'a::ordered_idom))" |
|
| 15013 | 408 |
proof - |
409 |
have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib) |
|
410 |
also have "... = (a < 0)" |
|
411 |
by (simp add: mult_less_0_iff zero_less_two |
|
412 |
order_less_not_sym [OF zero_less_two]) |
|
413 |
finally show ?thesis . |
|
414 |
qed |
|
415 |
||
| 20485 | 416 |
lemma odd_less_0: |
417 |
"(1 + z + z < 0) = (z < (0::int))"; |
|
| 15013 | 418 |
proof (cases z rule: int_cases) |
419 |
case (nonneg n) |
|
420 |
thus ?thesis by (simp add: linorder_not_less add_assoc add_increasing |
|
421 |
le_imp_0_less [THEN order_less_imp_le]) |
|
422 |
next |
|
423 |
case (neg n) |
|
424 |
thus ?thesis by (simp del: int_Suc |
|
| 20485 | 425 |
add: int_Suc0_eq_1 [symmetric] zadd_int compare_rls) |
| 15013 | 426 |
qed |
427 |
||
| 20485 | 428 |
text {* The premise involving @{term Ints} prevents @{term "a = 1/2"}. *}
|
429 |
||
| 15013 | 430 |
lemma Ints_odd_less_0: |
| 20485 | 431 |
assumes in_Ints: "a \<in> Ints" |
432 |
shows "(1 + a + a < 0) = (a < (0::'a::ordered_idom))"; |
|
433 |
proof - |
|
434 |
from in_Ints have "a \<in> range of_int" unfolding Ints_def [symmetric] . |
|
| 15013 | 435 |
then obtain z where a: "a = of_int z" .. |
436 |
hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))" |
|
437 |
by (simp add: a) |
|
438 |
also have "... = (z < 0)" by (simp only: of_int_less_iff odd_less_0) |
|
439 |
also have "... = (a < 0)" by (simp add: a) |
|
440 |
finally show ?thesis . |
|
441 |
qed |
|
442 |
||
443 |
lemma neg_number_of_BIT: |
|
| 20485 | 444 |
"neg (number_of (w BIT x)::'a) = |
445 |
neg (number_of w :: 'a::{ordered_idom,number_ring})"
|
|
446 |
by (simp add: neg_def number_of_eq numeral_simps double_less_0_iff |
|
447 |
Ints_odd_less_0 Ints_def split: bit.split) |
|
| 15013 | 448 |
|
| 20596 | 449 |
|
| 20485 | 450 |
text {* Less-Than or Equals *}
|
451 |
||
452 |
text {* Reduces @{term "a\<le>b"} to @{term "~ (b<a)"} for ALL numerals. *}
|
|
| 15013 | 453 |
|
454 |
lemmas le_number_of_eq_not_less = |
|
| 20485 | 455 |
linorder_not_less [of "number_of w" "number_of v", symmetric, |
456 |
standard] |
|
| 15013 | 457 |
|
458 |
lemma le_number_of_eq: |
|
459 |
"((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
|
|
| 20485 | 460 |
= (~ (neg (number_of (y + uminus x) :: 'a)))" |
| 15013 | 461 |
by (simp add: le_number_of_eq_not_less less_number_of_eq_neg) |
462 |
||
463 |
||
| 20485 | 464 |
text {* Absolute value (@{term abs}) *}
|
| 15013 | 465 |
|
466 |
lemma abs_number_of: |
|
| 20485 | 467 |
"abs(number_of x::'a::{ordered_idom,number_ring}) =
|
468 |
(if number_of x < (0::'a) then -number_of x else number_of x)" |
|
469 |
by (simp add: abs_if) |
|
| 15013 | 470 |
|
471 |
||
| 20485 | 472 |
text {* Re-orientation of the equation nnn=x *}
|
| 15013 | 473 |
|
| 20485 | 474 |
lemma number_of_reorient: |
475 |
"(number_of w = x) = (x = number_of w)" |
|
476 |
by auto |
|
| 15013 | 477 |
|
478 |
||
| 20485 | 479 |
subsection {* Simplification of arithmetic operations on integer constants. *}
|
| 15013 | 480 |
|
| 20900 | 481 |
lemmas arith_extra_simps [standard] = |
| 20485 | 482 |
number_of_add [symmetric] |
483 |
number_of_minus [symmetric] numeral_m1_eq_minus_1 [symmetric] |
|
484 |
number_of_mult [symmetric] |
|
485 |
diff_number_of_eq abs_number_of |
|
486 |
||
487 |
text {*
|
|
488 |
For making a minimal simpset, one must include these default simprules. |
|
489 |
Also include @{text simp_thms}.
|
|
490 |
*} |
|
| 15013 | 491 |
|
| 20485 | 492 |
lemmas arith_simps = |
493 |
bit.distinct |
|
494 |
Pls_0_eq Min_1_eq |
|
495 |
pred_Pls pred_Min pred_1 pred_0 |
|
496 |
succ_Pls succ_Min succ_1 succ_0 |
|
497 |
add_Pls add_Min add_BIT_0 add_BIT_10 add_BIT_11 |
|
498 |
minus_Pls minus_Min minus_1 minus_0 |
|
499 |
mult_Pls mult_Min mult_num1 mult_num0 |
|
500 |
add_Pls_right add_Min_right |
|
501 |
abs_zero abs_one arith_extra_simps |
|
| 15013 | 502 |
|
| 20485 | 503 |
text {* Simplification of relational operations *}
|
| 15013 | 504 |
|
| 20485 | 505 |
lemmas rel_simps = |
506 |
eq_number_of_eq iszero_number_of_Pls nonzero_number_of_Min |
|
507 |
iszero_number_of_0 iszero_number_of_1 |
|
508 |
less_number_of_eq_neg |
|
509 |
not_neg_number_of_Pls not_neg_0 not_neg_1 not_iszero_1 |
|
510 |
neg_number_of_Min neg_number_of_BIT |
|
511 |
le_number_of_eq |
|
512 |
||
513 |
declare arith_extra_simps [simp] |
|
514 |
declare rel_simps [simp] |
|
| 15013 | 515 |
|
516 |
||
| 20485 | 517 |
subsection {* Simplification of arithmetic when nested to the right. *}
|
| 15013 | 518 |
|
519 |
lemma add_number_of_left [simp]: |
|
| 20485 | 520 |
"number_of v + (number_of w + z) = |
521 |
(number_of(v + w) + z::'a::number_ring)" |
|
522 |
by (simp add: add_assoc [symmetric]) |
|
| 15013 | 523 |
|
524 |
lemma mult_number_of_left [simp]: |
|
| 20485 | 525 |
"number_of v * (number_of w * z) = |
526 |
(number_of(v * w) * z::'a::number_ring)" |
|
527 |
by (simp add: mult_assoc [symmetric]) |
|
| 15013 | 528 |
|
529 |
lemma add_number_of_diff1: |
|
| 20485 | 530 |
"number_of v + (number_of w - c) = |
531 |
number_of(v + w) - (c::'a::number_ring)" |
|
532 |
by (simp add: diff_minus add_number_of_left) |
|
| 15013 | 533 |
|
| 20485 | 534 |
lemma add_number_of_diff2 [simp]: |
535 |
"number_of v + (c - number_of w) = |
|
536 |
number_of (v + uminus w) + (c::'a::number_ring)" |
|
| 15013 | 537 |
apply (subst diff_number_of_eq [symmetric]) |
538 |
apply (simp only: compare_rls) |
|
539 |
done |
|
540 |
||
| 19380 | 541 |
|
| 20500 | 542 |
hide (open) const Pls Min B0 B1 succ pred |
| 19380 | 543 |
|
| 15013 | 544 |
end |