Haskell's type-defaulting rules defaults a type variable a with constraints (C1 a,...,Cn a) if:
This makes sense to me but it's also very limited: in practice it means that you always have to specify the type when it is ambiguous when you work with custom classes. For example, this doesn't compile:
class (Show a) => MyShow a where
myShow :: a -> String
myShow = show
instance (MyShow a) => MyShow (Maybe a) where
myShow Nothing = "Nothing"
myShow (Just x) = "Just " ++ (myShow x)
main = print $ myShow Nothing -- ambiguous
GHCi extends this set of rules in this way:
and GHC has an extension called ExtendedDefaultRules that enable these rules. However this extension is very specific: it works only on GHC and with standard classes. For instance, one can think about libraries that don't use the standard library. In that case, the GHCi extension won't work.
My question is: the only way to extend the Haskell's type-defaulting rules like GHCi does is with compiler extensions? And more generic: is there a way to define type-defaulting based on some rules on the constraints in Haskell 98?
There is no way to do what you want.
The current situation is impoverished by design. The idea was to do something minimal that could be extended in the future when we have a good design.
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