I'm quite new to Haskell, and I ran into this bug when trying to compile Frag.
src/AFRPVectorSpace.hs:51:25:
Could not deduce (Eq a) arising from a use of `/='
from the context (VectorSpace v a)
bound by the class declaration for `VectorSpace'
at src/AFRPVectorSpace.hs:(32,1)-(53,23)
Possible fix:
add (Eq a) to the context of
the class declaration for `VectorSpace'
In the expression: nv /= 0
In the expression:
if nv /= 0 then v ^/ nv else error "normalize: zero vector"
In an equation for `normalize':
normalize v
= if nv /= 0 then v ^/ nv else error "normalize: zero vector"
where
nv = norm v
Relevent code:
class Floating a => VectorSpace v a | v -> a where
zeroVector :: v
(*^) :: a -> v -> v
(^/) :: v -> a -> v
negateVector :: v -> v
(^+^) :: v -> v -> v
(^-^) :: v -> v -> v
dot :: v -> v -> a
norm :: v -> a
normalize :: v -> v
v ^/ a = (1/a) *^ v
negateVector v = (-1) *^ v
v1 ^-^ _ = v1 ^+^ v1 -- (negateVector v2)
norm v = sqrt (v `dot` v)
normalize v = if nv /= 0 then v ^/ nv else error "normalize: zero vector"
where
nv = norm v
My first guess is that I need to add a Deriving Eq
or something of that sort, but I'm not sure what exactly I need to do.
I'd guess you'd need to have class (Eq a,Floating a) => VectorSpace v a | v -> a
if you want to use /=
for a
in your default implementations.
Second alternative is to remove normalize
from the class and make it an ordinary function instead.
Third alternative is to add the constraint to the type of the normalize
, making it Eq a => v -> v
.
Prior to ghc 7.4.1, the Num a
class had Eq a
constraint, so any Num a
also had Eq a
. Floating a
has a constraint Num a
, so therefore anything Floating a
was also Eq a
.
However, this changed with 7.4.1, where the Eq a
constraint (as well as the Show a
constraint) was removed from the Num
class. This is why the code isn't working anymore.
So the solution to the problem is exactly what aleator gave: Add the Eq a
constraint explicitly to the VectorSpace
class.
Alternatively, you may want to download an older version of ghc (eg 6.8 based on the wiki notes). That version should compile the program without any changes. Then you can update the code to get it working with a newer version of ghc if you so desire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With