In "Types and Programming Languages" by Benjamin C. Pierce (WorldCat)
5.3.2 Definition: The set of free variables of a term t, written FV(t), is defined as follows:
FV(x) = {x}
FV(λx.t₁) = FV(t₁) \ {x}
FV(t₁ t₂) = FV(t₁) ∪ FV(t₂)
\ is relative complement
Based on above definition of free variables my understanding is
For
λx.x
x is not free
For
(x y)
x is free and y is free
For
(x λx.x)
the first x is free and the second x is not free.
So x is both free and not free in (x λx.x)
.
Since x is both free and not free it seems the definition of free variables is wrong.
How should the definition be interpreted for (x λx.x)
?
Side notes
These are here in case others are seeking specifics of why I ask this question but I think the question above is self contained.
FV(t) is used for substitution in 5.3.5 and so has a more specific context, I.e.
x ↦ s = λy.[x ↦ s]t₁ if y ≠ x and y ≠ FV(s)
My implementation is in Prolog and the terms are converted to an AST before being passed to fv/2.
AST
app(abs(var(x),var(x)),var(x))
Prolog code
fv(var(V),[V]) :- !.
fv(abs(var(Binder),T),FV) :-
fv(T,FV0),
ord_subtract(FV0,[Binder],FV), !.
fv(app(T1,T2),FV) :-
fv(T1,FV1),
fv(T2,FV2),
ord_union(FV1,FV2,FV).
Example run
?- fv(app(abs(var(x),var(x)),var(x)),Vs).
Vs = [x].
The result of the code is that x
is a free variable because app
is done last and the second x
is free.
In TAPL chapter 6 the book switches to De Bruijn index and so the question becomes pointless for the remainder of the book.
AFAIK there is no official implementation of FV(t) provided for the book. The code for Lambda Calculus from Pierce is here but uses De Bruijn index so no need for FV(t)
Nothing in the errata for the definition.
EDIT (01/30/2022)
"The Calculi of Lambda-Conversion" by Alonzo Church (pdf)
pp. 8-9
A formula is any finite sequence of primitive symbols. Certain formulas are distinguished as well-formed formulas, and each occurrence of a variable in a well-formed formula is distinguished as free or bound, in accordance with the following rules (1-4), which constitute a definition of these terms by recursion:
- A variable x is a well-formed formula, and the occurrence of the variable x in this formula is free.
- If F and A are well-formed, (FA) is well-formed, and an occurrence of a variable y in F is free or bound in (FA) according as it is free or bound in F, and an occurrence of a variable y in A is free or bound in (FA) according as it is free or bound in A.
- If M is well-formed and contains at least one free occurrence of x, then (λxM) is well-formed, and an occurrence of a variable y, other than x, in (λxM) is free or bound in (λxM) according as it is free or bound in M. All occurrences of x in (λxM) are bound.
- A formula is well-formed, and an occurrence of a variable in it is free, or is bound, only when this follows from 1-3.
The free variables of a formula are the variables which have at least one free occurrence in the formula. The bound variables of a formula are the variables which have at least one bound occurrence in the formula.
p. 14
A well-formed formula will be said to be in principal normal form if it is in normal form, and no variable is both a bound variable and free variable of it, and the first bound variable occurring in it (in the left-to-right order of the symbols which compose the formula) is the same as the first variable in alphabetical order which is not a free variable of it, and the variables which occur in it immediately following the symbol λ are, when taken in the order in which they occur in the formula, in alphabetical order, without repetitions, and without omissions except of variables which are free variables of the formula.
p. 15
7 III. Every well-formed formula has one of the three forms, x where x is a variable, or (FA), where F and A are well-formed, or (λxM), where M is well-formed and x is a free variable of M.
p. 58
- THE CALCULUS OF λ-K-CONVERSION. The calculus of λ-K-conversion is obtained if a single change is made in the construction of the calculus of λ-conversion which appears in §§ 5,6: namely, in the definition of well-formed formula (§5) deleting the words "and contains at least one free occurrence of x" from the rule 3.