Dependent Types and Value-Level Type Constraints

Research Depth 70 in the knowledge graph I know this Set as goal
Unlocks 3 downstream topics
type-systems dependent-types advanced

Core Idea

In dependent type systems, types can depend on values—not just other types. This enables properties like 'list of length n' or 'vector indexed from 1 to n' to be encoded in types, allowing type-checking to verify invariants that traditional type systems cannot, eliminating entire classes of runtime errors.

Explainer

In the type systems you have studied so far, types and values inhabit separate worlds. You can have a type `List<Int>` that describes "a list of integers," but the type says nothing about *how many* integers. The function `head` that returns the first element of a list can be given the type `List<T> → T`, but this is a lie — it crashes on an empty list. The type system cannot distinguish an empty list from a non-empty one because list length is a *value*, and values are invisible to types. Dependent types break down this wall: they allow types to contain and be parameterized by values, so `Vec<T, n>` means "a list of exactly n elements of type T," where n is a natural number known at compile time.

This idea connects to lambda calculus in a precise way. In the simply-typed lambda calculus, you have terms that depend on terms (ordinary functions), and types that depend on types (generic/parametric polymorphism). Dependent types add a third axis: types that depend on terms. The type `Vec<Int, 3>` depends on the value `3`. A function `append : Vec<T, m> → Vec<T, n> → Vec<T, m+n>` states in its type signature that appending a vector of length m to one of length n produces a vector of length m+n. The compiler *proves* this at type-checking time — if your implementation does not maintain this invariant, it will not compile. This is not testing; it is mathematical proof carried out by the type checker.

Consider what this buys you. A matrix multiplication function can have the type `Matrix<m, k> → Matrix<k, n> → Matrix<m, n>`, and the compiler will reject any attempt to multiply matrices with incompatible dimensions — not at runtime with an error message, but at compile time with a type error. An array index operation can require a proof that the index is within bounds, eliminating out-of-bounds errors entirely. A network protocol parser can encode the expected message format in the type, so a well-typed parser is guaranteed to handle all valid messages correctly.

The cost is significant. Type checking in dependent type systems is undecidable in general — the compiler may need to evaluate arbitrary computations to check whether two types are equal. Languages like Agda, Idris, and Coq manage this by requiring all functions used in types to be total (always terminating), which keeps type checking decidable at the expense of restricting what you can express. Writing dependently-typed programs also demands a different style of thinking: you are simultaneously writing code and constructing proofs, and the compiler is your proof assistant. The learning curve is steep, but the payoff is programs where entire categories of bugs — null pointer dereferences, buffer overflows, dimension mismatches, protocol violations — are ruled out by construction rather than caught by tests.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsInteger Order of OperationsVariable ExpressionsCombining Like TermsOne-Step EquationsTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionTime and Space ComplexityAmortized AnalysisHash TablesSymbol Tables and Scope ResolutionSemantic Analysis PhaseType Systems OverviewUnification AlgorithmType Inference AlgorithmsGradual Typing and Mixed Static-Dynamic TypesDependent Types and Value-Level Type Constraints

Longest path: 71 steps · 382 total prerequisite topics

Prerequisites (3)

Leads To (2)