Type inference for type abstractions in functions
Summary
GHC Proposal #448, section 5.2, subsection 4, bullet i, reads as follows:
In synthesis mode, when examining
\ @a -> expr
, we simply puta
in scope as a fresh skolem variable (that is, not equal to any other type) and then checkexpr
. (Presumably,expr
usesa
in a type signature.) When we infer thatexpr
has typety
, the expression\ @a -> expr
has typeforall a. ty
. Example:\ @a (x :: a) -> x
infers the typeforall a. a -> a
. (For this example, we note that\ @a (x :: a) -> x
is a short-hand for\ @a -> \ (x :: a) -> x
.)
Furthermore, bullet iii reads:
In synthesis mode, when examining a function argument
@a
to a functionf
, we bringa
into scope as a fresh skolem variable and check the remainder of the arguments and the right-hand side. In the type off
, we include aforall a.
in the spot corresponding to the type variable argument.
So, per the proposal, TypeAbstractions
should support type inference. However, at the moment, they don't:
ghci> f @a = 42 :: a
<interactive>:3:3: error: [GHC-14964]
• Invisible type pattern a has no associated forall
• In an equation for ‘f’: f @a = 42 :: a
Steps to reproduce
Try to compile this code:
{-# LANGUAGE TypeAbstractions #-}
module T25660 where
-- No signature:
f @a = 42 :: a
Expected behavior
The declaration of f
is accepted with the inferred type f :: forall a. Num a => a
.
Environment
- GHC version used: 9.12.1