CS 342 Lecture -*-Outline-*- * CLU basics ** syntax (page 213) most the same as chapter 1 so no symbols, no lists (built in), no quote... but there is a List cluster in the library only numbers so false is 0 again, 1 used for true operator names names have 2 flavors one part names: inc global funs two part names Tree$left ops of clusters clusters (cluster T ; Export: op1, op2... comment convention (rep var1 var2 ...) (define op1 (x) ...) (define op2 (y z) ...) ... ) outside the cluster, can call operations by (T$op1 arg), (T$op2 arg1 arg2) ... Point example on page 211 ** semantics (6.2.2) first benefit of cluster, grouping of related operations prevents namespace confusion only names of clusters globally known outside cluster, operation names have to be called by T$op1, not op1 a cluster defines a data type rep defines structure of record (cluster T (rep f1 f2 ...) something like type declarations in Pascal type T = record f1:T1; f2:T2; ... end; think of the variable names following rep as names of fields but in addition to the data structure, also define operations on that data structure abstract data type = data structure + operations (algs) put all the operations needed to *directly* manipulate the data structure in the cluster e.g., Point$new creates a new point Point$abscissa, Point$ordinate, Point$compare, Point$quadrant observe the state of a point Point$reflect, Point$rotate changes the state of a point will usually have these 3 kinds of operations application specific operations go outside the cluster e.g., enclosed-area on page 212. Why? to allow changes to the representation for efficiency, etc. e.g., figure 6.3 on page 214 *** inside a cluster to keep the details of the data structure inside the cluster, information hiding, a cluster has privilaged access to the data structure How? by only allowing direct manipulation of the data structure from within the text of the cluster direct manipulation of the representation: inside a cluster (cluster T (rep f1 f2 f3) ... ; here ) the following privilaged operations are defined selectors: f1, f2, f3 are functions that take an object of type T and return the corresponding field in Pascal: type TRec = record f1:T1; f2:T2; f3:T3 end; T = ^TRec; var x:T; x^.f1 corresponds to CLU (f1 x) y^.f2 (f2 y) aT^.f3 (f3 aT) so type of f1 is T -> T1... settors: set-f1, set-f2, set-f3 are functions that assign to the fields of an object of type T x^.f1 := e corresponds to CLU (set-f1 x e) y^.f2 := 32 (set-f2 y 32) aT^.f3 := g(z) (set-f3 aT (g z)) creator: the name of the cluster, T, is considered within the cluster to be the name of a function that creates a new object of type T var y:T; corresponds to CLU (set y (T e1 e2 e3)) new(y); y^.f1 := e1 y^.f2 := e2 y^.f3 := e3 look again at the two Point examples, page 211 and 214 *** scope scope in a cluster: within a cluster, the privilaged operations are defined also the one part names of other functions defined inside the cluster can be used to call other functions defined in the cluster (even those not exported) (can also use the 2 part names from within) outside a cluster: the privilaged operations (selectors, settors, constructor) cannot be used (allows the names of fields, how many fields, etc. to be changed without affecting clients) other functions defined in the cluster must be called with the two part name. other scope rules just as in chapter 1 (and LISP) global functions have global scope. variables not declared local to a function are global *** examples (6.2.4) look at lists and arrays