<figure><img src="image_1.png" /></figure>

HAL
open science

# The Undecidability of Pattern Matching in Calculi where Primitive Recursive Functions are Representable

Gilles Dowek

<figure><img src="image_2.png" /></figure>

## To cite this version:

Gilles Dowek. The Undecidability of Pattern Matching in Calculi where Primitive Recursive Functions are Representable. 1993. hal-04123502

HAL Id: hal-04123502

https://inria.hal.science/hal-04123502

Preprint submitted on 9 Jun 2023

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_3.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
# The Undecidability of Pattern Matching in Calculi where Primitive Recursive Functions are Representable

Gilles Dowek

## INRIA*†

We prove that the pattern matching problem is undecidable in polymorphic λ-calculi (as Girard's system F [8] [9]) and calculi supporting inductive types (as Gödel's system T [10] [9]) by reducing Hilbert's tenth problem to it. More generally pattern matching is undecidable in all the calculi in which primitive recursive functions can be fairly represented in a precised sense.

## Introduction

The higher order matching problem in a typed λ-calculus is the problem of determining whether a term is an instance of another i.e. to solve the equation $a = b$ where a and b are terms and b is ground. The decidability of pattern matching in simply typed λ-calculus is still an open problem.

Extensions of simply λ-calculus are obtained by adding dependent types, polymorphism, type constructors and inductive types. In [4] we have proved that pattern matching is undecidable in λ-calculi with dependent types or type constructors. We prove in this note that pattern matching is also undecidable in polymorphic λ-calculi (as Girard's System F [8] [9]) and in λ-calculi supporting inductive types (as Gödel's System T [10] [9]). More generally a λ-calculus cannot at the same time be sufficiently expressive to represent primitive recursive functions and let pattern matching be decidable.

## 1 Girard's System F

We use the definition of system F and the notations of [1] except that we write *Prop* instead of $*$ and $t[x \leftarrow t']$ for the term obtained by substituting the term $t'$ for the variable $x$ in the term $t$.

**Definition: Syntax**

$$T ::= \mathit{Prop} \mid x \mid (T T) \mid \lambda x : T.T \mid \Pi x : T.T$$

The notation $T \to T'$ is an abbreviation for $\Pi x : T.T'$ when $x$ has no occurrence in $T'$.

**Definition: Context**

<footnote>*B.P. 105, 78153 Le Chesnay CEDEX, France. dowek@margaux.inria.fr</footnote>

<footnote>†This research was partly supported by ESPRIT Basic Research Action “Logical Frameworks”.</footnote>
A context is a list of pairs <x, T> (written x : T) where x is a variable and T a term.

**Definition: Typing Rules**

We define inductively two judgements: $\Gamma$ is well-formed and $t$ has type $T$ in $\Gamma$ ($\Gamma \vdash t : T$) where $\Gamma$ is a context and $t$ and $T$ are terms.

[ ] well-formed

$\Gamma \vdash T : Prop$
$\Gamma[x : T]$ well-formed

$\Gamma$ well-formed
$\Gamma[x : Prop]$ well-formed

$\Gamma$ well-formed $x : T \in \Gamma$
$\Gamma \vdash x : T$

$\Gamma \vdash T : Prop \quad \Gamma[x : T] \vdash T' : Prop$
$\Gamma \vdash \Pi x : T.T' : Prop$

$\Gamma[x : Prop] \vdash T : Prop$
$\Gamma \vdash \Pi x : Prop.T : Prop$

$\Gamma \vdash T : Prop \quad \Gamma[x : T] \vdash T' : Prop \quad \Gamma[x : T] \vdash t : T'$
$\Gamma \vdash \lambda x : T.t : \Pi x : T.T'$

$\Gamma[x : Prop] \vdash T : Prop \quad \Gamma[x : Prop] \vdash t : T$
$\Gamma \vdash \lambda x : Prop.t : \Pi x : Prop.T$

$\Gamma \vdash t : (x : T)T' \quad \Gamma \vdash t' : T$
$\Gamma \vdash (t t') : T'[x \leftarrow t']$

**Definition: $\beta$-reduction and $\beta$-equivalence**

The $\beta$-reduction (in one step) ($\Delta$) is the smallest relation compatible with term structure
that verifies:

$$ (\lambda x : T.t \ u) \Delta t[x \leftarrow u] $$

The $\beta$-reduction relation ($\Delta^*$) is the reflexive-transitive closure of the relation $\Delta$ and the
$\beta$-equivalence ($\equiv$) is the reflexive-symmetric-transitive closure of the relation $\Delta$.

**Definition: Normal Term**

A term $t$ is said to be normal if there exists no term $u$ such that $t \Delta u$.
**Remark:** The proof given in this note also works if we consider $\eta$-reduction too.

**Theorem:** The reduction on well-typed terms is strongly normalizable and confluent, i.e. all the reduction sequences issued from a well-typed term $t$ are finite and if $u$ and $u'$ are normal terms such that $t \triangleright^* u$ and $t \triangleright^* u'$ then $u = u'$.

**Proof:** See [8] [9] for the $\beta$-reduction and [6] [7] [13] for the generalization to $\beta\eta$-reduction.

**Proposition:** Let $t$ be a normal well-typed term, $t$ is either an abstraction, a product or an atomic term i.e. a term of the form $(w c_1 ... c_p)$ where $w$ is a variable or a sort.

**Proof:** If the term $t$ is neither an abstraction nor a product then it can be written in a unique way $t = (w c_1 ... c_p)$ where $w$ is not an application. The term $w$ is not a product (if $p \neq 0$ because a product is of type $s$ for some sort $s$ and therefore cannot be applied and if $p = 0$ because $t$ is not a product). It is not an abstraction (if $p \neq 0$ because $t$ is in normal form and if $p = 0$ because $u$ is not an abstraction). It is therefore a variable or a sort.

**Definition:** We let $Nat = \Pi P : Prop.P \to (P \to P) \to P$ and for every natural number $n$, $\bar{n}$ be the Church natural representing $n$:

$$ \bar{n} = \lambda P : Prop.\lambda x : P.\lambda f : P \to P.(f ... (f x) ... ) (n \text{ times}) $$

**Proposition:** For every primitive recursive function $f$ of arity $n$, there exists in system $F$ a term $t$ of type $Nat \to ... \to Nat \to Nat$ such that if $a_1, ..., a_n$ are natural numbers, then:

$$ (t \bar{a}_1 ... \bar{a}_n) = \overline{(f a_1 ... a_n)} $$

Moreover the term $t$ can be effectively constructed from the definition of $f$. The term $t$ is said to *represents* the function $f$.

**Proof:** See [8] [9].

# 2 The Undecidability of Primitive Recursive Equations

Let us recall some well-known facts about primitive recursive functions.

**Proposition:** The following functions are primitive recursive:

* addition and multiplication,

* the function Equal such that $(Equal x y) = 0$ if $x = y$ and $(Equal x y) = 1$ otherwise,

* the function $\alpha$ such that $(\alpha x n)$ is the exponent of the $n^{th}$ prime number in the prime decomposition of $x$.

**Proposition:** For every finite sequence of natural numbers $a_1, ..., a_n$, there exists an natural number $x$ such that for every $i$, $1 \le i \le n$, $a_i = (\alpha x i)$.

**Proof:** We take $x = \Pi_{i=1}^n p_n^{a_i}$ where $p_n$ is the $n^{th}$ prime number.

**Proposition:** There is no effective method that decides if, given the definition of a primitive recursive function $f$, the equation $(f x_1 ... x_n) = 0$ has a solution.
**Proof:** We reduce Hilbert's tenth problem [3] to this one. Let $(P x_1 ... x_n)$ and $(Q x_1 ... x_n)$ two polynomials. Let us define the function $f$ as:

$$ (f x_1 ... x_n) = (\text{Equal} (P x_1 ... x_n) (Q x_1 ... x_n)) $$

The equation $(f x_1 ... x_n) = 0$ has a solution if and only if $(P x_1 ... x_n) = (Q x_1 ... x_n)$ also has one.

**Remark:** In the previous proposition we can restrict ourselves to equations with only one variable by taking:

$$ (f x) = (\text{Equal} (P (\alpha x 1) ... (\alpha x n)) (Q (\alpha x 1) ... (\alpha x n))) $$

# 3 The Undecidability of Pattern Matching in Girard's system F

**Definition:** A matching problem on one natural variable is a pair of terms $<a, b>$ such that $a$ is well-typed in the context $[x : Nat]$ and $b$ is well-typed in the empty context. A solution of such a problem is a pair $<\gamma, u>$ such that $\gamma$ is a well-formed context and $u$ a term well-typed of type $Nat$ in the context $\gamma$ such that $a[x \leftarrow u]$ and $b$ have the same normal form (these two terms are well-typed in the context $\gamma$).

**Remark:** Although $a$ may have only $x$ as free variable and $b$ does not have any, there is no restriction on the free variables of the term $u$ since $\gamma$ is an arbitrary well-formed context.

**Proposition:** Let $\Gamma$ be a context and $t$ a normal term well-typed in $\Gamma$ of type $Nat$ such that the normal form of $(t Nat \bar{0} \lambda y : Nat.y)$ is $\bar{0}$ then the term $t$ is a Church natural.

**Proof:** Let us consider the context $\Gamma' = [P : Prop; x : P; f : P \to P]$. Let the term $u$ be the normal form of $(t P x f)$. The term $u$ has type $P$ in $\Gamma'$. We have:

$$ (t Nat \bar{0} \lambda y : Nat.y) \equiv \bar{0} $$

so:

$$ u[P \leftarrow Nat, x \leftarrow \bar{0}, f \leftarrow \lambda y : Nat.y] \equiv \bar{0} $$

We prove by induction over the structure of $u$ that every normal term $u$ of type $P$ in the context $\Gamma'$ such that the normal form of $u[P \leftarrow Nat, x \leftarrow \bar{0}, f \leftarrow \lambda y : Nat.y]$ is $\bar{0}$ has the form $u = (f ... (f x) ...)$.

The term $u$ has type $P$ so it is neither an abstraction nor a product. It is thus an atomic term $(w c_1 ... c_p)$. If $w$ is different from $P$, $f$ and $x$ then the normal form of the term $u[P \leftarrow Nat, x \leftarrow \bar{0}, f \leftarrow \lambda y : Nat.y]$ is also atomic with head $w$ and thus is different from $\bar{0}$. So the variable $w$ is among $P$, $f$ and $x$. It is not the variable $P$ because we would have $p = 0$ and the normal form of $u[P \leftarrow Nat, x \leftarrow \bar{0}, f \leftarrow \lambda y : Nat.y]$ would be the term $Nat$ which is not $\bar{0}$, so it is either $x$ or $f$.
If $w = x$ then $p = 0$ so $u = x$ has the required form. If $w = f$ then $p = 1$, $u = (fu')$. The term $u[P \leftarrow Nat, x \leftarrow \bar{0}, f \leftarrow \lambda y : Nat.y]$ reduces to $u'[P \leftarrow Nat, x \leftarrow \bar{0}, f \leftarrow \lambda y : Nat.y]$, so the normal form of this term is $\bar{0}$. Thus, by induction hypothesis, we have $u' = (f \dots (f x) \dots)$ and $u = (f (f \dots (f x) \dots))$ has the required form.

At last since the normal form of the term $(t P x f)$ is $(f \dots (f x) \dots)$ and this term has not the form $(v f)$ with $v$ normal we have $t = \lambda P : Prop.\lambda x : P.\lambda f : P \to P.(f \dots (f x) \dots)$.

**Theorem:** There is no effective method that decides if a matching problem on one natural variable in system $F$ has a solution.

**Proof:** Let $f$ be an unary primitive recursive $f$, we build a matching problem on one natural variable $<a, b>$ that has a solution if and only if $f$ takes the value 0. Let $t$ be a term representing the function $f$ and Pair be the term:

$$Pair = \lambda x : Nat.\lambda y : Nat.\lambda g : Nat \to Nat \to Nat.(g x y)$$

Let:

$$a = (Pair (x Nat \bar{0} \lambda y : Nat.y) (t x)) \\ b = (Pair \bar{0} \bar{0})$$

Let $n$ be a natural number such that $(f n) = 0$, the pair $< [ ], \bar{n} >$ is a solution of the matching problem $< a, b >$. Conversely, let $< \gamma, u >$ be a solution of the matching problem $< a, b >$, the normal form of the term $(u Nat \bar{0} \lambda y : Nat.y)$ is $\bar{0}$ and the normal form of $(t u)$ is $\bar{0}$. Thus the normal form of $u$ is a Church natural $\bar{n}$ and $(f n) = 0$.

**Remark:** In [5] we have developed a more general notion of matching problem ans made a distinction between *universal variables* that cannot be instanciated by a substitution and *existential variables* that can be instanciated by a substitution. We have also defined a notion of *order* of a type $T$ in a context $\Gamma$:

* if $T$ is atomic, $T = (w c_1 \dots c_n)$ then if $w$ is an universal variable then $o(T) = 1$, if $w$ is an existential variable then $o(T) = \infty$ and if $w$ is a sort then $o(T) = 2$,

* if $T = \Pi y : U.V$ then $o(T) = \max\{1+u, v\}$ where $u$ is the order of $U$ in $\Gamma$ and $v$ is the order of $V$ in $\Gamma[y : U]$ letting $y$ be an existential variable (with the usual conventions $n+\infty = \infty$ and $\max\{n, \infty\} = \infty$).

We have proved in [5] that second order matching was decidable in all the systems of the cube of type systems [1] including system $F$. Since the order of $Nat$ is infinite, the problems considered in this note are of infinite order. So the problem of decidability of pattern matching in system $F$ with only finite order variables is left open. Since restricting the order of variables to finite order prohibits the use of polymorphism, this problem seems related to the problem of pattern matching in simply typed $\lambda$-calculus.
# 4 The Undecidability of Pattern Matching in Gödel's System T

**Definition:** Gödel's System T [10] [9] is an extension of simply typed λ-calculus in which:

* there are a primitive type Nat and primitive symbols $O: Nat$ and $S: Nat \to Nat$,

* for each type $T$, there is a primitive symbol $R_T$ (called the recursor of type $T$) of type $T \to (Nat \to T \to T) \to Nat \to T$,

* reduction is extended by the rules:

$$ (R_T a b O) \triangleright a $$

$$ (R_T a b (S x)) \triangleright (b x (R_T a b x)) $$

**Theorem:** The reduction on well-typed terms is strongly normalizable and confluent.

**Proof:** See [10] [9].

**Remark:** Usually $\eta$-reduction is not considered in system $T$. The proof given here also works if we consider $\eta$-reduction too, provided that the reduction relation is strongly normalizable and confluent.

**Proposition:** Let $\bar{n}$ be the term $(S \dots (S O) \dots)$ ($n$ times). For every primitive recursive function $f$ of arity $n$, there exists in system $T$ a term $t$ of type $Nat \to \dots \to Nat \to Nat$ representing the function $f$, moreover the term $t$ can be effectively constructed from the definition of $f$.

**Proof:** See [10] [9].

**Proposition:** In Gödel's system $T$, let $t$ be a normal term of type $Nat$ such that the normal form of $(R_{Nat} O \lambda y : Nat.\lambda z : Nat.z t)$ is $O$ then $t$ has the form $(S \dots (S O) \dots)$.

**Proof:** By induction over the structure of $t$. The term $t$ has type $Nat$ so it not an abstraction, since it is normal it is an atomic term $(w c_1 \dots c_p)$. If $w$ is different from $O$ and $S$ then the term $(R_{Nat} O \lambda y : Nat.\lambda z : Nat.z t)$ is normal and is different from $O$. So the variable $w$ is either $O$ or $S$.

If $w = O$ then $p = 0$ so $t = O$ has the required form. If $w = S$ then $p = 1$, $t = (S t')$. The term $(R_{Nat} O \lambda y : Nat.\lambda z : Nat.z t)$ reduces to $(R_{Nat} O \lambda y : Nat.\lambda z : Nat.z t')$, so the normal form of this term is $O$. Thus, by induction hypothesis, $t' = (S \dots (S O) \dots)$ and $t = (S (S \dots (S O) \dots))$ has the required form.

**Theorem:** There is no effective method that decides if a matching problem on one natural variable in system $T$ has a solution.

**Proof:** The proof is the same as the one for system $F$, except that we replace the term $(x Nat \bar{0} \lambda y : Nat.y)$ by the term $(R_{Nat} O \lambda y : Nat.\lambda z : Nat.z x)$.
**Remark:** In system *T* the type *Nat* is primitive, so even first-order pattern matching is undecidable.

Conclusion

In this note we have proved the undecidability of pattern matching in system *F* and system *T*. The proofs given here generalize to all the polymorphic systems of the cube of typed λ-calculi [1], to all the systems of this cube extended by inductive types [2] and to Martin-Löf's Type Theory [12]. More generally if we say that primitive recursive functions can be fairly represented in a typed λ-calculus when these functions can be represented and there exists a term *t* of type *Nat* → *Nat* such that if *u* is a term of type *Nat* then the term (*t* *u*) reduces to *∅* if and only if *u* represents an integer, then pattern matching is undecidable in all the systems in which primitive recursive functions can be fairly represented.

In [4] we have proved the undecidability of pattern matching in calculi with dependent types and type constructors. Pattern matching is therefore undecidable in seven calculi of the cube of typed λ-calculi [1]. The problem of the decidability of pattern matching in simply typed λ-calculus is left open. This problem is conjectured decidable in [11].

Acknowledgments

The author would like to thank Amy Felty, Serge Grigorieff, Gérard Huet and Christine Paulin for their help in the preparation of this note.

References

[1] H. Barendregt, Introduction to Generalized Type Systems, To appear in *Journal of Functional Programming*.

[2] Th. Coquand, Ch. Paulin, Inductively Defined Types, *Proceedings of Programming Logic*, P. Dybjer, L.Hallnås, B. Nordström, K. Peterson, J.M. Smith (Eds.), University of Göteborg and Chalmers University of Technology, 1989, pp. 191-207.

[3] M. Davis, Hilbert's Tenth Problem is Unsolvable, *The American Mathematician Monthly*, 80, 3, 1973, pp. 233-269.

[4] G. Dowek, L'Indécidabilité du Filtrage du Troisième Ordre dans les Calculs avec Types Dépendants ou Constructeurs de Types (The Undecidability of Third Order Pattern Matching in Calculi with Dependent Types or Type Constructors), *Compte Rendu à l'Académie des Sciences*, I, 312, 12, 1991, pp. 951-956.
[5] G. Dowek, A Second Order Pattern Matching Algorithm in the Cube of Typed λ-Calculi, *Proceedings of Mathematical Foundation of Computer Science*, 1991, Lecture Notes in Computer Science 520, pp. 151-160.

[6] J. Gallier, On Girard's Candidats de Réductibilité, *Logic and Computer Science*, P. Odifreddi (Ed.), Academic Press, London, 1990, pp. 123-203.

[7] H. Geuvers, The Church-Rosser Property for βη-reduction in Typed Lambda Calculi, *Proceedings of Logic in Computer Science*, 1992.

[8] J.Y. Girard, Interprétation fonctionnelle et élimination des coupures dans l'arithmétique d'ordre supérieur, *Thèse de Doctorat d'État*, Université de Paris VII, 1972.

[9] J.Y. Girard, Proofs and Types, translated and with appendices by P. Taylor and Y. Lafont, *Cambridge University Press*, 1989.

[10] K. Gödel. Über eine bisher noch nicht benützte Erweiterung des finiten Standpunktes, *Dialectica*, 12, 1958.

[11] G. Huet, Résolution d'Équations dans les Langages d'Ordre 1,2, ..., ω, *Thèse de Doctorat d'État*, Université de Paris VII, 1976.

[12] P. Martin-Löf, Intuitionistic Type Theory, *Bibliopolis*, Napoli, 1984.

[13] A. Salvesen, The Church-Rosser Property for Pure Type Systems with βη-reduction, *Manuscript*, University of Edinburgh, 1991.

[14] A. Yasuhara, Recursive Function Theory and Logic, *Academic Press*, New York, 1971.