为什么这个Haskell在扩展时会抱怨ambigous类型?

812*_*128 4 haskell types ghci

以下返回True(因为2147483647是素数).

length [f | f <- [2..(floor(sqrt 2147483647))], 2147483647 `mod` f == 0 ] == 0
Run Code Online (Sandbox Code Playgroud)

当我尝试扩展它时,为什么它不起作用?

Prelude> [n | n <- [2..], length [f | f <- [2..(floor(sqrt n))], n `mod` f == 0 ] == 0 ]

<interactive>:1:39:
    Ambiguous type variable `t' in the constraints:
      `RealFrac t' arising from a use of `floor' at <interactive>:1:39-51
      `Integral t' arising from a use of `mod' at <interactive>:1:56-64
      `Floating t' arising from a use of `sqrt' at <interactive>:1:45-50
    Probable fix: add a type signature that fixes these type variable(s)
Run Code Online (Sandbox Code Playgroud)

我不明白,为什么RealFrac会因使用地板而产生?我认为地板采用了RealFracs并制作了积分?另外它没有抱怨上面的例子,我只是输入更多的整数,就像我那样做.

Prelude> :t floor
floor :: (RealFrac a, Integral b) => a -> b
Run Code Online (Sandbox Code Playgroud)

Jos*_*Lee 10

让我们稍微疏忽一下:

Prelude> (\x -> x `mod` (floor . sqrt) x) 2

<interactive>:1:24:
    Ambiguous type variable `b' in the constraints:
      `Floating b' arising from a use of `sqrt' at <interactive>:1:24-27
      `Integral b' arising from a use of `mod' at <interactive>:1:7-30
      `RealFrac b' arising from a use of `floor' at <interactive>:1:16-20
    Probable fix: add a type signature that fixes these type variable(s)
Run Code Online (Sandbox Code Playgroud)

你使用的值为nfloat,将它传递给sqrtfloor.然后,您将该结果用作int,将结果传递给mod.编译器无法为具有所有这些实例的类型命名.

换句话说,它在你的第一个例子中起作用的原因

Prelude> 2 `mod` (floor . sqrt) 2
0
Run Code Online (Sandbox Code Playgroud)

是因为你使用两个不同的数字文字.一个可以是一个int,一个可以是一个浮点数.如果您对两者使用相同的值,则需要调用fromIntegral以将int转换为float.

您可以通过添加类型签名来获取不同的错误消息,更改[2..][2..] :: [Integer]:

No instance for (RealFrac Integer)
  arising from a use of `floor' at <interactive>:1:52-64
No instance for (Floating Integer)
  arising from a use of `sqrt' at <interactive>:1:58-63
Run Code Online (Sandbox Code Playgroud)

这可能会使您更清楚地使用n两种不同类型的值.