
Hi,
On Wed, Oct 8, 2008 at 2:31 PM, Tobias Bexelius
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...
{-# 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
According to the definition of Mult the parameter 'c' from above should be uniquely determined by a pair of types 'a' and 'b'. Now, lets say we have a type NumVec that instantiates both the Vec class and the Num as in instance Num (NumVec a) where instance Vec NumVec a where Then according to the instance declaration instance (Num x) => Mult x x x where ... (NumVec a) (NumVec a) (NumVec a) instantiates the class. On the other hand looking at the instance declaration: instance (Vec a x) => Mult (a x) (a x) x where ... (NumVec a) (NumVec a) a , is also an instance why the fun dep (a b -> c) is violated. Regards, Joel