$ISPATCHING 0REDICATE�$ISPATCHING� Select one case from a generic function – object-oriented dispatch, including multi-methods !�5NIFIED�4HEORY�OF�$ISPATCH – classifiers, predicate classes – pattern matching Michael Ernst, Craig Kaplan, Case selection and Craig Chambers – applicability – overriding University of Washington Static checking Seattle, Washington, USA – completeness http://www.cs.washington.edu/research/projects/cecil – uniqueness Ernst, ECOOP ’98, page 1 Ernst, ECOOP ’98, page 2 'OALS /UTLINE Unify and generalize dispatching mechanisms Predicate dispatching Small, orthogonal basic model Examples Functions are extensible Semantics Static checking Solution: predicate dispatching Ernst, ECOOP ’98, page 3 Ernst, ECOOP ’98, page 4 !PPLICABILITY /VERRIDING Predicates are boolean formulas over class tests Overriding is determined by logical implication and program expressions method draw(p) when p@Point pred ::= expr @ class | test( expr ) | let var := expr method draw(p) | not pred method draw(p) when p@Point | pred or pred when p@ColorPoint and test(p.y==0) | pred and pred method draw(p) method draw(p) when p@Point and test(p.y == 0) when p@ColorPoint { draw point on x axis } and test(p.y==0) Ernst, ECOOP ’98, page 5 Ernst, ECOOP ’98, page 6 Ernst, ECOOP ’ 98, 23 July 1998 1
/BJECT�ORIENTED�DISPATCH -ULTI�METHOD�DISPATCH �3MALL4ALK��#����*AVA� �#,/3��#ECIL��$YLAN� Applicability: run-time class is subclass of Applicability: run-time classes are subclasses specializer of specializers Overriding: subclassing (most specific Overriding: subclassing over all specializers specializer) method equal(p1, p2) class Point; when p1@Point and p2@Point method draw(p) when p@Point { … }; { return p1.x==p2.x and p1.y==p2.y; } class ColorPoint extends Point; method equal(p1, p2) method draw(p) when p@ColorPoint { … }; when p1@ColorPoint and p2@ColorPoint { return p1.x==p2.x and p1.y==p2.y Equivalently: method draw(p@Point) { … }; and p1.color==p2.color; } method draw(p@ColorPoint) { … }; Ernst, ECOOP ’98, page 7 Ernst, ECOOP ’98, page 8 #LASSIFIERS 0ATTERN�MATCHING �+EA��#ECIL� �-,��(ASKELL� Applicability: boolean conditions of runtime state Applicability: structural equivalence Overriding: lexical order Window Overriding: lexical order FullScreen Big Small fun sumList (nil) = 0 | sumList (h::t) = h + sumList(t); classify(w@Window) as FullScreen when test(w.area() == RootWindow.area()) method sumList(l) when l@Nil as Big when test(w.area() > RootWindow.area()/2) { return 0; } as Small otherwise; method sumList(l) when l@Cons and let h:=l.head and let t:=l.tail method move(w) when w@FullScreen { do nothing } { return h + sumList(t); } method move(w) when w@Big { move wireframe outline } method move(w) when w@Small { move opaque window } Ernst, ECOOP ’98, page 9 Ernst, ECOOP ’98, page 10 0REDICATE�ABSTRACTIONS .EW�CAPABILITY��DISJUNCTION Capabilities of in-line predicates: zip : given a pair of lists, return a list of pairs – return boolean value – bind variables method zip(l1, l2) when l1@Nil or l2@Nil predicate On_x_axis(p) { return Nil; } when (p@CartesianPoint and test(p.y == 0)) or (p@PolarPoint method zip(l1, l2) and (test(p.theta == 0) or test(p.theta == pi))) when l1@Cons and l2@Cons { return Cons(Pair(l1.head, l2.head), method draw(p) when p@Point { … } zip(l1.tail, l2.tail)); } method draw(p) when On_x_axis(p) { … } Ernst, ECOOP ’98, page 11 Ernst, ECOOP ’98, page 12 Ernst, ECOOP ’ 98, 23 July 1998 2
2UN�TIME�BEHAVIOR 4YPECHECKING Applicability: evaluate predicates (at run time) Completeness: Informally, disjunction of predicates = true Optimization: common subexpression elimination Overriding: logical implication Uniqueness: for each pair of predicates, Computed at compile time, used at run time – disjoint, – one subsumes the other, or – their intersection is overridden Ernst, ECOOP ’98, page 13 Ernst, ECOOP ’98, page 14 #OMPARING�PREDICATES &UTURE�WORK Compile-time tautology tests More efficient implementation strategies In general: undecidable Separate typechecking Black-box program expressions: NP-hard Small predicate expressions: fast Consistent classes: complete Ernst, ECOOP ’98, page 15 Ernst, ECOOP ’98, page 16 #ONTRIBUTIONS�OF PREDICATE�DISPATCHING Generalizes and subsumes previous techniques in a common framework Enables new varieties of tests Implementation: http://www.cs.washington.edu/research/projects/cecil/www/Gud Ernst, ECOOP ’98, page 17 Ernst, ECOOP ’ 98, 23 July 1998 3
Recommend