首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用类型约束时无法推断错误

使用类型约束时无法推断错误
EN

Stack Overflow用户
提问于 2014-05-17 22:27:53
回答 1查看 683关注 0票数 1

我正在使用Haskell实现一个线性代数示例。但是,我在声明magnitude函数时遇到了问题。

我的执行情况如下:

代码语言:javascript
复制
magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
magnitude = sqrt $ Data.Foldable.foldr1 (+) $ fmap (^2)

其思想是magnitude将接受Vec2DVec3DVec4D,并返回其组件的平方和的平方根。

三种向量类型中的每一种都实现了FunctorFoldable。例如,

代码语言:javascript
复制
newtype Vec2D = Vec2D (a, a) deriving (Eq, Show)
instance Functor Vec2D where
    fmap f (Vec2D (x, y)) = Vec2D (f x, f y)
instance Foldable Vec2D where
    foldr f b (Vec2D (x, y)) = f x $ f y b

然而,我收到了许多错误:

代码语言:javascript
复制
LinearAlgebra.hs:9:13:
    Could not deduce (Floating (t a -> a)) arising from a use of `sqrt'
    from the context (Foldable t, Functor t, Floating a)
      bound by the type signature for
                 magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
      at LinearAlgebra.hs:8:14-60
    Possible fix: add an instance declaration for (Floating (t a -> a))
    In the expression: sqrt
    In the expression: sqrt $ Data.Foldable.foldr1 (+) $ fmap (^ 2)
    In an equation for `magnitude':
        magnitude = sqrt $ Data.Foldable.foldr1 (+) $ fmap (^ 2)

LinearAlgebra.hs:9:20:
    Could not deduce (Foldable ((->) (t a -> a)))
      arising from a use of `Data.Foldable.foldr1'
    from the context (Foldable t, Functor t, Floating a)
      bound by the type signature for
                 magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
      at LinearAlgebra.hs:8:14-60
    Possible fix:
      add an instance declaration for (Foldable ((->) (t a -> a)))
    In the expression: Data.Foldable.foldr1 (+)
    In the second argument of `($)', namely
      `Data.Foldable.foldr1 (+) $ fmap (^ 2)'
    In the expression: sqrt $ Data.Foldable.foldr1 (+) $ fmap (^ 2)

LinearAlgebra.hs:9:41:
    Could not deduce (Num (t a -> a)) arising from a use of `+'
    from the context (Foldable t, Functor t, Floating a)
      bound by the type signature for
                 magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
      at LinearAlgebra.hs:8:14-60
    Possible fix: add an instance declaration for (Num (t a -> a))
    In the first argument of `Data.Foldable.foldr1', namely `(+)'
    In the expression: Data.Foldable.foldr1 (+)
    In the second argument of `($)', namely
      `Data.Foldable.foldr1 (+) $ fmap (^ 2)'
Failed, modules loaded: none.

我对FunctorFoldable还不完全满意--我认为这是错误的间接原因。

有人能向我解释一下错误信息指向什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-17 22:35:15

您应该使用(.)而不是($)将您的函数组合成一个管道。发生此错误的原因是,例如,Data.Foldable.foldr1 (+)希望应用于像[a]这样的Foldable类型,但实际上是将它直接应用于作为函数的fmap (^2)

代码语言:javascript
复制
magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
magnitude = sqrt . Data.Foldable.foldr1 (+) . fmap (^2)

代码语言:javascript
复制
magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
magnitude ta = sqrt $ Data.Foldable.foldr1 (+) $ fmap (^2) $ ta

两者都会做得更好。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23716694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档