6.4. Type annotations in patterns

Haskell 98 allows expressions to be annotated with type signatures. With the -98 option, these annotations are also allowed on patterns:

f (x::Int) = fromIntegral x :: Double
Moreover type variables in pattern annotations are treated specially: unless the type variable is already bound (by another pattern annotation), it is universally quantified over the pattern and its scope, e.g.
snoc (xs::[a]) (x::a) = xs++[x] :: [a]
Occurrences of the type variable in type signatures within this scope are bound to this type variable. In the above example the second and third occurrences of a are bound by the first. This permits locally defined variables to be given signatures in situations where it would be impossible in Haskell 98:
sortImage :: Ord b => (a -> b) -> [a] -> [a]
sortImage (f::a->b) = sortBy cmp
    where
        cmp :: a -> a -> Ordering
        cmp x y = compare (f x) (f y)
Note that the relationship between signature declarations and pattern annotations is asymmetrical: pattern annotations may capture type variables in signature declarations, but not vice versa. There is no connection between the type variables in the type signature of sortImage and those in its definition, but the occurrence of a in the signature of cmp is bound by the pattern (f::a->b).

There are some differences with GHC's scoped type variables: