Theoremresearch
Undecidability of polymorphic recursion
Also called Milner-Mycroft typability undecidability.
Type inference for ML with polymorphic recursion is undecidable, as it is equivalent to semi-unification.
Polymorphic recursion lets a recursive function call itself at a different instance of its own type. Mycroft (1984) proposed a typing rule for it as an extension of the Hindley-Milner type system, where a recursive function is monomorphic inside its own body. With the rule, more programs have types, but type inference becomes impossible: in 1993, Henglein and, independently, Kfoury, Tiuryn and Urzyczyn showed that typability with this rule is equivalent to the semi-unification problem, which is undecidable. Checking stays decidable when the programmer writes the type scheme of the recursive function. This is what Haskell and OCaml do.
Statement
Extend the terms of the Hindley-Milner page with fix f x -> e. The ML rule T-Fix gives f a plain type in the body. Mycroft's rule, in syntax-directed form, gives it a scheme:
Gamma, f : tau1 -> tau2, x : tau1 |- e : tau2 Gamma, f : Gen(Gamma, tau1 -> tau2), x : tau1 |- e : tau2
--------------------------------------------- T-Fix --------------------------------------------------------- T-FixPoly
Gamma |- fix f x -> e : tau1 -> tau2 Gamma |- fix f x -> e : tau1 -> tau2
Typability with polymorphic recursion is the problem: given a closed term , decide whether for some , with T-FixPoly in place of T-Fix.
Theorem (Henglein 1993; Kfoury, Tiuryn and Urzyczyn 1993).
- Typability with polymorphic recursion and semi-unification reduce to each other in logarithmic space.
- So typability with polymorphic recursion is undecidable. It is semi-decidable: the set of typable terms is recursively enumerable, and its complement is not.
In plain words
In ML, when a function calls itself, the inner call must use the function at the same type that is being worked out. A more generous rule lets the inner call use the function at any type that fits its final general form. Some useful programs, for example on nested data structures, need this freedom.
The catch is that the compiler must now guess the general form before it has finished reading the function, and each inner call may see a different copy of it. The theorem shows that this guessing problem is as hard as a known puzzle about patterns, semi-unification, and that no program solves that puzzle in all cases. So no compiler can find these types by itself every time. If the programmer writes the type of the function, the compiler only has to check it, and that is easy.
Proof
The two reductions are in the proof file. Here is their structure.
From typability to semi-unification. Give each node of the term a type variable. Applications, abstractions and constants give equations, as in the simply typed calculus. Each use of a let-bound or fix-bound name, bound at , must have a type that is an instance of the type of the definition, where only the variables that are not free in the context may change. This is the inequality
where is the tuple of the types of the fun-bound variables in scope at . A semi-unifier with residual must satisfy , so cannot move the variables of the context. That is exactly the side condition of generalization. The term is typable if and only if the system has a semi-unifier, and the system has linear size.
From semi-unification to typability. Take inequalities between types over the variables . Build one recursive function fix f x1 -> fun x2 -> ... -> fun xk -> body. With eq = fun a -> fun b -> if true then a else b, small terms built from the have exactly the types and with replaced by the type of . The body forces the result type of f to be under this replacement . The -th recursive call passes the term of type at place and bot = (fix g y -> g y) 0, which fits any type, everywhere else. T-FixPoly lets each call take its own instance of the type of f, so the call is typable exactly when . So the term is typable if and only if can be chosen as a semi-unifier. The papers also code an arbitrary signature of function symbols into types.
Undecidability. Kfoury, Tiuryn and Urzyczyn (1993) proved semi-unification undecidable, by a reduction from the boundedness problem of Turing machines. The second reduction carries this over to typability. The first reduction shows that typability is semi-decidable, since semi-unifiers can be listed and checked.
Consequences
- Inference must stop at recursive functions. A compiler cannot infer polymorphically recursive types for all programs. Haskell accepts polymorphic recursion only for a function with a type signature. OCaml asks for an explicit polymorphic annotation,
let rec f : 'a. .... - Checking is decidable. If each polymorphically recursive function has a written scheme,
fhas that scheme in its body, and the rest of the program is typed by unification as in algorithm W. The written scheme removes the guess. This is a simple case of bidirectional type checking. - An example that needs it. In
fix f x -> if true then 0 else f true + f 1, the two calls usefat and . T-Fix fails, since it needs one argument type. T-FixPoly gives . Functions on nested data types, such asdata Nested a = Flat a | Nest (Nested [a])in Haskell, need the same freedom in real code. - No monomorphization. A polymorphically recursive function can be used at infinitely many types in one run, such as , , and so on. So a compiler cannot make one copy per type, and monomorphization fails. Implementations use a uniform representation or dictionaries instead.
- Semi-algorithms exist. Since the problem is semi-decidable, a type checker can search for a typing and succeed on every typable term. It cannot always stop on an untypable one.
Tightness
- Plain ML recursion. With T-Fix, typability is decidable and complete for DEXPTIME. Kfoury, Tiuryn and Urzyczyn (1990) showed that it is equivalent to acyclic semi-unification, the decidable special case in which the inequalities have no cycles. Polymorphic recursion is what creates the cycles.
- One inequality. Semi-unification with one inequality is decidable. But a single recursive function with several recursive calls already gives several inequalities, and Reduction 2 needs only one such function.
- Annotations. With a written scheme for each polymorphically recursive function, the problem is no harder than ML typability.
- System F. Typability in System F is undecidable for a related reason: Wells (1999) reduced semi-unification to it.
Proof: Typability with polymorphic recursion is equivalent to semi-unificationsketch
This file gives the two reductions between typability in ML with polymorphic recursion and the semi-unification problem, and derives undecidability from the undecidability of semi-unification. The reductions follow Henglein (1993) and Kfoury, Tiuryn and Urzyczyn (1993), who prove them in full and show that both run in logarithmic space. The first reduction below is given with its correctness argument. The second is given for systems whose terms are already types. The papers also code an arbitrary signature of function symbols into types, and that coding is not repeated here. This is why the status is sketch.
Setting
Terms and rules are those of the Hindley-Milner page, in syntax-directed form, with T-FixPoly in place of T-Fix:
Gamma, f : Gen(Gamma, tau1 -> tau2), x : tau1 |- e : tau2
--------------------------------------------------------- T-FixPoly
Gamma |- fix f x -> e : tau1 -> tau2
Mycroft (1984) states the rule as for any scheme . The two forms derive the same judgments: from with not free in , the scheme is at least as general, and a more general scheme in the context keeps each judgment true.
A semi-unifier of a system is a substitution such that for each some has . Equations may also appear in a system. They add nothing, since holds under exactly when does, for a fresh (see the semi-unification page). We write for a tuple, coded with a fresh function symbol.
Reduction 1: from typability to semi-unification
Let be a closed term. Rename the bound variables so that they are all different. Give each node of a type variable , and each fun-bound variable and each fix argument a type variable . Call let-bound and fix-bound names polymorphic. For each polymorphic name, let be its binding node and the tuple of for the fun-bound variables and fix arguments in scope at , outside the definition. Let be the type of the definition: for let x = e1 in e2, and for fix f x -> e. The system has:
| node | constraint |
|---|---|
a fun-bound variable or fix argument |
|
a constant, if, an operator |
the equation of its rule, such as for 1 |
fun y -> e1 |
|
e1 e2 |
|
let x = e1 in e2 |
|
fix f y -> e1 |
|
| a use of a polymorphic name bound at |
The system has size linear in .
Lemma 1. is typable if and only if has a semi-unifier. If is one, then is a type of the node in one derivation.
Proof. The equations are those of the simply typed calculus, so they hold exactly when the types fit the rules other than T-Var' for polymorphic names. Consider such a use of a name bound at . In a derivation, the context at maps each fun-bound variable and fix argument in scope to , and each outer polymorphic name to a scheme. By induction on the nesting of binders, the free variables of these schemes are free in the types of the variables in scope at the outer binder, so . The T-Var' step at asks that is an instance of , that is, for a that does not move the variables of . The inequality says that and . The second equation holds exactly when fixes each variable of . On the variables of , the two conditions on are the same. So a derivation gives a semi-unifier, by reading off the types of the nodes, and a semi-unifier gives a derivation, by building it from the leaves up with the types .
The tuple is the whole trick. It turns the side condition of generalization, "do not touch the variables of the context", into an equation that the residual substitution must satisfy.
Reduction 2: from semi-unification to typability
Let be a system whose terms are types, built from type variables , the arrow, and , with . Use two helper terms:
eq = fun a -> fun b -> if true then a else b
bot = (fix g y -> g y) 0
The type of eq is , so eq a b forces and to have the same type. The term bot has a fresh type variable as its type, so it fits any argument position. Both are written out at each use, or bound once with let. For a type over , define a term with free variables :
If each has the type , then has exactly the type : the argument y is forced to the type of . Now let
fix f x1 -> fun x2 -> ... -> fun xk ->
(fun d1 -> ... -> fun dn ->
fun w1 -> ... -> fun wn -> (fun c1 -> ... -> fun cn -> 0) (eq w1 B(s1)) ... (eq wn B(sn)))
(f bot ... bot B(t1) bot ... bot)
...
(f bot ... bot bot ... bot B(tn))
In the -th argument line, f gets copies of bot, then arguments: at place and bot elsewhere.
Lemma 2. This term is typable if and only if the system has a semi-unifier.
Proof. Let the type of f be , with , and put . The last line of the body has type , because each is forced to the type of . So . The context outside the fix is empty, so T-FixPoly gives f the scheme that quantifies all variables of its type. The -th use of f takes an instance under some . The bot arguments fit any types. The argument at place is typable exactly when , since has the type . The other parts of the body always have types. So the term is typable exactly when some and satisfy all equations, that is, when is a semi-unifier. Conversely, a semi-unifier gives the types .
The term has size linear in the size of the system, and it uses one polymorphic recursive function.
Undecidability
Semi-unification is undecidable (Kfoury, Tiuryn and Urzyczyn 1993, Information and Computation), and Reduction 2, together with the coding of signatures in the papers, maps it to typability. So typability with polymorphic recursion is undecidable. Reduction 1 maps typability to semi-unification, whose positive instances can be listed. So typability is semi-decidable: a program can confirm each typable term, and no program can reject each untypable one.
References
- Alan Mycroft, Polymorphic type schemes and recursive definitions, International Symposium on Programming, LNCS 167, 1984, pages 217 to 228. The type system with polymorphic recursion.
- Fritz Henglein, Type inference with polymorphic recursion, ACM TOPLAS 15(2), 1993, pages 253 to 289. Both reductions, in log space.
- A. J. Kfoury, J. Tiuryn and P. Urzyczyn, Type reconstruction in the presence of polymorphic recursion, ACM TOPLAS 15(2), 1993, pages 290 to 311. Both reductions, independently.
- A. J. Kfoury, J. Tiuryn and P. Urzyczyn, The undecidability of the semi-unification problem, Information and Computation 102(1), 1993, pages 83 to 101.
- Alan Mycroft, Polymorphic type schemes and recursive definitions, International Symposium on Programming, Lecture Notes in Computer Science 167, 1984, pages 217 to 228, the typing rule that gives a recursive definition a type scheme inside its own body
- Fritz Henglein, Type inference with polymorphic recursion, ACM Transactions on Programming Languages and Systems 15(2), 1993, pages 253 to 289, the log-space reductions from typability with polymorphic recursion to semi-unification and back
- A. J. Kfoury, J. Tiuryn and P. Urzyczyn, Type reconstruction in the presence of polymorphic recursion, ACM Transactions on Programming Languages and Systems 15(2), 1993, pages 290 to 311, the equivalence of typability with polymorphic recursion and semi-unification, and the resulting undecidability
- A. J. Kfoury, J. Tiuryn and P. Urzyczyn, The undecidability of the semi-unification problem, Information and Computation 102(1), 1993, pages 83 to 101, the main theorem, by reduction from the boundedness problem for Turing machines
History
Mycroft (1984) proposed giving recursive definitions a type scheme in their own bodies. His motivation was that mutually recursive definitions in ML get less general types than the same definitions written apart. The decidability of typability stayed open. Kfoury, Tiuryn and Urzyczyn and, independently, Henglein reduced it to semi-unification and back. Kfoury, Tiuryn and Urzyczyn then proved semi-unification undecidable in 1990, with the journal versions of all three papers in 1993. Haskell accepts polymorphic recursion for functions with a type signature, and OCaml added explicit polymorphic annotations for it in version 3.12.
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 90HNZ3 Edit this page History Report an error Markdown