Docsity

Featherweight Java Syntax and Type Rules, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Featherweight Java Syntax and Type Rules and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Featherweight Java

Syntax:

P ::= CL e program CL ::= class C extends C {C f ; K m} class definition K ::= C (C f ){super (f ); this.f = f ; } constructor M ::= C m(C x){return e; } method e ::= expressions x variables e.f field access e.m(e) method invocation new C (e) object creation (C )e cast

Notes: C is for class names, m is for method names, and f is for field names. This syntax is literally a subset of Java. A Featherweight Java program can be run by a standard Java interpreter.

Subtyping

C <: C

C <: D D <: E

C <: E

CT (C ) = class C extends D{.. .} C <: D

More Auxiliary Functions

mtype(m, C) = let CT(C) = class C extends D {C f ; K M} if B m(B x){return e; } โˆˆ M then return B โ†’ B else return mtype(m, D)

override(m,D,C โ†’ C 0 ) = if mtype(m,D) = D โ†’ D 0 then return C = D and C 0 = D 0 else return true

Evaluation

Values: v ::= new C (v )

Evaluation contexts: E ::= [] | E .f | E .m(e) | v .m(v , E , e) | new C (v , E , e) | (C )E

Reduction rules:

(E-Proj)

fields(C ) = C f new C (v ).fi โˆ’โ†’ vi

(E-Invk)

mbody (m, C ) = (x, e) (new C (v )).m(u) โˆ’โ†’ [x 7 โ†’ u, this 7 โ†’ new C (v )]e

(E-Cast) C^ <:^ D (D)(new C (v )) โˆ’โ†’ new C (v )

Type Rules for Methods and Classes

M OK in C

x : C , this : C ` e : Ce Ce <: Cr CT (C ) = class C extends D{.. .} override(m, D, C โ†’ Cr ) Cr m(C x){return e; } OK in C

C OK

K = C (D g , C f ){super (g ); this.f = f ; } fields(D) = D g M OK in C class C extends D{C f ; K M} OK

Properties

Lemma (Preservation)

If ฮ“ e : C and e 7 โˆ’โ†’ eโ€ฒ^ then ฮ“ eโ€ฒ^ : C โ€ฒ^ for some D where C โ€ฒ^ <: C.

Definition

A bad cast is an expression of the form (C )(new D(v )) where D 6 <: C.

Lemma (Progress)

Suppose e is closed, well-typed, and in normal form. Then either e is a value or there is an evaluation context E such that e = E [b] where b is a bad cast.