Algorithm Search⌘K

Techniquecore

Bidirectional type checking

Also called bidirectional typing, local type inference.

Splits the typing judgment into a checking mode and a synthesis mode, so annotations are needed only where the two modes meet.

Bidirectional type checking splits the typing judgment in two. In checking mode, the type is an input: does ee have type AA? In synthesis mode, the type is an output: what type does ee have? Each typing rule chooses a mode for its conclusion and for each premise, so the rules read as two mutually recursive functions. Type information flows down from annotations and function types into the term, and up from variables. A lambda needs no annotation when its type is known from outside. An annotation is needed only where a term that can only be checked sits in a place that must synthesize. The technique sits between full type checking of explicitly typed terms and full type inference. It also scales to systems where inference is undecidable, such as higher-rank polymorphism and dependent types.

In plain words

A type checker can work in two ways. Sometimes it already knows the type that a piece of code must have, for example because the code is an argument to a function whose parameter type is known. Then it only has to check the code against that type. At other times it must work out the type from the code alone, for example for a variable whose type is in the context.

Bidirectional checking chooses one of the two ways for every kind of expression. Variables and function calls report their types. Function literals and branches are checked against a type given from outside. When a reporting expression appears where a check is needed, the checker compares the two types. When a checked-only expression appears where a report is needed, the programmer must write its type. That case is rare, so a few type signatures are usually enough.

When it applies

  • The language has a typing judgment, and the rules can be oriented so that every input of each premise is known when the premise is checked. This is the mode correctness of the rules.
  • Full inference is undecidable, too expensive, or gives poor error messages, but the programmer will write some types. Typical cases are higher-rank polymorphism, subtyping with polymorphism, dependent types and refinement types.
  • The terms are mostly in normal form, as in ordinary programs. Each beta-redex in the source, such as a lambda applied in place, needs one annotation.

It does not apply when no annotations are allowed at all. For annotation-free ML programs, Hindley-Milner inference with unification is the right tool.

The template

Write Γ⊢e⇒A\Gamma \vdash e \Rightarrow A for "ee synthesizes AA" and Γ⊢e⇐A\Gamma \vdash e \Leftarrow A for "ee checks against AA". Add the annotated term (e:A)(e : A) to the syntax. For the simply typed lambda calculus with Int\mathsf{Int}, Bool\mathsf{Bool}, ++ and if\mathsf{if}, the rules are as follows (Dunfield and Krishnaswami 2021, Section 2, extended with the base types).

  • Var⇒: if x:Ax : A is in Γ\Gamma, then Γ⊢x⇒A\Gamma \vdash x \Rightarrow A.
  • Lit⇒: Γ⊢n⇒Int\Gamma \vdash n \Rightarrow \mathsf{Int}, and Γ⊢true⇒Bool\Gamma \vdash \mathsf{true} \Rightarrow \mathsf{Bool}, Γ⊢false⇒Bool\Gamma \vdash \mathsf{false} \Rightarrow \mathsf{Bool}.
  • Anno⇒: if Γ⊢e⇐A\Gamma \vdash e \Leftarrow A, then Γ⊢(e:A)⇒A\Gamma \vdash (e : A) \Rightarrow A.
  • App⇒: if Γ⊢e1⇒A→B\Gamma \vdash e_1 \Rightarrow A \to B and Γ⊢e2⇐A\Gamma \vdash e_2 \Leftarrow A, then Γ⊢e1 e2⇒B\Gamma \vdash e_1\,e_2 \Rightarrow B.
  • Add⇒: if Γ⊢e1⇐Int\Gamma \vdash e_1 \Leftarrow \mathsf{Int} and Γ⊢e2⇐Int\Gamma \vdash e_2 \Leftarrow \mathsf{Int}, then Γ⊢e1+e2⇒Int\Gamma \vdash e_1 + e_2 \Rightarrow \mathsf{Int}.
  • Abs⇐: if Γ,x:A⊢e⇐B\Gamma, x : A \vdash e \Leftarrow B, then Γ⊢λx. e⇐A→B\Gamma \vdash \lambda x.\,e \Leftarrow A \to B.
  • If⇐: if Γ⊢e1⇐Bool\Gamma \vdash e_1 \Leftarrow \mathsf{Bool}, Γ⊢e2⇐A\Gamma \vdash e_2 \Leftarrow A and Γ⊢e3⇐A\Gamma \vdash e_3 \Leftarrow A, then Γ⊢if e1 then e2 else e3⇐A\Gamma \vdash \mathsf{if}\ e_1\ \mathsf{then}\ e_2\ \mathsf{else}\ e_3 \Leftarrow A.
  • Sub⇐ (mode switch): if Γ⊢e⇒A\Gamma \vdash e \Rightarrow A and A=BA = B, then Γ⊢e⇐B\Gamma \vdash e \Leftarrow B.

The lambda carries no type. The mode switch goes one way only: a synthesizing term can be checked, but a checking-only term must be annotated to synthesize.

How to orient rules: the Pfenning recipe. Dunfield and Pfenning (2004) gave a recipe, which the survey restates in Section 4.

  1. Find the principal judgment of each rule: the one that holds the connective that the rule introduces or eliminates. For an introduction rule it is usually the conclusion. For an elimination rule it is the first premise.
  2. Make the principal judgment checking for introduction rules and synthesizing for elimination rules. So λ\lambda, the arrow introduction, checks, and application, the arrow elimination, synthesizes its function.
  3. Orient the other premises so that known types are used: if a type is already known, check against it. In App⇒, the type AA is known once e1e_1 synthesizes, so e2e_2 is checked against AA.
  4. Variables synthesize, annotations synthesize and check their body, and one mode switch rule links the two modes.

The algorithm. Each rule has a distinct term form in its conclusion, except Sub⇐, which applies only to term forms that have no checking rule. So the rules are syntax-directed and read as code:

synth(G, e):
  x          -> G(x)
  n          -> Int
  (e : A)    -> check(G, e, A); A
  e1 e2      -> A -> B := synth(G, e1) (else error "not a function"); check(G, e2, A); B
  e1 + e2    -> check(G, e1, Int); check(G, e2, Int); Int
  otherwise  -> error "annotation needed"

check(G, e, T):
  \x. e1               -> T must be A -> B; check(G + x:A, e1, B)
  if e1 then e2 else e3 -> check(G, e1, Bool); check(G, e2, T); check(G, e3, T)
  otherwise            -> A := synth(G, e); A must equal T

Soundness. If Γ⊢e⇒A\Gamma \vdash e \Rightarrow A or Γ⊢e⇐A\Gamma \vdash e \Leftarrow A, then the term ∣e∣|e| with annotations erased has type AA in the ordinary (Curry-style) STLC. Proof: induction on the derivation. Each bidirectional rule, with its arrows read as colons, is an STLC rule. Anno⇒ erases to its premise, and Sub⇐ with A=BA = B is the identity.

Completeness for normal forms. Define neutral terms uu and normal terms mm by

u::=x∣n∣true∣false∣u m∣m+m,m::=u∣λx. m∣if m then m else m.u ::= x \mid n \mid \mathsf{true} \mid \mathsf{false} \mid u\ m \mid m + m, \qquad m ::= u \mid \lambda x.\,m \mid \mathsf{if}\ m\ \mathsf{then}\ m\ \mathsf{else}\ m.

If Γ⊢u:A\Gamma \vdash u : A in the STLC, then Γ⊢u⇒A\Gamma \vdash u \Rightarrow A. If Γ⊢m:A\Gamma \vdash m : A, then Γ⊢m⇐A\Gamma \vdash m \Leftarrow A. Proof: mutual induction on the term. A variable synthesizes its context type, and literals synthesize their types. For u mu\ m, the STLC derivation gives u:B→Au : B \to A and m:Bm : B. By induction, u⇒B→Au \Rightarrow B \to A and m⇐Bm \Leftarrow B, so App⇒ gives u m⇒Au\ m \Rightarrow A. For m1+m2m_1 + m_2, the STLC gives A=IntA = \mathsf{Int} and both operands of type Int\mathsf{Int}, and Add⇒ applies. For λx.m\lambda x.m, we have A=B→CA = B \to C and Γ,x:B⊢m:C\Gamma, x{:}B \vdash m : C, and Abs⇐ applies. For if\mathsf{if}, If⇐ applies by induction. For a neutral m=um = u, induction gives u⇒Au \Rightarrow A, and Sub⇐ gives u⇐Au \Leftarrow A. □\square

So a normal term needs no annotation at all, as long as its expected type is given at the top. Every other typable term becomes checkable when each term in a synthesis place that is not neutral gets an annotation. In this language, these places are the heads of beta-redexes and the heads of applications whose function is an if\mathsf{if}.

Cost. Each node of the term is visited once. The only other work is the type comparison in Sub⇐. With hash-consed types each comparison takes constant time, so the whole check is linear in the size of the annotated term, as for the type checking problem.

Classic examples

A script runs the checker above on each example and logs the rules it uses.

Twice, with no annotation inside. Check λf. λx. f (f x)⇐(Int→Int)→Int→Int\lambda f.\,\lambda x.\,f\,(f\,x) \Leftarrow (\mathsf{Int} \to \mathsf{Int}) \to \mathsf{Int} \to \mathsf{Int}. Abs⇐ twice puts f:Int→Intf : \mathsf{Int} \to \mathsf{Int} and x:Intx : \mathsf{Int} in the context, and leaves f (f x)⇐Intf\,(f\,x) \Leftarrow \mathsf{Int}. This is not a lambda or an if, so Sub⇐ synthesizes it. App⇒ synthesizes f⇒Int→Intf \Rightarrow \mathsf{Int} \to \mathsf{Int} and checks f x⇐Intf\,x \Leftarrow \mathsf{Int}, which in turn goes through Sub⇐, App⇒, Var⇒ for ff, and Sub⇐ and Var⇒ for xx. The script logs 10 rule uses: 2 Abs⇐, 3 Sub⇐, 2 App⇒ and 3 Var⇒. Neither binder needed a type.

A redex needs an annotation. (λx. x+1) 3(\lambda x.\,x + 1)\ 3 fails in both modes. App⇒ must synthesize the type of its function, and no rule synthesizes a lambda. The script reports "cannot synthesize a type for a lam" after one rule in synthesis mode, and after two rules in checking mode, where Sub⇐ comes first. With one annotation, ((λx. x+1):Int→Int) 3⇒Int((\lambda x.\,x + 1) : \mathsf{Int} \to \mathsf{Int})\ 3 \Rightarrow \mathsf{Int} succeeds, with 11 rule uses.

An annotated function applied to arguments. In the context g:Int→Intg : \mathsf{Int} \to \mathsf{Int}, the term (twice:(Int→Int)→Int→Int) g 3(\mathit{twice} : (\mathsf{Int} \to \mathsf{Int}) \to \mathsf{Int} \to \mathsf{Int})\ g\ 3 synthesizes Int\mathsf{Int}, with 17 rule uses. The arguments gg and 33 are checked against the parameter types that the annotation provides.

A conditional in function position. With b:Boolb : \mathsf{Bool}, the term (if b then λx.x else λx. x+1) 3(\mathsf{if}\ b\ \mathsf{then}\ \lambda x.x\ \mathsf{else}\ \lambda x.\,x + 1)\ 3 has no beta-redex, but it fails, because if\mathsf{if} only checks. Annotating the conditional with Int→Int\mathsf{Int} \to \mathsf{Int} fixes it: the script finds Int\mathsf{Int} with 17 rule uses.

Error location. Checking λx. x+true⇐Int→Int\lambda x.\,x + \mathsf{true} \Leftarrow \mathsf{Int} \to \mathsf{Int} fails with "expected Int, found Bool" at the subterm true\mathsf{true}, because the expected type Int\mathsf{Int} was pushed down to it. A unification-based checker would report a clash between two types and might name a different place.

Partial application in checking mode. With f:Int→Int→Intf : \mathsf{Int} \to \mathsf{Int} \to \mathsf{Int} and x:Intx : \mathsf{Int}, the term f xf\,x checks against Int→Int\mathsf{Int} \to \mathsf{Int} through Sub⇐, in 5 rule uses.

Variations

  • Pierce and Turner (2000). This paper made the idea widely known, and it is the earliest bidirectional paper that is often cited (survey, Section 10). It combines two parts for a language with subtyping and impredicative polymorphism. The first is the bidirectional propagation of types, as above. The second is the synthesis of missing type arguments at each polymorphic application, by solving the subtyping constraints of that one application alone. The design goal is to remove the annotations that are both common and boring, such as type arguments, and to keep the ones that serve as documentation (Section 1.1). Type arguments are inferred only for prenex polymorphic functions. Other instantiations must be written.
  • Subtyping. Replace the equality in Sub⇐ by the subtype check A<:BA <: B. This is the place where subsumption is used, and it keeps the system syntax-directed (Pierce, Chapter 16).
  • Higher-rank polymorphism. Dunfield and Krishnaswami (2013) give a bidirectional system for predicative higher-rank polymorphism. It instantiates quantifiers with existential variables in an ordered context, and they prove it sound, complete and decidable. Peyton Jones, Vytiniotis, Weirich and Shields (2007) describe the arbitrary-rank system that GHC uses. Both escape Wells' theorem because the programmer writes the polymorphic types of arguments.
  • Normal forms only. Remove Anno⇒ and allow Sub⇐ only at base types. Then the typable terms are exactly the beta-normal, eta-long terms (survey, Section 7 and Figure 2). Under the Curry-Howard correspondence, synthesis is elimination and checking is introduction, and this system is the normal natural deduction proofs.
  • Dependent types. Type checkers for dependent types check a lambda against a known Pi type, and synthesize the types of variables and applications, in the same way. The survey (Section 9.1) lists many such systems.
  • Sum elimination. A case can have a checking rule and a synthesizing rule. The original recipe had only the checking one (survey, Section 4, footnote 2).

Pitfalls

  • It is not inference. Bidirectional checking does not find the types of unannotated binders that occur in synthesis places. let f = fun x -> x + 1 in f 3 fails, because the let rule synthesizes the bound term. The script reports this failure. One annotation, let f = (fun x -> x + 1 : Int -> Int) in f 3, fixes it. Checking and inference are different problems, and a bidirectional system only promises the first.
  • "Annotations only at redexes" is false in general. Dunfield and Pfenning claimed that annotations are needed only at redexes. The survey (Section 4.1) shows that this holds for eliminations that do not bind variables. With binding eliminations such as case, some terms with no redex still need annotations. The conditional example above shows the same thing with if.
  • Mode correctness. Each premise must have all its inputs known when it is checked. A rule such as App with a checking function premise would need the type of the function before it is known, so the algorithm would have to guess.
  • One mode switch, at the right place. If a checking rule and Sub⇐ can both apply to the same term form, the system is no longer syntax-directed, and the algorithm must backtrack. Sub⇐ should apply only to forms that have no checking rule.
  • The equality in Sub⇐. With polymorphism, types must be compared up to the renaming of bound variables. With type-level computation, as in dependent types, they must be compared up to conversion, which is decidable only when that computation terminates.
  • Annotation placement in the grammar. If (e:A)(e : A) is parsed with low precedence, fun x -> e : T can mean an annotated lambda or a lambda with an annotated body. The two give different checks.

Exercises

  1. Extend the template with pairs ⟨e1,e2⟩\langle e_1, e_2 \rangle and projections π1e\pi_1 e, π2e\pi_2 e. Use the Pfenning recipe to choose the modes, and check that the completeness proof for normal forms still goes through.
  2. Give the bidirectional rule for let x = e1 in e2 in checking mode, where e2e_2 is checked against the expected type. Which terms does it accept that the synthesizing let rule rejects?
  3. Prove that if Γ⊢e⇒A\Gamma \vdash e \Rightarrow A and Γ⊢e⇒B\Gamma \vdash e \Rightarrow B, then A=BA = B.
  4. Show that λf. f (λx. x)\lambda f.\,f\,(\lambda x.\,x) checks against ((Int→Int)→Bool)→Bool((\mathsf{Int} \to \mathsf{Int}) \to \mathsf{Bool}) \to \mathsf{Bool} with no annotation, and count the rule uses.
  5. Replace the equality in Sub⇐ by the subtyping relation of the subtyping page with records. Show that the resulting system is sound for the declarative system with the subsumption rule.

References

  1. Jana Dunfield and Neel Krishnaswami, Bidirectional typing, ACM Computing Surveys 54(5), 2021, Article 98, Section 2 (the bidirectional simply typed lambda calculus with Var, Anno, the arrow rules and the mode switch Sub), Section 4 (the Pfenning recipe, with the remark in Section 4.1 that annotations on redexes alone are not enough with binding eliminations), Section 5 (polymorphism), Section 7 and Figure 2 (a bidirectional system for beta-normal, eta-long terms), Section 9.1 (dependent, refinement and intersection types), Section 10 (history)
  2. Jana Dunfield and Frank Pfenning, Tridirectional typechecking, ACM Symposium on Principles of Programming Languages (POPL 2004), pages 281 to 292, the design recipe (introduction rules check, elimination rules synthesize) as restated in Section 4 of the survey above
  3. Benjamin C. Pierce and David N. Turner, ACM Transactions on Programming Languages and Systems 22(1), 2000, pages 1 to 44, Section 1.1 (design goals for partial type inference), the bidirectional propagation of types, and the synthesis of type arguments at polymorphic applications by local constraint solving
  4. Jana Dunfield and Neelakantan R. Krishnaswami, Complete and easy bidirectional typechecking for higher-rank polymorphism, ACM International Conference on Functional Programming (ICFP 2013), pages 429 to 442, the declarative system, the algorithmic system with ordered contexts and existential variables, and its soundness, completeness and decidability theorems
  5. Simon Peyton Jones, Dimitrios Vytiniotis, Stephanie Weirich and Mark Shields, Practical type inference for arbitrary-rank types, Journal of Functional Programming 17(1), 2007, the bidirectional system for arbitrary-rank types that GHC uses, with checking and inference modes
  6. Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 10 (a typechecker for the simply typed lambda calculus), Chapter 16 (algorithmic subtyping), Section 23.6 (erasure and type reconstruction in System F)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples