[mercury-users] Re: default methods (was: boolean expressions from semidet predicates)
David Overton
david at overtons.id.au
Sun May 21 07:47:46 AEST 2006
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.
David
--------------------------------------------------------------------------
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