[m-dev.] io__write and arrays

Peter Schachte pets at cs.mu.oz.au
Tue Apr 8 15:31:01 AEST 1997


> 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
> 
> 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. (?)

Why not go a step further:  any type class which has defaults for all
(what are they called?) type class parametric predicates/functions
automatically applies to all types?  Can you think of a case where you
define defaults for all <mouthfull> when you don't want that type class to
apply to all types?

Hmmmm.  Just thought of a bit of a problem here, though.  What happens if
the user declares a C type and doesn't provide a write, display or print? 
Does display then report a runtime type error when someone tries to
display one?  You'd really like to create a type class displayable that
automatically includes all atomic Mercury terms plus Mercury terms
composed of only displayable Mercury terms, plus any non-mercury terms
that have a display operation (method, really).  Can you do that with type
classes?


-Peter Schachte      URL:  http://www.cs.mu.oz.au/~pets/
pets at cs.mu.OZ.AU     PGP:  finger pets at 128.250.37.150 for key
    [A computer is] like an Old Testament god, with a lot of rules
    and no mercy.  -- Joseph Campbell




More information about the developers mailing list