On Understanding Data Abstraction Revisitedweb.cecs.pdx.edu/~black/OOP/slides/Cook on...

Preview:

Citation preview

1

On UnderstandingData Abstraction

...Revisited

1

2

William R. CookThe University

of Texas at Austin

Dedicated to P. Wegner

2

3

Objects

????

Abstract Data Types

3

4

Warnings!

4

5

No “Objects Model the Real World”

5

6

No Inheritance

6

7

No Mutable State

7

8

No Subtyping!

8

9

Interfacesas types

9

10

NotEssential

(very nice but not essential)

10

[ ]11

discussinheritance

later

11

12

Abstraction

12

13

13

Hidden

Visible

14

14

15

ProceduralAbstraction

bool f(int x) { … }

15

16

ProceduralAbstraction

int → bool

16

17

(one kind of)Type

Abstraction

∀T.Set[T]

17

18

Abstract Data Type

signature Set empty! ! : Set insert! ! : Set, Int → Set isEmpty! ! : Set → Bool contains!! : Set, Int → Bool

18

Abstract

19

Abstract Data Type

signature Set empty! ! : Set insert! ! : Set, Int → Set isEmpty! ! : Set → Bool contains!! : Set, Int → Bool

19

20

Type+

Operations

20

21

ADT Implementation

abstype Set = List of Int empty! ! ! = [] insert(s, n) ! = (n : s) isEmpty(s)! ! = (s == []) contains(s, n) ! = (n ∈ s)

21

22

Using ADT valuesdef x:Set = emptydef y:Set = insert(x, 3)def z:Set = insert(y, 5)print( contains(z, 2) )==> false

22

23

23

Hiddenrepresentation:

List of Int

Visible name: Set

24

24

25

ISetModule = ∃Set.{ empty! ! : Set insert! ! : Set, Int → Set isEmpty! : Set → Bool contains! : Set, Int → Bool}

25

26

Natural!

26

27

just likebuilt-in types

27

28

MathematicalAbstract Algebra

28

29

Type Theory

∃x.P(existential types)

29

30

Abstract Data Type

=Data Abstraction

30

31

Right?

31

32

S = { 1, 3, 5, 7, 9 }

32

33

Another way

33

34

P(n) = even(n) & 1≤n≤9

34

35

S = { 1, 3, 5, 7, 9 }

P(n) = even(n) & 1≤n≤9

35

36

Sets ascharacteristic

functions

36

37

type Set = Int → Bool

37

38

Empty =

λn. false

38

39

Insert(s, m) =

λn. (n=m) ∨ s(n)

39

40

Using them is easydef x:Set = Emptydef y:Set = Insert(x, 3)def z:Set = Insert(y, 5)print( z(2) ) ==> false

40

41

So What?

41

42

Flexibility

42

43

set of alleven numbers

43

44

Set ADT:Not Allowed!

44

45

or… break open ADT

& change representation

45

46

set of even numbers

as afunction?

46

47

Even =

λn. (n mod 2 = 0)

47

48

Even interoperatesdef x:Set = Evendef y:Set = Insert(x, 3)def x:Set = Insert(y, 5)print( z(2) )==> true

48

49

Sets-as-functionsare

objects!

49

50

No type abstraction required!

type Set = Int → Bool

50

51

multiplemethods?

sure...

51

52

interface Set { contains: Int → Bool isEmpty: Bool}

52

53

What about Empty and Insert?

(they are classes)

53

54

class Empty { contains(n) { return false;} isEmpty() { return true;}}

54

55

class Insert(s, m) { contains(n) { return (n=m) ∨ s.contains(n) } isEmpty() { return false }}

55

56

Using Classesdef x:Set = Empty()def y:Set = Insert(x, 3)def z:Set = Insert(y, 5)print( z.contains(2) ) ==> false

56

57

An object is

the set of observations that

can be made upon it

57

58

Including more methods

58

59

interface Set { contains! : Int → Bool isEmpty! : Bool insert ! : Int → Set}

59

TypeRecursion

60

interface Set { contains! : Int → Bool isEmpty! : Bool insert ! : Int → Set}

60

61

class Empty { contains(n) !{ return false;} isEmpty() ! { return true;} insert(n) ! { return ! ! Insert(this, n);}}

61

ValueRecursion

62

class Empty { contains(n) !{ return false;} isEmpty() ! { return true;} insert(n) ! { return ! ! Insert(this, n);}}

62

63

Using objectsdef x:Set = Emptydef y:Set = x.insert(3)def z:Set = y.insert(5)print( z.contains(2) )==> false

63

64

Autognosis

64

65

Autognosis

(Self-knowledge)

65

66

Autognosis

An object can access other objects only through public

interfaces

66

67

operationson

multiple objects?

67

68

unionof

two sets

68

69

class Union(a, b) { contains(n) { a.contains(n) ∨ b.contains(n); } isEmpty() { a.isEmpty(n) ∧ b.isEmpty(n); } ...}

69

Complex Operation(binary) 70

interface Set { contains: Int → Bool isEmpty: Bool insert! ! : Int → Set union! ! : Set → Set}

70

71

intersectionof

two sets??

71

72

class Intersection(a, b) { contains(n) { a.contains(n) ∧ b.contains(n); }

isEmpty() { ? no way! ? } ...}

72

73

Autognosis:Prevents some operations

(complex ops)

73

74

Autognosis:Prevents some optimizations(complex ops)

74

75

Inspecting two representations &

optimizing operations on them are easy

with ADTs

75

76

Objects are fundamentally different

from ADTs

76

ADT(existential types)

SetImpl = ∃ Set . { empty : Set

isEmpty : Set → Bool contains : Set, Int → Bool

insert : Set, Int → Set union : Set, Set → Set

}

77

Object Interface(recursive types)

Set = { isEmpty! : Bool contains! : Int → Bool insert! : Int → Set union! : Set → Set}Empty : SetInsert : Set x Int → SetUnion : Set x Set → Set

77

ss

Empty Insert(s', m)

isEmpty(s) true false

contains(s, n) falsen=m ∨

contains(s', n)

insert(s, n) false Insert(s, n)

union(s, s'') isEmpty(s'') Union(s, s'')

Operations/Observations

78

78

s

Empty Insert(s', m)

isEmpty(s) true false

contains(s, n) falsen=m ∨

contains(s', n)

insert(s, n) false Insert(s, n)

union(s, s'') isEmpty(s'') Union(s, s'')

79

ADT Organization

79

s

Empty Insert(s', m)

isEmpty(s) true false

contains(s, n) falsen=m ∨

contains(s', n)

insert(s, n) false Insert(s, n)

union(s, s'') isEmpty(s'') Union(s, s'')

OO Organization

80

80

81

Objects are fundamental

(too)

81

82

Mathematicalfunctional

representation of data

82

83

Type Theoryµx.P

(recursive types)

83

84

ADTs require a

static type system

84

85

Objects work wellwith or without static typing

85

86

“Binary” Operations?

Stack, Socket, Window, Service, DOM, Enterprise

Data, ...

86

87

Objects arevery

higher-order(functions passed as data and

returned as results)

87

88

Verification

88

89

ADTs: construction

Objects: observation

89

90

ADTs: induction

Objects: coinductioncomplicated by: callbacks,

state

90

91

Objects are designed to be as difficult as possible to verify

91

92

Simulation

One object can simulate another!(identity is bad)

92

93

Java

93

94

What is a type?

94

95

Declare variables

Classify values

95

96

Class as type

=> representation

96

97

Class as type

=> ADT

97

98

Interfaces as type

=> behavior pure objects

98

99

Harmful!

instanceof Class(Class) expClass x;

99

100

Object-Oriented subset of Java:class name used only after “new”

100

101

It’s not an accident that “int” is an ADT

in Java

101

102

Smalltalk

102

103

class True ifTrue: a ifFalse: b ^a

class False ifTrue: a ifFalse: b ^b

103

104

True = λ a . λ b . a

False = λ a . λ b . b

104

105

Inheritance(in one slide)

105

106

AObject

Y(G)

G

Self-reference

Δ(A)AΔ

Modification

Δ(Y(G))GΔ

Y(ΔoG)

GΔInheritance Δ(Y(G))

106

Inheritance

106

107

107

History

107

108

108

User-defined types and

procedural data structures

as complementary approaches to

data abstraction

by J. C. Reynolds

New Advances in Algorithmic

Languages INRIA, 108

objects

Abstract data types User-defined types and

procedural data structures

as complementary approaches to

data abstraction

by J. C. Reynolds

New Advances in Algorithmic Languages

INRIA, 1975109

109

110

110

“[an object with two methods] is more a tour de force than a specimen of clear programming.”

- J. Reynolds

110

111

Extensibility Problem(aka Expression Problem)

111

1975 Discovered by J. Reynolds1990 Elaborated by W. Cook1998 Renamed by P. Wadler2005 Solved by M. Odersky (?)2025 Widely understood (?)

111

112

Summary

112

113

It is possible to do Object-Oriented

programming in Java

113

114

Lambda-calculuswas the first object-oriented language (1941)

114

115

Data Abstraction

/ \  ADT Objects

115

Recommended