CS 342 Lecture -*-Outline-*- * Semantic model: objects, environment, assignment, calls (draw pictures) ** Objects objects, are containers for data (other objects) *state + identity state = bits (for built-ins) or pointers to other objects identity, distinct for all objects (an address) created dynamically (on heap) indefinite lifetime (=> garbage collection in real CLU) may contain other objects (i.e., references to others) *draw picture of a point in Pascal: type PointRec = record x_coord:int; y_coord: int end; Point = ^PointRec *** Mutable objects (page 220) state can vary over time e.g., arrays, records, variants lists, points, array, ... allows state changes to be communicated to other parts of program (side-effects, aliasing) specify by abstract values + object identity *** Immutable objects state cannot vary with time e.g., int, pair in figure 6.6 in real CLU: bool, sequence, struct, oneof, procedures identity does not matter, since aliasing cannot be observed specify just abstract values *** Mutable and Immutable types a type is mutable if some of its objects are mutable otherwise the type is called immutable has nothing to do with changing the cluster definition. ** Environment (draw picture) maps names to variables (cells that contain refs to objects) does not map names to cells that contain values *** variables cell is changable ref. to an object, not themselves objects like Pascal: var p1, p2: Point; { a pointer!} new(p); new(p2); p1 := p2. ** Assignment creates an alias, copies reference to object does *not* copy object itself draw picture ** Calling mechanism call by value, where value is always a reference to an object (call by sharing) assign value of argument to formals (makes aliases) ---------- ; example showing mutation and aliasing (define back-rotate (p) ; p:Point ; MODIFIES: p ; EFFECT: rotate p -90 degrees (begin (Point$rotate p) (Point$rotate p) (Point$rotate p))) (define start_up () (begin (set p1 (Point$new 3 5)) (set p2 p1) ; makes p1 and p2 aliases (Point$reflect p1) ; mutates p1 (print (Point$abscissa p2)) ; what is printed here? -3 then -5 (print (Point$ordinate p2)) (back-rotate p1) (print (Point$abscissa p2)) ; what is printed here? 5 then -3 (print (Point$ordinate p2)) (print (Point$abscissa p2)) ; what is printed here? 5 then -3 (print (Point$ordinate p2)))) ----------