src/Tools/rat.ML
author wenzelm
Sat Oct 06 16:50:04 2007 +0200 (2007-10-06)
changeset 24867 e5b55d7be9bb
parent 24630 351a308ab58d
child 30161 c26e515f1c29
permissions -rw-r--r--
simplified interfaces for outer syntax;
haftmann@24584
     1
(*  Title:      Tools/rat.ML
haftmann@23251
     2
    ID:         $Id$
haftmann@23520
     3
    Author:     Tobias Nipkow, Florian Haftmann, TU Muenchen
haftmann@23251
     4
haftmann@23251
     5
Canonical implementation of exact rational numbers.
haftmann@23251
     6
*)
haftmann@23251
     7
haftmann@23251
     8
signature RAT =
haftmann@23251
     9
sig
wenzelm@23297
    10
  eqtype rat
haftmann@23251
    11
  exception DIVZERO
haftmann@23251
    12
  val zero: rat
haftmann@23251
    13
  val one: rat
haftmann@23251
    14
  val two: rat
wenzelm@24630
    15
  val rat_of_int: int -> rat
wenzelm@24630
    16
  val rat_of_quotient: int * int -> rat
wenzelm@24630
    17
  val quotient_of_rat: rat -> int * int
haftmann@23251
    18
  val string_of_rat: rat -> string
haftmann@23251
    19
  val eq: rat * rat -> bool
haftmann@23520
    20
  val ord: rat * rat -> order
haftmann@23251
    21
  val le: rat -> rat -> bool
haftmann@23251
    22
  val lt: rat -> rat -> bool
haftmann@23520
    23
  val sign: rat -> order
haftmann@23520
    24
  val abs: rat -> rat
haftmann@23251
    25
  val add: rat -> rat -> rat
haftmann@23251
    26
  val mult: rat -> rat -> rat
haftmann@23251
    27
  val neg: rat -> rat
haftmann@23251
    28
  val inv: rat -> rat
haftmann@23520
    29
  val rounddown: rat -> rat
haftmann@23251
    30
  val roundup: rat -> rat
haftmann@23251
    31
end;
haftmann@23251
    32
haftmann@23251
    33
structure Rat : RAT =
haftmann@23251
    34
struct
haftmann@23251
    35
haftmann@23520
    36
fun common (p1, q1) (p2, q2) =
haftmann@23520
    37
  let
haftmann@23520
    38
    val m = Integer.lcm q1 q2;
wenzelm@24630
    39
  in ((p1 * (m div q1), p2 * (m div q2)), m) end;
haftmann@23520
    40
wenzelm@24630
    41
datatype rat = Rat of int * int;  (*nominator, denominator (positive!)*)
haftmann@23251
    42
haftmann@23251
    43
exception DIVZERO;
haftmann@23251
    44
haftmann@23251
    45
fun rat_of_quotient (p, q) =
haftmann@23261
    46
  let
wenzelm@24630
    47
    val m = Integer.gcd (Int.abs p) q
wenzelm@24630
    48
  in Rat (p div m, q div m) end handle Div => raise DIVZERO;
haftmann@23520
    49
haftmann@23520
    50
fun quotient_of_rat (Rat r) = r;
haftmann@23520
    51
haftmann@23520
    52
fun rat_of_int i = Rat (i, 1);
haftmann@23251
    53
haftmann@23520
    54
val zero = rat_of_int 0;
haftmann@23520
    55
val one = rat_of_int 1;
haftmann@23520
    56
val two = rat_of_int 2;
haftmann@23251
    57
haftmann@23520
    58
fun string_of_rat (Rat (p, q)) =
wenzelm@24630
    59
  string_of_int p ^ "/" ^ string_of_int q;
haftmann@23520
    60
haftmann@23520
    61
fun eq (Rat (p1, q1), Rat (p2, q2)) = (p1 = p2 andalso q1 = q2);
haftmann@23251
    62
wenzelm@24630
    63
fun ord (Rat (p1, q1), Rat (p2, q2)) =
wenzelm@24630
    64
 case (Integer.sign p1, Integer.sign p2)
haftmann@23520
    65
 of (LESS, EQUAL) => LESS
haftmann@23520
    66
  | (LESS, GREATER) => LESS
haftmann@23520
    67
  | (EQUAL, LESS) => GREATER
haftmann@23520
    68
  | (EQUAL, EQUAL) => EQUAL
haftmann@23520
    69
  | (EQUAL, GREATER) => LESS
haftmann@23520
    70
  | (GREATER, LESS) => GREATER
haftmann@23520
    71
  | (GREATER, EQUAL) => GREATER
wenzelm@24630
    72
  | _ => int_ord (fst (common (p1, q1) (p2, q2)));
haftmann@23251
    73
haftmann@23520
    74
fun le a b = not (ord (a, b) = GREATER);
haftmann@23520
    75
fun lt a b = (ord (a, b) = LESS);
haftmann@23251
    76
haftmann@23520
    77
fun sign (Rat (p, _)) = Integer.sign p;
haftmann@23251
    78
wenzelm@24630
    79
fun abs (Rat (p, q)) = Rat (Int.abs p, q);
haftmann@23251
    80
haftmann@23520
    81
fun add (Rat (p1, q1)) (Rat (p2, q2)) =
haftmann@23520
    82
  let
haftmann@23520
    83
    val ((m1, m2), n) = common (p1, q1) (p2, q2);
wenzelm@24630
    84
  in rat_of_quotient (m1 + m2, n) end;
haftmann@23251
    85
haftmann@23520
    86
fun mult (Rat (p1, q1)) (Rat (p2, q2)) =
wenzelm@24630
    87
  rat_of_quotient (p1 * p2, q1 * q2);
wenzelm@24630
    88
wenzelm@24630
    89
fun neg (Rat (p, q)) = Rat (~ p, q);
haftmann@23251
    90
wenzelm@24630
    91
fun inv (Rat (p, q)) =
wenzelm@24630
    92
 case Integer.sign p
wenzelm@24630
    93
 of LESS => Rat (~ q, ~ p)
haftmann@24522
    94
  | EQUAL => raise DIVZERO
haftmann@24521
    95
  | GREATER => Rat (q, p);
haftmann@23251
    96
wenzelm@24630
    97
fun rounddown (Rat (p, q)) = Rat (p div q, 1);
haftmann@23520
    98
wenzelm@24630
    99
fun roundup (Rat (p, q)) =
wenzelm@24630
   100
 case Integer.div_mod p q
haftmann@23520
   101
 of (m, 0) => Rat (m, 1)
haftmann@23520
   102
  | (m, _) => Rat (m + 1, 1);
haftmann@23251
   103
haftmann@23251
   104
end;
haftmann@23251
   105
haftmann@23261
   106
infix 7 */ //;
wenzelm@23297
   107
infix 6 +/ -/;
haftmann@23251
   108
infix 4 =/ </ <=/ >/ >=/ <>/;
haftmann@23251
   109
haftmann@23251
   110
fun a +/ b = Rat.add a b;
haftmann@23251
   111
fun a -/ b = a +/ Rat.neg b;
haftmann@23251
   112
fun a */ b = Rat.mult a b;
wenzelm@23297
   113
fun a // b = a */ Rat.inv b;
haftmann@23261
   114
fun a =/ b = Rat.eq (a, b);
haftmann@23251
   115
fun a </ b = Rat.lt a b;
haftmann@23251
   116
fun a <=/ b = Rat.le a b;
haftmann@23251
   117
fun a >/ b = b </ a;
haftmann@23251
   118
fun a >=/ b = b <=/ a;
haftmann@23251
   119
fun a <>/ b = not (a =/ b);