[mercury-users] Typeclass question

Anthony Lee SENYARD anthls at cs.mu.OZ.AU
Wed May 19 16:21:16 AEST 2004


On Wed, May 19, 2004 at 01:01:16PM +1000, Fergus Henderson wrote:
> 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
> > ].

<snip>

> 	:- type list_of_float ---> +(list(float)).
> 
> 	:- instance mytype(list_of_float, float) where [
> 		(lookup(+L, X, Y) :- list__index0_det(L, X, Y))
> 	].
> 
> Then instead of calling lookup(Foo, ...) you would call lookup(+Foo, ...).

I've been wanting to do the same thing myself and tried out the
above solution with:

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

:- 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))
].

main -->
         { MyArray = array__array([1.0, 1.0, 0.0, 2.0]) },
         { test__lookup(+(MyArray), 2, Result) },
         io__write(Result).

------------------------------------------------------------
and got the following error message:

test.m:047: In predicate `test.main/2':
test.m:047:   type error: unsatisfied typeclass constraint(s):
test.m:047:   test.mytype((test.array_of_float), T).
test.m:047: In predicate `test.main/2':
test.m:047:   warning: unresolved polymorphism.
test.m:047:   The variable with an unbound type was:
test.m:047:       Result: T
test.m:047:   The unbound type variable(s) will be implicitly
test.m:047:   bound to the builtin type `void'.
For more information, try recompiling with `-E'.

Did I do something wrong?  Thanks for any help....

Regards,

Anthony

----------------------------------------------------------------------
Dr Anthony Senyard                      Phone: +61 3 8344-1406
Department of Computer Science          Fax:   +61 3 9348-1184
and Software Engineering                email: anthls at cs.mu.oz.au
University of Melbourne

Office: L.3.19, ICT Building,
111 Barry Street, Parkville, 3010,
Victoria, AUSTRALIA
----------------------------------------------------------------------
--------------------------------------------------------------------------
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