Given two functions:
f :: (A a, B b) => a -> b
g :: (B b, C c) => b -> c
Is there any way (in GHC) I can make it possible to write:
h x = g (f x)
Without having to add a type signature for f x
, e.g.
h x = g ((f x) :: T)
By having some "default type" that f x
takes if none is specified?
I suspect I need something like Defaulting in Haskell Prime but has this been implemented in GHC (or in GHC head)?
Stupid non-answer:
asT :: T -> T
asT = id
h = g . asT . f
Lack of "defaulting" is one of the pains of Haskell's flavor of generics. Things get too generic and Haskell doesn't know which instance to pick. The status quo is that "the programmer needs to explicitly resolve ambiguity." Rather than specifying top-level rules for defaulting, you just have to select the appropriate instance on a case-by-case basis. Num
defaulting is a hacky exception to this rule.
The proposal you linked has a good example of why choosing defaults isn't trivial when multiple classes are involved.
default A (Int, String, ())
default B (String, Int, ())
(A t, B t) => t -- defaults to what?
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