关于 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/