Algorithm Search⌘K

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 ee, decide whether ⊢e:τ\vdash e : \tau for some τ\tau, with T-FixPoly in place of T-Fix.

Theorem (Henglein 1993; Kfoury, Tiuryn and Urzyczyn 1993).

  1. Typability with polymorphic recursion and semi-unification reduce to each other in logarithmic space.
  2. 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 uu of a let-bound or fix-bound name, bound at bb, must have a type that is an instance of the type δb\delta_b of the definition, where only the variables that are not free in the context may change. This is the inequality

⟨δb,γb⟩≤⟨αu,γb⟩,\langle \delta_b, \gamma_b \rangle \le \langle \alpha_u, \gamma_b \rangle,

where γb\gamma_b is the tuple of the types of the fun-bound variables in scope at bb. A semi-unifier σ\sigma with residual ρ\rho must satisfy σγb ρ=σγb\sigma\gamma_b\,\rho = \sigma\gamma_b, so ρ\rho 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 si≤tis_i \le t_i between types over the variables v1,…,vkv_1, \ldots, v_k. 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 xjx_j have exactly the types sis_i and tit_i with vjv_j replaced by the type of xjx_j. The body forces the result type of f to be s1→⋯→sn→ints_1 \to \cdots \to s_n \to \mathsf{int} under this replacement σ\sigma. The ii-th recursive call passes the term of type tiσt_i\sigma at place ii and bot = (fix g y -> g y) 0, which fits any type, everywhere else. T-FixPoly lets each call take its own instance ρi\rho_i of the type of f, so the call is typable exactly when siσρi=tiσs_i\sigma\rho_i = t_i\sigma. So the term is typable if and only if σ\sigma 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. □\square

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, f has 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 use f at bool→int\mathsf{bool} \to \mathsf{int} and int→int\mathsf{int} \to \mathsf{int}. T-Fix fails, since it needs one argument type. T-FixPoly gives ∀α. α→int\forall \alpha.\, \alpha \to \mathsf{int}. Functions on nested data types, such as data 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 α\alpha, [α][\alpha], [[α]][[\alpha]] 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 Γ,f:σ⊢e:σ\Gamma, f : \sigma \vdash e : \sigma for any scheme σ\sigma. The two forms derive the same judgments: from σ=∀α⃗. τ\sigma = \forall \vec\alpha.\, \tau with α⃗\vec\alpha not free in Γ\Gamma, the scheme Gen(Γ,τ)\mathrm{Gen}(\Gamma, \tau) is at least as general, and a more general scheme in the context keeps each judgment true.

A semi-unifier of a system {s1≤t1,…,sn≤tn}\{s_1 \le t_1, \ldots, s_n \le t_n\} is a substitution σ\sigma such that for each ii some ρi\rho_i has siσρi=tiσs_i\sigma\rho_i = t_i\sigma. Equations s≐ts \doteq t may also appear in a system. They add nothing, since s≐ts \doteq t holds under σ\sigma exactly when g(z,z)≤g(s,t)g(z, z) \le g(s, t) does, for a fresh zz (see the semi-unification page). We write ⟨a1,…,ak⟩\langle a_1, \ldots, a_k \rangle for a tuple, coded with a fresh function symbol.

Reduction 1: from typability to semi-unification

Let ee be a closed term. Rename the bound variables so that they are all different. Give each node uu of ee a type variable αu\alpha_u, and each fun-bound variable and each fix argument yy a type variable αy\alpha_y. Call let-bound and fix-bound names polymorphic. For each polymorphic name, let bb be its binding node and γb\gamma_b the tuple of αy\alpha_y for the fun-bound variables and fix arguments in scope at bb, outside the definition. Let δb\delta_b be the type of the definition: αe1\alpha_{e_1} for let x = e1 in e2, and αb\alpha_b for fix f x -> e. The system Σ(e)\Sigma(e) has:

node uu constraint
a fun-bound variable or fix argument yy αu≐αy\alpha_u \doteq \alpha_y
a constant, if, an operator the equation of its rule, such as αu≐int\alpha_u \doteq \mathsf{int} for 1
fun y -> e1 αu≐αy→αe1\alpha_u \doteq \alpha_y \to \alpha_{e_1}
e1 e2 αe1≐αe2→αu\alpha_{e_1} \doteq \alpha_{e_2} \to \alpha_u
let x = e1 in e2 αu≐αe2\alpha_u \doteq \alpha_{e_2}
fix f y -> e1 αu≐αy→αe1\alpha_u \doteq \alpha_y \to \alpha_{e_1}
a use of a polymorphic name bound at bb ⟨δb,γb⟩≤⟨αu,γb⟩\langle \delta_b, \gamma_b \rangle \le \langle \alpha_u, \gamma_b \rangle

The system has size linear in ∣e∣|e|.

Lemma 1. ee is typable if and only if Σ(e)\Sigma(e) has a semi-unifier. If σ\sigma is one, then σαu\sigma\alpha_u is a type of the node uu in one derivation.

Proof. The equations are those of the simply typed calculus, so they hold exactly when the types σαu\sigma\alpha_u fit the rules other than T-Var' for polymorphic names. Consider such a use uu of a name bound at bb. In a derivation, the context at bb maps each fun-bound variable and fix argument yy in scope to σαy\sigma\alpha_y, 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 σαy\sigma\alpha_y of the variables in scope at the outer binder, so ftv(σΓb)=ftv(σγb)\mathrm{ftv}(\sigma\Gamma_b) = \mathrm{ftv}(\sigma\gamma_b). The T-Var' step at uu asks that σαu\sigma\alpha_u is an instance of Gen(σΓb,σδb)\mathrm{Gen}(\sigma\Gamma_b, \sigma\delta_b), that is, σαu=σδb ρ\sigma\alpha_u = \sigma\delta_b\,\rho for a ρ\rho that does not move the variables of σΓb\sigma\Gamma_b. The inequality says that σδb ρ=σαu\sigma\delta_b\,\rho = \sigma\alpha_u and σγb ρ=σγb\sigma\gamma_b\,\rho = \sigma\gamma_b. The second equation holds exactly when ρ\rho fixes each variable of σγb\sigma\gamma_b. On the variables of σδb\sigma\delta_b, the two conditions on ρ\rho 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 σαu\sigma\alpha_u. □\square

The tuple γb\gamma_b 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 {s1≤t1,…,sn≤tn}\{s_1 \le t_1, \ldots, s_n \le t_n\} be a system whose terms are types, built from type variables v1,…,vkv_1, \ldots, v_k, the arrow, int\mathsf{int} and bool\mathsf{bool}, with k≥1k \ge 1. 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 α→α→α\alpha \to \alpha \to \alpha, so eq a b forces aa and bb 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 tt over v1,…,vkv_1, \ldots, v_k, define a term B(t)\mathrm{B}(t) with free variables x1,…,xkx_1, \ldots, x_k:

B(vj)=xjB(int)=0B(bool)=true\mathrm{B}(v_j) = x_j \qquad \mathrm{B}(\mathsf{int}) = \texttt{0} \qquad \mathrm{B}(\mathsf{bool}) = \texttt{true}
B(t→t′)=fun y -> (fun d -> B(t′)) (eq y B(t))\mathrm{B}(t \to t') = \texttt{fun y -> (fun d -> } \mathrm{B}(t') \texttt{) (eq y } \mathrm{B}(t)\texttt{)}

If each xjx_j has the type τj\tau_j, then B(t)\mathrm{B}(t) has exactly the type [vj:=τj] t[v_j := \tau_j]\,t: the argument y is forced to the type of B(t)\mathrm{B}(t). 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 ii-th argument line, f gets kk copies of bot, then nn arguments: B(ti)\mathrm{B}(t_i) at place ii 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 τ1→⋯→τk→β\tau_1 \to \cdots \to \tau_k \to \beta, with xj:τjx_j : \tau_j, and put σ=[vj:=τj]\sigma = [v_j := \tau_j]. The last line of the body has type s1σ→⋯→snσ→ints_1\sigma \to \cdots \to s_n\sigma \to \mathsf{int}, because each wiw_i is forced to the type of B(si)\mathrm{B}(s_i). So β=s1σ→⋯→snσ→int\beta = s_1\sigma \to \cdots \to s_n\sigma \to \mathsf{int}. The context outside the fix is empty, so T-FixPoly gives f the scheme that quantifies all variables of its type. The ii-th use of f takes an instance under some ρi\rho_i. The bot arguments fit any types. The argument at place ii is typable exactly when siσρi=tiσs_i\sigma\rho_i = t_i\sigma, since B(ti)\mathrm{B}(t_i) has the type tiσt_i\sigma. The other parts of the body always have types. So the term is typable exactly when some τ1,…,τk\tau_1, \ldots, \tau_k and ρ1,…,ρn\rho_1, \ldots, \rho_n satisfy all nn equations, that is, when σ\sigma is a semi-unifier. Conversely, a semi-unifier σ\sigma gives the types τj=σvj\tau_j = \sigma v_j. □\square

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. □\square

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.
  1. 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
  2. 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
  3. 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
  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, 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.

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/90HNZ3/text.yaml