A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Bidirectional Type Checking

Research Depth 96 in the knowledge graph ☐ I know this ☆ Set as goal
5topics build on this
516prerequisites beneath it
See this on the map →
Type Systems OverviewHindley-Milner Type System→→Constraint-Based Type CheckingPolymorphism and Type Variables
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

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Type Systems Overview → Unification Algorithm → Type Inference Algorithms → Hindley-Milner Type System → Bidirectional Type Checking

Longest path: 97 steps · 516 total prerequisite topics

Prerequisites (2)

Leads To (2)