haskell - 为什么把sq改成point-free会改变类型

标签 haskell pointfree monomorphism-restriction

这个问题在这里已经有了答案:




9年前关闭。




Possible Duplicate:
What is going on with the types in this ghci session?



为了尝试和练习一些 Haskell 并了解 point free 我正在玩一个函数来平方一个数字

所以我开始定义
>let dup f x = f x x

所以我可以根据 dup 重写 sq (现在不用担心让 dup 点免费)
>let sq x = dup (*) x

并检查 sq 的类型 我看到了我期望看到的
>:t sq
>sq :: Num t => t -> t

所以我删除了 x 并得到
>let sq = dup (*)
>:t sq
sq :: Integer -> Integer

我错过了什么?

最佳答案

您遇到了单态限制。 Haskell 不会推断函数的多态类型,除非它们以“函数”样式(不是无点)给出。这意味着 let sq = dup (*)不会进行类型检查,但是 Haskell 对标准数字类有所谓的“默认规则”,这意味着它默认为单态类型“Integer->Integer”

Prelude> :set -XNoMonomorphismRestriction
Prelude> let dup f x = f x x
Prelude> let sq = dup (*)
Prelude> :t sq
sq :: Num t => t -> t

关于haskell - 为什么把sq改成point-free会改变类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15602500/

相关文章:

windows - 在 Windows 上,使用 cabal 安装的软件包似乎在 ghc/ghci 中不可用

haskell - 优化 curry 所需的无点风格

haskell - 什么时候可以将函数绑定(bind)到另一个名称?

haskell - 什么是单态限制?

没有参数的 Haskell Maybe 加法器

haskell - 从 TemplateHaskell 中的文件中读取模块

haskell - 在 Gitlab CI 中的构建之间缓存堆栈数据库

haskell - 对 Haskell 中的柯里化(Currying)和无点风格的困惑

haskell - 具有多个变量的无点组合

haskell - 什么是单态性限制?