Abstract
Generalized algebraic data types (GADTs) extend ordinary algebraic data types by refining the types of constructors with syntactic equality constraints. This is highly useful and allows for novel applications such as strongly-typed evaluators, typed LR parsing etc. To translate GADTs we need to enrich the System F style typed intermediate languages of modern language implementations to capture these equality constraints. We show that GADTs can be translated to a minor extension of System F where type equality proofs are compiled into System F typable proof terms. At run-time proof terms evaluate to the identity. Hence, they can be safely erased before execution of the program. We provide evidence that our approach scales to deal with extensions where equality is not anymore syntactic. The benefit of our method is that type checking of target programs remains as simple as type checking in System F. Thus, we can offer a light-weight approach to integrate GADTs and extensions of it into existing implementations.
Introduction
Generalized Algebraic Data Types (GADTs) represent a significant advancement in functional programming, allowing developers to encode complex invariants directly into the type system. By enabling constructor return types to be refined with syntactic equality constraints, GADTs facilitate the creation of robust, type-safe components such as strongly-typed evaluators and generic programming libraries. However, integrating GADTs into production-grade compilers presents a significant engineering challenge.
Most modern functional compilers, such as the Glasgow Haskell Compiler (GHC), utilize System F as a core intermediate language. While System F is exceptionally well-suited for standard parametric polymorphism, it lacks native support for the type equality constraints inherent in GADTs. When a program pattern-matches on a GADT constructor, it gains local type information (e.g., "in this branch, type variable a is equivalent to Int"). Standard System F provides no mechanism to represent, propagate, or utilize these equality proofs during the compilation pipeline.
Methodology: System $F_s$ and Staged Proofs
The authors propose a solution by introducing System $F_s$, a "staged" extension of System F. This approach is built upon several foundational concepts:
The Curry-Howard Isomorphism
The core of the translation relies on the "proofs-as-programs" principle. In System $F_s$, a proof that two types $t_1$ and $t_2$ are equal is represented as a pair of functions $(f, g)$, where $f: t_1 \to t_2$ and $g: t_2 \to t_1$. By treating these proofs as first-class values, the compiler can pass them through the intermediate representation just like any other term.
Staged Typing and the Staging Operator
To prevent these proof terms from interfering with the actual program logic, the authors introduce a staging operator, denoted as < >. This operator marks terms as "staged," effectively isolating them from the primary computation. This ensures that proof terms are only accessible to the type checker for verification purposes and do not impact the operational semantics of the program.
Type-Directed Translation
The paper defines a formal set of translation rules to lower GADT source code into System $F_s$. A key aspect of this process is the handling of "existential" variables—variables that appear in a constructor's definition but are not present in the resulting type. The translation ensures these variables are correctly scoped and that the associated equality proofs are properly applied during pattern matching.
Key Insights
Zero-Cost Proof Erasure
A pivotal insight of this research is that while the compiler requires equality proofs to guarantee type safety, these proofs are computationally redundant at runtime. The authors demonstrate that every valid proof term in their system eventually evaluates to the identity function (λx.x). Consequently, an "erasure theorem" is established: all proof terms and staging annotations can be safely removed after type checking without altering the program's behavior. This ensures that GADTs incur zero performance overhead in the final executable.
Simplicity of the Target Language
By extending System F only minimally, the authors maintain the simplicity of the target language's type checker. Rather than implementing a complex, GADT-aware logic engine within the core of the compiler, they leverage the existing machinery of function application and abstraction to handle type equalities.
Addressing the Decomposition Problem
The authors highlight a limitation in standard existential types: the "decomposition problem." If a compiler knows that List a is equal to List b, it should be able to infer that a equals b. System $F_s$ addresses this by providing explicit decomposition primitives, allowing the type system to "unwrap" equality proofs for complex types into proofs for their constituent parts.
Results and Extensibility
The formal properties of the translation are rigorously established:
- Type Soundness: The translation preserves types, ensuring that well-typed source programs result in well-typed System $F_s$ programs.
- Semantic Preservation: The erasure of proof terms does not change the meaning of the program.
- Scalability: The "proofs-as-programs" approach is shown to be extensible beyond simple syntactic equality. It can accommodate Associated Types and user-defined type equations, which are essential for modern Haskell features like type families.
Limitations and Future Work
Despite its strengths, the approach has boundaries. The paper assumes that source programs are already fully type-annotated, leaving the complex problem of GADT type inference for other research. Additionally, while the two-stage system works for current needs, more advanced type systems (e.g., those involving higher-kinded types or "kinds-as-types") might require a multi-stage hierarchy. Finally, the authors caution that allowing arbitrary user-defined type equality rules could lead to undecidability in the type-checking process, a common trade-off in highly expressive type systems.
Summary updated