for review: Make library/{integer,rational}.m mnc compilable.

Bert Thompson aet at hydra.cs.mu.oz.au
Thu Apr 16 22:21:37 AEST 1998


Peoples,

Some trivial changes are needed to make library/{integer,rational}.m
compile under mnc without compiler errors or warnings.

I've also used "__" consistently as the module dereference operator,
rather than ":". Only with ambiguous operators is the ":" needed
instead, as in "integer:'+'".

I've already committed, so let me know if you see anything dicy in
this fait accompli.

Bert

----------------------------------------

Estimated hours taken: 1

Made trivial code changes to allow mnc to compile these modules.
Note that these modules are correct only as Mercury code.
Although mnc compiles them, they are not otherwise usable by NU-Prolog. 

Our sole objective in compiling with mnc is to avoid errors during the
Mercury build.

library/integer.m:
library/rational.m:
	Changes to allow mnc to compile these modules.

Index: integer.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/integer.m,v
retrieving revision 1.1
diff -u -r1.1 integer.m
--- 1.1	1998/04/15 06:50:11
+++ integer.m	1998/04/16 10:28:26
@@ -82,10 +82,10 @@
 
 :- func integer(int) = integer.
 
-:- func integer:to_string(integer) = string.
+:- func integer__to_string(integer) = string.
 
-:- func integer:from_string(string) = integer.
-:- mode integer:from_string(in) = out is semidet.
+:- func integer__from_string(string) = integer.
+:- mode integer__from_string(in) = out is semidet.
 
 :- func integer:'+'(integer) = integer.
 
@@ -105,12 +105,12 @@
 
 :- func integer:'mod'(integer, integer) = integer.
 
-:- func integer:abs(integer) = integer.
+:- func integer__abs(integer) = integer.
 
-:- pred integer:pow(integer, integer, integer).
-:- mode integer:pow(in, in, out) is det.
+:- pred integer__pow(integer, integer, integer).
+:- mode integer__pow(in, in, out) is det.
 
-% :- func integer:float(integer) = float.
+% :- func integer__float(integer) = float.
 
 :- implementation.
 
@@ -137,11 +137,13 @@
 :- func log10base = int.
 log10base = 4.
 
-integer:'<'(X1, X2) :-
-	big_cmp(X1, X2) = lessthan.
+integer:'<'(X1, X2) :- 
+	big_cmp(X1, X2) = C,
+	C = lessthan.
 
 integer:'>'(X1, X2) :-
-	big_cmp(X1, X2) = greaterthan.
+	big_cmp(X1, X2) = C,
+	C = greaterthan.
 
 integer:'=<'(X1, X2) :-
 	big_cmp(X1, X2) = C,
@@ -152,7 +154,8 @@
 	( C = greaterthan ; C = equal).
 
 integer:'='(X1, X2) :-
-	big_cmp(X1, X2) = equal.
+	big_cmp(X1, X2) = C,
+	C = equal.
 
 :- func one = integer.
 one = integer(1).
@@ -187,7 +190,7 @@
 integer:'mod'(X1, X2) =
 	big_mod(X1, X2).
 
-integer:abs(N) = Abs :-
+integer__abs(N) = Abs :-
 	( N < integer(0) ->
 		Abs = -N
 	;
@@ -233,7 +236,7 @@
 
 	% Compare two integers.
 :- func big_cmp(integer, integer) = comparison.
-big_cmp(i(S1, D1), i(S2, D2)) =
+big_cmp(i(S1, D1), i(S2, D2)) = 
 	( S1 < S2 ->
 		lessthan
 	; S1 > S2 ->
@@ -292,7 +295,7 @@
 		)
 	).
 
-integer:from_string(S) = Big :-
+integer__from_string(S) = Big :-
 	string__to_char_list(S, Cs),
 	string_to_integer(Cs) = Big.
 
@@ -356,17 +359,17 @@
 	% XXX: What about machines that aren't 2's complement?
 :- func int_to_integer(int) = integer.
 int_to_integer(D) = Int :-
-	( int:min_int(D) ->
-		% were we to call int:abs, int overflow might occur.
+	( int__min_int(D) ->
+		% were we to call int__abs, int overflow might occur.
 		Int = integer(D + 1) - one
 	;
-		int:abs(D, AD),
+		int__abs(D, AD),
 		Int = i(signum(D), pos_int_to_digits(AD))
 	).
 
 :- func int_max = int.
 int_max = Maxint :-
-	int:max_int(Maxint).
+	int__max_int(Maxint).
 
 :- func signum(int) = int.
 signum(N) = SN :-
@@ -444,7 +447,7 @@
 :- mode drop_while(pred(in) is semidet, in) = out is det.
 drop_while(_F, []) = [].
 drop_while(F, [X|Xs]) =
-	( F(X) ->
+	( call(F,X) ->
 		drop_while(F, Xs)
 	;
 		[X|Xs]
@@ -493,7 +496,7 @@
 	mul_base(XsYs) = TenXsYs,
 	Sum = pos_plus(XYs, TenXsYs).
 
-integer:to_string(N) = S :-
+integer__to_string(N) = S :-
 	integer_to_string_2(N) = S.
 
 :- func integer_to_string_2(integer) = string.
@@ -528,7 +531,7 @@
 :- mode big_quot_rem(in, in, out, out) is det.
 big_quot_rem(N1, N2, Qt, Rm) :-
 	( N2 = zero ->
-		error("integer:big_quot_rem: division by zero")
+		error("integer__big_quot_rem: division by zero")
 	; N1 = zero ->
 		Qt = zero,
 		Rm = N2
@@ -574,18 +577,19 @@
 :- mode quot_rem_rev(in, in, in, out, out) is det.
 quot_rem_rev(Ur, U, V, Qt, Rm) :-
 	( V = [V0|_] ->
-		( V0 < base div 2 ->
+		BaseDiv2 = base div 2,
+		( V0 < BaseDiv2 ->
 			quot_rem_rev_2(mul_by_digit_rev(M, Ur),
 				mul_by_digit_rev(M, U),
 				mul_by_digit_rev(M, V), Q, R),
 			Qt = Q,
 			Rm = div_by_digit_rev(M, R),
-			M = base div (V0+1)
+			M = base div (V0 + 1)
 		;
 			quot_rem_rev_2(Ur, U, V, Qt, Rm)
 		)
 	;
-		error("integer:quot_rem_rev: software error")
+		error("integer__quot_rem_rev: software error")
 	).
 
 :- pred quot_rem_rev_2(list(digit), list(digit), list(digit), list(digit),
@@ -615,14 +619,16 @@
 		NewUr = pos_sub_rev(Ur, mul_by_digit_rev(Q, V)),
 		( pos_geq_rev(Ur, mul_by_digit_rev(Qhat, V)) ->
 			Q = Qhat
-		; pos_geq_rev(Ur, mul_by_digit_rev(Qhat-1, V)) ->
-			Q = Qhat-1
+		; pos_geq_rev(Ur, mul_by_digit_rev(Qhat - 1, V)) ->
+			Q = Qhat - 1
 		;
 			Q = Qhat - 2
 		),
 		V0 = head(V),
 		U0 = head(Ur),
-		( length(Ur) > length(V) ->
+		LengthUr = length(Ur),
+		LengthV = length(V),
+		( LengthUr > LengthV ->
 			Qhat = (U0*B+U1) div V0,
 			U1 = head(tail(Ur))
 		;
@@ -640,7 +646,7 @@
 	( HT = [Hd|_T] ->
 		H = Hd
 	;
-		error("integer:head: []")
+		error("integer__head: []")
 	).
 		
 :- func tail(list(T)) = list(T).
@@ -648,7 +654,7 @@
 	( HT = [_H|Tl] ->
 		T = Tl
 	;
-		error("integer:tail: []")
+		error("integer__tail: []")
 	).
 
 
@@ -689,7 +695,8 @@
 pos_lt_rev(Xs, Ys) :-
 	list__reverse(Xs, RXs),
 	list__reverse(Ys, RYs),
-	big_cmp(i(1, RXs), i(1, RYs)) = lessthan.
+	C = big_cmp(i(1, RXs), i(1, RYs)),
+	C = lessthan.
 
 :- pred pos_geq_rev(list(digit), list(digit)).
 :- mode pos_geq_rev(in, in) is semidet.
@@ -702,11 +709,13 @@
 :- pred pos_is_zero(list(digit)).
 :- mode pos_is_zero(in) is semidet.
 pos_is_zero(Ds) :-
-	nuke_zeros(Ds) = [].
+	NDs = nuke_zeros(Ds),
+	NDs = [].
 
-integer:pow(A, N, P) :-
-	( N < zero ->
-		error("integer:pow: negative exponent")
+integer__pow(A, N, P) :-
+	Zero = zero,
+	( N < Zero ->
+		error("integer__pow: negative exponent")
 	;
 		P = big_pow(A, N)
 	).
@@ -731,5 +740,6 @@
 :- mode big_odd(in) is semidet.
 big_odd(N) :-
 	N = i(_S, [D|_Ds]),
-	D mod 2 = 1.
+	Dmod2 is D mod 2,
+	Dmod2 = 1.
 
Index: rational.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/rational.m,v
retrieving revision 1.2
diff -u -r1.2 rational.m
--- 1.2	1998/04/15 09:20:50
+++ rational.m	1998/04/16 10:24:22
@@ -50,11 +50,11 @@
 
 :- func rational:'/'(rational, rational) = rational.
 
-:- func rational:numer(rational) = integer.
+:- func rational__numer(rational) = integer.
 
-:- func rational:denom(rational) = integer.
+:- func rational__denom(rational) = integer.
 
-:- func rational:abs(rational) = rational.
+:- func rational__abs(rational) = rational.
 
 :- func one = rational.
 
@@ -85,10 +85,12 @@
 	--->	r(integer, integer).
 
 rational:'<'(R1, R2) :-
-	cmp(R1, R2) = lessthan.
+	Cmp = cmp(R1, R2),
+	Cmp = lessthan.
 
 rational:'>'(R1, R2) :-
-	cmp(R1, R2) = greaterthan.
+	Cmp = cmp(R1, R2),
+	Cmp = greaterthan.
 
 rational:'=<'(R1, R2) :-
 	Cmp = cmp(R1, R2),
@@ -104,8 +106,8 @@
 
 %% XXX: There are ways to do this in some cases even if the
 %% float conversions would overflow.
-% rational:float(r(Num, Den)) =
-%	float:'/'(integer:float(Num), integer:float(Den)).
+% rational__float(r(Num, Den)) =
+%	float:'/'(integer__float(Num), integer__float(Den)).
 
 one = r(integer(1), integer(1)).
 
@@ -115,8 +117,8 @@
 
 rational:'-'(r(Num, Den)) = r(-Num, Den).
 
-rational:'+'(r(An, Ad), r(Bn, Bd)) =
-	rational_norm(An*CA + Bn*CB, M) :-
+rational:'+'(r(An, Ad), r(Bn, Bd)) = rational_norm(Numer, M) :-
+	Numer is An * CA + Bn * CB,
 	M = lcm(Ad, Bd),
 	CA = M // Ad,
 	CB = M // Bd.
@@ -124,9 +126,10 @@
 rational:'-'(R1, R2) =
 	R1 + (-R2).
 
-rational:'*'(r(An, Ad), r(Bn, Bd)) =
 	% XXX: need we call rational_norm here?
-	rational_norm((An//G1)*(Bn//G2), (Ad//G2)*(Bd//G1)) :-
+rational:'*'(r(An, Ad), r(Bn, Bd)) = rational_norm(Numer, Denom) :-
+	Numer is (An//G1) * (Bn//G2),
+	Denom is (Ad//G2) * (Bd//G1),
 	G1 = gcd(An, Bd),
 	G2 = gcd(Ad, Bn).
 
@@ -136,21 +139,21 @@
 :- func inverse(rational) = rational.
 inverse(r(Num, Den)) = Rat :-
 	( Num = izero ->
-		error("rational:inverse: division by zero")
+		error("rational__inverse: division by zero")
 	;
 		Rat = r(signum(Num)*Den, abs(Num))
 	).
 
-rational:numer(r(Num, _)) = Num.
+rational__numer(r(Num, _)) = Num.
 
-rational:denom(r(_, Den)) = Den.
+rational__denom(r(_, Den)) = Den.
 
-rational:abs(r(Num, Den)) = r(abs(Num), Den).
+rational__abs(r(Num, Den)) = r(abs(Num), Den).
 
 :- func rational_norm(integer, integer) = rational.
 rational_norm(Num, Den) = Rat :-
 	( Den = izero ->
-		error("rational:rational_norm: division by zero")
+		error("rational__rational_norm: division by zero")
 	; Num = izero ->
 		Rat = r(izero, ione)
 	;
@@ -214,5 +217,6 @@
 :- pred is_negative(rational).
 :- mode is_negative(in) is semidet.
 is_negative(r(Num, _)) :-
-	Num < izero.
+	Zero = izero,
+	Num < Zero.
 



More information about the developers mailing list