3

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?

blueocean
  • 216
  • 2
  • 11
  • You shouldn't use constraints on data types: http://stackoverflow.com/questions/12770278/typeclass-constraints-on-data-declarations – Tikhon Jelvis Jul 31 '13 at 13:36

1 Answers1

10

Class constraints on data types don't mean anything.

See this related question

Remove the constraint on the data type.

data Mat a = Mat {nexp :: Int, mat :: QT a}
    deriving (Eq, Show)
Community
  • 1
  • 1
Don Stewart
  • 137,316
  • 36
  • 365
  • 468