Type defaulting with non

The following program compiles with GHC:

main :: IO ()
main = print $ 2^2

despite the signature of

(^) :: (Num a, Integral b) => a -> b -> a 

due to GHC's type defaulting mechanism. I'm using numeric-prelude, which instead exports

(^) :: Ring.C a => a -> Integer -> a

This is extremely annoying to use with Int exponents, so I prefer Prelude's version with a polymorphic exponent. Thus, I define the following:

{-# LANGUAGE NoImplicitPrelude, RebindableSyntax, ScopedTypeVariables #-}

import Algebra.Ring as Ring (C)
import Algebra.ToInteger as ToInteger (C)
import NumericPrelude hiding ((^))

(^) :: forall a i . (Ring.C a, ToInteger.C i) => a -> i -> a
x0 ^ y0 = undefined

main :: IO ()
main = print $ 2^2

but I get no type defaulting, so there are errors about ambiguous types for the base and exponent.

This is the extent of documentation I could find about defaulting, which doesn't describe what the components of the tuple actually do, nor when the "default" default types are available.

Just from playing around, it looks like GHC is magically applying defaulting rules to built-in numeric types like Num and Integral , rather than to literals of arbitrary type.

Is there a way to extend this GHC magic to default any "integer-looking" type to (say) Int , even when the constraint is not Integral a ? Adding an explicit default (Int) (assuming that's the right way to do it), doesn't help.

More specifically, the problem I'm trying to solve is to find a way to use one function (^) that works for

  • Int exponents
  • Integer exponents
  • literal exponents with no explicit type annotation
  • so any solution that achieves those three points would suffice.

    链接地址: http://www.djcxy.com/p/43270.html

    上一篇: Haskell类型与新类型相关的类型安全性

    下一篇: 输入非违约