为什么ghci在这种情况下不提供预期的Ambiguous类型变量错误?

问题描述 投票:6回答:1

我正在通过a Haskell book工作。它有以下示例:

ghci> Right 3 >>= \x -> return (x + 100)  

它预计会出现这种错误:

<interactive>:1:0:  
    Ambiguous type variable `a' in the constraints:  
      `Error a' arising from a use of `it' at <interactive>:1:0-33  
      `Show a' arising from a use of `print' at <interactive>:1:0-33  
    Probable fix: add a type signature that fixes these type variable(s)  

当我运行它:

$ ghci

Prelude> Right 3 >>= \x -> return (x + 100) 

我明白了:

Right 103

即我没有得到预期的错误。

现在也许编译器或库已经改变了 - 但我不确定如何验证。

我的问题是:为什么ghci在这种情况下不提供预期的模糊类型变量错误?

haskell types ghc ghci
1个回答
6
投票

这是因为-XExtendedDefaultRules现在默认在GHCi中启用。要将其关闭(并获得预期的错误消息):

ghci> :set -XNoExtendedDefaultRules
ghci> Right 3 >>= \x -> return (x + 100)
<interactive>:2:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘print’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance (Show b, Show a) => Show (Either a b)
          -- Defined in ‘Data.Either’
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        ...plus 23 others
        ...plus 11 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In a stmt of an interactive GHCi command: print it

此扩展为默认的其他模糊代码添加了一些额外的方法。在您的情况下,您有一个类型为Num b => Either a b的表达式。 Regular Haskell defaulting rules告诉我们b应该默认为Integer。扩展规则(参见上面的链接)另外默认a()

© www.soinside.com 2019 - 2024. All rights reserved.