On Understanding Data Abstraction ... Revisited 1 1 William R. - - PowerPoint PPT Presentation

on understanding data abstraction revisited
SMART_READER_LITE
LIVE PREVIEW

On Understanding Data Abstraction ... Revisited 1 1 William R. - - PowerPoint PPT Presentation

On Understanding Data Abstraction ... Revisited 1 1 William R. Cook The University of Texas at Austin Dedicated to P. Wegner 2 2 Objects ???? Abstract Data Types 3 3 Warnings! 4 4 No Objects Model the Real World 5 5 No


slide-1
SLIDE 1

1

On Understanding Data Abstraction ... Revisited

1

slide-2
SLIDE 2

2

William R. Cook The University

  • f Texas at Austin

Dedicated to P. Wegner

2

slide-3
SLIDE 3

3

Objects ???? Abstract Data Types

3

slide-4
SLIDE 4

4

Warnings!

4

slide-5
SLIDE 5

5

No “Objects Model the Real World”

5

slide-6
SLIDE 6

6

No Inheritance

6

slide-7
SLIDE 7

7

No Mutable State

7

slide-8
SLIDE 8

8

No Subtyping!

8

slide-9
SLIDE 9

9

Interfaces as types

9

slide-10
SLIDE 10

10

Not Essential

(very nice but not essential)

10

slide-11
SLIDE 11

[ ]

11

discuss inheritance later

11

slide-12
SLIDE 12

12

Abstraction

12

slide-13
SLIDE 13

13 13

slide-14
SLIDE 14

Hidden Visible

14 14

slide-15
SLIDE 15

15

Procedural Abstraction bool f(int x) { … }

15

slide-16
SLIDE 16

16

Procedural Abstraction int → bool

16

slide-17
SLIDE 17

17

(one kind of)

Type Abstraction

∀T.Set[T]

17

slide-18
SLIDE 18

18

Abstract Data Type

signature Set empty

  • : Set

insert

  • : Set, Int → Set

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

18

slide-19
SLIDE 19

Abstract

19

Abstract Data Type

signature Set empty

  • : Set

insert

  • : Set, Int → Set

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

19

slide-20
SLIDE 20

20

Type + Operations

20

slide-21
SLIDE 21

21

ADT Implementation

abstype Set = List of Int empty

  • = []

insert(s, n) = (n : s) isEmpty(s) = (s == []) contains(s, n) = (n ∈ s)

21

slide-22
SLIDE 22

22

Using ADT values

def x:Set = empty def y:Set = insert(x, 3) def z:Set = insert(y, 5) print( contains(z, 2) )==> false

22

slide-23
SLIDE 23

23 23

slide-24
SLIDE 24

Hidden representation: List of Int Visible name: Set

24 24

slide-25
SLIDE 25

25

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

25

slide-26
SLIDE 26

26

Natural!

26

slide-27
SLIDE 27

27

just like built-in types

27

slide-28
SLIDE 28

28

Mathematical

Abstract Algebra

28

slide-29
SLIDE 29

29

Type Theory

∃x.P

(existential types)

29

slide-30
SLIDE 30

30

Abstract Data Type

=

Data Abstraction

30

slide-31
SLIDE 31

31

Right?

31

slide-32
SLIDE 32

32

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

32

slide-33
SLIDE 33

33

Another way

33

slide-34
SLIDE 34

34

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

34

slide-35
SLIDE 35

35

S = { 1, 3, 5, 7, 9 } P(n) = even(n) & 1≤n≤9

35

slide-36
SLIDE 36

36

Sets as characteristic functions

36

slide-37
SLIDE 37

37

type Set = Int → Bool

37

slide-38
SLIDE 38

38

Empty = λn. false

38

slide-39
SLIDE 39

39

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

39

slide-40
SLIDE 40

40

Using them is easy

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

40

slide-41
SLIDE 41

41

So What?

41

slide-42
SLIDE 42

42

Flexibility

42

slide-43
SLIDE 43

43

set of all even numbers

43

slide-44
SLIDE 44

44

Set ADT: Not Allowed!

44

slide-45
SLIDE 45

45

  • r…

break open ADT & change representation

45

slide-46
SLIDE 46

46

set of even numbers as a function?

46

slide-47
SLIDE 47

47

Even = λn. (n mod 2 = 0)

47

slide-48
SLIDE 48

48

Even interoperates

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

48

slide-49
SLIDE 49

49

Sets-as-functions are

  • bjects!

49

slide-50
SLIDE 50

50

No type abstraction required! type Set = Int → Bool

50

slide-51
SLIDE 51

51

multiple methods? sure...

51

slide-52
SLIDE 52

52

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

52

slide-53
SLIDE 53

53

What about Empty and Insert? (they are classes)

53

slide-54
SLIDE 54

54

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

54

slide-55
SLIDE 55

55

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

55

slide-56
SLIDE 56

56

Using Classes

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

56

slide-57
SLIDE 57

57

An object is the set of observations that can be made upon it

57

slide-58
SLIDE 58

58

Including more methods

58

slide-59
SLIDE 59

59

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

59

slide-60
SLIDE 60

Type Recursion

60

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

60

slide-61
SLIDE 61

61

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

61

slide-62
SLIDE 62

Value Recursion

62

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

62

slide-63
SLIDE 63

63

Using objects

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

63

slide-64
SLIDE 64

64

Autognosis

64

slide-65
SLIDE 65

65

Autognosis (Self-knowledge)

65

slide-66
SLIDE 66

66

Autognosis An object can access

  • ther objects only

through public interfaces

66

slide-67
SLIDE 67

67

  • perations
  • n

multiple objects?

67

slide-68
SLIDE 68

68

union

  • f

two sets

68

slide-69
SLIDE 69

69

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

69

slide-70
SLIDE 70

Complex Operation

(binary)

70

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

70

slide-71
SLIDE 71

71

intersection

  • f

two sets ??

71

slide-72
SLIDE 72

72

class Intersection(a, b) { contains(n) { a.contains(n) ∧ b.contains(n); } isEmpty() { ? no way! ? } ... }

72

slide-73
SLIDE 73

73

Autognosis: Prevents some

  • perations

(complex ops)

73

slide-74
SLIDE 74

74

Autognosis: Prevents some

  • ptimizations

(complex ops)

74

slide-75
SLIDE 75

75

Inspecting two representations &

  • ptimizing operations
  • n them are easy

with ADTs

75

slide-76
SLIDE 76

76

Objects are fundamentally different from ADTs

76

slide-77
SLIDE 77

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 : Set Insert : Set x Int → Set Union : Set x Set → Set

77

slide-78
SLIDE 78

s Empty Insert(s', m) isEmpty(s) true false contains(s, n) false n=m ∨ contains(s', n) insert(s, n) false Insert(s, n) union(s, s'') isEmpty(s'') Union(s, s'')

Operations/Observations

78 78

slide-79
SLIDE 79

s Empty Insert(s', m) isEmpty(s) true false contains(s, n) false n=m ∨ contains(s', n) insert(s, n) false Insert(s, n) union(s, s'') isEmpty(s'') Union(s, s'')

79

ADT Organization

79

slide-80
SLIDE 80

s Empty Insert(s', m) isEmpty(s) true false contains(s, n) false n=m ∨ contains(s', n) insert(s, n) false Insert(s, n) union(s, s'') isEmpty(s'') Union(s, s'')

OO Organization

80 80

slide-81
SLIDE 81

81

Objects are fundamental (too)

81

slide-82
SLIDE 82

82

Mathematical

functional representation

  • f data

82

slide-83
SLIDE 83

83

Type Theory µx.P

(recursive types)

83

slide-84
SLIDE 84

84

ADTs require a static type system

84

slide-85
SLIDE 85

85

Objects work well with or without static typing

85

slide-86
SLIDE 86

86

“Binary” Operations? Stack, Socket, Window, Service, DOM, Enterprise Data, ...

86

slide-87
SLIDE 87

87

Objects are very higher-order

(functions passed as data and returned as results)

87

slide-88
SLIDE 88

88

Verification

88

slide-89
SLIDE 89

89

ADTs: construction Objects: observation

89

slide-90
SLIDE 90

90

ADTs: induction Objects: coinduction complicated by: callbacks, state

90

slide-91
SLIDE 91

91

Objects are designed to be as difficult as possible to verify

91

slide-92
SLIDE 92

92

Simulation One object can simulate another! (identity is bad)

92

slide-93
SLIDE 93

93

Java

93

slide-94
SLIDE 94

94

What is a type?

94

slide-95
SLIDE 95

95

Declare variables Classify values

95

slide-96
SLIDE 96

96

Class as type => representation

96

slide-97
SLIDE 97

97

Class as type => ADT

97

slide-98
SLIDE 98

98

Interfaces as type => behavior pure objects

98

slide-99
SLIDE 99

99

Harmful! instanceof Class (Class) exp Class x;

99

slide-100
SLIDE 100

100

Object-Oriented subset of Java: class name used

  • nly after “new”

100

slide-101
SLIDE 101

101

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

101

slide-102
SLIDE 102

102

Smalltalk

102

slide-103
SLIDE 103

103

class True ifTrue: a ifFalse: b ^a class False ifTrue: a ifFalse: b ^b

103

slide-104
SLIDE 104

104

True = λ a . λ b . a False = λ a . λ b . b

104

slide-105
SLIDE 105

105

Inheritance

(in one slide)

105

slide-106
SLIDE 106

A Object Y(G) G Self- reference Δ(A) A Δ Modificatio n Δ(Y(G)) G Δ Y(ΔoG) G Δ Inheritance Δ(Y(G)) G Δ

106

Inheritance

106

slide-107
SLIDE 107

107

History

107

slide-108
SLIDE 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

slide-109
SLIDE 109
  • bjects

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, 1975

109 109

slide-110
SLIDE 110

110

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

  • J. Reynolds

110

slide-111
SLIDE 111

Extensibility Problem (aka Expression Problem)

111

1975 Discovered by J. Reynolds 1990 Elaborated by W. Cook 1998 Renamed by P. Wadler 2005 Solved by M. Odersky (?) 2025 Widely understood (?)

111

slide-112
SLIDE 112

112

Summary

112

slide-113
SLIDE 113

113

It is possible to do Object-Oriented programming in Java

113

slide-114
SLIDE 114

114

Lambda-calculus was the first

  • bject-oriented

language (1941)

114

slide-115
SLIDE 115

115

Data Abstraction

/ \

ADT Objects

115