CS 541 Lecture -*- Outline -*- * multi-methods and Cecil-like languages ** the toy language INST *** Point example ---------------------------- MULTI-METHODS IN INST type Point; class PointClass implements Point instance variables x: Integer; y: Integer; end; method x(p @ PointClass : Point):Integer is skip return p.x; method y(p @ PointClass : Point):Integer is skip return p.y; method set_x(p @ PointClass : Point, i @ Integer : Integer): Point is p.x := i return p; method set_y(p @ PointClass : Point, i @ Integer : Integer): Point is p.y := i return p; ------------------------------- explain the notation say how to type-check a method, because can access instance variables of a formal that is specialized parameters must be passed by constant. idea is that classes and types are orthogonal. ------------------------------- method mkPoint(a @ Integer : Integer, b @ Integer : Integer): Point is p : Point; p := new PointClass; set_x(p, a); set_y(p, b); return p; ---------------------------- Note the use of an auxiliary method, in place of variable declarations in method bodies ---------------------------------- MULTI-METHOD CALL SEMANTICS Example: observe myPt: Point; myPt := mkPoint(3,4) by output: Integer; set_x(myPt, 7); output := x(myPt) General case for call: f(e1, ..., en) 1. Let c-vec = (c1, ..., cn) be the classes of the e1, ..., en 2. There must just 1 a method named f, that is specialized on (c1, ..., cn) 3. Bind the actuals to the formals and execute that method's body Multi-Methods can access instance vars of arguments they specialize. ---------------------------------- ---------------------------------- class ColorPointClass implements Point instance variables x: Integer; y: Integer; c: Integer end; method x(p @ ColorPointClass : Point) : Integer is skip return p.x; method y(p @ ColorPointClass : Point) : Integer is skip return p.y; method set_x( p @ ColorPointClass : Point, i @ Integer : Integer): Point is p.x := i return p; method set_y( p @ ColorPointClass : Point, i @ Integer : Integer): Point is p.y := i return p; ----------------------------------- ------------------------------------ method c(p @ ColorPointClass : Point) : Integer is skip return p.c; method set_c(p @ ColorPointClass : Point, i @ Integer : Integer): Point is p.c := i return p; method mkColorPoint(a @ Integer : Integer, b @ Integer : Integer, c @ Integer : Integer): Point is p: Point; p := new ColorPointClass; set_x(p, a); set_y(p, b); set_c(p, c); return p; ---------------------------- *** binary multi-methods ------------------------------ MULTI-METHODS FOR EQUAL method and(e1 @ Integer : Integer e2 @ Integer : Integer): Integer is b: Integer; b := e1; if e1 then b := e2 else skip fi return b; method equal(p1 @ PointClass : Point, p2 @ PointClass : Point): Integer is skip return and(equal(p1.x, p2.x), equal(p1.y, p2.y)); method equal(p1 @ ColorPointClass : Point, p2 @ PointClass : Point): Integer is skip return and(equal(p1.x, p2.x), equal(p1.y, p2.y)); method equal(p1 @ PointClass : Point, p2 @ ColorPointClass : Point): Integer is skip return and(equal(x(p1), x(p2)), equal(y(p1), y(p2))); method equal(p1 @ ColorPointClass : Point, p2 @ ColorPointClass : Point): Integer is skip return and(equal(x(p1), x(p2)), equal(y(p1), y(p2))); method eqCP(p1 @ ColorPointClass : Point, p2 @ ColorPointClass : Point): Integer is skip return and(equal(p1, p2), equal(c(p1), p2.c)); ------------------------------ Note the variations in whether equal uses the instance vars directly. ----------------------------- HOW MULTI-METHODS SOLVE THE BINARY METHOD PROBLEM - symmetric - privileged access to all args - get to say what happens in all cases ------------------------------ ** improvements *** Inheritance --------------------------------- INHERITANCE Add subclasses and inheritance CD ::= class I inherits C* implements T instance variables VD end Example: class ColorPointClass inherits PointClass implements Point instance variables c: Integer end; -- just provide multi-methods c, set_c, -- and mkColorPoint -- inherits x, set_x, y, set_y -- following replaces all 4 defs. of equal method equal(p1 @ PointClass : Point, p2 @ PointClass : Point): Integer is skip return and(equal(x(p1), x(p2)), equal(y(p1), y(p2))); method eqCP(p1 @ ColorPointClass : Point, p2 @ ColorPointClass : Point): Integer is skip return and(equal(p1, p2), equal(c(p1), p2.c)); ---------------------------------- ----------------------------------- MULTI-METHOD LOOKUP WITH INHERITANCE Example: observe myPt: Point; myPt2: Point; myPt := mkPoint(3,4); myPt2 := MkColorPoint(3,4,5); by output: Integer; output := equal(myPt, myPt2) Ordering on class vectors (c1, ..., c2) <= (d1, ..., dm) iff m = n, and for each 1 <= i <= n, ci <= di Let specl(f) be the tuple of class specializers of f. General case for call: f(e1, ..., en) 1. Let c-vec = be the classes of the e1, ..., en 2. Let fs be the set of methods named f for each g in fs, c-vec <= specl(g) 3. It is an error if fs has no members. It is an error if there is no least element in fs. Otherwisse, there is a unique least element of fs, call it g-least. 3. Bind the actuals to the formals and execute gu's body ------------------------------------ *** type checking (if time) Go over the type rules, based on the grammar