src/Tools/rat.ML
 author wenzelm Sat, 09 Jun 2007 00:28:46 +0200 changeset 23297 06f108974fa1 parent 23261 85f27f79232f child 23520 483fe92f00c1 permissions -rw-r--r--
simplified type integer;
```
(*  Title:      Pure/General/rat.ML
ID:         \$Id\$
Author:     Tobias Nipkow, TU Muenchen

Canonical implementation of exact rational numbers.
*)

signature RAT =
sig
eqtype rat
exception DIVZERO
val zero: rat
val one: rat
val two: rat
val rat_of_int: integer -> rat
val rat_of_quotient: integer * integer -> rat
val quotient_of_rat: rat -> integer * integer
val string_of_rat: rat -> string
val eq: rat * rat -> bool
val cmp: rat * rat -> order
val le: rat -> rat -> bool
val lt: rat -> rat -> bool
val cmp_zero: rat -> order
val add: rat -> rat -> rat
val mult: rat -> rat -> rat
val neg: rat -> rat
val inv: rat -> rat
val roundup: rat -> rat
val rounddown: rat -> rat
end;

structure Rat : RAT =
struct

datatype rat = Rat of bool * integer * integer;

exception DIVZERO;

val zero = Rat (true, 0, 1);
val one = Rat (true, 1, 1);
val two = Rat (true, 2, 1);

fun rat_of_int i =
let
val (a, p) = Integer.signabs i
in Rat (a, p, 1) end;

fun norm (a, p, q) =
if p = 0 then Rat (true, 0, 1)
else
let
val (b, absp) = Integer.signabs p;
val m = Integer.gcd absp q;
in Rat (a = b, absp div m, q div m) end;

fun common (p1, q1, p2, q2) =
let
val q' = Integer.lcm q1 q2;
in (p1 * (q' div q1), p2 * (q' div q2), q') end

fun rat_of_quotient (p, q) =
let
val (a, absq) = Integer.signabs q;
in
if absq = 0 then raise DIVZERO
else norm (a, p, absq)
end;

fun quotient_of_rat (Rat (a, p, q)) = (if a then p else ~ p, q);

fun string_of_rat r =
let
val (p, q) = quotient_of_rat r;
in Integer.string_of_int p ^ "/" ^ Integer.string_of_int q end;

fun eq (Rat (false, _, _), Rat (true, _, _)) = false
| eq (Rat (true, _, _), Rat (false, _, _)) = false
| eq (Rat (_, p1, q1), Rat (_, p2, q2)) = p1 = p2 andalso q1 = q2;

fun cmp (Rat (false, _, _), Rat (true, _, _)) = LESS
| cmp (Rat (true, _, _), Rat (false, _, _)) = GREATER
| cmp (Rat (a, p1, q1), Rat (_, p2, q2)) =
let val (r1, r2, _) = common (p1, q1, p2, q2)
in if a then Integer.cmp (r1, r2) else Integer.cmp (r2, r1) end;

fun le a b = let val order = cmp (a, b) in order = LESS orelse order = EQUAL end;
fun lt a b = (cmp (a, b) = LESS);

fun cmp_zero (Rat (false, _, _)) = LESS
| cmp_zero (Rat (_, 0, _)) = EQUAL
| cmp_zero (Rat (_, _, _)) = GREATER;

fun add (Rat (a1, p1, q1)) (Rat(a2, p2, q2)) =
let
val (r1, r2, den) = common (p1, q1, p2, q2);
val num = (if a1 then r1 else ~ r1)
+ (if a2 then r2 else ~ r2);
in norm (true, num, den) end;

fun mult (Rat (a1, p1, q1)) (Rat (a2, p2, q2)) =
norm (a1 = a2, p1 * p2, q1 * q2);

fun neg (r as Rat (b, p, q)) =
if p = 0 then r
else Rat (not b, p, q);

fun inv (Rat (a, p, q)) =
if q = 0 then raise DIVZERO
else Rat (a, q, p);

fun roundup (r as Rat (a, p, q)) =
if q = 1 then r
else
let
fun round true q = Rat (true, q + 1, 1)
| round false q =
Rat (q = 0, 0, 1);
in round a (p div q) end;

fun rounddown (r as Rat (a, p, q)) =
if q = 1 then r
else
let
fun round true q = Rat (true, q, 1)
| round false q = Rat (false, q + 1, 1)
in round a (p div q) end;

end;

infix 7 */ //;
infix 6 +/ -/;
infix 4 =/ </ <=/ >/ >=/ <>/;

fun a +/ b = Rat.add a b;
fun a -/ b = a +/ Rat.neg b;
fun a */ b = Rat.mult a b;
fun a // b = a */ Rat.inv b;
fun a =/ b = Rat.eq (a, b);
fun a </ b = Rat.lt a b;
fun a <=/ b = Rat.le a b;
fun a >/ b = b </ a;
fun a >=/ b = b <=/ a;
fun a <>/ b = not (a =/ b);
```