3.1 Type Constructors and Kinds
3.1.7 Deciding Constructor Equivalence
The Stone-Harper algorithm for deciding constructor equivalence is shown in Figures 3.8 and 3.9. It comprises a number of interlocking judgments, on which I will attempt now to impose some narrative structure.
First of all, suppose we are given two well-formed constructors C1 and C2 to be compared
at kind K in context ∆. The main judgment ∆ ` C1 ⇔ C2 : K determines whether they are
equivalent by dividing the problem into a series of subproblems at base kinds. This makes sense due to extensionality: C1 and C2 are equivalent at pair kind precisely when their first projections
are equivalent and their second projections are equivalent, and they are equivalent at arrow kind precisely when, for any argument αof the domain kind, C1(α) is equivalent at C2(α) at the result
kind. At the unit and singleton base kinds, the algorithm trivially returns a positive answer because all constructors are equivalent at one of those kinds. At kind T, however, we must actually look at the constructors!
Elimination Contexts E ::= • | E(C)| πiE Constructor Paths P ::= b| E{α}
Natural kind extraction: ∆`P↑K ∆`b↑T
∆`α↑∆(α)
∆`P(C)↑K00[C/α] if ∆`P↑Πα:K0.K00
∆`π1P↑K0 if ∆`P↑Σα:K0.K00
∆`π2P↑K00[π1P/α] if ∆`P↑Σα:K0.K00
Weak head reduction: ∆`C1 −→wh C2
∆` E{(λα:K0.C)C0}−→ E{wh C[C0/α]}
∆` E{π1hα= C0,C00i} −→ E{wh C0}
∆` E{π2hα= C0,C00i} −→ E{wh C00[C0/α]}
∆`P−→wh C if ∆`P↑
s
(C)Weak head normalization: ∆`C=wh⇒D
∆`C=wh⇒D if ∆`C−→wh C0 and ∆`C0 =wh⇒D
∆`C=wh⇒C otherwise
Figure 3.8: Weak Head Normalization for Type Constructors
When comparing two constructors at kind T, the algorithm first reduces the constructors to weak head normal form (WHNF) [62]. Since the constructors have kindT, their WHNF’s will not beλ-abstractions, but rather paths, whose syntax is described at the top of Figure 3.8. A path P is either a base type or a sequence of eliminations (i.e., projections and applications) rooted at a constructor variable. The notation E{C} used in the definition of paths in Figure 3.8 signifies the substitution of C into the single hole • in the elimination contextE.
The first three rules in the weak head reduction judgment ∆ ` C1 −→wh C2 are completely
standardβ-reduction. The fourth rule is non-standard—it says that being a path is not equivalent to being in WHNF; to be in WHNF a path must also be abstract. For example, if α is bound in the context with kind T, then α is an abstract type. If α is bound with
s
(C), however, thenα is transparently equal to C and may thus be reduced to it. One can think of this reduction step as “looking up the definition of a type variable.” Whether a path has a “definition” or not is determined by a judgment called “natural kind extraction” and written ∆`C↑K. Intuitively, the natural kind of a constructor is the kind you would synthesize for it if the selfification rules did not exist. This intuition is reflected in the following fact, connecting principal and natural kinds.
Proposition 3.1.17 (Connection Between Natural and Principal Kinds)
If ∆`P⇒K, then ∆`P↑L, ∆`P : L, and K =
s
L(P).3.1. TYPE CONSTRUCTORS AND KINDS 53
Algorithmic kind equivalence: ∆`K1 ⇔K2
∆`1⇔1 ∆`T⇔T
∆`
s
(C1)⇔s
(C2) if ∆`C1 ⇔C2:T∆`Πα:K01.K100⇔Πα:K02.K002 if ∆`K01 ⇔K02 and ∆, α:K01`K001 ⇔K002 ∆`Σα:K01.K100⇔Σα:K02.K002 if ∆`K01 ⇔K02 and ∆, α:K01`K001 ⇔K002
Algorithmic constructor equivalence: ∆`C1⇔C2 : K
∆`C1 ⇔C2 : 1 ∆`C1 ⇔C2 :
s
(C) ∆`C1 ⇔C2 :T if ∆`C1 =wh⇒P1, ∆`C2=wh⇒P2, and ∆`P1↔P2↑T ∆`C1 ⇔C2 : Πα:K0.K00 if ∆, α:K0 `C1(α)⇔C2(α) : K00 ∆`C1 ⇔C2 : Σα:K0.K00 if ∆`π1C1⇔π1C2: K0 and ∆`π2C1 ⇔π2C2: K00[π1C1/α]Algorithmic path equivalence: ∆`P1 ↔P2 ↑K
∆`α↔α↑∆(α) ∆`unit↔unit↑T ∆`C01×C001 ↔C02×C002 ↑T if ∆`C01 ⇔C02:T and ∆`C001 ⇔C002 :T ∆`C0 1→C001 ↔C02→C002 ↑T if ∆`C01 ⇔C02:T and ∆`C001 ⇔C002 :T ∆` ∀α:K1.C1↔ ∀α:K2.C2↑T if ∆`K1 ⇔K2 and ∆, α:K1`C1⇔C2: T ∆` ∃α:K1.C1↔ ∃α:K2.C2↑T if ∆`K1 ⇔K2 and ∆, α:K1`C1⇔C2: T ∆`P1(C1)↔P2(C2)↑K00[C1/α] if ∆`P1↔P2 ↑Πα:K0.K00 and ∆`C1⇔C2 : K0 ∆`π1P1↔π1P2 ↑K0 if ∆`P1↔P2 ↑Σα:K0.K00 ∆`π2P1↔π2P2 ↑K00[π1P1/α] if ∆`P1↔P2 ↑Σα:K0.K00
Figure 3.9: Equivalence Algorithm for Constructors and Kinds
(namely, C). It is worth noting that this is the only place in the whole equivalence algorithm where the context ∆ is actually consulted.
Finally, now that we have reduced C1 and C2 to WHNF’s P1 and P2, we compare the two paths
structurally with the judgment ∆ ` P1 ↔ P2 ↑ K. In several cases, structural path comparison
requires recursive calls to the main equivalence judgment when it encounters subterms, such as function arguments, that are not necessarily paths. The kind K in the path equivalence judgment is the natural kind of P1. It is used in the function application case to synthesize the kind K0 at
which the arguments C1 and C2 are to be compared.
The proof that this algorithm is sound is fairly straightforward. The proof that it is complete, however, is quite complicated, the chief difficulty being that the algorithm itself is not obviously symmetric or transitive! Specifically, in the pair kind case of the main equivalence judgment, the second recursive call compares π2C1 and π2C2 at the kind K00[π1C1/α]. This does not clearly
imply that the two constructors are also algorithmically equivalent at the kind K00[π1C2/α], which
application and second projection cases of path equivalence.
This problem seems to require one to come up with a variant of the algorithm that is equivalent to it but more obviously symmetric and transitive. Stone and Harper have proposed two such variants. The first, described in their POPL paper [74], overcomes the asymmetries of the original algorithm by working with two equivalent contexts (∆1 and ∆2) and two equivalent kinds (K1 and
K2) in addition to the two constructors. The idea is to divide the algorithmic judgments into two
halves, such that each Ci only ends up “infecting” the kind Ki and context ∆i on its own half of the judgment. The proof that this algorithm is complete involves a Kripke-style logical relations argument that is fairly straightforward aside from the fact that, like the algorithm, it also deals with two contexts and two kinds.
The clunky nature of the six-place algorithm leads its proof of completeness to be rather ver- bose. More recently, Stone discovered an alternative algorithm/proof that is, I believe, much easier to follow. While structured like the original algorithm, it takes the form of a normalization pro- cedure for constructors that is both context- and kind-dependent. Two constructors are deemed equivalent if they have the same normal form, so symmetry and transitivity fall out trivially. More interesting is the logical relation used, which has the form C in K [D], whereD, C andK are sets
of contexts, constructors and kinds, respectively. The logical relation has the property that all of the constructors inC, when compared at any of the kinds inK and under any of the contexts inD, have the same normal form. The strengthened induction hypothesis implied by this logical relation results in a completeness proof that is considerably more elegant and readable than the original. It is described in detail in Stone and Harper’s forthcoming journal version of their paper [75].
Given soundness and completeness, it is not hard to show that the equivalence algorithm is decid- able. One consequence of decidability is that all well-formed constructors have (unique) WHNF’s. This is useful particularly when proving decidability oftype synthesis (see Section 3.2.4 below).
Theorem 3.1.18 (Soundness, Completeness and Decidability of Equivalence Algorithm)
1. If ∆`K1 kind and ∆`K2 kind, then ∆`K1≡K2 if and only if ∆`K1 ⇔K2,
which is decidable.
2. If ∆`C1: K and ∆`C2 : K, then ∆`C1 ≡C2 : K if and only if ∆`C1 ⇔C2: K,
which is decidable.
3. If ∆`P1 : K1 and ∆`P2 : K2 and ∆`P1↔P2 ↑K, then ∆`P1 ≡P2: K.
Proposition 3.1.19 (Well-Formed Constructors Have Weak Head Normal Forms)
If ∆`C : K, then there exists a unique D such that ∆`C=wh⇒D, and moreover ∆`C≡D : K.