[m-rev.] for review: add some functions to integer.m and rational.m
Julien Fischer
juliensf at students.cs.mu.OZ.AU
Thu Dec 18 15:20:11 AEDT 2003
On Tue, 16 Dec 2003, Fergus Henderson wrote:
> On 16-Dec-2003, Julien Fischer <juliensf at students.cs.mu.OZ.AU> wrote:
> >
> > Add some functions to the integer and rational modules.
> >
> > Clean up the code in these modules so it adheres more closely to
> > our current coding standards.
> ...
> > Index: library/integer.m
> ...
> > +:- pred chop(int::in, digit::out, digit::out) is det.
> >
> > -:- pred chop(int, digit, digit).
> > -:- mode chop(in, out, out) is det.
> > chop(N, Div, Mod) :-
> > - %Div = N div base,
> > - %Mod = N mod base.
> > Div = N >> log2base,
> > Mod = N /\ basemask.
>
> Please keep the comments.
> Although they would be better laid out like this:
>
> chop(N, Div, Mod) :-
> Div = N >> log2base, % i.e. Div = N div base
> Mod = N /\ basemask. % i.e. Mod = N mod base
>
Fixed.
> > +++ library/rational.m 16 Dec 2003 03:52:04 -0000
> ...
> > @@ -30,10 +32,17 @@
> > :- pred rational:'>='(rational, rational).
> > :- mode rational:'>='(in, in) is semidet.
>
> While you're converting this to modern coding styles, please replace the
> ":"s here with "." (or just delete the module qualifier).
>
Done.
> > @@ -108,88 +125,82 @@
> > % rational__float(r(Num, Den)) =
> > % float:'/'(integer__float(Num), integer__float(Den)).
> >
> > -one = r(integer(1), integer(1)).
> > +rational__one = r(integer(1), integer(1)).
> >
> > -zero = r(integer(0), integer(1)).
> > +rational__zero = r(integer(0), integer(1)).
>
> I thought you wanted to use integer__zero and integer__one?
>
I missed those ones ;-)
There's a relative diff below.
Julien.
diff -u library/integer.m library/integer.m
--- library/integer.m 16 Dec 2003 04:07:55 -0000
+++ library/integer.m 18 Dec 2003 03:54:44 -0000
@@ -693,8 +693,8 @@
:- pred chop(int::in, digit::out, digit::out) is det.
chop(N, Div, Mod) :-
- Div = N >> log2base,
- Mod = N /\ basemask.
+ Div = N >> log2base, % i.e. Div = N div base
+ Mod = N /\ basemask. % i.e. Mod = N mode base
:- func pos_plus(integer, integer) = integer.
diff -u library/rational.m library/rational.m
--- library/rational.m 16 Dec 2003 03:52:04 -0000
+++ library/rational.m 18 Dec 2003 04:03:13 -0000
@@ -20,17 +20,17 @@
:- type rational.
-:- pred rational:'<'(rational, rational).
-:- mode rational:'<'(in, in) is semidet.
+:- pred '<'(rational, rational).
+:- mode '<'(in, in) is semidet.
-:- pred rational:'>'(rational, rational).
-:- mode rational:'>'(in, in) is semidet.
+:- pred '>'(rational, rational).
+:- mode '>'(in, in) is semidet.
-:- pred rational:'=<'(rational, rational).
-:- mode rational:'=<'(in, in) is semidet.
+:- pred '=<'(rational, rational).
+:- mode '=<'(in, in) is semidet.
-:- pred rational:'>='(rational, rational).
-:- mode rational:'>='(in, in) is semidet.
+:- pred '>='(rational, rational).
+:- mode '>='(in, in) is semidet.
:- func rational__rational(int) = rational.
@@ -46,17 +46,17 @@
% :- func float(rational) = float.
-:- func rational:'+'(rational) = rational.
+:- func '+'(rational) = rational.
-:- func rational:'-'(rational) = rational.
+:- func '-'(rational) = rational.
-:- func rational:'+'(rational, rational) = rational.
+:- func rational + rational = rational.
-:- func rational:'-'(rational, rational) = rational.
+:- func rational - rational = rational.
-:- func rational:'*'(rational, rational) = rational.
+:- func rational * rational = rational.
-:- func rational:'/'(rational, rational) = rational.
+:- func rational / rational = rational.
:- func rational__numer(rational) = integer.
@@ -94,19 +94,19 @@
:- type rational
---> r(integer, integer).
-rational:'<'(R1, R2) :-
+'<'(R1, R2) :-
Cmp = cmp(R1, R2),
Cmp = lessthan.
-rational:'>'(R1, R2) :-
+'>'(R1, R2) :-
Cmp = cmp(R1, R2),
Cmp = greaterthan.
-rational:'=<'(R1, R2) :-
+'=<'(R1, R2) :-
Cmp = cmp(R1, R2),
(Cmp = lessthan ; Cmp = equal).
-rational:'>='(R1, R2) :-
+'>='(R1, R2) :-
Cmp = cmp(R1, R2),
(Cmp = greaterthan ; Cmp = equal).
@@ -125,30 +125,30 @@
% rational__float(r(Num, Den)) =
% float:'/'(integer__float(Num), integer__float(Den)).
-rational__one = r(integer(1), integer(1)).
+rational__one = r(integer__one, integer__one).
-rational__zero = r(integer(0), integer(1)).
+rational__zero = r(integer__zero, integer__one).
-rational:'+'(Rat) = Rat.
+'+'(Rat) = Rat.
-rational:'-'(r(Num, Den)) = r(-Num, Den).
+'-'(r(Num, Den)) = r(-Num, Den).
-rational:'+'(r(An, Ad), r(Bn, Bd)) = rational_norm(Numer, M) :-
+r(An, Ad) + r(Bn, Bd) = rational_norm(Numer, M) :-
M = lcm(Ad, Bd),
CA = M // Ad,
CB = M // Bd,
Numer = An * CA + Bn * CB.
-rational:'-'(R1, R2) = R1 + (-R2).
+R1 - R2 = R1 + (-R2).
% XXX: need we call rational_norm here?
-rational:'*'(r(An, Ad), r(Bn, Bd)) = rational_norm(Numer, Denom) :-
+r(An, Ad) * r(Bn, Bd) = rational_norm(Numer, Denom) :-
G1 = gcd(An, Bd),
G2 = gcd(Ad, Bn),
Numer = (An // G1) * (Bn // G2),
Denom = (Ad // G2) * (Bd // G1).
-rational:'/'(R1, R2) = R1 * inverse(R2).
+R1 / R2 = R1 * inverse(R2).
:- func inverse(rational) = rational.
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list