Theoremadvanced
Undecidability of type inference for System F
Also called Wells' theorem.
Without type annotations, no algorithm decides whether a pure lambda term has a System F type, or whether it has a given type. Wells proved it in 1994 by a reduction from semi-unification.
Wells' theorem says that type inference for System F is impossible in general. Take a pure lambda term with no type annotations. No algorithm can decide whether the term has a System F type. No algorithm can decide whether it has a given type in a given context. Leivant asked the question in 1983, and it stayed open until Wells solved it in 1994. His proof reduces semi-unification, which Kfoury, Tiuryn and Urzyczyn proved undecidable, to type checking. Then it reduces type checking to typability. The theorem is the reason that languages with first-class polymorphism ask for annotations. It also explains why ML keeps to the prenex fragment.
Statement
Curry-style System F. Types are . Terms are pure: . A context gives types to term variables. The rules are:
- Var: .
- Abs: from , derive .
- App: from and , derive .
- Gen: from with not free in , derive .
- Inst: from , derive .
A term is the erasure of a Church-style term exactly when it has a type in this system.
Theorem (Wells 1994, 1999). The following two problems are undecidable, and each one reduces to the other by a computable map.
- Typability (TYP). Given a term , are there a context and a type with ?
- Type checking (TC). Given , and , is derivable?
Both problems stay undecidable for terms of the -calculus, where every bound variable occurs in its body (Wells, Theorem 4.1 and Theorem 6.16). Both sets are recursively enumerable, because derivations can be listed and checked one by one. So the complement of each set is not recursively enumerable.
In plain words
A Church-style System F program writes every type argument. Checking such a program is easy. This theorem is about the other case, where the programmer writes no types and the compiler must find them all.
The compiler cannot do that in general. A finished type checker would also solve a puzzle that is known to have no solution procedure. In that puzzle, you fill in a pattern once, and the result must then match two other patterns, where each match may fill the leftover blanks in its own way. A polymorphic variable that is used twice behaves exactly like this. Its type is chosen once and then specialized two times. A small fixed program shape copies any such puzzle into a typing question.
So languages with this much polymorphism ask the programmer to write some types, or they limit where polymorphism can appear.
Proof
The proof file is a sketch. It proves the small steps in full. It proves the central reduction from semi-unification in full, in a context with two free variables. It cites the construction that closes this context. The plan follows Dudenhefner (2021), who gave a direct reduction to typability. That proof is checked in Coq. Wells' original route goes from semi-unification to type checking first.
- Two-inequality semi-unification is undecidable. An instance is two pairs of simple types, which have no quantifiers. A solution is three substitutions with for . Kfoury, Tiuryn and Urzyczyn show that this restricted problem is already undecidable (cited).
- Left-uniform form. Put , and , with fresh . The instance is solvable if and only if the original one is. Both pairs now share the same left side.
- The core term. For a left-uniform instance , let and . Let be the closure of over all its free variables, and let
Then has a type in if and only if the instance is solvable. The idea fits in one line. The variable gets one polymorphic type , and its two uses instantiate that type in two ways, to and to . The bound variables play the part of the residual substitutions . The variable forces the type of to be the type of again. The proof of the converse uses the normal form of Giannini and Ronchi Della Rocca, also Wells' Lemma 3.2, in which every Inst step comes before every Gen step. A solution may hold System F types. A pruning map sends it to a solution in simple types.
- Closing the context. A typability question cannot fix the types of free variables. Dudenhefner's prenex simulation (Lemma 29) builds a closed term that is typable if and only if is typable in . Wells' Theorems 6.14 and 6.15 do the same job with invariant type assumptions, a more general tool. This step is cited.
- From typability to type checking. Send , with free variables , to the question of whether , where and . The answer is yes exactly when is typable.
Steps 1 to 4 show that TYP is undecidable. Step 5 gives the same for TC. Wells' Theorem 6.15 reduces TC to TYP, so the two problems are equivalent.
A worked instance
Dudenhefner's Example 33 is the instance . It has no unifier: we would need and , which fails the occurs check. But it is semi-unifiable with
Then and .
In left-uniform form, , and . The proof extends by and , where . The type of is then
and the Church-style term
has type in . Its erasure is . A script with a Church-style checker for System F confirms the two equations, the left-uniform equations, the type and the erasure.
A one-line contrast shows that semi-unification is a strict weakening of unification. The single pair has no unifier, but it is semi-unifiable: take and . The pair has no solution at all, because is always larger than . The script finds no solution with types of size at most 3, as this size argument predicts.
Consequences
- First-class polymorphism needs annotations. A language that allows anywhere in a type cannot infer all types. In Haskell with GHC's
RankNTypesextension, a lambda-bound variable gets a polymorphic type only from a written signature. GHC then pushes the written types into the term, in the style of bidirectional type checking. - ML's restriction is not an accident. Hindley-Milner allows only at the outside of a type and only at
let. That fragment has principal types and a complete inference algorithm. - Church style and Curry style differ. For Church-style System F, type checking is one bottom-up pass, as on the type checking problem page. All the difficulty sits in the missing annotations.
- Typability and checking are equally hard. The Hindley-Milner world might suggest that checking is easier than inference. For Curry-style System F, the two problems are equivalent.
- Semi-algorithms are the best possible. A complete type inference tool for System F must run forever on some untypable terms.
Tightness
- Rank. A type has rank if no lies to the left of or more arrows, counting nesting. Typability is decidable for rank 2. It is undecidable for every rank of 3 or more (Kfoury and Wells 1994). So the boundary sits between rank 2 and rank 3.
- Partial annotations. Pfenning (1988) showed that it stays undecidable if the places of type abstractions and type applications are known and only the types are missing. His reduction uses second-order unification, which is undecidable.
- Full annotations. With all type arguments written, the problem is decidable, as stated above.
- Small fragments of terms. Wells' reduction uses only -terms. Step 3 above uses one fixed core term shape and only types without nested quantifiers, so the hardness does not need rich types (Dudenhefner, abstract).
Proof: Typability and type checking in Curry-style System F are undecidablesketch
This file proves the statement on the page as a sketch. The steps from two-inequality semi-unification to typability in a fixed context are proved in full, and so is the step from typability to type checking. Two results are cited: the undecidability of two-inequality semi-unification (Kfoury, Tiuryn and Urzyczyn 1993, Theorem 12 and the remark after it), and the construction that closes a context (Dudenhefner 2021, Lemma 29, or Wells 1999, Theorems 6.14 and 6.15). The normal form for derivations is also cited (Wells 1999, Lemma 3.2; Giannini and Ronchi Della Rocca 1988). The plan is Dudenhefner's, with the core idea from Wells' Theorem 4.1.
Setting
Types, terms and the rules Var, Abs, App, Gen and Inst are as on the page. A type is simple if it has no quantifier. A substitution maps type variables to types and acts on all free variables at once, avoiding capture. We write for the closure of over all its free variables, in any fixed order. means that for some .
Two-inequality semi-unification (SU2). Given simple types , are there substitutions into simple types with and ?
Left-uniform SU2 (LU). The same question for instances with .
Fact A (cited). SU2 is undecidable. Semi-unification is undecidable by a reduction from Turing machine boundedness, and the reduction yields instances with two inequalities (Kfoury, Tiuryn and Urzyczyn 1993, Theorem 12 and the remark after it; restated as Theorem 31 in Dudenhefner 2021).
Fact B (cited). If , then there is a derivation of the same judgment in which no Inst step directly follows a Gen step (Wells 1999, Lemma 3.2, the INST-before-GEN property; Giannini and Ronchi Della Rocca 1988). So in such a derivation, the type of each subterm is produced by one of Var, Abs or App, followed by some Inst steps, followed by some Gen steps.
Fact C. If , then , where is restricted to the free variables of . And if and is not in , then after the renaming of type variables that Gen steps bind. Both follow by induction on the derivation. The renaming is needed because Gen asks for variables that are not free in the context.
Two simple remarks are used many times. First, an Inst step needs a type of the form , and Inst does not touch quantifiers below an arrow. So if a type is with an arrow, then a chain of Inst steps from it yields , for some and some that moves only . Second, App needs its function premise to have an arrow type, and Gen produces a -type. So a chain of steps whose result must be an arrow ends with zero Gen steps.
Step 1: SU2 reduces to LU
Given , pick fresh and put
If the original instance has a solution , let agree with except that and . Since do not occur in ,
If solve the new instance, compare the two sides of each arrow. The left sides of the first equation give , and the right sides of the second give .
Remark: in Dudenhefner's printed proof of Lemma 35, the definition of reads . The computation after it needs , as above. On the worked instance of the page, the printed form gives but , with . So the equation fails there, and the form above is the one that works.
Step 2: pruning quantifiers out of a solution
Fix a type variable . Define from all types to simple types by , and (Dudenhefner, Definition 37).
Lemma. Let be any substitution, and let be a type in which does not occur. Define and for . Then .
Proof. Induction on . For a variable , which differs from , both sides are . For an arrow, use the induction hypothesis on both sides. For , the left side is , and is again a -type, so the right side is as well.
Corollary. Let be simple, and let be substitutions into arbitrary types with . Pick not in . Then maps into simple types, and with from the lemma, .
Proof. For simple , by induction on , since has no quantifier. So .
For an LU instance, one that is fresh for serves both inequalities, and is shared. So a solution of LU in arbitrary types gives a solution in simple types.
Step 3: LU reduces to typability in a fixed context
Let be an LU instance. Put and , and
Claim. if and only if the instance is solvable.
From a solution to a typing. Let solve the instance in simple types. Let list the free variables of , and let . It is closed. In the context :
- by Var and Inst, instantiating the closure with ;
- by Var and Inst, instantiating with . This type equals . Only the variables matter here, since they are all the variables of ;
- in the same way, ;
- so by App twice, and by Gen on each variable of . This is allowed because and are closed.
Abs gives . Inst gives , and App gives .
From a typing to a solution. Take a derivation of with the property of Fact B. Read it from the leaves.
- The head . It must get an arrow type for the outer App. Var gives . The body is an arrow, so by the first remark, a chain of Inst steps ends in an arrow only after it instantiates , and then it has for some type . By the second remark, no Gen step follows. So the argument must get the type .
- The argument. Abs gives a type , where . No Inst step applies to an arrow. A Gen step would give a -type, not . So , and .
- The variable . It is the function of an App, so it gets an arrow type. Var gives the closed type , and its body is an arrow. By the two remarks, all of are instantiated and no Gen follows. So for a substitution into arbitrary types.
- The term . App gives it the type , and the first gets the type . This type is an arrow, so no Inst follows. It is the function of the outer App, so no Gen follows. So the second gets the type , and App gives the type .
- The result of . is an arrow, so no Inst step follows, and only Gen steps can follow. So for some list .
- The two uses of . Var gives . The result must be the arrow . By the two remarks, all of are instantiated, by some that moves only , and no Gen follows. So for .
Compare the left sides of the arrows in step 6: for . This is a solution of the LU instance in arbitrary types. Step 2 turns it into a solution in simple types.
The doubled types and are arrows, whatever and are. The argument above uses this in steps 4, 5 and 6. If were a lone variable and were a -type, Inst steps could reach inside , and the reading of the derivation would break.
Step 4: closing the context (cited)
Typability asks for some context, so a free variable can get any type. Step 3 needs and to have exactly their types in . For a term , a term variable and a simple type , Dudenhefner's Lemma 29 (prenex simulation) builds a term in which is not free, such that for every context without :
The construction applies to a term . That term is built so that each of its types gives no more freedom in than does. It uses a few gadgets. One gadget forces a variable to have a type . Another assumption lets the term build a value of any simple type from variables of the right types. Wells' Theorem 6.14 reaches the same goal with invariant type assumptions. Those are type assumptions that every derivation must use, and the construction is more general and much longer.
Apply the lemma twice: first with and the type of in , then with . The result is a closed term , computable from the instance. By the lemma and Fact C, is typable if and only if (Dudenhefner, Lemma 40). Together with Steps 1 to 3 and Fact A, TYP is undecidable (Dudenhefner, Corollary 41).
Step 5: typability reduces to type checking
Let have free variables , and put with and . We show that is typable if and only if (Dudenhefner, Lemma 42).
If is typable, Fact C gives . Abs times gives . Call this type . Also , by Abs, Gen, Abs and Gen. Inst with gives the type . Then , so App gives . Inst with and App give .
If , the derivation contains a subderivation for the subterm , in some context. So is typable.
The map is computable. So TC is undecidable (Dudenhefner, Corollary 43).
Equivalence and the original route
Step 5 reduces TYP to TC. Wells' Theorem 6.15 reduces TC to TYP: given , and , it adds an assumption for a fresh , and it closes the context with Theorem 6.14. The resulting term contains , which can be typed only if has type , up to a renaming of type variables. So the two problems are many-one equivalent (Wells, Theorem 6.16). Wells' own proof of undecidability goes from semi-unification to TC directly (Theorem 4.1). It uses the term , with a context that depends on the instance and a free result type . That is the same pattern as Step 3, with in the role of and in the role of .
Recursive enumerability
A derivation is a finite tree, and each rule can be checked by a program. Listing all derivations by size and checking each one gives a procedure that halts exactly on the yes instances of TC. For TYP, the procedure also lists all candidate contexts and types. So both sets are recursively enumerable. Since they are not decidable, their complements are not recursively enumerable.
History
Girard (1972) and Reynolds (1974) defined System F with explicit types. Leivant presented the Curry-style system in 1983 and made the first attempt at typability. Henglein showed that typability is DEXPTIME-hard. Boehm and Pfenning showed that partial type reconstruction for Church-style terms is undecidable. Urzyczyn showed that typability is undecidable for the stronger system . For System F itself, Wells' paper calls the two questions "embarrassing open problems" up to 1994 (Wells 1999, Section 1.2). Kfoury, Tiuryn and Urzyczyn proved semi-unification undecidable in 1990, and the journal paper appeared in 1993. Wells used that result to settle the question in 1994. His journal paper of 1999 adds the full theory of invariant type assumptions. Dudenhefner (2021) gave a short direct reduction to typability, and he checked it in the Coq Library of Undecidability Proofs. That library also has a reduction from Turing machine halting to semi-unification. Together they give a fully checked reduction from halting to System F typability and type checking.
References
- 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, Lemma 3.2 (the INST-before-GEN property), Theorem 4.1 (semi-unification reduces to type checking, also for the lambda-I calculus), Theorem 6.14 (invariant type assumptions), Theorem 6.15 (type checking reduces to typability), Theorem 6.16 (main result)
- Joe B. Wells, Typability and type checking in the second-order lambda-calculus are equivalent and undecidable, Ninth Annual IEEE Symposium on Logic in Computer Science (LICS 1994), pages 176 to 185, the conference version of the main result
- Andrej Dudenhefner, The undecidability of System F typability and type checking for reductionists, 36th Annual ACM/IEEE Symposium on Logic in Computer Science (LICS 2021), IEEE, 2021, Problems 6 and 7 (the two decision problems), Example 33, Lemma 35 (left-uniform form), Definition 37 and Lemma 38 (pruning), Definition 19 and Lemma 29 (restricted typability and prenex simulation), Lemma 40 and Corollary 41 (typability), Lemma 42 and Corollary 43 (type checking), Section VII (the Coq mechanization)
- 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, Theorem 12 and the remark after it (semi-unification is undecidable already for two inequalities)
- Paola Giannini and Simona Ronchi Della Rocca, Characterization of typings in polymorphic type discipline, Third Annual Symposium on Logic in Computer Science (LICS 1988), pages 61 to 70, the normal form for derivations in which instantiation comes before generalization
- A. J. Kfoury and J. B. Wells, A direct algorithm for type inference in the rank-2 fragment of the second-order lambda-calculus, ACM Conference on LISP and Functional Programming (LFP 1994), pages 196 to 207, the introduction (typability is decidable at rank 2 and undecidable at every rank of 3 or more) and the rank-2 inference algorithm
- Frank Pfenning, Partial polymorphic type inference and higher-order unification, ACM Conference on LISP and Functional Programming (LFP 1988), the undecidability of partial type reconstruction, where the places of type abstractions and type applications are given, by reduction from second-order unification
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 DBPMSD Edit this page History Report an error Markdown