Ok, I'm trying to wrap my head in cool classes, and so I'm trying to define typeclass for geometric vector operations. I managed to get it to work for component +,-,*,/; but I am struggling with a point product.
class GeomVector a where (>+) :: a -> a -> a (>-) :: a -> a -> a (>*) :: a -> a -> a (>/) :: a -> a -> a (>.) :: a -> a -> Double data Vector a = Vec [a] deriving Show instance (Fractional a) => GeomVector (Vector a) where (>+) (Vec u) (Vec v) = Vec $ zipWith (+) uv (>-) (Vec u) (Vec v) = Vec $ zipWith (-) uv (>*) (Vec u) (Vec v) = Vec $ zipWith (*) uv (>/) (Vec u) (Vec v) = Vec $ zipWith (/) uv (>.) (Vec u) (Vec v) = sum $ u >* v
Obviously, my instance definition for (>.) Will not work, because the result is of type Fractional a , not Double .
But I do not know how to get this behavior from the declaration in the class.
I would like to:
class GeomVector [a] where (>.) :: [a] -> [a] -> a
But this is not true because [a] is a type, not a type variable.
I would like to explain this a little better, but I honestly do not understand enough about this. Hopefully the code will make it more obvious what I'm struggling with.
source share