[mercury-users] errors with typeclasses

david wallin david at wallin.cx
Thu Nov 2 02:34:49 AEDT 2000


Ok, I have done some changes (inspired by your replies) and the error 
list is now shorter. But let me explain a bit what I'm trying to do:

I want the population typeclass to contain chromosomes. A population 
should probably contain the same kind of chromosomes (but I'm not 
sure how to make this constraint) but this is not really that 
important right now.
Basically, I want to use typeclasses in the same way as I use 
interfaces in Java but without the limitations.

I want instances of population to be any structure and if possible, I 
would like to be able to use already existing structure 
implementations (ie not tailor-made to be used with the population 
typeclass).

The remaining error messages (at the end of this email), would I need 
multi-parameter typeclasses or constructor classes to get rid of this 
?


Ok, here's the code and error messages:

	:- typeclass chromosome(C)
		where [
		       func mutate_chromosome(C) = C,
		       func gene(C, int) = G <= gene(G),
		       pred set_gene(C, G) <= gene(G)
		      ].


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

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


	:- instance population(list(D)) <= chromosome(D)
		where [
/*line 83*/	       pred(add_chromosome/3) is list_add_chromosome,
/*line 84*/	       pred(remove_chromosome/3) is list_remove_chromosome,
		       func(get_chromosome/2) is list_get_chromosome
		      ].


%
% list_add_chromosome

	:- pred list_add_chromosome(list(T), T, list(T)) <= chromosome(T).
	:- mode list_add_chromosome(di, in, uo) is det.

	list_add_chromosome(ListIn, Chromosome, ListOut) :-
		ListOut = [Chromosome | ListIn].


(I tried without the 'chromosome(T)' constraint with the same
effect).


gusga.m:084: In clause for type class method implementation:
gusga.m:084:   in argument 2 of call to predicate `list_remove_chromosome/3':
gusga.m:084:   type error: variable `HeadVar__2' has type `C2',
gusga.m:084:   expected type was `D'.
gusga.m:083: In clause for type class method implementation:
gusga.m:083:   in argument 2 of call to predicate `list_add_chromosome/3':
gusga.m:083:   type error: variable `HeadVar__2' has type `C1',
gusga.m:083:   expected type was `D'.
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