Theoremadvanced
DEXPTIME-completeness of ML typability
Also called Mairson's theorem on ML typing.
Deciding whether an ML program is typable is complete for deterministic exponential time, since nested lets can double type sizes.
Deciding whether a program of core ML has a type is complete for DEXPTIME, the class of problems that a deterministic machine solves in time for some fixed . The upper bound comes from typing each let as if its definition were copied at each use. The lower bound simulates any exponential time Turing machine by a short ML term whose type inference does the computation. Mairson (1990) and Kfoury, Tiuryn and Urzyczyn (1990) proved the result independently. It explains why algorithm W is exponential in the worst case, and it shows that no faster algorithm exists. In practice the hard terms do not occur, and ML compilers infer types quickly.
Statement
ML typability is the problem: given a closed term of the Hindley-Milner type system, with fun, application, let, constants, if and operators, decide whether for some type . Write for the number of nodes of . DEXPTIME, also written EXPTIME, is .
Theorem (Mairson 1990; Kfoury, Tiuryn and Urzyczyn 1990).
- Upper bound. ML typability can be decided in time .
- Lower bound. For each problem in DEXPTIME there is a function , computable in polynomial time, such that if and only if the term is typable. So ML typability is DEXPTIME-hard under polynomial-time reductions, and with part 1 it is DEXPTIME-complete.
- Size of types. The principal type of a typable term has nodes when equal subtrees are shared, and nodes when it is written as a tree. Both bounds are reached.
In plain words
An ML compiler finds the types of a program with no help. This theorem says how hard that job can be in the worst case.
The answer is: exponentially hard. When a helper function is defined with let and used twice, the compiler in effect types two copies of it. If each helper is built from two uses of the one before, the copies double at each level. A short program can then stand for an enormous one.
The hard direction shows that this doubling is strong enough to run any computer program that finishes in exponential time. True and false become two shapes of type, gates become small functions, and the compiler's matching of types does the computing. So a fast type checker for ML would give a fast way to run all those programs, and that is known to be impossible.
Proof
The full argument is in the proof file. Here is its structure.
Upper bound. The key fact is that let is typed by copying: has the type exactly when has some type and has the type (Pierce, Section 22.7). The proof of the "if" direction uses the principal type theorem: all types of the copies are instances of one scheme of . Expanding every let in this way, with an extra unused argument so that an unused definition is still checked, gives a term with no let and with the same types. Its size is at most , by induction on the term. A term with no let is typable exactly when a set of equations has a unifier, and unification on a term DAG solves this in almost linear time. So the total time is .
Size bounds. The expanded term has at most nodes, so the unification DAG and the principal type have nodes. A DAG with nodes unfolds to a tree with at most nodes. The family $f_0 = $ fun x -> fun z -> z x x, $f_i = $ fun y -> f(i-1) (f(i-1) y) reaches both bounds: its term has nodes, and its principal type has nodes as a DAG and nodes as a tree.
Lower bound. Take a deterministic Turing machine that runs in time , and an input .
- Truth values as types.
fun x -> fun y -> xhas a type of the form , andfun x -> fun y -> yone of the form . The test(b 1 true) + 1is typable for the first and not for the second. - Gates.
not = fun p -> p f t,and = fun p -> fun q -> p q fandor = fun p -> fun q -> p t qcompute the right shape of result type by unification. Because afun-bound variable has one type, each wire is used once, and fan-out goes throughcopy = fun p -> p (pair t t) (pair f f). - Exponential size and time. A step of on a tape of cells is built level by level with
let, each level from two uses of the one below. Thenlet run_i = fun s -> run_(i-1) (run_(i-1) s)repeats the step times with only definitions. - Correctness. By the copying property, the term is typable exactly when its expansion is. In the expansion each
fun-bound variable occurs at most once, and for such terms beta reduction keeps the types in both directions. The expansion reduces to1 + 1if accepts , and totrue + 1if not.
The proof file gives the upper bound and the size bounds in full, and the lower bound as a sketch. Mairson (1990) gives the complete simulation.
Consequences
- W is exponential, not cubic. No algorithm decides ML typability in polynomial time, because DEXPTIME contains problems outside P by the time hierarchy theorem. Every inference algorithm for HM, W, J and the constraint-based ones, has an exponential worst case.
- Sharing is needed for the upper bound. An implementation that copies types as trees takes doubly exponential time on the family . Algorithm J with union-find works on a DAG and stays within .
- The hardness carries over. Each language that contains core ML typing with polymorphic
let, such as Standard ML, OCaml, F# and Haskell, has a DEXPTIME-hard type inference problem. Adding features can only make it harder: polymorphic recursion makes it undecidable. - Error messages can be huge. A type error in a term like can involve a type with millions of nodes. A compiler that prints types as trees can print output of doubly exponential size.
Tightness
- Without
let. Typability in the simply typed lambda calculus needs only one unification problem of linear size. It takes linear time with the Paterson-Wegman algorithm. So the whole cost comes fromlet-polymorphism. - Bounded nesting. If the nesting depth of
letis bounded by a constant , the bound of Lemma 3 of the proof file becomes polynomial: each level of nesting at most squares the size, so the expansion has at most about nodes, and typability is in polynomial time. McAllester (2003) gives a nearly linear bound for programs whose types have bounded order and arity. Real programs are close to this case. - The size bounds are exact in form. The family has a principal type of DAG size and tree size .
- Checking a given type is no easier in general: given and , deciding is also DEXPTIME-hard, since the term of the lower bound has type when it has a type at all.
Proof: ML typability is in DEXPTIME, and it is DEXPTIME-hardsketch
This file proves the two halves of the theorem. The upper bound (part 1) and the size bounds (part 3) have full proofs. They use only the principal type theorem and the copying property of let. The lower bound (part 2) is a sketch of a simulation of an exponential time Turing machine. It follows the plan of Mairson (1990): truth values are encoded as types, a circuit is computed by type inference, and nested let repeats a step exponentially many times. The encoding of the tape below is one way to fill in the plan. Mairson (1990) and Kfoury, Tiuryn and Urzyczyn (1990, 1994) give complete proofs.
Setting
Terms, types and the syntax-directed rules are those of the Hindley-Milner page, with T-Var', T-Abs, T-App and T-Let', the constants, if and the operators. The size of a term is the number of nodes of its syntax tree. A variable or a constant has size 1, and each fun, application, let, if and operator adds 1 to the sizes of its parts. We rename bound term variables so that they are all different and different from the free variables, which changes no size and no type.
Two standard facts about derivations are used. Type substitution: if , then . Weakening and strengthening: adding to a binding for a variable that is not free in , or removing one, does not change the types of . Both are proved by induction on derivations, after the generalized variables of each inner let are renamed apart. Weakening is the lemma on a more general context of the W proof in a special case.
Lemma 1: let by copying
Lemma 1. Let . Then if and only if for some and .
Proof. (Only if.) T-Let' gives and with . Each leaf of the second derivation that uses is a T-Var' step with . The substitution that sends the bound variables of to the chosen types does not touch , so type substitution gives , and weakening gives . Put this derivation in place of the leaf. The result is a derivation of .
(If.) If does not occur in , use T-Let' with and weakening. Otherwise let be the types of the copies of in the derivation of . By strengthening, for each . We need one type with such that each is an instance of .
Let . By the completeness of W (in the proof file of algorithm W) with the identity, for each there is with and . All the agree on the variables of , because is the subterm of at the place where occurs in . So these images only contain variables of . Let agree with them on , and let it send each other variable of to its own fresh variable. Put . By the soundness of W and type substitution, . The substitution that sends each fresh variable back to the image of its original under gives , and it only moves fresh variables, which are not free in . So . Replace the derivation of each copy by a T-Var' step for at the instance . This gives , and T-Let' finishes.
Lemma 2: the expansion
Define a term with no let. It commutes with every form of term except let, and
with fresh. The extra argument makes sure that is checked even when is not used.
Lemma 2. For each and : if and only if .
Proof. By induction on , for all at once. All cases but let follow from the induction hypothesis, since the rules for them only look at the types of the parts. For let, write . By T-Let' and the induction hypothesis in the contexts and , the term has the same types as . By Lemma 1, these are the types of , under the condition that has a type. T-App and T-Abs with an unused express exactly this condition.
Lemma 3. .
Proof. By induction on . A variable or constant has size . For fun, . For an application, , and the cases of if and the operators are the same. For let with and , let and . If occurs times in , then . So
because .
Part 1: the upper bound
Let . Compute in time proportional to its size, which is at most by Lemma 3. By Lemma 2, is typable exactly when is. A term with no let is typable exactly when the set of equations with one type variable per node, at each application and so on, has a unifier. There are equations for a term of size . Unification on a term DAG with union-find solves them in time, and the Paterson-Wegman algorithm in time. So typability is decided in time , which is inside DEXPTIME.
Part 3: the size of principal types
By Lemma 2, the principal type of is the principal type of , up to renaming. Unification on a DAG never creates more nodes than it starts with, so the principal type is a DAG with nodes. A DAG with nodes of out-degree at most 2 has each root path of length at most , so its tree form has at most nodes. This gives the bounds and .
Both bounds are reached. In the family
let f0 = fun x -> fun z -> z x x in
let f1 = fun y -> f0 (f0 y) in
...
let fk = fun y -> f(k-1) (f(k-1) y) in fk
the type of is . The function applies times in a row, and each application puts two copies of the previous type into the next one. So the principal type of the term, of size , has nodes as a DAG and nodes as a tree. Any algorithm that writes types as trees, as a plain implementation of W does, takes doubly exponential time on this family.
Part 2: the lower bound (sketch)
Let be in DEXPTIME. Then a deterministic one-tape Turing machine decides in time for a polynomial . Fix an input of length and let , large enough that . We may assume that uses at most cells and stays in its accepting state once it gets there. We build, in polynomial time, a term of size polynomial in that is typable if and only if accepts .
Step 1: truth values as types. Let t = fun x -> fun y -> x and f = fun x -> fun y -> y, both bound by let. Their schemes are and . Call a type of the form a true type and one of the form a false type, when and are type variables that occur nowhere else in the derivation. For the test (b 1 true) + 1, a true type of b gives , and a false type needs , so the test fails.
Step 2: gates. Define, with let,
let pair = fun a -> fun b -> fun k -> k a b
let not = fun p -> p f t
let and = fun p -> fun q -> p q f
let or = fun p -> fun q -> p t q
let copy = fun p -> p (pair t t) (pair f f)
If p has a true or false type, unification of that type with the argument types of a gate gives the result type of the gate: for example not applied to a true type unifies with , where and are fresh false and true types, so . Each use of t, f and a gate is a fresh instance, so the result again has variables that occur nowhere else. In this way type inference computes the gate. A fun-bound variable has one type, so a wire must not be used twice. Fan-out goes through copy, which turns one truth value into a pair of two independent fresh truth values, read by a continuation fun u -> fun v -> .... So each Boolean circuit with gates becomes a term of size in which each fun-bound variable occurs at most once, and whose type inference evaluates the circuit.
Step 3: one step of the machine. Encode the content of a cell, a state or a tape symbol, as a tuple of truth values, where depends only on . As in the Cook-Levin theorem, the next content of cell is a fixed circuit of cells , and . Store the tape of cells as a complete binary tree of depth , built from pair. For , define with let a function that takes a left neighbour cell, a tree of depth and a right neighbour cell and returns the next tree, and helper functions that copy a tree and that read its first and last cell. Each function of level is defined from the functions of level . These are let-bound, so they may be used several times, each at a fresh instance. Inside each body, each fun-bound variable occurs at most once. Each level adds nodes, with a constant that depends on , so all levels together have size .
Step 4: exponentially many steps. Let with blank boundary cells, and
let run1 = fun s -> run0 (run0 s) in
...
let runm = fun s -> run(m-1) (run(m-1) s) in ...
so that performs steps, as in the family of part 3. The initial tape has in its first cells and blanks elsewhere. Blank trees of each depth come from let blank_i = pair blank_(i-1) blank_(i-1), and the cells of are placed along paths of length , which takes nodes. A last function of size , built the same way, computes the "or" over all cells of the test "this cell holds the accepting state". Let be the test of Step 1 applied to this truth value. Its size is polynomial in , and it is easy to produce from .
Step 5: correctness. By Lemma 2, is typable exactly when is. All let definitions of are closed and sit at the top, so the expansion copies no fun-bound variable, and in each fun-bound variable occurs at most once. For such terms, a beta step does not change the types: subject reduction holds in the simply typed calculus, and subject expansion holds when the bound variable occurs once, or when it does not occur and the discarded argument has a type. In the construction, each discarded argument is a truth value or a tuple of truth values, and it has a type. Each beta step makes the term smaller, so reduces to its normal form, which is 1 + 1 if accepts and true + 1 if not. The first has a type and the second has none. So is typable if and only if accepts .
References
- Harry G. Mairson, Deciding ML typability is complete for deterministic exponential time, POPL 1990, pages 382 to 401. The lower bound by a simulation of an exponential time Turing machine.
- A. J. Kfoury, J. Tiuryn and P. Urzyczyn, ML typability is DEXPTIME-complete, CAAP 1990, LNCS 431, pages 206 to 220, and An analysis of ML typability, Journal of the ACM 41(2), 1994, pages 368 to 398. An independent proof, by an equivalence of ML typability with acyclic semi-unification.
- Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Section 22.7. Typing
letby substitution.
- Harry G. Mairson, Deciding ML typability is complete for deterministic exponential time, Seventeenth ACM Symposium on Principles of Programming Languages (POPL 1990), pages 382 to 401, the DEXPTIME-hardness proof by simulation of an exponential time Turing machine with nested let
- A. J. Kfoury, J. Tiuryn and P. Urzyczyn, ML typability is DEXPTIME-complete, CAAP 1990, Lecture Notes in Computer Science 431, pages 206 to 220, the main theorem, with ML typability shown log-space equivalent to acyclic semi-unification
- A. J. Kfoury, J. Tiuryn and P. Urzyczyn, An analysis of ML typability, Journal of the ACM 41(2), 1994, pages 368 to 398, the journal version of the CAAP 1990 paper, with the full proofs
- Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Section 22.7 (typing let by substitution, and the remark that nested lets make types exponentially large)
- David McAllester, A logical algorithm for ML type inference, Rewriting Techniques and Applications (RTA 2003), Lecture Notes in Computer Science 2706, pages 436 to 451, the nearly linear time bound for programs whose types have bounded order and arity
History
Milner (1978) gave algorithm W. It was fast on real programs, and for about ten years its worst case was not known. Kanellakis and Mitchell (1989) showed that ML typability is PSPACE-hard. In 1990, Mairson closed the gap with DEXPTIME-hardness, by a direct simulation of Turing machines, and Kfoury, Tiuryn and Urzyczyn proved the same result independently. Their proof shows that ML typability is equivalent to acyclic semi-unification, a restricted form of the semi-unification problem. The journal version appeared in 1994. McAllester (2003) explained the good behavior in practice by a bound that is nearly linear when the order and arity of types are bounded.
References
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag M7MW7Q Edit this page History Report an error Markdown