Re: [Haskell-cafe] Context for type parameters of type constructors

I didn't get the point yet why the context for 'data' is not sufficient for the 'instance' method definition.
Not sure who's relpy you're replying to, but I thought my response was pretty clear (I would though)... The problem is that to define 'zero' the type of the 'zero' must unify with the vector type. If you define vector operations as (v a) then a is a polymorphic variable. However the definition of zero implies that a is an Integral .. these are not the same type: (forall a . a) is not (forall a . Integral a => a) and thats where the type error comes from. You have two choices, drop the definition of zero, or use multi-parameter type classes. You could get rid of zero and have: class VectorSpace v where set :: Num a => a -> v a add :: Num a => v a -> v a -> v a scale :: Num a => a -> v a -> v a Should do the trick! (zero becomes "set 0") Regards, Keean.

On Tue, 30 Mar 2004, MR K P SCHUPKE wrote:
I didn't get the point yet why the context for 'data' is not sufficient for the 'instance' method definition.
Not sure who's relpy you're replying to,
This was clearly an answer to Dylan Thurston.
but I thought my response was pretty clear (I would though)...
I'm not sure you got the point of my question. :-(
You could get rid of zero and have:
class VectorSpace v where set :: Num a => a -> v a add :: Num a => v a -> v a -> v a scale :: Num a => a -> v a -> v a
Should do the trick! (zero becomes "set 0")
This wouldn't change much because the compiler also complains about the other instance definitions, e.g. instance VectorSpace VList where add (VList x) (VList y) = VList (zipWith (+) x y) is rejected because the compiler don't believes that the type of x and y is in class Num. My question was why he doesn't believe that. My definition data (Num a) => VList a = VList [a] clearly states that VLists will ever get types of class Num as parameters.

My question was why he doesn't believe that. My definition data (Num a) => VList a = VList [a] clearly states that VLists will ever get types of class Num as parameters.
Ah. Constraints on datatype declarations are a misfeature of Haskell, and
have no useful effect. You shouldn't use them.
--KW 8-)
--
Keith Wansbrough

On Tue, 30 Mar 2004, Keith Wansbrough wrote:
My question was why he doesn't believe that. My definition data (Num a) => VList a = VList [a] clearly states that VLists will ever get types of class Num as parameters.
Ah. Constraints on datatype declarations are a misfeature of Haskell, and have no useful effect. You shouldn't use them.
Ok. Btw. ghc suggests: VectorSpace.lhs:37: Could not deduce (Num a) from the context (VectorSpace VList) arising from the literal `0' at VectorSpace.lhs:37 Probable fix: Add (Num a) to the class or instance method `zero' ^^^^^^^^^^^^^^^ I know how to add context information to the signature in a class definition. But how can I add context information at the point of instantation of a method?
participants (3)
-
Henning Thielemann
-
Keith Wansbrough
-
MR K P SCHUPKE