Explicit Casts and Coercion Semantics in Simply Typed Lambda Calculus, Study notes of Electrical and Electronics Engineering

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

Pre 2010

Uploaded on 02/13/2009

koofers-user-euq-1
koofers-user-euq-1 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Explicit Casts
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
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Explicit Casts and Coercion Semantics in Simply Typed Lambda Calculus and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Explicit Casts

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 “

Type Tests (alternative to using exceptions)

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

Coercion Semantics for Subtyping

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 )}

.. .

Coercion Semantics for Subtyping

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

Syntax Directed Type System

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

Algorithmic Subtyping

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

Transitivity of Subtyping

Proposition

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.

Transitivity of Subtyping, continued

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)

 

S 1 //

}^ :^ B }}} }} }

}} }}} }} S 2

A  $ AA

AAA A AAA AA AA

T 1 //

}^ :^ B }}} }}}

}} }}} }}

IH 1)

11

T 2

For case (b), because T = Top we immediately have R <: T.