src/Tools/rat.ML
author blanchet
Mon, 15 Sep 2014 10:49:07 +0200
changeset 58335 a5a3b576fcfb
parent 30161 c26e515f1c29
permissions -rw-r--r--
generate 'code' attribute only if 'code' plugin is enabled
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24584
01e83ffa6c54 fixed title
haftmann
parents: 24522
diff changeset
     1
(*  Title:      Tools/rat.ML
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
     2
    Author:     Tobias Nipkow, Florian Haftmann, TU Muenchen
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     3
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     4
Canonical implementation of exact rational numbers.
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     5
*)
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     6
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     7
signature RAT =
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
     8
sig
23297
06f108974fa1 simplified type integer;
wenzelm
parents: 23261
diff changeset
     9
  eqtype rat
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    10
  exception DIVZERO
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    11
  val zero: rat
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    12
  val one: rat
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    13
  val two: rat
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    14
  val rat_of_int: int -> rat
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    15
  val rat_of_quotient: int * int -> rat
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    16
  val quotient_of_rat: rat -> int * int
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    17
  val string_of_rat: rat -> string
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    18
  val eq: rat * rat -> bool
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    19
  val ord: rat * rat -> order
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    20
  val le: rat -> rat -> bool
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    21
  val lt: rat -> rat -> bool
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    22
  val sign: rat -> order
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    23
  val abs: rat -> rat
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    24
  val add: rat -> rat -> rat
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    25
  val mult: rat -> rat -> rat
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    26
  val neg: rat -> rat
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    27
  val inv: rat -> rat
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    28
  val rounddown: rat -> rat
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    29
  val roundup: rat -> rat
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    30
end;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    31
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    32
structure Rat : RAT =
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    33
struct
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    34
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    35
fun common (p1, q1) (p2, q2) =
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    36
  let
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    37
    val m = Integer.lcm q1 q2;
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    38
  in ((p1 * (m div q1), p2 * (m div q2)), m) end;
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    39
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    40
datatype rat = Rat of int * int;  (*nominator, denominator (positive!)*)
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    41
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    42
exception DIVZERO;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    43
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    44
fun rat_of_quotient (p, q) =
23261
85f27f79232f tuned integers
haftmann
parents: 23251
diff changeset
    45
  let
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    46
    val m = Integer.gcd (Int.abs p) q
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    47
  in Rat (p div m, q div m) end handle Div => raise DIVZERO;
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    48
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    49
fun quotient_of_rat (Rat r) = r;
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    50
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    51
fun rat_of_int i = Rat (i, 1);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    52
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    53
val zero = rat_of_int 0;
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    54
val one = rat_of_int 1;
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    55
val two = rat_of_int 2;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    56
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    57
fun string_of_rat (Rat (p, q)) =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    58
  string_of_int p ^ "/" ^ string_of_int q;
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    59
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    60
fun eq (Rat (p1, q1), Rat (p2, q2)) = (p1 = p2 andalso q1 = q2);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    61
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    62
fun ord (Rat (p1, q1), Rat (p2, q2)) =
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    63
 case (Integer.sign p1, Integer.sign p2)
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    64
 of (LESS, EQUAL) => LESS
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    65
  | (LESS, GREATER) => LESS
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    66
  | (EQUAL, LESS) => GREATER
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    67
  | (EQUAL, EQUAL) => EQUAL
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    68
  | (EQUAL, GREATER) => LESS
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    69
  | (GREATER, LESS) => GREATER
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    70
  | (GREATER, EQUAL) => GREATER
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    71
  | _ => int_ord (fst (common (p1, q1) (p2, q2)));
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    72
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    73
fun le a b = not (ord (a, b) = GREATER);
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    74
fun lt a b = (ord (a, b) = LESS);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    75
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    76
fun sign (Rat (p, _)) = Integer.sign p;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    77
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    78
fun abs (Rat (p, q)) = Rat (Int.abs p, q);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    79
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    80
fun add (Rat (p1, q1)) (Rat (p2, q2)) =
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    81
  let
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    82
    val ((m1, m2), n) = common (p1, q1) (p2, q2);
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    83
  in rat_of_quotient (m1 + m2, n) end;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    84
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    85
fun mult (Rat (p1, q1)) (Rat (p2, q2)) =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    86
  rat_of_quotient (p1 * p2, q1 * q2);
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    87
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    88
fun neg (Rat (p, q)) = Rat (~ p, q);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    89
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    90
fun inv (Rat (p, q)) =
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    91
 case Integer.sign p
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    92
 of LESS => Rat (~ q, ~ p)
24522
ec3e5c1a0479 fixed Rat.inv
haftmann
parents: 24521
diff changeset
    93
  | EQUAL => raise DIVZERO
24521
9565ac68c3cd fixed Rat.inv
haftmann
parents: 23520
diff changeset
    94
  | GREATER => Rat (q, p);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
    95
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    96
fun rounddown (Rat (p, q)) = Rat (p div q, 1);
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
    97
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    98
fun roundup (Rat (p, q)) =
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24584
diff changeset
    99
 case Integer.div_mod p q
23520
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
   100
 of (m, 0) => Rat (m, 1)
483fe92f00c1 tuned arithmetic modules
haftmann
parents: 23297
diff changeset
   101
  | (m, _) => Rat (m + 1, 1);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   102
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   103
end;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   104
23261
85f27f79232f tuned integers
haftmann
parents: 23251
diff changeset
   105
infix 7 */ //;
23297
06f108974fa1 simplified type integer;
wenzelm
parents: 23261
diff changeset
   106
infix 6 +/ -/;
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   107
infix 4 =/ </ <=/ >/ >=/ <>/;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   108
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   109
fun a +/ b = Rat.add a b;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   110
fun a -/ b = a +/ Rat.neg b;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   111
fun a */ b = Rat.mult a b;
23297
06f108974fa1 simplified type integer;
wenzelm
parents: 23261
diff changeset
   112
fun a // b = a */ Rat.inv b;
23261
85f27f79232f tuned integers
haftmann
parents: 23251
diff changeset
   113
fun a =/ b = Rat.eq (a, b);
23251
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   114
fun a </ b = Rat.lt a b;
471b576aad25 moved generic algebra modules
haftmann
parents:
diff changeset
   115
fun a <=/ b = Rat.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);