






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Explicit casts and coercion semantics in the context of a simply typed lambda calculus with support for subtyping and exceptions. It covers the syntax, type rules, evaluation contexts, reduction rules, and coercion functions associated with subtyping. The document also touches upon the implementation of a type checker for subtyping and handling conditional expressions.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







The basis for this extension is the simply typed lambda calculus with support for subtyping and exceptions. Syntax: e ::=... | e as T
Type rule: Γ e : S Γ e as T : T Evaluation contexts:
E ::=... | E as T
Reduction rule:
` v : T v as T −→ v
6 ` v : T v as T −→ raise “cast error “
Syntax: e ::=... | if e in T then x. e else e Type rule: Γ e 1 : S Γ, x : T 1 e 2 : T 2 Γ e 3 : T 2 Γ if e 1 in T 1 then x. e 2 else e 3 : T 2 Evaluation contexts: E ::=... | if E in T 1 then x. e 2 else e 3
Reduction rules: ` v : T 1 if v in T 1 then x. e 2 else e 3 −→ [x 7 → v ]e 2
6 ` v : T 1 if v in T 1 then x. e 2 else e 3 −→ e 3
For each subtype derivation, we’ll associate a function that coerces a value of the left-hand type to a value of the right-hand type. Given a derivation D, we write JDK for the coercion function.
[[ Bool <: Bool
]] = λx : Bool. x [[ Int <: Float
]] = intToFloat [[ D 1 :: T 1 <: S 1 D 2 :: S 2 <: T 2 S 1 → S 2 <: T 1 → T 2
]] = λf : S 1 → S 2. λx : T 1. ([D 2 ] (f ([D 1 ] x)))
dom(R 2 ) ⊆ dom(R 1 ) ∀l ∈ dom(R 2 ). R 1 (l) = R 2 (l) R 1 <: R 2
=^ λr^ :^ R^1.^ {l^ =^ r^ .l l∈dom(R 2 )}
.. .
Define a translation from the language with subtyping to the language with explicit coercions:
Γ ` e ⇒ e : T
The translation rule corresponding to subsumption inserts a coercion. Γ e ⇒ e′^ : S D :: S <: T Γ e ⇒ (JDK e′) : T
I (^) The basic idea is to sprinkle uses of <: in all the places that you would use type equality. I (^) For example, in function application:
Γ e 1 : T 11 → T 12 Γ e 2 : T 2 T 2 <: T 11 Γ ` (e 1 e 2 ) : T 12
I (^) There’s also a problem with the definition of subtyping: we don’t know when, or in what order, to apply the record width and depth rules.
dom(R 2 ) ⊆ dom(R 1 ) ∀l ∈ dom(R 2 ). R 1 (l) = R 2 (l) R 1 <: R 2
dom(R 1 ) = dom(R 2 ) for l ∈ dom(R 1 ). R 1 (l) <: R 2 (l) R 1 <: R 2 I (^) These two rules can be repaced by the following rule:
dom(R 2 ) ⊆ dom(R 1 ) for l ∈ dom(R 2 ). R 1 (l) <: R 2 (l) R 1 <: R 2
If R <: S and S <: T then R <: T. Proof. by induction on S. Case S = Int. Then R = Int and T = Int or T = Top. In either case, R <: T. Case S = Bool. Similar. Case S = Top. So T = Top and then R <: T.
Case S = S 1 → S 2. Then R = R 1 → R 2 with S 1 <: R 1 and R 2 <: S 2 , and either (a) T = T 1 → T 2 with T 1 <: S 1 and S 2 <: T 2 or else (b) T = Top. The following diagram shows the proof for case (a): R 1 //R 2
A $
AAA
AAA AAA AA AA (^) IH 2)
}^ :^ B }}} }} }
}} }}} }} S 2
A $ AA
AAA A AAA AA AA
}^ :^ B }}} }}}
}} }}} }}
IH 1)
11
For case (b), because T = Top we immediately have R <: T.