Home Denotational Semantics of Typed Lambda Calculus IV — CPO Model for PCF
Post
Cancel

Denotational Semantics of Typed Lambda Calculus IV — CPO Model for PCF

In this article, we consider the domain-theoretic semantics of PCF. This provides some insight into the properties of CPOs, and also provides the basis for semantic reasoning about PCF.

We show soundness of the PCF equational axioms system, and therefore the reduction rules, for our CPO model, called APCF. It is impossible for the PCF equational proof system to be complete for proving all equations between PCF terms that hold in PCF. The reason is that, as shown in Easy Foundations for Programming Languages V — PCF Iteration and Recursion, every partial recursive function is definable in PCF.

By soundness, and the fact that all the numerals denote distinct elements of the model, two expressions of type natnat will be equal in PCF iff they operationally define the same partial recursive function. Since equality between partial recursive functions is not recursively enumerable, no recursive axiom system could be complete for APCF.

CPO Model for PCF

The model APCF is the full continuous hierarchy over APCFnat=N and APCFbool=B, with constants of PCF interpreted as described below.

Since the type constants of PCF are interpreted as pointed CPOs, and cartesian product and continuous function constractors preserve pointedness, all of the type of PCF are interpreted as pointed CPOs. This gives us least-fixed-point operators at all types, allowing us to interpret the fixed-point constants of PCF according to Lemma 1 in the last article.

We interpret constants 0,1,2, and true,false as the standard natural numbers and boolean elements of the lifted set N and B. If we choose continuous functions for basic PCF operations +, Eq? and conditional, then every term of PCF will have a meaning in APCF. It follows that every term in PCF has a fixed point.

We interpret + and Eq? in APCF as the lifted versions, + and Eq?, of the standard functions. In other words, we interpret + as the extension of addition that is strict in both arguments, so that for every xAPCFnat,

nat+x=x+nat=nat

We treat the PCF equality test similarly, so that the PCF expression Eq?MN has value true if M and N denote the same element of N different from , value false if M and N denote different non- elements of N, and value if either M and N denotes .

The interpretation of conditional in APCF is a little subtle. You may guess that we could also interpret conditional as a strict function, with value of the appropriate type whenever any of its arguments is of some type. However, this does not work, since in the case that M denotes but N does not, we would have

if false then M else N=,

which contradicts an equational axiom of PCF.

Recall that we have the following equational axioms for conditional discussed in Easy Foundations for Programming Languages III — The Language PCF & Its Syntax.
(if true then M else N)=M(if false then M else N)=N

An interpretation of conditional that satisfies the equational axioms of PCF is the following.

APCF[[if P then M else N]]η={APCF[[M]]ηif APCF[[P]]η=trueAPCF[[N]]ηif APCF[[P]]η=falseotherwise

Since the equational axioms of PCF only mention conditional expressions when the first argument is true or false, it is easy to see that the axioms are satisfied. This completes the definition of APCF.

Now we have the following immediate consequences of the general soundness theorem for Henkin models.

(Theorem 1.) Let M and N be expressions of PCF over typed variables from Γ. If ΓM=N:σ is provable from the axioms for PCF, then the CPO model APCF satisfies the equation ΓM=N:σ.

(Corollary 1.) If ΓM:σ is a well-typed term of PCF, and MN, then the CPO model APCF satisfies the equation ΓM=N:σ.

These soundness results show that the denotational semantics given by APCF has the minimal properties required of any denotational semantics. Specifically, if we can prove M=N, or reduce one term to the other, then these two terms will have the same meaning in APCF. It follows that if two terms have different meanings, then we cannot prove them equal or reduce one to the other. Therefore, we can use APCF to reason about unprovability or the non-existence of reductions.

Examples

In this section, we will use two examples to illustrate how to work out the meaning of some expressions in APCF and make some comments about the difference between the least fixed point of a function and other fixed points.

Ex.1 Factorial Function

Recall that the factorial function may be written fact=deffixnatnatF, where F is the expression

F=defλf:natnat.λy:nat.(if Eq?y0 then 1 else yf(y1)).

We will work out the meaning of this closed term assuming that and denote multiplication and subtraction functions which are strict extensions of the standard ones. In other words, we assume that multiplication and subtraction have their standard meaning on non-least elements of N, but have value if either of their arguments is .

Following the definition of fix in a CPO, we can see that the meaning of factorial is the least upper bound of a directed set. Specifically, APCF[[fact]] is the least upper bound {F¯n()n0}, where F¯=APCF[[F]]. We will work out the first few cases of F¯n() to understand what this means.

Since all of the lambda terms involved are closed, we will dispense with the formality of writing APCF[[]] and use lambda terms as names for elements of the appropriate domains.

F¯0()=natnatF¯1()=λy:nat. if Eq?y0 then 1 else y(F¯0())(y1)=λy:nat. if Eq?y0 then 1 else y(natnat)(y1)=λy:nat. if Eq?y0 then 1 else natF¯2()=λy:nat. if Eq?y0 then 1 else y(F¯1())(y1)=λy:nat. if Eq?y0 then 1 else y(λx:nat. if Eq?x0 then 1 else nat)(y1)=λy:nat. if Eq?y0 then 1 else y( if Eq?(y1)0 then 1 else nat)

Continuing in this way, we can see that F¯n() is the function which computes y! whenever 0y<n and has value nat otherwise. Since nat represents “undefined,” this means that F¯n() is defined for 0y<n and undefined otherwise.

A property of this collection of functions is that for a particular argument y≠⊥, there are two possible values for F¯n()y — if ny, then the value is , while if n>y, the value is y!.

As spelled out in the statement of Lemma 4 in the CPO article, the least upper bound of {F¯n()n0} is the function mapping any yN to the least upper bound of the set {F¯n()yn0}. The set contains only , if y=⊥, and has two elements and y! otherwise. Therefore APCF[[fact]] is the function from N to N which maps to , and any other yN to y!.

Such strictness of factorial agrees with our computational intuition and experience, since we do not expect any computation of fact M to terminate if the expression M cannot be simplified to a numeral. But in the case M reduces to a numeral for y, we can reduce fact M to the numeral for y!.

In general, the least fixed point of a function is the fixed point that is “computed in practice.” Later we will formally show that the PCF operational and denotational semantics coincide. Simply put, the least fixed point of a function f has all the properties of fix f that we can determine by applying fix-reduction some number of times.

Ex.2 Function with Many Fixed Points

This example illustrates the difference between the least fixed point of a function and other fixed points.

Consider the function F:(natnat)(natnat) defined by

F=defλf:natnat.λx:nat.(if Eq?x1 then 1 else f(x2))

where subtraction xy yields 0 if yx.

In contrast to the function used to define factorial, we will see that F has many fixed points. The reason is that F is a function mapping even arguments to . This allows us to find greater fixed points (in the point-wise order) which map even arguments to natural numbers other than .

Before discussing alternate fixed points further, we will work out the meaning of fix F.

As in factorial, we let F¯=APCF[[F]]. The least fixed point of F¯ is the least upper bound of the set {F¯n()n0}. The simplest way to understand this set is to begin by working out the first few functions.

F¯0()=natnatF¯1()=λx:nat.if Eq?x1 then 1 else (F¯0())(x2)=λx:nat.if Eq?x1 then 1 else natF¯2()=λx:nat.if Eq?x1 then 1 else (F¯1())(x2)=λx:nat.if Eq?x1 then 1 else (λy:nat.if Eq?y1 then 1 else nat)(x2)=λx:nat.if Eq?x1 then 1 else (if Eq?(x2)1 then 1 else nat)

Continuing in this way, we can see that F¯n() is the function which maps any odd x<2n to 1 and has value nat otherwise. The least upper bound f of all such functions is the function mapping all odd x to 1, and any other natural number to .

This corresponds to the fact that if we try to compute the value of (fixF)x by reduction, we will succeed after a finite number of reduction steps iff x is odd. If x is even, we can reduce the expression indefinitely, but will never produce a numeral.

It is not hard to see that the fixed points of F above are precisely the functions g:natnat satisfying the two conditions:

g(1)=1g(x+2)=g(x)

Since these two equations do not determine a value for even x, any function mapping all even x (and 0) to some number n will be a fixed point of F. All of these alternatives are greater than f since any natural number n is greater than in the ordering on the lifted CPO of natural numbers. However, none of these alternative fixed points is the function computed by reduction.

Fixed-Point Induction

The equational proof system for PCF is sound for the CPO model and other semantic models of PCF. It is adequate to specify the result of any program — if P:nat is closed, and P denotes a number n in the CPO model APCF, then we can prove P=n.

However, the equational proof system is not very powerful when it comes to proving equations between parts of programs, such as terms defining functions. This is illustrated in the example below.

In this section, we study an extension to the equational proof system that has been found, in practice, to be sufficient for proving many equations between PCF terms.

Ex. 3 Equation of Function Terms

If f:DD and g:DD are continuous functions on some CPO D, then it is not hard to see that fix(fg)=f(fix(fg)). The reason is that

fix(fg)={(fg)i()i0}={(fg)(),(fgfg)(),(fgfgfg)(),...}={(f(gf)i)()i0}=f(fix(fg))

by monotonicity, general facts about least upper bound, and the associativity of composition.

In the rest of this example, we show that there is no equational proof of fix(fg)=f(fix(fg)), treating f and g as variables of type σσ, for any type σ, using only the equational proof system of PCF.

There are two facts about PCF:

  1. The reduction rules of PCF are confluent;
  2. The reduction rules of PCF are exactly directed versions of the equational axioms, plus symmetry, transitivity and congruence rules.

Based on these, we know that the equation fix(fg)=f(fix(fg)) is provable iff both terms reduce to a common term. However, an easy induction shows that any term obtained by reducing fix(fg) will have an equal number of f’s and g’s, while terms obtained from f(fix(fg)) will have more f than g. Therefore, we cannot prove fix(fg)=f(fix(fg)) using the equational proof system.

Extended Proof System

The extended proof system is based on the CPO interpretation of terms. In addition to equations, we will use formulas which assert that the meaning of one term is an approximation of another. More specifically, we use assertions of the form MN or, written out with type assignments, ΓMN:σ. To be precise, an approximation ΓMN:σ is satisfied at environment ηΓ for CPO model A if A[[M]]ηA[[N]]η.

The proof system given in this section is sound for deriving assertions that hold in all CPO models. A common way of reading MN is, “M is an approximation of N.”

One obvious inference rule is

(eq)=defΓM=N:σqqΓMN:σ

which allows us to derive approximations from equations. This rule may be used to derive versions of the (β) and (η) axioms using approximation in place of equality.

A “converse” to this rule is

(asym)ΓMN:σ,=defΓNM:σΓM=N:σ

which is sound since a partial order is anti-symmetric. This rule lets us derive equations from approximations.

Since is transitive, we also have the rule

(trans)ΓMN:σ,=defΓNP:σΓMP:σg

If we add a constant σ to the language, for each type σ, then we write the axiom scheme

(bot)ΓσM:σ

since σ is the least element of type σ. The tag “bot” stands for the “bottom” element.

We also know that the least element of a function type is the function mapping each argument to . This gives us the equational axiom scheme

(botf)Γ⊥=λx:σ.⊥:στ

We also have a congruence rule for each syntactic form. For application, the rule

(acong)ΓM1M2:στ,=defΓN1N2:σΓM1N1M2N2:τg

tells us that every continuous function is monotonic. The congruence rule for lambda abstraction

(fcong)=defΓ,x:σMN:τΓλx:σ.Mλx:σ.N:στg

is sound since we order functions point-wise.

The final rule is an induction rule for fixed points. From the equational axiom for fixed points, we can prove that f(fixf)fixf, and similar properties that follow from fixf being a fixed point of f. However, in the standard equational proof system, there is no obvious way to make use of the fact that fixf is the least fixed point of f. With approximations instead of equations, we could write a rule

ΓMN=N:σΓfixMN:σ

saying that fixM is an approximation to any fixed point of M. This follows from the more powerful rule of “fixed point induction”, also called Scott induction after Dana Scott.

In stating the rule of fixed point induction, we write ΦA to mean that the equation or approximation A is provable from the set Φ of equations and approximations using the axioms and inference rules given here, combined with the equational axioms and inference rules of typed lambda calculus with fixed points and any additional equational hypotheses (such as axioms for algebraic datatypes).

If A has the form Γ,x:σMN:τ, then we write [P/x]A for the result Γ[P/x]M[P/x]N:τ of removing variable x from the type assignment and substituting P for x in both terms, assuming ΓP:σ. If A is an equation, we define [P/x]A similarly.

Using this notation, the rule to find the fixed point is written as follows.

(fpfind)Φ[/x]A,Φ,[c/x]A[F(c)/x]AΦ[fixF/x]A constant c not in Φ

If we think of A as a way of saying that the variable x has some property, then [/x]A says that this property holds for . The second hypothesis, Φ,[c/x]A[F(c)/x]A, is a way of saying that if this property holds for some arbitrary c. then it holds for F(c). If follows that this property holds for every element of {Fn()n0}, by induction on n. Using the fact that the value of any term depends continuously on each of its free variables, we can easily verify that the property A holds for the least upper bound of this set, namely fixF. This gives us the conclusion of the fixed point induction rule.

A feature of fixed-point induction is that we reason implicitly about sets of form {Fi()i0} without introducing natural numbers into the formal system. While the soundness proof for fixed-point induction uses induction on the integers, the proof system only uses formulas such as approximation or equality.

Ex 4. Fixed-Point Induction Example

To illustrate the use of fixed-point induction, we will show that if N is a fixed point of M, then fixMN.

We assume ΓMN=N:σ (according to the definition of fixed point), which gives us

ΓMNN:σg

since equality implies approximation. This will be useful at a later step of the proof.

Let us now think about how we can use the fixed point induction rule. The conclusion of the rule has the form [fixF/x]A, which matches the statement we wish to prove if we take

AΓ,x:σxN:σ,

with x not free in N, and let F be M. Reading the fixed point induction rule from the bottom up, we can see that it suffices to prove

[/x]AΓ⊥≤N:σ,

which is an axiom, and to show that we can prove

[Mc/x]AΓMcN:σ

if we take [c/x]AΓcN:σ as an additional hypothesis.

If we begin with cN, then by monotonicity (the “congruence rule” for application), we have

ΓMcMN:σ=defg

But since we have already proved MNN above, this gives us

ΓMcN:σ=defg

which is exactly what we need in order to finish the proof by fixed point induction.

This post is licensed under CC BY 4.0 by the author.

Denotational Semantics of Typed Lambda Calculus III — Full Continuous Hierarchy

Denotational Semantics of Typed Lambda Calculus V — CPO Model for Imperative Programs