simple lesson wanted for overloading list
Zuurbier, E. - AMSXE
Erik.Zuurbier@klm.nl
Mon, 14 Feb 2000 09:10:06 +0100
Andrew Jonathan Fine wrote:
=============
Suppose I have the following abstract type:
:: Greek = Alpha | Beta | Gamma | Delta | Epsilon
I want to do
Start :: Bool
Start = isMember Alpha [Alpha Beta Gamma]
but I get the error that isMember has no instance
How to I overload the StdList operators for an abstact type?
============
The type checker's error messages are a bit misleading here.
What it wants to say is that somewhere inside the implementation of isMember
a class is used of which there is no instance for Greek.
If you check isMember's type, you can get a hint:
isMember :: a !.[a] -> .Bool | Eq a
The part after the vertical bar means that for isMember to work on a given
type a,
a must be a member of class Eq, or in other words, == must be defined for a.
In this case a is Greek, so what you can do is:
instance == Greek
where (==) Alpha Alpha = True
(==) Beta Beta = True
(==) Gamma Gamma = True
(==) Delta Delta = True
(==) Epsilon Epsilon = True
(==) _ _ = False
Then your example Start function should work. If you find this cumbersome, I
agree.
maybe one time Clean will be able to automatically generate instances of ==
and <
for certain types.
By the way, Greek is an algebraic type, not an abstract type. And also: you
need to
put comma's between list members, like:
Start = isMember Alpha [Alpha, Beta, Gamma]
Regards Erik Zuurbier