haskell - 为什么这个 Haskell 在扩展时会提示类型不明确?

标签 haskell types ghci

以下返回 True(因为 2147483647 是质数)。

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

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

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)

但我不明白,为什么使用地板会产生 RealFrac?我以为 Floor 采用 RealFracs 并产生了 Integrals?另外,它没有提示上面的例子,我只是像当时一样输入更多的整数。

Prelude> :t floor
floor :: (RealFrac a, Integral b) => a -> b

最佳答案

让我们稍微澄清一下:

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)

您将 n 的值用作 float ,并将其传递给 sqrtfloor。然后,您将该结果用作 int,并将该结果传递给 mod。编译器无法命名包含所有这些实例的类型。

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

Prelude> 2 `mod` (floor . sqrt) 2
0

是因为您使用了两个不同的数字文字。一个可以是 int,一个可以是 float。如果您对两者使用相同的值,则需要调用 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

这可能会更清楚地表明您将 n 的值用作两种不同的类型。

关于haskell - 为什么这个 Haskell 在扩展时会提示类型不明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7354317/

相关文章:

types - 在 F# 中,具有不同 arity 和返回类型的两个函数可以共享一个名称吗?

与实际类型已知的 void 指针比较

haskell - ghci没有从文件加载函数

haskell - 找出函数属于哪个模块

Haskell 随机数生成

haskell - 检查密码是否足够强——Haskell 与过程语言

haskell - Cloud Haskell 中的 ManagedProcess 如何工作?

c# - Json.NET 中的 JConstructor 和 JRaw

haskell - 无法将预期类型 `Maybe (String, Int, String)' 与实际类型 `([Char], t0, [Char])' 匹配

multithreading - 为什么 `-threaded` 让它变慢?