简体   繁体   English

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

[英]Could not deduce error when using type constraints

I am using Haskell to implement a linear algebra example. 我正在使用Haskell实现线性代数示例。 However, I run into a problem when declaring the magnitude function. 但是,在声明magnitude函数时遇到了一个问题。

My implementation is as follows: 我的实现如下:

magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
magnitude = sqrt $ Data.Foldable.foldr1 (+) $ fmap (^2)

The idea is that magnitude will accept Vec2D , Vec3D , or Vec4D , and return the square root of the sum of the squares of their components. 这个想法是, magnitude将接受Vec2DVec3DVec4D ,并返回其分量平方和的平方根。

Each of the three vector types implements Functor and Foldable . 三种向量类型中的每一个都实现FunctorFoldable For example, 例如,

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

However, I receive a multitude of errors: 但是,我收到许多错误:

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.

I'm not entirely comfortable with Functor or Foldable yet - and I believe this is the indirect reason for the errors. 我对FunctorFoldable尚不完全满意-我相信这是错误的间接原因。

Can someone explain to me what the error messages are pointing at? 有人可以向我解释错误消息指的是什么吗?

You ought to combine your functions into a pipeline with (.) not ($) . 您应该使用(.)而不是($)将函数组合到管道中。 This error is occurring because, for instance, Data.Foldable.foldr1 (+) expects to be applied to a Foldable type like [a] but you're actually applying it directly to fmap (^2) which is a function. 发生此错误的原因是,例如, Data.Foldable.foldr1 (+)希望应用于[a]类的Foldable类型,但实际上是将其直接应用于fmap (^2)

magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
magnitude = sqrt . Data.Foldable.foldr1 (+) . fmap (^2)

or 要么

magnitude :: (Foldable t, Functor t, Floating a) => t a -> a
magnitude ta = sqrt $ Data.Foldable.foldr1 (+) $ fmap (^2) $ ta

both will do better. 两者都会做的更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM