Discussion Topics - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Discussion Topics

Description:

Craig Chambers, University of Washington. ECOOP 1993 ' ... Curtis Clifton, Gary T. Leavens, Craig Chambers, Todd Millstein. July 2000 ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 30
Provided by: venk9
Category:

less

Transcript and Presenter's Notes

Title: Discussion Topics


1
(No Transcript)
2
Discussion Topics
  • Single Dispatching
  • Multiple Dispatching
  • Predicate Dispatching
  • Open Classes
  • Generic Functions
  • Code examples
  • Predicate dispatching as multiple dispatching
  • Efficient dispatching using DAGs

3
Reference Publications
  • Efficient Multiple and Predicate Dispatching
  • Craig Chambers and Weimin Chen, University of
    Washington
  • OOPSLA 1999
  • Predicate Classes
  • Craig Chambers, University of Washington
  • ECOOP 1993
  • MultiJava Modular Open Classes and Symmetric
    Multiple Dispatch for Java
  • Curtis Clifton, Gary T. Leavens, Craig
    Chambers, Todd Millstein
  • July 2000

4
Method Dispatching
  • Methods as messages in OOP
  • Method dispatching
  • Static
  • Dynamic
  • Dispatching styles
  • based on receiver
  • based on arguments
  • based on receiver and arguments

5
Single Dispatching
  • Dispatching based on
  • Run-time type of receiver object
  • Not based on run-time type of arguments
  • Sometimes inconvenient to program
  • Examples
  • Java, C

6
Motivating Example
  • public class Shape
  • //
  • public boolean intersect(Shape s)
  • /../
  • public class Rectangle
  • //
  • public boolean intersect(Rectangle s)
  • /efficient code for two rectangles/

7
Example (contd)
  • Shape s1, s2
  • Rectangle r1,r2
  • r1 new Rectangle()
  • r2 new Rectangle()
  • s1 r1 s2 r2
  • r1.intersect(r2)
  • r1.intersect(s2)
  • s1.intersect(r2)
  • s1.intersect(s2)

8
One possible solution
  • public class Rectangle
  • //
  • public boolean intersect(Shape s)
  • if (s instanceof Rectangle )
  • this.intersect((Rectangle)s)
  • else super.intersect(s)
  • public boolean intersect(Rectangle s)
  • /efficient code for two rectangles/

9
Single Dispatching - drawbacks
  • instanceof tedious and error-prone
  • not easily extensible
  • what if a new shape is introduced in the example?

10
Double dispatching
  • Double dispatching a solution
  • Visitor pattern
  • Still tedious
  • class Shape
  • bool intersect(Shape s)
  • s.beingIntersected(this)
  • bool intersectRect(Rectangle r)
  • return false
  • class Rectangle extends Shape
  • bool beingIntersected(Shape s)
  • s.intersectRect(this)
  • bool intersectRect(Rectangle r)
  • //

11
Dispatching internals
  • Static overloading of methods
  • Java, C
  • intersect method in example
  • Name, number and static argument types
  • Statically overloaded methods belong to distinct
    generic functions

12
Generic functions
  • A set of methods with the same signature
  • Formal parameters all renamed to be the same
  • Dispatching involves selecting the best matching
    method
  • Member methods could be physically defined
    separately

13
Canonical Syntax of Generic Functions
Efficient Multiple and Predicate
Dispatching Craig Chambers and Weimin Chen
14
Generic Function Example
Efficient Multiple and Predicate
Dispatching Craig Chambers and Weimin Chen
15
GF in Shapes Example
  • df intersect1(f1,f2)
  • f1_at_Rectangle and f2_at_Shape gt m2
  • or f1_at_Shape and f2_at_Shape gt m1
  • df intersect2(f1,f2)
  • f1_at_Rectangle and f2_at_Rectangle gt m3
  • public class Shape
  • //
  • public boolean intersect(Shape s)
  • /../
  • public class Rectangle
  • //
  • public boolean intersect(Shape s)
  • //
  • public boolean intersect(Rectangle s)
  • /efficient code for two rectangles/

m1
m2
m3
16
Multiple Dispatching
  • Multimethods
  • statictype_at_specializer
  • Method dispatch based on run-time type of
    arguments also
  • intersect(Shape_at_Rectangle r)
  • Dynamically dispatch on formal parameter r.

17
Multiple dispatching example
  • public class Shape
  • //
  • public boolean intersect(Shape s)
  • /../
  • public class Rectangle
  • //
  • public boolean intersect(Shape_at_Rectangle s)
  • /efficient code for two rectangles/

18
GF in Shapes Example with multimethods
  • df intersect1(f1,f2)
  • f1_at_Shape and f2_at_Shape gt m1
  • or f1_at_Rectangle and f2_at_Rectangle gt m2

public class Shape // public boolean
intersect(Shape s) /../ public
class Rectangle // public boolean
intersect(Shape_at_Rectangle s) /efficient
code for two rectangles/
m1
m2
19
Dispatch Strategy
  • Identify applicable methods based on argument
    type
  • Subset of methods in the GF
  • Choose the most specific applicable method
  • Pointwise subtype of all applicable methods
  • No applicable method
  • message-not-understood error
  • Multiple most-specific methods
  • Message-ambiguous error

20
Dispatch strategy applied
  • Shape s1, s2
  • Rectangle r1,r2
  • r1 new Rectangle()
  • r2 new Rectangle()
  • s1 r1 s2 r2
  • r1.intersect(r2)
  • r1.intersect(s2)
  • s1.intersect(r2)
  • s1.intersect(s2)
  • df intersect1(f1,f2)
  • f1_at_Shape and f2_at_Shape gt m1
  • or f1_at_Rectangle and f2_at_Rectangle gt m2
  • m1 Shapeintersect(Shape)
  • m2 Rectangle(Shape_at_Rectangle)
  • (Rectangle, Rectangle) a pointwise
  • sub-type of (Shape,Shape)

21
Predicate classes
  • Normal classes with predicate expressions
  • Dynamic class hierarchy
  • Implicit property-based classification
  • Based on run-time value,state,etc
  • Classes follow normal inheritance rules

22
Predicate objects - Example
23
GF for predicate dispatching
24
Dispatch Strategy for predicate methods
  • Generic function applied to argument tuple
  • Set of applicable methods found by
  • binding arguments to formals
  • evaluating predicate for each method
  • Most specific method selected
  • m1 Method m2 when m1s predicate implies m2s
  • m1 is atleast as specific as m2
  • Errors
  • message-ambiguous and message-not-understood
    errors

25
Predicate dispatching as a superset
  • All other models a subset of the predicate
    dispatching model
  • Single dispatching
  • formal1_at_Cm
  • Multiple dispatching
  • formal1_at_Classm1 and formal2_at_Classm2 .
  • Predicate classes
  • formali_at_PredClass, PredClass is a subclass of
    Class
  • becomes
  • formali_at_Class and test Test, Test is the
    predicate

26
Algorithm
  • Time and space efficient dispatch functions for
    predicate dispatching model
  • Three stages
  • Reduce general predicate dispatching model to
    simpler multiple dispatching model
  • Strategy for performing multiple-dispatching
    using a series of single dispatches (lookup DAG)
  • Implementation strategies for each of the single
    dispatches

27
First stage
  • Replace all test expressions with Expr_at_True
  • Remove all Name Expr clauses with Expr
  • Convert predicates into disjunctive normal form
  • Replace not(Expr_at_Class)with Expr_at_!Class
  • Place each method ms predicate into canonical
    form
  • Conjunction gt m
  • Remove all atomic tests that are guaranteed to be
    true by static analysis
  • Remove all conjunctions that are guaranteed to be
    false by static analysis
  • Merge any duplicate conjunctions

28
(No Transcript)
29
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com