newtype Matrix (w :: Nat) (h :: Nat) v a where
Matrix :: v a -> Matrix w h v a
(其中VA爲Data.Vector.Generic.Vector v a
,你怎麼說?)
和:
instance Foldable (Matrix w h v) where
foldMap = foldMapTree
foldMapTree :: (Vector v a, Monoid m) => (a -> m) -> Matrix w h v a -> m
GHC抱怨:
Matrix.hs:55:15:
Could not deduce (Vector v a) arising from a use of ‘foldMapTree’
from the context (Monoid m)
更改爲:
instance Vector v a => Foldable (Matrix w h v) where
foldMap = foldMapTree
給出:
Matrix.hs:54:10:
Variable ‘a’ occurs more often than in the instance head
in the constraint: Vector v a
(Use UndecidableInstances to permit this)
使用UndecidableInstances
,因爲它打破了幾乎一切並沒有多大幫助......有可能是一個簡單的解決這個問題......其他答案建議UndecidableInstances
不是「壞「本身。但顯然我無法得到這個工作...
添加'UndecidableInstances'不應該破壞現有的代碼,它只允許更多的程序。 – augustss 2014-09-23 08:59:07
您無法將'Matrix nm'作爲'Functor'的實例,原因與您無法[[Set]]相同(https://stackoverflow.com/questions/19177125/sets-functors-and-eq混淆)或[排序二叉樹](https://stackoverflow.com/questions/20296936/is-it-possible-to-create-a-functor-instance-for-sorted-binary-trees-in-haskell) 「Functor」的一個實例。 – user2407038 2014-09-23 09:42:34
您有使用'Data.Vector.Generic'的特殊原因嗎?如果你將自己限制在'Data.Vector'中,我認爲它會簡單得多。 – 2014-09-23 09:55:32