author | wenzelm |
Fri, 16 Jul 1999 22:25:07 +0200 | |
changeset 7025 | afbd8241797b |
parent 6920 | c912740c3545 |
child 7037 | 77d596a5ffae |
permissions | -rw-r--r-- |
5545 | 1 |
(* Title: HOL/ex/BinEx.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1998 University of Cambridge |
|
5199 | 5 |
|
5545 | 6 |
Examples of performing binary arithmetic by simplification |
7 |
||
8 |
Also a proof that binary arithmetic on normalized operands |
|
9 |
yields normalized results. |
|
10 |
*) |
|
11 |
||
12 |
set proof_timing; |
|
5199 | 13 |
|
6920 | 14 |
(*** Addition ***) |
5491 | 15 |
|
6909 | 16 |
Goal "(#13::int) + #19 = #32"; |
5199 | 17 |
by (Simp_tac 1); |
18 |
result(); |
|
19 |
||
6909 | 20 |
Goal "(#1234::int) + #5678 = #6912"; |
5199 | 21 |
by (Simp_tac 1); |
22 |
result(); |
|
23 |
||
6909 | 24 |
Goal "(#1359::int) + #-2468 = #-1109"; |
5199 | 25 |
by (Simp_tac 1); |
26 |
result(); |
|
27 |
||
6909 | 28 |
Goal "(#93746::int) + #-46375 = #47371"; |
5199 | 29 |
by (Simp_tac 1); |
30 |
result(); |
|
31 |
||
6920 | 32 |
(*** Negation ***) |
5491 | 33 |
|
6909 | 34 |
Goal "- (#65745::int) = #-65745"; |
5199 | 35 |
by (Simp_tac 1); |
36 |
result(); |
|
37 |
||
6909 | 38 |
Goal "- (#-54321::int) = #54321"; |
5199 | 39 |
by (Simp_tac 1); |
40 |
result(); |
|
41 |
||
5491 | 42 |
|
6920 | 43 |
(*** Multiplication ***) |
5491 | 44 |
|
6909 | 45 |
Goal "(#13::int) * #19 = #247"; |
5199 | 46 |
by (Simp_tac 1); |
47 |
result(); |
|
48 |
||
6909 | 49 |
Goal "(#-84::int) * #51 = #-4284"; |
5199 | 50 |
by (Simp_tac 1); |
51 |
result(); |
|
52 |
||
6909 | 53 |
Goal "(#255::int) * #255 = #65025"; |
5199 | 54 |
by (Simp_tac 1); |
55 |
result(); |
|
56 |
||
6909 | 57 |
Goal "(#1359::int) * #-2468 = #-3354012"; |
5199 | 58 |
by (Simp_tac 1); |
59 |
result(); |
|
60 |
||
6909 | 61 |
Goal "(#89::int) * #10 ~= #889"; |
5199 | 62 |
by (Simp_tac 1); |
63 |
result(); |
|
64 |
||
6909 | 65 |
Goal "(#13::int) < #18 - #4"; |
5199 | 66 |
by (Simp_tac 1); |
67 |
result(); |
|
68 |
||
6909 | 69 |
Goal "(#-345::int) < #-242 + #-100"; |
5199 | 70 |
by (Simp_tac 1); |
71 |
result(); |
|
72 |
||
6909 | 73 |
Goal "(#13557456::int) < #18678654"; |
5199 | 74 |
by (Simp_tac 1); |
75 |
result(); |
|
76 |
||
6909 | 77 |
Goal "(#999999::int) <= (#1000001 + #1)-#2"; |
5199 | 78 |
by (Simp_tac 1); |
79 |
result(); |
|
80 |
||
6909 | 81 |
Goal "(#1234567::int) <= #1234567"; |
5199 | 82 |
by (Simp_tac 1); |
83 |
result(); |
|
5545 | 84 |
|
85 |
||
6920 | 86 |
(*** Division and Remainder ***) |
87 |
||
88 |
Goal "(#10::int) div #3 = #3"; |
|
89 |
by (Simp_tac 1); |
|
90 |
result(); |
|
91 |
||
92 |
Goal "(#10::int) mod #3 = #1"; |
|
93 |
by (Simp_tac 1); |
|
94 |
result(); |
|
95 |
||
96 |
(** A negative divisor **) |
|
97 |
||
98 |
Goal "(#10::int) div #-3 = #-4"; |
|
99 |
by (Simp_tac 1); |
|
100 |
result(); |
|
101 |
||
102 |
Goal "(#10::int) mod #-3 = #-2"; |
|
103 |
by (Simp_tac 1); |
|
104 |
result(); |
|
105 |
||
106 |
(** A negative dividend |
|
107 |
[ The definition agrees with mathematical convention but not with |
|
108 |
the hardware of most computers ] |
|
109 |
**) |
|
110 |
||
111 |
Goal "(#-10::int) div #3 = #-4"; |
|
112 |
by (Simp_tac 1); |
|
113 |
result(); |
|
114 |
||
115 |
Goal "(#-10::int) mod #3 = #2"; |
|
116 |
by (Simp_tac 1); |
|
117 |
result(); |
|
118 |
||
119 |
(** A negative dividend AND divisor **) |
|
120 |
||
121 |
Goal "(#-10::int) div #-3 = #3"; |
|
122 |
by (Simp_tac 1); |
|
123 |
result(); |
|
124 |
||
125 |
Goal "(#-10::int) mod #-3 = #-1"; |
|
126 |
by (Simp_tac 1); |
|
127 |
result(); |
|
128 |
||
129 |
(** A few bigger examples **) |
|
130 |
||
131 |
Goal "(#8452::int) mod #3 = #1"; |
|
132 |
by (Simp_tac 1); |
|
133 |
result(); |
|
134 |
||
135 |
Goal "(#59485::int) div #434 = #137"; |
|
136 |
by (Simp_tac 1); |
|
137 |
result(); |
|
138 |
||
139 |
Goal "(#1000006::int) mod #10 = #6"; |
|
140 |
by (Simp_tac 1); |
|
141 |
result(); |
|
142 |
||
143 |
||
144 |
(*** Testing the cancellation of complementary terms ***) |
|
5601 | 145 |
|
6909 | 146 |
Goal "y + (x + -x) = (#0::int) + y"; |
5601 | 147 |
by (Simp_tac 1); |
148 |
result(); |
|
149 |
||
6909 | 150 |
Goal "y + (-x + (- y + x)) = (#0::int)"; |
5601 | 151 |
by (Simp_tac 1); |
152 |
result(); |
|
153 |
||
6909 | 154 |
Goal "-x + (y + (- y + x)) = (#0::int)"; |
5601 | 155 |
by (Simp_tac 1); |
156 |
result(); |
|
157 |
||
6909 | 158 |
Goal "x + (x + (- x + (- x + (- y + - z)))) = (#0::int) - y - z"; |
5601 | 159 |
by (Simp_tac 1); |
160 |
result(); |
|
161 |
||
6909 | 162 |
Goal "x + x - x - x - y - z = (#0::int) - y - z"; |
5601 | 163 |
by (Simp_tac 1); |
164 |
result(); |
|
165 |
||
6909 | 166 |
Goal "x + y + z - (x + z) = y - (#0::int)"; |
5601 | 167 |
by (Simp_tac 1); |
168 |
result(); |
|
169 |
||
6909 | 170 |
Goal "x+(y+(y+(y+ (-x + -x)))) = (#0::int) + y - x + y + y"; |
5601 | 171 |
by (Simp_tac 1); |
172 |
result(); |
|
173 |
||
6909 | 174 |
Goal "x+(y+(y+(y+ (-y + -x)))) = y + (#0::int) + y"; |
5601 | 175 |
by (Simp_tac 1); |
176 |
result(); |
|
177 |
||
6909 | 178 |
Goal "x + y - x + z - x - y - z + x < (#1::int)"; |
5601 | 179 |
by (Simp_tac 1); |
180 |
result(); |
|
181 |
||
182 |
||
5545 | 183 |
Addsimps normal.intrs; |
184 |
||
185 |
Goal "(w BIT b): normal ==> (w BIT b BIT c): normal"; |
|
186 |
by (case_tac "c" 1); |
|
187 |
by Auto_tac; |
|
188 |
qed "normal_BIT_I"; |
|
189 |
||
190 |
Addsimps [normal_BIT_I]; |
|
191 |
||
192 |
Goal "w BIT b: normal ==> w: normal"; |
|
193 |
by (etac normal.elim 1); |
|
194 |
by Auto_tac; |
|
195 |
qed "normal_BIT_D"; |
|
196 |
||
197 |
Goal "w : normal --> NCons w b : normal"; |
|
198 |
by (induct_tac "w" 1); |
|
199 |
by (auto_tac (claset(), simpset() addsimps [NCons_Pls, NCons_Min])); |
|
200 |
qed_spec_mp "NCons_normal"; |
|
201 |
||
202 |
Addsimps [NCons_normal]; |
|
203 |
||
204 |
Goal "NCons w True ~= Pls"; |
|
205 |
by (induct_tac "w" 1); |
|
206 |
by Auto_tac; |
|
207 |
qed "NCons_True"; |
|
208 |
||
209 |
Goal "NCons w False ~= Min"; |
|
210 |
by (induct_tac "w" 1); |
|
211 |
by Auto_tac; |
|
212 |
qed "NCons_False"; |
|
213 |
||
214 |
Goal "w: normal ==> bin_succ w : normal"; |
|
215 |
by (etac normal.induct 1); |
|
216 |
by (exhaust_tac "w" 4); |
|
217 |
by (auto_tac (claset(), simpset() addsimps [NCons_True, bin_succ_BIT])); |
|
218 |
qed "bin_succ_normal"; |
|
219 |
||
220 |
Goal "w: normal ==> bin_pred w : normal"; |
|
221 |
by (etac normal.induct 1); |
|
222 |
by (exhaust_tac "w" 3); |
|
223 |
by (auto_tac (claset(), simpset() addsimps [NCons_False, bin_pred_BIT])); |
|
224 |
qed "bin_pred_normal"; |
|
225 |
||
226 |
Addsimps [bin_succ_normal, bin_pred_normal]; |
|
227 |
||
228 |
Goal "w : normal --> (ALL z. z: normal --> bin_add w z : normal)"; |
|
229 |
by (induct_tac "w" 1); |
|
230 |
by (Simp_tac 1); |
|
231 |
by (Simp_tac 1); |
|
232 |
by (rtac impI 1); |
|
233 |
by (rtac allI 1); |
|
234 |
by (induct_tac "z" 1); |
|
235 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [bin_add_BIT]))); |
|
236 |
by (safe_tac (claset() addSDs [normal_BIT_D])); |
|
237 |
by (ALLGOALS Asm_simp_tac); |
|
238 |
qed_spec_mp "bin_add_normal"; |
|
239 |
||
6909 | 240 |
Goal "w: normal ==> (w = Pls) = (number_of w = (#0::int))"; |
5545 | 241 |
by (etac normal.induct 1); |
242 |
by Auto_tac; |
|
243 |
qed "normal_Pls_eq_0"; |
|
244 |
||
245 |
Goal "w : normal ==> bin_minus w : normal"; |
|
246 |
by (etac normal.induct 1); |
|
247 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [bin_minus_BIT]))); |
|
248 |
by (resolve_tac normal.intrs 1); |
|
249 |
by (assume_tac 1); |
|
250 |
by (asm_full_simp_tac (simpset() addsimps [normal_Pls_eq_0]) 1); |
|
251 |
by (asm_full_simp_tac |
|
6920 | 252 |
(simpset_of Int.thy |
253 |
addsimps [number_of_minus, iszero_def, |
|
254 |
read_instantiate [("y","int 0")] zminus_equation]) 1); |
|
255 |
by (etac not_sym 1); |
|
5545 | 256 |
qed "bin_minus_normal"; |
257 |
||
258 |
Goal "w : normal ==> z: normal --> bin_mult w z : normal"; |
|
259 |
by (etac normal.induct 1); |
|
5569
8c7e1190e789
Renaming of Integ/Integ.* to Integ/Int.*, and renaming of related constants
paulson
parents:
5545
diff
changeset
|
260 |
by (ALLGOALS |
8c7e1190e789
Renaming of Integ/Integ.* to Integ/Int.*, and renaming of related constants
paulson
parents:
5545
diff
changeset
|
261 |
(asm_simp_tac (simpset() addsimps [bin_minus_normal, bin_mult_BIT]))); |
5545 | 262 |
by (safe_tac (claset() addSDs [normal_BIT_D])); |
263 |
by (asm_simp_tac (simpset() addsimps [bin_add_normal]) 1); |
|
264 |
qed_spec_mp "bin_mult_normal"; |
|
265 |