Foundations of Object-Oriented Programming

RALF HINZE

Background Foundations of Object-Oriented Programming FVOP FEOP

FOOP Introduction Objects RALF HINZE Classes Open Recursion Institut f¨urInformatik III, Universit¨at Bonn Delegation ¨omerstraße 164, 53117 Bonn Inheritance Email: [email protected] Conclusion Homepage: http://www.informatik.uni-bonn.de/~ralf Appendix

February 2007

(Pick up the slides at . . . /~ralf/talks.html#54.)

1 / 28 Foundations of Vision Object-Oriented Programming

RALF HINZE

Background Possible future course “Foundations of Programming”: FVOP

I FVOP: Foundations of Value-Oriented Programming; FEOP

I FEOP: Foundations of Effect-Oriented Programming; FOOP Introduction I FOOP: Foundations of Object-Oriented Programming. Objects Classes Open Recursion Subtyping Delegation Inheritance Conclusion Appendix

A large part of the material is classroom-tested:

I advanced course on “Principles of Programming Languages”;

I introductory course on “Programming”.

2 / 28 Foundations of Concept Object-Oriented Programming

RALF HINZE You can never understand one language until you understand at least two. Background Ronald Searle (1920–) FVOP FEOP

Make everything as simple as possible, FOOP but not simpler. Introduction Objects Albert Einstein (1859–1955) Classes Open Recursion Subtyping Delegation I Idea: explain programming language concepts by growing a “teaching Inheritance language”: Conclusion

I empty language, Appendix I functional language, I imperative language, I object-oriented language;

I define everything precisely: syntax and semantics;

I concentrate on essential concepts and ideas;

I guiding principle: a concept should be as simple and pure as possible;

I the concepts should be orthogonal;

I use the teaching language to explain features of “real programming languages”.

3 / 28 Foundations of Foundations of Value-Oriented Programming Object-Oriented Programming

RALF HINZE

Background local FVOP val base = 8 FEOP FOOP in Introduction function digits (n : Nat): List hNati = Objects Classes if n 0 then Nil Open Recursion else Cons (n % base, digits (n ÷ base)) Subtyping Delegation Inheritance function nat (ds : List hNati): Nat = Conclusion case ds of Appendix Nil ⇒ 0 | Cons (, ds0) ⇒ d + base ∗ nat (ds0) end end

Introduces expressions, declarations, the concept of etc. Advanced topics: type abstraction, polymorphism, contracts etc.

4 / 28 Foundations of Foundations of Effect-Oriented Programming Object-Oriented Programming

RALF HINZE

Background

FVOP

FEOP

local FOOP val bal = ref 0 Introduction Objects in Classes function deposit (amount : Nat): () = Open Recursion Subtyping bal := !bal + amount Delegation Inheritance function withdraw (amount : Nat): () = Conclusion bal := !bal − amount Appendix function balance () : Nat = !bal end

Introduces IO, state, exceptions, the concept of extent etc.

5 / 28 Foundations of Table of contents Object-Oriented Programming

RALF HINZE

1 Background Background FVOP 2 Foundations of Value-Oriented Programming FEOP FOOP Introduction 3 Foundations of Effect-Oriented Programming Objects Classes Open Recursion 4 Foundations of Object-Oriented Programming Subtyping Delegation Introduction Inheritance Objects Conclusion Appendix Classes Open Recursion Subtyping Delegation Inheritance Conclusion

5 Appendix

6 / 28 Foundations of Introduction — Jargon Object-Oriented Programming

RALF HINZE

Background

FVOP Every subject has its jargon, object-oriented programming is no exception: FEOP abstract class, anonymous class, behaviour, class hierarchy, class method, FOOP Introduction class variable, class, constructor, delegation, dispatch table, down cast, Objects dynamic binding, dynamic dispatch, encapsulation, extension, field, final, Classes Open Recursion friend, generic class, implementation, inclusion polymorphism, inheritance, Subtyping Delegation inner class, instance variable, instance, interface inheritance, interface, late Inheritance binding, , method invocation, method, , Conclusion name subtyping, new, object creation, object, object-oriented, open Appendix recursion, overriding, package, private, protected, public, redefinition, self, structural subtyping, subclass, subtype polymorphism, subtyping, super, superclass, this, up cast, (virtual) method table, visibility. Object-orientation seems to be complex and loaded. To help de-mystify the subject, we shall identify the essential principles or characteristics.

7 / 28 Foundations of Characteristics — What is object-oriented programming? Object-Oriented Programming

I Dynamic dispatch: RALF HINZE

When an operation is invoked on an object, the object itself Background

determines what code gets executed. Two objects with the same FVOP

interface may be implemented quite differently. FEOP

I Encapsulation: FOOP Introduction The implementation of an object is hidden from view. Changes to the Objects implementation can only affect the object itself. Classes Open Recursion I Open recursion: Subtyping Delegation One method can invoke another method via a special identifier called Inheritance self , which is late-bound — dynamic dispatch for recursive invocations. Conclusion Appendix I Subtyping: An object that supports more operations can be used as an object that supports less operations. The ability to ignore parts of an interface allows us to write general code that manipulates different sorts of objects in a uniform way.

I Inheritance: The behaviour of an object can be reused in another object so that common behaviour must be implemented just once. This reuse of behaviour can be achieved

I via objects and delegation or I via classes and subclassing.

8 / 28 Foundations of Recapitulation: encapsulation in an effect-oriented language Object-Oriented Programming

RALF HINZE Encapsulation is achieved by delimiting the scope of internals — only Background entities that have a name can be accessed and reused. FVOP

FEOP local FOOP val bal = ref 0 Introduction in Objects Classes function deposit (amount : Nat): () = Open Recursion Subtyping bal := !bal + amount Delegation Inheritance function withdraw (amount : Nat): () = Conclusion bal := !bal − amount Appendix function balance () : Nat = !bal end

The representation of a bank account, the reference cell bal, is local to deposit, withdraw and balance. Observation: The functions deposit, withdraw and balance belong together, but they are only loosely coupled.

9 / 28 Foundations of Objects Object-Oriented Programming Wish: linguistic support for integrating operations into a single entity. RALF HINZE

Background

val my-account = FVOP

object FEOP local FOOP Introduction val bal = ref 0 Objects Classes in Open Recursion method deposit (amount : Nat): () = Subtyping Delegation bal := !bal + amount Inheritance Conclusion method withdraw (amount : Nat): () = Appendix bal := !bal − amount method balance : Nat = !bal end end

The entity is called an object. As a first approximation an object can be seen as a record of functions. Syntactic changes: object ... end bracket; method instead of function.

10 / 28 Foundations of Method invocation Object-Oriented Programming

RALF HINZE

Background An integrated function aka method is invoked using the dot notation. FVOP FEOP (Recall: ≫ is the prompt of the evaluator). FOOP Introduction my-account.deposit (4711) Objects ≫ Classes () Open Recursion Subtyping ≫ my-account.withdraw (815) Delegation () Inheritance Conclusion ≫ my-account.withdraw (2765) Appendix () ≫ my-account.balance 1131

Jargon: we say “invoking a method on an object” or “sending an object a message”.

11 / 28 Foundations of Interfaces Object-Oriented Programming

RALF HINZE

Background Every well-formed expression has a type; object ... end is an expression; FVOP so what is the type of an object? FEOP

FOOP type Account = Introduction Objects object Classes method deposit : Nat → () Open Recursion Subtyping method withdraw : Nat → () Delegation Inheritance method balance : Nat Conclusion end Appendix

Recall: a type represents our static knowledge of an expression. An object is solely defined by its behaviour; the object’s internal representation does not appear in its type! An object type such as Account is also called an interface. An interface is the set of operations an object supports.

12 / 28 Foundations of Objects and Interfaces Object-Oriented Programming

RALF HINZE A different implementation of a bank account with the same interface: Background

FVOP

val your-account = FEOP

object FOOP local Introduction Objects val n = ref 0 Classes Open Recursion val history = array [10] i ⇒ ref 0 Subtyping Delegation function inc (n : Nat): Nat = (n + 1)% 10 Inheritance in Conclusion method deposit (amount : Nat): () = Appendix history.[inc (!n)] := !history.[!n] + amount; n := inc (!n) method withdraw (amount : Nat): () = history.[inc (!n)] := !history.[!n] − amount; n := inc (!n) method balance : Nat = !history.[!n] end end

13 / 28 Foundations of Demo Object-Oriented Programming

RALF HINZE

Background

FVOP ≫ my-account.balance 1131 FEOP FOOP ≫ your-account.deposit 4711 Introduction () Objects Classes ≫ my-account.withdraw 1000; your-account.deposit 1000 Open Recursion () Subtyping Delegation ≫ my-account.balance Inheritance 131 Conclusion Appendix ≫ your-account.balance 5711

The objects my-account and your-account are interchangeable — at least from the point of view of the language. They respond to the same messages. Each object itself determines what code gets executed.

14 / 28 Foundations of Objects and Interfaces Object-Oriented Programming

RALF HINZE

The fact that objects with the same interface are interchangeable allows us Background to write code that manipulates objects in a uniform way. FVOP

FEOP function transfer (account1 : Account, amount : Nat, account2 : Account): () = FOOP account1.withdraw amount; Introduction account .deposit amount Objects 2 Classes Open Recursion Subtyping Delegation Inheritance We can transfer money between bank accounts, even if the bank Conclusion accounts are implemented differently. Appendix

≫ (my-account.balance, your-account.balance) (131, 5711) ≫ transfer (your-account, 815, my-account) () ≫ (my-account.balance, your-account.balance) (946, 4896)

15 / 28 Foundations of Objects — abstract syntax Object-Oriented Programming

RALF HINZE

Background

FVOP

FEOP

FOOP We extend our language with anonymous objects and method invocations. Introduction Objects Classes Open Recursion m ∈ Method method declarations Subtyping Delegation e ::= ··· Inheritance | object m end anonymous object Conclusion | e.x method invocation Appendix

An object is a collection of methods.

16 / 28 Foundations of Methods — abstract syntax Object-Oriented Programming

RALF HINZE

Background

FVOP

FEOP A method declaration is essentially a sequence of method definitions. FOOP Introduction Objects m ::= method x : τ = e method definition Classes | m1 m2 sequential declaration Open Recursion Subtyping | local d in m end local declaration Delegation Inheritance Conclusion local is the interface between the effect-oriented language and the Appendix object-oriented language. local makes explicit that the internal representation of an object, possibly comprising instance variables or fields, is local to some of the methods (encapsulation).

17 / 28 Foundations of Objects — dynamic semantics Object-Oriented Programming

RALF HINZE The value of an object is essentially a dispatch or method table. Background

FVOP

µ ∈ Id →fin Expr method tables FEOP ν ::= ··· FOOP Introduction | object µ end object Objects Classes Open Recursion A method table maps a name to an expression, not to a value! Subtyping Delegation Inheritance Evaluation rules: Conclusion Appendix m ⇓ µ object m end ⇓ object µ end

e ⇓ object µ end µ(x) ⇓ ν e.x ⇓ ν

The name x is looked up at run-time in the method table associated with the object e (dynamic dispatch).

18 / 28 Foundations of Methods — dynamic semantics Object-Oriented Programming

Evaluation rules: RALF HINZE

Background (method x : τ = e) ⇓ {x 7→ e } FVOP FEOP

FOOP m1 ⇓ µ1 m2 ⇓ µ2 Introduction Objects m1 m2 ⇓ µ1, µ2 Classes Open Recursion Subtyping d ⇓ δ mδ ⇓ µ Delegation Inheritance local d in m end ⇓ µ Conclusion Appendix Method bodies are not evaluated. Why?

I method balance : Nat is not a natural number but a computation that yields a natural number.

I Later: the method body may contain the identifier self , which refers to the object itself and which is late-bound.

The sequence m1 m2 evaluates to the method table µ1 extended by µ2; the methods do not see each other. Later definitions shadow earlier ones. The declaration d is local to the method declaration m.

19 / 28 Foundations of Summary Object-Oriented Programming

RALF HINZE

Background

FVOP

FEOP

FOOP I An object is solely defined by the set of operations it supports. Introduction Objects I The implementation of an operation is called a method. Classes Open Recursion I The object’s internal representation — state, other objects etc — is Subtyping Delegation hidden from view outside the object’s definition (encapsulation). Inheritance Conclusion I When an operation is invoked on an object, the object itself determines what code gets executed (dynamic dispatch). Appendix

I An interface is the set of operations an object supports.

I The object’s internal representation does not appear in its type.

20 / 28 Foundations of What’s next? Object-Oriented Programming

RALF HINZE

Background

FVOP

FEOP

FOOP Introduction Objects I object templates: classes; Classes Open Recursion I recursive method invocation: self ; Subtyping Delegation I more flexibility: subtyping; Inheritance Conclusion I re-use of behaviours: inheritance via delegation; Appendix

I re-use of behaviours: inheritance via classes.

21 / 28 Foundations of Finite maps Object-Oriented Programming

RALF HINZE

Background

FVOP When X and Y are sets X →fin Y denotes the set of finite maps from X to FEOP Y . The domain of a finite map ϕ is denoted dom ϕ. FOOP Introduction Objects Classes Open Recursion Subtyping Delegation I the singleton map is written {x 7→ y } Inheritance I dom{x 7→ y } = {x } Conclusion I {x 7→ y }(x) = y Appendix

I when ϕ1 and ϕ2 are finite maps the map ϕ1, ϕ2 called ϕ1 extended by ϕ2 is the finite map with

I dom (ϕ1, ϕ2) = dom ϕ1 ∪ dom ϕ2  ϕ2(x) if x ∈ dom ϕ2 I (ϕ1, ϕ2)(x) = ϕ1(x) otherwise

22 / 28