[Haskell] Instance declaration of classes with method type constraints

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Thu Jun 30 08:18:38 EDT 2005


Johan Holmquist <johho590 at student.liu.se> writes:

> > class Num b => Rect a b | a -> b where
> > instance Rect IRect Int where
> 
> However, I haven't been able to make PRect an instance of this class (with 
> extensions). I might not have grasped this yet, but I came to think; if the 
> old class declaration would say that "width" and "height" returns something 
> with unfixed type in the "Num" class, then wouldn't it be possible to make 
> PRect an instance of that class (since PRect has a type parameter)?
> 
> Something like this:
> 
> class Rect a where
> 	width  :: (Num b) => a -> b
> 	height :: (Num b) => a -> b
> 
> data Num a => PRect a = PRect (a, a) (a, a) deriving (Eq, Show)
> instance Rect PRect a where ...

I think this is just a bracketting problem.  With the second defn of
class Rect above, you probably mean

  instance Rect (PRect a) where ...

whereas if you were intending the earlier defn of class Rect, with
two type parameters, then you may mean

  instance Rect (PRect a) a where ...

Regards,
    Malcolm


More information about the Haskell mailing list