[m-dev.] string streams and formatting
Peter Schachte
schachte at cs.mu.OZ.AU
Mon Jan 10 12:19:31 AEDT 2000
On Thu, Jan 06, 2000 at 12:13:28PM +1100, Michael Day wrote:
> But back to formatting. Below is a prototype module for some basic output
> formatting, simply consisting of wrapping the thing you want to write in a
> format type and writing that instead. Examples:
>
> write(ascii(Term)), % replaces io__write
> write(binary(Term)), % replaces io__write_binary
>
> (Possibly replace ascii(T) and unicode(T) with text(T) and an optional
> character encoding specifier, that would appear to be more convenient).
>
> write(right(80, hex(42))), % "2A" on right of 80 char field
Three comments:
1) I wouldn't call this "write." I think write should either write
terms (cf Prolog) or raw data (cf C). Probably the former:
why make a gratuitous change to the current library? At any
rate, "write" doesn't seem like a good name for the predicate
that does formated output. How about "format" or "output?"
2) I think you'll do better if you base this on lists, rather
than individual outputs. For example, putting out 2A on the
right of a 80 character field is not as useful as putting, eg,
"The result is 2A" in an 80 character field. For that, you'd
want the goal to look something like
output(right(80, [string("The result is "), hex(42)]))
3) Most importantly: for several reasons, I think the best way to
handle this is using a format type class (I didn't say
sequence!), rather than just a format type. The two most
important reasons are: a) it allows users to define their own
format types, which will allow them to use the same formatted
output predicates for their own types which need special
formatting; and b) it allows much more expressive and compact
types. Eg, you can define string as an instance of the format
class, and then you can just write
output(right(80, ["The result is ", hex(42)]))
for the previous example. You'll probably need to use
existential types for this, but I believe they work well
enough now that this could be made to work. Int, float, etc,
should also be made instance of the format class, so if you
hadn't wanted hex output in the previous example, you could
just write
output(right(80, ["The result is ", 42]))
Oh, and if you make some(T) list(T) an instance of format,
that handles point 2 above without making users write
singleton lists when they've only got one thing to output. So
all of the following would be valid:
output(42)
output(["The result is ", 42])
output(right(80, hex(42)))
output(right(80, ["The result is ", hex(42)])
> Fairly straight forward and still lacking in many things, but it seems
> like a reasonable way of approaching formatting. The
> implementation has been included solely for people who wish to
> offer suggestions to improve my Mercury coding style and has no
> bearing on the interface or any future implementation. Comments would be
> appreciated. Please don't tell me to use a sequence type class again until
> we actually have one. Thank you :)
Aw, come on! You're taking all the fun out of it :-).
--
Peter Schachte Intellectual brilliance is no guarentee
mailto:schachte at cs.mu.OZ.AU against being dead wrong.
http://www.cs.mu.oz.au/~schachte/ -- David Fasold
PGP: finger schachte at 128.250.37.3
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list