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