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, 𝖹𝖾𝗋𝗈:𝖤𝗑𝗉𝖨𝗇𝗍,𝖲𝗎𝖼𝖼:𝖤𝗑𝗉𝖨𝗇𝗍→𝖤𝗑𝗉𝖨𝗇𝗍,𝖯𝖺𝗂𝗋:𝖤𝗑𝗉𝑏→𝖤𝗑𝗉𝑐→𝖤𝗑𝗉(𝑏,𝑐), together with an evaluator whose declared type is 𝖾𝗏𝖺𝗅:𝖤𝗑𝗉𝑎→𝑎. In the 𝖹𝖾𝗋𝗈 branch the evaluator must return the literal 0, and 0 has type 𝖨𝗇𝗍, not 𝑎.
Try to type that branch in System F extended by algebraic data types with existential components and higher kinds — write 𝐹𝐴 for that language. The branch body must be given the result type 𝑎, so the only possible leaf is 0:𝖨𝗇𝗍∈ΓΓ⊢0:𝑎FA−Const. The premise of FA-Const is 0:𝑎∈Γ. The displayed tree is therefore not a derivation. No other rule repairs it: in 𝐹𝐴 type equality is syntactic identity of types, the type variable 𝑎 is not the type constant 𝖨𝗇𝗍, 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 𝑎 and 𝖨𝗇𝗍 may be interchanged. System 𝐹𝐶(𝑋) 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 𝐹𝐶(𝑋) 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.
Fix disjoint countable sets of type variables𝑎,𝑏,𝑐,𝑐𝑜, term variables𝑥,𝑓, coercion constants𝐶, value type constructors𝑇, type functions𝑆𝑛 of declared arity 𝑛, and data constructors𝐾. Sorts, kinds, types-and-coercions, terms, patterns and environments are generated by 𝛿::=𝖳𝖸∣𝖢𝖮,𝜅,𝜄::=⋆∣𝜅1→𝜅2∣𝜎1∼𝜎2,𝑑::=𝑎∣𝑇,𝑔::=𝑐𝑜∣𝐶,𝜑,𝜌,𝜎,𝜏,𝜐,𝛾::=𝑎∣𝐶∣𝑇∣𝜑1𝜑2∣𝑆𝑛𝜑𝑛∣∀𝑎:𝜅.𝜑∣𝗌𝗒𝗆𝛾∣𝛾1∘𝛾2∣𝛾@𝜑∣𝗅𝖾𝖿𝗍𝛾∣𝗋𝗂𝗀𝗁𝗍𝛾∣𝛾1∼𝛾2∣𝗅𝖾𝖿𝗍𝖼𝛾∣𝗋𝗂𝗀𝗁𝗍𝖼𝛾∣𝛾1▸𝛾2,𝑢::=𝑥∣𝐾,𝑒::=𝑢∣Λ𝑎:𝜅.𝑒∣𝑒𝜑∣𝜆𝑥:𝜎.𝑒∣𝑒1𝑒2∣𝗅𝖾𝗍𝑥:𝜎=𝑒1𝗂𝗇𝑒2∣𝖼𝖺𝗌𝖾𝑒1𝗈𝖿―――――𝑝→𝑒2∣𝑒▸𝛾,𝑝::=𝐾―――𝑏:𝜅―――𝑥:𝜎,Γ::=𝜖∣Γ,𝑢:𝜎∣Γ,𝑑:𝜅∣Γ,𝑔:𝜅∣Γ,𝑆𝑛:𝜅. 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 ―――𝜑𝑛 for a sequence 𝜑1⋯𝜑𝑛. A type function symbol 𝑆𝑛 occurs only in the saturated form 𝑆𝑛𝜑𝑛.
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.
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.
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 𝑑 in Γ. For TySCon it is the result kind 𝜄 of the unique declaration of 𝑆𝑛. For TyAll it is ⋆. For TyApp the induction hypothesis gives one kind 𝜅1→𝜅2 for 𝜎1; an arrow kind determines 𝜅2. ◻
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. ◻
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 𝖨𝗇𝗍 has kind 𝖨𝗇𝗍∼𝖨𝗇𝗍. Lemma 131.11 extends that to every well-kinded type. Second, Left and Right apply to ordinary application 𝜑1𝜑2 and not to 𝑆𝑛𝜑𝑛; 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.
In AppT the metavariable 𝛿 is 𝖳𝖸 when 𝜅 is a type kind and 𝖢𝖮 when 𝜅 is an equality kind, so one rule covers both type application and coercion application. A binder 𝑏:𝜄 of a data constructor is existential when 𝜄 is a type kind and a coercion binder when 𝜄 is an equality kind.
A program is pgm::=―――decl;𝑒 with decl::=𝖽𝖺𝗍𝖺𝑇:𝜅𝗐𝗁𝖾𝗋𝖾―――――――――――𝐾:∀―――𝑎:𝜅.∀―――𝑏:𝜄.――𝜎→𝑇――𝑎∣𝗍𝗒𝗉𝖾𝑆𝑛:𝜅𝑛→𝜄∣𝖺𝗑𝗂𝗈𝗆𝐶:𝜎1∼𝜎2. Declaration checking uses
Γ⊢𝗄𝜅:𝖳𝖸Γ,𝑇:𝜅⊢𝖳𝖸𝜎𝐾:⋆foreach𝐾
Γ⊢(𝖽𝖺𝗍𝖺𝑇:𝜅𝗐𝗁𝖾𝗋𝖾――――𝐾:𝜎𝐾):(𝑇:𝜅,――――𝐾:𝜎𝐾)
Data
Γ⊢𝗄𝜅𝑖:𝖳𝖸Γ⊢𝗄𝜄:𝖳𝖸
Γ⊢(𝗍𝗒𝗉𝖾𝑆𝑛:𝜅𝑛→𝜄):(𝑆𝑛:𝜅𝑛→𝜄)
Type
Γ⊢𝗄𝜅:𝖢𝖮
Γ⊢(𝖺𝗑𝗂𝗈𝗆𝐶:𝜅):(𝐶:𝜅)
Coerce
A program ―――decl;𝑒 is well typed at 𝜎 when the declarations extend the initial environment Γ0 to Γ and Γ⊢𝖾𝑒:𝜎. An environment produced this way is a top-level environment: it binds only 𝑇, 𝑆𝑛, 𝐾 and 𝐶, and no type variable, coercion variable or term variable.
The printed declaration rule checks Γ⊢𝖳𝖸𝜎:⋆ in Γ. A constructor type mentions the type constructor it builds — 𝖹𝖾𝗋𝗈:∀𝑎:⋆.∀𝑐𝑜:𝑎∼𝖨𝗇𝗍.𝖤𝗑𝗉𝑎 mentions 𝖤𝗑𝗉 — so that premise is never derivable for a recursive declaration. Data above checks the constructor types in Γ,𝑇:𝜅. Nothing else in the calculus changes, and every later proof uses the corrected rule.
Declare 𝖽𝖺𝗍𝖺𝖤𝗑𝗉:⋆→⋆𝗐𝗁𝖾𝗋𝖾𝖹𝖾𝗋𝗈:∀𝑎:⋆.(𝑎∼𝖨𝗇𝗍)⇒𝖤𝗑𝗉𝑎,𝖲𝗎𝖼𝖼:∀𝑎:⋆.(𝑎∼𝖨𝗇𝗍)⇒𝖤𝗑𝗉𝖨𝗇𝗍→𝖤𝗑𝗉𝑎. The 𝖹𝖾𝗋𝗈 branch that had no 𝐹𝐴 derivation now has one. In the alternative 𝖹𝖾𝗋𝗈(𝑐𝑜:𝑎∼𝖨𝗇𝗍) the pattern binds 𝑐𝑜, so CoVar gives Γ⊢𝖢𝖮𝑐𝑜:𝑎∼𝖨𝗇𝗍, Sym gives Γ⊢𝖢𝖮𝗌𝗒𝗆𝑐𝑜:𝖨𝗇𝗍∼𝑎, and Cast gives Γ⊢𝖾0:𝖨𝗇𝗍𝑐𝑜:𝑎∼𝖨𝗇𝗍∈ΓΓ⊢𝖢𝖮𝑐𝑜:𝑎∼𝖨𝗇𝗍CoVarΓ⊢𝖢𝖮𝗌𝗒𝗆𝑐𝑜:𝖨𝗇𝗍∼𝑎SymΓ⊢𝖾0▸𝗌𝗒𝗆𝑐𝑜:𝑎Cast. The whole evaluator is 𝖾𝗏𝖺𝗅=Λ𝑎:⋆.𝜆𝑒:𝖤𝗑𝗉𝑎.𝖼𝖺𝗌𝖾𝑒𝗈𝖿⎧{
{⎨{
{⎩𝖹𝖾𝗋𝗈(𝑐𝑜:𝑎∼𝖨𝗇𝗍)→0▸𝗌𝗒𝗆𝑐𝑜𝖲𝗎𝖼𝖼(𝑐𝑜:𝑎∼𝖨𝗇𝗍)(𝑒′:𝖤𝗑𝗉𝖨𝗇𝗍)→(𝖾𝗏𝖺𝗅𝖨𝗇𝗍𝑒′+1)▸𝗌𝗒𝗆𝑐𝑜. The evidence appears twice in the term and nowhere in the run-time behaviour: theorem 131.35 shows that both casts vanish.
★☆☆ Write the derivation of Γ⊢𝖾(𝖾𝗏𝖺𝗅𝖨𝗇𝗍𝑒′+1)▸𝗌𝗒𝗆𝑐𝑜:𝑎 in the 𝖲𝗎𝖼𝖼 branch of example 131.10, naming the rule at every node. Then say which single premise fails if 𝗌𝗒𝗆 is deleted.
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.
Proof. Simultaneous induction on the two derivations.
Clause 1.TyVar: CoRefl applies to the same premises and gives Γ⊢𝖢𝖮𝑑:𝑑∼𝑑. TyApp𝜑=𝜎1𝜎2: the induction hypothesis gives Γ⊢𝖢𝖮𝜎𝑖:𝜎𝑖∼𝜎𝑖, and Comp, whose third premise is the kinding of 𝜎1𝜎2 already in hand, gives Γ⊢𝖢𝖮𝜎1𝜎2:𝜎1𝜎2∼𝜎1𝜎2. TySCon: the same with SComp. TyAll𝜑=∀𝑎:𝜅.𝜎: split on the sort of 𝜅. If Γ⊢𝗄𝜅:𝖳𝖸, the induction hypothesis in Γ,𝑎:𝜅 and CoAllT give Γ⊢𝖢𝖮∀𝑎:𝜅.𝜎:∀𝑎:𝜅.𝜎∼∀𝑎:𝜅.𝜎. If Γ⊢𝗄𝜅:𝖢𝖮, then 𝑎∉fv(𝜎) and 𝜑=𝜅⇒𝜎; clause 2 gives Γ⊢𝖢𝖮𝜅:𝜅∼𝜅, clause 1 for 𝜎 gives Γ⊢𝖢𝖮𝜎:𝜎∼𝜎, and CompC gives Γ⊢𝖢𝖮𝜅⇒𝜎:𝜅⇒𝜎∼𝜅⇒𝜎.
Clause 2.EqTy𝜅=𝜎1∼𝜎2: clause 1 gives Γ⊢𝖢𝖮𝜎𝑖:𝜎𝑖∼𝜎𝑖, and EqCoerce gives Γ⊢𝖢𝖮𝜎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 𝛾.
Proof. The obstruction is that 𝛾 has one fixed kind while 𝜑 may place 𝑎 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 𝑎 is reached; the invariant is that after processing a subterm 𝜓, the constructed coercion has kind 𝜓[𝜎1/𝑎]∼𝜓[𝜎2/𝑎]. Formally we induct simultaneously on the two kinding derivations.
Clause 1.TyVar with 𝑑=𝑎: then 𝜅=𝜅′, 𝜑[𝛾/𝑎]=𝛾 and 𝜑[𝜎𝑖/𝑎]=𝜎𝑖, so the conclusion is the hypothesis on 𝛾. TyVar with 𝑑≠𝑎: the substitution leaves 𝑑 unchanged and lemma 131.11 gives Γ⊢𝖢𝖮𝑑:𝑑∼𝑑. TyApp𝜑=𝜑1𝜑2: the induction hypothesis gives Γ⊢𝖢𝖮𝜑𝑗[𝛾/𝑎]:𝜑𝑗[𝜎1/𝑎]∼𝜑𝑗[𝜎2/𝑎] for 𝑗=1,2. Lemma 131.18 gives Γ⊢𝖳𝖸(𝜑1𝜑2)[𝜎1/𝑎]:𝜅, which is the third premise of Comp; that rule then gives the required coercion, because substitution commutes with application. TySCon: identical with SComp. TyAll𝜑=∀𝑏:𝜄.𝜓, where 𝑏 is chosen outside fv(𝛾)∪fv(𝜎1)∪fv(𝜎2)∪fv(Γ)∪{𝑎}. If Γ⊢𝗄𝜄:𝖳𝖸, weaken 𝛾 to Γ,𝑏:𝜄 by lemma 131.5, apply the induction hypothesis to 𝜓 there, and conclude by CoAllT. If Γ⊢𝗄𝜄:𝖢𝖮, then 𝜑=𝜄⇒𝜓 with 𝑏∉fv(𝜓); clause 2 applied to 𝜄 and clause 1 applied to 𝜓 feed CompC.
Clause 2.EqTy𝜅=𝜐1∼𝜐2: clause 1 gives coercions 𝜐𝑗[𝛾/𝑎] of kind 𝜐𝑗[𝜎1/𝑎]∼𝜐𝑗[𝜎2/𝑎], and EqCoerce assembles 𝜐1[𝛾/𝑎]∼𝜐2[𝛾/𝑎] 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.
Let Γ⊢𝖢𝖮𝛾:𝜎1∼𝜎2 with both sides of kind ⋆, and let 𝖳𝗋𝖾𝖾:⋆→⋆. Taking 𝜑=𝖳𝗋𝖾𝖾𝑎, theorem 131.12 produces the coercion 𝖳𝗋𝖾𝖾𝛾 of kind 𝖳𝗋𝖾𝖾𝜎1∼𝖳𝗋𝖾𝖾𝜎2: the head is the reflexivity witness 𝖳𝗋𝖾𝖾 supplied by CoRefl, and the argument is 𝛾. Taking 𝜑=∀𝑏:⋆.𝑎→𝖨𝗇𝗍 gives Γ⊢𝖢𝖮∀𝑏:⋆.𝛾→𝖨𝗇𝗍:∀𝑏:⋆.𝜎1→𝖨𝗇𝗍∼∀𝑏:⋆.𝜎2→𝖨𝗇𝗍, where 𝛾→𝖨𝗇𝗍 abbreviates (→)𝛾𝖨𝗇𝗍, an instance of Comp whose head is the reflexivity witness (→).
Decomposition runs the other way. From Γ⊢𝖢𝖮𝛾:𝖳𝗋𝖾𝖾𝜎1∼𝖳𝗋𝖾𝖾𝜎2, Right extracts Γ⊢𝖢𝖮𝗋𝗂𝗀𝗁𝗍𝛾:𝜎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.
Suppose 𝑆1 were allowed to appear unapplied, so that 𝑆1𝜎 counted as an ordinary application. With the two axioms 𝐶1:𝑆1𝖨𝗇𝗍∼𝖡𝗈𝗈𝗅 and 𝐶2:𝑆1𝖡𝗈𝗈𝗅∼𝖡𝗈𝗈𝗅 the coercion 𝐶1∘𝗌𝗒𝗆𝐶2 has kind 𝑆1𝖨𝗇𝗍∼𝑆1𝖡𝗈𝗈𝗅, and Right would produce a coercion of kind 𝖨𝗇𝗍∼𝖡𝗈𝗈𝗅. Both axioms are individually harmless — they say only that a type function takes the value 𝖡𝗈𝗈𝗅 at two arguments — so the failure is caused by decomposition, not by the axioms. Keeping 𝑆𝑛𝜑𝑛 syntactically distinct from 𝜑1𝜑2 removes the Right step: the premise of Right does not match 𝑆1𝖨𝗇𝗍∼𝑆1𝖡𝗈𝗈𝗅. 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.
★☆☆ Let Γ⊢𝖢𝖮𝛾:𝜎1∼𝜎2. Build, node by node, the coercion of kind (𝜎1∼𝖨𝗇𝗍)⇒𝖳𝗋𝖾𝖾𝜎1∼(𝜎2∼𝖨𝗇𝗍)⇒𝖳𝗋𝖾𝖾𝜎2 that theorem 131.12 constructs for 𝜑=(𝑎∼𝖨𝗇𝗍)⇒𝖳𝗋𝖾𝖾𝑎, naming the rule used at each node. (Half a page; both clauses of the theorem occur.)
★★☆ Give a well-formed top-level environment containing two axioms about one type function 𝑆1 of kind ⋆→⋆ from which a coercion of kind 𝖨𝗇𝗍∼𝖡𝗈𝗈𝗅 is derivable once Right is allowed to decompose 𝑆1-applications. Then check that your environment still satisfies definition 131.29 when Right keeps its printed restriction.
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.
Declare 𝗍𝗒𝗉𝖾𝖤𝗅𝖾𝗆1:⋆→⋆ and 𝖺𝗑𝗂𝗈𝗆ebs:𝖤𝗅𝖾𝗆1𝖡𝗂𝗍𝖲𝖾𝗍∼𝖢𝗁𝖺𝗋. 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 𝖡𝗂𝗍𝖲𝖾𝗍 is expected. With Γ⊢𝖾c:𝖢𝗁𝖺𝗋, Γ⊢𝖢𝖮ebs:𝖤𝗅𝖾𝗆1𝖡𝗂𝗍𝖲𝖾𝗍∼𝖢𝗁𝖺𝗋𝐶𝑜𝑉𝑎𝑟=axiom,Γ⊢𝖢𝖮𝗌𝗒𝗆ebs:𝖢𝗁𝖺𝗋∼𝖤𝗅𝖾𝗆1𝖡𝗂𝗍𝖲𝖾𝗍𝑆𝑦𝑚=fromthelineabove,Γ⊢𝖾c▸𝗌𝗒𝗆ebs:𝖤𝗅𝖾𝗆1𝖡𝗂𝗍𝖲𝖾𝗍𝐶𝑎𝑠𝑡=fromthetwolinesabove. A parametric axiom quantifies on each side separately, 𝖺𝗑𝗂𝗈𝗆el:(∀𝑒:⋆.𝖤𝗅𝖾𝗆1[𝑒])∼(∀𝑒:⋆.𝑒), and is used at a type by CoInstT: Γ⊢𝖢𝖮el@𝖨𝗇𝗍:𝖤𝗅𝖾𝗆1[𝖨𝗇𝗍]∼𝖨𝗇𝗍. A single quantifier over an equality, ∀𝑎:⋆.(𝖤𝗅𝖾𝗆1[𝑎]∼𝑎), is not a type of definition 131.1 at all: TyAll requires its body to have kind ⋆, and an equality kind is not ⋆.
★☆☆ A source declaration 𝗇𝖾𝗐𝗍𝗒𝗉𝖾𝑇=𝖬𝗄𝖳(𝑇→𝑇) is translated by a single axiom. State it, check that Coerce accepts it, and write the two casts that convert between 𝑇 and 𝑇→𝑇.
Everything proved so far used the coercion rules in the forward direction. The metatheory needs them in the backward direction as well: from Γ⊢𝖢𝖮𝛾:𝜅 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.
Proof of Proposition 131.16 — Failure of regularity for the printed decomposition rules
Proof. Let 𝐹:⋆→⋆ and 𝐺:(⋆→⋆)→⋆ be value type constructors and 𝖬𝖺𝗒𝖻𝖾:⋆→⋆. Put Γ=𝐹:⋆→⋆,𝐺:(⋆→⋆)→⋆,𝖬𝖺𝗒𝖻𝖾:⋆→⋆,𝖨𝗇𝗍:⋆,𝑔:𝐹𝖨𝗇𝗍∼𝐺𝖬𝖺𝗒𝖻𝖾. The binding of 𝑔 is well formed: TyApp gives Γ⊢𝖳𝖸𝐹𝖨𝗇𝗍:⋆ and Γ⊢𝖳𝖸𝐺𝖬𝖺𝗒𝖻𝖾:⋆, so EqTy applies with the common kind ⋆. By CoVar and then Left, Γ⊢𝖢𝖮𝗅𝖾𝖿𝗍𝑔:𝐹∼𝐺. But Γ⊢𝗄𝐹∼𝐺:𝖢𝖮 would need, by EqTy, one kind for both 𝐹 and 𝐺; 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 𝑔 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.
Every other rule is unchanged. All statements from here on are about 𝐹𝐶𝑟(𝑋). 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 𝐹𝐶𝑟(𝑋).
Proof. Induction on the derivation. TyVar at 𝑎 returns 𝜐, which has kind 𝜅′=𝜅′[𝜐/𝑎] because 𝑎∉fv(𝜅′); 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(𝜐)∪{𝑎}. The kinding and coercion cases are identical rule by rule; CoInstT additionally uses the equality (𝜎[𝜐/𝑎])[𝜑/𝑏]=(𝜎[𝜑/𝑏])[𝜐/𝑎], valid because 𝑏∉fv(𝜐). ◻
Let Γ be well formed, meaning that each binding 𝑑:𝜅 or 𝑆𝑛:𝜅 satisfies Γ<⊢𝗄𝜅:𝖳𝖸, each binding 𝑔:𝜅 satisfies Γ<⊢𝗄𝜅:𝖢𝖮 and each binding 𝑢:𝜎 satisfies Γ<⊢𝖳𝖸𝜎:⋆, where Γ< is the prefix preceding it. If Γ⊢𝖢𝖮𝛾:𝜅 in 𝐹𝐶𝑟(𝑋) then Γ⊢𝗄𝜅:𝖢𝖮.
Proof. Induction on the coercion derivation. CoRefl: TyVar gives Γ⊢𝖳𝖸𝑑:𝜅 from the same premises, and EqTy gives Γ⊢𝗄𝑑∼𝑑:𝖢𝖮. 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.
CoAllT𝑟: the added premise gives Γ,𝑎:𝜅⊢𝖳𝖸𝜎:⋆; the induction hypothesis gives Γ,𝑎:𝜅⊢𝗄𝜎∼𝜏:𝖢𝖮, whose EqTy premise and lemma 131.4 give Γ,𝑎:𝜅⊢𝖳𝖸𝜏:⋆. TyAll then kinds both ∀𝑎:𝜅.𝜎 and ∀𝑎:𝜅.𝜏 at ⋆, and EqTy concludes. CoInstT: by the induction hypothesis both quantified types have kind ⋆; inverting TyAll and applying lemma 131.18 kinds 𝜎[𝜐/𝑎] and 𝜏[𝜐/𝑏] at ⋆.
Comp: the third premise gives Γ⊢𝖳𝖸𝜎1𝜎2:𝜅; inverting TyApp gives Γ⊢𝖳𝖸𝜎1:𝜅1→𝜅 and Γ⊢𝖳𝖸𝜎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 𝑆𝑛.
Left𝑟: 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. Right𝑟: the added premises are the conclusion.
CompC𝑟: the induction hypothesis on 𝛾 gives Γ⊢𝗄𝜅1∼𝜅2:𝖢𝖮, 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 𝜅𝑖⇒𝜎𝑖 at ⋆. LeftC and RightC: invert TyAll on the two sides supplied by the induction hypothesis; the first gives Γ⊢𝗄𝜅𝑖:𝖢𝖮 and hence EqCo, the second gives Γ⊢𝖳𝖸𝜎𝑖:⋆ and hence EqTy.
EqCoerce𝑟: the added premise is the left half of the conclusion; the right half follows because 𝜏𝑖 shares the kind of 𝜎𝑖 by the induction hypothesis, so EqTy applies to 𝜏1,𝜏2. CastC: the induction hypothesis on 𝛾2 gives Γ⊢𝗄𝜅∼𝜅′:𝖢𝖮; its EqCo premise gives Γ⊢𝗄𝜅′:𝖢𝖮. ◻
Regularity is not decoration. Cast concludes Γ⊢𝖾𝑒▸𝛾:𝜏 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.
★★☆ For each of Left𝑟 and EqCoerce𝑟, 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 CoAllT𝑟, say why deleting the added premise leaves a statement that is still well formed but no longer implies regularity.
𝑣::=Λ𝑎:𝜅.𝑒∣𝜆𝑥:𝜎.𝑒∣𝐾――𝜎――𝜑――𝑒plainvalues,cv::=𝑣∣𝑣▸𝛾cvalues,𝐸::=[]∣𝐸𝑒∣𝐸𝜑∣𝐸▸𝛾∣𝖼𝖺𝗌𝖾𝐸𝗈𝖿――――𝑝→𝑒evaluationcontexts, where 𝐾――𝜎――𝜑――𝑒 is a saturated constructor application. The step relation is closed under contexts: 𝐸⟨𝑒⟩⟶𝐸⟨𝑒′⟩ whenever 𝑒⟶𝑒′.
A cvalue is a plain value under at most one cast. A term such as 𝗍𝗋𝗎𝖾▸𝛾 with Γ⊢𝖢𝖮𝛾:𝖡𝗈𝗈𝗅∼𝑆1𝜐 cannot be reduced further without changing its type, so the finished forms of evaluation must include it.
𝑇𝐵𝑒𝑡𝑎(Λ𝑎:𝜅.𝑒)𝜑⟶𝑒[𝜑/𝑎]𝐵𝑒𝑡𝑎(𝜆𝑥:𝜎.𝑒)𝑒′⟶𝑒[𝑒′/𝑥]𝐿𝑒𝑡𝐵𝑒𝑡𝑎𝗅𝖾𝗍𝑥:𝜎=𝑒1𝗂𝗇𝑒2⟶𝑒2[𝑒1/𝑥]𝐶𝑎𝑠𝑒𝑅𝑒𝑑𝖼𝖺𝗌𝖾(𝐾――𝜎――𝜑――𝑒)𝗈𝖿…𝐾――𝑏――𝑥→𝑒′…⟶𝑒′[――𝜑/――𝑏,――𝑒/――𝑥]𝐶𝑜𝑚𝑏(𝑣▸𝛾1)▸𝛾2⟶𝑣▸𝛾1∘𝛾2 together with four rules that move a cast off the head of an elimination. With Γ⊢𝖢𝖮𝛾:∀𝑎:𝜅.𝜎1∼∀𝑏:𝜅.𝜎2 and Γ⊢𝗄𝜅:𝖳𝖸, 𝑇𝑃𝑢𝑠ℎ((Λ𝑎:𝜅.𝑒)▸𝛾)𝜑⟶(Λ𝑎:𝜅.𝑒▸𝛾@𝑎)𝜑. With Γ⊢𝖢𝖮𝛾:𝜅⇒𝜎∼𝜅′⇒𝜎′, 𝛾1=𝗌𝗒𝗆(𝗅𝖾𝖿𝗍𝖼𝛾) and 𝛾2=𝗋𝗂𝗀𝗁𝗍𝖼𝛾, 𝐶𝑃𝑢𝑠ℎ((Λ𝑎:𝜅.𝑒)▸𝛾)𝜑⟶(Λ𝑎′:𝜅′.(𝑒[𝑎′▸𝛾1/𝑎])▸𝛾2)𝜑. With Γ⊢𝖢𝖮𝛾:𝜎1→𝜎2∼𝜏1→𝜏2, 𝛾1=𝗌𝗒𝗆(𝗋𝗂𝗀𝗁𝗍(𝗅𝖾𝖿𝗍𝛾)) and 𝛾2=𝗋𝗂𝗀𝗁𝗍𝛾, 𝑃𝑢𝑠ℎ((𝜆𝑥:𝜎1.𝑒)▸𝛾)𝑒′⟶(𝜆𝑦:𝜏1.(𝑒[𝑦▸𝛾1/𝑥])▸𝛾2)𝑒′. Finally, for 𝐾:∀―――𝑎𝑛:𝜅.∀―――𝑏:𝜄.――𝜌→𝑇――𝑎𝑛 and Γ⊢𝖢𝖮𝛾:𝑇――𝜎∼𝑇――𝜏, 𝐾𝑃𝑢𝑠ℎ𝖼𝖺𝗌𝖾((𝐾――𝜎――𝜑――𝑒)▸𝛾)𝗈𝖿―――――𝑝→rhs⟶𝖼𝖺𝗌𝖾(𝐾――𝜏―――𝜑′――𝑒′)𝗈𝖿―――――𝑝→rhs, where 𝛾𝑖=𝗋𝗂𝗀𝗁𝗍(𝗅𝖾𝖿𝗍⋯𝗅𝖾𝖿𝗍⏟__⏟__⏟𝑛−𝑖𝛾), 𝜃=―――――[𝛾𝑖/𝑎𝑖],―――――[𝜑𝑖/𝑏𝑖], 𝜑′𝑖=𝜑𝑖▸𝜃(𝜄𝑖) when 𝜄𝑖 is an equality kind and 𝜑′𝑖=𝜑𝑖 otherwise, and 𝑒′𝑖=𝑒𝑖▸𝜃(𝜌𝑖).
The printed operational semantics has no rule for 𝗅𝖾𝗍 and no evaluation context that enters one, so 𝗅𝖾𝗍𝑥:𝖨𝗇𝗍=0𝗂𝗇𝑥 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 𝗅𝖾𝗍 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 Γ⊢𝗄𝜅:𝖢𝖮 to fail and CPush requires it to hold, and definition 131.2 assigns each kind exactly one sort.
Take the declarations of example 131.10 and evaluate 𝖾𝗏𝖺𝗅𝖨𝗇𝗍(𝖹𝖾𝗋𝗈𝖨𝗇𝗍𝖨𝗇𝗍). The second 𝖨𝗇𝗍 is the coercion argument: by CoRefl it has kind 𝖨𝗇𝗍∼𝖨𝗇𝗍, which is what 𝖹𝖾𝗋𝗈’s second quantifier demands after instantiating 𝑎 by 𝖨𝗇𝗍. 𝖾𝗏𝖺𝗅𝖨𝗇𝗍(𝖹𝖾𝗋𝗈𝖨𝗇𝗍𝖨𝗇𝗍)𝑇𝐵𝑒𝑡𝑎⟶(𝜆𝑒:𝖤𝗑𝗉𝖨𝗇𝗍.𝖼𝖺𝗌𝖾𝑒𝗈𝖿…)(𝖹𝖾𝗋𝗈𝖨𝗇𝗍𝖨𝗇𝗍)𝐵𝑒𝑡𝑎⟶𝖼𝖺𝗌𝖾(𝖹𝖾𝗋𝗈𝖨𝗇𝗍𝖨𝗇𝗍)𝗈𝖿…𝐶𝑎𝑠𝑒𝑅𝑒𝑑⟶0▸𝗌𝗒𝗆𝖨𝗇𝗍. The result is a cvalue, not a plain value, and its type is 𝖨𝗇𝗍: the substitution replaced the pattern variable 𝑐𝑜 by the reflexivity coercion 𝖨𝗇𝗍. Theorem 131.35 shows that the erasure of the whole computation is the two-step reduction of 𝖾𝗏𝖺𝗅()𝖹𝖾𝗋𝗈 to 0.
Proof. Induction on the typing derivation. Var at 𝑥 returns 𝑒′, 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(𝑒′)∪{𝑥} and apply the induction hypothesis to the body. App, AppT, Cast and Case apply it to each premise; no type or coercion changes, because 𝑥 is a term variable. ◻
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 Γ,𝑎:𝜅,Γ′⊢𝖢𝖮𝛾:𝜎∼𝜏 becomes Γ,Γ′[𝜑/𝑎]⊢𝖢𝖮𝛾[𝜑/𝑎]:𝜎[𝜑/𝑎]∼𝜏[𝜑/𝑎] by the coercion clause of lemma 131.18, which is the premise Cast needs at the substituted types. ◻
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 Γ,𝑎:𝜅⊢𝖾𝑒:𝜎0 with 𝜎=𝜎0[𝜑/𝑎] and Γ⊢𝛿𝜑:𝜅; lemma 131.26 concludes.
Beta and LetBeta. Invert App together with Abs, respectively Let, and apply lemma 131.25.
CaseRed. Inverting Case gives Γ⊢𝖾𝐾――𝜎――𝜑――𝑒:𝑇――𝜐 and, for the selected alternative, Γ⊢𝗉𝐾――――𝑏:𝜃(𝜄)――――𝑥:𝜃(𝜎)→𝑒′:𝑇――𝜐→𝜎 with 𝜃=――――[𝜐/𝑎]. Inverting the constructor application through Var, AppT and App shows ――𝜎=――𝜐, that each 𝜑𝑖 has kind 𝜃(𝜄𝑖), and that each 𝑒𝑖 has type 𝜃(𝜎𝑖) further instantiated by ――――[𝜑/𝑏]. Applying lemma 131.26 once per 𝑏𝑖 and lemma 131.25 once per 𝑥𝑖 gives the substituted right-hand side the type 𝜎.
Comb. Inverting Cast twice gives Γ⊢𝖢𝖮𝛾1:𝜎𝑣∼𝜏1 and Γ⊢𝖢𝖮𝛾2:𝜏1∼𝜎; Trans composes them and Cast reapplies.
TPush. Here Γ⊢𝖢𝖮𝛾:∀𝑎:𝜅.𝜎1∼∀𝑏:𝜅.𝜎2 and Γ,𝑎:𝜅⊢𝖾𝑒:𝜎1. Under 𝑎:𝜅, TyVar gives Γ,𝑎:𝜅⊢𝖳𝖸𝑎:𝜅, so CoInstT gives Γ,𝑎:𝜅⊢𝖢𝖮𝛾@𝑎:𝜎1∼𝜎2[𝑎/𝑏] — the left side is 𝜎1[𝑎/𝑎]=𝜎1. Then Cast types 𝑒▸𝛾@𝑎 at 𝜎2[𝑎/𝑏] and AbsT gives ∀𝑎:𝜅.𝜎2[𝑎/𝑏], which is ∀𝑏:𝜅.𝜎2 up to renaming. The enclosing AppT premise is unchanged.
CPush. Here 𝜅 is an equality kind, Γ,𝑎:𝜅⊢𝖾𝑒:𝜎 with 𝑎∉fv(𝜎), and Γ⊢𝖢𝖮𝛾:𝜅⇒𝜎∼𝜅′⇒𝜎′. LeftC and Sym give Γ⊢𝖢𝖮𝛾1:𝜅′∼𝜅 and RightC gives Γ⊢𝖢𝖮𝛾2:𝜎∼𝜎′. Under 𝑎′:𝜅′, CoVar gives Γ,𝑎′:𝜅′⊢𝖢𝖮𝑎′:𝜅′ and CastC gives Γ,𝑎′:𝜅′⊢𝖢𝖮𝑎′▸𝛾1:𝜅. Lemma 131.26 with 𝛿=𝖢𝖮 types 𝑒[𝑎′▸𝛾1/𝑎] at 𝜎, Cast moves it to 𝜎′, and AbsT gives 𝜅′⇒𝜎′.
Push. Writing 𝜎1→𝜎2 as (→)𝜎1𝜎2, the premises of Left𝑟 and Right𝑟 are discharged by theorem 131.19 applied to 𝛾, so 𝛾1 has kind 𝜏1∼𝜎1 and 𝛾2 has kind 𝜎2∼𝜏2. Then 𝑦:𝜏1 gives 𝑦▸𝛾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 𝐾:∀―――𝑎𝑛:𝜅.∀―――𝑏:𝜄.――𝜌→𝑇――𝑎𝑛. Repeated Left𝑟 and one Right𝑟 give Γ⊢𝖢𝖮𝛾𝑖:𝜎𝑖∼𝜏𝑖, their side premises again supplied by theorem 131.19. Theorem 131.12, clause 1 for each 𝜌𝑖 and clause 2 for each equality kind 𝜄𝑖, gives 𝜃(𝜌𝑖) the kind ――――[𝜎/𝑎]𝜌𝑖∼――――[𝜏/𝑎]𝜌𝑖 and 𝜃(𝜄𝑖) the kind ――――[𝜎/𝑎]𝜄𝑖∼――――[𝜏/𝑎]𝜄𝑖. Hence Cast types each 𝑒′𝑖 and CastC kinds each 𝜑′𝑖 at the arguments required by 𝐾 instantiated at ――𝜏, so the reduct is a constructor application of type 𝑇――𝜏. The scrutinee’s type changed from 𝑇――𝜎 cast to 𝑇――𝜏 into 𝑇――𝜏 directly, and the alternatives were already typed against 𝑇――𝜏 by Case. ◻
Consistency is undecidable in general; the parameter 𝑋 of 𝐹𝐶(𝑋) is a decision procedure for it, chosen with the source construct being compiled. Theorem 131.41 discharges it outright for GADT programs.
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 (→)𝜎𝑥𝜎, ∀𝑎:𝜅.𝜎 and 𝑇――𝜐. Each is a value type, and its head determines the shape of the value, because Γ binds no term variable.
For cv=𝑣▸𝛾, inverting Cast gives Γ⊢𝖾𝑣:𝜏 and Γ⊢𝖢𝖮𝛾:𝜏∼𝜎 with 𝜏 one of the three value types just listed. Sym gives Γ⊢𝖢𝖮𝗌𝗒𝗆𝛾:𝜎∼𝜏. In case 1, 𝜎=(→)𝜎1𝜎2 is of the form 𝑇――𝜎 with 𝑇=(→); clause 1 of definition 131.29 applied to 𝗌𝗒𝗆𝛾 and the value type 𝜏 gives 𝜏=(→)𝜏1𝜏2, so 𝑣 is a lambda abstraction. Case 3 is the same argument with 𝑇 in place of (→). In case 2, clause 2 gives 𝜏=∀𝑎:𝜅.𝜏0, so 𝑣 is a type abstraction; and its binder kind is 𝜅 because ∀𝑎:𝜅.𝜏0 and ∀𝑎:𝜅.𝜎0 are related, whose kinds TyAll records. ◻
Let Γ be a consistent well-formed top-level environment in which every 𝖼𝖺𝗌𝖾 of the term has one alternative for each constructor of the scrutinee’s type constructor, and let Γ⊢𝖾𝑒:𝜎. Then either 𝑒 is a cvalue, or there is 𝑒′ with 𝑒⟶𝑒′ and Γ⊢𝖾𝑒′:𝜎.
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 Γ⊢𝖾𝑒1𝑒2:𝜎1 and Γ⊢𝖾𝑒1:𝜎2→𝜎1. By the induction hypothesis either 𝑒1 steps, in which case the context 𝐸𝑒2 lifts the step; or 𝑒1 is a cvalue. By lemma 131.30 clause 1 it is then 𝜆𝑥.𝑒3, giving a Beta redex, or (𝜆𝑥.𝑒3)▸𝛾, giving a Push redex.
AppT with Γ⊢𝖾𝑒𝜑:𝜎0[𝜑/𝑎]. Either 𝑒 steps under 𝐸𝜑, or by clause 2 it is Λ𝑎:𝜅.𝑒0, giving TBeta, or (Λ𝑎:𝜅′.𝑒0)▸𝛾. 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 𝑒0 of type 𝑇――𝜐. Either 𝑒0 steps under the context 𝖼𝖺𝗌𝖾𝐸𝗈𝖿――――𝑝→𝑒, or by clause 3 it is 𝐾――𝜎――𝜑――𝑒, giving CaseRed because the alternatives are exhaustive for 𝑇, or it is that application under a cast, giving KPush.
Cast with Γ⊢𝖾𝑒0▸𝛾:𝜏. Either 𝑒0 steps under 𝐸▸𝛾; or 𝑒0 is a plain value, and then 𝑒0▸𝛾 is already a cvalue; or 𝑒0 is 𝑣▸𝛾′, giving a Comb redex. ◻
The term 𝑓=𝜆𝑔:(𝖨𝗇𝗍∼𝖡𝗈𝗈𝗅).1+𝗍𝗋𝗎𝖾▸𝑔 is well typed in a consistent top-level environment: the false equality is a hypothesis of 𝑓, not an axiom. Theorem 131.31 is not violated, because to reduce a call of 𝑓 one must first supply a closed coercion of kind 𝖨𝗇𝗍∼𝖡𝗈𝗈𝗅, and consistency of the top-level environment says that none exists.
𝑥∘=𝑥,𝐾∘=𝐾,(𝜆𝑥:𝜑.𝑒)∘=𝜆𝑥.𝑒∘,(Λ𝑎:𝜅.𝑒)∘=𝜆𝑎.𝑒∘,(𝑒𝜎)∘=𝑒∘(),(𝑒1𝑒2)∘=𝑒∘1𝑒∘2,(𝑒▸𝛾)∘=𝑒∘,(𝐾―――𝑎:𝜅―――𝑥:𝜑)∘=𝐾――𝑎――𝑥, extended homomorphically to 𝗅𝖾𝗍 and 𝖼𝖺𝗌𝖾. 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.
Proof. Clause 1 is immediate: erasure deletes the outer cast of 𝑣▸𝛾 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 (𝑒[𝑒′/𝑥])∘=𝑒∘[𝑒′∘/𝑥], which holds by induction on 𝑒 because erasure is compositional and does not bind. TBeta erases to a beta step at the unit argument, using (𝑒[𝜑/𝑎])∘=𝑒∘[()/𝑎]. Comb, TPush, CPush, Push and KPush all erase to an equality. For Comb and TPush both sides erase to 𝑣∘ and (𝜆𝑎.𝑒∘)(). For CPush and Push, the reduct’s binder is renamed and its body is 𝑒[𝑦▸𝛾1/𝑥], whose erasure is 𝑒∘[𝑦/𝑥]; 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 𝖼𝖺𝗌𝖾(𝐾――𝑎――𝑒∘)𝗈𝖿…. Closure under evaluation contexts follows because 𝐸⟨𝑒⟩∘=𝐸∘⟨𝑒∘⟩ for the erased contexts, where (𝐸▸𝛾)∘=𝐸∘. ◻
For well-typed 𝑒1 in a consistent well-formed top-level environment, 𝑒1⟶∗𝑒2 implies 𝑒∘1⟶∗𝑒∘2, and 𝑒1 reduces to a cvalue if and only if 𝑒∘1 reduces to a value.
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.
★★☆ Compute both sides of one KPush step for 𝖢𝗈𝗇𝗌:∀𝑎:⋆.𝑎→[𝑎]→[𝑎] and Γ⊢𝖢𝖮𝛾:[𝖨𝗇𝗍]∼[𝜏], then erase both. State which clause of theorem 131.12 produces the coercion that casts the tail.
The source language is Hindley–Milner with constrained constructor signatures: 𝜋::=𝜂∣∀𝑎.𝜋,𝜂::=𝜏∣(𝜏∼𝜏)⇒𝜂,𝜏::=𝑎∣𝜏→𝜏∣𝑇――𝜏, and 𝐶::=𝜖∣𝐶,𝑐𝑜:𝜏1∼𝜏2 is a set of named equality assumptions. The judgment 𝐶;Γ⊢𝖦𝑒:𝜋⇝𝑒′ reads: under the assumptions 𝐶 and environment Γ, the source expression 𝑒 has type 𝜋 and elaborates to the 𝐹𝐶𝑟 term 𝑒′.
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 𝐶. That search is decidable, and the algorithm is first-order unification with evidence.
normalize 𝐶 to a solved form ―――――𝛾:𝑎∼𝜐 with the 𝑎 pairwise distinct, ordered, and disjoint from fv(――𝜐), by decomposing each assumption with Right𝑟 and orienting it with Sym and Trans;
normalize the fresh assumption 𝑐𝑜′:𝜏1∼𝜏2 the same way, obtaining ――――――𝛾′:𝑎′∼𝜐′;
match each equation of step 2 against an equation of step 1;
Let 𝐶={𝑐1:[𝑎]∼[𝑏],𝑐2:𝑏∼𝑐} with 𝑎<𝑏<𝑐, and let the goal be [𝑎]∼[𝑐]. Step 1 decomposes 𝑐1 to 𝗋𝗂𝗀𝗁𝗍𝑐1:𝑎∼𝑏 and composes it with 𝑐2, giving the solved form {(𝗋𝗂𝗀𝗁𝗍𝑐1)∘𝑐2:𝑎∼𝑐,𝑐2:𝑏∼𝑐}. Step 2 normalizes the goal to 𝗋𝗂𝗀𝗁𝗍𝑐3:𝑎∼𝑐. Step 3 matches the two. Step 4 reverses the decomposition: since Γ⊢𝖢𝖮[]:[]∼[] by CoRefl, the coercion is 𝛾=[]((𝗋𝗂𝗀𝗁𝗍𝑐1)∘𝑐2), an instance of Comp whose kind is [𝑎]∼[𝑐].
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 𝛿=𝖳𝖸 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 𝛿=𝖢𝖮, 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 𝐹𝐶𝑟 type, its equality constraints becoming coercion binders ―――𝑏:𝜄 with 𝜄 an equality kind, and the pattern binds exactly those binders. ◻
Let Γ be an environment whose domain contains no type variable and no coercion constant. If Γ⊢𝖢𝖮𝛾:𝜎1∼𝜎2 then 𝜎1=𝜎2. Consequently every environment produced by definition 131.37 from a GADT program is consistent.
Proof. Induction on the derivation of the coercion judgment. The two leaf rules are CoRefl, whose conclusion has 𝜎1=𝜎2=𝑑, and CoVar, which is unavailable: a coercion variable 𝑐𝑜 is a type variable and a coercion constant 𝐶 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. CoAllT𝑟: the induction hypothesis in Γ,𝑎:𝜅 does not apply directly, because that environment does contain a type variable; but 𝑎 is bound with a type kind, so CoVar still cannot fire at 𝑎 — 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, CompC𝑟 and EqCoerce𝑟 give 𝜎1=𝜎2 componentwise; Left𝑟, Right𝑟, 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. ◻
★★☆ 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 𝗌𝗒𝗆𝑐𝑜 and which source subterm forces it.
A pure type system with explicit convertibility proofs, written 𝜆𝑓𝑆, also stores in the term the reason two types were identified; the system 𝜆𝑓𝑆 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 Γ⊢𝑓𝑎:𝐴Γ⊢𝑓𝐴′:𝑠Γ⊢𝑓𝐻:𝐴=𝐴′Γ⊢𝑓𝑎𝐻:𝐴′f−Conv, and the erasure |⋅| deleting every 𝐻 satisfies: if Γ⊢𝑓𝐴:𝐵 then |Γ|⊢|𝐴|:|𝐵|, and 𝜆𝑆 and 𝜆𝑓𝑆 are equivalent frameworks. The resemblance to Cast is real but shallow, and the exact differences matter for what can be transferred.
The table also records what is not shared. In 𝜆𝑓𝑆 the equations are generated by one fixed reduction relation and are therefore consistent by confluence; in 𝐹𝐶𝑟(𝑋) they are supplied by the program and consistency is an assumption that a decision procedure must discharge. Conversely 𝜆𝑓𝑆 equates terms of dependent types, which 𝐹𝐶𝑟(𝑋) 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 𝐹𝐶(𝑋) of Sulzmann, Chakravarty, Peyton Jones and Donnelly; the calculus proved about here is the audited system 𝐹𝐶𝑟(𝑋) 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 𝐹𝐶𝑟(𝑋); 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 𝑋. Nothing here bounds the size of coercions; the push rules make them grow, and the simplifications 𝗌𝗒𝗆𝜎=𝜎 and 𝑒▸𝜎=𝑒 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.
★★☆ 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.
★★☆ In a program where 𝑥:𝑇𝑎 and 𝑦:𝑎 and matching 𝑥 against a constructor refines 𝑎 to 𝖨𝗇𝗍, a compiler proposes to float 𝗅𝖾𝗍𝑧=𝑦+1 out of the enclosing abstraction. Write the source and the floated form in 𝐹𝐶𝑟(𝑋), show which typing premise the floated form lacks, and give the repaired transformation that abstracts the binding over the coercion bound by the match.
★★★ Take 𝑒=Λ𝑐𝑜:(𝜎1∼𝖨𝗇𝗍).𝜆𝑥:𝜎1.𝑥▸𝑐𝑜 and a coercion 𝛾′ of kind ((𝜎1∼𝖨𝗇𝗍)⇒𝜎1→𝖨𝗇𝗍)∼((𝜎2∼𝖨𝗇𝗍)⇒𝜎2→𝖨𝗇𝗍). Reduce (𝑒▸𝛾′)𝛾″ 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.
★★★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
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.