Algorithm Search⌘K

Problemintro

Type checking problem

Also called typechecking.

Given a typing context, a term and a type, decide whether the term has that type under the rules of a type system.

The type checking problem asks whether a given term has a given type in a given context: decide Γ⊢e:τ\Gamma \vdash e : \tau. It is the question a compiler front end answers for every program it accepts. Its difficulty depends on how much type information the term carries. For the explicitly typed simply typed lambda calculus it takes linear time, for ML with let it is DEXPTIME-complete, and for the implicitly typed System F it is undecidable. It is a different problem from type inference, which must find the types.

In plain words

A type checker gets three things: a program fragment, the types of the names it may use, and a claimed type. It answers yes or no: does the fragment have that type under the rules? The checker does not invent anything when the programmer has written the type of every variable. It walks over the program once, works out the type of each piece from its parts, and compares at each function call that the argument has the type the function wants. That is fast. The problem gets harder when the program leaves types out, because then the checker must guess them, and for some type systems no program can always make that guess.

Definition

Fix a type system SS, that is, a grammar of types τ\tau, a grammar of terms ee, and inference rules for the judgment Γ⊢Se:τ\Gamma \vdash_S e : \tau.

  • Instance. A context Γ=x1:τ1,…,xk:τk\Gamma = x_1 : \tau_1, \dots, x_k : \tau_k, a term ee whose free variables are among x1,…,xkx_1, \dots, x_k, and a type τ\tau.
  • Question. Is there a derivation of Γ⊢Se:τ\Gamma \vdash_S e : \tau?

The input size nn is the total number of symbols in Γ\Gamma, ee and τ\tau, type annotations included.

The answer depends on the term syntax. In a Church-style (explicitly typed) system, each binder carries its type, as in λx:Int. x\lambda x{:}\mathsf{Int}.\ x, and type abstractions and applications are written in the term. In a Curry-style (implicitly typed) system, terms are untyped, as in λx. x\lambda x.\ x, and the derivation must choose the types of the binders. The same rules in the two styles give two different checking problems.

Variants

  • Type computation. Given Γ\Gamma and ee, compute a type τ\tau with Γ⊢e:τ\Gamma \vdash e : \tau. In a Church-style system where each term has at most one type (Pierce, Theorem 9.3.3, uniqueness of types), checking is type computation followed by one type comparison.
  • Curry-style checking. Given Γ\Gamma, an untyped ee and τ\tau, decide Γ⊢e:τ\Gamma \vdash e : \tau. The type variables in τ\tau are fixed: they are not to be instantiated.
  • Typability. Given an untyped ee, decide whether some Γ\Gamma and τ\tau exist. This is the decision form of the type inference problem. For System F, Wells (1999) showed that typability and Curry-style checking are equivalent under many-one reductions, and both are undecidable.
  • Partial annotations. Some but not all types are written. Bidirectional type checking handles systems where annotations are needed only at a few places.
  • Checking with subtyping. The comparison of types becomes a subtype test. For subtyping of simple types, records and recursive types, the test is decidable. For full bounded quantification (F<:F_{<:}), the subtype relation is undecidable (Pierce 1994), so checking explicitly typed F<:F_{<:} terms is undecidable.
  • Checking with definitional equality. In dependent type theories, two types are equal if their terms compute to the same normal form. Checking then needs a normalization procedure. It is decidable for theories where every term normalizes, and it can be undecidable when types may contain non-terminating computations.

Examples

Church-style STLC, a yes instance. Let e=λf:Int→Int. λx:Int. f (f x)e = \lambda f{:}\mathsf{Int}{\to}\mathsf{Int}.\ \lambda x{:}\mathsf{Int}.\ f\ (f\ x), Γ\Gamma empty, and τ=(Int→Int)→Int→Int\tau = (\mathsf{Int} \to \mathsf{Int}) \to \mathsf{Int} \to \mathsf{Int}. The bottom-up pass gives x:Intx : \mathsf{Int}, then f x:Intf\ x : \mathsf{Int} after one comparison of the domain Int\mathsf{Int} of ff with the type Int\mathsf{Int} of xx, then f (f x):Intf\ (f\ x) : \mathsf{Int} after a second comparison, then the two arrows from T-Abs. The computed type equals τ\tau, so the answer is yes. The script check computes the same type with two application comparisons.

The same term, a no instance. With τ=(Int→Int)→Bool→Int\tau = (\mathsf{Int} \to \mathsf{Int}) \to \mathsf{Bool} \to \mathsf{Int} the answer is no. The term has the unique type (Int→Int)→Int→Int(\mathsf{Int} \to \mathsf{Int}) \to \mathsf{Int} \to \mathsf{Int}, and it differs from τ\tau in the second argument.

Curry-style, a no instance. For the untyped λx. x\lambda x.\ x and τ=Int→Bool\tau = \mathsf{Int} \to \mathsf{Bool}, the answer is no: T-Abs needs x:Int⊢x:Boolx : \mathsf{Int} \vdash x : \mathsf{Bool}, and T-Var gives only Int\mathsf{Int}. For τ=Int→Int\tau = \mathsf{Int} \to \mathsf{Int} the answer is yes, and so is the answer for τ=α→α\tau = \alpha \to \alpha with a fixed type variable α\alpha.

Why naive comparison is quadratic. Let Tm=Int→Int→⋯→IntT_m = \mathsf{Int} \to \mathsf{Int} \to \dots \to \mathsf{Int} with mm arrows, a type tree with 2m+12m+1 nodes, and take

ek,m=λf:Tm→Tm. λa:Tm. f (f (⋯(f⏟k a)⋯ )).e_{k,m} = \lambda f{:}T_m{\to}T_m.\ \lambda a{:}T_m.\ \underbrace{f\ (f\ (\cdots (f}_{k}\ a)\cdots)).

The term has 6m+2k+76m + 2k + 7 nodes. A checker that compares types as trees does kk comparisons of two copies of TmT_m, so k(2m+1)k(2m+1) node visits. For k=m=50k = m = 50 the term has 407 nodes and the checker makes 5050 node visits. With k=m=n/8k = m = n/8 the cost grows as n2/32n^2/32.

Known algorithms

Church-style STLC: one bottom-up pass. Compute the type of each subterm from the types of its children (Pierce, Chapter 10). T-Var looks up the context, T-Abs builds an arrow, and T-App and T-If compare two types. Each node does constant work besides the comparisons.

Linear time with shared types. Two ways remove the quadratic cost.

  1. Hash-consing. Store each type once, in a table keyed by its constructor and the numbers of its children (hash-consing). Then equal types are the same node, and a comparison is one test of two numbers. Each annotation node and each arrow built by T-Abs costs one table lookup, so the pass takes expected O(n)O(n) time with a hash table, or O(nlog⁡n)O(n \log n) worst case with a balanced search tree on pairs of numbers.
  2. Reduction to unification. Give each subterm e′e' an unknown Xe′X_{e'} and write one equation per node: Xx=AX_{x} = A for a variable bound with annotation AA or given AA by Γ\Gamma, Xλx:A.b=A→XbX_{\lambda x{:}A.b} = A \to X_b, Xe1=Xe2→Xe1e2X_{e_1} = X_{e_2} \to X_{e_1 e_2}, Xc=BoolX_{c} = \mathsf{Bool} and Xt=Xf=Xif c t fX_{t} = X_{f} = X_{\mathsf{if}\ c\ t\ f} for a conditional, and Xe=τX_e = \tau at the root. The rules are syntax directed, so a solution of the equations is exactly an assignment of a type to each node that satisfies every rule, and Γ⊢e:τ\Gamma \vdash e : \tau holds if and only if the equations are unifiable. The system has O(n)O(n) symbols, and the Paterson-Wegman algorithm decides it in worst-case O(n)O(n) time.

Curry-style STLC. Use the same equations with a fresh unknown for each binder instead of its annotation, and treat the type variables of τ\tau and Γ\Gamma as constants. Unifiability is again equivalent to Γ⊢e:τ\Gamma \vdash e : \tau, so this problem is also linear with Paterson-Wegman. The type inference problem page gives the details of constraint generation.

ML (Hindley-Milner). Compute the principal type of ee with algorithm W or algorithm J, then test whether τ\tau is an instance of it by one-way matching. The principal type can be exponentially large as a DAG, so this is exponential time in the worst case.

Church-style System F. One bottom-up pass, as for STLC, with two more rules: a type abstraction Λα.e\Lambda\alpha.e gets ∀α.τ\forall\alpha.\tau, and a type application e [T]e\,[T] substitutes TT for the bound variable. Types are compared up to the renaming of bound type variables (for example with de Bruijn indices). This decides checking for every term.

Bounds

  • Church-style and Curry-style STLC: Θ(n)\Theta(n). The upper bound is the Paterson-Wegman reduction above, and the lower bound holds because the checker must read the whole input.
  • ML with let (Curry style, Hindley-Milner): DEXPTIME-complete. The upper bound is algorithm W followed by matching. Hardness comes from typability (Mairson 1990; Kfoury, Tiuryn and Urzyczyn 1990; see ML typability is DEXPTIME-complete) by the reduction in the next section. With a fixed bound on the nesting depth of let, the cost is polynomial.
  • Church-style System F: decidable. Curry-style System F: undecidable (Wells' theorem). Typability in the rank-2 fragment of System F is decidable, and typability at every finite rank of 3 or more is undecidable (Kfoury and Wells 1994).
  • Explicitly typed F<:F_{<:} with the full contravariant rule for bounded quantifiers: undecidable (Pierce 1994, by a reduction from two-counter machines). The kernel variant, which requires equal bounds, is decidable.

Reductions

  • Typability reduces to checking, for ML. Let ee be closed, and let e′=let u=e in fun v→ve' = \mathsf{let}\ u = e\ \mathsf{in}\ \mathsf{fun}\ v \to v. If ee has a type, T-Let types e′e' with the type of the body, and ⊢e′:Int→Int\vdash e' : \mathsf{Int} \to \mathsf{Int}. If ee has no type, then T-Let has no premise for ee, and e′e' has no type at all. So ee is typable if and only if ⊢e′:Int→Int\vdash e' : \mathsf{Int} \to \mathsf{Int}, and the map from ee to e′e' takes linear time. This transfers DEXPTIME-hardness from typability to checking.
  • Typability reduces to checking, for System F. Wells uses the same idea with the term K I (λx1…xk. M)K\,I\,(\lambda x_1 \dots x_k.\,M) and the type ∀α.α→α\forall\alpha.\alpha \to \alpha, where K=λu.λv.uK = \lambda u.\lambda v.u and I=λw.wI = \lambda w.w. The details are on the Wells' theorem page.
  • Checking reduces to unification, for simple types, as shown above. This is the bridge from type systems to first-order unification and the most general unifier.
  • Semi-unification reduces to Curry-style System F checking (Wells 1999, Theorem 4.1). Semi-unification is undecidable, so checking is undecidable too.

Applications

  • Compilers. Every statically typed compiler solves this problem on each function. Java, C# and Go use mostly Church-style syntax for declarations, so their checkers are close to the linear bottom-up pass, with extra work for overloading and generics.
  • Proof assistants. The kernel of Coq, Lean or Agda is a type checker for an explicitly typed dependent type theory. By the Curry-Howard correspondence, checking the proof term is checking the proof.
  • Bytecode verification. The JVM verifies class files by a type-checking pass over the bytecode before it runs them.
  • Typed intermediate languages. GHC can check its typed core language (System FC, a Church-style extension of System F) after each optimization pass. The check is turned on by a flag (-dcore-lint), and it catches bugs in the optimizer.

References

  1. Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 9, Section 9.3 (uniqueness of types and the inversion lemma for the simply typed lambda calculus), Chapter 10 (the typechecker as one recursive function), Section 23.6 (erasure and typability in System F), Chapter 28 (undecidability of bounded quantification)
  2. Michael S. Paterson and Mark N. Wegman, Linear unification, Journal of Computer and System Sciences 16(2), 1978, pages 158 to 167, the main theorem (unifiability of a set of term equations in time linear in the size of the input; STOC 1976 version)
  3. Harry G. Mairson, Deciding ML typability is complete for deterministic exponential time, POPL 1990, pages 382 to 401, the DEXPTIME-hardness of ML typability
  4. Assaf J. Kfoury, Jerzy Tiuryn and Pawel Urzyczyn, ML typability is DEXPTIME-complete, CAAP 1990, LNCS 431, pages 206 to 220, the upper and lower bounds for ML typability
  5. Joe B. Wells, Typability and type checking in System F are equivalent and undecidable, Annals of Pure and Applied Logic 98(1-3), 1999, pages 111 to 156, Theorem 4.1 (type checking reduces from semi-unification) and Section 6 (typability and type checking are equivalent); LICS 1994 version, pages 176 to 185
  6. Assaf J. Kfoury and Joe B. Wells, A direct algorithm for type inference in the rank-2 fragment of the second-order lambda-calculus, LFP 1994, pages 196 to 207, the abstract and main results (typability is decidable at rank 2 and undecidable at every rank k of 3 or more)
  7. Benjamin C. Pierce, Bounded quantification is undecidable, Information and Computation 112(1), 1994, pages 131 to 165, the reduction from two-counter machines to the subtype relation of full F-sub (POPL 1992 version)

Where this leads

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
  • a known algorithm
  • a contract.yaml