[mercury-users] Re: default methods (was: boolean expressions from semidet predicates)

Jonathan Morgan jonmmorgan at gmail.com
Mon May 22 13:59:49 AEST 2006


On 5/21/06, David Overton <david at overtons.id.au> wrote:
> On 20/05/06, Jonathan Morgan <jonmmorgan at gmail.com> wrote:> The point I am making is that a typeclass system with defaults allows> implementors to define two methods in terms of each other, leaving the> user to choose which of the two operations they define.  Sometimes> they will want to define both, if they can implement them more> efficiently.  This cannot be done with Mercury's typeclass-system, and> I don't see that it would hurt the type-system to include them.
> You can get the equivalent of Haskell's default methods by doingsomething like this:
>         :- typeclass eq_class(T) where [                pred eq(T::in, T::in) is semidet,               pred neq(T::in, T::in) is nondet        ].
>         :- pred default_neq(T, T) <= eq_class(T).       :- mode default_neq(in, in) is semidet.
>         default_neq(A, B) :- not eq(A, B).
>
> This provides a default implementation of neq/2 method which can beused by any instance, e.g.:
>         :- instance eq_class(int) where [               pred(eq/2) is eq_int,           pred(neq/2) is default_neq      ].
>         :- pred eq_int(int::in, int::in) is semidet.    eq_int(A, B) :- A = B.
> So the only real difference from Haskell is that you have to explicitly mentionthe use of the "default" method in the instance declaration.  But this is not abad thing, IMHO.

Thanks for suggesting that: it wouldn't have occurred to me to do
something like that, but it makes sense.  You are still left with the
problem that if two default routines are mutually defined in terms of
each other and the user chooses both defaults the program will crash,
but that shouldn't be a problem.  Also, the default methods would have
to be provided and documented - in a language with default methods
they would just be taken by default.

However, I think the current method is best for two reasons:

1. It will require no work to change it.
2. Forcing the user to explicitly specify that they are using the
defaults is probably a good thing, though it may be more verbose.

Jon

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list