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
haftmann@23251
     1
(*  Title:      Pure/General/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
haftmann@23261
    15
  val rat_of_int: integer -> rat
haftmann@23261
    16
  val rat_of_quotient: integer * integer -> rat
haftmann@23261
    17
  val quotient_of_rat: rat -> integer * integer
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 signabs: rat -> order * rat
haftmann@23520
    24
  val sign: rat -> order
haftmann@23520
    25
  val abs: rat -> rat
haftmann@23251
    26
  val add: rat -> rat -> rat
haftmann@23251
    27
  val mult: rat -> rat -> rat
haftmann@23251
    28
  val neg: rat -> rat
haftmann@23251
    29
  val inv: rat -> rat
haftmann@23520
    30
  val rounddown: rat -> rat
haftmann@23251
    31
  val roundup: rat -> rat
haftmann@23251
    32
end;
haftmann@23251
    33
haftmann@23251
    34
structure Rat : RAT =
haftmann@23251
    35
struct
haftmann@23251
    36
haftmann@23520
    37
fun common (p1, q1) (p2, q2) =
haftmann@23520
    38
  let
haftmann@23520
    39
    val m = Integer.lcm q1 q2;
haftmann@23520
    40
  in ((Integer.mult p1 (Integer.div m q1), Integer.mult p2 (Integer.div m q2)), m) end;
haftmann@23520
    41
haftmann@23520
    42
datatype rat = Rat of integer * integer; (* nominator, denominator (positive!) *)
haftmann@23251
    43
haftmann@23251
    44
exception DIVZERO;
haftmann@23251
    45
haftmann@23251
    46
fun rat_of_quotient (p, q) =
haftmann@23261
    47
  let
haftmann@23520
    48
    val m = Integer.gcd (Integer.abs p) q
haftmann@23520
    49
  in Rat (Integer.div p m, Integer.div q m) end handle Div => raise DIVZERO;
haftmann@23520
    50
haftmann@23520
    51
fun quotient_of_rat (Rat r) = r;
haftmann@23520
    52
haftmann@23520
    53
fun rat_of_int i = Rat (i, 1);
haftmann@23251
    54
haftmann@23520
    55
val zero = rat_of_int 0;
haftmann@23520
    56
val one = rat_of_int 1;
haftmann@23520
    57
val two = rat_of_int 2;
haftmann@23251
    58
haftmann@23520
    59
fun string_of_rat (Rat (p, q)) =
haftmann@23520
    60
  Integer.string_of_int p ^ "/" ^ Integer.string_of_int q;
haftmann@23520
    61
haftmann@23520
    62
fun eq (Rat (p1, q1), Rat (p2, q2)) = (p1 = p2 andalso q1 = q2);
haftmann@23251
    63
haftmann@23520
    64
fun ord (Rat (p1, q1), Rat (p2, q2)) = 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
haftmann@23520
    72
  | _ => Integer.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
haftmann@23520
    79
fun abs (Rat (p, q)) = Rat (Integer.abs p, q);
haftmann@23251
    80
haftmann@23520
    81
fun signabs (Rat (p, q)) =
haftmann@23251
    82
  let
haftmann@23520
    83
    val (s, p') = Integer.signabs p;
haftmann@23520
    84
  in (s, Rat (p', q)) end;
haftmann@23251
    85
haftmann@23520
    86
fun add (Rat (p1, q1)) (Rat (p2, q2)) =
haftmann@23520
    87
  let
haftmann@23520
    88
    val ((m1, m2), n) = common (p1, q1) (p2, q2);
haftmann@23520
    89
  in rat_of_quotient (Integer.add m1 m2, n) end;
haftmann@23251
    90
haftmann@23520
    91
fun mult (Rat (p1, q1)) (Rat (p2, q2)) =
haftmann@23520
    92
  rat_of_quotient (Integer.mult p1 p2, Integer.mult q1 q2);
haftmann@23251
    93
haftmann@23520
    94
fun neg (Rat (p, q)) = Rat (Integer.neg p, q);
haftmann@23520
    95
haftmann@23520
    96
fun inv (Rat (p, 0)) = raise DIVZERO
haftmann@23520
    97
  | inv (Rat (p, q)) = Rat (q, p);
haftmann@23251
    98
haftmann@23520
    99
fun rounddown (Rat (p, q)) = Rat (Integer.div p q, 1);
haftmann@23520
   100
haftmann@23520
   101
fun roundup (Rat (p, q)) = case Integer.divmod p q
haftmann@23520
   102
 of (m, 0) => Rat (m, 1)
haftmann@23520
   103
  | (m, _) => Rat (m + 1, 1);
haftmann@23251
   104
haftmann@23251
   105
end;
haftmann@23251
   106
haftmann@23261
   107
infix 7 */ //;
wenzelm@23297
   108
infix 6 +/ -/;
haftmann@23251
   109
infix 4 =/ </ <=/ >/ >=/ <>/;
haftmann@23251
   110
haftmann@23251
   111
fun a +/ b = Rat.add a b;
haftmann@23251
   112
fun a -/ b = a +/ Rat.neg b;
haftmann@23251
   113
fun a */ b = Rat.mult a b;
wenzelm@23297
   114
fun a // b = a */ Rat.inv b;
haftmann@23261
   115
fun a =/ b = Rat.eq (a, b);
haftmann@23251
   116
fun a </ b = Rat.lt a b;
haftmann@23251
   117
fun a <=/ b = Rat.le a b;
haftmann@23251
   118
fun a >/ b = b </ a;
haftmann@23251
   119
fun a >=/ b = b <=/ a;
haftmann@23251
   120
fun a <>/ b = not (a =/ b);