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