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

haw*_*eye 7 haskell types ghc ghci

我正在写一本Haskell书.它有以下示例:

ghci> Right 3 >>= \x -> return (x + 100)  
Run Code Online (Sandbox Code Playgroud)

它预计会出现这种错误:

<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)  
Run Code Online (Sandbox Code Playgroud)

当我运行它:

$ ghci

Prelude> Right 3 >>= \x -> return (x + 100) 
Run Code Online (Sandbox Code Playgroud)

我明白了:

Right 103
Run Code Online (Sandbox Code Playgroud)

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

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

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

Ale*_*lec 7

这是由于-XExtendedDefaultRulesGHCi现在默认启用.要将其关闭(并获得预期的错误消息):

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
Run Code Online (Sandbox Code Playgroud)

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