A case for DOT: theoretical foundations for objects with pattern matching and GADT-style reasoning

Aleksander Boruch-Gruszecki, Radoslaw Wasko, Yichen Xu, Lionel Parreaux

TL;DR

This paper introduces cDOT, a formal type-theoretic foundation that allows object-oriented (OO) languages to safely use complex pattern matching on generic classes. By extending the Dependent Object Types (DOT) calculus, the authors provide a mathematically sound way for compilers to "reconstruct" hidden type relationships—bringing the power of Generalized Algebraic Data Types (GADTs) to mainstream OO languages like Java, Kotlin, and Scala.

What Problem Does This Paper Address?

Modern OO languages are increasingly adopting functional programming idioms, specifically pattern matching. However, combining generics with pattern matching often leads to "puzzling type errors."

For example, if you have a generic class Expr<T> and a subclass IntLit that specifically extends Expr<Int>, a programmer expects that matching an object against IntLit should "reveal" that T is actually Int. Most current compilers (like Kotlin's) fail to make this connection, forcing programmers to use unsafe casts or deal with rejected code.

While functional languages have long solved this using GADTs, OO languages face unique challenges:

  1. Inheritance Hierarchies: OO hierarchies are nested and often "open" (extensible), unlike the flat, closed data types in functional languages.
  2. Variance: OO languages use covariance and contravariance (e.g., List<String> being a subtype of List<Object>), which makes reasoning about type relationships much more complex than simple equality.
  3. Lack of Formalism: Previous attempts to bridge this gap lacked mechanized proofs of soundness, leaving language designers unsure if their implementations were truly safe.

Methods

The authors developed cDOT, a new calculus in the Dependent Object Types (DOT) family. DOT is the mathematical heart of Scala 3, designed to model path-dependent types and structural refinement.

Key technical innovations include:

  • Subtyping Reconstruction: A technique that allows the type system to "invert" subtyping relationships. When a case match succeeds at runtime, cDOT uses that evidence to refine the static types in that specific code branch.
  • Type Members as Parameters: cDOT does away with traditional type parameters (like <T>). Instead, it encodes them as internal type members. This allows the calculus to treat generics, modules, and objects within a single, unified framework.
  • Path-Dependent Identity: Building on pDOT, cDOT uses "paths" (like x.y.z) to track object identity, which is crucial for reasoning about types that depend on specific values.
  • Mechanized Soundness: The authors didn't just write the rules on paper; they implemented the entire calculus and its safety proofs in the Coq proof assistant (~13,100 lines of code), ensuring the system is mathematically sound.

Datasets

Not discussed in the paper. As a theoretical research paper in formal methods, it relies on mathematical proofs and encodings rather than empirical datasets.

Key Insights and Ideas

  • The "Most Precise" Type: The core idea is that every object "knows" the exact type arguments used when it was created. Even if that information is "lost" due to widening (e.g., treating a List<String> as a List<Any>), pattern matching allows the compiler to recover that "most precise" information.
  • Inequations over Equations: Traditional GADT reasoning focuses on type equality ($T = Int$). Because of OO variance, cDOT reasons about type inequations ($Int <: T$ or $T <: Int$). This is a more general and powerful form of reasoning.
  • Subsuming GADTs: The authors proved that cDOT is at least as powerful as traditional GADT systems by showing they can "encode" a standard GADT calculus ($\lambda_{2,G\mu}$) into cDOT without losing any type safety.
  • Symmetric Aliasing: They identified a subtle issue in previous DOT variants where path aliasing was asymmetric. They introduced the Sngl-Self rule to make aliasing symmetric, which was essential for proving that types are preserved during evaluation.

Key Results

  • Formal Soundness: The paper provides a machine-verified proof of Type Safety (Progress and Preservation) for cDOT.
  • GADT Integration: Successfully demonstrated a type-preserving encoding of GADTs into an OO framework, proving that "subtyping reconstruction" is a viable foundation for GADT-style reasoning in OO.
  • Real-World Impact: The first author was a lead developer for the Scala 3 compiler implementation of these features. The paper serves as the formal "spec" for how Scala 3 handles GADT-style pattern matching.
  • Decidability Insights: While DOT subtyping is generally undecidable, the authors provide a roadmap for how language implementers can create "incomplete but sound" algorithms that work for 99% of real-world code.

Limitations and Future Work

  • Algorithmic Complexity: The paper focuses on a declarative system (defining what is true). Developing a fully efficient algorithmic version that handles all edge cases of intersection and union types remains an ongoing challenge.
  • Decidability: Because cDOT can encode highly complex systems (like System $F_{<:}$), its subtyping is likely undecidable in the general case. Compilers must use heuristics to avoid infinite loops in extreme edge cases.
  • Exhaustivity Checking: While the paper solves the typing of pattern matches, it does not deeply explore exhaustivity checking (ensuring the programmer covered every possible case), which is particularly difficult in open OO hierarchies.

Summary updated

BibTeX
@article{boruchgruszecki2022case,
  author = {Aleksander Boruch-Gruszecki and Radoslaw Wasko and Yichen Xu and Lionel Parreaux},
  title = {{A case for DOT: theoretical foundations for objects with pattern matching and GADT-style reasoning}},
  journal = {Proc. ACM Program. Lang},
  year = {2022},
  doi = {10.1145/3563342},
}
Figure