[m-dev.] io__write and arrays

David Glen JEFFERY dgj at cs.mu.oz.au
Tue Apr 8 14:26:42 AEST 1997

Peter Schachte wrote:
> On Tue, 8 Apr 1997, Tyson Richard DOWD wrote:
> > > It would be nice if `io__write' did something a bit more meaningful
> > > for arrays.
> > 
> Actually, there should probably be at least three standard ways to write
> things:  (1) so that it's guaranteed they can be read back in, regardless
> of the operators in force at the time; (2) so they look reasonably good,
> but still reflecting the internal representation; and (3) "pretty,"
> reflecting the entity represented.  These correspond to display, write,
> and print in Prolog.
> But all of this goes beyond the C interface.  These things should all be
> possible for ordinary Mercury types, as well.  I think the place to put
> this facility is in the type definition syntax, or perhaps an extra
> declaration, but not as part of the C interface.

I think type classes provide a nice solution to this. The programmer could
specify how the output functions are to work for a given type, with default
definitions provided.

In a Haskellish syntax:

class Printable a where
    display:: a -> io__state -> io__state
    write:: a -> io__state -> io__state
    print:: a -> io__state -> io__state

    display = io__inbuilt_write
    write   = io__inbuilt_write
    print   = io__inbuilt_write

Here, io__inbuilt_write corresponds to what is now `io__write'. It is
polymorphic, without typeclass a constraint. Using this you could do things

instance Printable Char     --- Just uses the default definitions
instance Printable (Tree234 k v) where
    write = output_tree234_nicely
instance Printable (Uniq_array a) where
    display = uniq_array_display

:- pragma c_code(uniq_array_display(Array::in, IO0::di, IO::uo),

The downside is that a declaration would be required for each type that you
want to be able to print. Perhaps a mechanism which says `Everything is in this class. Here's the default definition which can be overriden' would be
nice. (?)

love and cuddles,

This .sig deliberately left blank

More information about the developers mailing list