Fossacs 05
Fossacs 05
Fossacs 05
Healfdene Goguen
AT&T Labs, 180 Park Ave., Florham Park NJ 07932 USA. hhg@att.com.
1 Introduction
In this article we study the decidability of algorithms for βη-conversion for type
theories. We consider two algorithms for the Logical Framework not immediately
modeled by reduction to a common βη-normal form: Coquand’s untyped algo-
rithm relating syntactically distinct β-weak-head normal forms [3], and Harper
and Pfenning’s type-based algorithm [8]. We demonstrate that these algorithms
can be shown correct, complete and decidable based on standard metatheoretic
properties of type theory, such as strong normalization of βη-reduction, subject
reduction, injectivity of the type constructor Π, and so on. We then apply the
same technique to the polymorphic λ-calculus System F.
The focus of many existing developments of the metatheory of type theories
with βη-equality has been on decidability of typechecking, without concern for
algorithms for conversion. The fact that the standard metatheory is sufficient
to justify the decidability of algorithms has never been demonstrated as far
as we are aware, even for Coquand’s simple syntactic algorithm. With Harper
and Pfenning’s definition of a more complex algorithm for equality based on type
information, it has become more important to show that the traditional approach
to the metatheory of type theories, justifying termination and Church–Rosser
for the reduction relation, can be used to show decidability for algorithms that
are more complex than the simple comparison of normal forms.
We believe that it may be more efficient and uniform to justify algorithms for
βη-conversion through the traditional approach to metatheory than by study-
ing the algorithm directly. As an example, our proof of the termination of the
algorithm for the Logical Framework only requires a single logical relation, as
opposed to the two logical relations used in [11]. Similarly, Harper and Pfen-
ning’s approach has not been extended to systems with polymorphism, whereas
we are able to adapt our proof straightforwardly to System F. Any approach to
the metatheory of type theory with βη-equality is sufficient, and several meth-
ods already exist. Geuvers [4], Goguen [6, 7] and Salvesen [9] all have different
approaches to the difficulties presented by η.
The key to all three proofs of decidability is a simple length measure |−|
on normal forms of terms, where the value of the measure for an abstraction
in normal form, |λx:A.M |, is greater than the value of the measure for an ap-
plication to a variable in normal form, |M (x)|. In Coquand’s algorithm, an ab-
straction λx:A.M and a weak-head normal term y(N1 . . . Nn ) are related if M
and y(N1 . . . Nn , x) are related; by our measure, the combined length of the con-
clusion, |λx:A.M | + |y(N1 . . . Nn )|, is greater than the combined length of the
premisses, |M | + |y(N1 . . . Nn , x)|. This same idea can be translated to Harper
and Pfenning’s type-directed algorithm for conversion.
The remainder of this paper is structured as follows. Section 2 introduces the
syntax and standard metatheory for the Logical Framework. Section 3 justifies
Coquand’s algorithm using the standard metatheory. Section 4 justifies Harper
and Pfenning’s algorithm using a similar approach. Section 5 presents a type-
directed algorithm for conversion for System F and justifies this algorithm. We
draw conclusions and discuss future work in Section 6.
2.1 Syntax
We assume an infinite collection of variables x, y, z ∈ V . The language of terms
and contexts is defined by the following grammar.
Γ ∈ C ::= () | Γ, x:A
s ∈ S ::= type | kind
M, N, P, A, B ∈ T ::= s | x | λx:A.M | M (N ) | Πx:A.B
We say a term is basic if it is a variable x or a sort s, and a term is canonical
if it is of the form λx:A.M or Πx:A1 .A2 . Substitution, [N/x]M , is defined as
usual for terms, with the obvious extension to contexts. We identify terms and
contexts up to α-equivalence, and write FV(M ) for the free variables in M . Let
Γ = x1 :A1 , . . . , xn :An ; then dom(Γ ) ≡ {x1 , . . . , xn }, and Γ (x) is the partial
function that returns Ai if x = xi for some 1 ≤ i ≤ n.
2.2 Judgements and Derivations
Γ ` A : type
(type) () ` type : kind (Weak) (x:A 6∈ Γ )
Γ, x:A ` type : kind
Γ ` type : kind
(Var) (x:A ∈ Γ )
Γ `x:A
Γ ` A1 : type Γ, x:A1 ` A2 : s
(Π) (s ∈ {type, kind})
Γ ` Πx:A1 .A2 : s
Γ, x:A1 ` M : A2 Γ ` M1 : Πx:A1 .A2 Γ ` M2 : A1
(λ) (App)
Γ ` λx:A1 .M : Πx:A1 .A2 Γ ` M1 (M2 ) : [M2 /x]A2
Γ `M :A Γ `A=B:s
(Eq)
Γ `M :B
Lemma 1.
– If M is normal then there is no N such that M →β N .
– If M is weak-head normal then there is no N such that M →w N .
– Any term M is either weak-head normal or there is an N such that M →w N .
Proposition 2.
1. Free Variables. If Γ ` M : A then FV(M ) ∪ FV(A) ⊆ dom(Γ ).
2. Context Validity. If Γ ` J then Γ ` type : kind.
3. Thinning. If Γ, Γ 0 ` J, x 6∈ dom(Γ, Γ 0 ) and Γ ` A : type then Γ, x:A, Γ 0 ` J.
4. Substitution. If Γ, x:A, Γ 0 ` J and Γ ` N : A then Γ, [N/x]Γ 0 ` [N/x]J.
5. Type Correctness. If Γ ` M : A then Γ ` A : s for some s.
6. Splitting. If Γ ` M = N : A then Γ ` M, N : A.
7. Uniqueness of Types. If Γ ` M : A and Γ ` M : B then Γ ` A = B : s or
A = s and B = s for some s.
8. Context Replacement. Γ, x:A, Γ 0 ` J and Γ ` A = B : s imply Γ, x:B, Γ 0 ` J.
9. Church–Rosser. If Γ ` M = N : A then M ↓βη N .
10. Injectivity of Π. If Γ ` Πx:A.B = Πx:C.D : s then Γ ` A = C : type and
Γ, x:A ` B = D : s.
11. Subject Reduction. If Γ ` M : A and M →βη N then Γ ` M = N : A.
12. Strong Normalization. Γ ` M : A implies M is strongly normalizing under
→βη .
13. Strengthening. If Γ, x:A, Γ 0 ` J and x 6∈ FV(Γ 0 ) ∪ FV(J) then Γ, Γ 0 ` J.
Lemma 2. If Γ ` M : A, Γ ` N : B and M ↓βη N then there is an s such that
Γ ` M = N : A and Γ ` A = B : s.
We also observe without proof that Ext is equivalent to the following rule:
Γ ` M : Πx:A1 .A2
(η)
Γ ` λx:A1 .M (x) = M : Πx:A1 .A2
3.1 Definition
Coquand’s algorithm is defined inductively by the inference rules in Figure 2.
The algorithm M ⇐⇒ N simply reduces its arguments M and N to weak-head
normal form. The algorithm M ←→ N compares terms in weak-head normal
form: the interesting cases are the non-structural rules λ-Left and λ-Right, where
the left- or right-hand side is an abstraction and two terms are equivalent after
an application of Ext.
P ←→ Q
(WHRed) (M pw P and N pw Q)
M ⇐⇒ N
(Var) x ←→ x (type) type ←→ type
A1 ⇐⇒ B1 A2 ⇐⇒ B2
(Π)
Πx:A1 .A2 ←→ Πx:B1 .B2
M1 ←→ N1 M2 ⇐⇒ N2
(App) (M1 and N1 weak-head normal and not canonical)
M1 (M2 ) ←→ N1 (N2 )
M ⇐⇒ N
(λ)
λx:A.M ←→ λx:B.N
M ⇐⇒ N (x)
(λ-Left) (N weak-head normal and not canonical)
λx:A.M ←→ N
M (x) ⇐⇒ N
(λ-Right) (M weak-head normal and not canonical)
M ←→ λx:B.N
Proof. By nested induction on the sum of |M nf | and |N nf | and the sum of the
lengths of the β-reduction sequences for M and N .
By Lemma 1 Case 1 M and N are weak-head normal or have weak-head
reducts. If M or N has a weak-head reduct, then by WHRed M ⇐⇒ N ter-
minates if M wnf ←→ N wnf terminates, where the latter follows by the induc-
tion hypothesis for reduction; M ⇐⇒ N terminates with the same result as
M wnf ←→ N wnf . Otherwise, M and N are in weak-head normal form. We
perform case analysis on M and N to show that M ←→ N terminates; then
M ⇐⇒ N terminates with the same result. We consider several cases:
– M is basic and N is an application. Then M ←→ N terminates in failure.
– M ≡ M1 (M2 ) and N ≡ N1 (N2 ). If M and N are weak-head normal then
M1 and N1 must be weak-head normal and not abstractions. If M1 or N1
is a product then it is canonical, so M1 (M2 ) ←→ N1 (N2 ) fails immedi-
ately. Otherwise, M nf ≡ M1nf (M2nf ) and N nf ≡ N1nf (N2nf ), so by induction
hypothesis M1 ←→ N1 and M2 ⇐⇒ N2 terminate. If both succeed then
M ←→ N succeeds, and otherwise it fails.
– M ≡ λx:A1 .M0 and N is not canonical. Only rule λ-Left applies, and so
λx:A1 .M0 ←→ N terminates if M0 ⇐⇒ N (x) terminates. Clearly M nf ≡
λx:Anf nf
1 .M0 and (N (x))
nf
≡ N nf (x), and |λx:Anf nf nf nf
1 .M0 | + |N | = |M0 | +
3+|N nf | > |M0nf |+|N nf |+2 = |M0nf |+|N nf (x)|, so M0 ⇐⇒ N (x) terminates
by induction hypothesis; if this succeeds then λx:A1 .M0 ←→ N succeeds,
and otherwise it fails.
– M ≡ λx:A1 .M0 and N ≡ Πy:B1 .B2 . Then M ←→ N fails immediately.
Lemma 4 (Completeness). If Γ ` M = N : A then M ⇐⇒ N .
Proof. By Church–Rosser and Splitting it suffices to show that if M ↓βη N and
Γ ` M : A and Γ ` N : B then M ⇐⇒ N , which we show using the same
induction principle as for Lemma 3. As in that lemma, we can distinguish two
cases, depending on whether M and N are both weak-head normal. If either is
not, then by Church–Rosser if M ↓βη N then M wnf ↓βη N wnf , since M and N
are well-typed, and by Subject Reduction Γ ` M wnf : A and Γ ` N wnf : B.
Therefore M wnf ⇐⇒ N wnf follows by the induction hypothesis on reduction
sequences, so M wnf ←→ N wnf by inversion, and so M ⇐⇒ N by WHRed. We
now consider the cases where M and N are in weak-head normal form: we show
that if M ↓βη N then M ←→ N , from which M ⇐⇒ N . We consider several
cases where M and N are in weak-head normal form.
– M and N are basic. If x ↓βη y then x = y, so x ←→ y. type ←→ type.
– M is basic and N is an application. x ↓βη N1 (N2 ) and type ↓βη N1 (N2 ) are
impossible.
– M ≡ λx:A1 .M0 and N not canonical. Let λx:A1 .M0 ↓βη N , Γ ` λx:A1 .M0 :
A and Γ ` N : B. Then M0 pM00 (x) with x 6∈ FV(M00 ), and N pM00 ;
hence M0 ↓βη N (x), since N is weak-head normal and not canonical. By
Generation Γ, x:A1 ` M0 : A2 and Γ ` Πx:A1 .A2 = A : s; by Splitting
Γ ` Πx:A1 .A2 : s, and by Generation Γ ` A1 : type. Then by Lemma 2
Γ ` A = B : s, so Γ ` N : Πx:A1 .A2 ; by Thinning Γ, x:A1 ` N : Πx :
A1 .A2 and by App Γ, x:A1 ` N (x) : A2 . Therefore, by induction hypothesis
M0 ⇐⇒ N (x) implies λx:A1 .M0 ←→ N .
– M ≡ λx:A1 .M0 and N ≡ Πy:B1 .B2 . Suppose λx:A1 .M0 ↓βη Πy:B1 .B2 ,
Γ ` λx:A1 .M0 : A and Γ ` Πy:B1 .B2 : B. Then Γ ` λx:A1 .M0 : Πx:A1 .A2
and Γ ` Πy:B1 .B2 : s by Generation. By Lemma 2 Γ ` Πx:A1 .A2 = s : s0 ,
and by Church–Rosser Πx:A1 .A2 ↓βη s, which is impossible.
4.1 Definition
We begin by defining a slight variant of Harper and Pfenning’s algorithm.
The algorithm relies on an erasure function from the dependent types and
kinds of the Logical Framework into simple types. We define our erasure into
simple types formed only with constructors o and τ1 → τ2 , where Harper and
Pfenning distinguish between sorts and constants in the Logical Framework; our
approach should allow different judgements to be handled uniformly as in PTS.
We use a single base type because we have used the same syntactic category for
types and kinds, and it is more uniform not to distinguish between the two in
the algorithm.
Formally, we define the simple types and contexts with the following BNF
grammar:
σ, τ ∈ S ::= o | σ → τ
∆ ∈ X ::= () | ∆, x:σ
The erasure is defined inductively on the structure of types and kinds in
weak-head normal form as follows:
type− ≡ o x− ≡ o (A(M ))− ≡ o
kind− ≡ o (λx:A.M )− ≡ o (Πx:A1 .A2 )− ≡ A− −
1 → A2
The definition extends in the obvious way to contexts. Erasure has the following
simple properties, shown by induction on derivations.
∆ ` P ←→ Q : o
(Base) (M pw P and N pw Q)
∆ ` M ⇐⇒ N : o
∆, x:τ1 ` M (x) ⇐⇒ N (x) : τ2
(→) (x 6∈ dom(∆))
∆ ` M ⇐⇒ N : τ1 → τ2
(Var) ∆ ` x ←→ x : ∆(x) (type) ∆ ` type ←→ type : o
∆ ` A1 ⇐⇒ B1 : o ∆, x:A−
1 ` A2 ⇐⇒ B2 : o
(Π)
∆ ` Πx:A1 .A2 ←→ Πx:B1 .B2 : o
∆ ` M1 ←→ N1 : τ1 → τ2 ∆ ` M2 ⇐⇒ N2 : τ1
(App)
∆ ` M1 (M2 ) ←→ N1 (N2 ) : τ2
Lemma 7.
– If ∆, ∆0 ; M ; N ; τ ⇒ b and x 6∈ dom(∆, ∆0 ) then ∆, x:σ, ∆0 ; M ; N ; τ ⇒ b.
– If ∆, ∆0 ; M ; N → v and x 6∈ dom(∆, ∆0 ) then ∆, x:σ, ∆0 ; M ; N → v.
Proof. We prove this by nested induction on the sum of |M nf | and |N nf | and the
sum of the lengths of the β-reduction sequences of M and N . As for Coquand’s
algorithm, we use Lemma 1 Case 1 to perform case analysis on whether M and
N are weak-head normal or not.
We consider several cases where M and N are weak-head normal.
– M and N basic. If M = N = x and x:τ ∈ ∆ then ∆ ` x ←→ x : τ
succeeds. If M = N = type then ∆ ` type ←→ type : o succeeds, and
∆ ` type ←→ type : τ1 → τ2 fails. Similarly, if M 6= N or M ≡ x 6∈ dom(∆)
then ∆ ` M ←→ N : τ fails. Each result lifts to ⇐⇒ by Lemma 8.
– M ≡ λx:A1 .M0 and N is not canonical. ∆ ` M ⇐⇒ N : o fails because no
rules match ∆ ` λx:A1 .M0 ←→ N : o. ∆ ` λx:A1 .M0 ⇐⇒ N : τ1 → τ2
terminates by definition iff ∆, x:τ1 ` (λx:A1 .M0 )(x) ⇐⇒ N (x) : τ2 , which
by Lemma 9 terminates iff ∆, x:τ1 ` M0 ⇐⇒ N (x) : τ2 terminates. But
∆, x:τ1 ` M0 ⇐⇒ N (x) : τ2 terminates by induction hypothesis, since
|λx:Anf nf nf nf nf nf nf
1 .M0 | + |N | = |M0 | + 3 + |N | > |M0 | + |N | + 2 = |M0 | +
nf
nf
|N (x)|.
The outline of our proof of the correctness of the algorithm follows Harper and
Pfenning’s proof. The primary difference is that because we rely on established
metatheoretic results, Subject Reduction also applies to β-reducts at the level
of types by assumption.
Lemma 12 (Correctness).
– If Γ − ` M ⇐⇒ N : A− and Γ ` M, N : A then Γ ` M = N : A.
– If Γ − ` M ←→ N : τ , Γ ` M : A, Γ ` N : B and M and N not canonical
then Γ ` M = N : A and either Γ ` A = B : s with A− = B − = τ
or A = B = kind. If Γ − ` M ←→ N : A− and Γ ` M, N : A then
Γ ` M = N : A.
5 System F
In this final technical section we show that our technique also works for a typed
conversion algorithm for System F, hence extending our results beyond that of
Harper and Pfenning.
5.1 Syntax
We begin by introducing the term syntax and inference rules for System F.
The following grammar presents the contexts, types, and terms of System F:
Γ ∈ C ::= () | Γ, x:A
A, B, C ∈ Y ::= X | A → B | ∀X.A
M, N, P, Q ∈ T ::= x | λx:A.M | M (N ) | ΛX.M | M (A)
Similar to the Logical Framework, we say that a term is canonical if it is of the
form λx:A.M or ΛX.M .
We use the same notations for reduction, substitution and so on as for the
Logical Framework. We say that a context Γ is valid if each x ∈ dom(Γ ) occurs
exactly once in Γ . We write FTV(A) for the free type variables occurring in A,
and similarly for contexts.
Reduction is extended with β and η reductions for the type-level constructors:
Lemma 13. Let M and N be weak-head normal and not canonical. Then if
Γ ; M ; N → A then Γ ; M ; N ; A ⇒ tt; if Γ ; M ; N → ⊥ then Γ ; M ; N ; A ⇒ ff for
any A, and if Γ ; M ; N → A then Γ ; M ; N ; B ⇒ b for any B.
Γ ` P ←→ Q : X
(TyVar) (M pw P and N pw Q)
Γ ` M ⇐⇒ N : X
Γ, x:A ` M (x) ⇐⇒ N (x) : B
(→) (x 6∈ dom(Γ ))
Γ ` M ⇐⇒ N : A → B
Γ ` M (X) ⇐⇒ N (X) : A
(∀) (X 6∈ FTV(Γ ) ∪ FTV(M ) ∪ FTV(N ))
Γ ` M ⇐⇒ N : ∀X.A
(Var) Γ ` x ←→ x : Γ (x)
Γ ` M1 ←→ N1 : A → B Γ ` M2 ⇐⇒ N2 : A
(App)
Γ ` M1 (M2 ) ←→ N1 (N2 ) : B
Γ ` M ←→ N : ∀X.A
(TyApp)
Γ ` M (B) ←→ N (B) : [B/X]A
Proof. By nested induction on the sum of |M nf | and |N nf | and the sum of the
lengths of β-reduction sequences for M and N . As in the previous sections, if M
and N are not weak-head normal then the result follows by the nested induction
hypothesis.
We consider several cases where M and N are weak-head normal.
– M ≡ ΛX.M0 and N not canonical. Γ ` ΛX.M0 ⇐⇒ N : X fails immedi-
ately, and Γ ` ΛX.M0 ⇐⇒ N : A → B fails since Γ, x:A ` (ΛX.M0 )(x) ⇐⇒
N (x) : B fails by Lemma 13, since Γ, x:A ` (ΛX.M0 )(x) ←→ N (x) : B
fails. Suppose A ≡ ∀X.B; then Γ ` M0 ⇐⇒ N (X) : B terminates by in-
duction hypothesis on the combined length of the normal forms of ΛX.M0
and N , Γ ` (ΛX.M0 )(X) ⇐⇒ N (X) : B terminates by Lemma 14, and so
Γ ` ΛX.M0 ⇐⇒ N : ∀X.B terminates.
– M ≡ M1 (C) with M1 weak-head normal and not canonical, and N ≡ N1 (D)
with N1 weak-head normal and not canonical. If C 6= D then the algo-
rithm fails. Otherwise, Γ ` M1 ←→ N1 : A terminates: if it fails or if
A 6≡ ∀X.B then Γ ` M1 (C) ←→ N1 (C) fails, and otherwise Γ ` M1 (C) ←→
N1 (C) : [C/X]B succeeds. The results lift to Γ ` M1 (C) ⇐⇒ N1 (C) : D by
Lemma 13.
Lemma 16 (Completeness). If Γ ` M = N : A then Γ ` M ⇐⇒ N : A.
Lemma 17 (Correctness).
– If Γ ` M ⇐⇒ N : A and Γ ` M, N : A then Γ ` M = N : A.
– If Γ ` M ←→ N : A, Γ ` M : B and Γ ` N : C then Γ ` M = N : A and
A = B = C.
We have demonstrated that the standard metatheory for the Logical Framework
and System F for βη-equality is sufficient to justify algorithms for conversion
not immediately modeled by reduction. We used a simple inductive measure to
show the completeness and decidability of the algorithms.
A natural extension of this work would be to study the algorithm for conver-
sion for the Calculus of Constructions with βη-equality. We have made substan-
tial progress towards this goal by showing how type dependency can be erased
and reconstructed for the Logical Framework, and how polymorphism can be jus-
tified. Existing developments using erasure to study metatheory of dependent
type theories [5] suggest that the type-directed algorithm for the non-dependent
version of a calculus could be used to typecheck the dependently typed version.
One of the primary motivations for Harper and Pfenning’s algorithm was
singleton types, where Coquand’s untyped algorithm may fail to identify equal
terms. It seems that it should be possible to extend our technique given the
metatheory for βη-equality, but one of the benefits of giving an algorithm di-
rectly is that it addresses problems with the reduction relation, such as failure
of confluence. This is an interesting area for further research.
Acknowledgments
I would like to thank Bob Harper for stimulating my renewed interest in this
topic, and the anonymous referees and Andreas Abel for helpful corrections and
comments. I would also like to thank my wife Adriana Compagnoni for her
encouragement and support as I was writing this article.
References
1. D. Aspinall. Subtyping with singleton types. In L. Pacholski and J. Tiuryn, editors,
Computer Science Logic, pages 1–15. Springer, Berlin, 1994.
2. H. Barendregt. Lambda calculi with types. In S. Abramsky, D. M. Gabbai, and
T. S. E. Maibaum, editors, Handbook of Logic in Computer Science, volume 2.
Oxford University Press, 1991.
3. T. Coquand. An algorithm for testing conversion in type theory. In G. Huet and
G. Plotkin, editors, Logical Frameworks. Cambridge University Press, 1991.
4. H. Geuvers. Logics and Type Systems. PhD thesis, Katholieke Universiteit Nij-
megen, Sept. 1993.
5. H. Geuvers and M.-J. Nederhof. A modular proof of strong normalization for the
calculus of constructions. Journal of Functional Programming, 1(2):155–189, Apr.
1991.
6. H. Goguen. A Typed Operational Semantics for Type Theory. PhD thesis, Univer-
sity of Edinburgh, Aug. 1994.
7. H. Goguen. A syntactic approach to eta equality in type theory. In Symposium on
Principles of Programming Languages, Jan. 2005.
8. R. Harper and F. Pfenning. On equivalence and canonical forms in the LF type
theory. ACM Trans. on Computational Logic, 2004. To appear.
9. A. Salvesen. The Church-Rosser property for pure type systems with βη-reduction,
Nov. 1991. Unpublished manuscript.
10. C. A. Stone and R. Harper. Equivalence and singletons. ACM Transactions on
Programming Languages and Systems, 2004. Submitted.
11. J. Vanderwaart and K. Crary. A simplified account of the metatheory of linear LF.
Electronic Notes in Theoretical Computer Science, 70(2), 2002. Extended version
available as Technical Report CMU-CS-01-154.