Haskell: Defaulting constraints to type
Posted
by yairchu
on Stack Overflow
See other posts from Stack Overflow
or by yairchu
Published on 2010-05-18T23:53:30Z
Indexed on
2010/05/19
0:00 UTC
Read the original article
Hit count: 216
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
.
- Is there a way to define "default types" for other constraints too?
- Is using default types bad practice? (it does complain when using
-Wall
..)
© Stack Overflow or respective owner