With -XTypeApplications
in GHC 8.0, you can specify types explicitly with @
preceding function arguments. What types does it exactly specify, especially when several @
are introduced?
If you look at the type of a function
elem :: (Foldable t, Eq a) => a -> t a -> Bool
we see it has two polymorphic variables, t
and a
. These variables are what the @
type applications specify. It seems that variables introduced in the context — where typeclass constraints go — affect order, and hence the first @
specifies the t
, and the second the a
. In functions without context variables
const :: a -> b -> a
the order is more obvious, the a
is first and b
is second. As Cactus mentioned in a comment above, you can also use explicit foralls to specify the order yourself.
myConst :: forall b a. a -> b -> a
Now the first type application will specify the b
and the second the a
.
You may run into this problem of needing to specify types particularly if you're using overloaded strings or lists
elem c "abc...xyz" -- What string type is this?
elem c ['a' .. 'z'] -- What list constructor is this?
therefore we use explicit type applications
elem @[] @Char c ['a' .. 'z']
in this case we only have to specify the @[]
and say "this is a []
list type constructor" because GHC infers Char
from the list elements, so @Char
can be omitted here.
If a polymorphic argument GHC is able to infer happens to come first you can leverage -XPartialTypeSignatures
which allows you to use _
in type signatures including type application signatures, telling GHC to just infer that [part of the] type, to make things less verbose.
f @_ @[]
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