+ All Categories
Home > Documents > A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and...

A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and...

Date post: 24-Dec-2015
Category:
Upload: june-adams
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
43
A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University
Transcript
Page 1: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

A Formal Model of Modularityin Aspect-Oriented Programming

Jonathan Aldrich

15-819: Objects and Aspects

Carnegie Mellon University

Page 2: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Outline AOP Modularity Challenges Open Modules A Bit of Formality Comparison to Aspect-Aware Interfaces Lessons Learned and Discussion

Page 3: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Modularity and Encapsulation Parnas’ advice:

Modularize a system to hide information that may change

Encapsulation A mechanism for enforcing information hiding Java classes & packages, ML modules…

Aspect-oriented Programming More flexible ways of modularizing a system

Page 4: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Is AOP Modular? Back to Parnas: Does AOP hide

information that is likely to change? Yes, within the aspect

Aspect code can be evolved separately

No, not within the base code Minor changes to base code break the aspect

Page 5: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

Page 6: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1x += dx; p1y += dy;

p2x += dx; p2y += dy;

...

}

Page 7: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1x += dx; p1y += dy;

p2x += dx; p2y += dy;

...

}

aspect AssureShapeInvariants {

}

Page 8: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1x += dx; p1y += dy;

p2x += dx; p2y += dy;

...

}

aspect AssureShapeInvariants {

pointcut moves() =

call(Shape+.moveBy(..));

}

Page 9: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1x += dx; p1y += dy;

p2x += dx; p2y += dy;

...

}

aspect AssureShapeInvariants {

pointcut moves() =

call(Shape+.moveBy(..));

after(): moves() {

scene.checkInvariants();

}

}

Page 10: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Broken Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1x += dx; p1y += dy;

p2x += dx; p2y += dy;

...

}

aspect AssureShapeInvariants {

pointcut moves() =

call(Shape+.moveBy(..));

after(): moves() {

scene.checkInvariants();

}

}

Change representation to use Point

Page 11: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Broken Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1.moveBy(dx, dy);

p2.moveBy(dx, dy);

...

}

aspect AssureShapeInvariants {

pointcut moves() =

call(Shape+.moveBy(..));

after(): moves() {

scene.checkInvariants();

}

}

Change representation to use Point

Page 12: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Example: Broken Assurance Aspectclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1.moveBy(dx, dy);

p2.moveBy(dx, dy);

...

}

aspect AssureShapeInvariants {

pointcut moves() =

call(Shape+.moveBy(..));

after(): moves() {

scene.checkInvariants();

}

}

Change representation to use Point

Now the scene invariants are checked in the middle of a Rectangle move—when they might be broken!

Page 13: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Analysis Aspects can violate information hiding

Assurance aspect depends on Shape internals

Similar to OO Fragile Base Class Problem Observing impl. dependant calling patterns

Can fix each individual problem Better: use modules to forestall issue

Page 14: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Fix #1: external adviceclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1x += dx; p1y += dy;

p2x += dx; p2y += dy;

...

}

aspect AssureShapeInvariants {

pointcut moves():

call(Shape+.moveBy(..))

&& !within(shape.*);

after(): moves() {

scene.checkInvariants();

}

}

Only specifies calls that are external to the shape package

Page 15: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Fix #2: semantic pointcutclass Point extends Shape {

void moveBy(int dx, int dy) {

x += dx; y += dy;

...

}

class Rectangle extends Shape {

void moveBy(int dx, int dy) {

p1x += dx; p1y += dy;

p2x += dx; p2y += dy;

...

}

class Shape {

public pointcut moves():

call(Shape+.moveBy(..));

}

aspect AssureShapeInvariants {

after(): Shape.moves() {

scene.checkInvariants();

}

}

Move pointcut to the shape package

Now the shape maintainer is responsible for preserving its

semantics when shapes evolve

Page 16: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules

void moveBy(int, int);void animate(Motion);

Open ModuleOrdinary functional interface

Page 17: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules

void moveBy(int, int);void animate(Motion);

pointcut moves;

Open ModuleOrdinary functional interface

Semantic Pointcut• Denotes some internal event• Promise to maintain event semantics as code evolves[Gudmunson & Kiczales]

Page 18: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules

void moveBy(int, int);void animate(Motion);

pointcut moves;

Open ModuleClients can call interface functions

Page 19: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules

void moveBy(int, int);void animate(Motion);

pointcut moves;

Open ModuleClients can call interface functions

Clients can advise external calls to interface functions

Page 20: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules

void moveBy(int, int);void animate(Motion);

pointcut moves;

Open ModuleClients can call interface functions

Clients can advise external calls to interface functions

Clients can advise pointcuts in interface

Page 21: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules

void moveBy(int, int);void animate(Motion);

pointcut moves;

Open ModuleClients can call interface functions

Clients can advise external calls to interface functions

Clients can advise pointcuts in interface

Clients cannot advise any internal calls (not even to exported

functions)

X

Page 22: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Module Properties: Equivalence

Motivation Rules describe when changes could affect

clients Can be used to prove correctness of refactorings

Bisimulation-based equivalence All functions map args to same results Invoke internal pointcuts in same way

Same sequence, same context

Page 23: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Module Properties: Abstraction

Verifies correctness of equivalence rules Shows that information hiding works

Informal statement of theorem Consider two module implementations that are

equivalent according to the bisimulation rules No client code can distinguish the behavior of

these modules (even by using aspects) Note: this would fail for AspectJ!

Page 24: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Comparison to Aspect-Aware Interfaces AAI: more obliviousness, extensibility

Don’t have to anticipate semantic pointcuts OM: truly separate development

AAI cannot be computed in this case OM: shows technical properties of AAI

AAI is an OM interface computed by tools Abstraction supports evolvability Not all information in AAI is needed

Don’t need exact advice Don’t need pointcuts for external calls to interface functions

Page 25: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Tool and Language Implications Tools: Provide editable interface pointcuts

Change base code and affected pointcuts at the same time

Tools: Support dependency tracking Let you know when you’re depending on impl. Warn you to re-check pointcuts when impl.

Changes Language

Make it easier to write non-invasive pointcuts

Page 26: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Discussion Extensibility vs. Reasoning Tool vs. Language-based reasoning Open Modules into real AOP systems Analysis based on Open Modules

Page 27: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

End of Presentation/Extra Slides

Page 28: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

TinyAspect Example(* fibonacci function *)val fib = fn x:int => 1around call(fib) (x:int) = if (x > 2) then fib(x-1) + fib(x-2) else proceed x

(* caching library *)val inCache = fn ...val lookupCache = fn ...val updateCache = fn ...

(* advice to cache calls to fib *)

pointcut cacheFunction = call(fib)

around cacheFunction(x:int) =

if (inCache x)

then lookupCache x

else let v = proceed x

in updateCache x v; v

Page 29: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

TinyAspect: Syntax

Page 30: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Evaluation Environment captures advice

Declarations add labels to environment

Functions are looked up just before application

Page 31: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Evaluation Environment captures advice

Declarations add labels to environment

Advice updates environment

Page 32: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

TinyAspect: Values and Contexts

Page 33: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

TinyAspect: Reduction Rules

Page 34: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

TinyAspect: Expression Typing

Page 35: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

TinyAspect: Declaration Typing

Page 36: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

TinyAspect: Properties

Page 37: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules: Syntax

Page 38: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules: Examplestructure Math = struct val fib = fn x:int => 1 around call(fib) (x:int) = …

structure cacheFib = Cache(struct pointcut f = call(fib) end)end :> sig fib : int->intend

structure Cache =

functor(X : sig f : pc(int->int) end) =>

struct

around X.f(x:int) = ...

(* same definition *)

end

Page 39: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Open Modules: Semantics Standard type system

Signature subtyping permitted

Runtime semantics mostly standard E.g, functor application uses substitution

Page 40: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Sealing has operational effect Value bindings given fresh labels

External advice doesn’t affect internal calls C.f. “freeze” operator in Jigsaw, other systems

Pointcuts are unchanged

Open Modules: Semantics

Page 41: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Reynolds’ Abstraction Property No client can distinguish two modules that are

“observationally equivalent” (c.f. Pi-calculus) calling functions in interface advising external calls to interface advising pointcuts in interface

Means that information hiding works You can change the internals of a module w/o affecting

clients

Page 42: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Observational Equivalence Functions behave equivalently for all args

Expression evaluation must be bisimilar w.r.t. a set of available labels Can take any step other than looking up

Can both look up the same label in

Page 43: A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich 15-819: Objects and Aspects Carnegie Mellon University.

Formal Abstraction Theorem

Proof insight Client can only advise labels in Libraries treat these labels equivalently

Key invariant Clients are structurally identical

Except for embedded equivalent values


Recommended