Lectures onType Theory
Chapter 119
Chapter 119Optional

Coercive Subtyping and Coherent Cast Insertion

Prerequisites. Direct starred prerequisites: none. No later core chapter depends on this route.

Fix a well-formed context Γ. Let A,B,C,D be types in Γ, with primitive declared casts Γa:AB, Γb:AC, Γc:BD, and Γd:CD. After weakening the four casts to Γ,x:A, subsumption derives that x may be used at D, but it suppresses which program is run. Cast insertion can produce either c(a(x)) or d(b(x)). If those terms are observably different, source typing is ambiguous. The calculus must make equality of parallel coercion paths a hypothesis, not a hope.

Convention 119.1 — Coercive-subtyping source card

The framework fixes coercive subtyping together with completion and conservativity. A type theory T is specified in a logical framework. A finite set R of basic subtyping rule schemata derives judgments ΓA<cB:Type, read “c coerces A to B”. Three systems are distinguished: T[R]0 adds only the subtyping judgments to T; T[R] additionally allows coercive application and coercive definition; T[R]0K adds subkinding. The source coherence conditions on R are the three clauses of Luo’s definition, stated in T[R]0:

  1. if ΓA<cB:Type then ΓA type, ΓB type, and Γc:AB;

  2. ΓA<cA:Type is derivable for no Γ, A, c;

  3. if ΓA<cB:Type and ΓA<cB:Type, then Γcc:AB.

The subtyping judgment of T[R]0 is closed under judgmental equality of its two type arguments, so a coercion between A and B is also a coercion between any A and B for which ΓAA type and ΓBB type. All completion and conservativity results below use exactly that signature.

Coercion paths

Clause 2 of convention 119.1 forbids a declared edge from a type to itself, while a usable calculus still needs an identity cast and composition of casts. The two demands are reconciled by separating the declared edges from the paths generated over them.

Definition 119.2 — Context-indexed coercion signature and paths

Write σ:ΓΔ for a well-typed simultaneous substitution that assigns, in Γ, a term to every variable declared in Δ. A coercion signature consists of a dependent type theory T and a finite set R of declared-edge schemata r=(Δ;A,B,c), where Δ is a well-formed context, ΔA type, ΔB type, and Δc:AB. Every well-typed instance must satisfy Luo’s irreflexivity condition: for every σ:ΓΔ, the judgment ΓA[σ]B[σ] type is not derivable. The instance of r along σ is written r[σ], and its declared cast is c[σ].

The context-indexed coercion-path judgment pCoeΓ(A,B) is generated by the following rules.

Γ ctxΓA type
idACoeΓ(A,A)
Coe-Id
r=(Δ;A0,B0,c)Rσ:ΓΔ
r[σ]CoeΓ(A0[σ],B0[σ])
Coe-Edge
pCoeΓ(A,B)qCoeΓ(B,C)
qpCoeΓ(A,C)
Coe-Comp
pCoeΓ(A,B)ΓAA typeΓBB type
pCoeΓ(A,B)
Coe-Conv

Each path pCoeΓ(A,B) names a target term progp in Γ, its cast program, defined by progidA:=λx.x,progr[σ]:=c[σ]for r=(Δ;A0,B0,c),progqp:=λx.progq(progpx). The endpoint-conversion clause changes only the typing derivation of progp, not its term. Two paths pCoeΓ(A,B) and qCoeΓ(A,B) are parallel in Γ when ΓAA type and ΓBB type. The signature is path coherent when, for every well-formed Γ, all four well-formed endpoints A,A,B,B, and every such pair of parallel paths, Γprogpprogq:AB after converting the type of progq along the two endpoint equalities.

For example, if a and c are the declared instances from the opening diamond, the left route is a path in the same context: aCoeΓ(A,B)cCoeΓ(B,D)caCoeΓ(A,D)CoeComp. Its cast program is λx.c(a(x)). The derivation does not remain well scoped after replacing Γ by another context; it must first be transported by a substitution.

The context index is load bearing. It records the free variables on which a declared cast may depend and makes weakening under a binder a mathematical operation rather than an implicit change of scope.

Lemma 119.3 — Structural stability of coercion paths

Let pCoeΔ(A,B).

  1. Δprogp:AB.

  2. For every σ:ΓΔ, there is a path p[σ]CoeΓ(A[σ],B[σ]) satisfying Γprogp[σ]progp[σ]:A[σ]B[σ].

  3. If Δ,z:C is a well-formed extension, then the weakening of p belongs to CoeΔ,z:C(A,B), where A, B, and progp are weakened along the projection Δ,z:CΔ.

  4. If Δ and Δ have the same variables in the same order and corresponding declaration types are judgmentally equal after each preceding context conversion, then the identity variable list gives a well-typed substitution ι:ΔΔ. Hence p[ι]CoeΔ(A[ι],B[ι]), and its cast program is judgmentally equal to progp[ι].

Proof of Lemma 119.3 — Structural stability of coercion paths

Proof. Prove clauses 1 and 2 simultaneously by induction on the derivation of pCoeΔ(A,B). For Coe-Id, target lambda introduction gives the required function, and substitution produces idA[σ]. For a declared Coe-Edge instance r[τ], where r=(Ξ;A0,B0,c), the schema typing judgment substituted along τ:ΔΞ gives Δc[τ]:A0[τ]B0[τ]. Substitution along σ produces the declared instance r[τσ], and the substitution-composition law identifies its cast with c[τ][σ].

For Coe-Comp, the two typing induction hypotheses and target application and lambda introduction type the composite cast. The two substitution induction hypotheses, followed by lambda and application congruence, identify (qp)[σ] with q[σ]p[σ]. In the Coe-Conv case, substitution stability of judgmental equality converts the substituted source and target endpoints; the cast term is unchanged. These are all four generators.

Clause 3 is clause 2 at the projection substitution. For clause 4, pointwise context conversion makes the identity variable list well typed; apply clause 2 to that substitution. No free variable is introduced or discarded without one of these substitutions. ◻

Path coherence is the path-closed form of clause 3 of convention 119.1; the requirement that a declared edge join distinct types is clause 2. Clause 1 is the well-formedness demanded of every instance of a schema in R. The identity and composite paths are generated rather than declared, so admitting them does not violate clause 2: they are the coercions that T[R] derives, not the basic rules whose irreflexivity the source requires.

The opening diamond violates path coherence unless the signature contains a derivation Γλx.c(a(x))λx.d(b(x)):AD. Declaring only unique declared edges does not help: the two composites use different edges and remain parallel.

Two judgments and where a cast may be inserted

The source language is subsumptive: it records that a term may be used at a supertype and says nothing about which program is run.

Definition 119.4 — Subsumptive source judgment

The judgment Γe:A has the ordinary typing rules of T together with

Γe:ApCoeΓ(A,B)
Γe:B
Sub

No term of the target theory occurs in this judgment.

The shortest route to a program annotates each Sub step with its path and reads off progp. That recipe is not coherent, and the failure is not the diamond of the chapter opening.

Remark 119.5 — Unrestricted insertion is incoherent

Take a target theory with judgmental function η, closed base types A, A, B, and closed declared-edge schemata ι:AA,σ:A×B2,τ:A×B2, with progι:=λx.a for a fixed a:A, progσ:=λz.tt, and progτ:=λz.ff. In every well-formed context Δ, each set CoeΔ(X,Y) generated by instances of these three closed schemata is either empty or contains the single edge together with its composites with identities. The target βη-laws identify those identity composites with the single edge, so the signature is path coherent in every context. Weakening the three paths to Γ=x:A,y:B, the source term (x,y) has two annotated derivations of Γ(x,y):2:

  • form the pair at A×B, then use Sub along σ, yielding progσ(x,y)tt;

  • use Sub along ι on the first component, form the pair at A×B, then use Sub along τ, yielding progτ(progιx,y)ff.

Since ttff fails, path coherence alone does not make annotated subsumption derivation independent. The two final paths are σCoeΓ(A×B,2) and τCoeΓ(A×B,2), which are not parallel: the interior Sub step changed the type at which the pair rule concluded. Coherence therefore requires controlling where a cast may be inserted, not only which casts are equal.

The control is bidirectional. Insertion is confined to the one place where a type is supplied from outside, so every structural rule concludes at the type its own premises determine.

Definition 119.6 — Coercion-inserting elaboration

The elaboration judgments are synthesis ΓeAt and checking ΓeAt, where t is a term of T.

(x:A)Γ
ΓxAx
Coe-Var
ΓeAt
Γ(e:A)At
Coe-Ann
Γe1x:ABt1Γe2At2
Γe1e2B[t2/x]t1t2
Coe-App
Γe1At1Γe2Bt2
Γ(e1,e2)A×B(t1,t2)
Coe-Pair
Γ,x:AeBt
Γλx.ex:ABλx.t
Coe-Lam
ΓeAtpCoeΓ(A,B)
ΓeBprogpt
Coe-Insert

Coe-Insert is the only rule that consumes a coercion path, and it is the only rule that changes mode. Every other rule concludes at a type built from the types its premises produce.

Reading the two judgments back into definition 119.4 erases the target terms and replaces each Coe-Insert by Sub, so every elaboration derivation has an underlying subsumptive derivation.

Theorem 119.7 — Elaboration type preservation

If ΓeAt or ΓeAt, then Γt:A in T.

Proof of Theorem 119.7 — Elaboration type preservation

Proof. Simultaneous rule induction on the two derivations. Coe-Var is the target variable rule and Coe-Ann is its premise unchanged. For Coe-App the two induction hypotheses give Γt1:x:AB and Γt2:A, and target application derives Γt1t2:B[t2/x]. For Coe-Pair the two induction hypotheses and product introduction derive Γ(t1,t2):A×B. For Coe-Lam the induction hypothesis in the extended context and Π-introduction derive Γλx.t:x:AB. For Coe-Insert the induction hypothesis gives Γt:A, while clause 1 of lemma 119.3 gives Γprogp:AB; target application derives Γprogpt:B. These are all six rules. ◻

Lemma 119.8 — Synthesis is determined

Assume the coercion signature is path coherent, target definitional equality is a congruence stable under substitution, and dependent products are injective in every well-formed context: from Γx:A1B1x:A2B2 type one may derive ΓA1A2 type and, after context conversion, Γ,x:A1B1B2 type. Let Γ1 and Γ2 have the same variables in the same order, with corresponding declaration types judgmentally equal after each preceding context conversion. For every such pair of contexts:

  1. if Γ1eA1t1 and Γ2eA2t2, then, after converting the second judgment into Γ1, one has Γ1A1A2 type and Γ1t1t2:A1;

  2. if Γ1eB1t1, Γ2eB2t2, and Γ1B1B2 type after context conversion, then Γ1t1t2:B1.

Proof of Lemma 119.8 — Synthesis is determined

Proof. The strengthened context and expected-type clauses are the induction invariant needed under lambdas and when two applications synthesize judgmentally equal, but not syntactically identical, domains. Proceed by simultaneous induction on the two pairs of derivations. In the calculations below, judgments from Γ2 are compared in Γ1 by the ordinary target context-conversion rule. A coercion path from Γ2 is transported along the identity variable substitution supplied by clause 4 of lemma 119.3; therefore its cast program is compared in Γ1, not in an unmentioned ambient context. In each synthesis case the source term’s outermost constructor selects the rule, so both derivations end in the same rule of definition 119.6.

Coe-Var: both derivations read corresponding declarations for the same variable. Pointwise context equality gives Γ1A1A2 type, and t1=t2=x.

Coe-Ann: both conclude at the written annotation, so A1=A2, and the strengthened clause 2 for the premises, with reflexivity of the written type, gives Γ1t1t2:A1.

Coe-Pair: clause 1 for the two component premises gives Γ1A1A2 type, Γ1B1B2 type and componentwise equality of the elaborated components. Product congruence gives Γ1A1×B1A2×B2 type and pair congruence closes the terms.

Coe-App: clause 1 for the function premise gives Γ1x:A1B1x:A2B2 type and Γ1t1ft2f:x:A1B1. Injectivity of Π gives Γ1A1A2 type and Γ1,x:A1B1B2 type, so the two argument premises check at judgmentally equal types and the strengthened clause 2 gives Γ1t1at2a:A1. Stability of equality under substitution gives Γ1B1[t1a/x]B2[t2a/x] type, and application congruence closes the terms.

For clause 2 there is exactly one checking rule per source constructor. If the source is λx.e, both derivations use Coe-Lam, so ΓiBix:AiCi type for i{1,2}. Dependent-Π injectivity applied to the assumed Γ1B1B2 type gives Γ1A1A2 type and, after context conversion, Γ1,x:A1C1C2 type. The body contexts Γ1,x:A1 and Γ2,x:A2 are pointwise judgmentally equal, so the strengthened induction hypothesis applies directly to the body derivations. Lambda congruence gives equality at B1.

Every other source term uses Coe-Insert. The two derivations synthesize A1 and A2 with paths pCoeΓ1(A1,B1) and qCoeΓ2(A2,B2). Transport clause 4 of lemma 119.3 gives a path qιCoeΓ1(A2[ι],B2[ι]), whose cast is the context conversion of progq. Clause 1 gives Γ1A1A2 type and Γ1u1u2:A1; the expected-type premise gives Γ1B1B2 type, where the right-hand endpoints abbreviate their substitutions along ι. Hence p and qι are parallel in the exact sense of definition 119.2. Path coherence gives Γ1progpprogqι:A1B1 after endpoint conversion. Application congruence and conversion of the second result from B2 to B1 give Γ1progpu1progqιu2:B1. The cast-program equality in clause 4 of lemma 119.3 identifies the last term with the context conversion of progqu2, which is the required conclusion. ◻

Theorem 119.9 — Coherence of insertion

Under the hypotheses of lemma 119.8, if ΓeAt1 and ΓeAt2, then Γt1t2:A. Consequently a source program elaborated at a fixed expected type has one target program up to definitional equality.

Proof of Theorem 119.9 — Coherence of insertion

Proof. This is clause 2 of lemma 119.8 with B1=B2=A and reflexivity of target conversion. ◻

Three hypotheses carry the theorem, and none is decorative. Deleting path coherence refutes it at the opening diamond: with A=1, D=2, one composite constantly tt and the other constantly ff, both composites are parallel paths whose casts are not equal. Deleting the mode restriction refutes it at remark 119.5, where the signature is path coherent. Deleting injectivity of Π breaks the Coe-App case, because the two argument premises would no longer be known to check at judgmentally equal types.

Exercise 119.1

★☆☆ List the two elaboration derivations for the opening diamond at the expected type D. State the exact function equality needed to apply theorem 119.9; equality only at one chosen input is not enough.

Exercise 119.2

★★☆ In remark 119.5, attempt both elaborations in definition 119.6 with expected type 2. Name the rule whose premise fails in the second attempt, and give the annotation the source programmer must write to obtain the ff program.

Variance and dependency

Function space is contravariant in its domain and covariant in its codomain. Fix types A1,A2 in Γ, a type B1(y) in Γ,y:A1, and a type B2(x) in Γ,x:A2. Given pCoeΓ(A2,A1) and a path qxCoeΓ,x:A2(B1(progpx),B2(x)), define arr(p,q)(f):=λx.progqx(f(progpx)). The dependency in the source of qx is forced by typing. Replacing it by B1(x) is ill formed unless p is the identity or an additional transport is provided.

Records are the same calculation in declaration order, with both fields covariant. For types A,A in Γ, take the source record type Rec1:=(n:N, v:VecAn) and the target Rec2:=(m:N, w:VecAm). A fieldwise cast is given by a path pCoeΓ(N,N) for the first field and a path qnCoeΓ,n:N(VecAn,VecA(progpn)) for the second. Here p is weakened from Γ to Γ,n:N by lemma 119.3. The record cast is rec(p,q)(r):=(progp(r.n), progqr.n(r.v)). The index progpn in the target type of qn is the record analogue of the occurrence B1(progpx) above: the later field’s type must be read at the already coerced earlier field.

Two instances show why that index cannot be simplified to n. With p=idN and a declared edge α:AA, the family qn:=mapα with mapα:VecAnVecAn applying α to every entry gives rec(id,q)(r)=(r.n, mapα(r.v)), and the length is unchanged. With progp:=suc, the same qn is ill typed: mapα(r.v) has type VecA(r.n) where the second field of the result is required at VecA(suc(r.n)). No path built from α repairs this, because every such path preserves the length index; the second field must be supplied a cast that is already indexed by progpn.

Proposition 119.10 — Coherence of the dependent function cast

Let Γ ctx, ΓA1 type, ΓA2 type, Γ,y:A1B1(y) type, and Γ,x:A2B2(x) type. Suppose p,pCoeΓ(A2,A1) satisfy Γprogpprogp:A2A1, and suppose qxCoeΓ,x:A2(B1(progpx),B2(x)),qxCoeΓ,x:A2(B1(progpx),B2(x)). Assume the fiber cast programs satisfy Γ,x:A2progqxprogqx:B1(progpx)B2(x) after conversion along the first equality. Then Γarr(p,q)arr(p,q):y:A1B1(y)x:A2B2(x).

Proof of Proposition 119.10 — Coherence of the dependent function cast

Proof. Fix f:y:A1B1(y) and x:A2. Function congruence gives Γ,x:A2f(progpx)f(progpx):B1(progpx). After the stipulated fiber conversion, congruence with the equal fiber casts gives equality of the two bodies at B2(x). Lambda congruence first closes over x, then over f, yielding equality of the casts. ◻

Completion and conservativity

Adding every composite coercion as a declared edge is called coercion completion. A tempting argument says that uniqueness of the declared edges already makes every inserted coercion unique. Soloviev and Luo identify the missing step in Sections 4–5.3: coercion completion acts on derivations, and the premises reconstructed from two derivations of the same judgment need not have literally identical contexts or kinds. The completion proof must first show that the relevant presupposed judgments match up to equality in T, and then use coherence to preserve that matching while rebuilding each rule. Their Section 4 also leaves open a simple general syntactic condition on arbitrary rules of T and R that would make this reconstruction work. The theorem below therefore applies to their displayed logical- framework systems and coherent rule sets; it does not license arbitrary completion.

Theorem 119.11 — Completion and conservativity at the source signature

For the systems T[R], T[R]0, and T[R]0K, if the basic rule set R satisfies the three coherence conditions of convention 119.1, then the coercion completion transformation Θ is defined on every derivation of T[R], and any two derivations of the same judgment have T-equal images; on a derivation in T[R]0K it leaves the final judgment unchanged. Consequently, a judgment of T[R] that is not of subtyping or subkinding form is derivable in T exactly under the unchanged-completion condition proved below.

Proof of Theorem 119.11 — Completion and conservativity at the source signature

Proof. We reconstruct the two inductions. For a derivation d, write pre(d) for the derivations of all presuppositions of its conclusion: context validity; kind formation for a displayed kind; typing of both sides of an equality; and, for A<cB, the typings of A, B, and c:AB. Structural induction on d proves the following extraction invariant:

If Θ(d) is defined, then Θ is defined on every member of pre(d), and the image extracted from d is T-equal to the image obtained from the separately extracted presupposition.

For a variable or primitive formation rule the extracted derivation is the corresponding premise or an identity derivation. Weakening and context replacement use equality of the prefix contexts. For product formation, abstraction, application, and substitution, apply the induction hypotheses to the domain and codomain presuppositions and rebuild the same logical-framework rule after context replacement. Equality formation uses the two endpoint extractions; equality typing uses those plus the type extraction. A basic subtyping rule uses coherence clause 1 to obtain the three required typings. Coercive application extracts the function typing, argument typing, coercion typing, and the equalities that align their independently reconstructed contexts and kinds; after those replacements, ordinary application derives f(c(k)). Coercive definition has the same four obligations under its binder. These are all rule families of T[R], so the extraction invariant holds.

We next prove, by induction on the sum of the heights of d and d, that whenever they derive the same validity, kind-formation, or term-typing judgment and both images are defined, their images are T-equal. If the last rules agree, apply the induction hypothesis to corresponding premises and congruence to the rebuilt conclusion. The possible unequal last rules are structural rules, equality conversion, and coercive versus ordinary application or two coercive applications. Structural and conversion cases are reduced to smaller derivations by the extraction invariant and transitivity of T-equality. An ordinary application compared with a coercive one would give, after the smaller comparisons, both equality and a proper subtyping judgment between the same endpoint kinds; coherence clauses 1 and 2 exclude that case. If both are coercive applications, the smaller comparisons identify their source and target kinds. Coherence clause 3 identifies their coercions, and application congruence identifies the completed conclusions. The coercive-definition comparison uses that argument under lambda congruence. Thus all three classes of presupposed judgment have derivation-independent images.

Totality of Θ now follows by structural induction on d. Images of the premises exist by induction. To rebuild the last rule, the extraction invariant reduces each missing context or kind equality to a comparison of two presupposed judgments; the preceding derivation-independence lemma supplies that equality. The coercive-application and coercive-definition cases use the same comparison and coherence clause 3 for their coercion. Hence every reconstruction obligation is discharged. A derivation already in T[R]0K contains no coercive application or definition to replace, and the deterministic context-alignment convention inserts no change at its conclusion; therefore its final judgment is unchanged.

For conservativity, let d derive a non-subtyping, non-subkinding judgment J in T[R]. If J has an unchanged completion, then Θ(d) is literally a T-derivation of J. Conversely, a T-derivation contains no coercive rule and is fixed by Θ. For a subtyping or subkinding conclusion the identical argument lands in T[R]0 or T[R]0K, respectively. Derivation independence shows that the existence of one unchanged derivation is equivalent to every derivation being unchanged. This proves both assertions.

The proof uses the complete source rule classification and its three coherence clauses. It proves no path coherence for a different target calculus, licenses no insertion outside definition 119.6, and supplies no generic map law. ◻

The restriction matters exactly at the opening diamond. If its composites are not equal, completion records two different coercions from A to D, clause 3 of convention 119.1 fails, and insertion is not derivation independent.

Sources

The coercive-subtyping framework follows Luo [Luo99]. Completion and conservativity follow Soloviev and Luo [SL02], especially Theorems 5.8 and 6.1 on printed p. 23.

Suggested first pass.

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

Exercise 119.3

★★☆ Derive the function cast for (A1B1)<:(A2B2). Give the domain and codomain paths, mark the contravariant occurrence, and type every intermediate application.

Exercise 119.4

★★★ For Rec1 and Rec2 above, write both fieldwise casts in full and type every projection and application. Then take progp:=suc, display the type at which the second field is required, and give the ill-typed term obtained by reusing mapα there.

Exercise 119.5

★★☆ In Soloviev and Luo’s paper, read Sections 4–5.4. For the ordinary dependent application rule, state the two equality obligations that may fail after completing its premises separately. Then name the lemma and the coherence consequence used to restore those obligations, and state the generality question that the source leaves open. The answer must distinguish coherence of basic coercions from equality of arbitrary completed premises.

Exercise 119.6

★★★ Practical project.coherent-coercion-path-checker Implement in Agda or Kappa a finite coercion graph whose edges carry symbolic cast programs. Enumerate the simple paths between a source and a target, normalize each composite by associativity and by deletion of identities, and reject a source/target pair whose normalized programs differ. Maintain the invariant that every accepted source/target pair has exactly one normalized program. On the inputs chain, coherent-diamond, and bad-diamond, print unique, coherent, and rejected: parallel-casts respectively. A mutation that compares only path length must fail the last oracle. The program checks a finite graph; it does not decide judgmental equality in a dependent target theory.

Search the book

Type to search the local edition.