[Haskell-beginners] how to instance a class

Thomas Davie tom.davie at gmail.com
Thu Feb 26 03:12:54 EST 2009


On 26 Feb 2009, at 09:02, Daneel Yaitskov wrote:

> --------------
> But I can't instance my own class:
>
> class (Num a) => SVect a where
> (***) :: Num b => a -> b -> a
>
> instance (Num t) => SVect (Vertex3 t) where
> (Vertex3 x y z) *** c = Vertex3 (c*x) (c*y) (c*z)
>
> GHC posts about the error:
>
> surface.hs:107:36:
>    Couldn't match expected type `b' against inferred type `t'
>      `b' is a rigid type variable bound by
>          the type signature for `***' at surface.hs:103:14
>      `t' is a rigid type variable bound by
>          the instance declaration at surface.hs:105:14
>    In the second argument of `(*)', namely `x'
>    In the first argument of `Vertex3', namely `(c * x)'
>    In the expression: Vertex3 (c * x) (c * y) (c * z)
> ------------------------
>
> I understand the "c" argument must have type as x,y and z, but
> I don't know what need to do.

I don't immediately see what is causing your error, but try  
downloading the VectorSpace package off hackage – it may save you a  
lot of wheel reinventing here.

Bob


More information about the Beginners mailing list