Haskell 中的求幂
有人可以告诉我为什么 Haskell Prelude 定义了两个单独的求幂函数(即 ^
和 **
)?我认为类型系统应该消除这种重复。
Prelude> 2^2
4
Prelude> 4**0.5
2.0
Can someone tell me why the Haskell Prelude defines two separate functions for exponentiation (i.e. ^
and **
)? I thought the type system was supposed to eliminate this kind of duplication.
Prelude> 2^2
4
Prelude> 4**0.5
2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上存在三个求幂运算符:
(^)
、(^^)
和(**)
。^
是非负整数幂,^^
是整数幂,**
是浮点幂:原因是类型安全:数值运算的结果通常与输入参数具有相同的类型。但您无法将
Int
进行浮点幂并获得Int
类型的结果。因此类型系统会阻止您执行此操作:(1::Int) ** 0.5
会产生类型错误。(1::Int) ^^ (-1)
也是如此。另一种说法是:
Num
类型在^
下封闭(它们不需要具有乘法逆元),Fractional
类型在^
下封闭^^
、Floating
类型在**
下封闭。由于Int
没有Fractional
实例,因此您无法将其计算为负幂。理想情况下,
^
的第二个参数静态地约束为非负数(目前,1 ^ (-2)
会引发运行时异常)。但Prelude
中没有自然数的类型。There are actually three exponentiation operators:
(^)
,(^^)
and(**)
.^
is non-negative integral exponentiation,^^
is integer exponentiation, and**
is floating-point exponentiation:The reason is type safety: results of numerical operations generally have the same type as the input argument(s). But you can't raise an
Int
to a floating-point power and get a result of typeInt
. And so the type system prevents you from doing this:(1::Int) ** 0.5
produces a type error. The same goes for(1::Int) ^^ (-1)
.Another way to put this:
Num
types are closed under^
(they are not required to have a multiplicative inverse),Fractional
types are closed under^^
,Floating
types are closed under**
. Since there is noFractional
instance forInt
, you can't raise it to a negative power.Ideally, the second argument of
^
would be statically constrained to be non-negative (currently,1 ^ (-2)
throws a run-time exception). But there is no type for natural numbers in thePrelude
.Haskell 的类型系统不够强大,无法将三个求幂运算符表示为一个。你真正想要的是这样的:
即使你打开多参数类型类扩展,这也不会真正起作用,因为实例选择需要比 Haskell 目前允许的更聪明。
Haskell's type system isn't powerful enough to express the three exponentiation operators as one. What you'd really want is something like this:
This doesn't really work even if you turn on the multi-parameter type class extension, because the instance selection needs to be more clever than Haskell currently allows.
它没有定义两个运算符——而是定义了三个!报告中:
这意味着存在三种不同的算法,其中两种给出精确结果(
^
和^^
),而**
给出近似结果。通过选择要使用的运算符,您可以选择要调用的算法。It doesn't define two operators -- it defines three! From the Report:
This means there are three different algorithms, two of which give exact results (
^
and^^
), while**
gives approximate results. By choosing which operator to use, you choose which algorithm to invoke.^
要求其第二个参数是Integral
。如果我没记错的话,如果您知道正在使用积分指数,那么实现会更有效。另外,如果您想要类似2 ^ (1.234)
的值,即使您的底数是整数 2,您的结果显然也是小数。您有更多选项,以便可以更严格地控制进出指数函数的类型。Haskell 的类型系统与其他类型系统(例如 C、Python 或 Lisp)的目标不同。鸭子类型(几乎)与 Haskell 思维方式相反。
^
requires its second argument to be anIntegral
. If I'm not mistaken, the implementation can be more efficient if you know you are working with an integral exponent. Also, if you want something like2 ^ (1.234)
, even though your base is an integral, 2, your result will obviously be fractional. You have more options so that you can have more tight control over what types are going in and out of your exponentiation function.Haskell's type system does not have the same goal as other type systems, such as C's, Python's, or Lisp's. Duck typing is (nearly) the opposite of the Haskell mindset.