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