62
Imperative Programming: Some Loose Ends A Virtual Machine for imPL Object-Oriented Programming A Realistic Object System Meta-Programming 10—Object-Oriented Programming CS4215: Programming Language Implementation Martin Henz March 22, 2011 Generated on Saturday 24 March, 2012, 11:16 CS4215: Programming Language Implementation 10—Object-Oriented Programming

10—Object-Oriented Programming

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

10—Object-Oriented Programming

CS4215: Programming Language Implementation

Martin Henz

March 22, 2011

Generated on Saturday 24 March, 2012, 11:16

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 2: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

1 Imperative Programming: Some Loose EndsDenotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

2 A Virtual Machine for imPL

3 Object-Oriented Programming

4 A Realistic Object System

5 Meta-Programming

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 3: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Semantic Domains

Domain name Definition

EV Int + Bool + Fun + Rec + {⊥}SV Int + Bool + Fun + Rec

DV Loc

Rec Id Loc

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 4: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Function Application

Σ | ∆ E1 (f ,Σ′) Σ′ | ∆ E2 (v2,Σ′′)

Σ | ∆ (E1 E2) f (l ,Σ′′[l ← v2])

where l is a new location in Σ′′.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 5: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Property Access

Σ | ∆ E (f ,Σ′)

Σ | ∆ E .q (Σ′(f (q)),Σ′)

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 6: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Property Assignment

Σ | ∆ E1 (f ,Σ′) Σ′ | ∆ E2 (v ,Σ′′)

Σ | ∆ E1.q:=E2 (v ,Σ′′[f (q)← v ])

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 7: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

imPL: Passing Records to Functions

let a = [Myfield: 0]

in

(fun b ->

b.Myfield := 1;

b := [Myfield: 2];

b.Myfield := 3

end

a);

a.Myfield

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 8: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Alternative: Pass-by-Reference

Σ | ∆ E1 (f ,Σ′)

Σ | ∆ (E1 x) f (∆(x),Σ′)

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 9: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Alternative: Pass-by-Copy

Σ | ∆ E1 (f ,Σ′) Σ′ | ∆ E2 (v2,Σ′′)

Σ | ∆ (E1 E2) f (l ,Σ′′′)

if v2 ∈ Rec,where l , l ′1, . . . , l

nare new locations in Σ′′,

{q1, . . . , qn} = dom(v2),Σ′′′ = Σ′′[l ← {(q1, l

1), . . . , (qn, l′

n)}]

[l ′1 ← Σ′′(v2(q1))] . . . [l′

n← Σ′′(v2(qn))]

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 10: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

A Naive Interpreter

Idea

Translate denotational semantics to a high-level language, in ourcase Java.

Example

public class Sequence implements Expression {

public Expression firstPart, secondPart;

public StoreAndValue eval(Store s, Environment e) {

StoreAndValue s_and_v_1 = firstPart.eval(s, e);

return secondPart.eval(s_and_v_1.store,e);

}

}

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 11: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Naive Implementation of Store

public class Store extends Vector<Value> {

public Store extend(int location, Value value) {

Store newStore = (Store) clone();

newStore.add(location,value);

return newStore;

}

}

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 12: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Observation

Threading of store

The store is threaded through the execution of the program. Atany point in time, only one store is in use. Older stores areinaccessible.

Idea

Use only one store, and physically change it upon assignment.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 13: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Realistic Implementation of Store

public class Store extends Vector<Value> {

public Store set(int location, Value value) {

add(location,value);

return this;

}

public static Store theStore = new Store();

}

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 14: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Naive Implementation of Assignment

public StoreAndValue eval(Store s, Environment e) {

StoreAndValue s_and_v = rightHandSide.eval(s, e);

s_and_v.store.setElementAt(s_and_v.value,

e.access(leftHandSide));

return new StoreAndValue(s_and_v.store,

s_and_v.value);

}

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 15: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Denotational SemanticsAlternatives: Pass-by-reference and pass-by-copyA Realistic Interpreter for imPL

Realistic Implementation of Assignment

public class Assignment implements Expression {

public String leftHandSide, rightHandSide;

public Value eval(Environment e) {

Value r = rightHandSide.eval(e);

Store.theStore.set(e.access(leftHandSide),r);

return r;

}

}

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 16: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

IdeaAssignmentSequencesLoops

1 Imperative Programming: Some Loose Ends

2 A Virtual Machine for imPLIdeaAssignmentSequencesLoops

3 Object-Oriented Programming

4 A Realistic Object System

5 Meta-Programming

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 17: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

IdeaAssignmentSequencesLoops

Idea

Reuse heap for implementing imperative constructs

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 18: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

IdeaAssignmentSequencesLoops

Translation of Assignment

E →֒ s

x := E →֒ s.ASSIGNS x

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 19: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

IdeaAssignmentSequencesLoops

Execution of Assignment

s(pc) = ASSIGNS x

(os, pc, e, rs, h)⇉s (os, pc+ 1, e, rs, update(e, x , v , h′))

where (v , h′) = pop(os, h)

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 20: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

IdeaAssignmentSequencesLoops

Translation of Sequences

E1 →֒ s1 E2 →֒ s2

E1;E2 →֒ s1.POP.s2

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 21: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

IdeaAssignmentSequencesLoops

Implementation of Sequences

s(pc) = POP

(os, pc, e, rs, h)⇉s (os, pc+ 1, e, rs, h′)

where (v , h′) = pop(os, h)

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 22: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

IdeaAssignmentSequencesLoops

Translation of Loops

E1 →֒ s1 E2 →֒ s2

while E1 do E2

→֒s1.(JOFR |s2 + 3|).s2.POP.

(GOTOR − (|s1|+ 2 + |s2|)).LDCB true

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 23: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

1 Imperative Programming: Some Loose Ends

2 A Virtual Machine for imPL

3 Object-Oriented ProgrammingKnowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

4 A Realistic Object System

5 Meta-Programming

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 24: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Knowledge Representation View of Objects

Aggregation

Classification

Specialization

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 25: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Aggregation

let myVehicle = [MaxSpeed: 85] in ... end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 26: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Classification

let newVehicle = fun ms -> [MaxSpeed: ms] end

in

...

let myVehicle = (newVehicle 85) in

...

end

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 27: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Specialization

let newCar = fun mp -> let c = (newVehicle 95)

in c.MaxPassengers := mp

end

end

in

...

let myCar = (newCar 5) in

...

end

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 28: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Specialization

Usually, the concept of inheritance (class extension) achievesspecialization in object-oriented languages.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 29: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Control Flow in Procedural Languages (time)P1 P2 P3

control

time

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 30: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Control Flow in Object-oriented Languages (time)

applicationmethodself orobject application

control

M3M2M1M3M2M1methods

O2O1objects

time

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 31: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Execution of Object Application

window . move ( 100 , 50 )

code

lookupmethod

lookupclass

argumentsidentifiermethodobject

execution

codeaddress

class

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 32: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Early Binding in Procedural Languages

procedureapplication

procedureexecution

executingcode

earlybinding

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 33: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Late Binding in Object-oriented Languages

applicationobject

application"this"

executionmethod

bindinglate

"this"setting

methodmethod

bindingearly

application execution

executingcode

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 34: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

A Simple Object System in imPL

let stack =

[Push: fun this x ->

this.Content := x :: this.Content end,

Pop: fun this ->

let top = this.Content.First in

this.content := this.Content.Second;

top

end,

Makeempty:

fun this -> this.Content := []; this end

]

in ... end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 35: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Creating an Object

let mystack = (stack.Makeempty []) in ... end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 36: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Knowledge Representation View of ObjectsSoftware Development ViewObject-oriented programming in imPL

Operating on Objects

(stack.Push mystack 1);

(stack.Push mystack 2);

(stack.Pop mystack) + (stack.Pop mystack)

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 37: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

1 Imperative Programming: Some Loose Ends

2 A Virtual Machine for imPL

3 Object-Oriented Programming

4 A Realistic Object SystemFirst-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

5 Meta-Programming

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 38: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

First-Class Properties

q

E1 E2

E1.E2

E1 E2

E1 hasproperty E2

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 39: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Example

let access = fun r p -> r.p end

r = [A: 1, B: 2]

in (access r B) * 2

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 40: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Adding Properties in imPL?

let r = [A: 1, B: 2]

in r . C := 3

end

will fail in imPL, due to the semantics of record propertyassignment:

Σ | ∆ E1 (f ,Σ′) Σ′ | ∆ E2 (v ,Σ′′)

Σ | ∆ E1.q:=E2 (v ,Σ′′[f (q)← v ])

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 41: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Adding Properties in oPL!

let r = [A: 1, B: 2]

in r . C := 3

end

will succeed in oPL, adding the property C to the record.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 42: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

“First-class” and Adding Properties

let addProperty = fun r p v -> r.p := v end

in (addProperty [A:1,B:2] C 3)

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 43: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Late Binding in imPL

let new = fun theClass -> [Class: theClass] end

in ...

let mystack = (new stack) in

(stack.Makeempty mystack)

end

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 44: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Method Lookup

let lookup = fun object methodname ->

object.Class.methodname

end

in ...

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 45: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Method Lookup

( (lookup mystack Push) mystack 1 )

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 46: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

“This” Application

let stack =

[Push:fun this x -> this.Content := x::this.Content end,

Pop: fun this -> let top = this.Content.First

in this.Content

:= this.Content.Second;

top

end

end,

Makeempty: fun this -> this.Content := []; this end,

Pushtwice: fun this x -> ((lookup this Push) this x);

((lookup this Push) this x)

end]

in ... end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 47: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Inheritance

...

let stackWithTop =

[Parent: stack,

Top: fun this -> this.Content.First]

in ...

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 48: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Inheritance

let lookupInClass =

recfun lookupInClass theClass methodname ->

if theClass hasproperty methodname

then theClass.methodname

else (lookupInClass theClass.Parent methodname)

end

end

in

let lookup = fun object methodname ->

(lookupInClass object.Class methodname)

end

in ...

end

endCS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 49: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Object-oriented Syntax for oPL

method q(x1 · · · xn) -> E end

is abbreviation for the association

q : fun this x1 · · · xn -> E end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 50: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

A Syntax for Classes

class M1 · · ·Mn end

stands for[ M1, · · · ,Mn]

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 51: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

A Syntax for Classes

class extends x M1 · · ·Mn end

stands for[Parent:x , M1, · · · ,Mn]

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 52: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

A Syntax for Object Application

E.q(E1 · · ·En)

stands for

let obj = E in ((lookup obj q) obj E1 · · ·En) end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 53: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Putting it all together

let stack =

class method Push(x)->this.Content:=x::this.Content end,

method Pop() -> let top = this.Content.First in

this.Content

:= this.Content.Second;

top

end

end,

method Makeempty()

-> this.Content := []; this

end

end

in ...end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 54: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Putting it all together

...

let stackWithTop =

class extends stack

method Top() -> this.Content.First end

end

in

let myStackWithTop = (new stackWithTop)

in myStackWithTop.Makeempty();

myStackWithTop.Push(1);

myStackWithTop.Push(2);

myStackWithTop.Pop() + myStackWithTop.Pop()

end

end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 55: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Implementation of oPL

Efficient implementation of records described in the previouschapter is not possible for oPL (properties are first-classvalues in oPL).

Virtual machine-based implementation of oPL needs to revertto representing records using hashtables.

Existing languages such as Java avoid hashing for object fieldsthrough a type system.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 56: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Implementing Late Binding

Target function can only be determined at runtime.

Function lookup needs to follow the ancestor line of the classof the given object until a class is found that contains amethod under the given property

Process of method lookup can become bottleneck in theimplementation of object-oriented languages.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 57: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Optimizing Late Binding

Observation

The rationale behind this optimization is that the actual argumentobjects may change frequently between invocations of lookup,whereas the classes of those argument objects change much lessfrequently. This rationale has been confirmed by statistics onreal-world programs [Hoelzle:91].

Idea

Save the class and the result of the lookup for future use.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 58: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Inline Caching

Translate (lookup obj q) to a machine instruction sequence

LD <index of obj>

LDPS q

LOOKUP <class heap address> <cache>

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 59: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

First-class PropertiesLate Binding in imPLInheritanceObject-oriented Syntax for oPLImplementation of oPL

Inline Caching

LD <index of obj>

LDPS q

LOOKUP <class heap address> <cache>

LOOKUP has two extra parameters.

After lookup, it stores the class and the function.

Subsequent lookups compare the class with the class of thecurrent argument. If the same, no need for lookup; just usethe cached value!

Technique is called inline caching because the machine code isused to store the lookup result.

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 60: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

1 Imperative Programming: Some Loose Ends

2 A Virtual Machine for imPL

3 Object-Oriented Programming

4 A Realistic Object System

5 Meta-Programming

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 61: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Representing oPL Programs in oPL

(f x + 1)

can be represented by the oPL program

let fragment =

(new application).

Init((new identifier).Init(F),

(new binaryPrimitiveOperator).

Init(Plus,

(new identifier).Init(X),

(new integerConstant).Init(1)

))

in ... end

CS4215: Programming Language Implementation 10—Object-Oriented Programming

Page 62: 10—Object-Oriented Programming

Imperative Programming: Some Loose EndsA Virtual Machine for imPL

Object-Oriented ProgrammingA Realistic Object System

Meta-Programming

Meta-Programming in oPL

We can then implement programs in oPL that manipulate datastructures that represent oPL program (meta-programming).Examples:

compiler in oPL from oPL to an imPL VM language

interpreter for oPL in oPL itself, a so-called meta-circular

interpreter

CS4215: Programming Language Implementation 10—Object-Oriented Programming