Simon writes:
> You correctly say that you want a still more specific type. Without
> understanding your application better I am nervous about recommending
> this, but you *can* get what you say you want by using a multi-parameter class
>
> class Weird a b c where
> ...
> f2 :: Weirder a b -> c -> Wierder a b
> ...
>
> and now use the instance
>
> instance Weird d e (d,e) where
> f2 w (x,y) = Weirdest x y
>
> But multi-parameter type classes aren't a panacea. It may be that there's a
> more direct
> way to achieve what you really want.
What I really want is to define a pool of reusable resources. So
I want a class declaration something like:
class ResourcePool p where
newp :: p -> e -> p -- adds new element e to the resource pool p
As one instance of this class, I want a resource pool where the type "e"
becomes a pair consisting of an Id and a value (x), as follows:
data Pool id x = EmptyPool | NewPool id x
instance ResourcePool (Pool id x) where
newp p (id,x) = NewPool id x
This yields the type checking error
*** Expression : newp
*** Declared type : Pool a b -> c -> Pool a b
*** Inferred type : Pool a b -> (a,b) -> Pool a b
I am interested in this separation between class and instance because
I will want several instances of ResourcePool, all of which share
some properties of the newp operator, but only some of which have
elements that have an id and a value. In addition, some pools will
be implemented as association lists, where finding the element identified
by an id is done by a linear search (using normal Haskell list operations),
while other pools may be implemented as hash table lookups.
On the subject of multi-parameter classes, the hugs system does not
seem to permit them. Is the story different on GHC?
Cheers
Peter White