## Higher-rank, impredicative polymorphism in Scala

One project that I spent some time thinking about prior arriving in Lausanne was whether I might be able to put together a Scala library for manipulating data-structures with binding based around the use of parametricity to help enforce the adequacy of function spaces (ala Boxes Go Bananas). However, one problem is that Scala does not have higher-rank polymorphism like GHC. The initial thought was, "well, Scala has higher-rank existential types, so adding the dual case should be pretty straightforward to add to the typechecker". However, it turns out it is not so straightforward to add universal quantification to the implementation of the subtyping algorithm and ensure that constraints can be solved unambiguously. It still may make sense to try and add them in some fashion.

Later, while working on Featherweight Scala, it occurred to me that subtyping is very likely to be undecidable in Scala because its language of types is essentially as expressive as full F_{<:}. Benjamin agreed that there existed a straightforward reduction to languages with only bounded existential quantification rather than bounded universal quantification, but did not know offhand if there is a paper that gives it.

So I spent a bit of time thinking about encoding universal quantification using existential quantification, and as alluded to in TAPL (the answer to exercise 24.3.3), it can be done, but there is no completely local encoding of universals into existentials. To obtain the classical equivalences between the negation of universal quantification and existential quantification (∀α.τ implies ¬∃α.¬τ) in a constructive setting you have to use the double-negation encoding, which essentially means CPS converting your program. The other classical equivalence under negation, ∃α.τ implies ¬∀α.¬τ, is constructively valid which is why it is existential quantification is macro-expressible in languages with universal quantification.

Since I am not aware of any papers that illustrate the encoding of universals into existentials, I figured I would explain it here. The encoding of the universal type pretty much follows from the equivalence:

|∀α.τ| = (∃α.|τ|→0)→0

Here I am using 0 for the void or empty type and using the standard constructive encoding of negation:

|¬τ| = |τ| → 0

The term level encodings are not so difficult, but does require a little thought:

|Λα.e : ∀α.τ|_{k} = k (λx:∃α.|τ|→0.let (α,k') = unpack x in |e : τ|_{k'})

The term encoding has the form |e : τ|_{k}, where τ is the type of the term and k is a continuation threaded through the encoding. The first thing to note is that because type abstractions are values, we always immediately pass them to the continuation. Given that,

if we look at the type we need to give the encoding of type abstraction, it is a function that that takes an existentially quantified continuation, and never returns. The only way to never return is to use the continuation. This tells us just about everything else we need to know about the encoding.

Because the continuation is existentially quantified, it is not possible to invoke it as is, so we must unpack it first. This is how we simulate the introduction of a type variable without using type abstraction. The next problem is that in order to call the continuation, we need to apply it to something with the correct type. The continuation takes values of type |τ|, possibly referencing a free type variable α, which is fortunately what the encoding of the type abstraction's body gives us. Therefore, we can use the unpacked continuation k' as the continuation argument of the recursive call to the encoding.

Given that we know how to encode type abstractions, encoding type applications is straightforward:

|e[τ] : τ'|_{k} = |e : ∀α.τ'{α/τ}|_{(λv:(∃α.τ'{α/τ}→0)→0.v (pack(τ,k) as ∃α.(τ'{α/τ}→0)))}

We encode the term to be applied and pass it a new continuation, that packs up the original continuation and applies it to function to which the term evaluates. I am writing {α/τ} to mean replace τ with α. I am using curly braces rather than the usual square brackets to avoid confusion with the square brackets that is commonly used for type application.

While I am almost certain that the encoding above is correct, it would be good at some point to prove it. This is the sort of proof for which Twelf is very well suited, so that is probably the route I would take.

Okay, so that is the theoretical half out of the way. Now, given the above, can we implement higher-rank impredicative polymoprhism in Scala? Yes, with a minor caveat.

First, I'll define an abstract class that defines the type of bounded universally quantified values, and methods for creating and destructing them.

abstract class Universal { type Univ[Bound,Body[_]] def createUniv[Bound,Body[_]] (impl : ((Body[A] => Nothing) forSome { type A <: Bound }) => Nothing) : Univ[Bound,Body] def applyUniv[Bound,Body[_],A<:Bound](arg : Univ[Bound,Body]) : Body[A] }

This should be fairly straightforward given the encoding described above. The only tricky bit is the use of a higher-kinded type parameter (

) to give the body of the universal type. The other important point to note is that it is not necessary to completely CPS convert the implementation because Scala provides primitives for non-local control transfers.

Body[_]

One possible implementation of the above abstract class is the following:

class UniversalImpl extends Universal { type Univ[Bound,Body[_]] = ((Body[A] => Nothing) forSome { type A <: Bound }) => Nothing def createUniv[Bound,Body[_]] (impl : ((Body[A] => Nothing) forSome { type A <: Bound }) => Nothing) : Univ[Bound,Body] = impl def applyUniv [Bound,Body[_],A<:Bound](univ : Univ[Bound,Body]) : Body[A] = { case class Control(arg : Body[A]) extends Throwable val res = try { univ((arg:Body[A]) => throw new Control(arg)) } catch { case Control(arg) => arg case e => throw e } res } }

The implementation of the abstract type and the code for

are trivial. The implementation of

createUniv

is a little more interesting. Here, we create a local case class

applyUniv

, extending

Control

so that it may be thrown as an exception, that will hold a value of the desired result type. We then just pass the representation of the type abstraction a continuation that throws a new instance of

Throwable

. We immediately catch it and return the value stored within. If we happen to catch some other sort of

Control

we just re-throw it.

Throwable

And that's it. It is worth looking at a few examples of how this might be used. The first thing that is necessary is to create a concrete implementation of `Universal`

:

val u : Universal = new UniversalImpl

Given

, we can create an instance of the polymorphic identity function:

u

type Id[T] = T => T val id = u.createUniv[AnyRef,Id]( (k: (Id[A] => Nothing) forSome { type A }) => k(x => x)) val idString = u.applyUniv[AnyRef,Id,String](id) val idStringList = u.applyUniv[AnyRef,Id,List[String]](id) println(idString("Foo")) println(idStringList(List("Foo", "Bar", "Baz")))

The first thing that needs to be done is to define a type function for representation the body of the universally quantified type. For the polymorphic identity function we call it

. The rest is pretty straightforward. It ought to be possible to make the above code a little less verbose if Scala allowed type parameters to methods to be curried: it should be possible for it to infer that the first two arguments to

Id[T]

are

u.applyUniv

and

AnyRef

from the argument

Id

. However, because it will not be able to infer the last type parameter, we have to give all three. It might also be desirable in some case to not have to give a type definition to define the body of the a universal type; this could be achieve by extending Scala with support for anonymous type functions.

id

Now for that caveat I mentioned. When we defined the type abstraction,

u.createUniv[AnyRef,Id]( (k: (Id[A] => Nothing) forSome { type A }) => k(x => x))

a significant difference between how this code is written, and how it would have been written in terms of my original explanation of the encoding, is that there is no explicit unpacking of the existential. All existential unpacking in Scala is implicit. This is nice in some ways, but it means that we have no way to give a name to type that the existential is hiding. Therefore, when constructing a function to pass to the continuation,

k(x => x)

it must be the case that Scala can infer the domain of this function's type, because it is not possible to write a type annotation for

without having a name for the existentially hidden type. I think in most cases this should not be possible, as Scala knows the type that

x

is expecting as an argument, but there may be some corner cases I have not considered.

k

(Does anyone know how to prevent WordPress from completely screwing up less than and greater than inside of <pre> tags?)