src/Pure/General/integer.ML
author blanchet
Fri, 20 Dec 2013 20:36:38 +0100
changeset 54838 16511f84913c
parent 33029 2fefe039edf1
child 63227 d3ed7f00e818
permissions -rw-r--r--
reconstruct SPASS-Pirate steps of the form 'x ~= C x' (or more complicated)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28308
d4396a28fb29 fixed headers
haftmann
parents: 24633
diff changeset
     1
(*  Title:      Pure/General/integer.ML
24633
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
     3
33002
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
     4
Auxiliary operations on (unbounded) integers.
24633
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
     5
*)
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
     6
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
     7
signature INTEGER =
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
     8
sig
33029
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 33002
diff changeset
     9
  val min: int -> int -> int
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 33002
diff changeset
    10
  val max: int -> int -> int
33002
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    11
  val add: int -> int -> int
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    12
  val mult: int -> int -> int
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    13
  val sum: int list -> int
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    14
  val prod: int list -> int
24633
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    15
  val sign: int -> order
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    16
  val div_mod: int -> int -> int * int
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    17
  val square: int -> int
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    18
  val pow: int -> int -> int (* exponent -> base -> result *)
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    19
  val gcd: int -> int -> int
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    20
  val gcds: int list -> int
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    21
  val lcm: int -> int -> int
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    22
  val lcms: int list -> int
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    23
end;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    24
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    25
structure Integer : INTEGER =
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    26
struct
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    27
33029
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 33002
diff changeset
    28
fun min x y = Int.min (x, y);
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 33002
diff changeset
    29
fun max x y = Int.max (x, y);
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 33002
diff changeset
    30
33002
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    31
fun add x y = x + y;
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    32
fun mult x y = x * y;
24633
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    33
33002
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    34
fun sum xs = fold add xs 0;
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    35
fun prod xs = fold mult xs 1;
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    36
f3f02f36a3e2 uniform use of Integer.add/mult/sum/prod;
wenzelm
parents: 29606
diff changeset
    37
fun sign x = int_ord (x, 0);
24633
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    38
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    39
fun div_mod x y = IntInf.divMod (x, y);
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    40
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    41
fun square x = x * x;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    42
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    43
fun pow k l =
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    44
  let
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    45
    fun pw 0 _ = 1
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    46
      | pw 1 l = l
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    47
      | pw k l =
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    48
          let
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    49
            val (k', r) = div_mod k 2;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    50
            val l' = pw k' (l * l);
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    51
          in if r = 0 then l' else l' * l end;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    52
  in
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    53
    if k < 0
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    54
    then error "pow: negative exponent"
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    55
    else pw k l
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    56
  end;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    57
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    58
fun gcd x y =
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    59
  let
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    60
    fun gxd x y = if y = 0 then x else gxd y (x mod y)
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    61
  in if x < y then gxd y x else gxd x y end;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    62
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    63
fun gcds xs = fold gcd xs 0;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    64
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    65
fun lcm x y = (x * y) div (gcd x y);
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    66
fun lcms xs = fold lcm xs 1;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    67
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    68
end;
0a3a02066244 moved Tools/integer.ML to Pure/General/integer.ML;
wenzelm
parents:
diff changeset
    69