6.12.2. Letgeneralisation¶

MonoLocalBinds
¶ Since: 6.12.1 Infer less polymorphic types for local bindings by default.
An MLstyle language usually generalises the type of any let
bound or
where
bound variable, so that it is as polymorphic as possible. With the
extension MonoLocalBinds
GHC implements a slightly more conservative
policy, using the following rules:
 A variable is closed if and only if
 the variable is letbound
 one of the following holds:
 the variable has an explicit type signature that has no free type variables, or
 its binding group is fully generalised (see next bullet)
 A binding group is fully generalised if and only if
 each of its free variables is either imported or closed, and
 the binding is not affected by the monomorphism restriction (Haskell Report, Section 4.5.5)
For example, consider
f x = x + 1
g x = let h y = f y * 2
k z = z+x
in h x + k x
Here f
is generalised because it has no free variables; and its
binding group is unaffected by the monomorphism restriction; and hence
f
is closed. The same reasoning applies to g
, except that it has
one closed free variable, namely f
. Similarly h
is closed, even
though it is not bound at top level, because its only free variable
f
is closed. But k
is not closed, because it mentions x
which is not closed (because it is not letbound).
Notice that a toplevel binding that is affected by the monomorphism restriction is not closed, and hence may in turn prevent generalisation of bindings that mention it.
The rationale for this more conservative strategy is given in the papers “Let should not be generalised” and “Modular type inference with local assumptions”, and a related blog post.
The extension MonoLocalBinds
is implied by TypeFamilies
and GADTs
. You can switch it off again with
NoMonoLocalBinds
but type inference becomes
less predictable if you do so. (Read the papers!)