Algorithm Search⌘K

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 σ::=a∣σ→σ∣∀a. σ\sigma ::= a \mid \sigma \to \sigma \mid \forall a.\,\sigma. Terms are pure: M::=x∣λx. M∣M MM ::= x \mid \lambda x.\,M \mid M\,M. A context Γ\Gamma gives types to term variables. The rules are:

  • Var: Γ⊢x:Γ(x)\Gamma \vdash x : \Gamma(x).
  • Abs: from Γ,x:σ⊢M:τ\Gamma, x{:}\sigma \vdash M : \tau, derive Γ⊢λx.M:σ→τ\Gamma \vdash \lambda x.M : \sigma \to \tau.
  • App: from Γ⊢M:σ→τ\Gamma \vdash M : \sigma \to \tau and Γ⊢N:σ\Gamma \vdash N : \sigma, derive Γ⊢M N:τ\Gamma \vdash M\,N : \tau.
  • Gen: from Γ⊢M:σ\Gamma \vdash M : \sigma with aa not free in Γ\Gamma, derive Γ⊢M:∀a.σ\Gamma \vdash M : \forall a.\sigma.
  • Inst: from Γ⊢M:∀a.σ\Gamma \vdash M : \forall a.\sigma, derive Γ⊢M:[a:=τ]σ\Gamma \vdash M : [a := \tau]\sigma.

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.

  1. Typability (TYP). Given a term MM, are there a context Γ\Gamma and a type τ\tau with Γ⊢M:τ\Gamma \vdash M : \tau?
  2. Type checking (TC). Given Γ\Gamma, MM and τ\tau, is Γ⊢M:τ\Gamma \vdash M : \tau derivable?

Both problems stay undecidable for terms of the λI\lambda I-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.

  1. Two-inequality semi-unification is undecidable. An instance is two pairs (σ1,τ1),(σ2,τ2)(\sigma_1, \tau_1), (\sigma_2, \tau_2) of simple types, which have no quantifiers. A solution is three substitutions φ,ψ1,ψ2\varphi, \psi_1, \psi_2 with ψi(φ(σi))=φ(τi)\psi_i(\varphi(\sigma_i)) = \varphi(\tau_i) for i=1,2i = 1, 2. Kfoury, Tiuryn and Urzyczyn show that this restricted problem is already undecidable (cited).
  2. Left-uniform form. Put σ′=σ1→σ2\sigma' = \sigma_1 \to \sigma_2, τ1′=τ1→a2\tau_1' = \tau_1 \to a_2 and τ2′=a1→τ2\tau_2' = a_1 \to \tau_2, with fresh a1,a2a_1, a_2. The instance (σ′,τ1′),(σ′,τ2′)(\sigma', \tau_1'), (\sigma', \tau_2') is solvable if and only if the original one is. Both pairs now share the same left side.
  3. The core term. For a left-uniform instance (σ,τ1),(σ,τ2)(\sigma, \tau_1), (\sigma, \tau_2), let S=σ→σS = \sigma \to \sigma and Ui=τi→τiU_i = \tau_i \to \tau_i. Let ∀.ρ\forall.\rho be the closure of ρ\rho over all its free variables, and let
    ΓI={ x:∀c. (c→c)→c,  y:∀. (U1→U2→S) }.\Gamma_I = \{\, x : \forall c.\,(c \to c) \to c,\ \ y : \forall.\,(U_1 \to U_2 \to S) \,\}.
    Then x (λz. y z z)x\,(\lambda z.\,y\,z\,z) has a type in ΓI\Gamma_I if and only if the instance is solvable. The idea fits in one line. The variable zz gets one polymorphic type ∀a⃗. φ(S)\forall \vec a.\,\varphi(S), and its two uses instantiate that type in two ways, to φ(U1)\varphi(U_1) and to φ(U2)\varphi(U_2). The bound variables a⃗\vec a play the part of the residual substitutions ψ1,ψ2\psi_1, \psi_2. The variable xx forces the type of y z zy\,z\,z to be the type of zz 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.
  4. Closing the context. A typability question cannot fix the types of free variables. Dudenhefner's prenex simulation (Lemma 29) builds a closed term MIM_I that is typable if and only if x (λz. y z z)x\,(\lambda z.\,y\,z\,z) is typable in ΓI\Gamma_I. Wells' Theorems 6.14 and 6.15 do the same job with invariant type assumptions, a more general tool. This step is cited.
  5. From typability to type checking. Send MM, with free variables x1,…,xnx_1, \ldots, x_n, to the question of whether ⊢K I (λx1…xn. M):∀a. a→a\vdash K\,I\,(\lambda x_1 \ldots x_n.\,M) : \forall a.\,a \to a, where K=λuv. uK = \lambda u v.\,u and I=λu. uI = \lambda u.\,u. The answer is yes exactly when MM 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 (a, b→b),(a→a, b)(a,\ b \to b), (a \to a,\ b). It has no unifier: we would need a=b→ba = b \to b and b=a→ab = a \to a, which fails the occurs check. But it is semi-unifiable with

φ(a)=a,φ(b)=a→a,ψ1(a)=(a→a)→a→a,ψ2(a)=a.\varphi(a) = a,\quad \varphi(b) = a \to a,\quad \psi_1(a) = (a \to a) \to a \to a,\quad \psi_2(a) = a.

Then ψ1(φ(a))=(a→a)→a→a=φ(b→b)\psi_1(\varphi(a)) = (a \to a) \to a \to a = \varphi(b \to b) and ψ2(φ(a→a))=a→a=φ(b)\psi_2(\varphi(a \to a)) = a \to a = \varphi(b).

In left-uniform form, σ′=a→a→a\sigma' = a \to a \to a, τ1′=(b→b)→a2\tau_1' = (b \to b) \to a_2 and τ2′=a1→b\tau_2' = a_1 \to b. The proof extends φ\varphi by φ(a1)=ψ2(φ(σ1))=a\varphi(a_1) = \psi_2(\varphi(\sigma_1)) = a and φ(a2)=ψ1(φ(σ2))=P→P\varphi(a_2) = \psi_1(\varphi(\sigma_2)) = P \to P, where P=(a→a)→a→aP = (a \to a) \to a \to a. The type of zz is then

ρ=∀a. (a→a→a)→a→a→a,\rho = \forall a.\,(a \to a \to a) \to a \to a \to a,

and the Church-style term

x [ρ] (λz:ρ. Λa. y [φ(a)] [φ(b)] [φ(a1)] [φ(a2)] (z [ψ1(a)]) (z [ψ2(a)]))x\,[\rho]\,\big(\lambda z{:}\rho.\ \Lambda a.\ y\,[\varphi(a)]\,[\varphi(b)]\,[\varphi(a_1)]\,[\varphi(a_2)]\ (z\,[\psi_1(a)])\ (z\,[\psi_2(a)])\big)

has type ρ\rho in ΓI\Gamma_I. Its erasure is x (λz. y z z)x\,(\lambda z.\,y\,z\,z). A script with a Church-style checker for System F confirms the two equations, the left-uniform equations, the type ρ\rho and the erasure.

A one-line contrast shows that semi-unification is a strict weakening of unification. The single pair (a, a→b)(a,\ a \to b) has no unifier, but it is semi-unifiable: take φ=id\varphi = \mathrm{id} and ψ(a)=a→b\psi(a) = a \to b. The pair (a→b, a)(a \to b,\ a) has no solution at all, because ψ(φ(a→b))\psi(\varphi(a \to b)) is always larger than φ(a)\varphi(a). 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 ∀\forall anywhere in a type cannot infer all types. In Haskell with GHC's RankNTypes extension, 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 ∀\forall 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 kk if no ∀\forall lies to the left of kk 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 λI\lambda I-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 ∀.ρ\forall.\rho for the closure of ρ\rho over all its free variables, in any fixed order. Γ⊩M\Gamma \Vdash M means that Γ⊢M:τ\Gamma \vdash M : \tau for some τ\tau.

Two-inequality semi-unification (SU2). Given simple types σ1,τ1,σ2,τ2\sigma_1, \tau_1, \sigma_2, \tau_2, are there substitutions φ,ψ1,ψ2\varphi, \psi_1, \psi_2 into simple types with ψ1(φ(σ1))=φ(τ1)\psi_1(\varphi(\sigma_1)) = \varphi(\tau_1) and ψ2(φ(σ2))=φ(τ2)\psi_2(\varphi(\sigma_2)) = \varphi(\tau_2)?

Left-uniform SU2 (LU). The same question for instances with σ1=σ2\sigma_1 = \sigma_2.

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 Γ⊢M:τ\Gamma \vdash M : \tau, 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 Γ⊢M:τ\Gamma \vdash M : \tau, then Γ′⊢M:τ\Gamma' \vdash M : \tau, where Γ′\Gamma' is Γ\Gamma restricted to the free variables of MM. And if Γ⊢M:τ\Gamma \vdash M : \tau and xx is not in Γ\Gamma, then Γ,x:σ⊢M:τ\Gamma, x{:}\sigma \vdash M : \tau 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 ∀a.ρ\forall a.\rho, and Inst does not touch quantifiers below an arrow. So if a type is ∀a1…ak. ρ\forall a_1 \ldots a_k.\,\rho with ρ\rho an arrow, then a chain of Inst steps from it yields ∀aj+1…ak. ψ(ρ)\forall a_{j+1} \ldots a_k.\,\psi(\rho), for some jj and some ψ\psi that moves only a1,…,aja_1, \ldots, a_j. Second, App needs its function premise to have an arrow type, and Gen produces a ∀\forall-type. So a chain of steps whose result must be an arrow ends with zero Gen steps.

Step 1: SU2 reduces to LU

Given (σ1,τ1),(σ2,τ2)(\sigma_1, \tau_1), (\sigma_2, \tau_2), pick fresh a1,a2a_1, a_2 and put

σ′=σ1→σ2,τ1′=τ1→a2,τ2′=a1→τ2.\sigma' = \sigma_1 \to \sigma_2,\qquad \tau_1' = \tau_1 \to a_2,\qquad \tau_2' = a_1 \to \tau_2.

If the original instance has a solution φ,ψ1,ψ2\varphi, \psi_1, \psi_2, let φ′\varphi' agree with φ\varphi except that φ′(a1)=ψ2(φ(σ1))\varphi'(a_1) = \psi_2(\varphi(\sigma_1)) and φ′(a2)=ψ1(φ(σ2))\varphi'(a_2) = \psi_1(\varphi(\sigma_2)). Since a1,a2a_1, a_2 do not occur in σ1,σ2,τ1,τ2\sigma_1, \sigma_2, \tau_1, \tau_2,

ψ1(φ′(σ′))=ψ1(φ(σ1))→ψ1(φ(σ2))=φ(τ1)→φ′(a2)=φ′(τ1′),\psi_1(\varphi'(\sigma')) = \psi_1(\varphi(\sigma_1)) \to \psi_1(\varphi(\sigma_2)) = \varphi(\tau_1) \to \varphi'(a_2) = \varphi'(\tau_1'),
ψ2(φ′(σ′))=ψ2(φ(σ1))→ψ2(φ(σ2))=φ′(a1)→φ(τ2)=φ′(τ2′).\psi_2(\varphi'(\sigma')) = \psi_2(\varphi(\sigma_1)) \to \psi_2(\varphi(\sigma_2)) = \varphi'(a_1) \to \varphi(\tau_2) = \varphi'(\tau_2').

If φ,ψ1,ψ2\varphi, \psi_1, \psi_2 solve the new instance, compare the two sides of each arrow. The left sides of the first equation give ψ1(φ(σ1))=φ(τ1)\psi_1(\varphi(\sigma_1)) = \varphi(\tau_1), and the right sides of the second give ψ2(φ(σ2))=φ(τ2)\psi_2(\varphi(\sigma_2)) = \varphi(\tau_2). □\square

Remark: in Dudenhefner's printed proof of Lemma 35, the definition of φ′(a2)\varphi'(a_2) reads ψ1(φ(σ1))\psi_1(\varphi(\sigma_1)). The computation after it needs ψ1(φ(σ2))\psi_1(\varphi(\sigma_2)), as above. On the worked instance of the page, the printed form gives ψ1(φ′(σ′))=P→P→P\psi_1(\varphi'(\sigma')) = P \to P \to P but φ′(τ1′)=P→P\varphi'(\tau_1') = P \to P, with P=(a→a)→a→aP = (a \to a) \to a \to a. 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 pp. Define prunep\mathrm{prune}_p from all types to simple types by prunep(b)=b\mathrm{prune}_p(b) = b, prunep(ρ→ρ′)=prunep(ρ)→prunep(ρ′)\mathrm{prune}_p(\rho \to \rho') = \mathrm{prune}_p(\rho) \to \mathrm{prune}_p(\rho') and prunep(∀b.ρ)=p\mathrm{prune}_p(\forall b.\rho) = p (Dudenhefner, Definition 37).

Lemma. Let ψ\psi be any substitution, and let ρ\rho be a type in which pp does not occur. Define ψ′(p)=p\psi'(p) = p and ψ′(b)=prunep(ψ(b))\psi'(b) = \mathrm{prune}_p(\psi(b)) for b≠pb \neq p. Then ψ′(prunep(ρ))=prunep(ψ(ρ))\psi'(\mathrm{prune}_p(\rho)) = \mathrm{prune}_p(\psi(\rho)).

Proof. Induction on ρ\rho. For a variable bb, which differs from pp, both sides are prunep(ψ(b))\mathrm{prune}_p(\psi(b)). For an arrow, use the induction hypothesis on both sides. For ∀b.ρ0\forall b.\rho_0, the left side is ψ′(p)=p\psi'(p) = p, and ψ(∀b.ρ0)\psi(\forall b.\rho_0) is again a ∀\forall-type, so the right side is pp as well. □\square

Corollary. Let σ,τ\sigma, \tau be simple, and let φ,ψ\varphi, \psi be substitutions into arbitrary types with ψ(φ(σ))=φ(τ)\psi(\varphi(\sigma)) = \varphi(\tau). Pick pp not in φ(σ)\varphi(\sigma). Then φ′=prunep∘φ\varphi' = \mathrm{prune}_p \circ \varphi maps into simple types, and with ψ′\psi' from the lemma, ψ′(φ′(σ))=φ′(τ)\psi'(\varphi'(\sigma)) = \varphi'(\tau).

Proof. For simple σ\sigma, prunep(φ(σ))=φ′(σ)\mathrm{prune}_p(\varphi(\sigma)) = \varphi'(\sigma) by induction on σ\sigma, since σ\sigma has no quantifier. So ψ′(φ′(σ))=ψ′(prunep(φ(σ)))=prunep(ψ(φ(σ)))=prunep(φ(τ))=φ′(τ)\psi'(\varphi'(\sigma)) = \psi'(\mathrm{prune}_p(\varphi(\sigma))) = \mathrm{prune}_p(\psi(\varphi(\sigma))) = \mathrm{prune}_p(\varphi(\tau)) = \varphi'(\tau). □\square

For an LU instance, one pp that is fresh for φ(σ)\varphi(\sigma) serves both inequalities, and φ′\varphi' 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 (σ,τ1),(σ,τ2)(\sigma, \tau_1), (\sigma, \tau_2) be an LU instance. Put S=σ→σS = \sigma \to \sigma and Ui=τi→τiU_i = \tau_i \to \tau_i, and

ΓI={ x:∀c. (c→c)→c,  y:∀. (U1→U2→S) }.\Gamma_I = \{\, x : \forall c.\,(c \to c) \to c,\ \ y : \forall.\,(U_1 \to U_2 \to S) \,\}.

Claim. ΓI⊩x (λz. y z z)\Gamma_I \Vdash x\,(\lambda z.\,y\,z\,z) if and only if the instance is solvable.

From a solution to a typing. Let φ,ψ1,ψ2\varphi, \psi_1, \psi_2 solve the instance in simple types. Let a⃗\vec a list the free variables of φ(S)\varphi(S), and let ρ=∀a⃗. φ(S)\rho = \forall \vec a.\,\varphi(S). It is closed. In the context ΓI,z:ρ\Gamma_I, z{:}\rho:

  • y:φ(U1)→φ(U2)→φ(S)y : \varphi(U_1) \to \varphi(U_2) \to \varphi(S) by Var and Inst, instantiating the closure with φ\varphi;
  • z:ψ1(φ(S))z : \psi_1(\varphi(S)) by Var and Inst, instantiating a⃗\vec a with ψ1\psi_1. This type equals ψ1(φ(σ))→ψ1(φ(σ))=φ(τ1)→φ(τ1)=φ(U1)\psi_1(\varphi(\sigma)) \to \psi_1(\varphi(\sigma)) = \varphi(\tau_1) \to \varphi(\tau_1) = \varphi(U_1). Only the variables a⃗\vec a matter here, since they are all the variables of φ(S)\varphi(S);
  • in the same way, z:φ(U2)z : \varphi(U_2);
  • so y z z:φ(S)y\,z\,z : \varphi(S) by App twice, and y z z:ρy\,z\,z : \rho by Gen on each variable of a⃗\vec a. This is allowed because ΓI\Gamma_I and ρ\rho are closed.

Abs gives λz. y z z:ρ→ρ\lambda z.\,y\,z\,z : \rho \to \rho. Inst gives x:(ρ→ρ)→ρx : (\rho \to \rho) \to \rho, and App gives ΓI⊢x (λz. y z z):ρ\Gamma_I \vdash x\,(\lambda z.\,y\,z\,z) : \rho.

From a typing to a solution. Take a derivation of ΓI⊢x (λz. y z z):θ0\Gamma_I \vdash x\,(\lambda z.\,y\,z\,z) : \theta_0 with the property of Fact B. Read it from the leaves.

  1. The head xx. It must get an arrow type for the outer App. Var gives ∀c.(c→c)→c\forall c.(c \to c) \to c. The body is an arrow, so by the first remark, a chain of Inst steps ends in an arrow only after it instantiates cc, and then it has (θ→θ)→θ(\theta \to \theta) \to \theta for some type θ\theta. By the second remark, no Gen step follows. So the argument must get the type θ→θ\theta \to \theta.
  2. The argument. Abs gives λz. y z z\lambda z.\,y\,z\,z a type ρ→ρ′\rho \to \rho', where ΓI,z:ρ⊢y z z:ρ′\Gamma_I, z{:}\rho \vdash y\,z\,z : \rho'. No Inst step applies to an arrow. A Gen step would give a ∀\forall-type, not θ→θ\theta \to \theta. So ρ=θ=ρ′\rho = \theta = \rho', and ΓI,z:θ⊢y z z:θ\Gamma_I, z{:}\theta \vdash y\,z\,z : \theta.
  3. The variable yy. It is the function of an App, so it gets an arrow type. Var gives the closed type ∀b⃗. (U1→U2→S)\forall \vec b.\,(U_1 \to U_2 \to S), and its body is an arrow. By the two remarks, all of b⃗\vec b are instantiated and no Gen follows. So y:φ(U1)→φ(U2)→φ(S)y : \varphi(U_1) \to \varphi(U_2) \to \varphi(S) for a substitution φ\varphi into arbitrary types.
  4. The term y zy\,z. App gives it the type φ(U2)→φ(S)\varphi(U_2) \to \varphi(S), and the first zz gets the type φ(U1)\varphi(U_1). This type is an arrow, so no Inst follows. It is the function of the outer App, so no Gen follows. So the second zz gets the type φ(U2)\varphi(U_2), and App gives y z zy\,z\,z the type φ(S)\varphi(S).
  5. The result of y z zy\,z\,z. φ(S)\varphi(S) is an arrow, so no Inst step follows, and only Gen steps can follow. So θ=∀a⃗. φ(S)\theta = \forall \vec a.\,\varphi(S) for some list a⃗\vec a.
  6. The two uses of zz. Var gives θ=∀a⃗. φ(S)\theta = \forall \vec a.\,\varphi(S). The result must be the arrow φ(Ui)\varphi(U_i). By the two remarks, all of a⃗\vec a are instantiated, by some ψi\psi_i that moves only a⃗\vec a, and no Gen follows. So ψi(φ(S))=φ(Ui)\psi_i(\varphi(S)) = \varphi(U_i) for i=1,2i = 1, 2.

Compare the left sides of the arrows in step 6: ψi(φ(σ))=φ(τi)\psi_i(\varphi(\sigma)) = \varphi(\tau_i) for i=1,2i = 1, 2. This is a solution of the LU instance in arbitrary types. Step 2 turns it into a solution in simple types. □\square

The doubled types S=σ→σS = \sigma \to \sigma and Ui=τi→τiU_i = \tau_i \to \tau_i are arrows, whatever σ\sigma and τi\tau_i are. The argument above uses this in steps 4, 5 and 6. If σ\sigma were a lone variable bb and φ(b)\varphi(b) were a ∀\forall-type, Inst steps could reach inside φ(σ)\varphi(\sigma), 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 xx and yy to have exactly their types in ΓI\Gamma_I. For a term NN, a term variable vv and a simple type σ\sigma, Dudenhefner's Lemma 29 (prenex simulation) builds a term VV in which vv is not free, such that for every context Γ\Gamma without vv:

Γ⊩V  ⟺  Γ,v:∀.σ⊩N.\Gamma \Vdash V \iff \Gamma, v : \forall.\sigma \Vdash N.

The construction applies λv.N\lambda v.N to a term V1 V2V_1\,V_2. That term is built so that each of its types gives vv no more freedom in NN than ∀.σ\forall.\sigma does. It uses a few gadgets. One gadget forces a variable to have a type c→cc \to c. Another assumption w:∀. a→b→b→aw : \forall.\,a \to b \to b \to a 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 v=xv = x and the type of xx in ΓI\Gamma_I, then with v=yv = y. The result is a closed term MIM_I, computable from the instance. By the lemma and Fact C, MIM_I is typable if and only if ΓI⊩x (λz. y z z)\Gamma_I \Vdash x\,(\lambda z.\,y\,z\,z) (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 MM have free variables x1,…,xnx_1, \ldots, x_n, and put M′=K I (λx1…xn. M)M' = K\,I\,(\lambda x_1 \ldots x_n.\,M) with K=λuv. uK = \lambda u v.\,u and I=λu. uI = \lambda u.\,u. We show that MM is typable if and only if ⊢M′:∀a. a→a\vdash M' : \forall a.\,a \to a (Dudenhefner, Lemma 42).

If MM is typable, Fact C gives x1:σ1,…,xn:σn⊢M:τx_1{:}\sigma_1, \ldots, x_n{:}\sigma_n \vdash M : \tau. Abs nn times gives ⊢λx1…xn. M:σ1→⋯→σn→τ\vdash \lambda x_1 \ldots x_n.\,M : \sigma_1 \to \cdots \to \sigma_n \to \tau. Call this type π\pi. Also ⊢K:∀a. a→∀b. b→a\vdash K : \forall a.\,a \to \forall b.\,b \to a, by Abs, Gen, Abs and Gen. Inst with a:=∀a. a→aa := \forall a.\,a \to a gives KK the type (∀a.a→a)→∀b. b→∀a. a→a(\forall a.a \to a) \to \forall b.\,b \to \forall a.\,a \to a. Then ⊢I:∀a. a→a\vdash I : \forall a.\,a \to a, so App gives ⊢K I:∀b. b→∀a. a→a\vdash K\,I : \forall b.\,b \to \forall a.\,a \to a. Inst with b:=πb := \pi and App give ⊢M′:∀a. a→a\vdash M' : \forall a.\,a \to a.

If ⊢M′:∀a. a→a\vdash M' : \forall a.\,a \to a, the derivation contains a subderivation for the subterm MM, in some context. So MM is typable.

The map M↦(∅,M′,∀a. a→a)M \mapsto (\varnothing, M', \forall a.\,a \to a) is computable. So TC is undecidable (Dudenhefner, Corollary 43). □\square

Equivalence and the original route

Step 5 reduces TYP to TC. Wells' Theorem 6.15 reduces TC to TYP: given Γ\Gamma, MM and τ\tau, it adds an assumption z:τ→⊥z : \tau \to \bot for a fresh zz, and it closes the context with Theorem 6.14. The resulting term contains z Mz\,M, which can be typed only if MM has type τ\tau, 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 b (λx. c x x)b\,(\lambda x.\,c\,x\,x), with a context that depends on the instance and a free result type β\beta. That is the same pattern as Step 3, with bb in the role of xx and cc in the role of yy.

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 FωF_\omega. 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

  1. 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)
  2. 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
  3. 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)
  4. 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)
  5. 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
  6. 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
  7. 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of correctness
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/DBPMSD/text.yaml