Lesson 8 Imperative Objects - PowerPoint PPT Presentation

About This Presentation
Title:

Lesson 8 Imperative Objects

Description:

fix(instrCounterClass r) newInstrCounter unit ... where we don't want it evaluated, and by. t' unit. where we do need its value. 2/12/02 ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 25
Provided by: davidma84
Category:

less

Transcript and Presenter's Notes

Title: Lesson 8 Imperative Objects


1
Lesson 8Imperative Objects
  • 2/12
  • Chapter 18

2
Object-Oriented Programming
  • Multiple representations (polymorphism)
  • Encapsulation of state
  • Subtyping
  • Inheritance (sharing implementation code)
  • Open Recursion

3
Simple 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

4
Simple 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

5
Object generators
newCounter ? _ Unit . let x ref 1 in
get ? _ Unit . !x, inc
? _ Unit . x succ(!x) gt newCounter Unit
-gt Counter
6
Subtyping
  • 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

7
Grouping 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

8
Simple 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

9
Simple 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

10
Adding 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

11
Calling 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

12
Classes 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

13
Open 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

14
Open 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)

15
Oops, 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
  • ? ...

16
Possible 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

17
Thunks
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.
18
Open 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

19
Inheritance 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

20
Replacing 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)
21
Inheritance 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

22
Replacing 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))

23
Replacing 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

24
Replacing 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)
Write a Comment
User Comments (0)
About PowerShow.com