GHC错误消息解释

时间:2015-04-16 13:21:00

标签: haskell ghc

我正在玩一个非常简单的haskell程序,忘记了函数调用中的引用(以下示例代码中的最后lookup调用)。

import Data.List
main :: IO ()
main = do
    let sum = sumAllOddSquaresSmallerThan 100
    print sum
    let testMap = [(4,"Foo"), (2,"Bar"), (1,"Baz")]
    print $ lookup' testMap 2

sumAllOddSquaresSmallerThan :: (Ord a, Integral a) => a -> a
sumAllOddSquaresSmallerThan n = sum(takeWhile (<n) (filter odd (map (^2) [1..])))

lookup' :: Eq a => [(a,b)] -> a -> b
lookup' [] _ = error("key not in the list")
lookup' ((x',y'):xs) x = if x' == x then y' else lookup xs x

GHC给出的错误信息如下:

CompilerErrorTest.hs:12:70:
    Could not deduce (Integral b1) arising from a use of ‘^’
    from the context (Ord a, Integral a)
      bound by the type signature for
                 sumAllOddSquaresSmallerThan :: (Ord a, Integral a) => a -> a
      at CompilerErrorTest.hs:11:32-60
    The type variable ‘b1’ is ambiguous
    Note: there are several potential instances:
      instance Integral Int -- Defined in ‘GHC.Real’
      instance Integral Integer -- Defined in ‘GHC.Real’
      instance Integral GHC.Types.Word -- Defined in ‘GHC.Real’
    In the first argument of ‘map’, namely ‘(^ 2)’
    In the second argument of ‘filter’, namely ‘(map (^ 2) [1 .. ])’
    In the second argument of ‘takeWhile’, namely
      ‘(filter odd (map (^ 2) [1 .. ]))’

CompilerErrorTest.hs:12:71:
    Could not deduce (Num b1) arising from the literal ‘2’
    from the context (Ord a, Integral a)
      bound by the type signature for
                 sumAllOddSquaresSmallerThan :: (Ord a, Integral a) => a -> a
      at CompilerErrorTest.hs:11:32-60
    The type variable ‘b1’ is ambiguous
    Note: there are several potential instances:
      instance Num Double -- Defined in ‘GHC.Float’
      instance Num Float -- Defined in ‘GHC.Float’
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in ‘GHC.Real’
      ...plus three others
    In the second argument of ‘(^)’, namely ‘2’
    In the first argument of ‘map’, namely ‘(^ 2)’
    In the second argument of ‘filter’, namely ‘(map (^ 2) [1 .. ])’

CompilerErrorTest.hs:17:60:
    Could not deduce (a ~ [([(a, b)], b0)])
    from the context (Eq a)
      bound by the type signature for
                 lookup' :: Eq a => [(a, b)] -> a -> b
      at CompilerErrorTest.hs:15:12-36
      ‘a’ is a rigid type variable bound by
          the type signature for lookup' :: Eq a => [(a, b)] -> a -> b
          at CompilerErrorTest.hs:15:12
    Relevant bindings include
      x :: a (bound at CompilerErrorTest.hs:17:22)
      xs :: [(a, b)] (bound at CompilerErrorTest.hs:17:18)
      y' :: b (bound at CompilerErrorTest.hs:17:14)
      x' :: a (bound at CompilerErrorTest.hs:17:11)
      lookup' :: [(a, b)] -> a -> b (bound at CompilerErrorTest.hs:16:1)
    In the second argument of ‘lookup’, namely ‘x’
    In the expression: lookup xs x

更糟糕的是,我首先尝试了FP Complete在线haskell IDE中的示例,它隐藏了错误消息的最后两行(我没有注意到......在消息末尾附近)< / p>

有人可以解释为什么GHC会在无关的sumAllOddSquaresSmallerThan函数中出现这些错误,以及我如何能够迅速缩小错误来源?

1 个答案:

答案 0 :(得分:0)

修复程序似乎将ghc更新为7.10.1版本,其错误消息在查找错误方面更有帮助......但它可能不适合所有人。