6.4.15. Arbitraryrank polymorphism¶

RankNTypes
¶ Implies: ExplicitForAll
Since: 6.8.1 Allow types of arbitrary rank.

Rank2Types
¶ Since: 6.8.1 A deprecated alias of
RankNTypes
.
GHC’s type system supports arbitraryrank explicit universal quantification in types. For example, all the following types are legal:
f1 :: forall a b. a > b > a
g1 :: forall a b. (Ord a, Eq b) => a > b > a
f2 :: (forall a. a>a) > Int > Int
g2 :: (forall a. Eq a => [a] > a > Bool) > Int > Int
f3 :: ((forall a. a>a) > Int) > Bool > Bool
f4 :: Int > (forall a. a > a)
Here, f1
and g1
are rank1 types, and can be written in standard
Haskell (e.g. f1 :: a>b>a
). The forall
makes explicit the
universal quantification that is implicitly added by Haskell.
The functions f2
and g2
have rank2 types; the forall
is on
the left of a function arrow. As g2
shows, the polymorphic type on
the left of the function arrow can be overloaded.
The function f3
has a rank3 type; it has rank2 types on the left
of a function arrow.
The language option RankNTypes
(which implies
ExplicitForAll
) enables higherrank
types. That is, you can nest forall
s arbitrarily deep in function
arrows. For example, a foralltype (also called a “type scheme”),
including a typeclass context, is legal:
 On the left or right (see
f4
, for example) of a function arrow  As the argument of a constructor, or type of a field, in a data type
declaration. For example, any of the
f1, f2, f3, g1, g2
above would be valid field type signatures.  As the type of an implicit parameter
 In a pattern type signature (see Lexically scoped type variables)
The RankNTypes
option is also required for any type with a
forall
or context to the right of an arrow (e.g.
f :: Int > forall a. a>a
, or g :: Int > Ord a => a > a
).
Such types are technically rank 1, but are clearly not Haskell98, and
an extra extension did not seem worth the bother.
In particular, in data
and newtype
declarations the constructor
arguments may be polymorphic types of any rank; see examples in
Examples. Note that the declared types are nevertheless always
monomorphic. This is important because by default GHC will not
instantiate type variables to a polymorphic type
(Impredicative polymorphism).
The obsolete language option Rank2Types
is a synonym for
RankNTypes
. They used to specify finer distinctions that GHC no
longer makes.
6.4.15.1. Examples¶
These are examples of data
and newtype
declarations whose data
constructors have polymorphic argument types:
data T a = T1 (forall b. b > b > b) a
data MonadT m = MkMonad { return :: forall a. a > m a,
bind :: forall a b. m a > (a > m b) > m b
}
newtype Swizzle = MkSwizzle (forall a. Ord a => [a] > [a])
The constructors have rank2 types:
T1 :: forall a. (forall b. b > b > b) > a > T a
MkMonad :: forall m. (forall a. a > m a)
> (forall a b. m a > (a > m b) > m b)
> MonadT m
MkSwizzle :: (forall a. Ord a => [a] > [a]) > Swizzle
In earlier versions of GHC, it was possible to omit the forall
in
the type of the constructor if there was an explicit context. For
example:
newtype Swizzle' = MkSwizzle' (Ord a => [a] > [a])
Since GHC 8.0 declarations such as MkSwizzle'
will cause an outofscope
error.
As for type signatures, implicit quantification happens for nonoverloaded types too. So if you write this:
f :: (a > a) > a
it’s just as if you had written this:
f :: forall a. (a > a) > a
That is, since the type variable a
isn’t in scope, it’s implicitly
universally quantified.
You construct values of types T1, MonadT, Swizzle
by applying the
constructor to suitable values, just as usual. For example,
a1 :: T Int
a1 = T1 (\xy>x) 3
a2, a3 :: Swizzle
a2 = MkSwizzle sort
a3 = MkSwizzle reverse
a4 :: MonadT Maybe
a4 = let r x = Just x
b m k = case m of
Just y > k y
Nothing > Nothing
in
MkMonad r b
mkTs :: (forall b. b > b > b) > a > [T a]
mkTs f x y = [T1 f x, T1 f y]
The type of the argument can, as usual, be more general than the type
required, as (MkSwizzle reverse)
shows. (reverse
does not need
the Ord
constraint.)
When you use pattern matching, the bound variables may now have polymorphic types. For example:
f :: T a > a > (a, Char)
f (T1 w k) x = (w k x, w 'c' 'd')
g :: (Ord a, Ord b) => Swizzle > [a] > (a > b) > [b]
g (MkSwizzle s) xs f = s (map f (s xs))
h :: MonadT m > [m a] > m [a]
h m [] = return m []
h m (x:xs) = bind m x $ \y >
bind m (h m xs) $ \ys >
return m (y:ys)
In the function h
we use the record selectors return
and
bind
to extract the polymorphic bind and return functions from the
MonadT
data structure, rather than using pattern matching.
6.4.15.2. Type inference¶
In general, type inference for arbitraryrank types is undecidable. GHC uses an algorithm proposed by Odersky and Laufer (“Putting type annotations to work”, POPL‘96) to get a decidable algorithm by requiring some help from the programmer. We do not yet have a formal specification of “some help” but the rule is this:
For a lambdabound or casebound variable, x, either the programmer provides an explicit polymorphic type for x, or GHC’s type inference will assume that x’s type has no foralls in it.
What does it mean to “provide” an explicit type for x? You can do that by giving a type signature for x directly, using a pattern type signature (Lexically scoped type variables), thus:
\ f :: (forall a. a>a) > (f True, f 'c')
Alternatively, you can give a type signature to the enclosing context, which GHC can “push down” to find the type for the variable:
(\ f > (f True, f 'c')) :: (forall a. a>a) > (Bool,Char)
Here the type signature on the expression can be pushed inwards to give a type signature for f. Similarly, and more commonly, one can give a type signature for the function itself:
h :: (forall a. a>a) > (Bool,Char)
h f = (f True, f 'c')
You don’t need to give a type signature if the lambda bound variable is a constructor argument. Here is an example we saw earlier:
f :: T a > a > (a, Char)
f (T1 w k) x = (w k x, w 'c' 'd')
Here we do not need to give a type signature to w
, because it is an
argument of constructor T1
and that tells GHC all it needs to know.
6.4.15.3. Implicit quantification¶
GHC performs implicit quantification as follows. At the outermost level
(only) of userwritten types, if and only if there is no explicit
forall
, GHC finds all the type variables mentioned in the type that
are not already in scope, and universally quantifies them. For example,
the following pairs are equivalent:
f :: a > a
f :: forall a. a > a
g (x::a) = let
h :: a > b > b
h x y = y
in ...
g (x::a) = let
h :: forall b. a > b > b
h x y = y
in ...
Notice that GHC always adds implicit quantifiers at the outermost level of a userwritten type; it does not find the innermost possible quantification point. For example:
f :: (a > a) > Int
 MEANS
f :: forall a. (a > a) > Int
 NOT
f :: (forall a. a > a) > Int
g :: (Ord a => a > a) > Int
 MEANS
g :: forall a. (Ord a => a > a) > Int
 NOT
g :: (forall a. Ord a => a > a) > Int
If you want the latter type, you can write
your forall
s explicitly. Indeed, doing so is strongly advised for
rank2 types.
Sometimes there is no “outermost level”, in which case no implicit quantification happens:
data PackMap a b s t = PackMap (Monad f => (a > f b) > s > f t)
This is rejected because there is no “outermost level” for the types on the RHS
(it would obviously be terrible to add extra parameters to PackMap
),
so no implicit quantification happens, and the declaration is rejected
(with “f
is out of scope”). Solution: use an explicit forall
:
data PackMap a b s t = PackMap (forall f. Monad f => (a > f b) > s > f t)