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