[mercury-users] errors with typeclasses

david wallin david at wallin.cx
Fri Nov 3 11:53:24 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]).
>

That worked out fine on 'list_add_chromosome/3' & 
'list_remove_chromosome/3', but not when I tried to apply this on 
'list_get_chromosome' :


%
% list_get_chromosome

:- func list_get_chromosome(list_population, int) = C3 <= chromosome(C3).
:- mode list_get_chromosome(in, in) = out.

list_get_chromosome(Population, Index) = Chromosome :-
	Population = list_population(List),
	get_chromosome_helper(List, Index, 'new some_chromosome'(Chromosome)).


%
% get_chromosome_helper

:- pred get_chromosome_helper(list(some_chromosome), int, some_chromosome).
:- mode get_chromosome_helper(in, in, out).


This resulted in the following error message :

gusga.m:088: In type class method implementation:
gusga.m:088:   warning: unresolved polymorphism.
gusga.m:088:   The variable with an unbound type was:
gusga.m:088:       HeadVar__3 :: C3
gusga.m:088:   The unbound type variable(s) will be implicitly
gusga.m:088:   bound to the builtin type `void'.

Of course, the typeclass declaration of 'get_chromosome' looks a bit 
different with a existential type constraint and that mode is 'out' 
instead of 'in' as in the previous functions :

	       some [C3] func get_chromosome(P, int) = C3 => chromosome(C3),
	       mode get_chromosome(in, in) = out is det

confused,


--david.



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