[mercury-users] clause/1, functor/3, arg/3 ...
fjh at cs.mu.oz.au
Wed Jul 9 00:24:00 AEST 1997
Ralph Becket, you wrote:
> Fergus Henderson wrote on 8 Jul:
> > ...
> > For any given type, you can use a switch and pattern matching,
> > but if you want to write a single predicate that works for every type,
> > then you need something like arg/3 & functor/3.
> > An immediate application of this is the io__print, io__write, io__read,
> > io__write_binary, and io__read_binary predicates that will be included
> > in the next release. The io__print predicate subsumes the functionality
> > of io__write_int, io__write_string, io__write_float, etc.
> io__write_many seems to be sufficient for all that stuff.
io__write_many works for int, string, float, and char, but
it doesn't work for the "etc.".
> it's nice to have `print whatever' procedures in an interactive/
> debugging environment, I'm not sure I see what it buys you otherwise.
It saves you from having to write the code for `write_foo'.
(You also avoid having to write the code for `read_foo',
`write_binary_foo', `read_binary_foo', `foo_to_string',
`foo_from_string', `hash_foo', `copy_foo', `compare_foo',
and so on. It adds up.)
> Why not have a programmer extensible overloaded function with
> universal scope, for example:
> :- func fmt(my_thing::in)=string::out.
> fmt(X) = S :-
> X = case1(...), string__format("...", [...], S);
> Programmers would similarly be expected to provide unmarshalling
> procedures for reading things in.
Well, when we have added support for Haskell-style type classes
(which is very high on our agenda), then we will do exactly that.
However, even with type classes, it is still very helpful if the compiler
can provide the marshalling & unmarshalling procedures automatically.
That's why Haskell lets you write "deriving Eq", "deriving Text", etc.,
on a type definition. When we extend Mercury to support type classes,
we'll do a similar thing: user-defined types will be automatically
made instances of certain type classes [such as the one(s) for arg &
functor, read, and write], unless the user specifies otherwise.
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 22.214.171.124 | -- the last words of T. S. Garp.
More information about the users