为什么GHCi没有显示与GHC相同的错误消息

时间:2017-10-23 15:08:05

标签: haskell compiler-errors ghc ghci

关于Haskell type error on compilation中给出的错误的推理我发现了GHCi的奇怪行为

test.hs

members :: String -> Bool;
members str = and [ b | x <- str, b <- map (elem x) "abcde" ]

生成以下错误消息

Prelude> :l test.hs
[1 of 1] Compiling Main             ( test.hs, interpreted )
test.hs:2:53: error:
    • Couldn't match type ‘Char’ with ‘t0 Char’
      Expected type: [t0 Char]
        Actual type: [Char]
    • In the second argument of ‘map’, namely ‘"abcde"’
      In the expression: map (elem x) "abcde"
      In a stmt of a list comprehension: b <- map (elem x) "abcde"

,而

GHCi

Prelude> members :: String -> Bool; members xs = and [ b | x <- xs, b <- map (elem x) "abcde"]

生成

<interactive>:18:78: error:
    • Couldn't match type ‘Char’ with ‘[Char]’
      Expected type: [[Char]]
        Actual type: [Char]
    • In the second argument of ‘map’, namely ‘"abcde"’
      In the expression: map (elem x) "abcde"
      In a stmt of a list comprehension: b <- map (elem x) "abcde"

更新

我也不明白,为什么GHC省略了Foldable t0类约束 - 我 本来希望错误信息是这样的:

test.hs:2:53: error:
    • Couldn't match type ‘Char’ with ‘t0 Char’
      Expected type: Foldable t0 => [t0 Char]
        Actual type: [Char]
    • In the second argument of ‘map’, namely ‘"abcde"’
      In the expression: map (elem x) "abcde"
      In a stmt of a list comprehension: b <- map (elem x) "abcde"

1 个答案:

答案 0 :(得分:2)

由FTP(Foldable-Traversable-Proposal)函数elem引入 获得了新型签名。

elem :: (Eq a, Foldable t) => a -> t a -> Bool

似乎GHCi使用ExtendedDefaultRules扩展名,可以通过

停用
:set -NoExtendedDefaultRules

此扩展程序和trac:10971使Foldable和。{ Traversable默认为[]

更新

未显示类约束,因为检查它的类型是在稍后阶段完成的。