| author | wenzelm |
| Fri, 01 Jan 2021 17:08:51 +0100 | |
| changeset 73019 | 05e2cab9af8b |
| parent 69729 | 4591221824f6 |
| child 73020 | b51515722274 |
| 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 *) |
|
|
73019
05e2cab9af8b
tuned signature -- prefer Isabelle/ML structure Integer;
wenzelm
parents:
69729
diff
changeset
|
19 |
val log2: int -> int |
| 24633 | 20 |
val gcd: int -> int -> int |
|
63227
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
wenzelm
parents:
33029
diff
changeset
|
21 |
val lcm: int -> int -> int |
| 24633 | 22 |
val gcds: int list -> int |
23 |
val lcms: int list -> int |
|
| 68028 | 24 |
val radicify: int -> int -> int -> int list (* base -> number of positions -> value -> coefficients *) |
25 |
val eval_radix: int -> int list -> int (* base -> coefficients -> value *) |
|
| 24633 | 26 |
end; |
27 |
||
28 |
structure Integer : INTEGER = |
|
29 |
struct |
|
30 |
||
| 33029 | 31 |
fun min x y = Int.min (x, y); |
32 |
fun max x y = Int.max (x, y); |
|
33 |
||
| 33002 | 34 |
fun add x y = x + y; |
35 |
fun mult x y = x * y; |
|
| 24633 | 36 |
|
| 33002 | 37 |
fun sum xs = fold add xs 0; |
38 |
fun prod xs = fold mult xs 1; |
|
39 |
||
40 |
fun sign x = int_ord (x, 0); |
|
| 24633 | 41 |
|
42 |
fun div_mod x y = IntInf.divMod (x, y); |
|
43 |
||
44 |
fun square x = x * x; |
|
45 |
||
| 69729 | 46 |
fun pow k l = IntInf.pow (l, k); |
| 24633 | 47 |
|
|
73019
05e2cab9af8b
tuned signature -- prefer Isabelle/ML structure Integer;
wenzelm
parents:
69729
diff
changeset
|
48 |
val log2 = IntInf.log2; |
|
05e2cab9af8b
tuned signature -- prefer Isabelle/ML structure Integer;
wenzelm
parents:
69729
diff
changeset
|
49 |
|
|
63227
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
wenzelm
parents:
33029
diff
changeset
|
50 |
fun gcd x y = PolyML.IntInf.gcd (x, y); |
|
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
wenzelm
parents:
33029
diff
changeset
|
51 |
fun lcm x y = abs (PolyML.IntInf.lcm (x, y)); |
| 24633 | 52 |
|
|
63227
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
wenzelm
parents:
33029
diff
changeset
|
53 |
fun gcds [] = 0 |
|
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
wenzelm
parents:
33029
diff
changeset
|
54 |
| gcds (x :: xs) = fold gcd xs x; |
| 24633 | 55 |
|
|
63227
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
wenzelm
parents:
33029
diff
changeset
|
56 |
fun lcms [] = 1 |
|
d3ed7f00e818
Integer.lcm normalizes the sign as in HOL/GCD.thy;
wenzelm
parents:
33029
diff
changeset
|
57 |
| lcms (x :: xs) = abs (Library.foldl PolyML.IntInf.lcm (x, xs)); |
| 24633 | 58 |
|
| 68028 | 59 |
fun radicify base len k = |
60 |
let |
|
61 |
val _ = if base < 2 |
|
62 |
then error ("Bad radix base: " ^ string_of_int base) else ();
|
|
63 |
fun shift i = swap (div_mod i base); |
|
64 |
in funpow_yield len shift k |> fst end; |
|
65 |
||
66 |
fun eval_radix base ks = |
|
67 |
fold_rev (fn k => fn i => k + i * base) ks 0; |
|
68 |
||
| 24633 | 69 |
end; |