haskell - 为什么 GHCi 不显示与 GHC 相同的错误消息

标签 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"

最佳答案

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

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

GHCi 似乎使用了 ExtendedDefaultRules 扩展,可以通过以下方式停用

:set -NoExtendedDefaultRules

此扩展和 trac:10971使可折叠并且 Traversable 默认为 []

更新

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

关于haskell - 为什么 GHCi 不显示与 GHC 相同的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46892710/

相关文章:

haskell - 为什么 foldr' 不如 foldl' 严格?

java - 告诉 Java 编译器在运行时可以访问该字段?

image - Juicy Pixels 提示内存不足

haskell - 函数依赖中的模糊类型

plsql - 错误(36,1): PLS-00103: Encountered the symbol "SET"

haskell - 当构造函数静态已知时消除 GADT 上的模式匹配

haskell - 在没有 unsafeCoerce 的情况下玩弄存在主义

haskell - 代数类型 - Haskell

Haskell 泛化问题(涉及列表理解)

xcode4 - 如何从Xcode链接器查看更多信息?