gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:42:02 26 4
gpt4 key购买 nike

关于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使得 FoldableTraversable默认为 []

更新

没有显示类约束,因为对它的类型检查是在以后的阶段完成的。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com