Skip to content

Remove magic from type defaulting

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.)

Trac metadata
Trac field Value
Version 8.0.1
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information