[mercury-users] errors with typeclasses

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Nov 3 08:16:17 AEDT 2000


On 02-Nov-2000, david wallin <david at wallin.cx> wrote:
> >
> >The fix is straight-forward:
> >
> >  	:- func list_add_chromosome(list_population,
> >  			    C) = list_population <= chromosome(C).
> >  	:- mode list_add_chromosome(in, in) = out is det.
> >
> >  	list_add_chromosome(PopIn, Chromosome) = PopOut :-
> >  		PopIn = list_population(List),
> >  		PopOut = list_population([some_chromosome(Chromosome) | List]).
> 
> Ok, I did that (but changed the variable name to C1 for clarity). 
> This error seems strange to me (surprise!) since the 
> 'some_chromosome' type declaration constrains C to implement the 
> chromosome typeclass, what's the problem ?
> 
> gusga.m:101: In clause for function `gusga:list_add_chromosome/2':
> gusga.m:101:   in argument 1 of functor `list_population/1':
> gusga.m:101:   in argument 1 of functor `./2':
> gusga.m:101:   in unification of argument
> gusga.m:101:   and term `some_chromosome(Chromosome)':
> gusga.m:101:   type error in argument(s) of functor `some_chromosome/1'.
> gusga.m:101:   Argument 1 (Chromosome) has type `C1',
> gusga.m:101:   expected type was `C'.

Ah.  Sorry, that is a tricky one, and I led you astray.
The problem here is that we're attempting to use the functor
`some_chromosome/1' is as the constructor for an existentially
quantified type, but in such situations the compiler requires
that you insert `new ' in front of the constructor name.

So the line

  		PopOut = list_population([some_chromosome(Chromosome) | List]).

should be

  		PopOut = list_population(['new some_chromosome'(Chromosome) | List]).

There's certainly a lot of room for improvement in that compiler error
message.  I think it is probably not too hard to make the compiler
check if the functor involved is existentially typed, and if so to
give you some hints about possible causes of the problem.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "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