Title: Lesson 8 Imperative Objects
1Lesson 8Imperative Objects
2Object-Oriented Programming
- Multiple representations (polymorphism)
- Encapsulation of state
- Subtyping
- Inheritance (sharing implementation code)
- Open Recursion
3Simple Objects
- c let x ref 1 in
- get ? _ Unit . !x,
- inc ? _ Unit . x succ(!x)
- gt c get Unit -gt Nat, inc Unit -gt Unit
- c.inc unit
- gt unit Unit
- c.get unit
- gt 2 Nat
- (c.inc unit c.inc unit c.get unit)
- 4 Nat
- type Counter get Unit -gt Nat, inc Unit -gt
Unit
4Simple Objects
- type Counter get Unit -gt Nat, inc Unit -gt
Unit - inc3 ? c Counter . (c.inc unit c.inc unit
c.inc unit) - gt inc3 Counter -gt Unit
- (inc3 c c.get unit)
- gt 7 Nat
5Object generators
newCounter ? _ Unit . let x ref 1 in
get ? _ Unit . !x, inc
? _ Unit . x succ(!x) gt newCounter Unit
-gt Counter
6Subtyping
- type ResetCounter get Unit -gt Nat, inc
Unit -gt Unit, - reset Unit -gt Unit
- newResetCouner
- ? _ Unit . let x ref 1 in
- get ? _ Unit . !x,
- inc ? _ Unit . c succ(!x),
- reset ? _ Unit . x 1
- gt newResetCounter Unit -gt ResetCounter
- rc newResetCounter unit
- (inc3 rc rc.reset unit inc3 rc rc.get unit)
- gt 4 Nat
7Grouping Instance Variables
- c let r x ref 1 in
- get ? _ Unit . !(r.x),
- inc ? _ Unit . r.x succ(!(r.x))
- gt c Counter
- type CounterRep x Ref Nat
8Simple Classes
- counterClass
- ? r CounterRep .
- get ? _ Unit . !(r.x),
- inc ? _ Unit . r.x
succ(!(r.x)) - gt counterClass CounterRep -gt Counter
- newCounter
- ? _ Unit . let r x ref 1 in
counterClass r - gt newCounter Unit -gt Counter
9Simple Classes - Inheritance
- resetCounterClass
- ? r CounterRep .
- let super counterClass r in
- get super.get,
- inc super.inc,
- reset ? _ Unit . r.x 1
- gt resetCounterClass CounterRep -gt ResetCounter
- newResetCounter
- ? _ Unit . let r x ref 1 in
resetCounterClass r - gt newResetCounter Unit -gt ResetCounter
10Adding Instance Variables
- type BackupCounter get Unit -gt Nat, inc
Unit -gt Unit, - reset Unit -gt Unit, backup Unit
-gt Unit - type BackupCounterRep x Ref Nat, b Ref
Nat - backupCounterClass
- ? r BackupCounterRep .
- let super resetCounterClass r in
( r CounterRep ) - get super.get,
- inc super.inc,
- reset ? _ Unit . r.x !(r.b), (
override ) - backup ? _ Unit . r.b !(r.x)
- gt backupCounterClass BackupCounterRep -gt
BackupCounter
11Calling Superclass methods
- funnyBackupCounterClass
- ? r BackupCounterRep .
- let super backupCounterClass r in
- get super.get,
- inc ? _ Unit . (super.backup unit
super.inc unit), - reset super.reset,
- backup super.backup
- gt funnyBackupCounterClass BackupCounterRep -gt
BackupCounter
12Classes with Self
- type SetCounter get Unit -gt Nat, set Nat
-gt Unit, - inc Unit -gt Unit
- setCounterClass
- ? r CounterRep .
- fix (? self SetCounter .
- get ? _ Unit . !(r.x),
- set ? i Nat . r.x i,
- inc ? _ Unit .
self.set(succ(self.get unit))) - gt setCounterClass CounterRep -gt SetCounter
13Open Recursion
- setCounterClass
- ? r CounterRep .
- ? self SetCounter
- get ? _ Unit . !(r.x),
- set ? i Nat . r.x i,
- inc ? _ Unit . self.set(succ(self.g
et unit)) - gt setCounterClass CounterRep -gt SetCounter -gt
SetCounter - newSetCounter
- ? _ Unit . let r x ref 1 in fix
(setCounterClass r) - gt newSetCounter Unit -gt SetCounter
14Open Recursion, Inheritance
- type InstrCounter get Unit -gt Nat, set
Nat -gt Unit, - inc Unit -gt Unit, accesses Unit -gt
Nat - type InstrCounterRep x Ref Nat, a Ref Nat
- instrCounterClass
- ? r InstrCounterRep .
- ? self InstrCounter .
- let super setCounterClass r self in
- get super.get,
- set ? i Nat .(r.a succ(!(r.a))
super.set i), - inc super.inc,
- accesses ? _ Unit. !(r.a)
- gt instrCounterClass InstrCounterRep -gt
InstrCounter -gt InstrCounter - newInstrCounter ? _ Unit . let r x ref
1, a ref 0 in - fix(instrCounterClass r)
15Oops, can't create objects!
- newInstrCounter ? _ Unit . let r x ref
1, a ref 0 in - fix(instrCounterClass r)
- newInstrCounter unit
- ? let r x ref 1, a ref 0 in
fix(instrCounterClass r) - ? fix(instrCounterClass ivars)
- ? fix(? self InstrCounter .
- let super setCounterClass ivars
self in imethods) - ? let super setCounterClass ivars (fix f ) in
imethods - ? let super (?self SetCounter . smethods)
(fix f ) - in imethods
- ? let super (?self SetCounter . smethods)
- (let super setCounterClass ivars (fix f ) in
imethods) - in imethods
- ? ...
16Possible Fixes
- suspend evaluation of self using thunks
- use call-by-name at critical points
- build loops using refs
- abandon lambda-calculus and create a new
specialized calculus for OO
17Thunks
Wrap a term in a lambda abstraction t
T is replaced by t' ? _ Unit . t
Unit -gt T where we don't want it evaluated,
and by t' unit where we do need its
value.
18Open Recursion with Thunks
- setCounterClass
- ? r CounterRep .
- ? self Unit -gt SetCounter .
- ? _ Unit .
- get ? _ Unit . !(r.x),
- set ? i Nat . r.x i,
- inc ? _ Unit . (self
unit).set(succ((self unit).get unit)) - gt setCounterClass CounterRep -gt (Unit -gt
SetCounter) -gt - (Unit -gt
SetCounter) - newSetCounter
- ? _ Unit . let r x ref 1 in fix
(setCounterClass r) unit
19Inheritance with Thunks
- instrCounterClass
- ? r InstrCounterRep .
- ? self Unit -gt InstrCounter .
- ? _ Unit.
- let super setCounterClass r self unit in
- get super.get,
- set ? i Nat. (r.a succ(!(r.a))
super.set i), - inc super.inc,
- accesses ? _ Unit. !(r.a)
- gt instrCounterClass InstrCounterRep -gt (Unit
-gt InstrCounter) - -gt (Unit -gt
InstrCounter) - newInstrCounter ? _ Unit . let r x ref
1, a ref 0 in - fix(instrCounterClass r)
unit
20Replacing fix with ref
setCounterClass CounterRep -gt (Ref SetCounter)
-gt SetCounter ? r CounterRep. ? self
Ref SetCounter. get ? _ Unit . !(r.x),
set ? i Nat . r.x i, inc ? _
Unit . (!self).set(succ((!self).get
unit)) dummySetCounter SetCounter get ?
_ Unit . 0, set ? i Nat . unit,
inc ? _ Unit . unit newSetCounter Unit
-gt SetCounter ? _ Unit . let r x
ref 1 in let cAux ref
dummySetCounter in (cAux
(setCounterClass r cAux) !cAux)
21Inheritance with ref
- instrCounterClass
- ? r InstrCounterRep . ? self Ref
InstrCounter . - let super setCounterClass r self in
- get super.get,
- set ? i Nat. (r.a succ(!(r.a))
super.set i), - inc super.inc,
- accesses ? _ Unit. !(r.a)
- gt instrCounterClass InstrCounterRep -gt (Ref
InstrCounter) - -gt
InstrCounter - Type error Ref InstrCounter lt/ Ref SetCounter
22Replacing Ref with Source
- setCounterClass CounterRep -gt (Source
SetCounter) -gt SetCounter - ? r CounterRep. ? self Source SetCounter.
- get ? _ Unit . !(r.x),
- set ? i Nat . r.x i,
- inc ? _ Unit . (!self).set(succ((!s
elf).get unit))
23Replacing Ref with Source, 2
- instrCounterClass
- ? r InstrCounterRep . ? self Source
InstrCounter . - let super setCounterClass r self in
- get super.get,
- set ? i Nat. (r.a succ(!(r.a))
super.set i), - inc super.inc,
- accesses ? _ Unit. !(r.a)
- gt instrCounterClass InstrCounterRep -gt (Source
InstrCounter) - -gt
InstrCounter - Type checks Source InstrCounter lt Source
SetCounter
24Replacing fix with ref
dummyInstrCounter InstrCounter get ? _
Unit . 0, set ? i Nat . unit,
inc ? _ Unit . unit, accesses ? _ Unit
. 0 newInstrCounter Unit -gt InstrCounter
? _ Unit . let r x ref 1, a ref 0 in
let cAux ref
dummyInstrCounter in (cAux
(instrCounterClass r cAux) !cAux)