[mercury-users] Typeclass question
Fergus Henderson
fjh at cs.mu.OZ.AU
Mon May 31 06:45:22 AEST 2004
On 19-May-2004, Anthony Lee SENYARD <anthls at cs.mu.OZ.AU> wrote:
> On Wed, May 19, 2004 at 01:01:16PM +1000, Fergus Henderson wrote:
> > :- 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:
...
> 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....
Mercury's type class system doesn't have functional dependencies,
so the compiler can't infer that calling lookup on a value of type
array_of_float will give you back a result of type float.
You need to disambiguate the types, e.g. by using an explicit type
annotation. For example, change the call to io__write to
io__write(Result `with_type` float).
or
io__write_float(Result).
--
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