[Haskell-cafe] Functional dependencies and incoherent instances

Tobias Bexelius tobias.bexelius at avalanchestudios.se
Wed Oct 8 08:31:01 EDT 2008


Hi,

Im trying to overload a multiplication operator for scalars and vectors,
but keep running into the error message "Functional dependencies
conflict". What I think is going on is that the dependency check doesn't
work with incoherent (or overlapping) instances. In the example below,
the two instances of Mult are overlapping. What I want is the vector
version to be used for vectors and the scalar version used for numbers,
even if a vector-type is an instance of the Num-class (I believe
-fallow-incoherent-instances would make that kind of choise for me,
right?).

Im using the Visual Studio plugin Visual Haskell, and thus GHC version
6.6. Otherwise I think associated types might have worked better for
this...

Regards
Tobias Bexelius



{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances
-fallow-incoherent-instances #-}

data V2 a = V2 a a

class Vec v a where
    dot :: v a -> v a -> a
instance Num a => Vec V2 a where
    V2 a1 a2 `dot` V2 b1 b2 = a1*b1+a2*b2

class Mult a b c | a b -> c where
	(*.) :: a -> b -> c
instance (Num x) => Mult x x x where (*.) = (*)
instance (Vec a x) => Mult (a x) (a x) x where (*.) = dot


More information about the Haskell-Cafe mailing list