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