Consider this example:
applyKTimes :: Integral i => i -> (a -> a) -> a -> a
applyKTimes 0 _ x = x
applyKTimes k f x = applyKTimes (k-1) f (f x)
applyThrice :: (a -> a) -> a -> a
applyThrice = applyKTimes 3
The 3
in applyThrice
is defaulted by GHC to an Integer
as shown when compiling with -Wall
:
Warning: Defaulting the following constraint(s) to type 'Integer'
'Integral t'
arising from a use of 'applyKTimes'
So I guess that Integer
is the default Integral a => a
.
-Wall
..)Yes, you can, although it's not quite so simple as adding a default per typeclass, and it only works for Num
and its subclasses in the Prelude
and standard libraries. The syntax is default (t1, ..., tn)
, and only one such declaration can be used per module.
Adding default (Int)
, for example, would change the default for Integral
in your code to Int
.
The default default of (Integer, Double)
isn't just a GHC policy: it's from the Haskell 98 Report. (GHCi does have extended default rules, though.)
Here's a discussion of some of the problems with the current system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With