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