src/Tools/integer.ML
author haftmann
Fri, 29 Jun 2007 21:23:05 +0200
changeset 23520 483fe92f00c1
parent 23517 93d1ad7662a9
child 24584 01e83ffa6c54
permissions -rw-r--r--
tuned arithmetic modules
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/General/int.ML
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     4
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     5
Unbounded integers.
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     6
*)
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     7
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     8
signature INTEGER =
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     9
sig
23298
404988d8b1e0 eqtype int -- explicitly encourage overloaded equality;
wenzelm
parents: 23251
diff changeset
    10
  eqtype int
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    11
  val zero: int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    12
  val one: int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    13
  val two: int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    14
  val int: Int.int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    15
  val machine_int: int -> Int.int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    16
  val string_of_int: int -> string
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    17
  val int_of_string: string -> int option
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    18
  val eq: int * int -> bool
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    19
  val ord: int * int -> order
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    20
  val le: int -> int -> bool
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    21
  val lt: int -> int -> bool
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    22
  val signabs: int -> order * int
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    23
  val sign: int -> order
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    24
  val abs: int -> int
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    25
  val min: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    26
  val max: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    27
  val inc: int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    28
  val add: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    29
  val sub: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    30
  val mult: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    31
  val divmod: int -> int -> int * int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    32
  val div: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    33
  val mod: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    34
  val neg: int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    35
  val exp: int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    36
  val log: int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    37
  val pow: int -> int -> int (* exponent -> base -> result *)
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    38
  val gcd: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    39
  val lcm: int -> int -> int
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    40
end;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    41
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    42
structure Integer : INTEGER =
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    43
struct
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    44
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    45
open IntInf;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    46
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    47
val int = fromInt;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    48
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    49
val zero = int 0;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    50
val one = int 1;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    51
val two = int 2;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    52
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    53
val machine_int = toInt;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    54
val string_of_int = toString;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    55
val int_of_string = fromString;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    56
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    57
val eq = op = : int * int -> bool;
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    58
val ord = compare;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    59
val le = curry (op <=);
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    60
val lt = curry (op <);
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    61
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    62
fun sign k = ord (k, zero);
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    63
fun signabs k = (ord (k, zero), abs k);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    64
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    65
val min = curry min;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    66
val max = curry max;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    67
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    68
val inc = curry (op +) one;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    69
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    70
val add = curry (op +);
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    71
val sub = curry (op -);
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    72
val mult = curry ( op * );
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    73
val divmod = curry divMod;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    74
nonfix div val div = curry div;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    75
nonfix mod val mod = curry mod;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    76
val neg = ~;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    77
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    78
fun pow k l =
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    79
  let
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    80
    fun pw 0 _ = 1
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    81
      | pw 1 l = l
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    82
      | pw k l =
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    83
          let
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    84
            val (k', r) = divmod k 2;
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    85
            val l' = pw k' (mult l l);
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    86
          in if r = 0 then l' else mult l' l end;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    87
  in if k < zero
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    88
    then error "pow: negative exponent"
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23517
diff changeset
    89
    else pw k l
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    90
  end;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    91
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    92
fun exp k = pow k two;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    93
val log = int o log2;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    94
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    95
fun gcd x y =
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    96
  let
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    97
    fun gxd x y = if y = zero then x else gxd y (mod x y)
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    98
  in if lt x y then gxd y x else gxd x y end;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    99
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   100
fun lcm x y = div (mult x y) (gcd x y);
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   101
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   102
end;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   103
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   104
type integer = Integer.int;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   105
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   106
infix 7 *%;
23298
404988d8b1e0 eqtype int -- explicitly encourage overloaded equality;
wenzelm
parents: 23251
diff changeset
   107
infix 6 +% -%;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   108
infix 4 =% <% <=% >% >=% <>%;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   109
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   110
fun a +% b = Integer.add a b;
23298
404988d8b1e0 eqtype int -- explicitly encourage overloaded equality;
wenzelm
parents: 23251
diff changeset
   111
fun a -% b = Integer.sub a b;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   112
fun a *% b = Integer.mult a b;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   113
fun a =% b = Integer.eq (a, b);
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   114
fun a <% b = Integer.lt a b;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   115
fun a <=% b = Integer.le a b;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   116
fun a >% b = b <% a;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   117
fun a >=% b = b <=% a;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   118
fun a <>% b = not (a =% b);