Predicate Dispatching: a Uni Ed Theory of Dispatch

Predicate Dispatching: a Uni Ed Theory of Dispatch

Predicate Dispatching: A Uni ed Theory of Dispatch Michael Ernst Craig Kaplan Craig Chamb ers Technical rep ort UW-CSE-98-01-0 2 Department of Computer Science and Engineering UniversityofWashington Seattle, WA, USA 98195-2350 fmernst;csk;[email protected] http://www.cs.washington.edu/research/projects/cecil/ 12 January 1998 Abstract Predicate dispatching generalizes previous metho d dispatch mechanisms by p ermitting arbitrary predicates to control metho d applicability and by using logical implication b etween predicates as the overriding relation- ship. The metho d selected to handle a message send can dep end not just on the classes of the arguments, as in ordinary ob ject-oriented dispatch, but also on the classes of sub comp onents, on an argument's state, and on relationships b etween ob jects. This simple mechanism subsumes and extends ob ject-oriented single and multiple dispatch, ML-style pattern matching, predicate classes, and classi ers, which can all b e regarded as syntactic sugar for predicate dispatching. This pap er intro duces predicate dispatching, gives motivating examples adapted from a prototyp e implementation, and presents its static and dynamic semantics. 1 Intro duction Many programming languages supp ort some mechanism for dividing the b o dy of a generic function into a set of cases, with a declarative mechanism for selecting the right case for each dynamic invo cation of the generic function. Case selection can b e broken down into tests for applicability a case is a candidate for invo cation if its guard is satis ed and overriding which selects one of the applicable cases for invo cation. Ob ject-oriented languages use overloaded metho ds as the cases. A metho d is applicable if the run-time class of the receiver argument is the same as or a sub class of the class on which the receiver is sp ecialized. + Multiple dispatching [BKK 86, Cha92] enables testing the classes of all of the arguments. One metho d over- rides another if its sp ecializer classes are sub classes of the other's, using either lexicographic CLOS [Ste90] or p ointwise Cecil [Cha93a] ordering. Predicate classes [Cha93b] automatically classify an ob ject of class A as an instance of virtual sub class B a sub class of A whenever B 's predicate an arbitrary expression typically testing the runtime state of an ob ject is true. This creation of virtual class hierarchies makes metho d dispatching applicable even in cases where the e ective class of an ob ject maychange over time. Classi ers [HHM90b] and mo des [Tai93] are similar mechanisms for reclassifying an ob ject into one of a numb er of sub classes based on a case-statement- like test of arbitrary b o olean conditions. Pattern matching as in ML [MTH90] bases applicability tests on the run-time datatyp e constructor tags of the arguments and their sub comp onents. As with classi ers and mo des, textual ordering determines + overriding. Some languages, such as Haskell [HJW 92], allow arbitrary b o olean guards to accompany pat- terns, restricting applicability. Views [Wad87] extend pattern matching to abstract data typ es by enabling them to o er various concrete datatyp e-likeinterfaces. Predicate dispatching integrates, generalizes, and provides a uniform interface to these similar but previ- ously incomparable mechanisms. A metho d declaration sp eci es its applicability via a predicate expression, 1 E 2 expr The set of expressions in the underlying programming language T 2 typ e The set of typ es in the underlying programming language c 2 class-id The namespace of classes m; f 2 metho d-id The namespace of metho ds and elds p 2 pred-id The namespace of predicate abstractions v; w 2 var-id The namespace of variables Figure 1: Syntactic domains and variables which is a logical formula over class tests i.e., tests that an ob ject is of a particular class or one of its sub- classes and arbitrary b o olean-valued expressions from the underlying programming language. A metho d is applicable when its predicate expression evaluates to true. Metho d m overrides metho d m when m 's pred- 1 2 1 icate logically implies that of m ; this relationship is computed at compile time. Static typ echecking veri es 2 that, for all p ossible combinations of arguments to a generic function, there is always a single most-sp eci c ap- plicable metho d. This ensures that there are no message-not-understo o d errors called match-not-exhaustive in ML or message-ambiguous errors at run-time. Predicate expressions capture the basic primitive mechanisms underlying a wide range of declarative dispatching mechanisms. Combining these primitives in an orthogonal and general manner enables new sorts of dispatching that are not expressible by previous dispatch mechanisms. Predicate dispatching preserves several desirable prop erties from its ob ject-oriented heritage, including that metho ds can b e declared in any order and that new metho ds can b e added to existing generic functions without mo difying the existing metho ds or clients; these prop erties are not shared by pattern-matching-based mechanisms. Section 2 intro duces the syntax, semantics, and use of predicate dispatching through a series of examples. Section 3 de nes its dynamic and static semantics formally. Section 4 discusses predicate tautology testing, which is the key mechanism required by the dynamic and static semantics. Section 5 surveys related work, and Section 6 concludes with a discussion of future directions for research. 2 Overview This section demonstrates some of the capabilities of predicate dispatching byway of a series of examples. We incrementally present a high-level syntax which app ears in full in Figure 5; Figure 1 lists supp orting syntactic domains. Predicate dispatching is parameterized by the syntax and semantics of the host programming language in which predicate dispatching is emb edded. 2.1 Dynamic dispatch Each metho d implementation has an attached predicate expression which sp eci es when the metho d is ap- plicable. Predicate expressions include class tests and negations, conjunctions, and disjunctions of predicate expressions. An omitted predicate expression indicates that its metho d handles all typ e-correct arguments. metho d-sig ::= signature m h T i : T metho d-decl ::= metho d m h formal-pattern i [ when pred-expr ] metho d-b o dy pred-expr ::= expr @ c succeeds if expr evaluates to an instance or sub class of class c j not pred-expr negation j pred-expr and pred-expr conjunction short-circuited j pred-expr or pred-expr disjunction short-circuited Metho d signature declarations give the typ e signature shared by a family of metho d implementations. A message send expression need examine only the corresp onding metho d signature declaration to determine its typ e-correctness, while a set of overloaded metho d implementations must completely and unambiguously implement the corresp onding signature in order to b e typ e-correct. 2 Predicate dispatching can simulate b oth singly- and multiply-dispatched metho ds by sp ecializing formal parameters on a class via the \@class" syntax. Sp ecialization limits the applicability of a metho d to ob jects that are instances of the given class or one of its sub classes. ML-style pattern matching is mo deled by considering each constructor of a datatyp e to b e a class and sp ecializing metho ds on the constructor classes. More generally, predicate dispatching supp orts the construction of arbitrary conjunctions, disjunctions, and negations of class tests. The following example uses predicate dispatching to implement the Zip function 1 which converts a pair of lists into a list of pairs: type List; class Cons subtypes List { head:Any, tail:List }; class Nil subtypes List; signature ZipList, List:List; method Zipl1, l2 when l1@Cons and l2@Cons { return ConsPairl1.head, l2.head, Zipl1.tail, l2.tail; } method Zipl1, l2 when l1@Nil or l2@Nil { return Nil; } The rst Zip metho d tests the classes of b oth arguments, and it only applies when b oth are instances of Cons or some sub class; this is an implicit conjunction of two class tests. The second Zip metho d uses explicit disjunction to test whether either argument is an instance of Nil or some sub class. The typ e checker can verify statically that the two implementations of Zip are mutually exclusive and exhaustiveover all p ossible arguments that match the signature, ensuring that there will b e no \message not understo o d" or \message ambiguous" errors at run-time, without requiring the cases to b e put in any particular order. ML-style pattern matching requires all cases to b e written in one place and put in a particular total order, resolving ambiguities in favor of the rst successfully matching pattern. In a traditional singly- or multiply-dispatched ob ject-oriented language without the ability to order cases, either the base case of Zip must b e written as the default case for all pairs of List ob jects unnaturally, and unsafely in the face of future additions of new sub classes of the default typ e, or three separate but identical base metho ds must b e written one for NilAny, one for AnyNil, and a third for NilNil to resolve the ambiguitybetween the rst two. In our exp erience with ob ject-oriented languages using a p ointwise, not lexicographic, ordering, these triplicate base metho ds for binary messages o ccur frequently. As a syntactic convenience, class tests can b e written in the formal argument list: formal-pattern ::= [ v ][@ c ] like v @ c in pred-expr The rst Zip metho d ab ovewould then b e rewritten as method Zipl1@Cons, l2@Cons { return ConsPairl1.head, l2.head, Zipl1.tail, l2.tail; } 2.2 Pattern matching Predicates can test the run-time classes of comp onents of an argument, just as pattern matching can query substructures, by suxing the @class test with a record-like list of eld names and corresp onding class tests; names can b e b ound to eld contents at the same time.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    18 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us