Opened 3 years ago

Closed 3 years ago

#12271 closed feature request (duplicate)

Remove magic from type defaulting

Reported by: crockeea Owned by:
Priority: normal Milestone:
Component: Compiler Version: 8.0.1
Keywords: ExtendedDefaultRules Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case:
Blocked By: Blocking:
Related Tickets: 8171 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 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.)

Change History (2)

comment:1 Changed 3 years ago by thomie

Keywords: ExtendedDefaultRules added

I think this request is covered by #8171, can you confirm?

comment:2 Changed 3 years ago by crockeea

Resolution: duplicate
Status: newclosed

I believe that ticket covers this case. I made added a comment there; closing this as duplicate.

Note: See TracTickets for help on using tickets.