Presentation is loading. Please wait.

Presentation is loading. Please wait.

It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction.

Similar presentations


Presentation on theme: "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction."— Presentation transcript:

1 It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction

2 20 March 2001CS 655: Lecture 152 Menu Data Abstraction before CLU Data Abstraction in CLU Reasoning about Data Abstractions –Abstraction Functions –Rep Invariants

3 20 March 2001CS 655: Lecture 153 What is a type? Last time: a set of values Today: an abstraction for decomposing a program that provides a set of operations Sets of values don’t work because you are tied to the representation

4 20 March 2001CS 655: Lecture 154 Data Abstraction in C/Pascal? User-defined types: typedef enum { red = 0, green, blue } color; typedef struct { int locx; int locy; } location; Type checking either: –By structure (e.g., color dumb = 23) –By name (maybe in Pascal, ambiguous) Only way to use type, is to access its representation; no restrictions on where you can do this.

5 20 March 2001CS 655: Lecture 155 Data Abstraction in BLISS? User specifies accessing algorithm for structure elements May modify either structure definition or algorithms without affecting the other structure array[i, j] = (.array +.i * 10 +.j) May define memory allocation routines But: only arrays, no typed elements

6 20 March 2001CS 655: Lecture 156 Data Abstraction in Simula67 Define a class with hidden attributes (visible only in the class implementation) and protected attributes (visible in subclass implementations also) Unfortunately, not widely known: –From Sweden –Few Publications (mostly in Swedish), no language Report, no decent textbook until 1986 Alan Kay learned about Simula by reading the source code, thinking it was an Algol compiler! –Big influence on Smalltalk and C++; small influence on CLU

7 20 March 2001CS 655: Lecture 157 Providing Data Abstraction Type check by name Restrict what code can access the representation of a data type –CLU, Alphard: only operations of the type –Other (possibly) reasonable answers: C++: allow functions outside the type that are declared friend s to access representation C with LCLint: in files and functions according to a naming convention, elsewhere when explicitly annotated [Stata97]: operations can access the only some of the representation

8 20 March 2001CS 655: Lecture 158 Data Abstraction in CLU intmap = data type is create, insert, lookup Operations create = proc () returns (intset) effects Returns a new, empty intmap. insert = proc (s: intmap, k: string, val: int) requires s does not have a key k. modifies s effects s post maps k to val. lookup = proc (s: intmap, k: string) returns (int) requires There is a key k in s. effects Returns value associated with key in s. Rest of program sees black box described by specification.

9 20 March 2001CS 655: Lecture 159 Black-box Interface intmap down (intmap) returns (rep) rep = representation of intmap Only code in the cluster implementing intmap can call intmap$up or intmap$down. up (rep) returns (intmap) There is nothing else special about code in the cluster!

10 20 March 2001CS 655: Lecture 1510 Parameterized Data Abstractions Don’t want to implement stringintmap, stringrealmap, intintmap, etc. Value Parameters: –Don’t want to implement Factorial2 (), Factorial3 (), Factorial4 (),... –Implement Factorial (n: int) Type Parameters: –Implement map[tkey: type, tval: type] Problem: how will we implement lookup if we don’t know anything about tkey?

11 20 March 2001CS 655: Lecture 1511 Specification map = data type [tkey: type, tval: type] is create, insert, lookup Requires tkey has an operation equal: proctype (t, t) returns (bool) that is an equivalence relation on t. Operations create = proc () returns (map) effects Returns a new, empty map. insert = proc (s: map, k: tkey, val: tval) requires s has no key k’ such that tkey$equal (k, k’). modifies s effects lookup (s post. k) = val. lookup = proc (s: map, k: tkey) returns (tval) requires s has a key k’ in s such that tkey$equal (k, k’). effects Returns value associated with k in s.

12 20 March 2001CS 655: Lecture 1512 Where Clauses map = cluster [tkey: type, tval: type] is create, insert, lookup where tkey has equal: proctype (tkey, tkey) returns bool Used in implementation, not specification. Checked by compiler.

13 20 March 2001CS 655: Lecture 1513 Implementing Data Abstractions Need a concrete representation map = cluster [tkey: type, tval: type] is create, insert, lookup where tkey has equal: proctype (tkey, tkey) returns (bool) pair = record [key: tkey, value: tval] rep = array [pair] create = proc () returns (map) return end create up(rep$new ())

14 20 March 2001CS 655: Lecture 1514 Implementing map insert = proc (m: map, k: tkey, v: tval) % Better spec would remove requires % clause and signal exception if key % is already in the map. down (m).addh (pair${key: k, value: v}) end insert

15 20 March 2001CS 655: Lecture 1515 Printing maps map = cluster [tkey: type, tval] is... unparse... unparse = proc (m: map) returns (string) where tkey has unparse: proctype (tkey) returns (string) tval has unparse: proctype (tval) returns (string) Why put the where clause about equal on the cluster instead of member operation?

16 20 March 2001CS 655: Lecture 1516 CLU: Special Types bool –Language control structures (if, while) depend on type bool int, char, real, string, null –Built-in language support for literals record, struct, variant, oneof, array, sequence –Special constructor syntax T${ … } any –Union of all possible types, use force to convert (with checking) to actual type

17 20 March 2001CS 655: Lecture 1517 CLU Operators Assignment (:=) –Always means sharing (recall immutable types) –Types by name must match Everything else is syntactic sugar, all types can use: 3 + 2  int$add (3, 2) m1,m2: map[string,int] m1 + m2  map[string,int]$add (m1, m2) ai: array[int] ai[n] := ai[n-1]  array[int]$store (ai, n, array[int]$fetch (ai, n-1)) Four exceptions: up, down, cand, cor

18 Questions? Next: Reasoning About Data Abstractions

19 20 March 2001CS 655: Lecture 1519 Reasoning about Data Abstractions They are abstractions – need to invent a formal notation ( A ) for describing them They have representations – need to define a mapping from concrete representation to that formal notation Abstraction Function: A: rep  A

20 20 March 2001CS 655: Lecture 1520 Describing maps A map can be described by a sequence of (key, value) pairs with unique keys: [ (key 0, value 0 ), (key 1, value 1 ), … ] such that if key = key i the value associated with key is value i. A: rep  [(key 0, value 0 ), (key 1, value 1 ), …]

21 20 March 2001CS 655: Lecture 1521 Abstraction Function A: array [record [key: tkey, value: tval]]  [(key 0, value 0 ), (key 1, value 1 ), …] A(r) = [(r[rep$low(r)].key, r[rep$low(r)].value), (r[rep$low(r) + 1].key, r[rep$low(r)+1].value),... (r[rep$high(r)].key, r[rep$high(r)].value)] Problem: What if r contains duplicate keys?

22 20 March 2001CS 655: Lecture 1522 Rep Invariant “It better not!” I : rep  Boolean I (r) = (r[i].key = r[k].key implies i = k)

23 20 March 2001CS 655: Lecture 1523 Reasoning with Rep Invariants Prove by induction, for a datatype t: 1.For each operation that creates new t: prove that returned reps r of returned t satisfies I (r) 2.For each cluster operation: assume all t objects passed as parameters satisfy have reps r that satisfy I (r), prove they do at all cluster exit points. Argue that only cluster operations can alter the rep, so if you can prove invariant holds for all cluster operations, it must always hold.

24 20 March 2001CS 655: Lecture 1524 What can go wrong? map = cluster [tkey: type, tval: type] is... choose = proc (m: map) returns (record [key: tkey, value: tval]) % requires m is not empty. % effects Returns a (key, value) pair in m. return (down (m)[rep$low (down(m))] end choose p = proc (m: map[string, int]) map[string,int]$insert (m, “duplicate”, 3) pair p := map[string,int]$choose (m) p.key = “duplicate” end p

25 20 March 2001CS 655: Lecture 1525 Rep Exposure Can’t share mutable objects in data representations Sharing immutable objects is okay Could compiler prevent this? –Yes, pretty easy Why doesn’t CLU compiler prevent this? –Sometimes efficiency requires rep exposure –e.g., create a map by passing in an array of pairs

26 20 March 2001CS 655: Lecture 1526 The programming community must soon come to terms with the topics that they address, including: What are the qualitative and quantitave effects of strong type- checking? How do verification considerations affect language design? What abstraction mechanisms should languages provide? How can security of high-level languages be extended to real- time applications? Jim Horning, 1977 (from 6 March manifest)

27 20 March 2001CS 655: Lecture 1527 Charge Read Stroustrup and Smalltalk papers before class Thursday Think about what Object-Oriented Programming really means – could Stroustrup and Ingalls really be writing about the same thing? Is CLU Object-Oriented? If not, would adding syntactic sugar make CLU object- oriented?


Download ppt "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction."

Similar presentations


Ads by Google