Hi,

>>> class Aggregate a where
>>>  toList::(Num b)=>a->[b]
>>>  fromList::(Num b)=>[b]->a
>>
>>> data MyAgg =Agg Int Int
>>> instance Aggregate MyAgg where
>>>  toList (Agg x y) = [x,y]
>>>  fromList [x,y] = (Agg x y)
>>
>> Hugs won't compile this because it says the declared type is too general.
>> Any function that requires an Aggregate can use MyAgg.  Why am I
>> getting an error?
> The type of toList in the class declaration claims that it can
> produce a list of any type in Num, but with MyAgg you can only get
> a list of Int.  So the type of toList in the class is more general
> than the one you have given.

Recently I've got the same error in a similar case and I didn't solve the
problem. Usually a dependence between the types a and b can be inferred from the
functions toList and fromList defined in an instance of Aggregate.
To keep the class as general as possible, one can't describe such dependence.
I thing multi-parameter type classes would do it, but they aren't allowed in
Haskell. So what should one do?


Martin Stein


Reply via email to