关于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"
答案 0 :(得分:2)
由FTP(Foldable-Traversable-Proposal)函数elem
引入
获得了新型签名。
elem :: (Eq a, Foldable t) => a -> t a -> Bool
似乎GHCi使用ExtendedDefaultRules
扩展名,可以通过
:set -NoExtendedDefaultRules
此扩展程序和trac:10971使Foldable
和。{
Traversable
默认为[]
。
未显示类约束,因为检查它的类型是在稍后阶段完成的。