[mercury-users] Typeclass question

Fergus Henderson fjh at cs.mu.OZ.AU
Wed May 19 13:01:16 AEST 2004


On 19-May-2004, m.spain at ugrad.unimelb.edu.au <m.spain at ugrad.unimelb.edu.au> wrote:
> Actually what I would really like to be able to do is to have a
> type T1(T2) as the parameter to a typeclass.  An example of the
> type of thing I'd like to be able to do is:
> 
> :- typeclass mytype(T1(T2)) where [
>         pred lookup(T1(T2), int, T2),
>         mode lookup(in, in, out) is det
> ].

You can have

	:- typeclass mytype(T1, T2) where [
		pred lookup(T1, int, T2),
		mode lookup(in, in, out) is det
	].

> :- instance mytype(list(float)) where [
>         pred(lookup/3) is list__index0_det
> ].
> 
> :- instance mytype(array(float)) where [
>         pred(lookup/3) is array__lookup
> ].
> 
> :- instance mytype(array(int)) where [
>         pred(lookup/3) is array__lookup
> ].
> 
> Is there any way to do this in Mercury?

Not exactly as shown.  But there are work-arounds.
Given the above class declaration, you can't write

	:- instance mytype(list(float), float) where [
		pred(lookup/3) is list__index0_det
	].
	:- instance mytype(array(float), float) where [
		pred(lookup/3) is array__lookup
	].
	:- instance mytype(array(int), int) where [
		pred(lookup/3) is array__lookup
	].

But you can write something like this instead:

	:- type list_of_float ---> +(list(float)).
	:- type array_of_float ---> +(array(float)).
	:- type array_of_int ---> +(array(int)).

	:- instance mytype(list_of_float, float) where [
		(lookup(+L, X, Y) :- list__index0_det(L, X, Y))
	].
	:- instance mytype(array_of_float, float) where [
		(lookup(+A, X, Y) :- array__lookup(A, X, Y))
	].
	:- instance mytype(array_of_int, int) where [
		(lookup(+A, X, Y) :- array__lookup(A, X, Y))
	].

Then instead of calling lookup(Foo, ...) you would call lookup(+Foo, ...).

-- 
Fergus Henderson                    |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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