Exploring Type Containment
Transcription
Exploring Type Containment
Exploring Type Containment Mid-term exam for MPRI 2-4 course / Examen partiel du cours MPRI 2-4 2015/12/04 — Duration / durée: 2h30 Answers will also be judged by their clarity and concision. Les réponses seront aussi jugées pour leur clarté et leur concision. Our goal is to study type containment, some variations on the type-containment relation, and how it can be extended to other programming language features such as products, side effects, existential types. We will study some language meta-theoretical properties. Notre but est d’étudier le type containment, quelques variantes sur la relation de type containment, et comment elle peut être étendue à d’autres constructions des langages de programmation comme les produits, les effets de bord, les types existentiels. Nous étudierons aussi quelques propriétés de la méta-théorie du langage. 1 Preliminaries / Préliminaires We use the same conventions as in the course. We write a (resp. M ) for implicitly (resp. explicitly) typed expressions. Implicitly typed expressions and types are Nous utilisons les mêmes conventions que dans le cours. Nous écrivons a (resp. M ) pour les expressions implicitement (resp. explicitement) typées. Les expressions implicitement typées et les types sont a ::= x | λx. a | a a σ, τ ::= α | τ → τ | ∀α. τ We recall the typing rules of the implicitly typed version of System F . Nous rappelons les règles de typage pour la version implicitement typée du système F . Sub Var x:τ ∈Γ Γ`x:τ Gen Γ, α ` a : τ Γ ` a : ∀α. τ Abs Γ, x : τ ` a : σ App Γ`a:τ →σ Γ ` a a0 : σ Γ ` λx. a : τ → σ F6 Our presentation is however parameterized by a type-containment relation 6. We write ftv(τ ) for the set of free type variables of τ . For System F , 6 is defined by the following Rule InstGen alone. In the course, we defined F η as F (6) where 6 is the smallest relation satisfying the following rules: Γ ` a0 : τ τ 6σ Γ`a:τ ftv(σ) ∈ dom(Γ) Γ`a:σ F6 Notre présentation est cependant paramétrée par une relation de type containment 6. Nous notons ftv(τ ) l’ensemble des variables libres de τ . Pour le Système F , la relation 6 est définie par la seule règle InstGen cidessous. Dans le cours, nous avons défini F η comme F (6) où 6 est la plus petite relation satisfaisant les règles suivantes : Trans InstGen β̄ ∈ / ftv(∀ᾱ. σ) ∀ᾱ. σ 6 ∀β̄. σ[ᾱ 7→ τ̄ ] Arrow τ0 6 τ σ 6 σ0 0 τ →σ6τ →σ 0 τ1 6 τ2 τ2 6 τ3 Dist-Right τ 6σ ∀α. τ 6 ∀α. σ τ1 6 τ3 ∀α. (τ → σ) 6 τ → (∀α. σ) All α∈ / ftv(τ ) Question 1 Show that Rule Dist-Right is actually an equivalence, i.e. whenever α ∈ / ftv(τ ), we also have τ → ∀α. σ 6 ∀α. (τ → σ). Montrez que la règle Dist-Right est en fait une équivalence, i.e. lorsque α ∈ / ftv(τ ), nous avons également τ → ∀α. σ 6 ∀α. (τ → σ). 1 The kernel of 6 is the equivalence relation (6) ∩ (>), which we write ≈. Le noyau de 6 est la relation d’équivalence (6) ∩ (>), que nous notons ≈. Question 2 Show that ≈ contains the elimination of useless binders, i.e. ∀α. τ ≈ τ whenever α ∈ / τ. Montrez que ≈ contient l’élimination des lieurs inutiles, i.e. ∀α. τ ≈ τ dès que α ∈ / τ. Question 3 What other equivalence (besides those of Question 1 and Question 2) is in ≈ (no proof is required)? Define a rewriting relation that puts types in canonical form for ≈. Quelle autre équivalence (à part celles des Question 1 et Question 2) est dans ≈ (aucune preuve n’est demandée) ? Définissez une relation de réécriture qui met les types en forme canonique pour ≈. We write ftv+ (τ ) (resp. ftv− (τ )) for the subset of free type variables that occur positively (resp. negatively) in τ . We also write ftv⊕ (τ ) for free type variables that occurs only positively in τ , i.e. ftv+ (τ ) \ ftv− (τ ). Nous notons ftv+ (τ ) (resp. ftv− (τ )) le sous-ensemble des variables de types libres qui apparaissent positivement (resp. négativement) dans τ . Nous notons également ftv⊕ (τ ) les variables de types libres qui apparaissent seulement positivement dans τ , i.e. ftv+ (τ ) \ ftv− (τ ). ftv− (α) = ∅ ftv (τ → σ) = ftv+ (τ ) ∪ ftv− (σ) ftv− (∀α. τ ) = ftv− (τ ) \ {α} ftv+ (α) = {α} ftv (τ → σ) = ftv− (τ ) ∪ ftv+ (σ) ftv+ (∀α. τ ) = ftv+ (τ ) \ {α} − + Question 4 We write ⊥ for ∀β. β. Show that τ [α 7→ ⊥] 6 τ Nous notons ⊥ le type ∀β. β. Montrez que if α ∈ / ftv− (τ ) and τ 6 τ [α 7→ ⊥] if α ∈ / ftv+ (τ ) Question 5 In fact, Rule Dist-Right is itself unnecessarily too general and can be replaced by the more restrictive rule Dist-Right-Neg: Dist-Right-Neg Dist-Right-Only-Pos ∀α. (τ → σ) ≤ τ → (∀α. σ) ∀α. (τ → σ) ≤ τ → (∀α. σ) α∈ / ftv(τ ) α ∈ ftv− (σ) Show that Rule Dist-Right-Only-Pos is admissible without using rules Dist-Right nor Dist-Right-Neg. 2 En fait, la règle Dist-Right est elle-même inutilement trop générale et peut être remplacée par la règle plus restrictive Dist-Right-Neg : α∈ / ftv(τ ) α ∈ ftv⊕ (σ) Montrez que la règle Dist-Right-Only-Pos est admissible sans utiliser les règles Dist-Right ni Dist-Right-Neg. Type containment with pairs We now consider an extension of System F with pairs. τ ::= . . . | τ × τ 0 Nous considérons maintenant une extension du Système F avec des paires. Γ ` a1 : τ1 a ::= ... | (a, a) | πi a Γ ` a2 : τ2 Γ ` a1 × a2 : τ1 × τ2 2 Γ ` a : τ1 × τ2 Γ ` πi a : τi Question 6 How would you extend the type containment relation? (Give the new rules without justification.) Comment étendriez-vous la relation de type containment ? (Donnez les nouvelles règles sans justification.) We wish to show that type containment is coherent in the following sense: if τ 6 σ then it cannot be the case that τ and σ have two different top constructors. However, τ or σ may not have a top constructor. So, we define Nous souhaitons montrer que le type containment est cohérent dans le sens suivant : si τ 6 σ alors il n’est pas possible que τ et σ aient deux constructeurs de tête différents. Cependant τ ou σ peuvent ne pas avoir de constructeur de tête. Aussi, nous définissons : top(α) = α top(τ ) = α top(τ ) 6= α top(∀α. τ ) = ⊥ top(∀α. τ ) = top(τ ) top(τ → σ) = (→) top(τ × σ) = (×) Question 7 Show that Montrez que τ 6 σ =⇒ top(τ ) = top(σ) ∨ top(τ ) = ⊥ 3 Type containment and effects The language is equipped with a small-step call-by-value reduction semantics. We now extend the language with references. Hence, we use a store semantics and restrict generalization to non-expansive expressions u. a ::= . . . | c Le langage est muni d’une sémantique à réduction à petits pas en appel par valeur. Nous étendons maintenant le langage avec des références. Aussi, nous utilisons une sémantique avec mémoire et restreignons la généralisation aux expressions non-expansives u. c ::= ` | ref | (!) | (:=) u ::= x | v Question 8 Tell for each constant whether it is a constructor or a destructor. Dites pour chaque constante si c’est un constructeur ou un destructeur. Question 9 Give the grammar for values v. Donnez la grammaire des valeurs v. Question 10 Give an example showing that typecontainment is unsafe in the presence of side effects. (Justify.) Donnez un exemple montrant que le type containment est dangereux en présence d’effets de bords. (Justifiez.)- Question 11 We keep references but remove Rule Dist-Right. Show that the following typing rule is admissible: Nous conservons les références mais retirons la règle Dist-Right. Montrez que la règle de typage suivante est admissible : Gen+ α ∈ ftv⊕ (τ ) Γ, α ` a : τ Γ ` a : ∀α. τ 3 Question 12 We assume that the language F η without Dist-Right is sound in the presence of effects. Is Rule Gen+ sound in System F ? Même question dans ML? 4 Nous supposons que le langage F η sans la règle Dist-Right est sûr en présence d’effets. La règle Gen+ est-elle correcte dans le Système F ? Même question dans ML ? Type containment with existential types We now consider an extension of F η with existential types (ignoring products for the moment): τ ::= . . . | ∃α. τ a ::= . . . | let x = a in a Nous considérons maintenant une extension du F η avec des types existentiels (et ignorons les produits pour l’instant) : Γ ` a : τ [α 7→ τ 0 ] Γ ` a : ∃α. τ Γ, α, x : α ` a0 : τ 0 Γ ` τ0 Γ ` let x = a in a0 : τ 0 Γ ` a : ∃α. τ Question 13 Regarding type containment for existential types, what would you propose for the dual of rules InstGen and All? Concernant le type containment pour les types existentiels, que proposeriez-vous pour les règles duales de InstGen et All ? Question 14 As for distributivity, we could add the equivalence: En ce qui concerne la distributivité, nous pourrions ajouter l’équivalence : (∃α. τ ) → τ 0 ≈ ∀α. (τ → τ 0 ) Justify this equivalence by exhibiting for both directions a term of System F that is βη-equivalent to the identity taking an expression of one type to the other type. Use explicitly typed terms instead of writing their typing derivation. if α ∈ / ftv(τ 0 ) Justifiez cette équivalence en donnant pour chaque direction un terme du système F qui soit βη-équivalent à l’identité et envoie une expression d’un type sur l’autre type. Utilisez des termes explicitement typés plutôt que d’écrire leurs dérivations de typage. Question 15 Can you do the same for the equivalence: Pouvez-vous faire de même pour l’équivalence : ∃α. (τ → τ 0 ) ≈? (∀α. τ ) → τ 0 In the rest of this section, we assume that there are both existential types and products. We ignore distributivity rules. We also assume constants (unit, integers,. . . ). We will use existential types to simulate width subtyping between n-ary products (a1 , . . . an ), which in System F have the following typing rule: Γ ` a1 : τ1 if α ∈ / ftv(τ 0 ) Dans le reste de cette section, nous supposons que nous avons à la fois des types existentiels et des produits. Nous ignorons les règles de distributivité. Nous supposons également que nous avons des constantes (unité, entiers, . . . ). Nous allons utiliser les types existentiels pour simuler le sous-typage en largeur entre des produits n-aires (a1 , . . . an ) qui dans le système F ont la règle de typage suivante : ... Γ ` an : τn Γ ` (a1 , . . . an ) : Π(τ1 , . . . τn ) 4 Question 16 Give the typing rule in System F for the projection πi a that returns the i’th component of an n-ary product a provided i is in 1..n. Donnez la règle de typage dans le Système F pour la projection πi a qui renvoie le i-ème champ du produit n-aire a pourvu que i soit dans 1..n. Has the function (λx. πi x) a principal type in System F? Est-ce que la fonction (λx. πi x) a un type principal dans le Système F ? Question 17 Propose a uniform encoding [[(a1 , . . . an )]] of tuples into pairs so that the encoding of the projection [[πi ]] has a principal type in System F . Give both the encodings and their types. Proposez un codage uniforme [[(a1 , . . . an )]] des tuples dans les paires de telle façon que le codage de la projection [[πi ]] ait un type principal dans le Système F . Donnez à la fois les codages et leurs types. Question 18 Show that the encoding of a tuple with n fields can also be typed in F η as if it were a truncated tuple with only the first i fields. Montrez que le codage d’un tuple à n champs peut aussi être typé dans F η comme s’il avait été tronqué avec seulement ses i premiers champs. Question 19 We wish to model subtyping, but in the absence of polymorphism, for instance to be used in ML. We introduce constants ⊥ for ∀α. α and > for ∃α. α and then restrict type containment to monomorphic types, but we keep ⊥ and >. Give a direct definition of ≤ between monomorphic types. 5 Nous souhaitons modéliser le sous-typage, mais en l’absence de polymorphisme, par exemple pour l’utiliser dans ML. Nous introduisons des constantes ⊥ pour ∀α. α et > pour ∃α. α, puis nous restreignons le type containment aux types monomorphes, mais en conservant ⊥ et >. Donnez une définition directe de la relation ≤ obtenue entre les types monomorphes. Explicit type-containment We now study the meta-theoretical properties of the language, which is easier on its explicitly typed version. We introduce coercions to witness the use of type containment in expressions. For the sake of simplicity, we ignore distributivity. Nous étudions maintenant les propriétés métathéoriques du langage, ce qui est plus facile sur sa version explicitement typée. Nous introduisons des coercions pour témoigner de l’utilisation du type containment dans les expressions. Par souci de simplicité, nous ignorons la distributivité. ϕ, ψ ::= Λ~ α · ~τ | ϕ, τ → ϕ | ϕ × ϕ | ∀α. ϕ | ϕ; ϕ InstGen Arrow Γ ` ϕ : τ0 6 τ ~ ` ~τ Γ ` ∀~ α. σ Γ, β ~ σ[~ Γ ` Λβ~ · ~τ : ∀~ α. σ 6 ∀β. α 7→ ~τ ] Prod Γ ` ϕ : τ 6 τ0 Γ ` ψ : σ 6 σ0 0 Γ`ϕ×ψ :τ ×σ 6τ ×σ 0 All Γ ` ϕ, τ 0 → ψ : τ → σ 6 τ 0 → σ 0 Γ, α ` ϕ : τ 6 σ Γ ` ∀α. ϕ : ∀α. τ 6 ∀α. σ We thus write coercions inside explicitlytyped terms. We include primitive products, but leave constants out for the sake of brevity. Γ ` ψ : σ 6 σ0 Trans Γ ` ϕ : τ1 6 τ2 Γ ` ψ : τ2 6 τ3 Γ ` ϕ; ψ : τ1 6 τ3 Nous écrivons donc des coercions à l’intérieur des termes explicitement typés. Nous incluons des produits primitifs, mais excluons les constantes par souci de concision. 5 Sub M ::= x | λx. M | M M | Λα. M | hϕiM Values and evaluation contexts: Some reduction rules: M1 −→ M2 E[M1 ] −→ E[M2 ] Γ ` ϕ : τ 6 τ0 Γ ` hϕiM : τ 0 Valeurs et contextes d’évaluation : V ::= λx : τ. M | Λα. V | (V, V ) R-Context Γ`M :τ E ::= [ ] M | V [ ] | ([ ], M ) | (V, [ ]) | Λα. [ ] | hϕi[ ] Certaines règles de réduction : R-Beta-Arrow R-Beta-Prod (λx : τ. M ) V −→ M [x 7→ V ] πi (V1 , V2 ) −→ Vi R-Iota-InstGen R-Iota-Arrow ~ V [~ hΛβ~ · ~τ i(Λ~ α. V ) −→ Λβ. α 7→ ~τ ] hϕ : σ → ψi(λx : τ. M ) −→ λx : σ. hψiM [x 7→ hϕix] In the following questions, you may use standard helper lemmas provided you clearly cite them. Dans les questions suivantes, vous pouvez utiliser des lemmes auxiliaires standards à condition de les citer clairement. Question 20 Give the missing reduction rules. Donnez les règles de réduction manquantes. Question 21 State the subject reduction lemma and prove the case Red-Iota-Arrow. Énoncez le lemme d’auto-réduction et prouvez le cas Red-Iota-Arrow. Question 22 State the progress lemma and prove it. Only give the cases for pairs and coercions. Énoncez le lemme de progression et prouvez-le. Ne donnez que les cas des paires et des coercions. Question 23 Show that there cannot be an infinite number of ι-reduction steps without a β-reduction step. Why is this important? Montrez qu’il ne peut pas y avoir une infinité d’étapes de ι-réduction sans étape de β-réduction. Pourquoi estce important ? 6 6 Solutions Question 1 This is proved by the following derivation: InstGen ∀α. σ 6 σ (τ → ∀α. σ) 6 τ → σ InstGen τ → ∀α. σ 6 ∀α. (τ → ∀α. σ) Arrow ∀α. (τ → ∀α. σ) 6 ∀α. (τ → σ) τ → ∀α. σ 6 ∀α. (τ → σ) All Trans Question 2 Both directions are proved with Rule InstGen. In the direct way, we use the substitution [α 7→ α] and β̄ is empty. In the inverse way, we use the empty substitution and β̄ is α. Question 3 The permutation of binders are also part of ≈. We define α∈ / ftv(τ ) α∈ / ftv(τ ) by the following rules: α appears before β in τ τ σ τ → ∀α. σ ∀α. (τ → σ) ∀α. τ τ ∀β. ∀α. τ ∀α. ∀β. τ C[τ ] C[σ] where the order of appearance is from left to right (for example) and C is any type context. Question 4 Let 6+ be 6 and 6− be >. We let range over {+, −} and write ¯ for the inverse of the sign of . We can factor the two cases as τ 6 τ [α 7→ ⊥] if α ∈ / ftv (τ ) The proof is by induction on the size of τ . When α is a variable, the case is empty if is +; otherwise, the conclusion is ⊥ 6 τ which follows from Rule InstGen. If τ if of the form ∀β. σ, we may assume w.l.o.g. that β and α are distinct. We conclude by induction hypothesis applied to σ and rule All. If τ is of the form σ 0 → σ, then α ∈ / ftv¯(σ 0 ) ∪ ftv (σ). By induction hypothesis, we have σ 0 [α 7→ ⊥] 6 σ 0 and σ 6 σ[α 7→ ⊥]; thus we may conclude by rule Arrow. Question 5 Assume α ∈ ftv⊕ (σ), which implies α ∈ / ftv− (σ). We write θ for [α 7→ ⊥] On the one hand, we have σθ 6 σ by Question 4. Thus by All, we have σθ 6 ∀α. σ. (Since since α is not free in ∀α. σθ, we have σθ ≈ ∀α. σθ.) By rule Arrow, we then have τ → σθ 6 τ → (∀α. σ) (4). On the other hand, we have ∀α. (τ → σ) 6 τ → σθ (2) by InstGen. We conclude ∀α. (τ → σ) 6 τ → (∀α. σ) by transitivity of (2) and (4). Question 6 We need a congruence rule and a rule pushing quantifiers inside. (Moving quantifiers outside would be derivable exactly as on the right-hand sides of arrow types.) Prod τ 6 τ0 σ 6 σ0 Dist-Prod ∀α. (τ × σ) 6 (∀α. τ ) × (∀α. σ) τ × σ 6 τ 0 × σ0 7 Question 7 The proof is by induction on the derivation of τ 6 σ. Case InstGen: If top(τ ) is ⊥, then the property holds by definition. Otherwise, if top(τ ) is a variable β, then this variable is not bound and τ2 will be the same variable β, so the property holds. Else, top(τ ) and top(σ) are the same. Case Arrow, Dist-Right: Both sides have the arrow top symbol. Case Prod and Dist-Prod: Both sides have the product top symbol. Case All: By induction hypothesis, the property holds for the premise. If the left-hand side of the premise has the top symbol ⊥, then so does τ . Otherwise both sides of the premise have the same top symbol, and so do τ and σ. Case Trans: The premises are τ 6 ρ and ρ 6 σ. If top(τ ) is ⊥ then the conclusion holds. Otherwise by induction hypothesis, we have top(τ ) = top(ρ) (1), which implies that top(ρ) is not ⊥. Hence by induction hypothesis, we have top(ρ) = top(σ) (2). Combined with (1), this implies top(τ ) = top(σ). Question 8 Locations ` are constructors. All store operations are destructors. Question 9 v ::= λx. a | c | (:=) v Question 10 The expression λx. ref (λy. y) has type ∀α. τ → ref (α → α) for any τ . One may chose τ such that α ∈ / τ, e.g. τ may be int. Then, by Rule Dist-Right, we could assign it type τ → ∀α. ref (α → α), which is unsound, as it would create a polymorphic reference. So Rule Dist-Right is to be blamed. Indeed, applying this function to a value of type τ (e.g. an integer) would return a polymorphic reference of type ∀α. ref (α → α). From there, we could store not of type bool → bool in the reference and read it back as a value of type int → int and apply it to 0, which would lead to the struck program not 0. Question 11 It suffices to show that we can prove the conclusion from the premises without using Dist-Right. Assume Γ, α ` a : τ (1) and α ∈ ftv⊕ (τ ) (2). By type substitution applied to (1) we have Γ ` a : τ [α 7→ ⊥] (3). By Question 4 applied to (2), we have τ [α 7→ ⊥] ≤ τ . Hence, by All, ∀α. τ [α 7→ ⊥] ≤ ∀α. τ (1). Since α does not appear in τ [α 7→ ⊥], we have ∀α. τ [α 7→ ⊥] ≈ τ [α 7→ ⊥], hence τ [α 7→ ⊥] ≤ ∀α. τ (4). By Sub applied to (3) and (4), we have Γ ` a : ∀α. τ . Question 12 This rule is safe in F η without Dist-Right, so it is safe in System F as well, which is a strict subset. For the same reason, it is also safe in ML. It actually implements (a restriction of) the solution called “the relaxed value restriction” used in OCaml. Question 13 Inst-Exi Exi β̄ ∈ / ftv(∃ᾱ. τ ) τ1 6 τ2 ∃β̄. τ [ᾱ 7→ τ̄ ] 6 ∃ᾱ. τ ∃α. τ1 6 ∃α. τ2 8 Question 14 def g = λf : (∃α. τ ) → τ 0 . Λα. λx : τ. f (pack x as ∃α. τ ) def g −1 = λf : ∀α. (τ → τ 0 ). λx : ∃α. τ. let x0 , β = unpack x in f β x0 The erasure of both g and g −1 is the application λf. λx. f x, which is η-equivalent to the identity. Question 15 We can only convert in one direction (namely, from left to right): def g = λf : ∃α. (τ → τ 0 ). λx : ∀α. τ. let f 0 , α = unpack f in f 0 (x α) Its type erasure is only βη-equivalent to the identity (as one linear let-binding must be reduced). However, the other direction cannot be defined. Question 16 Γ ` a : Π(τ1 , . . . τn ) i ∈ 1..n Γ ` πi a : τi Question 16 (continued) No, because we do not know how many fields the argument may contain, so we need an infinite collection of types: ∀α1 . . . αn . Π(α1 , . . . αi , . . . αn ) → αi n≥i Question 17 We write ”()” for the unit value of type unit. We add a unit extra field to every tuple and hide the type of its last component. [[Π(a1 , . . . an )]] [[πi ]] def = def = (a1 , (a2 , . . . (an , ()))) : τ1 × (τ2 . . . × (τn × unit)) fst ◦ sndi : ∀α1 . . . αn β. α1 × (α2 . . . × (αn × β)) → αi Question 18 We write > for ∃α. α. The unit value may always be considered of type >. Hence, the encoding of a tuple can also be given type τ1 × (τ2 . . . × (τn × >)). Moreover, the tail of the product can always be hidden by type-containment as follows: InstExi τp+1 . . . × > ≤ > Prod Prod τp × (τp+1 × . . . (τn × >)) 6 τp × > .. . τ1 × . . . (τp × (τp+1 × . . . (τn × >))) 6 τ1 × . . . (τp × >) Question 19 It is the smallest relation satisfying the following rules: α≤α ⊥≤τ τ ≤> τ1 0 ≤ τ1 τ2 ≤ τ2 0 τ1 ≤ τ1 0 τ2 ≤ τ2 0 τ1 → τ2 ≤ τ1 0 → τ2 0 τ1 × τ2 ≤ τ1 0 × τ2 0 (Adding the transitivity rule is correct, but it is unnecessary as transitivity is provable.) 9 Question 20 R-Iota-Prod R-Iota-All R-Iota-Trans hϕ1 × ϕ2 i(V1 , V2 ) −→ (hϕ1 iV1 , hϕ2 iV2 ) h∀α. ϕi(Λα. V ) −→ Λα. hϕiV hϕ1 ; ϕ2 iV −→ hϕ1 ihϕ2 iV2 Question 21 Assume Γ ` M0 : τ0 and M0 −→ M0 0 . We show that Γ ` M0 0 : τ0 by induction on the proof of reduction. Case R-Iota-Arrow: Then M0 is hϕ, σ → ψi(λτ : x. M ) and M0 0 is λy : σ. hψiM [x 7→ hϕiy]. and τ0 is σ → σ 0 . By inversion of typing, we have Γ, x : τ ` M : τ 0 , Γ ` ϕ : σ → τ (2), and Γ ` ψ : τ 0 → σ 0 . By weakening (which we assume extends to coercion judgments as well), we have Γ, x : τ, y : σ ` M : τ 0 (1), Γ, x : τ, y : σ ` ϕ : σ → τ (2), and Γ, y : σ ` ψ : τ 0 → σ 0 (3). By Var applied to (1) then Sub applied with (3), we have Γ, x : τ, y : σ ` hϕiy : τ (4). By the substitution lemma for terms applied to (1) and (4), we have Γ, y : σ ` M [x 7→ hϕiy] : τ 0 . Applying Sub with (3), we obtain Γ, y : σ ` hψiM [x 7→ hϕiy] : σ 0 . The conclusion follows by Rule Abs. Question 22 Assume that Γ ` M : τ where Γ is reduced to type variables and M is irreducible. We show that M is a value V by induction on M . Case M is πi M1 : By inversion of typing, Γ ` M1 : τ1 × τ2 where τ is τi . Since πi [ ] is an evaluation context, M1 must be irreducible. By induction hypothesis, it must be a value. By classification, it must be a pair. Then M would reduce, so this case is not possible. Case M is hϕiM 0 : By inversion Γ ` M 0 : τ 0 for some type τ 0 . Since hϕiE is an evaluation context, M 0 is irreducible as well. By induction, M 0 is a value V , whose shape is determined by τ 0 . We show by induction on ϕ that V is such that some reduction rule applies to M , which is a contradiction and implies that this case cannot occur. Subcase ϕ is Λβ~ · ~τ . Then τ 0 is of the form ∀~ α. σ and thus V is of the form Λ~ α. V 0 . M reduces by R-Iota-InstGen. Subcase ϕ is ϕ1 , σ → ϕ2 . Then τ 0 is an arrow type and thus V is an abstraction. M reduces by R-Iota-Arrow. Subcase ϕ is ϕ1 × ϕ2 . Then τ 0 is a product type and thus V is a pair. M reduces by R-Iota-Prod. Subcase ϕ is ∀α. ϕ0 . Then τ 0 is a type abstraction and V is of the form Λα. V 0 . M reduces by R-Iota-All. Subcase ϕ is ϕ1 ; ϕ2 . Then M unconditionally reduces by R-Iota-Trans. Question 23 ι-reduction consumes a coercion ϕ and when it introduces a new coercion inside the term it is a strictly sub-coercion of ϕ. Formally, we count the number of coercions appearing inside terms, including subcoercions of coercoins. Then, this number strictly decreases during reduction. It is important that ι-reduction terminates for the semantics to be type erasing. 10