[m-rev.] for review: printing of integers using io.print

Julien Fischer jfischer at opturion.com
Sun Mar 2 23:48:20 AEDT 2014


For review by anyone.

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

Improve printing of arbitrary precision integers by io.print etc.

library/stream.string_writer.m:
 	Make the print family of predicates print the decimal representation
 	of integer/0 values instead of their underlying representation.

library/io.m:
 	Update the documentation of io.print to conform to the above change.

 	Fix a typo: s/hander/handler/

NEWS:
 	Announce the above change.

tests/hard_coded/Mmakefile:
tests/hard_coded/print_bigint.{m.exp}:
 	Test printing of integers with io.print.

Julien.

diff --git a/NEWS b/NEWS
index 7b8ce03..fe4c664 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,9 @@ Changes to the Mercury standard library:
    io module.  These behave like the print and write predicates, but also
    write a terminating newline character.

+* io.print and string_writer.print now print arbitrary precision integers
+  in their decimal form instead of printing their underlying representation.
+
  Changes to the extras distribution:

  * We have added support for Unicode and other enhancements to the lex and
diff --git a/library/io.m b/library/io.m
index db43016..893b28e 100644
--- a/library/io.m
+++ b/library/io.m
@@ -387,8 +387,10 @@
      % type.  It is output in a format that is intended to be human readable.
      %
      % If the argument is just a single string or character, it will be printed
-    % out exactly as is (unquoted).  If the argument is of type univ, then it
-    % will print out the value stored in the univ, but not the type.
+    % out exactly as is (unquoted).  If the argument is of type integer (i.e.
+    % an arbitrary precision integer), then its decimal representation will be
+    % printed.  If the argument is of type univ, then it will print out the
+    % value stored in the univ, but not the type.
      %
      % io.print/5 is the same as io.print/4 except that it allows the caller to
      % specify how non-canonical types should be handled. io.print/3 and
@@ -5660,7 +5662,7 @@ io.call_system_return_signal(Command, Result, !IO) :-
      --->    io_error(string).       % This is subject to change.
      % Note that we use `io_error' rather than `io.error' because io.print,
      % which may be called to print out the uncaught exception if there is no
-    % exception hander, does not print out the module name.
+    % exception handler, does not print out the module name.

  io.make_io_error(Error) = io_error(Error).

diff --git a/library/stream.string_writer.m b/library/stream.string_writer.m
index 4b72df7..32bfbc9 100644
--- a/library/stream.string_writer.m
+++ b/library/stream.string_writer.m
@@ -49,8 +49,10 @@
      % readable.
      %
      % If the argument is just a single string or character, it will be printed
-    % out exactly as is (unquoted).  If the argument is of type univ, then it
-    % will print out the value stored in the univ, but not the type.
+    % out exactly as is (unquoted).  If the argument is of type integer (i.e.
+    % an arbitrary precision integer), then its decimal representation will be
+    % printed.  If the argument is of type univ, then it will print out the
+    % value stored in the univ, but not the type.
      %
      % print/5 is the same as print/4 except that it allows the caller to
      % specify how non-canonical types should be handled.  print/4 implicitly
@@ -177,6 +179,7 @@
  :- import_module array.
  :- import_module bitmap.
  :- import_module int.
+:- import_module integer.
  :- import_module require.
  :- import_module rtti_implementation.
  :- import_module term_io.
@@ -259,6 +262,8 @@ print(Stream, NonCanon, Term, !State) :-
          put(Stream, Char, !State)
      ; dynamic_cast(Term, OrigUniv) ->
          write_univ(Stream, OrigUniv, !State)
+    ; dynamic_cast(Term, BigInt) ->
+        put(Stream, integer.to_string(BigInt), !State)
      ;
          print_quoted(Stream, NonCanon, Term, !State)
      ).
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 209dfaa..abb7084 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -229,6 +229,7 @@ ORDINARY_PROGS=	\
  	pragma_inline \
  	pretty_printing \
  	prince_frameopt \
+	print_bigint \
  	print_stream \
  	profdeep_seg_fault \
  	promise_equiv_with_svars \
diff --git a/tests/hard_coded/print_bigint.exp b/tests/hard_coded/print_bigint.exp
new file mode 100644
index 0000000..43c1e02
--- /dev/null
+++ b/tests/hard_coded/print_bigint.exp
@@ -0,0 +1,2 @@
+128391829381928390189238901823128931283908192389182903819283901890251908239081290380182031
+-128391829381928390189238901823128931283908192389182903819283901890251908239081290380182031
diff --git a/tests/hard_coded/print_bigint.m b/tests/hard_coded/print_bigint.m
new file mode 100644
index 0000000..30d07f5
--- /dev/null
+++ b/tests/hard_coded/print_bigint.m
@@ -0,0 +1,19 @@
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+
+% Test io.print with arbitrary precision integers.
+
+:- module print_bigint.
+:- interface.
+
+:- import_module io.
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module integer.
+
+main(!IO) :- 
+    I = integer.det_from_string("128391829381928390189238901823128931283908192389182903819283901890251908239081290380182031"),
+    NegI = -I,
+    io.print_line(I, !IO),
+    io.print_line(NegI : integer, !IO).





More information about the reviews mailing list