Title: 6.894 Administrivia
16.894 Administrivia
- Sign up for paper presentations on 2/27.
- look at titles, find papers,
- once selected, well copy.
- Next lecture JB on VMs ( interpreters)
2OODLs - The Big Picture
- Abstraction
- Dynamism
- Reflection
- Types
- Objects
Meta-object Protocols
3Abstraction
What else?
- ?-abstraction (A. Church, 1934)
- operations, abstracted over actual values.
- Abstract Data Types (ADT)
- interface, abstracted over actual implementation.
Classes are ADTs. - Dynamic Dispatch
- abstracts idiom choice by type.
4Dynamism
- What is traditionally fixed at some point in a
program? - code (defn of functions)
- types (and their structure)
- binary image (incl. optimizations)
5Enabling 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?
6Reflection
- 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 SF96reflection-oriented programming is
programming as if the interpreter had been
modified.
7Reflection, contd
- Terminology
- Reification elt. of interpreter state made
manipulable by user program. - Reflection program value injected into
interpreters state. (what I earlier called
dynamism).
8Java Reflection API
- Ex. Java Reflection API.
- ms x.getClass().
getDeclaredMethods()msi.invoke(y, args)
9Whats Missing from Java Reflection?
- Reflection, aka Dynamism!
- Javas 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.
10Reflection Dynamism Adaptivity
- Design How do we expose structure and behavior?
- Implementation Pay as you go.
- How do we structure value domains?
- Whats a mechanism for choosing different
behavior?
- Analysis can a function be redefined?
- Optimistic optimization is this function
unlikely to be redefined?
11Adaptive Programming, Different Approaches
Common Theme Different Perspectivesfor
different purposes
- Lieberherr et al., Lie96.
- Abstract over paths
- Norvig Cohn NC97
- Metaobject Protocols (MOP)
- Aspect-oriented Programming (AOP)
- Refactoring FBB99, Extreme Programming
- Subject-Oriented ProgrammingHO93
12Aspect-Oriented Programming
- Programming crosscutting concerns
- Ex.s synchronization, scheduling, persistence,
replication. - See www.aspectj.org, KLM97
- Recall hack program or hack interpreter.
13AspectJ
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("Abou
t to set something in " p1
" to the new value " newval)
14Meta-Object Protocols (MOPs)
- 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!
15MOPs, contd.
- 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?
16WHOA?(What Has Openness Accomplished?)
- Gained Abstraction, Dynamism, Adaptibility!
- Lost Safety, Performance.
- fill(aShape, aColor) ? NAM?
- for(i1 i lt bigNum i) ai,j bi,j
ci,j
find and sort all methods on each time?
17Safety First
- Types Type Checking.
- The basic ruleThe arg type matches the fun
type.
18Type Checking for Java
- anObject.aMethod(arg1, ..., argn)
- inheritance (subsumption),
- overloading (dispatch by signature)
- overriding (dynamic dispatch by receiver concrete
type).
19Limitations 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 // errors int 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 // errors float
getY() return y // errors
20Java Type Issues, Contd.
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.eq
ual(aCPoint)) // System.out.println(aCPoint.equa
l(aPoint))
21Too Much Typing?
public static int foo(int x, int y) int
t if (x lt y) t x 1 else
t y - 1 return t 2
- We should be able to skip annotating t with type
int. - Type Reconstruction, aka Type Inference.
- Still an open research area for Java.
22Binary Methods
- See BCC95
- Several proposed solutions...
Multiple Dispatch
Our favorite
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
23What 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.
24Making Types More Expressive
predicate on-x-axis(p_at_point) when
(p_at_cartesianPoint and test(p.y 0))
or (p_at_polarPoint and (test(p.theta
0)or test(p.theta pi)))
draw the point method draw(p_at_point) ... use
a contrasting color so point is visible method
draw(p_at_on-x-axis) ...
25Dynamic 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.
26Implementing Reflection
- Recall the motivation hack all over the program,
or hack the interpreter.
27Implementing Reflection.Strategies
- Interpreter Tuning
- Threading, VVM
28Dynamic Optimization
- Customization CUL91,DCG95
- Dynamic Partial Evaluation Sul01
- Dynamic compilation to native Sun HotSpot
- IR ? Native (IR/Native interoperability)
- Jalapeño from IBM AAC99
- simple native, optimized native
29Dynamic Optimization, Contd
- HP Dynamo BDB00
- Native Interpreter trace cache
30Optimizing in the Face of Dynamism
- Key insight while most everything might change,
most does not. - Optimize with respect to quasi-invariants.
31Basic TechniquePartial Evaluation
- Produces specialized versions of code based on
assumptions about values. - See JGS93, CD93.
- From CD93the essential purpose of partial
evaluation eliminating interpretive overhead.
32Partial EvaluationSpecializing functions
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 (x2)2
- specialization with respect to 1st parameter.
33When to Partially Evaluate?
- When some argument values change much less
frequently than others. - Ray tracing, interpreters.
34Definition of PE
- out p(in1, in2)p_in1 mix(p, in1)out
p_in1 in2 - Equational definition of mixp(in1, in2)
mix (p, in1) in2
specializedprogram
35Futamara Projections
- See Fut71
- 1st target mix (int, source)
- 2nd compiler mix(mix, int)
- target mix(int, source)
- mix(mix, int)(source)
- compiler(source)
36Futamura, contd
- 3rd cogen mix(mix, mix)
- p(in1, in2) mix(p, in1) (in2)
- mix(mix, p) (in1) (in2)
- mix (mix, mix) (p) (in1)
(in2)
37Challenges for Partial Evaluation
- Infinite looping, infinite specialization
- Efficiency, especially
- Dynamic PE -- open challenge.
38Staged Programming
- Closely related to partial evaluation
- Quasiquotation Baw99
- MetaML TS00, Modal ML
- DyCGPM99, CEHK96, Tempo
39Notes to myself
- staged programming, metaml?
- Discuss the DVM?
- Projects
- real MOP for Java.
- type inference for Java.
- on stack replacement, deoptimization
40Another Favorite Java Gotcha
class 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
41Talk Overview
Abstraction, Design
Implementation, Optimization