io__write et al

Fergus Henderson fjh at cs.mu.oz.au
Tue Apr 22 17:58:41 AEST 1997


Hi,

I just had a long discussion with Peter Schachte regarding write, read,
print, display, and co.

We settled on the following design.

`io__print' is what you teach first years and what you use for
debugging.  The output is supposed to be human-readable;
it is not guaranteed to be readable via io__read.

	In detail, it has the following semantics:

	io__print:
		If the argument is a string or a character, print it
		without quotes.  Otherwise call io__print_quoted.

	io__print_quoted: (this predicate is not exported)
		If the type is univ, print the value, and then a
		type-qualifier (":"), and then the module-qualified type name.
		If the type is int, print as per io__write_int.
		If the type is float, print as per io__write_float.
		If the type is string or char, print it quoted
		and escaped.  If the type is array, print it as
		`to_array([..., ..., ...])'.
		If the type is a higher-order type, just print
		`<<higher order term>>".
		If the argument is a discriminated union
		type, then
		(a) If the argument is a member of type-class
		    "Portrayable", then call the portray method
		    [until we have type-classes, ignore this step]
		(b) Otherwise, print the functor and call
		    io__print_quoted on all the arguments.
		    The functor should be quoted if necessary
		    and escaped if necessary.  Operator notation
		    should be used if appropriate, and operator
		    precedences should be taken into account to
		    avoid redundent parentheses.

`io__write' prints something in a manner that can be read in again
with io__read for the same type.  It is similar to `io__print', except
that (a) it never calls the portray method (b) strings and characters
are always quoted and escaped if necessary, even at the top level.

`io__read' reads anything written by `io__write', except that
it may get an error for higher-order types and a few others
that you can't easily output.

`io__write_binary' and `io__read_binary' would also be useful.
Basically same as `io__read' and `io__write', but binary, not text, and
the results are not guaranteed to be meaningful if read in by a
different program from the one that wrote it.

Prolog's display/1 doesn't seem useful enough to be worth emulating.

Prolog hackers that are used to

	write('Foo = '), write(Foo), nl,

should instead write

	print("Foo = "), print(Foo), nl,


Comments?

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list