[英]Why does GHCi not show the same error message as GHC
对编译时 Haskell 类型错误中给出的错误进行推理,我发现 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使Foldable
和Traversable
默认为[]
。
类约束没有显示,因为它的类型检查是在稍后阶段完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.