lundi 26 janvier 2015

Overlapping Type Class Instances in Haskell


I'm writing a geometry library in Haskell that makes good use of type classes and the type checker to help enforce the validity of geometric operations. However the compiler complains that I have overlapping instances. Below is a minimal example, where I have replaced vector types with Int to simplify the code.



{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}

class PointSet a where
liesIn :: Int -> a -> Bool

class TopVec a where
linearSpan :: a -> Line

data Line = Line Int Int

instance PointSet Line where
point `liesIn` _ = True

class BoundLinear a where
direction :: a -> Int

instance (BoundLinear bdl) => TopVec bdl where
linearSpan bdline = Line 0 0

instance (BoundLinear bdl) => PointSet bdl where
point `liesIn` bdline = point `liesIn` (linearSpan bdline)


The compiler complains that I have overlapping instances for PointSet Line, arising from the use of liesIn. The matching instances are PointSet Line and (BoundLinear bdl) => PointSet bdl. But this doesn't make sense, because the the only instances of PointSet should be Line and any instance of BoundLinear. There should be no conflict because Line is not an instance of BoundLinear. Can anybody throw some light on this?





Aucun commentaire:

Enregistrer un commentaire