
On Mon, Mar 29, 2004 at 06:00:57PM +0200, Henning Thielemann wrote:
Thus I setup a type constructor VectorSpace in the following way:
module VectorSpace where
class VectorSpace v where zero :: v a add :: v a -> v a -> v a scale :: a -> v a -> v a
I haven't added context requirements like (Num a) to the signatures of 'zero', 'add', 'scale' because I cannot catch all requirements that instances may need.
The problematic part is the 'scale' operation because it needs both a scalar value and a vector. Without the 'scale' operation 'v' could be simply a type (*) rather than a type constructor (* -> *).
Right. I recommend you use multi-parameter type classes, with a type of the scalars and the type of the vectors. For the method you're using, you need to add a 'Num a' context. You say that you 'cannot catch all requirements that instances may need', but certainly any instance will need that context. If you use multi-parameter type classes, then in your instance declaration you can specify exactly what requirements you need. For instance:
class VectorSpace v a where zero :: v add :: v -> v -> v scale :: a -> v -> v
instance VectorSpace IntArray Int where ...
instance (Num a) => VectorSpace (GenericArray a) a where ...
Peace, Dylan