7.15. Partial Type Signatures

A partial type signature is a type signature containing special placeholders
written with a leading underscore (e.g., "_",
"_foo", "_bar") called
wildcards. Partial type signatures are to type signatures
what Section 7.14, “Typed Holes” are to expressions. During compilation these
wildcards or holes will generate an error message that describes which type
was inferred at the hole's location, and information about the origin of any
free type variables. GHC reports such error messages by default.

Unlike Section 7.14, “Typed Holes”, which make the program incomplete and
will generate errors when they are evaluated, this needn't be the case for
holes in type signatures. The type checker is capable (in most cases) of
type-checking a binding with or without a type signature. A partial type
signature bridges the gap between the two extremes, the programmer can choose
which parts of a type to annotate and which to leave over to the type-checker
to infer.

By default, the type-checker will report an error message for each hole in a
partial type signature, informing the programmer of the inferred type. When
the -XPartialTypeSignatures flag is enabled, the type-checker
will accept the inferred type for each hole, generating warnings instead of
errors. Additionally, these warnings can be silenced with the
-fno-warn-partial-type-signatures flag.

7.15.1. Syntax

A (partial) type signature has the following form: forall a b .. .
(C1, C2, ..) => tau. It consists of three parts:

The type variables: a b ..

The constraints: (C1, C2, ..)

The (mono)type: tau

We distinguish three kinds of wildcards.

7.15.1.1. Type Wildcards

Wildcards occurring within the monotype (tau) part of the type signature are
type wildcards ("type" is often omitted as this is the
default kind of wildcard). Type wildcards can be instantiated to any monotype
like Bool or Maybe [Bool], including
functions and higher-kinded types like (Int -> Bool) or
Maybe.

7.15.1.2. Named Wildcards

Type wildcards can also be named by giving the underscore an identifier as
suffix, i.e. _a. These are called named
wildcards. All occurrences of the same named wildcard within one
type signature will unify to the same type. For example:

The named wildcard forces the argument and result types to be the same.
Lacking a signature, GHC would have inferred forall a b. (Char, a) ->
(Char, b). A named wildcard can be mentioned in constraints,
provided it also occurs in the monotype part of the type signature to make
sure that it unifies with something:

Named wildcards should not be confused with type
variables. Even though syntactically similar, named wildcards can
unify with monotypes as well as be generalised over (and behave as type
variables).

In the first example above, _x is generalised over (and is
effectively replaced by a fresh type variable w_x). In the
second example, _x is unified with the
Bool type, and as Bool implements the
Show type class, the constraint Show
Bool can be simplified away.

By default, GHC (as the Haskell 2010 standard prescribes) parses identifiers
starting with an underscore in a type as type variables. To treat them as
named wildcards, the -XNamedWildCards flag should be enabled.
The example below demonstrated the effect.

foo :: _a -> _a
foo _ = False

Compiling this program without enabling -XNamedWildCards
produces the following error message complaining about the type variable
_a no matching the actual type Bool.

7.15.1.3. Extra-Constraints Wildcard

The third kind of wildcard is the extra-constraints
wildcard. The presence of an extra-constraints wildcard indicates
that an arbitrary number of extra constraints may be inferred during type
checking and will be added to the type signature. In the example below, the
extra-constraints wildcard is used to infer three extra constraints.

As a single extra-constraints wildcard is enough to infer any number of
constraints, only one is allowed in a type signature and it should come last
in the list of constraints.

Extra-constraints wildcards cannot be named.

7.15.2. Where can they occur?

Partial type signatures are allowed for bindings, pattern and expression signatures.
In all other contexts, e.g. type class or type family declarations, they are disallowed.
In the following example a wildcard is used in each of the three possible contexts.