The Hugs 98 User's Guide
Prev | Chapter 6. Language extensions supported by Hugs and GHC | Next

# 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:

- In GHC, type variables bound by pattern annotations are existentially quantified, and so may be instantiated. Thus the following is accepted by GHC but not Hugs:

      g (xs::[a]) = xs ++ "\n"

- In GHC, type variables bound in the head of a class or instance declaration are bound in method definitions in the `where` part, but this is not the case in Hugs.

- GHC also allows result type signatures, where a type signature is attached to the left side of a function definition, but Hugs does not.

Prev | Home | Next
Quantified types | Up | Implicit parameters