Lectures onType Theory
Chapter 163
Chapter 163Optional

Synthetic Guarded Domain Theory and Step-Indexed Semantics

Prerequisites. Direct starred prerequisites: Chapter 50, Chapter 58. No later core chapter depends on this route.

A single mutable cell holding a function of type NN is enough to write a divergent program in a language whose pure fragment has no recursion at all. Store the function λx.(get)x into the cell, then apply the cell’s contents to 0: the application reads the cell, obtains the same function, and applies it again.

The obstacle this raises is not the divergence but its denotation. A meaning for the cell’s contents is a function whose meaning mentions the heap, because the body performs get; and a meaning for the heap is a meaning for the cell’s contents. Writing H for the object of heap meanings and M for the effect that reading and writing produce, the requirement is H(NMN),MX=H(results in X with a new H), in which H occurs on both sides and, inside M, in a negative position. A set-theoretic solution of (163.1) does not exist: the right-hand side has strictly larger cardinality than H as soon as H has more than one element.

Chapter 58 produced unique solutions of equations whose recursive occurrence sits under the later modality. This chapter inserts that modality where the operational semantics already spends a step — at the read — and then carries the consequences through to a computational adequacy theorem and to a statement about contextual equivalence.

The internal logic of the topos of trees

Convention 163.1 — Recalled objects

Write S for the presheaf category [ωop,Set] over the poset 12 of positive integers. An object X is a family of sets Xn with restriction maps rnX:Xn+1Xn; a map is a family fn commuting with them. The later functor is (X)1={},(X)n+1=Xn,(nextX)1=!,(nextX)n+1=rnX, and Δ(S) denotes the constant object with Δ(S)n=S and identity restrictions. Chapter 58 proves that preserves finite limits and that every map g:XX has exactly one global element x with x1=g1() and xn+1=gn+1(xn).

Lemma 163.2 — Exponentials

For objects A,B of S, (BA)n={(fm)mn | fm:AmBm, rmBfm+1=fmrmA (m<n)}, with restriction deleting the last component. Consequently (BA)n depends only on the sets Am, Bm and the restriction maps between them for mn.

Proof of Lemma 163.2 — Exponentials

Proof. By the presheaf exponential, (BA)n=Nat(y(n)×A,B). Since y(n)m is a one-point set for mn and empty otherwise, such a natural transformation is exactly a family fm:AmBm for mn subject to the displayed naturality squares. Restriction along n+1n deletes fn+1. The displayed description mentions no datum at a level above n. ◻

Lemma 163.2 is the fact that makes (163.1) tractable: a level-n function space is determined by levels 1 through n of its domain and codomain, so an equation whose right-hand side puts every occurrence of the unknown under one can be solved by recursion on the level.

Definition 163.3 — Predicates and the later operator

A predicate P on an object X of S is a family of subsets PnXn closed under restriction: if αPn+1 then rnX(α)Pn. Predicates are ordered by inclusion at every level. The later operator on predicates is (P)1:=X1,(P)n+1:={αXn+1rnX(α)Pn}. Read αPn as “P holds of α after n steps of observation”.

Proposition 163.4 — The later operator is a predicate former

For every predicate P on X, the family P is a predicate on X, and P⊆⊳P level by level.

Proof of Proposition 163.4 — The later operator is a predicate former

Proof. Closure under restriction at the step from level 2 to level 1 is the inclusion into X1, which holds because (P)1=X1. For the step from n+2 to n+1, let α(P)n+2, so rn+1X(α)Pn+1; since P is closed under restriction, rnX(rn+1X(α))Pn, which says rn+1X(α)(P)n+1.

For the inclusion, P1X1=(P)1; and if αPn+1 then rnX(α)Pn by closure, that is α(P)n+1. ◻

Theorem 163.5 — L"ob induction

Let P be a predicate on X. If PP, then Pn=Xn for every n.

Proof of Theorem 163.5 — L"ob induction

Proof. By induction on n.

Stage 1. (P)1=X1 by definition 163.3, and PP gives X1P1.

Stage n+1. Let αXn+1. The induction hypothesis gives Pn=Xn, so rnX(α)Pn, that is α(P)n+1; the hypothesis PP then gives αPn+1. ◻

Theorem 163.5 is the proof principle used for every statement below whose subject matter is defined by guarded recursion. It is not a disguised induction on a syntactic measure: the object X is arbitrary, and the induction is on the stage of the model. Its hypothesis is the exact counterpart of the typing rule Fix of chapter 58: a proof of P from P.

Guarded lifting

Definition 163.6 — The lifting object

For X in S define LX by recursion on the level: (LX)1:=X1{},(LX)n+1:=Xn+1(LX)n, with restriction rn acting as rnX on the left summand and as the identity inclusion (LX)n(LX)n on the right. Write η:XLX for the left injection and θ:LXLX for the right injection, using (LX)n+1=(LX)n and (LX)1={} with θ1():=.

Proposition 163.7 — L solves its equation, uniquely

LXX+LX, and any object Y with YX+Y is isomorphic to LX. Every element of (LX)n is θk(η(a)) for a unique k<n and aXnk, or θn().

Proof of Proposition 163.7 — L solves its equation, uniquely

Proof. The isomorphism is the identity on the displayed disjoint unions, using (LX)1={} and (LX)n+1=(LX)n. The normal form follows by induction on n: at level 1 an element is in X1 or is ; at level n+1 it is in Xn+1, or lies in (LX)n and has the stated form by the induction hypothesis, and in the second case one further θ is prefixed. Uniqueness of Y: Y1X1{}, and if Yn(LX)n then Yn+1Xn+1Yn(LX)n+1; these bijections commute with restriction because the isomorphism YX+Y does. ◻

Definition 163.8 — Divergence and the monad structure

Let X be the global element of LX with (X)n=θn(); it is the unique fixed point of θ supplied by convention 163.1. For f:XLY define f:LXLY by recursion on the level, f(η(a)):=f(a),f(θ(t)):=θ((f)(t)),f1():=.

Proposition 163.9 — Monad laws

η=id, fη=f, and (gf)=gf.

Proof of Proposition 163.9 — Monad laws

Proof. The middle equation is the first clause of definition 163.8. The other two are proved by induction on the level, using proposition 163.7 to split an element as η(a), as θ(t) with t at a strictly smaller level, or as at level 1. In the θ case both sides are θ applied to the corresponding equation one level down, which is the induction hypothesis; in the η case both sides reduce by the middle equation; at level 1 both sides are . ◻

A recursive domain equation for one higher-order cell

Definition 163.10 — The heap object

Define objects H and, for each X, MX by MX:=L(X×H)H,H1:={},Hn+1:=(MΔ(N)Δ(N))n, with r1H:=! and rn+1H the restriction of the exponential of lemma 163.2.

The definition is not circular. By lemma 163.2 the level-n component of an exponential depends only on levels 1,,n of its domain and codomain, and the level-n component of L depends only on levels 1,,n of its argument; so Hn+1 is determined by H1,,Hn.

Theorem 163.11 — Solution of the equation

Write F:=(MΔ(N))Δ(N). Then HF=(L(Δ(N)×H)H)Δ(N), and H is the unique object with this property up to isomorphism.

Proof of Theorem 163.11 — Solution of the equation

Proof. The isomorphism. By the description of in convention 163.1, (F)1={}=H1 and (F)n+1=Fn=Hn+1 by definition 163.10. The restriction maps agree by construction.

Uniqueness. Let H satisfy the same isomorphism. At level 1 both are one-point sets. Suppose a family of bijections HmHm commuting with restriction has been given for mn. By lemma 163.2 and definition 163.6, the set Fn is built from H1,,Hn by disjoint unions, products and function sets, so those bijections induce a bijection FnFn; composing with the two isomorphisms gives a bijection Hn+1Hn+1, and it commutes with restriction because the isomorphisms do. ◻

Example 163.12 — The first two stages of the heap object

Unfold definition 163.10 twice. At level 1 the heap object is a one-point set: after one step of observation, nothing about the stored function is visible. At level 2, H2=F1=(functions N(MΔ(N))1),(MΔ(N))1=(functions H1(L(Δ(N)×H))1)=(N×{}){}, so H2 is the set of functions NN{}. A stage-2 heap is therefore a function on numbers that may either return a number or fail to return. That this is exactly the information available after two steps of observation is what the whole construction is for.

Exercise 163.1

★★☆ Compute H3 explicitly as a set built from N, disjoint unions and function sets, showing every step. Then exhibit the restriction map H3H2 and check that it agrees with lemma 163.2. Finally, state what a stage-3 heap records that a stage-2 heap does not.

Exercise 163.2

★★☆ Delete the from theorem 163.11, so that the equation reads HF with F as displayed.

  1. Show that the level-1 component of the right-hand side is then the set of functions N(H1(N×H1){}), and conclude that the recursion on levels no longer terminates.

  2. In sets, show that no set S with more than one element satisfies S(S(N×S){})N, by a cardinality argument. Name the position of the occurrence of S that makes the argument work.

A language with one higher-order cell

Definition 163.13 — The calculus Λ _ cell

Types and terms are τ,σ::=Nτσ,e::=xnsuceλx:τ.ee1e2getset(e1;e2), with n a numeral for each nN. Write F:=NN for the type of the cell’s contents. The typing rules are those of the simply typed calculus of chapter 2 together with

Γ ctx
Γget:F
Get
Γe1:FΓe2:τ
Γset(e1;e2):τ
Set

Values and evaluation contexts are v::=nλx:τ.e,E::=[]EevEsucEset(E;e). A configuration he consists of a closed value h with h:F, the contents of the cell, and a term e. The transitions are hE(λx:τ.e)vBetahEe[v/x],hEsucnSucchEn+1,hEgetReadhEh,hEset(v;e)WritevEe. Write for the union of the four relations and for its reflexive-transitive closure. A configuration converges to v, written hev, when hehv for some h.

Example 163.14 — Landin's knot

Let h:=λx:N.getx and e:=get0. Then heReadhh0Betahget0=he, so the configuration returns to itself after two steps, one of which is a Read. The pure fragment of Λcell has no recursion operator; all of this program’s recursion passes through the cell.

Proposition 163.15 — Every divergence reads the cell

Every infinite transition sequence of Λcell contains infinitely many Read steps.

Proof of Proposition 163.15 — Every divergence reads the cell

Proof. Suppose a transition sequence from he performs no Read step. Replace get everywhere by a fresh variable g:F and set(e1;e2) by (λz:F.e2)e1 with z not free in e2. This translation sends every Beta step to a Beta step, every Succ step to a Succ step and every Write step to a Beta step of the simply typed calculus, and it sends well-typed terms of Λcell to well-typed terms of the simply typed calculus over the context g:F. By the normalization theorem of chapter 2 that calculus has no infinite reduction sequence from a well-typed term, so the original sequence is finite.

Hence a transition sequence with only finitely many Read steps decomposes into finitely many Read-free segments, each finite, and is therefore finite itself. ◻

Proposition 163.15 identifies the exact operational event that the later modality will account for. The interpretation below spends one at each Read step and none at the other three.

Interpretation and soundness

Definition 163.16 — Interpretation of types and terms

Set [[N]]:=Δ(N),[[τσ]]:=(M[[σ]])[[τ]],[[x1:τ1,,xk:τk]]:=[[τ1]]××[[τk]], so that [[F]]=F and, by theorem 163.11, H[[F]]. A derivation of Γe:τ is interpreted as a map [[e]]:[[Γ]]M[[τ]] by the clauses [[xi]]ρ:=η(ρi,),[[n]]ρ:=η(n,),[[suce]]ρ:=(λ(a,).η(a+1,))([[e]]ρ),[[λx:τ.e]]ρ:=η(λa.[[e]](ρ,a), ),[[e1e2]]ρ:=(λ(φ,1).(λ(a,2).φa2)([[e2]]ρ1))([[e1]]ρ),[[get]]ρ:=θ((λφ.η(φ,))()),[[set(e1;e2)]]ρ:=(λ(φ,1).[[e2]]ρ(nextφ))([[e1]]ρ). For a closed value h of type F write [[h]]H:=next(φh), where φh is the function component of [[h]]()()=η(φh,); the clause for abstraction shows that this does not depend on .

The clause for get is the only one that produces a θ. It must: the heap is an element of [[F]], so the stored function is not available now, and the only way to use it is to apply the functorial action of and then re-enter L through θ. The clause for set is dual: it stores nextφ, which is available later, and spends no step.

Lemma 163.17 — Evaluation contexts

For every evaluation context E with ΓEe:τ and Γe:σ there is a map KE:[[Γ]]×[[σ]]×HM[[τ]] with [[Ee]]ρ=(λ(a,).KE(ρ,a,))([[e]]ρ).

Proof of Lemma 163.17 — Evaluation contexts

Proof. By induction on E. For E=[] take K[](ρ,a,):=η(a,); the equation is then η([[e]]ρ)=[[e]]ρ, which is proposition 163.9. For each other case the corresponding clause of definition 163.16 is already of the displayed form with the inductively given K substituted into the outer continuation; associativity of (), again from proposition 163.9, merges the two continuations into one. We display the case E=Ee2: the clause for application gives [[Eee2]]ρ=(λ(φ,1).c(φ,1))([[Ee]]ρ)=(λ(a,).(λ(φ,1).c(φ,1))KE(ρ,a,))([[e]]ρ), where c is the continuation of the application clause; the second equality is the induction hypothesis followed by associativity, and the bracketed map is KEe2. The remaining three cases are the same computation with c replaced by the continuation of the clause for v[], for suc, and for set. ◻

Lemma 163.18 — Substitution

Let Γ,x:τe:σ and let v be a closed value of type τ with value component [[v]]v, meaning the first component of [[v]]ρ=η([[v]]v,). Then [[e[v/x]]]ρ=[[e]](ρ,[[v]]v).

Proof of Lemma 163.18 — Substitution

Proof. By induction on the derivation of Γ,x:τe:σ. We display the two cases in which the variable is consumed or captured; the remaining four clauses of definition 163.16 are composites of the subterm interpretations with maps not mentioning ρ, so each is the induction hypothesis applied componentwise.

Variable case. If e=x then e[v/x]=v and [[v]]ρ=η([[v]]v,)=[[x]](ρ,[[v]]v) by the clause for variables. If e=y with y distinct from x, both sides are η(ρy,).

Binder case. If e=λy:σ1.e1 with yFV(v){x}, then [[λy:σ1.e1[v/x]]]ρ=definition163.16η(λa.[[e1[v/x]]](ρ,a),)=IHη(λa.[[e1]](ρ,[[v]]v,a),), and the right-hand side is [[λy:σ1.e1]](ρ,[[v]]v) after exchanging the last two components of the environment, which is legal because the two variables are distinct. ◻

Theorem 163.19 — Soundness of the interpretation

Let e:τ and let h be a closed value of type F. Write [[he]]:=[[e]]()[[h]]H. Then heBetahe,heSucche,orheWritehe implies [[he]]=[[he]], and heReadhEhimplies[[he]]=θ(next[[hEh]]).

Proof of Theorem 163.19 — Soundness of the interpretation

Proof. By lemma 163.17 it suffices to treat the redex, since both sides are the same continuation applied to the interpretation of the redex.

Beta. Unfolding the clauses for application and abstraction, [[(λx:τ.e)v]]ρ=definition163.16(λa.[[e]](ρ,a))[[v]]v=lemma163.18[[e[v/x]]]ρ, where [[v]]v is the value component of [[v]]ρ.

Succ. Both sides are η(n+1,).

Write. The clause for set evaluates e1 to a value v with function component φv and continues with [[e2]]ρ(nextφv), and nextφv=[[v]]H by definition 163.16. No θ is produced.

Read. Write =[[h]]H=next(φh). Then [[get]]ρ=definition163.16θ((λφ.η(φ,))(nextφh))=nat.θ(next(η(φh,)))=definition163.16θ(next([[h]]ρ)), the middle step by naturality of next. Applying lemma 163.17 to both sides and using that () commutes with θ by definition 163.8 gives the displayed equation. ◻

Corollary 163.20 — Reading is the only step the model counts

If hehv with exactly k Read steps, then [[he]]=θk(η([[v]]v,[[h]]H)).

Proof of Corollary 163.20 — Reading is the only step the model counts

Proof. Induction on the length of the transition sequence, applying the appropriate clause of theorem 163.19 at each step; the Read clause contributes one θ and the other three contribute none. At the end the configuration is a value, and the clauses of definition 163.16 for numerals and abstractions give the displayed η. ◻

Example 163.21 — The knot has denotation

Take h and e from example 163.14. By example 163.14 the configuration performs one Read in every two steps and never converges, so corollary 163.20 applies with every k and gives [[he]]=θk([[he]])for every k1. By proposition 163.7 an element of (L(Δ(N)×H))n with n leading θ’s is θn(), so [[he]]=.

The stored function is computed by the same unfolding. By example 163.12, [[h]]H at stage 2 is the function NN{} obtained from [[getx]] at stage 1; the clause for get places a θ there, and θ1()= by definition 163.6. Hence the stage-2 stored function is constantly : after two steps of observation, the knot’s contents already return nothing on every input.

The guarded logical relation

Corollary 163.20 runs from the operational semantics to the model. The converse direction — from a denotation to a run — is the content of computational adequacy, and it needs a relation between the two. At the type N the relation can be avoided, as theorem 163.23 below shows; at a function type it cannot, because the denotation of a function is not a run but a family of maps, one at each stage.

Lemma 163.22 — Determinism and progress

Let e:τ and let h be a closed value of type F. Then he has at most one transition, and it has none only if e is a value.

Proof of Lemma 163.22 — Determinism and progress

Proof. Every closed well-typed term is a value or decomposes uniquely as Ee0 with e0 one of (λx:τ.e)v, sucn, get, set(v;e); the decomposition is unique because the grammar of E in definition 163.13 fixes, at each constructor, which subterm is evaluated first, and the value grammar determines when to move on. Exactly one transition applies to each of the four redexes. For progress, a closed well-typed non-value term has such a decomposition by induction on its typing derivation: at an application e1e2 either e1 is not a value, and the induction hypothesis gives a decomposition inside Ee2, or e1 is a value of function type, hence an abstraction by inspection of the value grammar, and the same argument applies to e2. ◻

Theorem 163.23 — Adequacy at

Let e:N and let h be a closed value of type F. Then hem with exactly k Read steps[[he]]=θk(η(m,)) for some , and he has an infinite transition sequence if and only if [[he]]=.

Proof of Theorem 163.23 — Adequacy at

Proof. By lemma 163.22 exactly one of two things happens: the transition sequence from he is finite and ends in a value, or it is infinite.

Convergent case. Corollary 163.20 gives the displayed right-hand side, with m the value of the numeral reached.

Divergent case. By proposition 163.15 the sequence contains infinitely many Read steps. Fix n and take a prefix containing n Read steps, ending at hnen. Applying theorem 163.19 along that prefix gives [[he]]=θn(u) for some u. By proposition 163.7 the only element of (L(Δ(N)×H))n with n leading θ’s is θn(), so [[he]]n=()n. As n was arbitrary, [[he]]=.

The two right-hand sides are distinct, since θk(η(m,)) at stage k+1 is not θk+1(); so each direction of the stated equivalences follows from the other case. ◻

Theorem 163.23 says nothing about a closed term of type F: its denotation is an element of F, and there is no numeral to compare. The relation defined next repairs that, and the heap relation is where the later operator does its work.

Definition 163.24 — The guarded relations

Write Valτ for the set of closed values of type τ and Cfgτ for the set of configurations he with e:τ. Define predicates Vτ on Δ(Valτ)×[[τ]],Cτ on Δ(Cfgτ)×L([[τ]]×H),W on Δ(ValF)×H, simultaneously, by recursion on the stage and, at each stage, by induction on the type.

  1. W1:=Δ(ValF)×H1, and for n1, Wn+1:={(h,)(h,)(VF)n}, using Hn+1=[[F]]n from theorem 163.11.

  2. (VN)n:={(m,m)mN}.

  3. (Vτσ)n is the set of pairs (λx:τ.e,φ) such that for every mn, every (v,a)(Vτ)m and every (h,)Wm, (he[v/x], φm(a)())(Cσ)m.

  4. (Cτ)n is defined by cases on the normal form of its second argument, using proposition 163.7:

    • (he,η(a,))(Cτ)n when hehv by a sequence with no Read step, and (v,a)(Vτ)n and (h,)Wn;

    • (he,θ(t))(Cτ)n+1 when hehEget by a sequence with no Read step and (hEh,t)(Cτ)n;

    • (he,)(Cτ)1 always.

The recursion is well founded: clause 1 at stage n+1 mentions VF at stage n; clause 4 at stage n+1 mentions Cτ at stage n; clause 3 at stage n mentions Cσ at stages at most n and types smaller than τσ. Clause 1 is the later operator of definition 163.3 applied to VF and transported along H[[F]]: a heap and its denotation are related now exactly when the stored value and its denotation are related later.

Lemma 163.25 — The relations are predicates

Each of Vτ, Cτ and W is closed under restriction.

Proof of Lemma 163.25 — The relations are predicates

Proof. For VN the level plays no role. For Vτσ the defining condition quantifies over all mn, so it is inherited at n1; and restriction of φ from level n to level n1 deletes its top component by lemma 163.2, leaving the components at mn1 unchanged. For W: an element of Wn+2 is a pair in (VF)n+1, which restricts into (VF)n, that is into Wn+1; and every pair lies in W1. For Cτ, restriction of θ(t) from level n+1 is θ(r(t)) or, at n=1, the element ; the first case is the induction hypothesis and the second is unconditional. Restriction of η(a,) is η(r(a),r()), and the operational conditions do not mention the level. ◻

Theorem 163.26 — Fundamental lemma

Let x1:τ1,,xk:τke:τ. For every stage n, every family (vi,ai)(Vτi)n and every (h,)Wn, (he[v/x], [[e]](a))(Cτ)n.

Proof of Theorem 163.26 — Fundamental lemma

Proof. By induction on the derivation of Γe:τ. We write ρ for (a1,,ak) and γe for e[v/x].

Variable and numeral. [[xi]]ρ=η(ai,) and γxi=vi is already a value, so the first clause of definition 163.24(4) applies with the empty transition sequence, using (vi,ai)(Vτi)n and (h,)Wn. The numeral case is the same with (m,m)VN.

Abstraction. [[λx:τ0.e0]]ρ=η(λa.[[e0]](ρ,a),), and γ(λx:τ0.e0) is a value; so it suffices to show that the pair lies in (Vτ0σ)n. Let mn, let (v,a)(Vτ0)m and let (h1,1)Wm. By lemma 163.25 the pairs (vi,ai) lie in (Vτi)m, so the induction hypothesis for e0 at stage m gives (h1γe0[v/x],[[e0]](ρ,a)1)(Cσ)m, which is the required condition.

Application. Let e=e1e2. The induction hypothesis for e1 places (hγe1,[[e1]]ρ) in (Cτ0τ)n. We argue by induction on the number of leading θ’s of [[e1]]ρ. If there are none, the clause for η gives a Read-free run hγe1h1w with (w,φ)(Vτ0τ)n and (h1,1)Wn; applying the induction hypothesis for e2 at (h1,1) and then the defining condition of Vτ0τ at m=n yields the claim, because lemma 163.17 identifies the denotation of the whole application with the corresponding continuation applied to these pieces. If there is at least one leading θ, the clause for θ gives a Read step after a Read-free run, and the same decomposition at stage n1 is the inner induction hypothesis.

Successor and set. As in the application case, with the continuation of the corresponding clause of definition 163.16; for set the new heap is nextφv, and (v,φv)(VF)n from the induction hypothesis gives (v,nextφv)Wn+1 by definition 163.24(1), hence membership in Wn by lemma 163.25.

get. At stage 1 the second clause of definition 163.24(4) is unconditional, since [[get]]ρ has a leading θ and θ1()=. At stage n+1, the computation in the Read case of theorem 163.19 gives [[get]]ρ=θ(η(,rn)), so the θ clause requires (hh,η(,rn))(CF)n. The term h is a value, so the empty run discharges the operational condition, and two memberships remain: (h,)(VF)nand(h,rn)Wn. The first is the hypothesis (h,)Wn+1 read through definition 163.24(1). The second follows from the first by lemma 163.25 and the same clause one stage down. ◻

Corollary 163.27 — Adequacy at every type

Let e:τ and let (h,)Wn for every n. If [[e]]()=θk(η(a,)), then he converges to a value v with (v,a)(Vτ)n for every n>k.

Proof of Corollary 163.27 — Adequacy at every type

Proof. Apply theorem 163.26 at stage n and unfold the θ clause of definition 163.24(4) k times; each unfolding produces one Read step preceded by a Read-free run, and the remaining datum is η(a,), whose clause supplies the value v together with (v,a)(Vτ)nk. Since this holds for every n, the membership holds at every stage. ◻

Theorem 163.28 — Denotational equality implies contextual equivalence

Let e1:τ and e2:τ with [[e1]]=[[e2]]. Then for every context C with Cei:N, every closed value h of type F and every numeral m, hCe1mhCe2m.

Proof of Theorem 163.28 — Denotational equality implies contextual equivalence

Proof. Every clause of definition 163.16 defines the denotation of a term from the denotations of its immediate subterms, so [[Ce]] is a function of [[e]]; hence [[Ce1]]=[[Ce2]]. Apply theorem 163.23 to each side: the left converges to m if and only if the common denotation is θk(η(m,)), and likewise for the right. ◻

Exercise 163.3

★★☆ Let Ω:=set(λx:N.getx;get0) of type N.

  1. Show [[Ω]]()= for every , by computing the Write step and then applying example 163.21.

  2. Conclude from theorem 163.28 that Ω is contextually equivalent to every closed term of type N whose denotation is , and exhibit one such term that contains no set.

  3. State why the converse of theorem 163.28 is not established by these results, naming the property of the interpretation that would be required.

Two other step-indexed models, at their own signatures

Theorem 163.11 solved the equation for one cell of one fixed type. A language with allocation needs worlds: a world records, for each allocated location, the semantic type of its contents, and the recursion is then between worlds and semantic types. Two published models make different choices about that recursion, and neither theorem is a consequence of section 163.3section 163.6.

Predicative references and universe-indexed worlds. Koronkevich and Bowman, Type Universes as Kripke Worlds, remove the recursion instead of solving it. Their calculus λPR kinds the type of an allocated term by a universe level, and a world at level i records only allocations at levels below i; a function that closes over a level-i reference has a type at level i+1. The semantic worlds are then stratified by the universe hierarchy, and the type-world circularity does not arise. Their Theorem 3.3 is the fundamental lemma: if Γe:τ and τ::Typei, then for every γ in the context relation at a world W, the substituted term γ(e) lies in the expression relation E[[τ]]i(W), whose membership condition includes termination. Their language is therefore terminating, and Landin’s knot is not well typed in it: writing λx.(get)x into the cell would require the stored function to close over the very level at which the cell is allocated. The earlier workshop note One Weird Trick to Untie Landin’s Knot states a language and a termination claim that were conjectural and owns no theorem; only the later paper’s theorems are used here.

Substructural state with explicit step indices. Ahmed, Fluet and Morrisett give a step-indexed model of a substructural polymorphic λ-calculus with four kinds of mutable reference — unrestricted, relevant, affine and linear — supporting deallocation and type-varying update. Their semantic types are indexed by an explicit natural-number step count and by a local store description, related to actual stores by a judgment s:kψ asserting that the store s satisfies the description ψ for k steps, and their Theorem 1 is a soundness theorem for that calculus from which type safety follows. The index there is a number carried in the definition; in definition 163.24 it is the stage of the model and never appears in a formula. The two are related by the translation of chapter 58: an explicitly indexed family is a presheaf on ω, and a decrement of the index is an application of . That translation is a change of presentation; it does not transport their theorem to the language of definition 163.13, whose store discipline is unrestricted and whose type system has no substructural qualifiers.

General store with polymorphism. Sterling, Gratzer and Birkedal build a model of a dependent type theory with general reference types and recursive types by combining guarded recursion with impredicative polymorphism. Their higher-order state monad is (TA)w:=wwHwL(wwHw×Aw), and their semantic reference type is (refA)w={l|w|nextA=wl}: a world assigns to each location a delayed semantic type, which is where guardedness enters. Their Theorem 2.5 states that T is a strong monad each of whose values is a guarded domain, and Theorem 2.6 lists the equations of state that the model validates. Their worlds need not be syntactically definable, which is what makes the model compatible with relational reasoning about stateful abstract data types. Definition 163.10 is the degenerate case of a single location of a fixed type, where the world is constant and only the delay survives.

Comparison with a synthetic domain model

The guarded model produces fixed points without any order. The older synthetic route produces them from an order, and the two theorems are about disjoint classes of maps.

Definition 163.29 — Chain-complete posets and continuous maps

An ω-cpo is a partially ordered set in which every increasing sequence d0d1 has a least upper bound; it is pointed when it has a least element . A map f:DE of ω-cpos is continuous when it is monotone and f(idi)=if(di) for every increasing sequence.

Theorem 163.30 — Least fixed points

Let D be a pointed ω-cpo and f:DD continuous. Then fix(f):=ifi() exists, satisfies f(fix(f))=fix(f), and is below every d with f(d)d.

Proof of Theorem 163.30 — Least fixed points

Proof. Monotonicity and f() give fi()fi+1() by induction on i, so the sequence is increasing and its join exists. Then f(ifi())=cont.ifi+1()=f()ifi(). If f(d)d then fi()d by induction on i, using d and monotonicity, so the join is below d. ◻

Proposition 163.31 — Fixed-point induction

Let D be a pointed ω-cpo, f:DD continuous, and PD a subset containing and closed under joins of increasing sequences. If f(P)P then fix(f)P.

Proof of Proposition 163.31 — Fixed-point induction

Proof. fi()P by induction on i, and fix(f) is the join of that increasing sequence, which lies in P by closure. ◻

Remark 163.32 — The two fixed-point principles do not transport

Theorem 163.30 and the guarded fixed-point theorem of convention 163.1 apply to different maps and produce different objects.

  1. The identity map on a pointed ω-cpo with at least two elements is continuous and has every element as a fixed point; theorem 163.30 selects . The identity on an object of S is not of the form gnext unless the object is trivial, so the guarded theorem does not apply to it, and there is nothing for it to select.

  2. Conversely, the map θ:LXLX of definition 163.6 has exactly one fixed point, , by convention 163.1, and the object LX carries no order in which that could be described as least.

  3. The two induction principles have incomparable hypotheses. Proposition 163.31 requires P and closure under joins; theorem 163.5 requires PP. A predicate on LX that holds of η(a) for every a and fails at satisfies neither, and a predicate that holds only at satisfies the first but not the second.

Fiore and Rosolini construct a Grothendieck topos in which the category of ω-cpos and continuous maps sits as a reflective exponential ideal, and show that with the dominance induced by the two-element chain it is a model of Hyland’s axioms for synthetic domain theory; the fixed-point property there is that the canonical map from the initial algebra of the lifting functor to its final coalgebra is an isomorphism, which is proved by exhibiting the colimit and the limit of the same diagram. Their second model, built from ω-complete posets and stable maps, satisfies all of those axioms except two, and the failures are exactly that such categories are not closed under adding a top element and that unions of stable open subsets need not be stable open. Neither model validates theorem 163.5, and definition 163.10 does not solve its equation in either.

Exercise 163.4

★★☆ Let D be the pointed ω-cpo {01}{} of the natural numbers with a top element adjoined, and let f(d):=d+1 with f()=.

  1. Compute fix(f) by theorem 163.30 and identify all fixed points of f.

  2. Exhibit the corresponding object of S whose stage-n set is {0,,n1}{} and a map out of its later object whose unique fixed point is the family ()n, and say which of the fixed points found in part 1 it corresponds to.

  3. State which of theorem 163.30, theorem 163.5 proves that no other fixed point exists in its setting, and why the other cannot.

What is proved, and at which signature

Section 163.1Section 163.6 prove: the later operator on predicates and Löb induction in S (definition 163.3, theorem 163.5); the guarded lifting monad and its laws (definition 163.6, proposition 163.9); the solution of the recursive domain equation for one higher-order cell (theorem 163.11); soundness of the interpretation of Λcell (theorem 163.19); computational adequacy at N (theorem 163.23); the fundamental lemma for the guarded logical relation (theorem 163.26) and adequacy at every type (corollary 163.27); and the contextual consequence (theorem 163.28).

Four statements are not consequences of these.

Guarded productivity is not general recursion. Every in definition 163.16 corresponds to a Read step, and proposition 163.15 shows that no other source of divergence exists in Λcell. A language whose pure fragment diverges requires a further guarded clause, and proposition 163.15 then fails as stated.

Allocation is not modelled. Definition 163.10 fixes one cell of one type. A language with new requires worlds, and with them a second recursion, between worlds and semantic types; section 163.7 records three published resolutions of that recursion and their exact theorems.

The contextual result is one-directional. Theorem 163.28 sends denotational equality to contextual equivalence. Full abstraction is the converse, and nothing above establishes it; the interpretation of a function type in definition 163.16 is a full function space, which by the argument of chapter 158 contains elements that no term denotes.

No order is available. Remark 163.32 shows that neither fixed-point principle implies the other, so a proof that uses proposition 163.31 does not transport into S, and a proof that uses theorem 163.5 does not transport into a category of domains.

Suggested first pass.

Problems exercise 163.5, exercise 163.6, and exercise 163.8 form the suggested first pass. None of these problems is a prerequisite for a later chapter.

Exercise 163.5

★★★ Extend Λcell to two cells, both of type F, with get1,get2 and set1,set2.

  1. Write the recursive domain equation for the pair-of-cells object H2 and solve it by the level recursion of definition 163.10, computing H12 and H22 explicitly.

  2. Extend definition 163.16 and check the two new Read clauses of theorem 163.19.

  3. Write the two-cell version of Landin’s knot in which the first cell calls the second and the second calls the first, and compute its denotation at stages 1, 2 and 3.

  4. Now allow the second cell to hold a value of type N rather than F, and state precisely which part of the level recursion still works and which part becomes unnecessary.

Exercise 163.6

★★★ Theorem 163.26 was proved by induction on the typing derivation, with the stage bookkeeping visible only in the get case.

  1. Write out the application case in full, displaying the continuation K supplied by lemma 163.17 and the exact instance of definition 163.24(3) used.

  2. Give a second proof of the get case by Löb induction (theorem 163.5) on the predicate “for all h and related at this stage, the configuration and the denotation are related”, and state which hypothesis of theorem 163.5 the heap clause definition 163.24(1) supplies.

  3. Show that definition 163.24(3) becomes false if the quantifier “for every mn” is replaced by “for m=n”, by exhibiting a pair that would then fail to be closed under restriction.

Exercise 163.7

★★☆ Add to Λcell a recursive-function former recf:τσ.λx:τ.e with the transition hE(recf.λx:τ.e)vUnfoldhEe[v/x][recf.λx:τ.e/f].

  1. Give the semantic clause using the guarded fixed point of convention 163.1, arranged so that one Unfold step contributes one θ.

  2. State and prove the extension of proposition 163.15: every infinite transition sequence contains infinitely many steps that are Read or Unfold.

  3. Conclude that theorem 163.23 extends verbatim, and identify the single property of the extended interpretation that makes the same proof work.

Exercise 163.8

★★★ Practical project.guarded-store-adequacy Implement, in Kappa, a stage-indexed interpreter for Λcell and an operational machine for it, and use them to check adequacy on named programs.

Calculus to implement. The syntax, typing and transitions of definition 163.13, with de Bruijn indices and capture-avoiding substitution. Two executables are required: an operational machine that runs a configuration for a bounded number of transitions and reports the value, the number of Read steps, or a timeout; and a stage-n evaluator that computes [[he]]n as an element of (L(Δ(N)×H))n in the normal form θk(η(m,)) or θn() of proposition 163.7, representing a stage-n heap by the finite datum computed in example 163.12 and exercise 163.1.

Invariant. The evaluator must consume exactly one stage at each get and none at the other three transitions, and it must satisfy the restriction equation rn([[he]]n+1)=[[he]]n for every evaluated configuration; the program must check that equation and report a failure when it does not hold. This is the executable form of theorem 163.19.

Concrete result. For a configuration and a stage bound N, a report giving the machine’s outcome, the evaluator’s normal form at each stage 1,,N, and a verdict recording whether the two agree in the sense of theorem 163.23: convergence with k Read steps must match θk(η(m,)) for every stage above k, and a timeout at stage bound N must match θN().

Acceptance test. With N=6 the verdict must be agreement on: λx:N.xget3, converging to 3 with one Read; λx:N.xsuc(suc1), converging to 3 with no Read; λx:N.xset(λy:N.sucy;get1), converging to 2 with one Read; and the knot he of example 163.14, for which the evaluator must return θn() at every stage n6 and the machine must time out. The program must also print the stage-2 stored function of example 163.21 and check that it is constantly . Produce three mutations that still run — spend a stage at set instead of get, spend no stage at get, and drop the restriction check — and confirm that each makes at least one named case disagree. State explicitly that the program illustrates theorem 163.23 on finitely many configurations and finitely many stages, and does not prove it.

Search the book

Type to search the local edition.