CS 342 Lecture -*- Outline -*- * Data Abstraction a kind of abstraction by specification key to CLU, explains most of the language half of OOP ** computer systems are abstract data types ADT = state (perhaps several objects) + commands (operations) i.e., data structure + algorithms examples: editors, integers, real computers, prog. langauges,... ** Hiding the representation (thinking abstractly about data structures) Why? to separate concerns (only think about 1 thing in 1 place) to allow your program to be more easily modified almost all modifications change data structure key idea is a what (behavior) vs. how (implementation) distinction *** representation is a choice of data structure e.g. lists or arrays for sets 2's complement numbers for ints records with x and y for points lists of lists for relations (in RDB example) *** representation independence client code is rep independent if when representation is changed, client's behavior doesn't change e.g., code for =sets that uses subset test only... important for clarity, portability, correctness, even efficiency, because changing data structure usually important for efficiency why would client want to know representation? ** examples *** int abstract values: contiguous subset of mathematical integers that includes 0 notation for abstract values: digits operations: +, <, =, ... specification of each operation given by math dept. (an integral domain) *** notes (degrees of a scale) abstract values: (small) integers notation: digits operations: ---------------- create: int -> note degree: note -> int equal, lower, higher: note * note -> bool closest: note * note * note -> note thirdAbove, fifthAbove, octaveAbove, octaveBelow: note -> note display: note -> () ---------------- *** points (of integers) objects are mutable abstract value of a typical point: pair of x and y coordinates notation: operations and specs (see page 210): new, abscissa, ordinate, reflect, rotate, compare, quadrant client code examples see page 211 at bottom *** chords objects are mutable abstract value of a typical chord: 4-tuple of notes: a bass, tenor, alto, and soprano with bass < tenor < alto < soprano and for v in {tenor, alto, soprano} there is some i in {0,2,4} and some integer k such that degree(v) = degree(bass) + i + k*7 notation: ------------- (cluster Chord ; Export: create, soprano, alto, tenor, bass, display ; SUMMARY: 4-part chords (immutable) (rep b t a s) ; ABSTRACTION FUNCTION: r: rep represents a chord with bass b, ; tenor t, alto a, and soprano s ; REP INVARIANT: b < t < a < s (define create (bs ten alt sop) ; TYPE: Note * Note * Note * Note -> Chord ; REQUIRES: bs < ten < alt < sop ; EFFECT: return the chord with bass bs, tenor ten, alto alt, soprano sop (Chord bs ten alt sop)) (define soprano (c) ; TYPE: Chord -> Note ; EFFECT: Return the soprano note of c (s c)) (define alto (c) ; TYPE: Chord -> Note ; EFFECT: Return the alto note of c (a c)) (define tenor (c) ; TYPE: Chord -> Note ; EFFECT: Return the tenor note of c (t c)) (define bass (c) ; TYPE: Chord -> Note ; EFFECT: Return the bass note of c (b c)) (define display (c) ; TYPE: Chord -> () ; EFFECT: display the sop, alto, tenor and bass of c (in that order) (begin (Note$display (s c)) (Note$display (a c)) (Note$display (t c)) (Note$display (b c)))) ) ; Chord ------------ Point of this example is not the different representations, but that the invariant is maintained... ** Clusters (modules that implement data abstractions) cluster (see Point example) define rep type (data structure), implement operations *** operation export and hiding export list, operations not listed are hidden rep type and its operations typically hidden (rep type is not equal to the abstract type) *** representation (rep) data structure used to represent the objects in our language, some "instance variables" e.g., for points instance vars x-coord and y-coord e.g., for chords instance vars b,t,a,s representation invariant: property of representing objects for chords , that (b c) < (t c) < (a c) < (s c) etc. What does a rep object represent? abstraction function: map from rep to abstract values, defined whenever rep invariant is true e.g., for chords c represents <(b c), (t c), (a c), (s c)>