[GHC] #12271: Remove magic from type defaulting

GHC ghc-devs at haskell.org
Thu Jun 30 13:39:06 UTC 2016


#12271: Remove magic from type defaulting
-------------------------------------+-------------------------------------
           Reporter:  crockeea       |             Owner:
               Type:  feature        |            Status:  new
  request                            |
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  GHC rejects
  Unknown/Multiple                   |  valid program
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 The Prelude is great, but there are reasons people use drop-in
 replacements as well. In particular, I'm using [numeric-prelude] for its
 more mathematical numeric type class hierarchy. Unfortunately, I don't get
 type defaulting with their numeric type class replacements, which makes
 some operations a real pain to use.

 For example, GHC accepts the following:
 {{{
 main :: IO ()
 main = print $ 2^2
 }}}

 but not
 {{{
 {-# LANGUAGE NoImplicitPrelude, RebindableSyntax #-}

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

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

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

 (there are ambiguous types on both occurrences of `2`.) The
 [https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3.4
 HaskellReport] seems to suggest that there is no way to use the `default`
 keyword to get type defaulting in this case.

 What I'm asking for is a programmatic way to set defaulting rules for non-
 standard numeric classes. In terms of implementation, my naive approach
 would be to remove `Num` from the process altogether: if we ever see a
 "integer-looking" literal and its type cannot be inferred, always default
 it to the `IntDefault` type (which could be the same as the current
 "automatic" default, or the value in the `default` tuple if present). I'm
 guessing that this approach has some flaws; perhaps someone can point them
 out?

 Barring that, I suppose we need a way to specify a `Num` replacement. In
 the case of numeric-prelude, it would be `Algebra.Additive.C`. An even
 more flexible approach would be to allow defaulting for constants with a
 specific constraint. (That's equivalent to specifying a default type for
 constants with the constraint `Num a => a` and a possibly different
 default for `Integral a => a`.)

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12271>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list