Haskell/Existentially quantified types

Existential types, or 'existentials' for short, are a way of 'squashing' a group of types into one, single type.

Existentials are part of GHC's type system extensions. They aren't part of Haskell98, and as such you'll have to either compile any code that contains them with an extra command-line parameter of, or put   at the top of your sources that use existentials.

The keyword
The  keyword is used to explicitly bring fresh type variables into scope. For example, consider something you've innocuously seen written a hundred times so far:

But what are these  and  ? Well, they're type variables, you answer. The compiler sees that they begin with a lowercase letter and as such allows any type to fill that role. Another way of putting this is that those variables are 'universally quantified'. If you've studied formal logic, you will have undoubtedly come across the quantifiers: 'for all' (or $$\forall$$) and 'exists' (or $$\exists$$). They 'quantify' whatever comes after them: for example, $$\exists x$$ means that whatever follows is true for at least one value of x. $$\forall x$$ means that what follows is true for every possible value of x you can imagine. For example, $$\forall x, \, x^2 \geq 0$$ and $$\exists x, \, x^3 = 27$$.

The  keyword quantifies types in a similar way. We would rewrite 's type as follows:

So we see that for any combination of types  and   we can imagine,   takes the type. For example, we might choose  and. Then it's valid to say that  has the type. Here we are instantiating the general type of  to a more specific type.

However, in Haskell, any introduction of a lowercase type parameter implicitly begins with a  keyword, so those two previous type declarations for   are equivalent, as are the declarations below:

What makes life really interesting and the  so useful is that you can apply additional constraints on the type variables it introduces. Such constraints, $$P(x)$$, serve to guarantee certain properties of the type variable, $$x$$, as a kind of ad-hoc interface restriction, (similar to $$\exists x, P(x)$$ or $$\forall x, P(x)$$ stipulations).

Let's dive right into the deep end of this by seeing an example of the power of existential types in action.

Example: heterogeneous lists
The premise behind Haskell's type class system is grouping types that all share a common property. So if you know a type that is a member of some class, you know certain things about that type. For example,  is a member of class , so we know that elements of   can be compared for equality.

Suppose we have a group of values. We don't know if they are all the same type, but we do know they are all members of some class (and, by extension, that all the values have a certain property). It might be useful to throw all these values into a list. We can't do this normally because lists elements must be of the same type (homogeneous with respect to types). However, existential types allow us to loosen this requirement by defining a 'type hider' or 'type box':

We won't explain precisely what we mean by that data type definition, but its meaning should be clear to your intuition. The important thing is that we're calling the constructor on three values of different types,, yet we are able to place them all into a single list, so we must somehow have the same type for each one. Essentially, yes. This is because our use of the  keyword gives our constructor the type. If we were now writing a function to which we intend to pass, we couldn't apply a function such as   to the values inside the   because their type might not be. But we do know something about each of the elements: they can be converted to a string via. In fact, that's pretty much the only thing we know about them.

Let's expand on this a bit more. In the definition of  for   – the line marked with   – we don't know the type of. But as we mentioned, we do know that the type is an instance of Show due to the constraint on the  constructor. Therefore, it's legal to use the function  on , as seen in the right-hand side of the function definition.

As for, recall the type of print:

As we just declared  an instance of , we can print the values in the list.

A Further Explanation
One way to think about  is to think about types as a set of possible values. For example, Bool is the set {True, False, &perp;} (remember that bottom, &perp;, is a member of every type!), Integer is the set of integers (and bottom), String is the set of all possible strings (and bottom), and so on. serves as a way to assert a commonality or intersection of the specified types (i.e. sets of values). For example,  is the intersection of all types. This subset turns out to be the set whose sole element is bottom, {&perp;}, since it is an implicit value in every type. That is, the type whose only available value is bottom. However, since every Haskell type includes bottom, {&perp;}, this quantification in fact stipulates all Haskell types. However, the only permissible operations on it are those available to a type whose only element is bottom.

A few more examples:


 * 1) The list, , is the type of a list whose elements all have the type  , i.e. a list of bottoms.
 * 2) The list, , is the type of a list whose elements all have the type  . The Show class constraint requires the possible types to also be a member of the class, Show. However, &perp; is still the only value common to all these types, so this too is a list of bottoms.
 * 3) The list, , requires each element to be a member of the class, Num. Consequently, the possible values include numeric literals, which have the specific type,  , as well as bottom.
 * 4)   is the type of the list whose elements all have the same type  . Since we cannot presume any particular type at all, this too is a list of bottoms.

We see that most intersections over types just lead to bottoms because types generally don't have any values in common and so presumptions cannot be made about a union of their values.

However, recall that in the last section, we developed a heterogeneous list using a 'type hider'. This 'type hider' functions as a wrapper type which guarantees certain facilities by implying a predicate or constraint on the permissible types. In that case it was that they must be a member of the type class. In general, that seems to be the purpose of, to impose type constraint on the permissible types within a type declaration and thereby guaranteeing certain facilities with such types.

Let's declare one.

This means that:

So we can pass any type,, we want to   and it will create a T. So what happens when we deconstruct a   value with pattern matching...?

As we've just stated,  could be of any type. That means it's a member of some arbitrary type, so has the type. In other words the set whose only available value is bottom, &perp;.

However, we can make a heterogeneous list:

Of course, when we pattern match on  we cannot presume any features about its elements. So technically, we can't do anything useful with its elements, except reduce them to WHNF. However, if we introduce class constraints:

The class constraint serves to limit the types we are intersecting over, such that we now have values inside a  which are elements of some arbitrary type that are members of Show. The implication of this is that we can apply  to a value of type   upon deconstruction. It doesn't matter exactly which type it turns out to be.

To summarize, the interaction of the universal quantifier with data types produces a qualified subset of types guaranteeing certain facilities as described by one or more class constraints.

Example:
One monad that you may not have come across so far is the ST monad. This is essentially a more powerful version of the  monad: it has a much more complicated structure and involves some more advanced topics. It was originally written to provide Haskell with IO. As we mentioned in the ../Understanding monads/ chapter, IO is basically just a State monad with an environment of all the information about the real world. In fact, inside GHC at least, ST is used, and the environment is a type called.

To get out of the State monad, you can use. The analogous function for ST is called, and it has a rather particular type:

This is actually an example of a more complicated language feature called rank-2 polymorphism, which we don't go into detail here. It's important to notice that there is no parameter for the initial state. Indeed, ST uses a different notion of state to State; while State allows you to  and   the current state, ST provides an interface to references. You create references, which have type, with  , providing an initial value, then you can use   and   to manipulate them. As such, the internal environment of a ST computation is not one specific value, but a mapping from references to values. Therefore, you don't need to provide an initial state to runST, as the initial state is just the empty mapping containing no references.

However, things aren't quite as simple as this. What stops you creating a reference in one ST computation, then using it in another? We don't want to allow this because (for reasons of thread-safety) no ST computation should be allowed to assume that the initial internal environment contains any specific references. More concretely, we want the following code to be invalid:

What would prevent this? The effect of the rank-2 polymorphism in 's type is to constrain the scope of the type variable   to be within the first parameter. In other words, if the type variable  appears in the first parameter it cannot also appear in the second. Let's take a look at how exactly this is done. Say we have some code like the following:

The compiler tries to fit the types together:

The importance of the  in the first bracket is that we can change the name of the. That is, we could write:

This makes sense: in mathematics, saying $$\forall x. x > 5$$ is precisely the same as saying $$\forall y. y > 5$$; you're just giving the variable a different label. However, we have a problem with our above code. Notice that as the  does not scope over the return type of , we don't rename the   there as well. But suddenly, we've got a type mismatch! The result type of the ST computation in the first parameter must match the result type of, but now it doesn't!

The key feature of the existential is that it allows the compiler to generalise the type of the state in the first parameter, and so the result type cannot depend on it. This neatly sidesteps our dependence problems, and 'compartmentalises' each call to  into its own little heap, with references not being able to be shared between different calls.

Quantified Types as Products and Sums
A universally quantified type may be interpreted as an infinite product of types. For instance, a polymorphic function like:

can be understood as a product, or a tuple, of individual functions, one per every possible type. To construct a value of such type, we have to provide all the components of the tuple at once. In case of function types, this is possible because of polymorphism -- one formula generating an infinity of functions.

In case of numeric types, one numeric constant may be used to initialize multiple types at once. For instance, in:

may be conceptualized as a tuple consisting of an  value, a   value, etc.

Similarly, an existentially quantified type may be interpreted as an infinite sum. For instance,

may be conceptualized as a sum:

To construct a value of this type, we only have to pick one of the constructors. A polymorphic constructor  combines all those constructors into one.

Quantification as a primitive
Universal quantification is useful for defining data types that aren't already defined. Suppose there was no such thing as pairs built into haskell. Quantification could be used to define them. {-# LANGUAGE ExistentialQuantification, RankNTypes #-} newtype Pair a b = Pair (forall c. (a -> b -> c) -> c) makePair :: a -> b -> Pair a b makePair a b = Pair $ \f -> f a b

In GHCi: λ> :set -XExistentialQuantification λ> :set -XRankNTypes λ> newtype Pair a b = Pair {runPair :: forall c. (a -> b -> c) -> c} λ> makePair a b = Pair $ \f -> f a b  λ> pair = makePair "a" 'b'   λ> :t pair pair :: Pair [Char] Char λ> runPair pair (\x y -> x) "a" λ> runPair pair (\x y -> y) 'b'