Lectures onType Theory
Chapter 131
Chapter 131Optional

Explicit Equality Evidence and System FC

Prerequisites. Direct starred prerequisites: none. System F, higher kinds, existential abstraction and the dependent equality rules supply the mathematical prerequisites; no compiler or Haskell knowledge is assumed. No later core chapter depends on this route.

A source language may declare a data type whose constructors constrain the type index they return. The standard example is an expression type with three constructors, Zero:Exp Int,Succ:Exp IntExp Int,Pair:Exp bExp cExp (b,c), together with an evaluator whose declared type is eval:Exp aa. In the Zero branch the evaluator must return the literal 0, and 0 has type Int, not a.

Try to type that branch in System F extended by algebraic data types with existential components and higher kinds — write FA for that language. The branch body must be given the result type a, so the only possible leaf is 0:IntΓΓ0:aFAConst. The premise of FA-Const is 0:aΓ. The displayed tree is therefore not a derivation. No other rule repairs it: in FA type equality is syntactic identity of types, the type variable a is not the type constant Int, and there is no rule whose conclusion changes the type of a term. The branch cannot be typed at all.

The information the branch is missing is not a value. It is the fact, established by the pattern match, that in this branch a and Int may be interchanged. System FC(X) records that fact as a first-class object of the type language, and every place where the fact is used is marked in the term. This chapter freezes the calculus FC(X) of Sulzmann, Chakravarty, Peyton Jones and Donnelly (2007), whose syntax, typing rules, kinding rules and operational semantics are its Figures 1–4, audits its rules for regularity, proves substitution, preservation, progress, consistency and erasure at their exact hypotheses, and elaborates the GADT source fragment into it.

The frozen FC(X) signature

Definition 131.1 — Symbol classes and raw syntax

Fix disjoint countable sets of type variables a,b,c,co, term variables x,f, coercion constants C, value type constructors T, type functions Sn of declared arity n, and data constructors K. Sorts, kinds, types-and-coercions, terms, patterns and environments are generated by δ::=TYCO,κ,ι::=κ1κ2σ1σ2,d::=aT,g::=coC,φ,ρ,σ,τ,υ,γ::=aCTφ1φ2Snφna:κ.φsym γγ1γ2γ@φleft γright γγ1γ2leftc γrightc γγ1γ2,u::=xK,e::=uΛa:κ.eeφλx:σ.ee1e2let x:σ=e1 in e2case e1 of pe2eγ,p::=K b:κ x:σ,Γ::=ϵΓ,u:σΓ,d:κΓ,g:κΓ,Sn:κ. The metavariables ρ,σ,τ,υ are used where only a regular type can stand and γ where only a coercion can stand; the distinction is enforced by the kinding rules, not by the grammar. Write κσ for _:κ.σ when the bound variable does not occur in σ, and φn for a sequence φ1φn. A type function symbol Sn occurs only in the saturated form Snφn.

The language has no type-level lambda, so equality of regular types is syntactic identity up to renaming of bound variables. Every departure from that identity must be carried by an explicit coercion.

Definition 131.2 — Kind well-formedness

The judgment Γkκ:δ is generated by

Γk:TY
Star
Γkκ1:TYΓkκ2:TY
Γkκ1κ2:TY
FunK
ΓTYσ1:κΓTYσ2:κ
Γkσ1σ2:CO
EqTy
Γkγ1:COΓkγ2:CO
Γkγ1γ2:CO
EqCo

EqTy requires the two sides of an equality kind to have one kind. That single premise is what makes the coercion judgment below a statement about comparable objects, and section 131.4 shows that four of the printed coercion rules fail to preserve it.

Definition 131.3 — Type kinding

d:κΓΓkκ:TY
ΓTYd:κ
TyVar
ΓTYσ1:κ1κ2ΓTYσ2:κ1
ΓTYσ1σ2:κ2
TyApp
Sn:κnιΓΓTYσi:κi (1in)
ΓTYSnσn:ι
TySCon
Γ,a:κTYσ:Γkκ:δafv(Γ)
ΓTYa:κ.σ:
TyAll

Two consequences are needed constantly and are proved once.

Lemma 131.4 — Kind uniqueness

If ΓTYσ:κ and ΓTYσ:κ then κ=κ.

Proof of Lemma 131.4 — Kind uniqueness

Proof. The four rules of definition 131.3 have pairwise distinct subjects: an atom, an application σ1σ2, a saturated type-function application, and a quantified type. So the last rule of both derivations is the same, and we induct on σ. For TyVar the kind is read off the unique binding of d in Γ. For TySCon it is the result kind ι of the unique declaration of Sn. For TyAll it is . For TyApp the induction hypothesis gives one kind κ1κ2 for σ1; an arrow kind determines κ2. ◻

Lemma 131.5 — Weakening

Let Γ extend Γ by bindings whose subjects do not occur in Γ. Then each of Γkκ:δ, ΓTYσ:κ, ΓCOγ:κ and Γee:σ implies the same judgment under Γ.

Proof of Lemma 131.5 — Weakening

Proof. Every rule of definition 131.2, definition 131.3, definition 131.6, definition 131.7 either has no environment premise, or looks a binding up in Γ, or extends the environment by one fresh binding. Lookups survive the extension because Γ is a sub-environment of Γ; binder rules survive after renaming the bound variable away from the finitely many subjects of Γ. A structural induction on the derivation, replacing Γ by Γ at each node, gives the four statements simultaneously. ◻

Definition 131.6 — Coercion kinding

d:κΓΓkκ:TY
ΓCOd:dd
CoRefl
g:στΓ
ΓCOg:στ
CoVar
ΓCOγ:στ
ΓCOsym γ:τσ
Sym
ΓCOγ1:σ1σ2ΓCOγ2:σ2σ3
ΓCOγ1γ2:σ1σ3
Trans
Γ,a:κCOγ:στΓkκ:TYafv(Γ)
ΓCOa:κ.γ:a:κ.σa:κ.τ
CoAllT
ΓCOγ:a:κ.σb:κ.τΓTYυ:κ
ΓCOγ@υ:σ[υ/a]τ[υ/b]
CoInstT
ΓCOγ1:σ1τ1ΓCOγ2:σ2τ2ΓTYσ1σ2:κ
ΓCOγ1γ2:σ1σ2τ1τ2
Comp
ΓCOγi:σiτi (1in)ΓTYSnσn:κ
ΓCOSnγn:SnσnSnτn
SComp
ΓCOγ:σ1σ2τ1τ2
ΓCOleft γ:σ1τ1
Left
ΓCOγ:σ1σ2τ1τ2
ΓCOright γ:σ2τ2
Right
ΓCOγ:κ1κ2ΓCOγ:σ1σ2Γkκ1:CO
ΓCOγγ:κ1σ1κ2σ2
CompC
ΓCOγ:κ1σ1κ2σ2
ΓCOleftc γ:κ1κ2
LeftC
ΓCOγ:κ1σ1κ2σ2
ΓCOrightc γ:σ1σ2
RightC
ΓCOγ1:σ1τ1ΓCOγ2:σ2τ2
ΓCOγ1γ2:(σ1σ2)(τ1τ2)
EqCoerce
ΓCOγ1:κΓCOγ2:κκ
ΓCOγ1γ2:κ
CastC

The rule named EqCoerce is the rule printed as () in the source.

Two features of definition 131.6 deserve to be noticed before any of it is used. First, CoRefl witnesses reflexivity of an atom by the atom itself: the coercion Int has kind IntInt. Lemma 131.11 extends that to every well-kinded type. Second, Left and Right apply to ordinary application φ1φ2 and not to Snφn; the saturation requirement of definition 131.1 is what keeps the two apart, and remark 131.14 shows what decomposing a type function would cost.

Definition 131.7 — Term typing and patterns

u:σΓ
Γeu:σ
Var
ΓTYσx:Γ,x:σxee:σ
Γeλx:σx.e:σxσ
Abs
Γee1:σ2σ1Γee2:σ2
Γee1e2:σ1
App
Γee1:σ1Γ,x:σ1ee2:σ2
Γelet x:σ1=e1 in e2:σ2
Let
Γ,a:κee:σΓkκ:δafv(Γ)
ΓeΛa:κ.e:a:κ.σ
AbsT
Γee:a:κ.σΓkκ:δΓδφ:κ
Γeeφ:σ[φ/a]
AppT
Γee:σΓCOγ:στ
Γeeγ:τ
Cast
Γee:σΓppjej:στ (1jm)
Γecase e of pe:τ
Case
K:a:κ.b:ι.σTaΓθ=[υ/a]Γ,b:θ(ι),x:θ(σ)ee:τ
ΓpK b:θ(ι) x:θ(σ)e:Tυτ
Alt

In AppT the metavariable δ is TY when κ is a type kind and CO when κ is an equality kind, so one rule covers both type application and coercion application. A binder b:ι of a data constructor is existential when ι is a type kind and a coercion binder when ι is an equality kind.

Definition 131.8 — Declarations and programs

A program is pgm::=decl;e with decl::=data T:κ where K:a:κ.b:ι.σTatype Sn:κnιaxiom C:σ1σ2. Declaration checking uses

Γkκ:TYΓ,T:κTYσK: for each K
Γ(data T:κ where K:σK):(T:κ,K:σK)
Data
Γkκi:TYΓkι:TY
Γ(type Sn:κnι):(Sn:κnι)
Type
Γkκ:CO
Γ(axiom C:κ):(C:κ)
Coerce

A program decl;e is well typed at σ when the declarations extend the initial environment Γ0 to Γ and Γee:σ. An environment produced this way is a top-level environment: it binds only T, Sn, K and C, and no type variable, coercion variable or term variable.

Remark 131.9 — One corrected premise in Data

The printed declaration rule checks ΓTYσ: in Γ. A constructor type mentions the type constructor it builds — Zero:a:.co:aInt.Exp a mentions Exp — so that premise is never derivable for a recursive declaration. Data above checks the constructor types in Γ,T:κ. Nothing else in the calculus changes, and every later proof uses the corrected rule.

Example 131.10 — The evaluator that F_A could not type

Declare data Exp: where Zero:a:.(aInt)Exp a,Succ:a:.(aInt)Exp IntExp a. The Zero branch that had no FA derivation now has one. In the alternative Zero (co:aInt) the pattern binds co, so CoVar gives ΓCOco:aInt, Sym gives ΓCOsym co:Inta, and Cast gives Γe0:Intco:aIntΓΓCOco:aIntCoVarΓCOsym co:IntaSymΓe0sym co:aCast. The whole evaluator is eval=Λa:.λe:Exp a.case e of{Zero (co:aInt)0sym coSucc (co:aInt) (e:Exp Int)(eval Int e+1)sym co. The evidence appears twice in the term and nowhere in the run-time behaviour: theorem 131.35 shows that both casts vanish.

Exercise 131.1

★☆☆ Write the derivation of Γe(eval Int e+1)sym co:a in the Succ branch of example 131.10, naming the rule at every node. Then say which single premise fails if sym is deleted.

Constructing and decomposing evidence

Reflexivity is stated for atoms only. The first thing to prove is that it holds for every well-kinded type, and that the witness is the type itself. The coercion language contains no separate reflexivity constant, so the two clauses below must be proved together: the quantifier case κσ needs reflexivity of the equality kind κ, which CoAllT cannot supply.

Lemma 131.11 — Reflexivity

  1. If ΓTYφ:κ then ΓCOφ:φφ.

  2. If Γkκ:CO then ΓCOκ:κκ.

Proof of Lemma 131.11 — Reflexivity

Proof. Simultaneous induction on the two derivations.

Clause 1. TyVar: CoRefl applies to the same premises and gives ΓCOd:dd. TyApp φ=σ1σ2: the induction hypothesis gives ΓCOσi:σiσi, and Comp, whose third premise is the kinding of σ1σ2 already in hand, gives ΓCOσ1σ2:σ1σ2σ1σ2. TySCon: the same with SComp. TyAll φ=a:κ.σ: split on the sort of κ. If Γkκ:TY, the induction hypothesis in Γ,a:κ and CoAllT give ΓCOa:κ.σ:a:κ.σa:κ.σ. If Γkκ:CO, then afv(σ) and φ=κσ; clause 2 gives ΓCOκ:κκ, clause 1 for σ gives ΓCOσ:σσ, and CompC gives ΓCOκσ:κσκσ.

Clause 2. EqTy κ=σ1σ2: clause 1 gives ΓCOσi:σiσi, and EqCoerce gives ΓCOσ1σ2:(σ1σ2)(σ1σ2). EqCo: the same with the induction hypothesis of clause 2 on the two component kinds. ◻

The reflexivity witness is the mechanism behind the whole calculus: to lift a coercion γ through a type φ, replace one CoRefl leaf of the reflexivity derivation of φ by γ.

Theorem 131.12 — Lifting

Let afv(Γ) and ΓCOγ:σ1σ2 with ΓTYσ1:κ and ΓTYσ2:κ.

  1. If Γ,a:κTYφ:κ then ΓCOφ[γ/a]:φ[σ1/a]φ[σ2/a].

  2. If Γ,a:κkκ:CO then ΓCOκ[γ/a]:κ[σ1/a]κ[σ2/a].

Proof of Theorem 131.12 — Lifting

Proof. The obstruction is that γ has one fixed kind while φ may place a under applications, type functions and quantifiers. The decisive move is to run the reflexivity proof of lemma 131.11 on φ and change exactly the leaves at which a is reached; the invariant is that after processing a subterm ψ, the constructed coercion has kind ψ[σ1/a]ψ[σ2/a]. Formally we induct simultaneously on the two kinding derivations.

Clause 1. TyVar with d=a: then κ=κ, φ[γ/a]=γ and φ[σi/a]=σi, so the conclusion is the hypothesis on γ. TyVar with da: the substitution leaves d unchanged and lemma 131.11 gives ΓCOd:dd. TyApp φ=φ1φ2: the induction hypothesis gives ΓCOφj[γ/a]:φj[σ1/a]φj[σ2/a] for j=1,2. Lemma 131.18 gives ΓTY(φ1φ2)[σ1/a]:κ, which is the third premise of Comp; that rule then gives the required coercion, because substitution commutes with application. TySCon: identical with SComp. TyAll φ=b:ι.ψ, where b is chosen outside fv(γ)fv(σ1)fv(σ2)fv(Γ){a}. If Γkι:TY, weaken γ to Γ,b:ι by lemma 131.5, apply the induction hypothesis to ψ there, and conclude by CoAllT. If Γkι:CO, then φ=ιψ with bfv(ψ); clause 2 applied to ι and clause 1 applied to ψ feed CompC.

Clause 2. EqTy κ=υ1υ2: clause 1 gives coercions υj[γ/a] of kind υj[σ1/a]υj[σ2/a], and EqCoerce assembles υ1[γ/a]υ2[γ/a] at exactly the required kind. EqCo: the same with clause 2 on the two component kinds. ◻

Clause 2 is what makes the constructor-push rule of section 131.5 type-correct, and it is the reason the coercion language contains the forms γ1γ2 and γ1γ2 at all.

Example 131.13 — Lifting through a data type and a quantifier

Let ΓCOγ:σ1σ2 with both sides of kind , and let Tree:. Taking φ=Tree a, theorem 131.12 produces the coercion Tree γ of kind Tree σ1Tree σ2: the head is the reflexivity witness Tree supplied by CoRefl, and the argument is γ. Taking φ=b:.aInt gives ΓCOb:.γInt:b:.σ1Intb:.σ2Int, where γInt abbreviates ()γInt, an instance of Comp whose head is the reflexivity witness ().

Decomposition runs the other way. From ΓCOγ:Tree σ1Tree σ2, Right extracts ΓCOright γ:σ1σ2. Decomposition is the step that the elaboration of section 131.8 cannot do without, and it is also the step whose soundness depends on a property of the type language rather than of the coercion language.

Remark 131.14 — Why type functions are saturated

Suppose S1 were allowed to appear unapplied, so that S1σ counted as an ordinary application. With the two axioms C1:S1IntBool and C2:S1BoolBool the coercion C1sym C2 has kind S1IntS1Bool, and Right would produce a coercion of kind IntBool. Both axioms are individually harmless — they say only that a type function takes the value Bool at two arguments — so the failure is caused by decomposition, not by the axioms. Keeping Snφn syntactically distinct from φ1φ2 removes the Right step: the premise of Right does not match S1IntS1Bool. The same restriction stops a partial type-function application from instantiating a type variable, so a variable of higher kind ranges over injective constructors only.

Exercise 131.2

★☆☆ Let ΓCOγ:σ1σ2. Build, node by node, the coercion of kind (σ1Int)Tree σ1(σ2Int)Tree σ2 that theorem 131.12 constructs for φ=(aInt)Tree a, naming the rule used at each node. (Half a page; both clauses of the theorem occur.)

Exercise 131.3

★★☆ Give a well-formed top-level environment containing two axioms about one type function S1 of kind from which a coercion of kind IntBool is derivable once Right is allowed to decompose S1-applications. Then check that your environment still satisfies definition 131.29 when Right keeps its printed restriction.

Type functions and axioms

A type function is declared without an interpretation and given one, case by case, by equality axioms. This is the second extension of System F, and it is the one that adds power beyond GADTs.

Example 131.15 — A type family and the cast it licenses

Declare type Elem1: and axiom ebs:Elem1BitSetChar. The declaration Coerce is applicable because EqTy holds: both sides have kind . Now compute the type of a character used where an element of a BitSet is expected. With Γec:Char, ΓCOebs:Elem1BitSetChar=CoVaraxiom,ΓCOsym ebs:CharElem1BitSet=Symfrom the line above,Γecsym ebs:Elem1BitSet=Castfrom the two lines above. A parametric axiom quantifies on each side separately, axiom el:(e:.Elem1[e])(e:.e), and is used at a type by CoInstT: ΓCOel@Int:Elem1[Int]Int. A single quantifier over an equality, a:.(Elem1[a]a), is not a type of definition 131.1 at all: TyAll requires its body to have kind , and an equality kind is not .

Exercise 131.4

★☆☆ A source declaration newtype T=MkT (TT) is translated by a single axiom. State it, check that Coerce accepts it, and write the two casts that convert between T and TT.

Regularity, and four missing side conditions

Everything proved so far used the coercion rules in the forward direction. The metatheory needs them in the backward direction as well: from ΓCOγ:κ one wants to know that κ is a well-formed equality kind, so that the two types it relates are comparable. That property fails for the printed rules.

Proposition 131.16 — Failure of regularity for the printed decomposition rules

There is an environment Γ and a coercion γ with ΓCOγ:στ derivable from definition 131.6 for which Γkστ:CO is not derivable.

Proof of Proposition 131.16 — Failure of regularity for the printed decomposition rules

Proof. Let F: and G:() be value type constructors and Maybe:. Put Γ=F:, G:(), Maybe:, Int:, g:FIntGMaybe. The binding of g is well formed: TyApp gives ΓTYFInt: and ΓTYGMaybe:, so EqTy applies with the common kind . By CoVar and then Left, ΓCOleft g:FG. But ΓkFG:CO would need, by EqTy, one kind for both F and G; by lemma 131.4 their kinds are and (), which are distinct. So the conclusion of Left is not a well-formed equality kind. ◻

Nothing about consistency repairs this. Consistency is required only of the top-level environment, whereas g is a coercion variable of the kind a pattern match introduces, and the calculus deliberately admits locally false equality assumptions. The same computation with EqCoerce in place of Left produces an ill-formed kind from two perfectly good coercions whose left-hand sides have different kinds.

Convention 131.17 — The audited rule set

Write FCr(X) for the calculus of definition 131.1, definition 131.2, definition 131.3, definition 131.6, definition 131.7, definition 131.8 with four rules given extra premises and one rule given one:

ΓCOγ:σ1σ2τ1τ2ΓTYσ2:κ1ΓTYτ2:κ1
ΓCOleft γ:σ1τ1
Left^r
ΓCOγ:σ1σ2τ1τ2ΓTYσ2:κ1ΓTYτ2:κ1
ΓCOright γ:σ2τ2
Right^r
ΓCOγ1:σ1τ1ΓCOγ2:σ2τ2Γkσ1σ2:CO
ΓCOγ1γ2:(σ1σ2)(τ1τ2)
EqCoerce^r
Γ,a:κCOγ:στΓ,a:κTYσ:Γkκ:TYafv(Γ)
ΓCOa:κ.γ:a:κ.σa:κ.τ
CoAllT^r
ΓCOγ:κ1κ2ΓCOγ:σ1σ2ΓTYσ1:Γkκ1:CO
ΓCOγγ:κ1σ1κ2σ2
CompC^r

Every other rule is unchanged. All statements from here on are about FCr(X). Lemma 131.11, Theorem 131.12 were proved without using Left, Right, EqCoerce, and their uses of CoAllT and CompC discharge the extra premises from the reflexivity hypotheses, so both results hold verbatim in FCr(X).

Lemma 131.18 — Kinding substitution

Let afv(Γ) and ΓTYυ:κ. If Γ,a:κ,ΓTYσ:κ then Γ,Γ[υ/a]TYσ[υ/a]:κ[υ/a], and correspondingly for k and CO.

Proof of Lemma 131.18 — Kinding substitution

Proof. Induction on the derivation. TyVar at a returns υ, which has kind κ=κ[υ/a] because afv(κ); TyVar at another atom is unchanged, except that its kind is substituted, and lemma 131.5 moves the judgment into the substituted environment. TyApp, TySCon and TyAll commute with substitution, the last after renaming its bound variable away from fv(υ){a}. The kinding and coercion cases are identical rule by rule; CoInstT additionally uses the equality (σ[υ/a])[φ/b]=(σ[φ/b])[υ/a], valid because bfv(υ). ◻

Theorem 131.19 — Coercion regularity

Let Γ be well formed, meaning that each binding d:κ or Sn:κ satisfies Γ<kκ:TY, each binding g:κ satisfies Γ<kκ:CO and each binding u:σ satisfies Γ<TYσ:, where Γ< is the prefix preceding it. If ΓCOγ:κ in FCr(X) then Γkκ:CO.

Proof of Theorem 131.19 — Coercion regularity

Proof. Induction on the coercion derivation. CoRefl: TyVar gives ΓTYd:κ from the same premises, and EqTy gives Γkdd:CO. CoVar: the kind is the one recorded in Γ, which is well formed by hypothesis and by lemma 131.5. Sym and Trans: EqTy is symmetric and transitive in the required sense, since by lemma 131.4 the shared kind is determined.

CoAllTr: the added premise gives Γ,a:κTYσ:; the induction hypothesis gives Γ,a:κkστ:CO, whose EqTy premise and lemma 131.4 give Γ,a:κTYτ:. TyAll then kinds both a:κ.σ and a:κ.τ at , and EqTy concludes. CoInstT: by the induction hypothesis both quantified types have kind ; inverting TyAll and applying lemma 131.18 kinds σ[υ/a] and τ[υ/b] at .

Comp: the third premise gives ΓTYσ1σ2:κ; inverting TyApp gives ΓTYσ1:κ1κ and ΓTYσ2:κ1. The induction hypotheses and lemma 131.4 give τ1 the kind κ1κ and τ2 the kind κ1, so TyApp kinds τ1τ2 at κ. SComp: the same argument at the declared argument kinds of Sn.

Leftr: by the induction hypothesis σ1σ2 and τ1τ2 have one kind κ. The added premises give σ2 and τ2 the same kind κ1; inverting TyApp twice and using lemma 131.4 gives both σ1 and τ1 the kind κ1κ, and EqTy concludes. Rightr: the added premises are the conclusion.

CompCr: the induction hypothesis on γ gives Γkκ1κ2:CO, which by EqCo makes both κ1 and κ2 equality kinds; the added premise and the induction hypothesis on γ give σ1,σ2 kind . TyAll then forms both κiσi at . LeftC and RightC: invert TyAll on the two sides supplied by the induction hypothesis; the first gives Γkκi:CO and hence EqCo, the second gives ΓTYσi: and hence EqTy.

EqCoercer: the added premise is the left half of the conclusion; the right half follows because τi shares the kind of σi by the induction hypothesis, so EqTy applies to τ1,τ2. CastC: the induction hypothesis on γ2 gives Γkκκ:CO; its EqCo premise gives Γkκ:CO. ◻

Remark 131.20

Regularity is not decoration. Cast concludes Γeeγ:τ with no kinding premise on τ; without theorem 131.19 a well-typed term could have an ill-kinded type, and the canonical-forms argument of section 131.6 — which reads the head constructor off the type of a value — would have nothing to read.

Exercise 131.5

★★☆ For each of Leftr and EqCoercer, delete the added premise and exhibit a well-formed environment in which the resulting rule derives a coercion whose kind is not well formed. For CoAllTr, say why deleting the added premise leaves a statement that is still well formed but no longer implies regularity.

Dynamics

Definition 131.21 — Values, cvalues, contexts

v::=Λa:κ.eλx:σ.eK σ φ eplain values,cv::=vvγcvalues,E::=[]EeEφEγcase E of peevaluation contexts, where K σ φ e is a saturated constructor application. The step relation is closed under contexts: EeEe whenever ee.

A cvalue is a plain value under at most one cast. A term such as trueγ with ΓCOγ:BoolS1υ cannot be reduced further without changing its type, so the finished forms of evaluation must include it.

Definition 131.22 — Reduction

TBeta(Λa:κ.e)φe[φ/a]Beta(λx:σ.e)ee[e/x]LetBetalet x:σ=e1 in e2e2[e1/x]CaseRedcase (K σ φ e) of K b xee[φ/b,e/x]Comb(vγ1)γ2vγ1γ2 together with four rules that move a cast off the head of an elimination. With ΓCOγ:a:κ.σ1b:κ.σ2 and Γkκ:TY, TPush((Λa:κ.e)γ)φ(Λa:κ.eγ@a)φ. With ΓCOγ:κσκσ, γ1=sym (leftc γ) and γ2=rightc γ, CPush((Λa:κ.e)γ)φ(Λa:κ.(e[aγ1/a])γ2)φ. With ΓCOγ:σ1σ2τ1τ2, γ1=sym (right (left γ)) and γ2=right γ, Push((λx:σ1.e)γ)e(λy:τ1.(e[yγ1/x])γ2)e. Finally, for K:an:κ.b:ι.ρTan and ΓCOγ:TσTτ, KPushcase ((K σ φ e)γ) of prhscase (K τ φ e) of prhs, where γi=right(leftleftniγ), θ=[γi/ai],[φi/bi], φi=φiθ(ιi) when ιi is an equality kind and φi=φi otherwise, and ei=eiθ(ρi).

Remark 131.23 — Two completions of the printed figure

The printed operational semantics has no rule for let and no evaluation context that enters one, so let x:Int=0 in x is a closed well-typed term that is neither a cvalue nor reducible; the progress statement is false for the printed term grammar. LetBeta above repairs this by the call-by-name rule; its preservation case is lemma 131.25, and a reader who prefers to delete let from definition 131.1 obtains the same theory. Second, TPush and CPush have the same left-hand side, because κσ is notation for _:κ.σ. They do not overlap: TPush requires Γkκ:CO to fail and CPush requires it to hold, and definition 131.2 assigns each kind exactly one sort.

Example 131.24 — Running the evaluator

Take the declarations of example 131.10 and evaluate eval Int (Zero Int Int). The second Int is the coercion argument: by CoRefl it has kind IntInt, which is what Zero’s second quantifier demands after instantiating a by Int. eval Int (Zero Int Int)TBeta(λe:Exp Int.case e of )(Zero Int Int)Betacase (Zero Int Int) of CaseRed0sym Int. The result is a cvalue, not a plain value, and its type is Int: the substitution replaced the pattern variable co by the reflexivity coercion Int. Theorem 131.35 shows that the erasure of the whole computation is the two-step reduction of eval()Zero to 0.

Lemma 131.25 — Term substitution

If Γ,x:σ,Γee:τ and Γee:σ then Γ,Γee[e/x]:τ.

Proof of Lemma 131.25 — Term substitution

Proof. Induction on the typing derivation. Var at x returns e, whose typing is the hypothesis after lemma 131.5; Var at another subject is unchanged. Abs, AbsT, Let and Alt rename their bound subject away from fv(e){x} and apply the induction hypothesis to the body. App, AppT, Cast and Case apply it to each premise; no type or coercion changes, because x is a term variable. ◻

Lemma 131.26 — Type and coercion substitution in terms

Let afv(Γ), Γδφ:κ, and Γ,a:κ,Γee:σ. Then Γ,Γ[φ/a]ee[φ/a]:σ[φ/a].

Proof of Lemma 131.26 — Type and coercion substitution in terms

Proof. Induction on the typing derivation, using lemma 131.18 at every kinding and coercion premise. The only case that is not a direct rewriting is Cast: its coercion premise Γ,a:κ,ΓCOγ:στ becomes Γ,Γ[φ/a]COγ[φ/a]:σ[φ/a]τ[φ/a] by the coercion clause of lemma 131.18, which is the premise Cast needs at the substituted types. ◻

Theorem 131.27 — Preservation

Let Γ be a well-formed top-level environment, Γee:σ and ee. Then Γee:σ.

Proof of Theorem 131.27 — Preservation

Proof. It suffices to treat the eight contraction rules, since each evaluation context of definition 131.21 is an elimination whose typing rule uses the type of the hole as a premise, so replacing the hole by a term of the same type reuses the same derivation.

TBeta. Inverting AppT and AbsT gives Γ,a:κee:σ0 with σ=σ0[φ/a] and Γδφ:κ; lemma 131.26 concludes.

Beta and LetBeta. Invert App together with Abs, respectively Let, and apply lemma 131.25.

CaseRed. Inverting Case gives ΓeK σ φ e:Tυ and, for the selected alternative, ΓpK b:θ(ι) x:θ(σ)e:Tυσ with θ=[υ/a]. Inverting the constructor application through Var, AppT and App shows σ=υ, that each φi has kind θ(ιi), and that each ei has type θ(σi) further instantiated by [φ/b]. Applying lemma 131.26 once per bi and lemma 131.25 once per xi gives the substituted right-hand side the type σ.

Comb. Inverting Cast twice gives ΓCOγ1:σvτ1 and ΓCOγ2:τ1σ; Trans composes them and Cast reapplies.

TPush. Here ΓCOγ:a:κ.σ1b:κ.σ2 and Γ,a:κee:σ1. Under a:κ, TyVar gives Γ,a:κTYa:κ, so CoInstT gives Γ,a:κCOγ@a:σ1σ2[a/b] — the left side is σ1[a/a]=σ1. Then Cast types eγ@a at σ2[a/b] and AbsT gives a:κ.σ2[a/b], which is b:κ.σ2 up to renaming. The enclosing AppT premise is unchanged.

CPush. Here κ is an equality kind, Γ,a:κee:σ with afv(σ), and ΓCOγ:κσκσ. LeftC and Sym give ΓCOγ1:κκ and RightC gives ΓCOγ2:σσ. Under a:κ, CoVar gives Γ,a:κCOa:κ and CastC gives Γ,a:κCOaγ1:κ. Lemma 131.26 with δ=CO types e[aγ1/a] at σ, Cast moves it to σ, and AbsT gives κσ.

Push. Writing σ1σ2 as ()σ1σ2, the premises of Leftr and Rightr are discharged by theorem 131.19 applied to γ, so γ1 has kind τ1σ1 and γ2 has kind σ2τ2. Then y:τ1 gives yγ1:σ1 by Cast, lemma 131.25 types the substituted body at σ2, a second Cast moves it to τ2, and Abs gives τ1τ2.

KPush. Let K:an:κ.b:ι.ρTan. Repeated Leftr and one Rightr give ΓCOγi:σiτi, their side premises again supplied by theorem 131.19. Theorem 131.12, clause 1 for each ρi and clause 2 for each equality kind ιi, gives θ(ρi) the kind [σ/a]ρi[τ/a]ρi and θ(ιi) the kind [σ/a]ιi[τ/a]ιi. Hence Cast types each ei and CastC kinds each φi at the arguments required by K instantiated at τ, so the reduct is a constructor application of type Tτ. The scrutinee’s type changed from Tσ cast to Tτ into Tτ directly, and the alternatives were already typed against Tτ by Case. ◻

Consistency, canonical forms and progress

Definition 131.28 — Value type

A type υ is a value type when it has the form a:κ.υ or Tυ.

Definition 131.29 — Consistency

A top-level environment Γ is consistent when

  1. if ΓCOγ:Tσυ and υ is a value type, then υ=Tτ for some τ; and

  2. if ΓCOγ:a:κ.συ and υ is a value type, then υ=a:κ.τ for some τ.

Consistency is undecidable in general; the parameter X of FC(X) is a decision procedure for it, chosen with the source construct being compiled. Theorem 131.41 discharges it outright for GADT programs.

Lemma 131.30 — Canonical forms

Let Γ be a consistent top-level environment and let Γecv:σ with cv a cvalue.

  1. If σ=σ1σ2 then cv is λx:σ1.e or (λx:τ1.e)γ.

  2. If σ=a:κ.σ0 then cv is Λa:κ.e or (Λa:κ.e)γ.

  3. If σ=Tυ then cv is K σ φ e or (K σ φ e)γ with K a constructor of T.

Proof of Lemma 131.30 — Canonical forms

Proof. A plain value is typed by Abs, AbsT or a saturated chain of Var, AppT and App at a constructor; the three cases give types ()σxσ, a:κ.σ and Tυ. Each is a value type, and its head determines the shape of the value, because Γ binds no term variable.

For cv=vγ, inverting Cast gives Γev:τ and ΓCOγ:τσ with τ one of the three value types just listed. Sym gives ΓCOsym γ:στ. In case 1, σ=()σ1σ2 is of the form Tσ with T=(); clause 1 of definition 131.29 applied to sym γ and the value type τ gives τ=()τ1τ2, so v is a lambda abstraction. Case 3 is the same argument with T in place of (). In case 2, clause 2 gives τ=a:κ.τ0, so v is a type abstraction; and its binder kind is κ because a:κ.τ0 and a:κ.σ0 are related, whose kinds TyAll records. ◻

Theorem 131.31 — Progress and subject reduction

Let Γ be a consistent well-formed top-level environment in which every case of the term has one alternative for each constructor of the scrutinee’s type constructor, and let Γee:σ. Then either e is a cvalue, or there is e with ee and Γee:σ.

Proof of Theorem 131.31 — Progress and subject reduction

Proof. Preservation is theorem 131.27, so only the disjunction must be proved; induct on the typing derivation. Var cannot occur, since Γ binds no term variable and a bare constructor is not saturated unless it takes no arguments, in which case it is a plain value. Abs and AbsT give plain values. Let gives an LetBeta redex.

App with Γee1e2:σ1 and Γee1:σ2σ1. By the induction hypothesis either e1 steps, in which case the context Ee2 lifts the step; or e1 is a cvalue. By lemma 131.30 clause 1 it is then λx.e3, giving a Beta redex, or (λx.e3)γ, giving a Push redex.

AppT with Γeeφ:σ0[φ/a]. Either e steps under Eφ, or by clause 2 it is Λa:κ.e0, giving TBeta, or (Λa:κ.e0)γ. In the last case definition 131.2 assigns κ exactly one sort, which selects TPush or CPush; the side conditions of the selected rule are the kinding facts supplied by theorem 131.19 applied to γ.

Case with scrutinee e0 of type Tυ. Either e0 steps under the context case E of pe, or by clause 3 it is K σ φ e, giving CaseRed because the alternatives are exhaustive for T, or it is that application under a cast, giving KPush.

Cast with Γee0γ:τ. Either e0 steps under Eγ; or e0 is a plain value, and then e0γ is already a cvalue; or e0 is vγ, giving a Comb redex. ◻

Corollary 131.32 — Syntactic soundness

Under the hypotheses of theorem 131.31, either ecv with Γecv:σ for some cvalue cv, or the reduction from e is infinite.

Proof of Corollary 131.32 — Syntactic soundness

Proof. Iterate theorem 131.31. Each step preserves the type and the hypotheses, so no finite reduction sequence ends at a non-cvalue. ◻

Remark 131.33 — Local assumptions may be inconsistent

The term f=λg:(IntBool).1+trueg is well typed in a consistent top-level environment: the false equality is a hypothesis of f, not an axiom. Theorem 131.31 is not violated, because to reduce a call of f one must first supply a closed coercion of kind IntBool, and consistency of the top-level environment says that none exists.

Erasure

Definition 131.34 — Erasure

x=x,K=K,(λx:φ.e)=λx.e,(Λa:κ.e)=λa.e,(eσ)=e (),(e1e2)=e1 e2,(eγ)=e,(K a:κ x:φ)=K a x, extended homomorphically to let and case. Type abstraction erases to abstraction over a unit argument, and type application to application to (), so that the number of reduction steps of a call-by-value target is not changed by erasure.

Theorem 131.35 — Erasure

Let Γ be a consistent well-formed top-level environment and Γee1:σ.

  1. If e1 is a cvalue then e1 is a value.

  2. If e1e2 then e1e2 or e1=e2.

Proof of Theorem 131.35 — Erasure

Proof. Clause 1 is immediate: erasure deletes the outer cast of vγ and maps the three plain-value forms to abstractions and saturated constructor applications.

Clause 2 is by cases on the contraction rule, and the only work is to see which rules disappear. Beta, LetBeta and CaseRed erase to the corresponding untyped steps, using (e[e/x])=e[e/x], which holds by induction on e because erasure is compositional and does not bind. TBeta erases to a beta step at the unit argument, using (e[φ/a])=e[()/a]. Comb, TPush, CPush, Push and KPush all erase to an equality. For Comb and TPush both sides erase to v and (λa.e) (). For CPush and Push, the reduct’s binder is renamed and its body is e[yγ1/x], whose erasure is e[y/x]; both sides therefore erase to the same term up to renaming of the bound variable. For KPush the arguments change only by casts, so both sides erase to case (K a e) of. Closure under evaluation contexts follows because Ee=Ee for the erased contexts, where (Eγ)=E. ◻

Corollary 131.36 — Erasure soundness

For well-typed e1 in a consistent well-formed top-level environment, e1e2 implies e1e2, and e1 reduces to a cvalue if and only if e1 reduces to a value.

Proof of Corollary 131.36 — Erasure soundness

Proof. The forward direction iterates theorem 131.35. For the converse, note that every step of the erased term is the erasure of a step of the typed term: by theorem 131.31 a well-typed non-cvalue steps, and by theorem 131.35 its erasure either steps correspondingly or is unchanged; the unchanged cases Comb, TPush, CPush, Push and KPush strictly decrease the number of casts standing on the head of an elimination, so only finitely many of them occur consecutively. ◻

The practical content of corollary 131.36 is that equality evidence costs nothing at run time. It also explains why coercions are types rather than terms: a coercion is guaranteed to be a proof by being well kinded, whereas a term of an equality type would first have to be evaluated to check that it is not divergent.

Exercise 131.6

★★☆ Compute both sides of one KPush step for Cons:a:.a[a][a] and ΓCOγ:[Int][τ], then erase both. State which clause of theorem 131.12 produces the coercion that casts the tail.

Elaborating the GADT source fragment

The source language is Hindley–Milner with constrained constructor signatures: π::=ηa.π,η::=τ(ττ)η,τ::=aττTτ, and C::=ϵC,co:τ1τ2 is a set of named equality assumptions. The judgment C;ΓGe:πe reads: under the assumptions C and environment Γ, the source expression e has type π and elaborates to the FCr term e.

Definition 131.37 — Elaboration

x:πΓ
C;ΓGx:πx
G-Var
C;ΓGe:τeCCOγ:ττ
C;ΓGe:τeγ
G-Eq
C;ΓGe:πeafv(C,Γ)
C;ΓGe:a.πΛa:.e
G-Gen
C;ΓGe:a.πe
C;ΓGe:π[τ/a]eτ
G-Inst
C,co:τ1τ2;ΓGe:ηe
C;ΓGe:(τ1τ2)ηΛco:(τ1τ2).e
G-CIntro
C;ΓGe:(τ1τ2)ηeCCOγ:τ1τ2
C;ΓGe:ηeγ
G-CElim

For a pattern alternative, with K::a,b.(ττ)τTa, ab=, fv(τ,τ,τ)=fv(a,b), θ=[υ/a] and fresh co,

C,co:θ(τ)θ(τ);Γ,x:θ(τ)Ge:τre
C;ΓGK xe:TυτrK b: co:θ(τ)θ(τ) x:θ(τ)e
G-Alt

The coercion γ in G-Eq and G-CElim is an output, not an input: the elaborator must construct a proof of τ1τ2 from the named assumptions C. That search is decidable, and the algorithm is first-order unification with evidence.

Lemma 131.40 — Elaboration is type preserving

If C;ϵGe:τe then Cee:τ, where C is read as an FCr environment of coercion variables.

Proof of Lemma 131.40 — Elaboration is type preserving

Proof. Induction on the elaboration derivation. G-Var matches Var. G-Gen and G-Inst match AbsT and AppT with δ=TY and κ=; the freshness side condition of G-Gen is the side condition of AbsT. G-CIntro and G-CElim match the same two rules with δ=CO, the second using its coercion premise as the third premise of AppT. G-Eq matches Cast directly. G-Alt matches Alt: the source constructor signature is already an FCr type, its equality constraints becoming coercion binders b:ι with ι an equality kind, and the pattern binds exactly those binders. ◻

Theorem 131.41 — GADT consistency

Let Γ be an environment whose domain contains no type variable and no coercion constant. If ΓCOγ:σ1σ2 then σ1=σ2. Consequently every environment produced by definition 131.37 from a GADT program is consistent.

Proof of Theorem 131.41 — GADT consistency

Proof. Induction on the derivation of the coercion judgment. The two leaf rules are CoRefl, whose conclusion has σ1=σ2=d, and CoVar, which is unavailable: a coercion variable co is a type variable and a coercion constant C is a coercion constant, and the hypothesis excludes both from dom(Γ).

Every other rule builds its conclusion from premises about coercions in the same Γ. Sym and Trans preserve the identity of the two sides. CoAllTr: the induction hypothesis in Γ,a:κ does not apply directly, because that environment does contain a type variable; but a is bound with a type kind, so CoVar still cannot fire at a — it requires an equality kind — and the induction goes through with the strengthened hypothesis “Γ contains no coercion constant and no variable of equality kind”. Under that hypothesis Comp, SComp, CompCr and EqCoercer give σ1=σ2 componentwise; Leftr, Rightr, LeftC and RightC give it by taking the corresponding component of an identity; CoInstT substitutes the same υ on both sides of an identity; and CastC rewrites a kind by an identity and so changes nothing. Since elaboration introduces coercion variables only at pattern binders, and introduces no axiom, the resulting top-level environment satisfies the hypothesis, and definition 131.29 follows: a coercion between two value types forces them to be equal, so in particular their heads agree. ◻

Exercise 131.7

★★☆ Elaborate the source evaluator of example 131.10 using definition 131.37, and check every generated coercion against definition 131.6. Then say which elaboration rule introduces the coercion sym co and which source subterm forces it.

Explicit convertibility in pure type systems

A pure type system with explicit convertibility proofs, written λfS, also stores in the term the reason two types were identified; the system λfS of van Doorn, Geuvers and Wiedijk (2013) is the reference point here, and its rules are that paper’s Figure 4.1. Its conversion rule is Γfa:AΓfA:sΓfH:A=AΓfaH:AfConv, and the erasure || deleting every H satisfies: if ΓfA:B then |Γ||A|:|B|, and λS and λfS are equivalent frameworks. The resemblance to Cast is real but shallow, and the exact differences matter for what can be transferred.

FCr(X) λfS
What evidence proves interchangeability of two types of one kind convertibility of two terms, types included
Sort of evidence a type, of kind σ1σ2 a proof term H of a separate judgment ΓfH:A=A
Source of the equations top-level axioms, restricted by definition 131.29 the fixed β-rule, closed under congruence
Evidence in the type language yes; coercions are erased with types no; H is a term-level annotation
Decidability type checking is syntax-directed and decidable; consistency is the parameter X type checking is linear in the size of the term; every judgment has one derivation
Erasure theorem theorem 131.35: run-time behaviour is unchanged λfSλS: typability is preserved

The table also records what is not shared. In λfS the equations are generated by one fixed reduction relation and are therefore consistent by confluence; in FCr(X) they are supplied by the program and consistency is an assumption that a decision procedure must discharge. Conversely λfS equates terms of dependent types, which FCr(X) has no means to express, since its equality kinds relate types only. Neither erasure theorem implies the other.

Limits and seminar

The frozen source is the 2007 System FC(X) of Sulzmann, Chakravarty, Peyton Jones and Donnelly; the calculus proved about here is the audited system FCr(X) of convention 131.17, which adds five side conditions to the coercion rules, corrects the scope of Data (remark 131.9) and completes the reduction relation with LetBeta (remark 131.23). Proposition 131.16 shows that the additions are not cosmetic. Theorem 131.19, Theorem 131.27, Theorem 131.31, Theorem 131.35, Theorem 131.41 are local theorems about FCr(X); the published proofs supply the architecture of the argument and the statements of definition 131.28, definition 131.29, and are cited as comparisons, not as exact-signature imports.

Four boundaries are worth stating plainly. Consistency of a top-level environment is undecidable, so no theorem here provides a procedure; the chapter proves consistency outright only for the GADT fragment (theorem 131.41), where no axiom is generated. Nothing here is a theorem about type-family termination, overlap or confluence: an axiom set generated from associated types must be checked non-overlapping by whatever procedure plays the role of X. Nothing here bounds the size of coercions; the push rules make them grow, and the simplifications sym σ=σ and eσ=e for a reflexivity witness σ are optional compiler transformations, not part of the calculus. Finally, the later role and equality machinery of production compilers is a different system with different theorems, and the elaboration of section 131.8 covers the GADT fragment only; associated types add the class-predicate rules that were not developed here.

[4]

Suggested first pass.

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

Exercise 131.8

★★☆ Exhibit a two-axiom top-level environment that is inconsistent by clause 1 of definition 131.29, and a closed well-typed term that reduces, using that environment, to a stuck non-cvalue. Identify the exact step of the proof of theorem 131.31 that fails, and the clause of lemma 131.30 it depended on.

Exercise 131.9

★★☆ In a program where x:T a and y:a and matching x against a constructor refines a to Int, a compiler proposes to float let z=y+1 out of the enclosing abstraction. Write the source and the floated form in FCr(X), show which typing premise the floated form lacks, and give the repaired transformation that abstracts the binding over the coercion bound by the match.

Exercise 131.10

★★★ Take e=Λco:(σ1Int).λx:σ1.xco and a coercion γ of kind ((σ1Int)σ1Int)((σ2Int)σ2Int). Reduce (eγ)γ by CPush, giving the kind of every coercion that appears, and then verify the reduct’s type by the argument of theorem 131.27. Finally erase both sides and check theorem 131.35 clause 2 on this instance.

Exercise 131.11 — Practical: coercion kinder and eraser

★★★ Practical project.fc-coercion-kinder Complete project fc-coercion-kinder. Implement, for the finite signature of example 131.10 extended by one type function and one axiom, (i) kind well-formedness and type kinding, (ii) the coercion kinder of definition 131.6 under convention 131.17, (iii) the cast rule Cast, (iv) the reduction of definition 131.22 restricted to the rules TBeta, Beta, CaseRed and Comb, and (v) the erasure of definition 131.34. The invariant the implementation must maintain is that the kinder returns an equality kind only when both sides are assigned one common kind by the type kinder — that is, theorem 131.19 as a run-time check. The named cases print

lift-tree: Tree(a) ~ Tree(b)
decompose-typefun: rejected
left-mixed-kind: rejected
gadt-zero: 0
bogus-axiom: inconsistent

The checker is independent evidence for the finite signature; it does not prove theorem 131.27, theorem 131.31 and does not decide consistency for an arbitrary axiom set.

Search the book

Type to search the local edition.