Bidirectional Type Checking

Research Depth 70 in the knowledge graph I know this Set as goal
Unlocks 4 downstream topics
type-checking type-inference

Core Idea

Bidirectional type checking works in two modes: checking (verifying an expression has an expected type) and inference (discovering a term's type). This approach is more efficient than pure inference and handles more complex type systems. Many modern languages use bidirectional checking.

How It's Best Learned

Implement a bidirectional type checker for a language with polymorphism. Compare performance and error messages with unidirectional approaches.

Common Misconceptions

Type checking and inference are opposite processes (they are complementary modes). Bidirectional checking is only for functional languages (many imperative and systems languages use it).

Explainer

If you have studied type systems, you know the basic question: given an expression and a type environment, does this expression have a valid type? Pure type inference (as in Hindley-Milner) answers this by discovering the type from scratch — it examines the expression, generates constraints, and solves them. Pure type checking goes the other direction: you are given a type and verify that the expression conforms. Bidirectional type checking combines both modes, switching between them strategically to get the best of each.

The two modes are called synthesis (inference) and checking. In synthesis mode, the algorithm examines an expression and produces its type — information flows *out* of the expression. Variables synthesize their type from the environment, and function application synthesizes a return type from the function's known type. In checking mode, the algorithm receives an expected type and verifies the expression against it — information flows *into* the expression. Lambda abstractions are the classic example: `λx. x + 1` cannot synthesize a type on its own (what type is `x`?), but if you *check* it against `Int → Int`, the parameter type `Int` is pushed inward, and the body `x + 1` can then be verified.

The key insight is that type annotations at strategic points create "seeds" of type information that propagate through the program. When you write `let f: Int → Int = λx. x + 1`, the annotation `Int → Int` lets the checker switch into checking mode for the lambda body. Without bidirectional checking, a pure inference system would need to either require annotations everywhere (tedious) or solve complex constraint systems globally (expensive, with poor error messages). Bidirectional checking finds a practical middle ground: annotate function signatures, and the rest flows naturally.

This design also produces dramatically better error messages. In pure Hindley-Milner inference, a type mismatch might be reported far from its actual cause, because the unification engine only discovers the conflict when two distant constraints collide. In bidirectional checking, errors are localized: if you check `"hello"` against `Int`, the error points directly at `"hello"` and says it is not an `Int`. Modern languages including Rust, Swift, Kotlin, and recent versions of TypeScript all use bidirectional type checking. The pattern scales naturally to advanced features like generics, dependent types, and type-level computation, which is why it has become the dominant approach in practical type system implementation.

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 AlgorithmsHindley-Milner Type SystemBidirectional Type Checking

Longest path: 71 steps · 382 total prerequisite topics

Prerequisites (2)

Leads To (2)