Concepts of Program Design Featherweight Java
Overview higher & first-order syntax inference rules, induction tools to talk about languages abstract machines big step and small step operational semantics λ -calculus and de Bruijn indices value & type environments parametric polymorphism/ object oriented control stacks generics semantic features partial application/function closures functional (algebraic) data types static & dynamic static & dynamic typing scoping language concepts explicit & implicit procedural/imperative typing
Object oriented languages • What are the characteristics of an object oriented language? - objects bundle data and behaviour ‣ the object knows what to do - inheritance ‣ subclass relationship in the type system - method overloading as a form of polymorphism - abstraction (or encapsulation): ‣ hiding implementation details from the user, interaction through restricted interface
Object oriented languages • We will look a these language features today (apart from abstraction) using Featherweight Java, a simple object oriented language
Featherweight Java • Minimal OO language Classes c ::= class c extends c { c f ; k d } Constructors k ::= c ( c x ) {super ( x ); this. f = x ;} Methods d ::= c m ( c x ) {return e ;} Types τ ::= c Expressions e ::= x | e.f | e . m ( e ) | new c ( e ) | ( c ) e ‣ f : field name ‣ c : class name ‣ m : method name ‣ x : variable name ‣ e : abbreviation for e 1 , e 2 ,…. ‣ c x : abbreviation for c 1 x 1 ; …. • TinyC like expressions and statements could be added to make it a proper language - would not add anything of interest for our purposes
Featherweight Java • Class expressions class c extends c’ {c 1 f 1 ; c 2 f 2 ; … k d 1 d 2 … } declares a class • c to be a subclass of c’ • with additional fields c 1 f 1 • a single constructor k • methods d i
Featherweight Java • Constructor expressions c (c 1 ’ x 1 ’ ;…;c 1 x 1 ;…){ super (x 1 ’,…) this. f 1 = x 1 ; this. f 2 = x 2 ; … } declares a constructor for a class • with arguments c ’ 1 x ’ 1 corresponding to a superclass • with arguments c 1 x 1 corresponding to the new fields of the subclass • x ’ 1 are initialised via the superclass • this. f i = x i fields initialised in the subclass
Featherweight Java • Method expressions c m ( c 1 x 1 , c 2 x 2 , …) {return e ;} declares a method m • which returns a value of class c • with arguments x i of class c i • and a body returning the value of expression e
Featherweight Java • Field selection e . f select a field f from instance e • Method invocation e . m ( e 1 , e 2 , … ) invoke a method m of instance e with arguments e 1 , e 2 , …
Featherweight Java • Instance creation new c ( e 1 , e 2 , … ) creates new instance of class c with arguments e 1 , e 2 , … • Casting ( c ) e casts a value e to class c
Featherweight Java • Types - the set of types is limited to the set of class names - in examples, we assume the presence of types like int and bool , but we will not discuss the semantics of these types there is a class Object - all other classes are subclasses of Object a special variable this referring to the instance itself
Subclasses and Subtypes • Subclass/superclass relationship - similar to the subtype/supertype relationship - if c is a subclass of c’, the c has at least as many fields as c’ - objects of class c can be coerced to c’ by deleting the additional fields - we write c <: c’ to denote the subclass relationship
class Point extends Object { class Colour extends Object { int x; int red; int y; int green; int blue; Point (int x, int y) { super (); Colour (int r, int g, int b) { this.x = x; super (); this.y = y; this.red = r % 256; } this.green = g % 256; } this.blue = b % 256; class ColourPoint extends Point { } Colour c; } ColourPoint (int x, int y, Colour c) { super (x, y); this.c = c; } Colour getc () {return this.c;} }
Static Semantics • The static semantics is defined by the following judgements: subclass relationship τ <: τ ′ � Γ ⊢ e : τ expression typing d ok in c well formed method C ok well formed class T ok well formed class table fields ( c ) = { c 1 f 1 , c 2 f 2 , …} field lookup type ( m , c ) = c → c method type • A program consists of a class table T (sequence of class declarations) and an expression e. • We use the class table T as implicit parameter in the following. • We only look at some interesting aspects of the static semantics.
Static Semantics • Fields - we define a judgement to determine the field names and the types of a given class: fields ( className ) = className 1 fieldName 1 , … fields ( 홾횋횓횎회횝 ) = ∙ T ( c ) = 회횕횊횜횜 c 횎횡횝횎횗획횜 c ′ � { c f ; …} fields ( c ′ � ) = c ′ � f ′ � fields ( c ) = c ′ � f ′ � , c f fields ( 홿횘횒횗횝 ) = 횒횗횝 횡 , 횒횗횝 횢 fields ( 홲횘횕횘횞횛홿횘횒횗횝 ) = 횒횗횝 횡 , 횒횗횝 횢 , 홲횘횕횘횞횛 회
Static semantics • Typing judgment for expressions of the language Γ ⊢ e : τ - Every variable has to be declared x : τ ∈ Γ Γ ⊢ x : τ - Types of fields are defined in the class table Γ ⊢ e 0 : c 0 fields ( c 0 ) = c 1 f 1 … Γ ⊢ e 0 . f i : c i
Static Semantics • Argument and result types of methods are defined in the class table - methods of super types can be applied without cast Γ ⊢ e 0 : c 0 Γ ⊢ e : c type ( m , c 0 ) = c ′ � → c 1 c <: c ′ � Γ ⊢ e 0 . m ( e ) : c 1 • Instantiation fields ( c ) = c ′ � f Γ ⊢ e : c c <: c ′ � Γ ⊢ 횗횎횠 c ( e ) : c
Static Semantics • All casts are statically valid (could be more restrictive) Γ ⊢ e 0 : c ′ � Γ ⊢ ( c ) e 0 : c
Static Semantics • Subclass relationship τ <: τ τ 1 <: τ 2 τ 2 <: τ 3 τ 1 <: τ 3 T ( c ) = 회횕횊횜횜 c 횎횡횝횎횗획횜 c ′ � {…} c <: c ′ �
Static Semantics • Well-formed classes: fields of super fields of new class class k = c ( c ′ � x ′ � , cx ){ 횜횞횙횎횛 ( x ′ � ; 횝횑횒횜 . f = x ; } fields ( c ′ � ) = c ′ � f ′ � m ok in c 회횕횊횜횜 c extends c ′ � { c f ; k m } ok constructor methods
Static Semantics k = c ( c ′ � x ′ � , cx ){ 횜횞횙횎횛 ( x ′ � ; 횝횑횒횜 . f = x ; } fields ( c ′ � ) = c ′ � f ′ � m ok in c 회횕횊횜횜 c 횎횡횝횎횗획횜 c ′ � { c f ; k m } ok c c’ class ColourPoint extends Point { c f Colour c; k ColourPoint (int x, int y, Colour c) { super (x, y); this.c = c; } m Colour getc () {return this.c;} }
Static Semantics • Method overriding: new subclass methods must have - the same argument type - the same result type as the superclass method T ( c ) = 회횕횊횜횜 c 횎횡횝횎횗획횜 c ′ � {…} type ( m , c ′ � ) = c → c 0 x : c ; 횝횑횒횜 : c ⊢ e 0 : c 0 c 0 m ( c x ){ 횛횎횝횞횛횗 e 0 }; ok in c
Static Semantics To find the type of a method m of a class c, we have to search upwards in the class hierarchy for the definition of m. T ( c ) = 회횕횊횜횜 c 횎횡횝횎횗획횜 c ′ � {…; … c ′ � ′ � } c ′ � ′ � i = c i m i ( c i x ){ 횛횎횝횞횛횗 e } type ( m , c ) = c i → c i type ( m , c ′ � ) = c i → c i T ( c ) = 회횕횊횜횜 c 횎횡횝횎횗획횜 c ′ � {…; … c ′ � ′ � } m ∉ c ′ � ′ � type ( m , c ) = c i → c i
Static Semantics • Properties - casts may fail at run time: checks required - method invocation is statically checked - field selection is statically checked
Dynamic Semantics • We discuss parts of the single step or structural operational semantics of Featherweight Java • Values: an instance is a value if all of it’s arguments are values v value 횗횎횠 c ( v ) value in essence, an instance is just a collection of named fields, labelled with class names
Dynamic Semantics • Field Selection - c i ’ f i ’: fields of a superclass - c i f i : fields of a class itself - retrieve values of field from either superclass or class itself fields ( c ) = c ′ � f ′ � ; c f ; 횗횎횠 c ( v ′ � , v ) . f ′ � i ↦ v ′ � i fields ( c ) = c ′ � f ′ � ; c f ; 횗횎횠 c ( v ′ � , v ) . f j ↦ v j Note: contents of fields cannot be changed after initialisation. We could extend the language using the techniques described previously
Dynamic Semantics • Method Invocation - method invocation relies on an auxiliary predicate, body (defined later) which provides the list of formal arguments and the body of a method m defined in a class c - replace all formal parameters x by actual parameters v ’ - replace every occurrence of this by the instance itself body ( m , c ) = x → e 0 횗횎횠 c ( v ) . m ( v ′ � ) ↦ e 0 [ x := v ′ � , 횝횑횒횜 := 횗횎횠 c ( v )]
Dynamic Semantics • Type Cast - if c’ is a super type of c, the cast is just ignored - otherwise, cause a checked run-time error c <: c ′ � c ≮ : c ′ � ( c ′ � ) 횗횎횠 c ( e ) ↦ 횗횎횠 c ( e ) ( c ′ � ) 횗횎횠 c ( e ) ↦ 횎횛횛횘횛 • Evaluation order - usual rules to determine evaluation order
Recommend
More recommend