Reputation: 216
I have this piece of code in Haskell that refuses to compile:
data (Eq a, Num a, Show a) => Mat a = Mat {nexp :: Int, mat :: QT a}
deriving (Eq, Show)
data (Eq a , Show a ) => QT a = C a | Q (QT a ) (QT a ) (QT a ) (QT a )
deriving (Eq, Show)
cs:: (Num t) => Mat t -> [t]
cs(Mat nexp (Q a b c d)) =(css (nexp-1) a c)++(css (nexp-1) b d)
where
css 0 (C a) (C b) = (a-b):[]
css nexp (Q a b c d) (Q e f g h) = (zipWith (+) (css (nexp-1) a c) (css (nexp-1) e g))++(zipWith (+)(css (nexp-1) b d) (css (nexp-1) f h))
I have this error:
Could not deduce (Show t) arising from a use of `Mat'
from the context (Num t)
bound by the type signature for cs:: Num t => Mat t -> [t]
I searched online and found many similar questions, but nothing seems to get close to my problem. How can I make this code work?
Upvotes: 3
Views: 471
Reputation: 137957
Class constraints on data types don't mean anything.
Remove the constraint on the data type.
data Mat a = Mat {nexp :: Int, mat :: QT a}
deriving (Eq, Show)
Upvotes: 10