author | wenzelm |
Wed, 01 Jun 2016 16:02:02 +0200 | |
changeset 63209 | 82cd1d481eb9 |
parent 63208 | 3251e9dfea91 |
child 63210 | a0685d2b420b |
permissions | -rw-r--r-- |
63197
af562e976038
rat.ML is now part of Pure to allow tigther integration with Isabelle/ML;
wenzelm
parents:
30161
diff
changeset
|
1 |
(* Title: Pure/General/rat.ML |
23520 | 2 |
Author: Tobias Nipkow, Florian Haftmann, TU Muenchen |
63207 | 3 |
Author: Makarius |
23251 | 4 |
|
5 |
Canonical implementation of exact rational numbers. |
|
6 |
*) |
|
7 |
||
8 |
signature RAT = |
|
9 |
sig |
|
23297 | 10 |
eqtype rat |
23251 | 11 |
exception DIVZERO |
63201 | 12 |
val of_int: int -> rat |
13 |
val make: int * int -> rat |
|
14 |
val dest: rat -> int * int |
|
23251 | 15 |
val string_of_rat: rat -> string |
63208 | 16 |
val signed_string_of_rat: rat -> string |
23251 | 17 |
val eq: rat * rat -> bool |
23520 | 18 |
val ord: rat * rat -> order |
23251 | 19 |
val le: rat -> rat -> bool |
20 |
val lt: rat -> rat -> bool |
|
23520 | 21 |
val sign: rat -> order |
22 |
val abs: rat -> rat |
|
23251 | 23 |
val add: rat -> rat -> rat |
24 |
val mult: rat -> rat -> rat |
|
25 |
val neg: rat -> rat |
|
26 |
val inv: rat -> rat |
|
63206 | 27 |
val floor: rat -> int |
28 |
val ceil: rat -> int |
|
23251 | 29 |
end; |
30 |
||
31 |
structure Rat : RAT = |
|
32 |
struct |
|
33 |
||
63202 | 34 |
datatype rat = Rat of int * int; (*numerator, positive (!) denominator*) |
63199
da38571dd5bd
prefer more efficient Poly/ML operations, taking care of sign;
wenzelm
parents:
63198
diff
changeset
|
35 |
|
23520 | 36 |
fun common (p1, q1) (p2, q2) = |
63199
da38571dd5bd
prefer more efficient Poly/ML operations, taking care of sign;
wenzelm
parents:
63198
diff
changeset
|
37 |
let val m = PolyML.IntInf.lcm (q1, q2) |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24584
diff
changeset
|
38 |
in ((p1 * (m div q1), p2 * (m div q2)), m) end; |
23520 | 39 |
|
23251 | 40 |
exception DIVZERO; |
41 |
||
63201 | 42 |
fun make (p, q) = |
63200 | 43 |
let |
44 |
val m = PolyML.IntInf.gcd (p, q); |
|
45 |
val (p', q') = (p div m, q div m) handle Div => raise DIVZERO; |
|
46 |
in Rat (if q' < 0 then (~ p', ~ q') else (p', q')) end |
|
23520 | 47 |
|
63201 | 48 |
fun dest (Rat r) = r; |
23520 | 49 |
|
63201 | 50 |
fun of_int i = Rat (i, 1); |
23251 | 51 |
|
63208 | 52 |
fun string_of_rat (Rat (p, 1)) = string_of_int p |
53 |
| string_of_rat (Rat (p, q)) = string_of_int p ^ "/" ^ string_of_int q; |
|
54 |
||
55 |
fun signed_string_of_rat (Rat (p, 1)) = signed_string_of_int p |
|
56 |
| signed_string_of_rat (Rat (p, q)) = signed_string_of_int p ^ "/" ^ string_of_int q; |
|
23520 | 57 |
|
58 |
fun eq (Rat (p1, q1), Rat (p2, q2)) = (p1 = p2 andalso q1 = q2); |
|
23251 | 59 |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24584
diff
changeset
|
60 |
fun ord (Rat (p1, q1), Rat (p2, q2)) = |
63202 | 61 |
(case (Integer.sign p1, Integer.sign p2) of |
62 |
(LESS, EQUAL) => LESS |
|
23520 | 63 |
| (LESS, GREATER) => LESS |
64 |
| (EQUAL, LESS) => GREATER |
|
65 |
| (EQUAL, EQUAL) => EQUAL |
|
66 |
| (EQUAL, GREATER) => LESS |
|
67 |
| (GREATER, LESS) => GREATER |
|
68 |
| (GREATER, EQUAL) => GREATER |
|
63202 | 69 |
| _ => int_ord (fst (common (p1, q1) (p2, q2)))); |
23251 | 70 |
|
23520 | 71 |
fun le a b = not (ord (a, b) = GREATER); |
63202 | 72 |
fun lt a b = ord (a, b) = LESS; |
23251 | 73 |
|
23520 | 74 |
fun sign (Rat (p, _)) = Integer.sign p; |
23251 | 75 |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24584
diff
changeset
|
76 |
fun abs (Rat (p, q)) = Rat (Int.abs p, q); |
23251 | 77 |
|
23520 | 78 |
fun add (Rat (p1, q1)) (Rat (p2, q2)) = |
63202 | 79 |
let val ((m1, m2), n) = common (p1, q1) (p2, q2) |
63201 | 80 |
in make (m1 + m2, n) end; |
23251 | 81 |
|
63202 | 82 |
fun mult (Rat (p1, q1)) (Rat (p2, q2)) = make (p1 * p2, q1 * q2); |
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24584
diff
changeset
|
83 |
|
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24584
diff
changeset
|
84 |
fun neg (Rat (p, q)) = Rat (~ p, q); |
23251 | 85 |
|
24630
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
wenzelm
parents:
24584
diff
changeset
|
86 |
fun inv (Rat (p, q)) = |
63202 | 87 |
(case Integer.sign p of |
88 |
LESS => Rat (~ q, ~ p) |
|
24522 | 89 |
| EQUAL => raise DIVZERO |
63202 | 90 |
| GREATER => Rat (q, p)); |
23251 | 91 |
|
63206 | 92 |
fun floor (Rat (p, q)) = p div q; |
23520 | 93 |
|
63206 | 94 |
fun ceil (Rat (p, q)) = |
63202 | 95 |
(case Integer.div_mod p q of |
63206 | 96 |
(m, 0) => m |
97 |
| (m, _) => m); |
|
23251 | 98 |
|
99 |
end; |
|
100 |
||
63198
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
101 |
ML_system_overload (uncurry Rat.add) "+"; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
102 |
ML_system_overload (fn (a, b) => Rat.add a (Rat.neg b)) "-"; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
103 |
ML_system_overload (uncurry Rat.mult) "*"; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
104 |
ML_system_overload (fn (a, b) => Rat.mult a (Rat.inv b)) "/"; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
105 |
ML_system_overload Rat.eq "="; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
106 |
ML_system_overload (uncurry Rat.lt) "<"; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
107 |
ML_system_overload (uncurry Rat.le) "<="; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
108 |
ML_system_overload (fn (a, b) => Rat.lt b a) ">"; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
109 |
ML_system_overload (fn (a, b) => Rat.le b a) ">="; |
c583ca33076a
ad-hoc overloading for standard operations on type Rat.rat;
wenzelm
parents:
63197
diff
changeset
|
110 |
ML_system_overload (not o Rat.eq) "<>"; |
63209 | 111 |
|
112 |
ML_system_pp (fn _ => fn _ => fn x => Pretty.to_polyml (Pretty.str ("@" ^ Rat.string_of_rat x))); |