| author | wenzelm | 
| Mon, 02 Jul 2018 16:43:06 +0200 | |
| changeset 68572 | c8bf6077a87d | 
| parent 68028 | 1f9f973eed2a | 
| child 69729 | 4591221824f6 | 
| permissions | -rw-r--r-- | 
| 28308 | 1 | (* Title: Pure/General/integer.ML | 
| 24633 | 2 | Author: Florian Haftmann, TU Muenchen | 
| 3 | ||
| 33002 | 4 | Auxiliary operations on (unbounded) integers. | 
| 24633 | 5 | *) | 
| 6 | ||
| 7 | signature INTEGER = | |
| 8 | sig | |
| 33029 | 9 | val min: int -> int -> int | 
| 10 | val max: int -> int -> int | |
| 33002 | 11 | val add: int -> int -> int | 
| 12 | val mult: int -> int -> int | |
| 13 | val sum: int list -> int | |
| 14 | val prod: int list -> int | |
| 24633 | 15 | val sign: int -> order | 
| 16 | val div_mod: int -> int -> int * int | |
| 17 | val square: int -> int | |
| 18 | val pow: int -> int -> int (* exponent -> base -> result *) | |
| 19 | val gcd: int -> int -> int | |
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 20 | val lcm: int -> int -> int | 
| 24633 | 21 | val gcds: int list -> int | 
| 22 | val lcms: int list -> int | |
| 68028 | 23 | val radicify: int -> int -> int -> int list (* base -> number of positions -> value -> coefficients *) | 
| 24 | val eval_radix: int -> int list -> int (* base -> coefficients -> value *) | |
| 24633 | 25 | end; | 
| 26 | ||
| 27 | structure Integer : INTEGER = | |
| 28 | struct | |
| 29 | ||
| 33029 | 30 | fun min x y = Int.min (x, y); | 
| 31 | fun max x y = Int.max (x, y); | |
| 32 | ||
| 33002 | 33 | fun add x y = x + y; | 
| 34 | fun mult x y = x * y; | |
| 24633 | 35 | |
| 33002 | 36 | fun sum xs = fold add xs 0; | 
| 37 | fun prod xs = fold mult xs 1; | |
| 38 | ||
| 39 | fun sign x = int_ord (x, 0); | |
| 24633 | 40 | |
| 41 | fun div_mod x y = IntInf.divMod (x, y); | |
| 42 | ||
| 43 | fun square x = x * x; | |
| 44 | ||
| 67074 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 45 | fun pow k l = | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 46 | let | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 47 | fun pw 0 _ = 1 | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 48 | | pw 1 l = l | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 49 | | pw k l = | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 50 | let | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 51 | val (k', r) = div_mod k 2; | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 52 | val l' = pw k' (l * l); | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 53 | in if r = 0 then l' else l' * l end; | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 54 | in | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 55 | if k < 0 | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 56 | then IntInf.pow (l, k) | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 57 | else pw k l | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 58 | end; | 
| 24633 | 59 | |
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 60 | fun gcd x y = PolyML.IntInf.gcd (x, y); | 
| 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 61 | fun lcm x y = abs (PolyML.IntInf.lcm (x, y)); | 
| 24633 | 62 | |
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 63 | fun gcds [] = 0 | 
| 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 64 | | gcds (x :: xs) = fold gcd xs x; | 
| 24633 | 65 | |
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 66 | fun lcms [] = 1 | 
| 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 67 | | lcms (x :: xs) = abs (Library.foldl PolyML.IntInf.lcm (x, xs)); | 
| 24633 | 68 | |
| 68028 | 69 | fun radicify base len k = | 
| 70 | let | |
| 71 | val _ = if base < 2 | |
| 72 |       then error ("Bad radix base: " ^ string_of_int base) else ();
 | |
| 73 | fun shift i = swap (div_mod i base); | |
| 74 | in funpow_yield len shift k |> fst end; | |
| 75 | ||
| 76 | fun eval_radix base ks = | |
| 77 | fold_rev (fn k => fn i => k + i * base) ks 0; | |
| 78 | ||
| 24633 | 79 | end; | 
| 67074 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 80 | |
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 81 | (*slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore*) | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 82 | structure IntInf = | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 83 | struct | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 84 | open IntInf; | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 85 | fun pow (i, n) = Integer.pow n i; | 
| 
5da20135f560
slightly faster than Poly/ML 5.7.1 library implementation, notably on 32bit multicore (amending 2288cc39b038)
 wenzelm parents: 
67033diff
changeset | 86 | end; |