41
6.894, Spring 2001 - OODL Overview - Greg Sullivan - F ebruary 8 1 6.894 Administrivia • Sign up for paper presentations on 2/27. – look at titles, find papers, – once selected, we’ll copy. • Next lecture: JB on VM’s (& interpreters)

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

Embed Size (px)

Citation preview

Page 1: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1

6.894 Administrivia

• Sign up for paper presentations on 2/27.– look at titles, find papers,

– once selected, we’ll copy.

• Next lecture: JB on VM’s (& interpreters)

Page 2: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 2

OODL’s - The Big Picture

• Abstraction

• Dynamism

• Reflection

• Types

• Objects

Meta-object Protocols

Page 3: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 3

Abstraction

-abstraction (A. Church, 1934)– operations, abstracted over actual values.

• Abstract Data Types (ADT)– interface, abstracted over actual

implementation. Classes are ADT’s.

• Dynamic Dispatch– abstracts idiom “choice by type”.

What else?

•Inheritance

•macros

•???

“structure imposed on values.”

Page 4: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 4

Dynamism• What is traditionally fixed at some

point in a program?– code (def’n of functions)

– types (and their structure)

– binary image (incl. optimizations)

Why?

new code, over net, via debugger

new subclasses

optimizations, based on runtime data, more thorough analysis.

Page 5: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 5

Enabling Dynamism• How to add to language?

– (set! sort qsort) – (add-subclass Tree BinaryTree)– (set! “sort” qsort)

• How to implement?– (+ x y) -- in a tight loop– (dyncall walk a-fun a-tree)

Disabling Dynamism

• for performance?

• for security?

Page 6: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 6

Reflection

• Computation Reflection [Mae87]: Computational reflection is the activity performed by a computational system when doing computation about (and by that possibly affecting) its own computation.

• From [SF96]:reflection-oriented programming [is] programming as if the interpreter had been modified.

Page 7: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 7

Reflection, cont’d

• Terminology:– Reification = elt. of interpreter state

made manipulable by user program.

– Reflection = program value injected into interpreter’s state. (what I earlier called “dynamism”).

Page 8: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 8

Java Reflection API

• Ex.: Java Reflection API.– ms = x.getClass().

getDeclaredMethods();ms[i].invoke(y, args);

Page 9: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 9

What’s Missing from Java Reflection?

• Reflection, aka Dynamism!– Java’s reflection is “read only”.

– for every get, there should be a set!

• Java does have dynamic class loading– a big hammer.

• Also: performance, context (system, memory, load), dispatch hooks.

Page 10: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 10

Reflection + Dynamism=

Adaptivity• Design: How do we expose structure

and behavior?

• Implementation: “Pay as you go.”

• How do we structure value domains?• What’s a mechanism for choosing

different behavior?

• Analysis: can a function be redefined?

• Optimistic optimization: is this function unlikely to be redefined?

Page 11: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 11

Adaptive Programming, Different Approaches

• Lieberherr et al., [Lie96]. – Abstract over paths

• Norvig & Cohn [NC97]• Metaobject Protocols (MOP)• Aspect-oriented Programming (AOP)• Refactoring [FBB+99], Extreme

Programming• Subject-Oriented Programming[HO93]

Common Theme:Different Perspectivesfor different purposes

Page 12: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 12

Aspect-Oriented Programming

• Programming “crosscutting concerns”– Ex.s: synchronization, scheduling,

persistence, replication.

• See: www.aspectj.org, [KLM+97]

• Recall “hack program or hack interpreter.”

Page 13: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 13

AspectJ

pointcut setters(Point p1, int newval): instanceof(p1) && (receptions(void setX(newval) || receptions(void setY(newval)));before(Point p1, int newval): setters(p1, newval) { System.out.println("About to set something in " + p1 + " to the new value " + newval); }

Page 14: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 14

Meta-Object Protocols (MOP’s)

• Like Java, “reified” program elements are instances of “metaclasses” (Class, Method, etc.), but they can be subclassed!

• Metabehavior (semantics) defined by methods on metaclasses. – compute-applicable-methods, allocate-

instance, compute-effective-method, etc., and they can be overridden!

Page 15: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 15

MOP’s, cont’d.• See [KdB91], The Art of the Meta-

Object Protocol.

• Aaron Ucko, MEng project: adding predicate dispatching to CLOS.– subtypes generic function and method

metaclasses.

• Can you see how to implement AOP with a MOP?

Page 16: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 16

WHOA?(What Has Openness Accomplished?)

Gained: Abstraction, Dynamism, Adaptibility!

Lost: Safety, Performance.• fill(aShape, aColor); NAM?

• for(i=1; i < bigNum; i++) a[i,j] = b[i,j] + c[i,j]; find and sort all

methods on + each time?

Page 17: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 17

Safety First

• Types & Type Checking.• The basic rule:

“The arg type matches the fun type.”

:)(

::

xfA

xAfA

“Message not understood”

Page 18: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 18

Type Checking for Java

• anObject.aMethod(arg1, ..., argn);– inheritance (subsumption),

– overloading (dispatch by signature)

– overriding (dynamic dispatch by receiver concrete type).

Page 19: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 19

Limitations of Java Types• From the Java Language Spec.:

class Point {int x = 0, y = 0, color;void move(int dx, int dy) { x += dx; y += dy; }int getX() { return x; } // errorsint getY() { return y; } // errors

}class RealPoint extends Point {

float x = 0.0f, y = 0.0f;void move(int dx, int dy) { move((float)dx, (float)dy); }void move(float dx, float dy) { x += dx; y += dy; }float getX() { return x; } // errorsfloat getY() { return y; } // errors

}

Page 20: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 20

Java Type Issues, Cont’d.class Point extends Object { int x_val = 0; int y_val = 0;// ... boolean equal (Point other) {

return((x_val == other.x()) && (y_val == other.y()));

} boolean equal (Object other) {

return(false); }}

class ColorPoint extends Point {// ... boolean equal (ColorPoint other) {

return((x_val == other.x()) && (y_val == other.y()) && (color_val == other.color()));

} boolean equal (Object other) {

return(false); }}

class TestOverLoad extends Object { public static void main (String[] args) {

Point aPoint = new Point(2, 3);ColorPoint aCPoint = new ColorPoint(2, 3, 5);System.out.println(aPoint.equal(aCPoint));// System.out.println(aCPoint.equal(aPoint));

}}

Page 21: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 21

Too Much Typing?

• We should be able to skip annotating t with type int.• Type Reconstruction, aka Type Inference.• Still an open research area for Java.

public static int foo(int x, int y) { int t; if (x < y) t = x + 1; else t = y - 1; return t * 2;

}

Page 22: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 22

Binary Methods• See [BCC+95]

• Several proposed “solutions”...

boolean equal(Point p1, Point p2) {...}boolean equal(ColorPoint p1, ColorPoint p2) {...}

•We also need: boolean equal(Point p1, ColorPoint p2) {return(false);} boolean equal(ColorPoint p1, Point p2) {return(false);}

• See [CL95], [CLCM00]

Multiple DispatchOur favorite

Page 23: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 23

What are Types For?

• Software Engineering– Classes: data abstraction, modularity,

reuse. In Java, classes are also types.

• Safety – type checking.

• Control Abstraction – Dynamic dispatch– choice of code depending on receiver type.

Page 24: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 24

Making Types More Expressive

• Predicate Types, [EKC98]predicate on-x-axis(p@point) when (p@cartesianPoint and test(p.y == 0)) or (p@polarPoint and (test(p.theta == 0)or test(p.theta == pi))); draw the point:method draw(p@point) {...} use a contrasting color so point is visible:

method draw(p@on-x-axis) {...}

Page 25: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 25

Dynamic vs. Static Typing

• Safety Expressiveness tension.• Mixed typing?

– Soft typing for Scheme [CF91,Wri94]

• UI / Language features to support delimited regions with diff. regimes

• Dynamism and strong typing– new code must maintain invariants.– uncharted territory.

Page 26: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 26

Implementing Reflection

• Recall the motivation: hack all over the program, or hack the interpreter.

Page 27: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 27

Implementing Reflection.Strategies

• Interpreter Tuning– Threading, VVM

Page 28: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 28

Dynamic Optimization

• Customization [CUL91,DCG95]

• Dynamic Partial Evaluation [Sul01]

• Dynamic compilation to native [Sun HotSpot] – IR Native (IR/Native interoperability)

• Jalapeño from IBM [AAC+99] – simple native, optimized native

Page 29: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 29

Dynamic Optimization, Cont’d

• HP Dynamo [BDB00]– Native Interpreter + trace cache

Page 30: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 30

Optimizing in the Face of Dynamism

• Key insight: while most everything might change, most does not.

Optimize with respect to “quasi-invariants”.

Page 31: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 31

Basic Technique:Partial Evaluation

• Produces specialized versions of code based on assumptions about values.

• See [JGS93, CD93].• From [CD93]:

the essential purpose of partial evaluation: eliminating interpretive overhead.

Page 32: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 32

Partial EvaluationSpecializing functions

• specialization with respect to 1st parameter.

f(n,x) = if (n = 0) then 1 else if (even(n) then f(n/2, x)^2; else x * f(n-1, x);

f5(x) = x * (x^2)^2;

Page 33: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 33

When to Partially Evaluate?

• When some argument values change much less frequently than others.– Ray tracing, interpreters.

Page 34: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 34

Definition of PE

• out = [p](in1, in2)p_in1 = [mix](p, in1)out = [p_in1] in2;

• Equational definition of mix:[p](in1, in2) = [ [mix] (p, in1) ] in2

specializedprogram

Page 35: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 35

Futamara Projections

• See [Fut71]

• 1st: target = [mix] (int, source)

• 2nd: compiler = [mix](mix, int)target = [mix](int, source)

= [[mix](mix, int)](source)

= [compiler](source)

Page 36: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 36

Futamura, cont’d

• 3rd: cogen = [mix](mix, mix)[p](in1, in2) = [ [mix](p, in1) ](in2)

= [ [ [mix](mix, p) ] (in1) ](in2)

= [ [ [ [mix] (mix, mix) ](p) ] (in1) ] (in2)

Page 37: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 37

Challenges for Partial Evaluation

• Infinite looping, infinite specialization

• Efficiency, especially:– Dynamic PE -- open challenge.

Page 38: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 38

Staged Programming

• Closely related to partial evaluation

• Quasiquotation [Baw99]

• MetaML [TS00], Modal ML[]

• DyC[GPM+99], `C[EHK96], Tempo

Page 39: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 39

Notes to myself

• staged programming, metaml?

• Discuss the DVM?

• Projects:– “real” MOP for Java.

– type inference for Java.

• on stack replacement, deoptimization

Page 40: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 40

Another Favorite Java Gotchaclass Problem1 extends Object { // P is a subclass of Object, // C is a subclass of P. public static void foo(P x) {

bar(x); } public static void bar(P aP) {

System.out.println("bar on P"); } public static void bar(C aC) {

System.out.println("bar on C"); } public static void main (String[] args) {

foo(new C()); }} // class Problem1

Object

P

C

Page 41: 6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 1 6.894 Administrivia Sign up for paper presentations on 2/27. –look at titles, find papers,

6.894, Spring 2001 - OODL Overview - Greg Sullivan - February 8 41

Talk Overview

Implementation, Optimization

Abstraction, Design