Towards Dependent Types
This note continues Refinement Types past the point where a solver can do the work.
The progression from types to proofs
Features across static type systems sit along a single progression from ordinary types to formal proofs:
ordinary static type Int
↓
literal / union type 0 | 1 | 2
↓
parametric type List<A>
↓
algebraic type Result<A, E>
↓
newtype / branded type Positive (checked once, at construction)
↓
refinement type {x : Int | x > 0} (predicate proved by a solver)
↓
refinement relating values {x : Int | x > y && x < y + 10}
↓
dependent type Vector<A, n> (a value inside a type)
↓
propositions as types proof : x + 0 = xSeen along this line, many features in Scala, TypeScript, Rust and Haskell are different compromises on the same axis: Rust’s newtypes and typestate, Scala’s match types and singleton types, TypeScript’s narrowing and conditional types, and Haskell’s GADTs and DataKinds each trade expressiveness against inference and ergonomics at a different step, moving properties you’d otherwise test or document into properties the compiler checks.
Two senses of “dependent”
“Dependent” is used in two senses along the upper steps of this progression, which is a common source of confusion:
- Value-dependent refinements. A refinement that mentions another value (
hi >= lo) already makes one type depend on a value. Systems of this kind keep the predicates in a restricted, decidable logic. Rondon et al. (PDF) call Liquid Types a system of dependent types in this sense, and Pierce (PDF, section 30.5) describes Dependent ML, where dependent types are restricted to linear constraints and used to eliminate array bounds checks. Jhala’s tutorial (PDF, chapter 1) puts refinement types on this map as a restricted class of dependent types: less expressive, but with the proofs left to a solver. - Full dependent types. Types are indexed by arbitrary terms of the language, and types can compute. Agda, Idris, Lean and Rocq work this way.
The last two steps of the progression belong to full dependent types. A dependent type mentions a value: in Vector<A, n>, the length n is part of the type. Signatures can then state relationships, as in append : Vector a n -> Vector a m -> Vector a (n + m), and head can accept only vectors whose length is at least one, so head of an empty vector is a type error rather than a runtime crash. In the final step, a type is a logical statement and a value of that type is a proof of it: x + 0 = x has a value only if the statement is true. Wadler (PDF, section 3, “Propositions as Types”) places that step in logic: ordinary product, sum and function types correspond to propositional connectives (&, ∨, ⊃), and dependent types (Π and Σ types) are what extend the correspondence to predicate logic’s quantifiers ∀ and ∃.
Pierce’s version of the head example (section 30.5, “Going Further: Dependent Types”) is a list type indexed by its length, with hd : Πn:Nat. FloatList (succ n) -> Float: hd accepts only a list whose length is the successor of some n, so never an empty one. He also gives the warning: dependent types are a two-edged sword. Blurring type checking with theorem proving doesn’t make theorem proving easy; it makes type checking intractable, so programs need hints to guide the checker, which may be worth it for critical code but is almost certainly too costly for everyday programming. That’s why practical languages restrict them.
The price is that type checking can now involve running code: to check append, the compiler has to evaluate n + m. That’s why dependently typed languages require the functions that appear in types to be total (they terminate and handle every input), since otherwise type checking itself could loop forever and a diverging term could inhabit any proposition (Wadler, section 1, “Church, and the theory of computation”; see Types As Propositions).
Approximations in Haskell and Scala
Mainstream languages approximate the dependent end in their own ways:
- Haskell has no full dependent types, but GADTs,
DataKindsand type families let types be indexed by type-level copies of values, and singletons connect each type-level value to a run-time value. Maguire (PDF, chapter 15, “Dependent Types”) builds this up by hand and then with thesingletonspackage, ending with dependent pairs (Σ types). Bragilevsky (PDF, sections 13.2.3 and 13.2.4) introduces singletons on a door state machine and then uses them, with type-level naturals from thefinpackage, for a safe elevator interface (Workflows And States). Parsons (PDF, chapters 20-22) shows where the approximation leaks in production code: inappend :: Vector n a -> Vector m a -> Vector (Add n m) a, a tail-recursiveAddfails to type-check because GHC cannot deduceAdd n ('Succ m) ~ 'Succ (Add n m)unlessAdd’s recursion is rewritten to matchVCons(sections 20.6-20.8), and a missing equation in a type-level function yields a “stuck” unreduced type rather than an exhaustiveness error (section 22.4, which also weighs type families against functional dependencies and places type classes on the Lambda Cube as theType -> Valuebridge). - Scala 3 has singleton types, match types and dependently typed methods. Wampler (PDF, chapter 17, “Match Types” and “Dependent Typing”) builds a
DTList[N <: Int]whose length is in its type, soheadon an empty list is a compile error. Volpe (PDF, section 4.5, “Dependent types”, and section 8.1.2.5, “FSM Dependent Types”) uses match types for an everyday domain invariant, computing a finite state machine’s state and tracer types from its input type so mismatched combinations can’t be constructed. Path-dependent types are a different, older feature (Scala Type System).
Related: What Belongs In Types covers where this sits on the cost curve, and the Idris entries in Reading Paths push this furthest.
Library sources
- Pierce 2002, Types and Programming Languages – section 30.5, “Going Further: Dependent Types”: the indexed list, the cost warning and Dependent ML; on Two senses of “dependent”
- Wadler 2015, Propositions as Types – section 1, “Church, and the theory of computation”, on termination and logical consistency, and section 3, “Propositions as Types”, on Howard’s extension from propositional connectives to predicate quantifiers (
∀,∃) as dependent types; on Two senses of “dependent” - Jhala et al. 2020, Programming with Refinement Types: An Introduction to LiquidHaskell – the footnote in chapter 1 placing refinement types as a restricted class of dependent types, traded for automation; on Two senses of “dependent”
- Rondon et al. 2008, Liquid Types – Liquid Types as a system of dependent types whose predicates stay in a decidable logic; on Two senses of “dependent”
- Maguire 2018, Thinking with Types: Type-Level Programming in Haskell – chapter 15, “Dependent Types”, with singletons by hand, using
SBooland the existentialSomeSBoolfor a value read at run time (15.2), the generalSing/SingKindmachinery (15.3), thesingletonspackage (15.4) and dependent pairs with a structured-logging example (15.5); the chapter opens by naming its prerequisites, rank-N types, GADTs, type families and data kinds; on Approximations in Haskell and Scala - Bragilevsky 2021, Haskell in Depth – section 13.2, dependent types with singletons; on Approximations in Haskell and Scala
- Parsons 2023, Production Haskell – chapter 20, “Basic Type Level Programming” (sections 20.6-20.8 on length-indexed
Vectorand why tail-recursiveAddfails unification inappend), chapter 21, “Family Values” (closed, open and associated type families, data families, and bridging types and values), and chapter 22, “Trade-offs in Type Programming” (functional dependencies versus type families, the Lambda Cube, and stuck type families); on Approximations in Haskell and Scala - Wampler 2021, Programming Scala, 3rd Edition – chapter 17, match types, dependently typed methods and
DTList; on Approximations in Haskell and Scala - Volpe 2023, Functional Event-Driven Architecture: Powered by Scala 3 – section 4.5, “Dependent types”, and section 8.1.2.5, “FSM Dependent Types”, using Scala 3 match types to determine state and tracer types from an FSM’s input type; on Approximations in Haskell and Scala
Further reading:
- Noonan 2018, Ghosts of Departed Proofs (Functional Pearl) (doi:10.1145/3242744.3242755): sections 6, “Related Work”, and 7, “Summary”, on named values and phantom proofs that get many of the benefits of dependent and refinement types with only mild extensions to Haskell 2010, compared with Liquid Haskell and hs-to-coq, which keep the proofs outside the code; on Approximations in Haskell and Scala
- Hackett 2020, Review of “Thinking with Types” by Sandy Maguire (Journal of Functional Programming) – situates Maguire’s book as a digest of Haskell Symposium type-level programming techniques and notes two slips to watch for (conflating cardinal arithmetic of types with Curry-Howard, which has almost no proof material in the book, and presenting the codensity monad as the continuation monad); on Approximations in Haskell and Scala