Theorems for Free for Free: Parametricity, With and Without Types

Amal Ahmed, Dustin Jamner, Jeremy Siek, Philip Wadler

To appear at the International Conference on Functional Programming (ICFP) 2017.

The polymorphic blame calculus integrates static typing, including universal types, with dynamic typing. The primary challenge with this integration is preserving parametricity: even dynamically-typed code should satisfy it once it has been cast to a universal type. Ahmed et al. (2011) employ runtime type generation in the polymorphic blame calculus to preserve parametricity, but a proof that it does so has been elusive. Matthews and Ahmed (2008) gave a proof of parametricity for a closely related system that combines ML and Scheme, but later found a flaw in their proof. In this paper we prove that the polymorphic blame calculus satisfies relational parametricity. The proof relies on a step-indexed Kripke logical relation. The step-indexing is required to make the logical relation well-defined in the case for the dynamic type. The possible worlds include the mapping of generated type names to their concrete types and the mapping of type names to relations. We prove the Fundamental Property of this logical relation and that it is sound with respect to contextual equivalence. To demonstrate the utility of parametricity in the polymorphic blame calculus, we derive two free theorems.

Artifact: a type checker and small-step machine for the Polymorphic Blame Calculus.

Dustin Jamner, Jeremy Siek

Source code and build instructions:

We present a type checker and stepper for the Polymorphic Blame Calculus. Programs written in the code box below may be typechecked and, if they pass, loaded into the stepper. Programs in the stepper are split into redex and context and may be evaluated either one step at a time or 100 steps at a time. The global name store is also displayed. Additionally, our artifact includes a step backwards button that reverses the last step taken.

Note: there are some syntactic differences from the presentation in the paper, which nonetheless we expect will be the primary reference for the language. These changes were made to eliminate the necessity of Unicode, reduce ambiguity in the grammar, and make the type checking algorithm syntax-directed. We summarize these changes at the bottom of this page.

Examples from paper:

Additional examples: | Simple call | Omega | Factorial

Examples with errors:





Syntactic differences from the paper:

∀, λ, Λ, π1, π2 forall, lam, Lam, pi1, pi2 Greek letters and quantifiers are replaced by English keywords.
* The dynamic type is written with the ASCII asterisk.
e : T1p T2 e : T1 => T2 Cast expressions use an ASCII arrow and do not include the blame label.
(e1, e2) <e1, e2> Pairs are enclosed in < and > instead of parentheses.
T1 × T2 <T1, T2> Pair types are written with < and > instead of ×.
let x = e1 in e2 let x : T = e1 in e2 Let expressions require a type annotation.
exists X. T Existential types are provided by encoding into universals
pack T1, e in X. T2 Create an existential package from the value of e, hiding type T1 as X inside T2. Expression e should have type T2[T1/X]. The result type is exists X. T2.
unpack[T1,T2] X,x = e1 in e2 Unpack an existential. The expression e1 should have type exists X. T1 and expression e2 should have type T2. The variable x has type T1 and is in scope for e2.