3

Haskell には、コンパイルを拒否する次のコードがあります。

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))

このエラーがあります:

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]

オンラインで検索したところ、同様の質問がたくさん見つかりましたが、私の問題に近づくものはないようです。このコードを機能させるにはどうすればよいですか?

4

1 に答える 1

10

データ型に対するクラスの制約は何の意味もありません。

この関連する質問を参照してください

データ型の制約を取り除きます。

data Mat a = Mat {nexp :: Int, mat :: QT a}
    deriving (Eq, Show)
于 2013-07-31T13:35:10.670 に答える