| author | berghofe | 
| Thu, 10 Oct 2002 14:19:17 +0200 | |
| changeset 13636 | fdf7e9388be7 | 
| parent 13491 | ddf6ae639f21 | 
| child 14272 | 5efbb548107d | 
| permissions | -rw-r--r-- | 
| 1632 | 1 | (* Title: HOL/Integ/Bin.thy | 
| 6910 | 2 | ID: $Id$ | 
| 1632 | 3 | Authors: Lawrence C Paulson, Cambridge University Computer Laboratory | 
| 4 | David Spelt, University of Twente | |
| 5 | Copyright 1994 University of Cambridge | |
| 6 | Copyright 1996 University of Twente | |
| 7 | ||
| 8 | Arithmetic on binary integers. | |
| 9 | ||
| 5512 | 10 | The sign Pls stands for an infinite string of leading F's. | 
| 11 | The sign Min stands for an infinite string of leading T's. | |
| 1632 | 12 | |
| 13 | A number can have multiple representations, namely leading F's with sign | |
| 5512 | 14 | Pls and leading T's with sign Min. See ZF/ex/twos-compl.ML/int_of_binary | 
| 2988 
d38f330e58b3
Renamed sign constructors to eliminate clash with the Plus infix of Sum.thy
 paulson parents: 
1632diff
changeset | 15 | for the numerical interpretation. | 
| 1632 | 16 | |
| 17 | The representation expects that (m mod 2) is 0 or 1, even if m is negative; | |
| 11868 
56db9f3a6b3e
Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
 paulson parents: 
9207diff
changeset | 18 | For instance, -5 div 2 = -3 and -5 mod 2 = 1; thus -5 = (-3)*2 + 1 | 
| 1632 | 19 | *) | 
| 20 | ||
| 6910 | 21 | Bin = Int + Numeral + | 
| 1632 | 22 | |
| 23 | consts | |
| 5512 | 24 | NCons :: [bin,bool]=>bin | 
| 1632 | 25 | bin_succ :: bin=>bin | 
| 26 | bin_pred :: bin=>bin | |
| 27 | bin_minus :: bin=>bin | |
| 28 | bin_add,bin_mult :: [bin,bin]=>bin | |
| 29 | ||
| 5512 | 30 | (*NCons inserts a bit, suppressing leading 0s and 1s*) | 
| 5184 | 31 | primrec | 
| 13491 | 32 | NCons_Pls "NCons bin.Pls b = (if b then (bin.Pls BIT b) else bin.Pls)" | 
| 33 | NCons_Min "NCons bin.Min b = (if b then bin.Min else (bin.Min BIT b))" | |
| 5540 | 34 | NCons_BIT "NCons (w BIT x) b = (w BIT x) BIT b" | 
| 6910 | 35 | |
| 36 | instance | |
| 6988 | 37 | int :: number | 
| 38 | ||
| 11868 
56db9f3a6b3e
Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
 paulson parents: 
9207diff
changeset | 39 | primrec (*the type constraint is essential!*) | 
| 13491 | 40 | number_of_Pls "number_of bin.Pls = 0" | 
| 41 | number_of_Min "number_of bin.Min = - (1::int)" | |
| 11868 
56db9f3a6b3e
Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
 paulson parents: 
9207diff
changeset | 42 | number_of_BIT "number_of(w BIT x) = (if x then 1 else 0) + | 
| 
56db9f3a6b3e
Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
 paulson parents: 
9207diff
changeset | 43 | (number_of w) + (number_of w)" | 
| 1632 | 44 | |
| 5184 | 45 | primrec | 
| 13491 | 46 | bin_succ_Pls "bin_succ bin.Pls = bin.Pls BIT True" | 
| 47 | bin_succ_Min "bin_succ bin.Min = bin.Pls" | |
| 5546 | 48 | bin_succ_BIT "bin_succ(w BIT x) = | 
| 49 | (if x then bin_succ w BIT False | |
| 50 | else NCons w True)" | |
| 1632 | 51 | |
| 5184 | 52 | primrec | 
| 13491 | 53 | bin_pred_Pls "bin_pred bin.Pls = bin.Min" | 
| 54 | bin_pred_Min "bin_pred bin.Min = bin.Min BIT False" | |
| 5546 | 55 | bin_pred_BIT "bin_pred(w BIT x) = | 
| 56 | (if x then NCons w False | |
| 57 | else (bin_pred w) BIT True)" | |
| 1632 | 58 | |
| 5184 | 59 | primrec | 
| 13491 | 60 | bin_minus_Pls "bin_minus bin.Pls = bin.Pls" | 
| 61 | bin_minus_Min "bin_minus bin.Min = bin.Pls BIT True" | |
| 5546 | 62 | bin_minus_BIT "bin_minus(w BIT x) = | 
| 63 | (if x then bin_pred (NCons (bin_minus w) False) | |
| 64 | else bin_minus w BIT False)" | |
| 1632 | 65 | |
| 5184 | 66 | primrec | 
| 13491 | 67 | bin_add_Pls "bin_add bin.Pls w = w" | 
| 68 | bin_add_Min "bin_add bin.Min w = bin_pred w" | |
| 9207 | 69 | bin_add_BIT | 
| 70 | "bin_add (v BIT x) w = | |
| 71 | (case w of Pls => v BIT x | |
| 72 | | Min => bin_pred (v BIT x) | |
| 73 | | (w BIT y) => | |
| 74 | NCons (bin_add v (if (x & y) then bin_succ w else w)) | |
| 75 | (x~=y))" | |
| 1632 | 76 | |
| 5184 | 77 | primrec | 
| 13491 | 78 | bin_mult_Pls "bin_mult bin.Pls w = bin.Pls" | 
| 79 | bin_mult_Min "bin_mult bin.Min w = bin_minus w" | |
| 5546 | 80 | bin_mult_BIT "bin_mult (v BIT x) w = | 
| 81 | (if x then (bin_add (NCons (bin_mult v w) False) w) | |
| 82 | else (NCons (bin_mult v w) False))" | |
| 5491 | 83 | |
| 1632 | 84 | end |