author | wenzelm |
Fri, 18 Feb 2022 18:58:49 +0100 | |
changeset 75100 | 6eff5c260381 |
parent 71546 | 4dd5dadfc87d |
child 76213 | e44d86131648 |
permissions | -rw-r--r-- |
23146 | 1 |
(* Title: ZF/Bin.thy |
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
3 |
Copyright 1994 University of Cambridge |
|
4 |
||
5 |
The sign Pls stands for an infinite string of leading 0's. |
|
6 |
The sign Min stands for an infinite string of leading 1's. |
|
7 |
||
8 |
A number can have multiple representations, namely leading 0's with sign |
|
9 |
Pls and leading 1's with sign Min. See twos-compl.ML/int_of_binary for |
|
10 |
the numerical interpretation. |
|
11 |
||
12 |
The representation expects that (m mod 2) is 0 or 1, even if m is negative; |
|
13 |
For instance, ~5 div 2 = ~3 and ~5 mod 2 = 1; thus ~5 = (~3)*2 + 1 |
|
14 |
*) |
|
15 |
||
60770 | 16 |
section\<open>Arithmetic on Binary Integers\<close> |
23146 | 17 |
|
18 |
theory Bin |
|
68490
eb53f944c8cd
simplified ZF theory names (in contrast to 6a0801279f4c): session-qualification already achieves disjointness;
wenzelm
parents:
68233
diff
changeset
|
19 |
imports Int Datatype |
23146 | 20 |
begin |
21 |
||
22 |
consts bin :: i |
|
23 |
datatype |
|
24 |
"bin" = Pls |
|
25 |
| Min |
|
69587 | 26 |
| Bit ("w \<in> bin", "b \<in> bool") (infixl \<open>BIT\<close> 90) |
23146 | 27 |
|
28 |
consts |
|
29 |
integ_of :: "i=>i" |
|
30 |
NCons :: "[i,i]=>i" |
|
31 |
bin_succ :: "i=>i" |
|
32 |
bin_pred :: "i=>i" |
|
33 |
bin_minus :: "i=>i" |
|
34 |
bin_adder :: "i=>i" |
|
35 |
bin_mult :: "[i,i]=>i" |
|
36 |
||
37 |
primrec |
|
38 |
integ_of_Pls: "integ_of (Pls) = $# 0" |
|
39 |
integ_of_Min: "integ_of (Min) = $-($#1)" |
|
40 |
integ_of_BIT: "integ_of (w BIT b) = $#b $+ integ_of(w) $+ integ_of(w)" |
|
41 |
||
42 |
(** recall that cond(1,b,c)=b and cond(0,b,c)=0 **) |
|
43 |
||
44 |
primrec (*NCons adds a bit, suppressing leading 0s and 1s*) |
|
45 |
NCons_Pls: "NCons (Pls,b) = cond(b,Pls BIT b,Pls)" |
|
46 |
NCons_Min: "NCons (Min,b) = cond(b,Min,Min BIT b)" |
|
47 |
NCons_BIT: "NCons (w BIT c,b) = w BIT c BIT b" |
|
48 |
||
49 |
primrec (*successor. If a BIT, can change a 0 to a 1 without recursion.*) |
|
50 |
bin_succ_Pls: "bin_succ (Pls) = Pls BIT 1" |
|
51 |
bin_succ_Min: "bin_succ (Min) = Pls" |
|
52 |
bin_succ_BIT: "bin_succ (w BIT b) = cond(b, bin_succ(w) BIT 0, NCons(w,1))" |
|
53 |
||
54 |
primrec (*predecessor*) |
|
55 |
bin_pred_Pls: "bin_pred (Pls) = Min" |
|
56 |
bin_pred_Min: "bin_pred (Min) = Min BIT 0" |
|
57 |
bin_pred_BIT: "bin_pred (w BIT b) = cond(b, NCons(w,0), bin_pred(w) BIT 1)" |
|
58 |
||
59 |
primrec (*unary negation*) |
|
60 |
bin_minus_Pls: |
|
61 |
"bin_minus (Pls) = Pls" |
|
62 |
bin_minus_Min: |
|
63 |
"bin_minus (Min) = Pls BIT 1" |
|
64 |
bin_minus_BIT: |
|
65 |
"bin_minus (w BIT b) = cond(b, bin_pred(NCons(bin_minus(w),0)), |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
26190
diff
changeset
|
66 |
bin_minus(w) BIT 0)" |
23146 | 67 |
|
68 |
primrec (*sum*) |
|
69 |
bin_adder_Pls: |
|
46820 | 70 |
"bin_adder (Pls) = (\<lambda>w\<in>bin. w)" |
23146 | 71 |
bin_adder_Min: |
46820 | 72 |
"bin_adder (Min) = (\<lambda>w\<in>bin. bin_pred(w))" |
23146 | 73 |
bin_adder_BIT: |
46820 | 74 |
"bin_adder (v BIT x) = |
75 |
(\<lambda>w\<in>bin. |
|
76 |
bin_case (v BIT x, bin_pred(v BIT x), |
|
77 |
%w y. NCons(bin_adder (v) ` cond(x and y, bin_succ(w), w), |
|
23146 | 78 |
x xor y), |
79 |
w))" |
|
80 |
||
81 |
(*The bin_case above replaces the following mutually recursive function: |
|
82 |
primrec |
|
83 |
"adding (v,x,Pls) = v BIT x" |
|
84 |
"adding (v,x,Min) = bin_pred(v BIT x)" |
|
46820 | 85 |
"adding (v,x,w BIT y) = NCons(bin_adder (v, cond(x and y, bin_succ(w), w)), |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
26190
diff
changeset
|
86 |
x xor y)" |
23146 | 87 |
*) |
88 |
||
24893 | 89 |
definition |
90 |
bin_add :: "[i,i]=>i" where |
|
23146 | 91 |
"bin_add(v,w) == bin_adder(v)`w" |
92 |
||
93 |
||
94 |
primrec |
|
95 |
bin_mult_Pls: |
|
96 |
"bin_mult (Pls,w) = Pls" |
|
97 |
bin_mult_Min: |
|
98 |
"bin_mult (Min,w) = bin_minus(w)" |
|
99 |
bin_mult_BIT: |
|
100 |
"bin_mult (v BIT b,w) = cond(b, bin_add(NCons(bin_mult(v,w),0),w), |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
26190
diff
changeset
|
101 |
NCons(bin_mult(v,w),0))" |
23146 | 102 |
|
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
32960
diff
changeset
|
103 |
syntax |
71546 | 104 |
"_Int0" :: i (\<open>#' 0\<close>) |
105 |
"_Int1" :: i (\<open>#' 1\<close>) |
|
106 |
"_Int2" :: i (\<open>#' 2\<close>) |
|
107 |
"_Neg_Int1" :: i (\<open>#-' 1\<close>) |
|
108 |
"_Neg_Int2" :: i (\<open>#-' 2\<close>) |
|
58421 | 109 |
translations |
110 |
"#0" \<rightleftharpoons> "CONST integ_of(CONST Pls)" |
|
111 |
"#1" \<rightleftharpoons> "CONST integ_of(CONST Pls BIT 1)" |
|
112 |
"#2" \<rightleftharpoons> "CONST integ_of(CONST Pls BIT 1 BIT 0)" |
|
113 |
"#-1" \<rightleftharpoons> "CONST integ_of(CONST Min)" |
|
114 |
"#-2" \<rightleftharpoons> "CONST integ_of(CONST Min BIT 0)" |
|
115 |
||
116 |
syntax |
|
69587 | 117 |
"_Int" :: "num_token => i" (\<open>#_\<close> 1000) |
118 |
"_Neg_Int" :: "num_token => i" (\<open>#-_\<close> 1000) |
|
35112
ff6f60e6ab85
numeral syntax: clarify parse trees vs. actual terms;
wenzelm
parents:
32960
diff
changeset
|
119 |
|
69605 | 120 |
ML_file \<open>Tools/numeral_syntax.ML\<close> |
23146 | 121 |
|
122 |
||
123 |
declare bin.intros [simp,TC] |
|
124 |
||
125 |
lemma NCons_Pls_0: "NCons(Pls,0) = Pls" |
|
126 |
by simp |
|
127 |
||
128 |
lemma NCons_Pls_1: "NCons(Pls,1) = Pls BIT 1" |
|
129 |
by simp |
|
130 |
||
131 |
lemma NCons_Min_0: "NCons(Min,0) = Min BIT 0" |
|
132 |
by simp |
|
133 |
||
134 |
lemma NCons_Min_1: "NCons(Min,1) = Min" |
|
135 |
by simp |
|
136 |
||
137 |
lemma NCons_BIT: "NCons(w BIT x,b) = w BIT x BIT b" |
|
138 |
by (simp add: bin.case_eqns) |
|
139 |
||
46820 | 140 |
lemmas NCons_simps [simp] = |
23146 | 141 |
NCons_Pls_0 NCons_Pls_1 NCons_Min_0 NCons_Min_1 NCons_BIT |
142 |
||
143 |
||
144 |
||
145 |
(** Type checking **) |
|
146 |
||
46953 | 147 |
lemma integ_of_type [TC]: "w \<in> bin ==> integ_of(w) \<in> int" |
23146 | 148 |
apply (induct_tac "w") |
149 |
apply (simp_all add: bool_into_nat) |
|
150 |
done |
|
151 |
||
46953 | 152 |
lemma NCons_type [TC]: "[| w \<in> bin; b \<in> bool |] ==> NCons(w,b) \<in> bin" |
23146 | 153 |
by (induct_tac "w", auto) |
154 |
||
46953 | 155 |
lemma bin_succ_type [TC]: "w \<in> bin ==> bin_succ(w) \<in> bin" |
23146 | 156 |
by (induct_tac "w", auto) |
157 |
||
46953 | 158 |
lemma bin_pred_type [TC]: "w \<in> bin ==> bin_pred(w) \<in> bin" |
23146 | 159 |
by (induct_tac "w", auto) |
160 |
||
46953 | 161 |
lemma bin_minus_type [TC]: "w \<in> bin ==> bin_minus(w) \<in> bin" |
23146 | 162 |
by (induct_tac "w", auto) |
163 |
||
164 |
(*This proof is complicated by the mutual recursion*) |
|
71082 | 165 |
lemma bin_add_type [rule_format]: |
46953 | 166 |
"v \<in> bin ==> \<forall>w\<in>bin. bin_add(v,w) \<in> bin" |
23146 | 167 |
apply (unfold bin_add_def) |
168 |
apply (induct_tac "v") |
|
169 |
apply (rule_tac [3] ballI) |
|
170 |
apply (rename_tac [3] "w'") |
|
171 |
apply (induct_tac [3] "w'") |
|
172 |
apply (simp_all add: NCons_type) |
|
173 |
done |
|
174 |
||
71082 | 175 |
declare bin_add_type [TC] |
176 |
||
46953 | 177 |
lemma bin_mult_type [TC]: "[| v \<in> bin; w \<in> bin |] ==> bin_mult(v,w) \<in> bin" |
23146 | 178 |
by (induct_tac "v", auto) |
179 |
||
180 |
||
60770 | 181 |
subsubsection\<open>The Carry and Borrow Functions, |
69593 | 182 |
\<^term>\<open>bin_succ\<close> and \<^term>\<open>bin_pred\<close>\<close> |
23146 | 183 |
|
184 |
(*NCons preserves the integer value of its argument*) |
|
185 |
lemma integ_of_NCons [simp]: |
|
46953 | 186 |
"[| w \<in> bin; b \<in> bool |] ==> integ_of(NCons(w,b)) = integ_of(w BIT b)" |
23146 | 187 |
apply (erule bin.cases) |
46820 | 188 |
apply (auto elim!: boolE) |
23146 | 189 |
done |
190 |
||
191 |
lemma integ_of_succ [simp]: |
|
46953 | 192 |
"w \<in> bin ==> integ_of(bin_succ(w)) = $#1 $+ integ_of(w)" |
23146 | 193 |
apply (erule bin.induct) |
46820 | 194 |
apply (auto simp add: zadd_ac elim!: boolE) |
23146 | 195 |
done |
196 |
||
197 |
lemma integ_of_pred [simp]: |
|
46953 | 198 |
"w \<in> bin ==> integ_of(bin_pred(w)) = $- ($#1) $+ integ_of(w)" |
23146 | 199 |
apply (erule bin.induct) |
46820 | 200 |
apply (auto simp add: zadd_ac elim!: boolE) |
23146 | 201 |
done |
202 |
||
203 |
||
69593 | 204 |
subsubsection\<open>\<^term>\<open>bin_minus\<close>: Unary Negation of Binary Integers\<close> |
23146 | 205 |
|
46953 | 206 |
lemma integ_of_minus: "w \<in> bin ==> integ_of(bin_minus(w)) = $- integ_of(w)" |
23146 | 207 |
apply (erule bin.induct) |
46820 | 208 |
apply (auto simp add: zadd_ac zminus_zadd_distrib elim!: boolE) |
23146 | 209 |
done |
210 |
||
211 |
||
69593 | 212 |
subsubsection\<open>\<^term>\<open>bin_add\<close>: Binary Addition\<close> |
23146 | 213 |
|
46953 | 214 |
lemma bin_add_Pls [simp]: "w \<in> bin ==> bin_add(Pls,w) = w" |
23146 | 215 |
by (unfold bin_add_def, simp) |
216 |
||
46953 | 217 |
lemma bin_add_Pls_right: "w \<in> bin ==> bin_add(w,Pls) = w" |
23146 | 218 |
apply (unfold bin_add_def) |
219 |
apply (erule bin.induct, auto) |
|
220 |
done |
|
221 |
||
46953 | 222 |
lemma bin_add_Min [simp]: "w \<in> bin ==> bin_add(Min,w) = bin_pred(w)" |
23146 | 223 |
by (unfold bin_add_def, simp) |
224 |
||
46953 | 225 |
lemma bin_add_Min_right: "w \<in> bin ==> bin_add(w,Min) = bin_pred(w)" |
23146 | 226 |
apply (unfold bin_add_def) |
227 |
apply (erule bin.induct, auto) |
|
228 |
done |
|
229 |
||
230 |
lemma bin_add_BIT_Pls [simp]: "bin_add(v BIT x,Pls) = v BIT x" |
|
231 |
by (unfold bin_add_def, simp) |
|
232 |
||
233 |
lemma bin_add_BIT_Min [simp]: "bin_add(v BIT x,Min) = bin_pred(v BIT x)" |
|
234 |
by (unfold bin_add_def, simp) |
|
235 |
||
236 |
lemma bin_add_BIT_BIT [simp]: |
|
46953 | 237 |
"[| w \<in> bin; y \<in> bool |] |
46820 | 238 |
==> bin_add(v BIT x, w BIT y) = |
23146 | 239 |
NCons(bin_add(v, cond(x and y, bin_succ(w), w)), x xor y)" |
240 |
by (unfold bin_add_def, simp) |
|
241 |
||
242 |
lemma integ_of_add [rule_format]: |
|
46953 | 243 |
"v \<in> bin ==> |
46820 | 244 |
\<forall>w\<in>bin. integ_of(bin_add(v,w)) = integ_of(v) $+ integ_of(w)" |
23146 | 245 |
apply (erule bin.induct, simp, simp) |
246 |
apply (rule ballI) |
|
247 |
apply (induct_tac "wa") |
|
46820 | 248 |
apply (auto simp add: zadd_ac elim!: boolE) |
23146 | 249 |
done |
250 |
||
251 |
(*Subtraction*) |
|
46820 | 252 |
lemma diff_integ_of_eq: |
46953 | 253 |
"[| v \<in> bin; w \<in> bin |] |
23146 | 254 |
==> integ_of(v) $- integ_of(w) = integ_of(bin_add (v, bin_minus(w)))" |
255 |
apply (unfold zdiff_def) |
|
256 |
apply (simp add: integ_of_add integ_of_minus) |
|
257 |
done |
|
258 |
||
259 |
||
69593 | 260 |
subsubsection\<open>\<^term>\<open>bin_mult\<close>: Binary Multiplication\<close> |
23146 | 261 |
|
262 |
lemma integ_of_mult: |
|
46953 | 263 |
"[| v \<in> bin; w \<in> bin |] |
23146 | 264 |
==> integ_of(bin_mult(v,w)) = integ_of(v) $* integ_of(w)" |
265 |
apply (induct_tac "v", simp) |
|
266 |
apply (simp add: integ_of_minus) |
|
46820 | 267 |
apply (auto simp add: zadd_ac integ_of_add zadd_zmult_distrib elim!: boolE) |
23146 | 268 |
done |
269 |
||
270 |
||
60770 | 271 |
subsection\<open>Computations\<close> |
23146 | 272 |
|
273 |
(** extra rules for bin_succ, bin_pred **) |
|
274 |
||
275 |
lemma bin_succ_1: "bin_succ(w BIT 1) = bin_succ(w) BIT 0" |
|
276 |
by simp |
|
277 |
||
278 |
lemma bin_succ_0: "bin_succ(w BIT 0) = NCons(w,1)" |
|
279 |
by simp |
|
280 |
||
281 |
lemma bin_pred_1: "bin_pred(w BIT 1) = NCons(w,0)" |
|
282 |
by simp |
|
283 |
||
284 |
lemma bin_pred_0: "bin_pred(w BIT 0) = bin_pred(w) BIT 1" |
|
285 |
by simp |
|
286 |
||
287 |
(** extra rules for bin_minus **) |
|
288 |
||
289 |
lemma bin_minus_1: "bin_minus(w BIT 1) = bin_pred(NCons(bin_minus(w), 0))" |
|
290 |
by simp |
|
291 |
||
292 |
lemma bin_minus_0: "bin_minus(w BIT 0) = bin_minus(w) BIT 0" |
|
293 |
by simp |
|
294 |
||
295 |
(** extra rules for bin_add **) |
|
296 |
||
46953 | 297 |
lemma bin_add_BIT_11: "w \<in> bin ==> bin_add(v BIT 1, w BIT 1) = |
23146 | 298 |
NCons(bin_add(v, bin_succ(w)), 0)" |
299 |
by simp |
|
300 |
||
46953 | 301 |
lemma bin_add_BIT_10: "w \<in> bin ==> bin_add(v BIT 1, w BIT 0) = |
23146 | 302 |
NCons(bin_add(v,w), 1)" |
303 |
by simp |
|
304 |
||
46953 | 305 |
lemma bin_add_BIT_0: "[| w \<in> bin; y \<in> bool |] |
23146 | 306 |
==> bin_add(v BIT 0, w BIT y) = NCons(bin_add(v,w), y)" |
307 |
by simp |
|
308 |
||
309 |
(** extra rules for bin_mult **) |
|
310 |
||
311 |
lemma bin_mult_1: "bin_mult(v BIT 1, w) = bin_add(NCons(bin_mult(v,w),0), w)" |
|
312 |
by simp |
|
313 |
||
314 |
lemma bin_mult_0: "bin_mult(v BIT 0, w) = NCons(bin_mult(v,w),0)" |
|
315 |
by simp |
|
316 |
||
317 |
||
318 |
(** Simplification rules with integer constants **) |
|
319 |
||
320 |
lemma int_of_0: "$#0 = #0" |
|
321 |
by simp |
|
322 |
||
323 |
lemma int_of_succ: "$# succ(n) = #1 $+ $#n" |
|
324 |
by (simp add: int_of_add [symmetric] natify_succ) |
|
325 |
||
326 |
lemma zminus_0 [simp]: "$- #0 = #0" |
|
327 |
by simp |
|
328 |
||
329 |
lemma zadd_0_intify [simp]: "#0 $+ z = intify(z)" |
|
330 |
by simp |
|
331 |
||
332 |
lemma zadd_0_right_intify [simp]: "z $+ #0 = intify(z)" |
|
333 |
by simp |
|
334 |
||
335 |
lemma zmult_1_intify [simp]: "#1 $* z = intify(z)" |
|
336 |
by simp |
|
337 |
||
338 |
lemma zmult_1_right_intify [simp]: "z $* #1 = intify(z)" |
|
339 |
by (subst zmult_commute, simp) |
|
340 |
||
341 |
lemma zmult_0 [simp]: "#0 $* z = #0" |
|
342 |
by simp |
|
343 |
||
344 |
lemma zmult_0_right [simp]: "z $* #0 = #0" |
|
345 |
by (subst zmult_commute, simp) |
|
346 |
||
347 |
lemma zmult_minus1 [simp]: "#-1 $* z = $-z" |
|
348 |
by (simp add: zcompare_rls) |
|
349 |
||
350 |
lemma zmult_minus1_right [simp]: "z $* #-1 = $-z" |
|
351 |
apply (subst zmult_commute) |
|
352 |
apply (rule zmult_minus1) |
|
353 |
done |
|
354 |
||
355 |
||
60770 | 356 |
subsection\<open>Simplification Rules for Comparison of Binary Numbers\<close> |
357 |
text\<open>Thanks to Norbert Voelker\<close> |
|
23146 | 358 |
|
359 |
(** Equals (=) **) |
|
360 |
||
46820 | 361 |
lemma eq_integ_of_eq: |
46953 | 362 |
"[| v \<in> bin; w \<in> bin |] |
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
363 |
==> ((integ_of(v)) = integ_of(w)) \<longleftrightarrow> |
23146 | 364 |
iszero (integ_of (bin_add (v, bin_minus(w))))" |
365 |
apply (unfold iszero_def) |
|
366 |
apply (simp add: zcompare_rls integ_of_add integ_of_minus) |
|
367 |
done |
|
368 |
||
369 |
lemma iszero_integ_of_Pls: "iszero (integ_of(Pls))" |
|
370 |
by (unfold iszero_def, simp) |
|
371 |
||
372 |
||
373 |
lemma nonzero_integ_of_Min: "~ iszero (integ_of(Min))" |
|
374 |
apply (unfold iszero_def) |
|
375 |
apply (simp add: zminus_equation) |
|
376 |
done |
|
377 |
||
46820 | 378 |
lemma iszero_integ_of_BIT: |
46953 | 379 |
"[| w \<in> bin; x \<in> bool |] |
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
380 |
==> iszero (integ_of (w BIT x)) \<longleftrightarrow> (x=0 & iszero (integ_of(w)))" |
23146 | 381 |
apply (unfold iszero_def, simp) |
46820 | 382 |
apply (subgoal_tac "integ_of (w) \<in> int") |
23146 | 383 |
apply typecheck |
384 |
apply (drule int_cases) |
|
385 |
apply (safe elim!: boolE) |
|
386 |
apply (simp_all (asm_lr) add: zcompare_rls zminus_zadd_distrib [symmetric] |
|
387 |
int_of_add [symmetric]) |
|
388 |
done |
|
389 |
||
390 |
lemma iszero_integ_of_0: |
|
46953 | 391 |
"w \<in> bin ==> iszero (integ_of (w BIT 0)) \<longleftrightarrow> iszero (integ_of(w))" |
46820 | 392 |
by (simp only: iszero_integ_of_BIT, blast) |
23146 | 393 |
|
46953 | 394 |
lemma iszero_integ_of_1: "w \<in> bin ==> ~ iszero (integ_of (w BIT 1))" |
23146 | 395 |
by (simp only: iszero_integ_of_BIT, blast) |
396 |
||
397 |
||
398 |
||
399 |
(** Less-than (<) **) |
|
400 |
||
46820 | 401 |
lemma less_integ_of_eq_neg: |
46953 | 402 |
"[| v \<in> bin; w \<in> bin |] |
46820 | 403 |
==> integ_of(v) $< integ_of(w) |
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
404 |
\<longleftrightarrow> znegative (integ_of (bin_add (v, bin_minus(w))))" |
23146 | 405 |
apply (unfold zless_def zdiff_def) |
406 |
apply (simp add: integ_of_minus integ_of_add) |
|
407 |
done |
|
408 |
||
409 |
lemma not_neg_integ_of_Pls: "~ znegative (integ_of(Pls))" |
|
410 |
by simp |
|
411 |
||
412 |
lemma neg_integ_of_Min: "znegative (integ_of(Min))" |
|
413 |
by simp |
|
414 |
||
415 |
lemma neg_integ_of_BIT: |
|
46953 | 416 |
"[| w \<in> bin; x \<in> bool |] |
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
417 |
==> znegative (integ_of (w BIT x)) \<longleftrightarrow> znegative (integ_of(w))" |
23146 | 418 |
apply simp |
46820 | 419 |
apply (subgoal_tac "integ_of (w) \<in> int") |
23146 | 420 |
apply typecheck |
421 |
apply (drule int_cases) |
|
422 |
apply (auto elim!: boolE simp add: int_of_add [symmetric] zcompare_rls) |
|
46820 | 423 |
apply (simp_all add: zminus_zadd_distrib [symmetric] zdiff_def |
23146 | 424 |
int_of_add [symmetric]) |
425 |
apply (subgoal_tac "$#1 $- $# succ (succ (n #+ n)) = $- $# succ (n #+ n) ") |
|
426 |
apply (simp add: zdiff_def) |
|
427 |
apply (simp add: equation_zminus int_of_diff [symmetric]) |
|
428 |
done |
|
429 |
||
430 |
(** Less-than-or-equals (<=) **) |
|
431 |
||
432 |
lemma le_integ_of_eq_not_less: |
|
61395 | 433 |
"(integ_of(x) $\<le> (integ_of(w))) \<longleftrightarrow> ~ (integ_of(w) $< (integ_of(x)))" |
23146 | 434 |
by (simp add: not_zless_iff_zle [THEN iff_sym]) |
435 |
||
436 |
||
437 |
(*Delete the original rewrites, with their clumsy conditional expressions*) |
|
46820 | 438 |
declare bin_succ_BIT [simp del] |
439 |
bin_pred_BIT [simp del] |
|
23146 | 440 |
bin_minus_BIT [simp del] |
441 |
NCons_Pls [simp del] |
|
442 |
NCons_Min [simp del] |
|
443 |
bin_adder_BIT [simp del] |
|
444 |
bin_mult_BIT [simp del] |
|
445 |
||
446 |
(*Hide the binary representation of integer constants*) |
|
447 |
declare integ_of_Pls [simp del] integ_of_Min [simp del] integ_of_BIT [simp del] |
|
448 |
||
449 |
||
450 |
lemmas bin_arith_extra_simps = |
|
46820 | 451 |
integ_of_add [symmetric] |
452 |
integ_of_minus [symmetric] |
|
453 |
integ_of_mult [symmetric] |
|
454 |
bin_succ_1 bin_succ_0 |
|
455 |
bin_pred_1 bin_pred_0 |
|
456 |
bin_minus_1 bin_minus_0 |
|
23146 | 457 |
bin_add_Pls_right bin_add_Min_right |
458 |
bin_add_BIT_0 bin_add_BIT_10 bin_add_BIT_11 |
|
459 |
diff_integ_of_eq |
|
460 |
bin_mult_1 bin_mult_0 NCons_simps |
|
461 |
||
462 |
||
463 |
(*For making a minimal simpset, one must include these default simprules |
|
464 |
of thy. Also include simp_thms, or at least (~False)=True*) |
|
465 |
lemmas bin_arith_simps = |
|
466 |
bin_pred_Pls bin_pred_Min |
|
467 |
bin_succ_Pls bin_succ_Min |
|
468 |
bin_add_Pls bin_add_Min |
|
469 |
bin_minus_Pls bin_minus_Min |
|
46820 | 470 |
bin_mult_Pls bin_mult_Min |
23146 | 471 |
bin_arith_extra_simps |
472 |
||
473 |
(*Simplification of relational operations*) |
|
474 |
lemmas bin_rel_simps = |
|
475 |
eq_integ_of_eq iszero_integ_of_Pls nonzero_integ_of_Min |
|
476 |
iszero_integ_of_0 iszero_integ_of_1 |
|
477 |
less_integ_of_eq_neg |
|
478 |
not_neg_integ_of_Pls neg_integ_of_Min neg_integ_of_BIT |
|
479 |
le_integ_of_eq_not_less |
|
480 |
||
481 |
declare bin_arith_simps [simp] |
|
482 |
declare bin_rel_simps [simp] |
|
483 |
||
484 |
||
485 |
(** Simplification of arithmetic when nested to the right **) |
|
486 |
||
487 |
lemma add_integ_of_left [simp]: |
|
46953 | 488 |
"[| v \<in> bin; w \<in> bin |] |
23146 | 489 |
==> integ_of(v) $+ (integ_of(w) $+ z) = (integ_of(bin_add(v,w)) $+ z)" |
490 |
by (simp add: zadd_assoc [symmetric]) |
|
491 |
||
492 |
lemma mult_integ_of_left [simp]: |
|
46953 | 493 |
"[| v \<in> bin; w \<in> bin |] |
23146 | 494 |
==> integ_of(v) $* (integ_of(w) $* z) = (integ_of(bin_mult(v,w)) $* z)" |
495 |
by (simp add: zmult_assoc [symmetric]) |
|
496 |
||
46820 | 497 |
lemma add_integ_of_diff1 [simp]: |
46953 | 498 |
"[| v \<in> bin; w \<in> bin |] |
23146 | 499 |
==> integ_of(v) $+ (integ_of(w) $- c) = integ_of(bin_add(v,w)) $- (c)" |
500 |
apply (unfold zdiff_def) |
|
501 |
apply (rule add_integ_of_left, auto) |
|
502 |
done |
|
503 |
||
504 |
lemma add_integ_of_diff2 [simp]: |
|
46953 | 505 |
"[| v \<in> bin; w \<in> bin |] |
46820 | 506 |
==> integ_of(v) $+ (c $- integ_of(w)) = |
23146 | 507 |
integ_of (bin_add (v, bin_minus(w))) $+ (c)" |
508 |
apply (subst diff_integ_of_eq [symmetric]) |
|
509 |
apply (simp_all add: zdiff_def zadd_ac) |
|
510 |
done |
|
511 |
||
512 |
||
513 |
(** More for integer constants **) |
|
514 |
||
515 |
declare int_of_0 [simp] int_of_succ [simp] |
|
516 |
||
517 |
lemma zdiff0 [simp]: "#0 $- x = $-x" |
|
518 |
by (simp add: zdiff_def) |
|
519 |
||
520 |
lemma zdiff0_right [simp]: "x $- #0 = intify(x)" |
|
521 |
by (simp add: zdiff_def) |
|
522 |
||
523 |
lemma zdiff_self [simp]: "x $- x = #0" |
|
524 |
by (simp add: zdiff_def) |
|
525 |
||
46953 | 526 |
lemma znegative_iff_zless_0: "k \<in> int ==> znegative(k) \<longleftrightarrow> k $< #0" |
23146 | 527 |
by (simp add: zless_def) |
528 |
||
46953 | 529 |
lemma zero_zless_imp_znegative_zminus: "[|#0 $< k; k \<in> int|] ==> znegative($-k)" |
23146 | 530 |
by (simp add: zless_def) |
531 |
||
61395 | 532 |
lemma zero_zle_int_of [simp]: "#0 $\<le> $# n" |
23146 | 533 |
by (simp add: not_zless_iff_zle [THEN iff_sym] znegative_iff_zless_0 [THEN iff_sym]) |
534 |
||
535 |
lemma nat_of_0 [simp]: "nat_of(#0) = 0" |
|
536 |
by (simp only: natify_0 int_of_0 [symmetric] nat_of_int_of) |
|
537 |
||
61395 | 538 |
lemma nat_le_int0_lemma: "[| z $\<le> $#0; z \<in> int |] ==> nat_of(z) = 0" |
23146 | 539 |
by (auto simp add: znegative_iff_zless_0 [THEN iff_sym] zle_def zneg_nat_of) |
540 |
||
61395 | 541 |
lemma nat_le_int0: "z $\<le> $#0 ==> nat_of(z) = 0" |
23146 | 542 |
apply (subgoal_tac "nat_of (intify (z)) = 0") |
543 |
apply (rule_tac [2] nat_le_int0_lemma, auto) |
|
544 |
done |
|
545 |
||
546 |
lemma int_of_eq_0_imp_natify_eq_0: "$# n = #0 ==> natify(n) = 0" |
|
547 |
by (rule not_znegative_imp_zero, auto) |
|
548 |
||
549 |
lemma nat_of_zminus_int_of: "nat_of($- $# n) = 0" |
|
550 |
by (simp add: nat_of_def int_of_def raw_nat_of zminus image_intrel_int) |
|
551 |
||
61395 | 552 |
lemma int_of_nat_of: "#0 $\<le> z ==> $# nat_of(z) = intify(z)" |
23146 | 553 |
apply (rule not_zneg_nat_of_intify) |
554 |
apply (simp add: znegative_iff_zless_0 not_zless_iff_zle) |
|
555 |
done |
|
556 |
||
557 |
declare int_of_nat_of [simp] nat_of_zminus_int_of [simp] |
|
558 |
||
61395 | 559 |
lemma int_of_nat_of_if: "$# nat_of(z) = (if #0 $\<le> z then intify(z) else #0)" |
23146 | 560 |
by (simp add: int_of_nat_of znegative_iff_zless_0 not_zle_iff_zless) |
561 |
||
46953 | 562 |
lemma zless_nat_iff_int_zless: "[| m \<in> nat; z \<in> int |] ==> (m < nat_of(z)) \<longleftrightarrow> ($#m $< z)" |
23146 | 563 |
apply (case_tac "znegative (z) ") |
564 |
apply (erule_tac [2] not_zneg_nat_of [THEN subst]) |
|
565 |
apply (auto dest: zless_trans dest!: zero_zle_int_of [THEN zle_zless_trans] |
|
566 |
simp add: znegative_iff_zless_0) |
|
567 |
done |
|
568 |
||
569 |
||
570 |
(** nat_of and zless **) |
|
571 |
||
46820 | 572 |
(*An alternative condition is @{term"$#0 \<subseteq> w"} *) |
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
573 |
lemma zless_nat_conj_lemma: "$#0 $< z ==> (nat_of(w) < nat_of(z)) \<longleftrightarrow> (w $< z)" |
23146 | 574 |
apply (rule iff_trans) |
575 |
apply (rule zless_int_of [THEN iff_sym]) |
|
576 |
apply (auto simp add: int_of_nat_of_if simp del: zless_int_of) |
|
577 |
apply (auto elim: zless_asym simp add: not_zle_iff_zless) |
|
578 |
apply (blast intro: zless_zle_trans) |
|
579 |
done |
|
580 |
||
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
581 |
lemma zless_nat_conj: "(nat_of(w) < nat_of(z)) \<longleftrightarrow> ($#0 $< z & w $< z)" |
23146 | 582 |
apply (case_tac "$#0 $< z") |
583 |
apply (auto simp add: zless_nat_conj_lemma nat_le_int0 not_zless_iff_zle) |
|
584 |
done |
|
585 |
||
586 |
(*This simprule cannot be added unless we can find a way to make eq_integ_of_eq |
|
587 |
unconditional! |
|
588 |
[The condition "True" is a hack to prevent looping. |
|
589 |
Conditional rewrite rules are tried after unconditional ones, so a rule |
|
590 |
like eq_nat_number_of will be tried first to eliminate #mm=#nn.] |
|
591 |
lemma integ_of_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
592 |
"True ==> (integ_of(w) = x) \<longleftrightarrow> (x = integ_of(w))" |
23146 | 593 |
by auto |
594 |
*) |
|
595 |
||
596 |
lemma integ_of_minus_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
597 |
"(integ_of(w) = $- x) \<longleftrightarrow> ($- x = integ_of(w))" |
23146 | 598 |
by auto |
599 |
||
600 |
lemma integ_of_add_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
601 |
"(integ_of(w) = x $+ y) \<longleftrightarrow> (x $+ y = integ_of(w))" |
23146 | 602 |
by auto |
603 |
||
604 |
lemma integ_of_diff_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
605 |
"(integ_of(w) = x $- y) \<longleftrightarrow> (x $- y = integ_of(w))" |
23146 | 606 |
by auto |
607 |
||
608 |
lemma integ_of_mult_reorient [simp]: |
|
46821
ff6b0c1087f2
Using mathematical notation for <-> and cardinal arithmetic
paulson
parents:
46820
diff
changeset
|
609 |
"(integ_of(w) = x $* y) \<longleftrightarrow> (x $* y = integ_of(w))" |
23146 | 610 |
by auto |
611 |
||
58022 | 612 |
(** To simplify inequalities involving integer negation and literals, |
613 |
such as -x = #3 |
|
614 |
**) |
|
615 |
||
616 |
lemmas [simp] = |
|
617 |
zminus_equation [where y = "integ_of(w)"] |
|
618 |
equation_zminus [where x = "integ_of(w)"] |
|
619 |
for w |
|
620 |
||
621 |
lemmas [iff] = |
|
622 |
zminus_zless [where y = "integ_of(w)"] |
|
623 |
zless_zminus [where x = "integ_of(w)"] |
|
624 |
for w |
|
625 |
||
626 |
lemmas [iff] = |
|
627 |
zminus_zle [where y = "integ_of(w)"] |
|
628 |
zle_zminus [where x = "integ_of(w)"] |
|
629 |
for w |
|
630 |
||
631 |
lemmas [simp] = |
|
632 |
Let_def [where s = "integ_of(w)"] for w |
|
633 |
||
634 |
||
635 |
(*** Simprocs for numeric literals ***) |
|
636 |
||
637 |
(** Combining of literal coefficients in sums of products **) |
|
638 |
||
639 |
lemma zless_iff_zdiff_zless_0: "(x $< y) \<longleftrightarrow> (x$-y $< #0)" |
|
640 |
by (simp add: zcompare_rls) |
|
641 |
||
642 |
lemma eq_iff_zdiff_eq_0: "[| x \<in> int; y \<in> int |] ==> (x = y) \<longleftrightarrow> (x$-y = #0)" |
|
643 |
by (simp add: zcompare_rls) |
|
644 |
||
61395 | 645 |
lemma zle_iff_zdiff_zle_0: "(x $\<le> y) \<longleftrightarrow> (x$-y $\<le> #0)" |
58022 | 646 |
by (simp add: zcompare_rls) |
647 |
||
648 |
||
649 |
(** For combine_numerals **) |
|
650 |
||
651 |
lemma left_zadd_zmult_distrib: "i$*u $+ (j$*u $+ k) = (i$+j)$*u $+ k" |
|
652 |
by (simp add: zadd_zmult_distrib zadd_ac) |
|
653 |
||
654 |
||
655 |
(** For cancel_numerals **) |
|
656 |
||
657 |
lemma eq_add_iff1: "(i$*u $+ m = j$*u $+ n) \<longleftrightarrow> ((i$-j)$*u $+ m = intify(n))" |
|
658 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
|
659 |
apply (simp add: zcompare_rls) |
|
660 |
apply (simp add: zadd_ac) |
|
661 |
done |
|
662 |
||
663 |
lemma eq_add_iff2: "(i$*u $+ m = j$*u $+ n) \<longleftrightarrow> (intify(m) = (j$-i)$*u $+ n)" |
|
664 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
|
665 |
apply (simp add: zcompare_rls) |
|
666 |
apply (simp add: zadd_ac) |
|
667 |
done |
|
668 |
||
68233 | 669 |
context fixes n :: i |
670 |
begin |
|
671 |
||
672 |
lemmas rel_iff_rel_0_rls = |
|
673 |
zless_iff_zdiff_zless_0 [where y = "u $+ v"] |
|
674 |
eq_iff_zdiff_eq_0 [where y = "u $+ v"] |
|
675 |
zle_iff_zdiff_zle_0 [where y = "u $+ v"] |
|
676 |
zless_iff_zdiff_zless_0 [where y = n] |
|
677 |
eq_iff_zdiff_eq_0 [where y = n] |
|
678 |
zle_iff_zdiff_zle_0 [where y = n] |
|
679 |
for u v |
|
680 |
||
58022 | 681 |
lemma less_add_iff1: "(i$*u $+ m $< j$*u $+ n) \<longleftrightarrow> ((i$-j)$*u $+ m $< n)" |
682 |
apply (simp add: zdiff_def zadd_zmult_distrib zadd_ac rel_iff_rel_0_rls) |
|
683 |
done |
|
684 |
||
685 |
lemma less_add_iff2: "(i$*u $+ m $< j$*u $+ n) \<longleftrightarrow> (m $< (j$-i)$*u $+ n)" |
|
686 |
apply (simp add: zdiff_def zadd_zmult_distrib zadd_ac rel_iff_rel_0_rls) |
|
687 |
done |
|
688 |
||
68233 | 689 |
end |
690 |
||
61395 | 691 |
lemma le_add_iff1: "(i$*u $+ m $\<le> j$*u $+ n) \<longleftrightarrow> ((i$-j)$*u $+ m $\<le> n)" |
58022 | 692 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
693 |
apply (simp add: zcompare_rls) |
|
694 |
apply (simp add: zadd_ac) |
|
695 |
done |
|
696 |
||
61395 | 697 |
lemma le_add_iff2: "(i$*u $+ m $\<le> j$*u $+ n) \<longleftrightarrow> (m $\<le> (j$-i)$*u $+ n)" |
58022 | 698 |
apply (simp add: zdiff_def zadd_zmult_distrib) |
699 |
apply (simp add: zcompare_rls) |
|
700 |
apply (simp add: zadd_ac) |
|
701 |
done |
|
702 |
||
69605 | 703 |
ML_file \<open>int_arith.ML\<close> |
58022 | 704 |
|
60770 | 705 |
subsection \<open>examples:\<close> |
59748 | 706 |
|
61798 | 707 |
text \<open>\<open>combine_numerals_prod\<close> (products of separate literals)\<close> |
59748 | 708 |
lemma "#5 $* x $* #3 = y" apply simp oops |
709 |
||
61337 | 710 |
schematic_goal "y2 $+ ?x42 = y $+ y2" apply simp oops |
59748 | 711 |
|
712 |
lemma "oo : int ==> l $+ (l $+ #2) $+ oo = oo" apply simp oops |
|
713 |
||
714 |
lemma "#9$*x $+ y = x$*#23 $+ z" apply simp oops |
|
715 |
lemma "y $+ x = x $+ z" apply simp oops |
|
716 |
||
717 |
lemma "x : int ==> x $+ y $+ z = x $+ z" apply simp oops |
|
718 |
lemma "x : int ==> y $+ (z $+ x) = z $+ x" apply simp oops |
|
719 |
lemma "z : int ==> x $+ y $+ z = (z $+ y) $+ (x $+ w)" apply simp oops |
|
720 |
lemma "z : int ==> x$*y $+ z = (z $+ y) $+ (y$*x $+ w)" apply simp oops |
|
721 |
||
61395 | 722 |
lemma "#-3 $* x $+ y $\<le> x $* #2 $+ z" apply simp oops |
723 |
lemma "y $+ x $\<le> x $+ z" apply simp oops |
|
724 |
lemma "x $+ y $+ z $\<le> x $+ z" apply simp oops |
|
59748 | 725 |
|
726 |
lemma "y $+ (z $+ x) $< z $+ x" apply simp oops |
|
727 |
lemma "x $+ y $+ z $< (z $+ y) $+ (x $+ w)" apply simp oops |
|
728 |
lemma "x$*y $+ z $< (z $+ y) $+ (y$*x $+ w)" apply simp oops |
|
729 |
||
730 |
lemma "l $+ #2 $+ #2 $+ #2 $+ (l $+ #2) $+ (oo $+ #2) = uu" apply simp oops |
|
731 |
lemma "u : int ==> #2 $* u = u" apply simp oops |
|
732 |
lemma "(i $+ j $+ #12 $+ k) $- #15 = y" apply simp oops |
|
733 |
lemma "(i $+ j $+ #12 $+ k) $- #5 = y" apply simp oops |
|
734 |
||
735 |
lemma "y $- b $< b" apply simp oops |
|
736 |
lemma "y $- (#3 $* b $+ c) $< b $- #2 $* c" apply simp oops |
|
737 |
||
738 |
lemma "(#2 $* x $- (u $* v) $+ y) $- v $* #3 $* u = w" apply simp oops |
|
739 |
lemma "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u $* #4 = w" apply simp oops |
|
740 |
lemma "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u = w" apply simp oops |
|
741 |
lemma "u $* v $- (x $* u $* v $+ (u $* v) $* #4 $+ y) = w" apply simp oops |
|
742 |
||
743 |
lemma "(i $+ j $+ #12 $+ k) = u $+ #15 $+ y" apply simp oops |
|
744 |
lemma "(i $+ j $* #2 $+ #12 $+ k) = j $+ #5 $+ y" apply simp oops |
|
745 |
||
746 |
lemma "#2 $* y $+ #3 $* z $+ #6 $* w $+ #2 $* y $+ #3 $* z $+ #2 $* u = #2 $* y' $+ #3 $* z' $+ #6 $* w' $+ #2 $* y' $+ #3 $* z' $+ u $+ vv" apply simp oops |
|
747 |
||
748 |
lemma "a $+ $-(b$+c) $+ b = d" apply simp oops |
|
749 |
lemma "a $+ $-(b$+c) $- b = d" apply simp oops |
|
750 |
||
60770 | 751 |
text \<open>negative numerals\<close> |
59748 | 752 |
lemma "(i $+ j $+ #-2 $+ k) $- (u $+ #5 $+ y) = zz" apply simp oops |
753 |
lemma "(i $+ j $+ #-3 $+ k) $< u $+ #5 $+ y" apply simp oops |
|
754 |
lemma "(i $+ j $+ #3 $+ k) $< u $+ #-6 $+ y" apply simp oops |
|
755 |
lemma "(i $+ j $+ #-12 $+ k) $- #15 = y" apply simp oops |
|
756 |
lemma "(i $+ j $+ #12 $+ k) $- #-15 = y" apply simp oops |
|
757 |
lemma "(i $+ j $+ #-12 $+ k) $- #-15 = y" apply simp oops |
|
758 |
||
60770 | 759 |
text \<open>Multiplying separated numerals\<close> |
59748 | 760 |
lemma "#6 $* ($# x $* #2) = uu" apply simp oops |
761 |
lemma "#4 $* ($# x $* $# x) $* (#2 $* $# x) = uu" apply simp oops |
|
762 |
||
23146 | 763 |
end |