[m-rev.] diff: changes to integer module
Julien Fischer
juliensf at cs.mu.OZ.AU
Fri Nov 5 17:10:06 AEDT 2004
Estimated hours taken: 1
Branches: main
Add a new function to the integer module and deprecate an
existing predicate.
library/integer.m:
Add a function integer.det_from_string/1 that is similar
to integer.from_string/1 except that it throws an
exception instead of failing if the string cannot be
converted to an integer.
Deprecate the predicate version of integer.pow. We don't
gain anything from having both a predicate and function
version.
library/string.m:
Replace a call to the predicate version of integer.pow with
a call to the function version.
tests/hard_coded/integer_test.m:
Test integer.det_from_string.
Replace instances of `:' as the module qualifier.
Include integer.one/0 and integer.zero/0 in some
of the tests.
NEWS:
Mention these changes.
Julien.
Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.347
diff -u -r1.347 NEWS
--- NEWS 5 Nov 2004 05:03:15 -0000 1.347
+++ NEWS 5 Nov 2004 05:30:16 -0000
@@ -262,8 +262,13 @@
io.binary_input_stream/3
io.binary_output_stream/3
-* We've added some constant functions, integer.zero/0 and integer.one/0
- to integer.m. We have also added a function version of integer.pow/3.
+* The following functions have been added to the integer module:
+ integer.zero/0
+ integer.one/0
+ integer.det_from_string/1
+ integer.pow/2
+
+ The predicate integer.pow/3 has been deprecated.
* We've added some functions, rational.int/1, rational.from_integer/1,
rational.from_integers/2 and rational.reciprocal/1 to rational.m
Index: library/integer.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/integer.m,v
retrieving revision 1.15
diff -u -r1.15 integer.m
--- library/integer.m 15 Mar 2004 23:49:32 -0000 1.15
+++ library/integer.m 5 Nov 2004 05:37:53 -0000
@@ -41,6 +41,8 @@
:- func integer__from_string(string::in) = (integer::out) is semidet.
+:- func integer__det_from_string(string) = integer.
+
:- func '+'(integer) = integer.
:- func '-'(integer) = integer.
@@ -73,8 +75,9 @@
:- func integer__abs(integer) = integer.
-:- pred integer__pow(integer::in, integer::in, integer::out) is det.
:- func integer__pow(integer, integer) = integer.
+:- pragma obsolete(integer.pow/3).
+:- pred integer__pow(integer::in, integer::in, integer::out) is det.
:- func integer__float(integer) = float.
:- func integer__int(integer) = int.
@@ -888,10 +891,9 @@
C = pos_cmp(Xs, Ys),
( C = (>) ; C = (=) ).
-integer__pow(A, N) = P :-
- integer__pow(A, N, P).
+integer__pow(A, N, integer.pow(A, N)).
-integer__pow(A, N, P) :-
+integer__pow(A, N) = P :-
( big_isnegative(N) ->
error("integer__pow: negative exponent")
;
@@ -983,6 +985,14 @@
integer__from_string(S) = Big :-
string__to_char_list(S, Cs),
string_to_integer(Cs) = Big.
+
+integer__det_from_string(S) =
+ ( I = integer__from_string(S) ->
+ I
+ ;
+ func_error(
+ "integer.det_from_string/1: cannot convert to integer.")
+ ).
:- func string_to_integer(list(char)::in) = (integer::out) is semidet.
Index: library/string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.220
diff -u -r1.220 string.m
--- library/string.m 12 Oct 2004 06:37:39 -0000 1.220
+++ library/string.m 2 Nov 2004 04:15:45 -0000
@@ -1991,7 +1991,7 @@
->
AbsIntStr = ""
;
- integer__pow(integer(2), integer(int__bits_per_int), Div),
+ Div = integer__pow(integer(2), integer(int__bits_per_int)),
UnsignedInteger = integer(Int) mod Div,
( Base = 10 ->
AbsIntStr0 = integer__to_string(UnsignedInteger)
Index: tests/hard_coded/integer_test.m
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/integer_test.m,v
retrieving revision 1.2
diff -u -r1.2 integer_test.m
--- tests/hard_coded/integer_test.m 26 May 2003 09:01:07 -0000 1.2
+++ tests/hard_coded/integer_test.m 5 Nov 2004 05:35:49 -0000
@@ -11,14 +11,10 @@
:- import_module integer, string, list, int, require.
main -->
- {
- X = integer:from_string("1234567890987654321"),
- Y = integer:from_string(
- "98765432101234567890123400000009999111"),
- Z = integer(200)
- ;
- error("integer_test:main: internal error in test")
- },
+ X = integer.det_from_string("1234567890987654321"),
+ Y = integer.det_from_string(
+ "98765432101234567890123400000009999111"),
+ Z = integer(200)
test(X, Y, Z).
:- pred test(integer, integer, integer, io__state, io__state).
@@ -31,7 +27,7 @@
Minus = X - Y,
Div = Y // X,
Rem = Y rem X,
- integer:pow(X,Z,Pow),
+ Pow = integer.pow(X, Z),
fac(Z,Fac)
},
write_message("X: ", X),
@@ -45,52 +41,51 @@
write_message("fac(Z): ", Fac),
write_message("pow(X,Z): ", Pow),
{ X0 = integer(100000), X1 = integer(3) },
- write_integer(X0), io:write_string(" div mod "),
- write_integer(X1), io:write_string(" = "),
- write_integer(X0 div X1), io:write_string(" "),
- write_integer(X0 mod X1), io:nl,
- write_integer(-X0), io:write_string(" div mod "),
- write_integer(X1), io:write_string(" = "),
- write_integer(X0 div -X1), io:write_string(" "),
- write_integer(X0 mod -X1), io:nl,
- write_integer(X0), io:write_string(" div mod "),
- write_integer(-X1), io:write_string(" = "),
- write_integer(-X0 div X1), io:write_string(" "),
- write_integer(-X0 mod X1), io:nl,
- write_integer(-X0), io:write_string(" div mod "),
- write_integer(-X1), io:write_string(" = "),
- write_integer(-X0 div -X1), io:write_string(" "),
- write_integer(-X0 mod -X1), io:nl,
- write_integer(X0), io:write_string(" // rem "),
- write_integer(X1), io:write_string(" = "),
- write_integer(X0 // X1), io:write_string(" "),
- write_integer(X0 rem X1), io:nl,
- write_integer(-X0), io:write_string(" // rem "),
- write_integer(X1), io:write_string(" = "),
- write_integer(X0 // -X1), io:write_string(" "),
- write_integer(X0 rem -X1), io:nl,
- write_integer(X0), io:write_string(" // rem "),
- write_integer(-X1), io:write_string(" = "),
- write_integer(-X0 // X1), io:write_string(" "),
- write_integer(-X0 rem X1), io:nl,
- write_integer(-X0), io:write_string(" // rem "),
- write_integer(-X1), io:write_string(" = "),
- write_integer(-X0 // -X1), io:write_string(" "),
- write_integer(-X0 rem -X1), io:nl,
- { int:min_int(Minint) },
+ write_integer(X0), io.write_string(" div mod "),
+ write_integer(X1), io.write_string(" = "),
+ write_integer(X0 div X1), io.write_string(" "),
+ write_integer(X0 mod X1), io.nl,
+ write_integer(-X0), io.write_string(" div mod "),
+ write_integer(X1), io.write_string(" = "),
+ write_integer(X0 div -X1), io.write_string(" "),
+ write_integer(X0 mod -X1), io.nl,
+ write_integer(X0), io.write_string(" div mod "),
+ write_integer(-X1), io.write_string(" = "),
+ write_integer(-X0 div X1), io.write_string(" "),
+ write_integer(-X0 mod X1), io.nl,
+ write_integer(-X0), io.write_string(" div mod "),
+ write_integer(-X1), io.write_string(" = "),
+ write_integer(-X0 div -X1), io.write_string(" "),
+ write_integer(-X0 mod -X1), io.nl,
+ write_integer(X0), io.write_string(" // rem "),
+ write_integer(X1), io.write_string(" = "),
+ write_integer(X0 // X1), io.write_string(" "),
+ write_integer(X0 rem X1), io.nl,
+ write_integer(-X0), io.write_string(" // rem "),
+ write_integer(X1), io.write_string(" = "),
+ write_integer(X0 // -X1), io.write_string(" "),
+ write_integer(X0 rem -X1), io.nl,
+ write_integer(X0), io.write_string(" // rem "),
+ write_integer(-X1), io.write_string(" = "),
+ write_integer(-X0 // X1), io.write_string(" "),
+ write_integer(-X0 rem X1), io.nl,
+ write_integer(-X0), io.write_string(" // rem "),
+ write_integer(-X1), io.write_string(" = "),
+ write_integer(-X0 // -X1), io.write_string(" "),
+ write_integer(-X0 rem -X1), io.nl,
+ { int.min_int(Minint) },
( { integer(Minint) < integer(0) } ->
- io:write_string("integer(min_int) ok\n")
+ io.write_string("integer(min_int) ok\n")
;
- io:write_string("integer(min_int) failed\n")
+ io.write_string("integer(min_int) failed\n")
).
-
:- pred write_message(string, integer, io__state, io__state).
:- mode write_message(in, in, di, uo) is det.
write_message(String, Int) -->
io__write_string(String),
- { Str = integer:to_string(Int) },
+ { Str = integer.to_string(Int) },
io__write_string(Str),
io__nl.
@@ -98,17 +93,17 @@
:- mode fac(in, out) is det.
fac(X,F) :-
- ( X =< integer(0) ->
- F = integer(1)
+ ( X =< integer.zero ->
+ F = integer.one
;
- fac(X-integer(1),F1),
+ fac(X - integer.one,F1),
F = F1 * X
).
-:- pred write_integer(integer, io:state, io:state).
+:- pred write_integer(integer, io.state, io.state).
:- mode write_integer(in, di, uo) is det.
write_integer(X) -->
- { S = integer:to_string(X) },
- io:write_string(S).
+ { S = integer.to_string(X) },
+ io.write_string(S).
--------------------------------------------------------------------------
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