| author | wenzelm | 
| Wed, 16 Oct 2024 21:41:05 +0200 | |
| changeset 81177 | 137ea3d464be | 
| parent 78171 | 412a24a4c751 | 
| 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 | |
| 77768 | 9 | val build: (int -> int) -> int | 
| 78171 | 10 | val build1: (int -> int) -> int | 
| 33029 | 11 | val min: int -> int -> int | 
| 12 | val max: int -> int -> int | |
| 33002 | 13 | val add: int -> int -> int | 
| 14 | val mult: int -> int -> int | |
| 15 | val sum: int list -> int | |
| 16 | val prod: int list -> int | |
| 24633 | 17 | val sign: int -> order | 
| 18 | val div_mod: int -> int -> int * int | |
| 73020 
b51515722274
tuned signature -- prefer Isabelle/ML structure Integer;
 wenzelm parents: 
73019diff
changeset | 19 | val quot_rem: int -> int -> int * int | 
| 24633 | 20 | val square: int -> int | 
| 21 | val pow: int -> int -> int (* exponent -> base -> result *) | |
| 73019 
05e2cab9af8b
tuned signature -- prefer Isabelle/ML structure Integer;
 wenzelm parents: 
69729diff
changeset | 22 | val log2: int -> int | 
| 24633 | 23 | val gcd: int -> int -> int | 
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 24 | val lcm: int -> int -> int | 
| 24633 | 25 | val gcds: int list -> int | 
| 26 | val lcms: int list -> int | |
| 68028 | 27 | val radicify: int -> int -> int -> int list (* base -> number of positions -> value -> coefficients *) | 
| 28 | val eval_radix: int -> int list -> int (* base -> coefficients -> value *) | |
| 24633 | 29 | end; | 
| 30 | ||
| 31 | structure Integer : INTEGER = | |
| 32 | struct | |
| 33 | ||
| 77768 | 34 | fun build (f: int -> int) = f 0; | 
| 78171 | 35 | fun build1 (f: int -> int) = f 1; | 
| 77768 | 36 | |
| 33029 | 37 | fun min x y = Int.min (x, y); | 
| 38 | fun max x y = Int.max (x, y); | |
| 39 | ||
| 33002 | 40 | fun add x y = x + y; | 
| 41 | fun mult x y = x * y; | |
| 24633 | 42 | |
| 78171 | 43 | val sum = build o fold add; | 
| 44 | val prod = build1 o fold mult; | |
| 33002 | 45 | |
| 46 | fun sign x = int_ord (x, 0); | |
| 24633 | 47 | |
| 48 | fun div_mod x y = IntInf.divMod (x, y); | |
| 73020 
b51515722274
tuned signature -- prefer Isabelle/ML structure Integer;
 wenzelm parents: 
73019diff
changeset | 49 | fun quot_rem x y = IntInf.quotRem (x, y); | 
| 24633 | 50 | |
| 51 | fun square x = x * x; | |
| 52 | ||
| 69729 | 53 | fun pow k l = IntInf.pow (l, k); | 
| 24633 | 54 | |
| 73019 
05e2cab9af8b
tuned signature -- prefer Isabelle/ML structure Integer;
 wenzelm parents: 
69729diff
changeset | 55 | val log2 = IntInf.log2; | 
| 
05e2cab9af8b
tuned signature -- prefer Isabelle/ML structure Integer;
 wenzelm parents: 
69729diff
changeset | 56 | |
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 57 | fun gcd x y = PolyML.IntInf.gcd (x, y); | 
| 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 58 | fun lcm x y = abs (PolyML.IntInf.lcm (x, y)); | 
| 24633 | 59 | |
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 60 | fun gcds [] = 0 | 
| 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 61 | | gcds (x :: xs) = fold gcd xs x; | 
| 24633 | 62 | |
| 63227 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 63 | fun lcms [] = 1 | 
| 
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
 wenzelm parents: 
33029diff
changeset | 64 | | lcms (x :: xs) = abs (Library.foldl PolyML.IntInf.lcm (x, xs)); | 
| 24633 | 65 | |
| 68028 | 66 | fun radicify base len k = | 
| 67 | let | |
| 68 | val _ = if base < 2 | |
| 69 |       then error ("Bad radix base: " ^ string_of_int base) else ();
 | |
| 70 | fun shift i = swap (div_mod i base); | |
| 71 | in funpow_yield len shift k |> fst end; | |
| 72 | ||
| 77768 | 73 | fun eval_radix base = | 
| 74 | build o fold_rev (fn k => fn i => k + i * base); | |
| 68028 | 75 | |
| 24633 | 76 | end; |