【问题标题】:Why does GHCi not show the same error message as GHC为什么 GHCi 不显示与 GHC 相同的错误消息
【发布时间】:2017-10-24 00:57:11
【问题描述】:

推理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"

【问题讨论】:

    标签: haskell compiler-errors ghc ghci


    【解决方案1】:

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

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

    而且 GHCi 似乎使用了 ExtendedDefaultRules 扩展名,可以通过

    :set -NoExtendedDefaultRules
    

    这个扩展和trac:10971 使FoldableTraversable 默认为 []

    更新

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

    【讨论】:

    • 约束被省略了,因为它当时没有被检查,我猜。我们有一个Char,我们期待一个t Char。如果您定义equal :: a -&gt; b -&gt; Bool; equal x y = x == y,它类似于错误消息。仅当您将b 修复为a 时,您才会收到缺少Eq 实例错误,但在此之前(==) :: a -&gt; a -&gt; Bool 的(无约束)类型已经阻止我们对上面的equal 变体进行类型检查。跨度>
    • 想到了那一行,但不确定!
    • TBH,必须检查 GHC 的类型检查代码。我猜 TC 在实例检查之前出现。但这可能是一个有趣的后续问题(只要它是主题)。
    • 我想这个问题有点太宽泛了——但我会在下班后写一个;-)
    • 请使用您问题上的编辑链接添加其他信息。 Post Answer 按钮应仅用于问题的完整答案。 - From Review
    猜你喜欢
    • 2021-03-04
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2014-01-03
    • 2013-09-14
    • 1970-01-01
    相关资源
    最近更新 更多