author | haftmann |
Sat, 19 May 2007 11:33:30 +0200 | |
changeset 23024 | 70435ffe077d |
parent 22950 | 8b6d28fc6532 |
child 23036 | 65b4f545a76f |
permissions | -rw-r--r-- |
17848 | 1 |
(* Title: Pure/General/rat.ML |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow, TU Muenchen |
|
4 |
||
17940 | 5 |
Canonical implementation of exact rational numbers. |
17848 | 6 |
*) |
7 |
||
8 |
signature RAT = |
|
9 |
sig |
|
10 |
type rat |
|
11 |
exception DIVZERO |
|
12 |
val zero: rat |
|
19557 | 13 |
val one: rat |
22950 | 14 |
val two: rat |
15 |
val rat_of_int: Intt.int -> rat |
|
16 |
val rat_of_quotient: Intt.int * Intt.int -> rat |
|
17 |
val quotient_of_rat: rat -> Intt.int * Intt.int |
|
17848 | 18 |
val string_of_rat: rat -> string |
19 |
val eq: rat * rat -> bool |
|
22950 | 20 |
val cmp: rat * rat -> order |
21 |
val le: rat -> rat -> bool |
|
22 |
val cmp_zero: rat -> order |
|
23 |
val add: rat -> rat -> rat |
|
24 |
val mult: rat -> rat -> rat |
|
17848 | 25 |
val neg: rat -> rat |
26 |
val inv: rat -> rat |
|
17940 | 27 |
val roundup: rat -> rat |
28 |
val rounddown: rat -> rat |
|
17848 | 29 |
end; |
30 |
||
31 |
structure Rat: RAT = |
|
32 |
struct |
|
33 |
||
22950 | 34 |
datatype rat = Rat of bool * Intt.int * Intt.int; |
17848 | 35 |
|
36 |
exception DIVZERO; |
|
37 |
||
22950 | 38 |
val zero = Rat (true, Intt.int 0, Intt.int 1); |
39 |
val one = Rat (true, Intt.int 1, Intt.int 1); |
|
40 |
val two = Rat (true, Intt.int 2, Intt.int 1); |
|
19557 | 41 |
|
22950 | 42 |
fun rat_of_int i = |
43 |
if i < Intt.int 0 |
|
44 |
then Rat (false, ~i, Intt.int 1) |
|
45 |
else Rat (true, i, Intt.int 1); |
|
17848 | 46 |
|
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
47 |
fun norm (a, p, q) = |
22950 | 48 |
if p = Intt.int 0 then Rat (true, Intt.int 0, Intt.int 1) |
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
49 |
else |
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
50 |
let |
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
51 |
val absp = abs p |
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
52 |
val m = gcd (absp, q) |
22950 | 53 |
in Rat(a = (Intt.int 0 <= p), absp div m, q div m) end; |
17848 | 54 |
|
55 |
fun common (p1, q1, p2, q2) = |
|
56 |
let val q' = lcm (q1, q2) |
|
57 |
in (p1 * (q' div q1), p2 * (q' div q2), q') end |
|
58 |
||
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
59 |
fun rat_of_quotient (p, q) = |
22950 | 60 |
if q = Intt.int 0 then raise DIVZERO |
61 |
else norm (Intt.int 0 <= q, p, abs q); |
|
17848 | 62 |
|
63 |
fun quotient_of_rat (Rat (a, p, q)) = (if a then p else ~p, q); |
|
64 |
||
65 |
fun string_of_rat r = |
|
66 |
let val (p, q) = quotient_of_rat r |
|
22950 | 67 |
in Intt.string_of_int p ^ "/" ^ Intt.string_of_int q end; |
17848 | 68 |
|
17940 | 69 |
fun eq (Rat (false, _, _), Rat (true, _, _)) = false |
70 |
| eq (Rat (true, _, _), Rat (false, _, _)) = false |
|
71 |
| eq (Rat (_, p1, q1), Rat (_, p2, q2)) = p1 = p2 andalso q1 = q2 |
|
72 |
||
22950 | 73 |
fun le (Rat (false, _, _)) (Rat (true, _, _)) = true |
74 |
| le (Rat (true, _, _)) (Rat (false, _, _)) = false |
|
75 |
| le (Rat (a, p1, q1)) (Rat (_, p2, q2)) = |
|
17940 | 76 |
let val (r1, r2, _) = common (p1, q1, p2, q2) |
77 |
in if a then r1 <= r2 else r2 <= r1 end; |
|
78 |
||
22950 | 79 |
fun cmp (Rat (false, _, _), Rat (true, _, _)) = LESS |
80 |
| cmp (Rat (true, _, _), Rat (false, _, _)) = GREATER |
|
81 |
| cmp (Rat (a, p1, q1), Rat (_, p2, q2)) = |
|
17940 | 82 |
let val (r1, r2, _) = common (p1, q1, p2, q2) |
22950 | 83 |
in if a then Intt.cmp (r1, r2) else Intt.cmp (r2, r1) end; |
17940 | 84 |
|
22950 | 85 |
fun cmp_zero (Rat (false, _, _)) = LESS |
86 |
| cmp_zero (Rat (_, 0, _)) = EQUAL |
|
87 |
| cmp_zero (Rat (_, _, _)) = GREATER; |
|
17848 | 88 |
|
22950 | 89 |
fun add (Rat (a1, p1, q1)) (Rat(a2, p2, q2)) = |
17848 | 90 |
let |
91 |
val (r1, r2, den) = common (p1, q1, p2, q2) |
|
92 |
val num = (if a1 then r1 else ~r1) + (if a2 then r2 else ~r2) |
|
93 |
in norm (true, num, den) end; |
|
94 |
||
22950 | 95 |
fun mult (Rat (a1, p1, q1)) (Rat (a2, p2, q2)) = |
17848 | 96 |
norm (a1=a2, p1*p2, q1*q2); |
97 |
||
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
98 |
fun neg (r as Rat (b, p, q)) = |
22950 | 99 |
if p = Intt.int 0 then r |
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
100 |
else Rat (not b, p, q); |
17848 | 101 |
|
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
102 |
fun inv (Rat (a, p, q)) = |
22950 | 103 |
if p = Intt.int 0 then raise DIVZERO |
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
104 |
else Rat (a, q, p); |
17848 | 105 |
|
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
106 |
fun roundup (r as Rat (a, p, q)) = |
22950 | 107 |
if q = Intt.int 1 then r |
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
108 |
else |
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
109 |
let |
22950 | 110 |
fun round true q = Rat (true, q + Intt.int 1, Intt.int 1) |
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
111 |
| round false q = |
22950 | 112 |
if q = Intt.int 0 |
113 |
then Rat (true, Intt.int 0, Intt.int 1) |
|
114 |
else Rat (false, q, Intt.int 1); |
|
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
115 |
in round a (p div q) end; |
17940 | 116 |
|
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
117 |
fun rounddown (r as Rat (a, p, q)) = |
22950 | 118 |
if q = Intt.int 1 then r |
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
119 |
else |
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
120 |
let |
22950 | 121 |
fun round true q = Rat (true, q, Intt.int 1) |
122 |
| round false q = Rat (false, q + Intt.int 1, Intt.int 1) |
|
22574
e6c25fd3de2a
avoid overloaded integer constants (accomodate Alice);
wenzelm
parents:
22189
diff
changeset
|
123 |
in round a (p div q) end; |
17940 | 124 |
|
17848 | 125 |
end; |