Lectures onType Theory
Chapter 61
Chapter 61Core route

Logical Frameworks, Encodings, and Adequacy

The type A:propdedA is well formed in the PTS λP of definition 60.5, extended by prop: and ded:A:prop. This fact says nothing about whether an inhabitant of dedA denotes a derivation of the object proposition A. A framework must supply a representation in both directions and prove that canonical framework objects contain neither missing derivations nor exotic inhabitants. We begin with the smallest logic for which a binder is unavoidable.

LF and canonical objects

The Edinburgh Logical Framework, abbreviated LF, is a dependent lambda calculus used to represent the syntax, judgments, and derivations of another deductive system. Its four syntactic classes are K::=typex:AK,A,B::=aAMx:AB,M,N::=cxMNλ(x:A).M,Σ::=Σ,a:KΣ,c:A. Kinds classify LF families, LF families classify LF objects, and an LF signature Σ declares family constants a and object constants c. The framework judgments Σ sig,Γ ctxΣ,ΓΣA:K,ΓΣM:A are distinct from every encoded object judgment introduced below.

Definition 61.1 — LF typing

LF has the structural, product, abstraction, application, and conversion rules of the PTS in definition 60.3, restricted as follows. Its conversion rule uses LF beta-eta equality in place of PTS beta equality. Σtype kind,a:KΣΓΣa:KFam,c:AΣΓΣc:ACon. In kinds and families alike, the product domain is a family and the bound variable is an object variable; no product binds a family variable. The LF kernel has no recursion, pattern matching, induction, or user-defined rewriting.

The restriction keeps type checking decidable and makes the head of every closed normal object visible in its signature. Adding a case operator for an encoded syntax would permit a meta-function to inspect an object variable; such a function need not represent object-language binding.

Definition 61.2 — Atomic and canonical LF objects

An atomic LF object is a variable or object constant applied to zero or more canonical arguments. A canonical LF object at a product type x:AB has the form λ(x:A).M, where M is canonical at B; a canonical object at an atomic family has atomic form. Thus canonical objects are beta-normal and eta-long. We write ΓΣM can:A for the resulting mutually inductive canonical and atomic judgments.

Example 61.3 — Why beta-normal is insufficient

Let f:x:AB be an LF variable. The object f is beta-normal, but its canonical form is λ(x:A).fx. Adequacy is stated for the latter form. Otherwise one object-language constructor with a binding argument could be represented both by a lambda and by an unapplied function variable.

Theorem 61.4 — Canonical forms for LF

If Σ and Γ are well formed and ΓΣM:A, then there is a unique canonical M, up to alpha-equivalence, such that ΓΣM can:AandM=βηM. LF type checking and equality of well-typed objects are decidable.

Proof of Theorem 61.4 — Canonical forms for LF

Proof. The theorem is imported for the exact LF calculus of definition 61.1. Pfenning’s conversion procedure returns a unique canonical object at the given type and characterizes definitional equality by equality of those results [Pfe01]. The same presentation proves decidability of every LF judgment [Pfe01]. Harper, Honsell, and Plotkin give the fully-applied-head characterization that the adequacy proofs use [HHP93]. These imports include LF beta-eta conversion, dependent products, signatures, and contexts, but no rewrite extension. ◻

Exercise 61.1

★☆☆ In the context f:x:Ay:BC, write the eta-long canonical form of f. Then beta-reduce its application to canonical M:A and N:B[M/x] and mark the two contraction steps, including the substituted annotation on the inner lambda. (Four lines.)

Natural deduction as canonical LF data

Fix a finite set P0 of object propositional atoms. Let object propositions and labeled natural deductions be P,Q::=pPQ(pP0),D,E::=usupI(u.D)supE(D,E). The object judgment ΔD:P is generated by the three rules u:PΔΔu:PSupAssmΔ,u:PD:Qudom(Δ)ΔsupI(u.D):PQSupI ΔD:PQΔE:PΔsupE(D,E):QSupE. It is not an LF judgment. Object contexts are finite lists with pairwise distinct labels.

Definition 61.5 — The implicational LF signature

The signature Σ contains prop:type,imp:proppropprop,ded:proptype,supI:P:propQ:prop(dedPdedQ)ded(impPQ),supE:P:propQ:propded(impPQ)dedPdedQ. and a declaration p:prop for every pP0. The judgments-as-types representation maps an object judgment ΔP to the LF family dedP. The representation is defined compositionally by p:=p,PQ:=impPQ. The derivation clauses are u:=u,supI(u.D):=supIPQ(λ(u:dedP).D),supE(D,E):=supEPQDE. In the final two clauses, P,Q are determined by the object derivation. An object context u1:P1,,un:Pn maps to the LF context u1:dedP1,,un:dedPn.

Lemma 61.6 — Proposition-code inversion

Let Γ=Δ be a represented derivation context, so every declaration of Γ has family dedQ. If ΓΣM can:prop, then there is a unique object proposition P such that M=P.

Proof of Lemma 61.6 — Proposition-code inversion

Proof. The family prop is atomic, so M is atomic. Its head is either a declared atom p:prop with no arguments or imp fully applied to two canonical objects of family prop. In the first case decode p; in the second, apply the two induction hypotheses and decode impPQ as PQ. No context variable has family prop in a represented derivation context. Constructor disjointness and the two induction hypotheses give uniqueness. ◻

The representation of implication introduction uses higher-order abstract syntax, abbreviated HOAS: the LF binder represents the object derivation’s discharged assumption. Object substitution is therefore implemented by LF beta-reduction when a represented hypothetical derivation is applied to the substituting derivation. This fact does not add a computation rule between the constants supI and supE.

Example 61.7 — A hypothetical derivation

The object derivation of P(QP) is represented by supIPQP(λ(u:dedP).supIQP(λ(v:dedQ).u)). The LF type is dedP(QP). The two LF lambdas bind exactly the two object assumptions; the inner body may use u but does not use v.

Definition 61.8 — Compositionality and adequacy

A representation is compositional when it commutes with every object substitution. For derivation substitution this means D[E/u]=βD[E/u]. It is adequate for a judgment when, for each object context whose labels are pairwise distinct and each conclusion generated from the declared object atoms, it is a bijection between object derivations modulo alpha-equivalence and canonical LF objects of the represented family in the represented context.

Lemma 61.9 — Compositionality of the implication encoding

For every object derivation D, derivation E, and label u for which D[E/u] is defined, D[E/u]=βD[E/u].

Proof of Lemma 61.9 — Compositionality of the implication encoding

Proof. Induct on D. The assumption cases are u[E/u]=E=substitutionatuu[E/u],v[E/u]=v=vuv[E/u]. For elimination, the induction hypotheses apply to both subderivations and the constructor is unchanged. For D=supI(v.D0), choose the representative with vFV(E){u}. Then supI(v.D0)[E/u]=supIPQ(λ(v:dedP).D0[E/u])=IHsupIPQ(λ(v:dedP).D0[E/u])=supI(v.D0)[E/u]. These are all object derivation constructors. ◻

Theorem 61.10 — Adequacy for implicational natural deduction

Let Δ=u1:P1,,un:Pn, where the labels ui are pairwise distinct and every atom occurring in P1,,Pn,P belongs to P0. The representation is a compositional bijection {DΔD:P}/=α  {MΔΣM can:dedP}/=α.

Proof of Theorem 61.10 — Adequacy for implicational natural deduction

Proof. Soundness. Induct on ΔD:P. An assumption maps to the corresponding LF variable. For implication introduction, the induction hypothesis gives Δ,u:dedPΣD can:dedQ. LF abstraction and the constant supI give a canonical object of dedPQ. For implication elimination, the two induction hypotheses have the two argument families of supE, so canonical application gives the represented conclusion.

Completeness. Define an inverse by induction on the canonical-object derivation. Since dedP is atomic, the object is atomic. Its head is one of:

  1. a context variable ui, whose declared family forces P=Pi and which maps to the assumption derivation;

  2. supI fully applied to two proposition codes and a canonical function; eta-longness forces the function to be λ(u:dedR).M0. Apply the induction hypothesis to M0 in the extended context and return implication introduction;

  3. supE fully applied to two proposition codes and two canonical derivation objects. Apply the two induction hypotheses and return implication elimination.

The arguments headed by prop decode uniquely by lemma 61.6. No other signature constant returns a family headed by ded. Hence the inverse is total and excludes exotic inhabitants.

Inverse equations. Induction on object derivations shows that decoding D returns D. Induction on canonical forms shows that encoding the decoded form returns the same variable or the same fully applied constructor; in the introduction case the two LF lambdas agree up to alpha-equivalence. Thus the maps are inverse. Compositionality is lemma 61.9. ◻

Exercise 61.2

★★☆ Specialize to D=u and to an assumption derivation e:P independent of u. Encode the object proof reduction supE(supI(u.u),e):P cut e:P. Show that the left encoding is already LF beta-normal: supE has no rewrite rule for a supI argument. State why the two encodings are not LF beta-eta equal. Then give the general equation D[E/u]=βηD[E/u] from lemma 61.9 and explain why neither calculation proves completeness. (Three quarters of a page.)

A binding-rich typed encoding

The first encoding represented a binder in derivations. The next encoding represents the binder in the syntax of the simply typed lambda calculus itself. Object types, terms, and typing judgments are A,B::=ιAB,t,u::=xλ(x:A).ttu,Γstt:A. The subscript st keeps this object typing judgment separate from LF typing.

Definition 61.11 — Intrinsically typed STLC signature

The LF signature Σst contains tp:type,i:tp,arr:tptptp,tm:tptype,lam:A:tpB:tp(tmAtmB)tm(arrAB),app:A:tpB:tptm(arrAB)tmAtmB. The type representation and term representation are ι:=i,AB:=arrAB,x:=x,λ(x:A).t:=lamAB(λ(x:tmA).t),tu:=appABtu. The object typing derivation determines the implicit types A,B in the last two clauses. The object context x1:A1,,xn:An maps to x1:tmA1,,xn:tmAn.

Example 61.12 — Binding and substitution

The object term λ(f:AB).λ(x:A).fx maps to B(f):=lamAB(λ(x:tmA).appABfx). Consequently its complete representation is lamABAB(λ(f:tmAB).B(f)). Substituting a represented function g into the HOAS body contracts one LF redex and produces the representation of λ(x:A).gx. By contrast, the encoding of the object application has head app and does not contract: lam and app are representation constants, not LF computation rules. No definition of capture-avoiding substitution is added to the signature.

Lemma 61.13 — STLC representation commutes with substitution

If Γ,x:A,Δstt:B and Γstu:A, then t[u/x]=βt[u/x].

Proof of Lemma 61.13 — STLC representation commutes with substitution

Proof. Induct on t. Variables give the two defining substitution equations. Application uses both induction hypotheses. For t=λ(y:C).t0, choose the alpha-equivalent representative with yFV(u){x}dom(Γ,Δ). Then the object substitution enters the body, and the LF substitution enters the body of the representing LF lambda. The induction hypothesis gives t0[u/x]=βt0[u/x]. These are all term constructors. ◻

Lemma 61.14 — STLC type-code inversion

Let Γ contain only declarations x:tmA. If ΓΣstT can:tp, then there is a unique STLC type A with T=A.

Proof of Lemma 61.14 — STLC type-code inversion

Proof. The canonical object is atomic. No variable in Γ has family tp, and the only signature heads returning that family are i and the fully applied arr. Decode the first as ι; in the second case recursively decode the two canonical tp arguments. Constructor disjointness and the two induction hypotheses give uniqueness. ◻

Lemma 61.15 — No exotic STLC inhabitants

Let Γ contain only declarations x:tmA. Every canonical LF object ΓΣstM can:tmB has exactly one of the forms x,appABM1M2,lamA1B1(λ(x:tmA1).M0), with canonical subobjects at the displayed families. In the second form, A is an arbitrary object type and the result type is B. The third form occurs only when B=A1B1; its body has family tmB1 in the context extended by x:tmA1.

Proof of Lemma 61.15 — No exotic STLC inhabitants

Proof. The target family is atomic, so M is atomic. Its head is a variable or a constant. A variable in Γ has the first form. Among the constants of Σst, only app and lam return a family headed by tm. Eta-longness forces each constant to be fully applied. Their explicit tp arguments are uniquely object-type codes by lemma 61.14. In the lam case, the final argument has function type, so canonicality forces an LF lambda and extends the context by x:tmA1. There is no constant that eliminates, compares, or branches on an object of family tmA1; hence these cases exhaust the canonical inhabitants. ◻

Theorem 61.16 — Adequacy for intrinsically typed STLC

For every object context Γ and object type A, representation is a compositional bijection {tΓstt:A}/=α  {MΓΣstM can:tmA}/=α.

Proof of Theorem 61.16 — Adequacy for intrinsically typed STLC

Proof. Soundness is rule induction on object typing. The variable case selects its LF context declaration. The application and abstraction cases apply the corresponding LF signature constant; in the abstraction case, the induction hypothesis is used under the LF variable that represents the object binder.

For completeness, recursively invert the three forms of lemma 61.15. A variable maps to the object variable with the type recorded by its context declaration. An app head yields canonical subobjects at tm(arrAB) and tmA; decode them and apply the object application rule. An lam head forces the target type to be A1B1 and yields a canonical body at tmB1 in the context extended by x:tmA1; decode the body and apply the object abstraction rule.

Induction on object terms proves decode-after-encode is the identity. Induction on canonical objects proves encode-after-decode is the identity, with alpha-equivalence in the binder case. The preceding substitution lemma supplies compositionality. ◻

The phrase intrinsically typed names what the index of tm does: ill-typed object terms have no representation. This encoding is not an adequacy theorem for raw terms plus a separate typing relation. Such an encoding would use an unindexed family term and another family of:termtptype, and would require a new canonical-forms proof.

Kernel checking and adequacy are distinct obligations; the following extension witnesses the gap. Extend Σst by inspect:A:tptmAtmA. For canonical M:tmA, the fully applied object inspectAM is well typed and canonical. Its head is not a variable, lam, or app, so the STLC inverse has no case for it. Kernel checking and canonicalization survive this extension while adequacy does not.

Exercise 61.3

★★☆ Reconstruct the inspect counterexample above from the extended signature. Exhibit the new canonical form that appears in lemma 61.15. Explain why no STLC constructor decodes it and therefore why sound typing of the LF constant does not preserve adequacy. (Half a page.)

Proposition 61.17 — Framework trust and adequacy obligations

For an LF signature Σ and representation , the following obligations are logically distinct:

  1. the LF kernel checks ΓΣM:A;

  2. canonicalization computes the unique M of theorem 61.4;

  3. an adequacy theorem proves that M decodes to exactly one object of the represented judgment;

  4. any framework program that searches for or transforms M preserves the checked LF family.

None of the first, second, or fourth obligations implies the third.

Proof of Proposition 61.17 — Framework trust and adequacy obligations

Proof. The first two concern only LF. The third quantifies over a separate object syntax and its representation inverse, data absent from LF typing. The preceding inspect extension satisfies LF typing and canonicalization while making the completeness inverse partial; equivalently, the encoding is no longer surjective onto canonical LF inhabitants. The fourth concerns an algorithm external to the kernel; even a type-preserving identity transformation proves no inverse equation for . ◻

User-defined rewriting is therefore excluded from the LF kernel in this chapter. A rewrite-enhanced framework needs confluence, subject reduction, normalization or a terminating conversion procedure, and a fresh adequacy proof for each encoding. A PTS classification or the LF theorem above cannot donate those results.

A controlled comparison of binding representations

Fix the same intrinsically typed STLC as above. Every representation in this section must support three operations and the same theorem: renameρ(t),t[σ],Γstt:AΔstt[σ]:A whenever σ maps each variable declared by Γ to a term of the same type in Δ. Comparing this one obligation prevents a convenient notation from being mistaken for a transferred theorem.

De Bruijn substitutions

Let a typed context be a list with its newest declaration at the left. A typed de Bruijn variable i:AΓ is generated by X0:A(A,Γ)Zeroi:AΓi+1:A(B,Γ)Succ. Terms are variables i, applications, and annotated abstractions λAt. A de Bruijn renaming ρ:ΓΔ maps each derivation i:AΓ to a derivation ρ(i):AΔ. A de Bruijn substitution σ:ΓΔ maps it to a term Δstσ(i):A.

Under a binder, define ρ(0):=0,ρ(i+1):=ρ(i)+1,σ(0):=0,σ(i+1):=rename(+1)(σ(i)). The actions are structural: renameρ(i):=ρ(i),renameρ(tu):=renameρ(t)renameρ(u),renameρ(λAt):=λArenameρ(t), and substitution is defined separately by i[σ]:=σ(i),(tu)[σ]:=t[σ]u[σ],(λAt)[σ]:=λA(t[σ]).

Lemma 61.18 — Weakening commutes with de Bruijn substitution

If Δsts:B and τ:ΔΞ, then rename(+1)(s[τ])=(rename(+1)s)[τ]. Both sides are terms of type B in the context obtained by adding the same newest declaration to Ξ.

Proof of Lemma 61.18 — Weakening commutes with de Bruijn substitution

Proof. Prove the statement after k enclosing binders simultaneously for every k0. The weakening renaming is then the k-fold lift (+1)k, and the substitution is τk. Induct on s. For a variable i, the two sides are rename(+1)k(τk(i))and((+1)k(i))[τ(k+1)]; the zero and successor clauses of lifting make them identical. Application uses the two induction hypotheses. Under λA, the defining clauses lift both the weakening and the substitution once, so the body equation is the induction hypothesis at k+1. Taking k=0 gives the displayed law. ◻

Given substitutions σ:ΓΔ and τ:ΔΞ, define their pointwise composite by (σ;τ)(i):=σ(i)[τ]. This is initially a raw term map. The typing clause of the following lemma shows that each component has the type declared for i in Γ.

Lemma 61.19 — De Bruijn substitution laws

For typed substitutions of the displayed domains, t[id]=t,t[σ][τ]=t[σ;τ],renameρ(t)=t[varρ]. If Γstt:A and σ:ΓΔ, then Δstt[σ]:A.

Proof of Lemma 61.19 — De Bruijn substitution laws

Proof. The three equations are inductions on t. In the abstraction case, the defining equations give (σ;τ)=σ;τ,id=id; the identity equation is proved on variables by the zero and successor cases. For composition, the zero case is reflexive, while the successor case is (σ;τ)(i+1)=(rename(+1)σ(i))[τ]=lemma61.18rename(+1)(σ(i)[τ])=(σ;τ)(i+1). For typing, induct on Γstt:A. The variable case is the defining property of σ. Application uses both induction hypotheses. Abstraction uses σ:(A,Γ)(A,Δ); its zero branch is typed by Zero, and its successor branch is typed by weakening the corresponding component of σ. The object abstraction rule then gives the conclusion. ◻

Cofinite locally nameless syntax

A locally nameless term uses atoms x for free variables and indices i for bound variables: t::=xituλAt. Opening t at depth k with u, written t[ku], replaces k by u and increments the depth under a binder. Put tu:=t[0u]. A term is locally closed when every bound index is captured by an enclosing lambda.

The cofinite abstraction rule is LfinAxL. Γ,x:Alntx:BΓlnλAt:ABLNLam. Free-variable substitution t[u/x] ignores bound indices and enters λAt without lifting.

Lemma 61.20 — Opening commutes with free substitution

If u is locally closed and yFV(u){x}, then (ty)[u/x]=(t[u/x])y.

Proof of Lemma 61.20 — Opening commutes with free substitution

Proof. Generalize opening from depth 0 to an arbitrary depth k and induct on t. At the free atom x, the right side opens the inserted u and local closure gives u[ky]=u; a different free atom uses yx. A bound index follows from the definition of opening. Application uses both induction hypotheses, and the abstraction case applies the body hypothesis at depth k+1. ◻

Lemma 61.21 — Locally nameless substitution

If Γ,x:A,Δlnt:B and Γlnu:A, then Γ,Δlnt[u/x]:B. Consequently, free renaming and preservation are admissible for locally nameless typing.

Proof of Lemma 61.21 — Locally nameless substitution

Proof. Induct on the typing derivation. Variables and application use the corresponding substitution clauses. In the abstraction case, let L be the finite exclusion set in LN-Lam. Put L:=LFV(u)dom(Γ,Δ){x}. Let yL be arbitrary. The cofinite premise at this particular y gives Γ,x:A,Δ,y:Clnt0y:D. The induction hypothesis therefore gives Γ,Δ,y:Cln(t0y)[u/x]:D. Rule induction on the typing derivation of u shows that u is locally closed. Because yFV(u){x}, lemma 61.20 gives (t0y)[u/x]=(t0[u/x])y. Since y was arbitrary outside L, rule LN-Lam closes the abstraction with exclusion set L. Free renaming is substitution by a variable; preservation is the statement proved above. ◻

For a finite typed simultaneous substitution σ=(u1/x1,,un/xn):ΓΔ, where the xi are pairwise distinct, define t[σ] in one structural traversal: the atom xi maps to ui, every other atom and every bound index is unchanged, application is componentwise, and the operation enters a lambda without lifting. If, in addition, xiFV(uj) whenever ij, this direct operation is equal to the right-to-left iteration t[σ]:=((t[un/xn]))[u1/x1]. Every component uj of a typed substitution is locally closed. Induction on the typing derivation, using the variable lookup clause and the same cofinite binder argument as lemma 61.21, proves Δlnt[σ]:BwheneverΓlnt:B and Δlnσ:Γ. Induction on t, using the same enlarged cofinite set in the abstraction case, gives t[id]=t and t[σ][τ]=t[σ;τ]. Thus the simultaneous operation invoked in theorem 61.26 is derived rather than silently identified with the one-variable lemma.

The finite set in LN-Lam is load-bearing. An existential rule choosing one atom would not support the fresh choice required after substituting a term whose free variables include that atom.

Parametric higher-order syntax

For a family V indexed by object types, define the intrinsically indexed constructors var:V(A)Term(V,A),app:Term(V,AB)×Term(V,A)Term(V,B),abs:(V(A)Term(V,B))Term(V,AB). For a typed object context, put Env(V,):=1,Env(V,A,Γ):=V(A)×Env(V,Γ),PTerm(Γ,A):=V:TySetEnv(V,Γ)Term(V,A). An inhabitant of PTerm(Γ,A) is an open parametric higher-order abstract syntax term, abbreviated PHOAS term; closed terms use Γ=. The quantifier alone is not the promised invariant. We state the relational condition that the host language must enforce or the representation theorem must assume.

Definition 61.22 — PHOAS parametricity

Let RAV(A)×W(A) be a relation family. Its Kripke lifting TermRel(R) relates variables by R, applications componentwise, and abstractions f,g when, for every extension RR, v:V(A),w:W(A).RA(v,w)TermRel(R)B(f(v),g(w)). Extend R componentwise to EnvRel(R)Env(V,Γ)×Env(W,Γ). A polymorphic p:PTerm(Γ,A) is parametric when EnvRel(R)(γ,δ)TermRel(R)A(p(V)(γ),p(W)(δ)) for every V,W,R,γ,δ.

Example 61.23 — A non-parametric PHOAS inhabitant

Fix an object type A. Suppose a non-parametric host supplies a polymorphic test inspectV:V(AA)Bool that, at the constant family V0(X):=N, tests whether its argument is zero. In the one-variable context Γ=(AA) define pbad(V)(v,):={var(v)inspectV(v)=true,abs(λw.var(w))inspectV(v)=false. Both branches have type Term(V,AA), so pbad:PTerm(Γ,AA). Let RAA relate 0 to 1 at V0 and take the corresponding related environments. The first output has head var and the second has head abs, so no TermRel(R) derivation relates them. Thus pbad violates definition 61.22. The universal quantifier excludes this inhabitant only in a host whose polymorphism enforces the relational condition; a host with the displayed inspection primitive does not.

Theorem 61.24 — PHOAS renaming, substitution, and preservation

Let p:PTerm(Γ,B) satisfy definition 61.22. Let a typed renaming ρ:ΓΔ provide, naturally in V, maps ρV:Env(V,Δ)Env(V,Γ) that select a variable of the same object type for every declaration of Γ. Let a typed substitution θ:ΓTΔ provide θV:Env(V,Δ)Env(Term(V,),Γ) naturally and parametrically. Then:

  1. For every relation family R and every pair of EnvRel(R)-related environments γ,δ, the outputs p(V)(γ) and p(W)(δ) are TermRel(R)-related. In particular, their head constructors are equal.

  2. renaming and simultaneous substitution have the type-preserving maps renameρ:PTerm(Γ,B)PTerm(Δ,B),Substθ:PTerm(Γ,B)PTerm(Δ,B).

  3. both operations preserve parametricity, and identity and composition hold extensionally at every V and environment.

The equality in clause 3 is extensional HOAS equality: two abstraction fields are equal when they return equal bodies at every host argument. A host that expresses this equality by its intensional identity type must supply function extensionality.

Proof of Theorem 61.24 — PHOAS renaming, substitution, and preservation

Proof. The obstruction in the proof is that Term(V,) has no general functorial action on maps of variable families: V occurs to the left of an arrow in abs. Substitution instead instantiates the polymorphic term at the nested family Term(V,) and uses graph-relation free theorems to justify flattening, identity, and composition.

Suppose a polymorphic test selected different constructors for two related environment entries v0,v1. Relate them by RA and use equality on constructor tags as the result relation. Parametricity requires the two outputs to have related, hence equal, tags, a contradiction. The same argument applies beneath a binder because definition 61.22 quantifies over extensions R that add the fresh pair of bound variables.

Renaming is precomposition: renameρ(p)(V)(δ):=p(V)(ρV(δ)). Every component selected by ρ has its declaration’s object type, so the result retains index B. Naturality of ρ and parametricity of p prove parametricity of the result.

For substitution, define the structural flattening function flat(var(t)):=t,flat(app(t,u)):=app(flat(t),flat(u)),flat(abs(f)):=abs(λv.flat(f(var(v)))) from Term(Term(V,),B) to Term(V,B). The abstraction clause injects the target bound variable before passing it to the source body; every recursive call is on a constructor subterm. Put Substθ(p)(V)(δ):=flat(p(Term(V,))(θV(δ))). Every constructor in the definition carries its object type, so induction on the input proves that flat preserves the index B. Relational induction shows that flattening preserves related terms; together with parametricity of p and θ, this proves parametricity of the result.

The identity law uses the free theorem, not merely an induction on an arbitrary host function. Let mapVarV:Env(V,Γ)Env(Term(V,),Γ) inject every environment component. Instantiate parametricity of p with the graph of var. It gives TermRel(graph(var))(p(V)(γ),p(Term(V,))(mapVarV(γ))). Induction on that relational derivation proves that flattening its right-hand term returns its left-hand term; its abstraction case uses the extensional HOAS equality stated in the theorem. This is Substid(p)(V)(γ)=p(V)(γ).

For composition, let θ:ΓTΔ and ϕ:ΔTΞ. Define their composite componentwise by (θ;ϕ)V(ξ):=flatEnv(θTerm(V,)(ϕV(ξ))), where flatEnv applies flat to every environment component. By the componentwise definition of EnvRel, the graph of flat relates this nested environment to its flatEnv image. Naturality and parametricity of θ and ϕ separately prove that the displayed composite is a natural, parametric substitution family. Instantiate parametricity of p with that graph relation. It relates p(Term(Term(V,),))(θTerm(V,)(ϕV(ξ))) to p(Term(V,))((θ;ϕ)V(ξ)). Induction on this TermRel(graph(flat)) derivation proves that flattening the left twice equals flattening the right once; the Kripke-related abstraction case again uses extensional HOAS equality. Unfolding the two substitution definitions therefore gives the exact pointwise equation Substϕ(Substθ(p))(V)(ξ)=Substθ;ϕ(p)(V)(ξ). This argument uses no functorial action of Term on variable-family maps; the negative occurrence of the variable family in abs supplies no such general action. Renaming identity and composition are ordinary precomposition. Thus free-variable renaming uses ρ, while beta-substitution uses the environment substitution θ; neither operation requires an ill-typed closed “variable injection.” ◻

Contextual syntax

A contextual object [ΓA] is an intrinsically typed term paired with the exact context of variables it may use. A contextual substitution σ:[ΔΓ] assigns to each declaration x:A of Γ an object [ΔA]. Its action is defined by variable lookup, componentwise application, and lifting beneath a lambda. Thus contextual syntax packages the domain and codomain that were external indices on de Bruijn substitutions.

Proposition 61.25 — Contextual substitution

If t:[ΓA] and σ:[ΔΓ], then t[σ]:[ΔA]. Identity and composition satisfy t[idΓ]=t,t[σ][τ]=t[σ;τ]. For a fixed ordered typed context Γ and a chosen naming of its declarations, erasing names induces a bijection from contextual objects modulo alpha-renaming to typed de Bruijn terms. This bijection commutes with substitution.

Proof of Proposition 61.25 — Contextual substitution

Proof. Induct on t. Variable lookup has the family declared by σ. Application uses both induction hypotheses. Under an abstraction, extend σ by the new variable and weaken its old components; this is exactly the lifted substitution σ of lemma 61.19. The identity and composition equations follow by the same induction, with the abstraction case reduced to the corresponding lifted equations. Relative to the fixed ordered Γ, erasure maps the ith contextual variable to index i; its inverse selects the chosen name of the ith declaration. Changing that naming changes the inverse only by simultaneous alpha-renaming. Induction on terms proves both inverse equations modulo alpha and the substitution square. ◻

Theorem 61.26 — One obligation, four proofs

De Bruijn, cofinite locally nameless, parametric HOAS, and contextual syntax each admit capture-avoiding substitution and prove preservation for the fixed intrinsically typed STLC.

Proof of Theorem 61.26 — One obligation, four proofs

Proof. The four preservation results are lemma 61.19, lemma 61.21, theorem 61.24, proposition 61.25. Each cited statement constructs substitution in its representation and proves the displayed typing implication of this section. ◻

Remark 61.27 — The four proof invariants

The theorem asserts four preservation results, not implications between their hypotheses. The following concrete boundaries prevent such a transfer.

  1. The de Bruijn term λA0 and its lifted substitution contain no atom, so they do not determine the atom required by LN-Lam.

  2. The locally nameless term λA0 is one fixed syntax tree, not a function quantified over variable families; it therefore supplies no instance of definition 61.22.

  3. The inhabitant pbad of example 61.23 has the required PHOAS type but fails the relation-family premise.

  4. The PHOAS body abs(λx.var(x)) is closed at every V and therefore does not record whether it was packaged as [AA] or weakened to [B,AA]; a contextual object distinguishes those two carriers.

Exercise 61.4

★★★ Represent (λ(x:A).λ(y:B).x)u in all four representations. Perform the beta-substitution, naming the lifted substitution, fresh atom, relation family, or contextual substitution used in the binder case. Verify that all four results translate to the de Bruijn term λB(rename(+1)u). (Two pages.)

The boundary of an encoding theorem

Proposition 61.28 — Adequacy is signature-relative

The adequacy theorems theorem 61.10, theorem 61.16 hold only for their displayed LF signatures, represented contexts, canonical forms, and object judgments. They do not imply adequacy for:

  1. an extension of either signature by a constant returning dedP or tmA;

  2. raw STLC syntax with a separate typing family;

  3. a framework with user-defined conversion rules;

  4. nominal unification, contextual modal systems other than the displayed calculus, or a proof assistant’s full kernel.

Proof of Proposition 61.28 — Adequacy is signature-relative

Proof. Completeness in both adequacy proofs inverts the finite list of possible heads in the displayed signature. An added result constant creates a new canonical head. Raw syntax changes the represented family and the inverse relation. User-defined conversion changes canonical forms. Nominal unification, different contextual calculi, and proof-assistant kernels have constructors and conversion rules absent from the two signatures. Thus every listed extension invalidates at least one premise of the canonical-head inversion. ◻

Generalized framework signatures can package declarations and proof-assistant kernels can check their elaborated terms. The trust obligation is kernel soundness for those declarations. The adequacy obligation remains a separate pair of inverse maps for each encoded judgment. Neither a successful kernel check nor a framework feature list proves those maps inverse.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 61.5, then complete exercise 61.8.

Exercise 61.5

★★☆ Choose a canonical LF object of family ded(PQ)(PQ) and run the inverse from theorem 61.10 constructor by constructor. Re-encode the object derivation and prove that the result is alpha-equivalent to the chosen LF object.

Exercise 61.6

★★☆ Add one constant k:tm(arrii) to Σst. Show that LF type checking and canonicalization still succeed for k, but the completeness inverse of theorem 61.16 has no case. State the weakest extension of the object syntax that would restore a bijection and prove the new head case.

Exercise 61.7

★★★ For the two-binder redex of exercise 61.4, remove one load-bearing binder condition in turn: de Bruijn lifting, the cofinite exclusion set, PHOAS relation extension, and the exact contextual domain. For each representation, give a concrete substitution whose result is ill-scoped, captured, non-parametric, or assigned the wrong contextual type. Then restore the condition and compute the corrected result. (Two pages.)

Exercise 61.8

★★★ Practical project.lf-adequacy-checker Implement in Agda or Kappa a canonical-form checker and decoder for the intrinsically typed LF signature Σst. Preserve the invariant that every accepted atomic object has a head declared in the input LF signature and is fully applied to canonical arguments. The program must decode the represented identity and application terms, reject an underapplied lam, and report an exotic result head after the test signature adds inspect. Print either the decoded STLC term and type or the first failed canonical-head condition. Those four outcomes are the decidable acceptance test.

Sources. The LF calculus and its metatheory originate with Harper, Honsell, and Plotkin [HHP93]. Canonical representations, judgments-as-types, and conversion are developed in [Pfe01]. The cofinite abstraction rule and its substitution proof follow [Cha12]. The PHOAS relation follows the representation discipline of [Chl08]; the theorem printed here states explicitly the parametricity hypothesis that the source identifies as essential.

Search the book

Type to search the local edition.