Exponentiation in Haskell

后端 未结 4 1402
北恋
北恋 2020-12-12 16:05

Can someone tell me why the Haskell Prelude defines two separate functions for exponentiation (i.e. ^ and **)? I thought the type system was suppo

4条回答
  •  温柔的废话
    2020-12-12 16:44

    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:

    class Exp a b where (^) :: a -> b -> a
    instance (Num a,        Integral b) => Exp a b where ... -- current ^
    instance (Fractional a, Integral b) => Exp a b where ... -- current ^^
    instance (Floating a,   Floating b) => Exp a b where ... -- current **
    

    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.

提交回复
热议问题