[mercury-users] errors with typeclasses

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Nov 2 13:12:18 AEDT 2000


On 02-Nov-2000, david wallin <david at wallin.cx> wrote:
> Round three... (perhaps someday this code will actually compile)
> 
> Ok, thank you Ralph, I have applied your ideas, I took the 
> existential types approach although its a bit more liberal than 
> necessary.
> 
> Here's the relevant code with the changes :
> 
> 
> 	:- typeclass chromosome(C) where [...]
> 	:- typeclass population(P)
> 		where [
> 		       func add_chromosome(P, C1) = P <= chromosome(C1),
> 		       mode add_chromosome(in, in) = out is det,
>		       ...
> 	      ].
> 
> 	:- type some_chromosome --->
> 		some [C] some_chromosome(C) => chromosome(C).
> 
> 	:- type list_population --->
> 		list_population(list(some_chromosome)).
> 
> 	:- instance population(list_population)
> 		where [
> 86:		       func(add_chromosome/2) is list_add_chromosome,
> 		       ...
> 		      ].
> 
> 	:- func list_add_chromosome(list_population,
> 			    some_chromosome) = list_population.
> 	:- mode list_add_chromosome(in, in) = out is det.
> 
> 	list_add_chromosome(PopIn, Chromosome) = PopOut :-
> 		PopIn = list_population(List),
> 		PopOut = list_population([Chromosome | List]).
...
> gusga.m:086: In clause for type class method implementation:
> gusga.m:086:   in unification of variable `HeadVar__3'
> gusga.m:086:   and term `list_add_chromosome(HeadVar__1, HeadVar__2)':
> gusga.m:086:   type error in argument(s) of functor `list_add_chromosome/2'.
> gusga.m:086:   Argument 2 (HeadVar__2) has type `C1',
> gusga.m:086:   expected type was `(gusga:some_chromosome)'.

The problem here is just that you forgot to unwrap the `some_chromosome' type
at the right point.  The typeclass interface has type

	func add_chromosome(P, C1) = P <= chromosome(C1),

and your instance declaration gives P = list_population,
but you declared list_add_chromosome to have type

 	:- func list_add_chromosome(list_population,
 			    some_chromosome) = list_population.

rather than

 	:- func list_add_chromosome(list_population, C) = list_population
			<= chromosome(C).

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

Likewise for the other methods.

-- 
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