A Semantics for Context-Oriented Programming with Layers Dave - - PowerPoint PPT Presentation

a semantics for context oriented programming with layers
SMART_READER_LITE
LIVE PREVIEW

A Semantics for Context-Oriented Programming with Layers Dave - - PowerPoint PPT Presentation

What is COP Problem description COP formalization A Semantics for Context-Oriented Programming with Layers Dave Clarke and Ilya Sergey Katholieke Universiteit Leuven {Dave.Clarke,Ilya.Sergey}@cs.kuleuven.be International workshop on


slide-1
SLIDE 1

What is COP Problem description COP formalization

A Semantics for Context-Oriented Programming with Layers

Dave Clarke and Ilya Sergey

Katholieke Universiteit Leuven {Dave.Clarke,Ilya.Sergey}@cs.kuleuven.be

International workshop on Context-Oriented Programming at ECOOP’09 7 July 2009

Ilya Sergey COP with Layers

slide-2
SLIDE 2

What is COP Problem description COP formalization

Why yet another semantics for COP?

Ilya Sergey COP with Layers

slide-3
SLIDE 3

What is COP Problem description COP formalization COP definition Some of COP implementations

COP definition

Context-oriented programming (COP) is a programming approach whereby the context in which expressions evaluate can be adapted as a program runs

Ilya Sergey COP with Layers

slide-4
SLIDE 4

What is COP Problem description COP formalization COP definition Some of COP implementations

COP key features

Context-dependent evaluation Explicit context Context manipulation

Ilya Sergey COP with Layers

slide-5
SLIDE 5

What is COP Problem description COP formalization COP definition Some of COP implementations

COP key features

Context-dependent evaluation Explicit context Context manipulation

Ilya Sergey COP with Layers

slide-6
SLIDE 6

What is COP Problem description COP formalization COP definition Some of COP implementations

COP key features

Context-dependent evaluation Explicit context Context manipulation

Ilya Sergey COP with Layers

slide-7
SLIDE 7

What is COP Problem description COP formalization COP definition Some of COP implementations

COP is implicitly (or explicitly) assumed in... Groovy LISP/Clojure Ruby Objective C ContextJ, ContextL Lasagne Ambience CaesarJ

Ilya Sergey COP with Layers

slide-8
SLIDE 8

What is COP Problem description COP formalization COP definition Some of COP implementations

Example: with, without and proceed in action

Enhanced ContextJ

class Actor { void act () { ... without(Logging) { stealth (); } } } layer Logging { class Actor { void act () { proceed (); println("Acted"); } } } with (Logging) { (new Actor ()). act (); }

void act() {...} class D layer Logging void act() {...} procced() with(Logging){new Actor().act()} act() void stealth() {...} without(Logging) { stealth(); }

Ilya Sergey COP with Layers

slide-9
SLIDE 9

What is COP Problem description COP formalization Possible pitfalls

Problems to solve

What is the order of expression evaluation for COP language?

There is a big step semantics (Schippers et al.)

How to ensure that all method invocations are resolved at runtime? Are statically-defined methods overridden correctly at runtime?

Ilya Sergey COP with Layers

slide-10
SLIDE 10

What is COP Problem description COP formalization Possible pitfalls

Problems to solve

What is the order of expression evaluation for COP language?

There is a big step semantics (Schippers et al.)

How to ensure that all method invocations are resolved at runtime? Are statically-defined methods overridden correctly at runtime?

Ilya Sergey COP with Layers

slide-11
SLIDE 11

What is COP Problem description COP formalization Possible pitfalls

Problems to solve

What is the order of expression evaluation for COP language?

There is a big step semantics (Schippers et al.)

How to ensure that all method invocations are resolved at runtime? Are statically-defined methods overridden correctly at runtime?

Ilya Sergey COP with Layers

slide-12
SLIDE 12

What is COP Problem description COP formalization Possible pitfalls

Problems to solve

What is the order of expression evaluation for COP language?

There is a big step semantics (Schippers et al.)

How to ensure that all method invocations are resolved at runtime? Are statically-defined methods overridden correctly at runtime?

Ilya Sergey COP with Layers

slide-13
SLIDE 13

What is COP Problem description COP formalization Possible pitfalls

Problems to solve

What is the order of expression evaluation for COP language?

There is a big step semantics (Schippers et al.)

How to ensure that all method invocations are resolved at runtime? Are statically-defined methods overridden correctly at runtime?

Ilya Sergey COP with Layers

slide-14
SLIDE 14

What is COP Problem description COP formalization Possible pitfalls

We need an operational semantics with the sound type system!

Ilya Sergey COP with Layers

slide-15
SLIDE 15

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

What is ContextFJ

ContextFJ is a language to describe core features of the

Context-Oriented programming

Based on Featherweight Java Has layers as dedicated language constructs Includes proceed, with and without statements Has no inheritance and subtyping

Ilya Sergey COP with Layers

slide-16
SLIDE 16

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

ContextFJ syntax: Terms and Contexts

Terms t ::= x | t.f | t.mL(t) | new C(t) | with(l)t | without(l)t | proceed(t) Values v ::= new C(v) Evaluation Contexts E[ ] ::= [ ] | E[[ ].f] | E[[ ].mL(t)] | E[v.mL(v, [ ], t)] | E[new C(v, [ ], t)] | E[with(l)[ ]] | E[without(l)[ ]]

Ilya Sergey COP with Layers

slide-17
SLIDE 17

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

ContextFJ syntax: layers and method bindings

Layer definition L ::= layer l {B} Method bindings B ::= (m, C0) → M

Ilya Sergey COP with Layers

slide-18
SLIDE 18

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Evaluation: bound methods

Bound methods BML([ ]) = ∅ BML(E[[ ].f]) BML(E[[ ].m(t)]) BML(E[v.m(v, [ ], t)]) BML(E[new C(v, [ ], t)]) 9 > = > ; = BML(E) BML(E[with(l)[ ]]) = ( BML(E), if l ∈ L BML(E) ∪ dom(l),

  • therwise

BML(E[without(l)[ ]]) = BML∪{l}(E)

Ilya Sergey COP with Layers

slide-19
SLIDE 19

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Evaluation: excluded layers

Excluded layers XL([ ]) = ∅ XL(E[[ ].f]) XL(E[[ ].m(t)]) XL(E[v.m(v, [ ], t)]) XL(E[new C(v, [ ], t)]) XL(E[with(l)[ ]]) 9 > > = > > ; = XL(E) XL(E[without(l)[ ]]) = {l} ∪ XL(E)

Ilya Sergey COP with Layers

slide-20
SLIDE 20

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Reduction rules (1)

(E-With)

E[with(l)v] → E[v]

(E-Without)

E[without(l)v] → E[v]

Ilya Sergey COP with Layers

slide-21
SLIDE 21

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Reduction rules (2)

(E-InvkLayer) lmbody(l, m, C) = (x, t) (m, C) / ∈ BML(E′) l / ∈ XL(E′) E[with(l)E′[(new C(v)).mL(u)]] → E[with(l)E′[ {x → u, proceed → this.mL∪{l}, this → new C(v)} t]] (E-InvkClass) (m, C) / ∈ BML(E) mbody(m, C) = (x, t) E[new C(v)).mL(u)] → E[{x → u, this → new C(v)}t]

Ilya Sergey COP with Layers

slide-22
SLIDE 22

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Example: evaluation

layer l1 {class C { D m(){return proceed(); } }} layer l2 {class D { C n(){return new C(); } }} class C {D m(){return new D(); }} class D {} with(l1){with(l2){ new C().m() .n()}} → with(l1){with(l2){ new C().m{l1}() .n()}} → with(l1){with(l2){ new D().n() }} → with(l1){ with(l2){new C()} } → with(l1){new C()} → new C()

Ilya Sergey COP with Layers

slide-23
SLIDE 23

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Type system outline

Some methods need other undefined methods of specific types to be evaluated - requirements Before invoke method we should satisfy its requirements Layers provide new methods and require some other ones

Ilya Sergey COP with Layers

slide-24
SLIDE 24

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Type system outline

Some methods need other undefined methods of specific types to be evaluated - requirements Before invoke method we should satisfy its requirements Layers provide new methods and require some other ones

Ilya Sergey COP with Layers

slide-25
SLIDE 25

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Type system outline

Some methods need other undefined methods of specific types to be evaluated - requirements Before invoke method we should satisfy its requirements Layers provide new methods and require some other ones

Ilya Sergey COP with Layers

slide-26
SLIDE 26

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

ContextFJ syntax again: method definitions

Method definition M ::= C [Ψ] m(C x){return t; } Method requirements Ψ ::= ǫ | MT, Ψ Method types MT ::= (m, C0) → [Ψ]C → C • L Excluded layers L ::= a set of layer names | ⊤ (∀L.L ⊆ ⊤)

Ilya Sergey COP with Layers

slide-27
SLIDE 27

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Method types demystified

(m, C0) → [Ψ]C → C • L m is a method’s name; C0 is a receiver class type; C are parameter types; and C is the result type; L is the set of excluded layers Ψ is a set of method requirements

Ilya Sergey COP with Layers

slide-28
SLIDE 28

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Term typing

Typing relation Ψ; Γ ⊢ t : C Term is well-typed. Root term t must be typed in the empty environment. ∃C : ∅; ∅ ⊢ t : C

Ilya Sergey COP with Layers

slide-29
SLIDE 29

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Expression typing

Key idea

Method invocations add new requirement into the set Ψ Layer activations with(l) removes provided methods from Ψ proceed() and without(l) statements modify method types in Ψ, excluding new layers

Ilya Sergey COP with Layers

slide-30
SLIDE 30

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Method invocation typing

(T-Invk)

Ψ ; Γ ⊢ t0 : C Ψ; Γ ⊢ t : C mtype(m, C, Ψ) = [Φ]C → D • L′ Φ Ψ L ⊆ L′ Ψ; Γ ⊢ t0.mL(t) : D Method invocation is well-typed if it is defined in some class or in requirements Ψ; its requirements are satisfied by Ψ; its set of excluded layers L is weaker than a set L′ we suppose to exclude for method of this type.

Ilya Sergey COP with Layers

slide-31
SLIDE 31

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Layer activation typing

(T-With)

(Ψ, Φ) ; Γ ⊢ t : C layer l {B} Φ ⊆ provides(l) requires(l) Ψ ∀((m, C0) → C → D • L ∈ Φ) · l / ∈ L Ψ ; Γ ⊢ with(l)t : C Using a layer l by with(l) statement allows us to exclude a part Φ of requirements from the environment

Ilya Sergey COP with Layers

slide-32
SLIDE 32

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Benefits and limitations

Caught errors

Unresolved method calls Illegal method overriding in layers proceed() calls without a higher method to proceed to

System limitations

No inheritance No class-based polymorphism Too many annotations are required for analysis

Ilya Sergey COP with Layers

slide-33
SLIDE 33

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Benefits and limitations

Caught errors

Unresolved method calls Illegal method overriding in layers proceed() calls without a higher method to proceed to

System limitations

No inheritance No class-based polymorphism Too many annotations are required for analysis

Ilya Sergey COP with Layers

slide-34
SLIDE 34

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Future work

Semantics for contexts and inheritance C <: D layer l1{class C { F m(C x){return t1; } }} layer l2{class D { F m(C x){return t2; } }}

with (l1) { with (l2) { new C().m(...); } }

Which one of m()s should we pick?

Ilya Sergey COP with Layers

slide-35
SLIDE 35

What is COP Problem description COP formalization ContextFJ Syntax Dynamic semantics Type system Q&A

Q&A

Thanks for your attention!

Ilya Sergey COP with Layers