[mercury-users] errors with typeclasses

david wallin david at wallin.cx
Thu Nov 2 09:08:24 AEDT 2000


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 [
		       func mutate_chromosome(C) = C,
		       some [G] func gene(C, int) = G => gene(G),
		       pred set_gene(C, G) <= gene(G)
		      ].


	:- typeclass population(P)
		where [
		       func add_chromosome(P, C1) = P <= chromosome(C1),
		       mode add_chromosome(in, in) = out is det,

		       func remove_chromosome(P, C2) = P <= chromosome(C2),
		       mode remove_chromosome(in, in) = out is det,

		       some [C3] func get_chromosome(P, int) = C3 =>
				chromosome(C3),
		       mode get_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,
87:		       func(remove_chromosome/2) is list_remove_chromosome,
88:		       func(get_chromosome/2) is list_get_chromosome
		      ].


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

(I'll just skip the 'list_remove_chromosome' function since it's the 
same error as above)

	%
	% list_get_chromosome

	:- func list_get_chromosome(list_population, int) = some_chromosome.
	:- mode list_get_chromosome(in, in) = out.

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

gusga.m:088: In clause for type class method implementation:
gusga.m:088:   unsatisfiable typeclass constraint(s):
gusga.m:088:   `gusga:chromosome((gusga:some_chromosome))'.
gusga.m:087: In clause for type class method implementation:
gusga.m:087:   in unification of variable `HeadVar__3'
gusga.m:087:   and term `list_remove_chromosome(HeadVar__1, HeadVar__2)':
gusga.m:087:   type error in argument(s) of functor `list_remove_chromosome/2'.
gusga.m:087:   Argument 2 (HeadVar__2) has type `C2',
gusga.m:087:   expected type was `(gusga:some_chromosome)'.
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)'.
For more information, try recompiling with `-E'.


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