Both Matrix
and Vector
构造函数有种类*->*
,所以它们看起来像值构造函数。但是当我尝试类似的事情时
instance Functor Vector a where
fmap g ( Vector a ) = Vector ( g a )
我收到此错误:
Not in scope: data constructor `Vector'
这是有道理的,因为我无法使用let v = Vector [1..3]
无论如何。
但是检查源代码我发现 Matrix 和 Vector 构造函数都是从各自的模块导出的:
Vector.hs
module Data.Packed.Vector (
Vector,
fromList, (|>), toList, buildVecto..
) where
Matrix.hs
module Data.Packed.Matrix (
Element,
Matrix,rows,cols...
) where
Dido 代表应用函子、单子等。
As 康拉德·帕克 https://stackoverflow.com/users/744300/conrad-parker说,我们需要Storable
实例。
使用最近的 ghc 扩展,我们可以定义一个更通用的 Functor':
{-# LANGUAGE ConstraintKinds, TypeFamilies #-}
import Numeric.LinearAlgebra
import Foreign.Storable(Storable)
import GHC.Exts (Constraint)
class Functor' c where
type Ok c u v :: Constraint
type Ok c u v = ()
fmap' :: Ok c u v => (u -> v) -> c u -> c v
instance Functor' Vector where
type Ok Vector u v = (Storable u, Storable v)
fmap' = mapVector
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)