We’re used to having universally quantified types for polymorphic functions. Existentially quantified types are used much less often. How can we express existentially quantified types using universal type quantifiers?
We’re used to having universally quantified types for polymorphic functions. Existentially quantified types are
Share
It turns out that existential types are just a special case of Σ-types (sigma types). What are they?
Sigma types
Just as Π-types (pi types) generalise our ordinary function types, allowing the resulting type to depend on the value of its argument, Σ-types generalise pairs, allowing the type of second component to depend on the value of the first one.
In a made-up Haskell-like syntax, Σ-type would look like this:
Assuming
* :: *(i.e. the inconsistentSet : Set), we can defineexists a. aas:The first component is a type and the second one is a value of that type. Some examples:
exists a. ais fairly useless – we have no idea what type is inside, so the only operations that can work with it are type-agnostic functions such asidorconst. Let’s extend it toexists a. F aor evenexists a. Show a => F a. GivenF :: * -> *, the first case is:The second one is a bit trickier. We cannot just take a
Show atype class instance and put it somewhere inside. However, if we are given aShow adictionary (of typeShowDictionary a), we can pack it with the actual value:This is a bit inconvenient to work with and assumes that we have a
Showdictionary around, but it works. Packing the dictionary along is actually what GHC does when compiling existential types, so we could define a shortcut to have it more convenient, but that’s another story. As we will learn soon enough, the encoding doesn’t actually suffer from this problem.Digression: thanks to constraint kinds, it’s possible to reify the type class into concrete data type. First, we need some language pragmas and one import:
GADTs already give us the option to pack a type class along with the constructor, for example:
However, we can go one step further:
It works much like the
BSTexample above: pattern matching onD :: Dict ctxgives us access to the whole contextctx:We also get quite natural generalisation for existential types that quantify over more type variables, such as
exists a b. F a b.The encoding
Now, the question is: can we encode Σ-types with just Π-types? If yes, then the existential type encoding is just a special case. In all glory, I present you the actual encoding:
There are some interesting parallels. Since dependent pairs represent existential quantification and from classical logic we know that:
forall r. ris almost⊥, so with a bit of rewriting we get:And finally, representing universal quantification as a dependent function:
Also, let’s take a look at the type of Church-encoded pairs. We get a very similar looking type:
We just have to express the fact that
bmay depend on the value ofa, which we can do by using dependent function. And again, we get the same type.The corresponding encoding/decoding functions are:
The special case actually simplifies things enough that it becomes expressible in Haskell, let’s take a look:
Note that we can view
f :: (x :: *) -> x -> xasf :: forall x. x -> x. That is, a function with extra*argument behaves as a polymorphic function.And some examples:
Notice that
someListis actually constructed viaencode, but we dropped theaargument. That’s because Haskell will infer whatxin theforall x.part you actually mean.From Π to Σ?
Strangely enough (although out of the scope of this question), you can encode Π-types via Σ-types and regular function types: