Kernel Implementations III 8 February 2019 OSU CSE 1 Implementing - - PowerPoint PPT Presentation

kernel implementations iii
SMART_READER_LITE
LIVE PREVIEW

Kernel Implementations III 8 February 2019 OSU CSE 1 Implementing - - PowerPoint PPT Presentation

Kernel Implementations III 8 February 2019 OSU CSE 1 Implementing a Kernel Class From the examples so far, you should see there are two major questions to address: Q1 : What data representation (a.k.a. data structure ) should be used


slide-1
SLIDE 1

Kernel Implementations III

8 February 2019 OSU CSE 1

slide-2
SLIDE 2

Implementing a Kernel Class

  • From the examples so far, you should see

there are two major questions to address:

– Q1: What data representation (a.k.a. data structure) should be used to represent a value of the new type being implemented? – Q2: What algorithms should be used to manipulate that data representation to implement the contracts of the kernel methods?

8 February 2019 OSU CSE 2

slide-3
SLIDE 3

How To Think About Q1

  • Two-level thinking: Restrict your attention

to exactly two adjacent levels in the tower

– One is the level for which you are implementing a new kernel class

  • See the kernel interface for the new type you are

creating

– The other is the level directly below the level for the new kernel class you are creating

  • See the interfaces for the types of the variables

you are using to represent a value of the new type

8 February 2019 OSU CSE 3

slide-4
SLIDE 4

8 February 2019 OSU CSE 4

Queue Queue3 implements QueueKernel extends QueueSecondary extends Object extends Standard extends Iterable extends

Example revisited: Queue represented using a Sequence.

slide-5
SLIDE 5

8 February 2019 OSU CSE 5

Queue Queue3 implements QueueKernel extends QueueSecondary extends Object extends Standard extends Iterable extends

The class Queue3 uses the interface Sequence.

Sequence uses

slide-6
SLIDE 6

8 February 2019 OSU CSE 6

Queue Queue3 implements QueueKernel extends QueueSecondary extends Object extends Standard extends Iterable extends Sequence uses

The two levels

  • f the tower of

abstractions for Queue3 are this level with Queue ...

slide-7
SLIDE 7

8 February 2019 OSU CSE 7

Queue Queue3 implements QueueKernel extends QueueSecondary extends Object extends Standard extends Iterable extends Sequence uses

... and this level with Sequence.

slide-8
SLIDE 8

Example: Queue3 on Sequence

public class Queue3<T> extends QueueSecondary<T> { private Sequence<T> entries; ... }

8 February 2019 OSU CSE 8

slide-9
SLIDE 9

public class Queue3<T> extends QueueSecondary<T> { private Sequence<T> entries; ... }

8 February 2019 OSU CSE 9

There is one instance variable (a.k.a. data member) in this representation of a Queue: a Sequence called entries.

Example: Queue3 on Sequence

slide-10
SLIDE 10

Picture: Queue3 on Sequence

8 February 2019 OSU CSE 10

< 4, 7, 3 > What the client sees: a Queue whose value is < 4, 7, 3 >.

slide-11
SLIDE 11

Picture: Queue3 on Sequence

8 February 2019 OSU CSE 11

< 4, 7, 3 > < 4, 7, 3 > What the implementer can manipulate: a Sequence whose value is < 4, 7, 3 >.

slide-12
SLIDE 12

8 February 2019 OSU CSE 12

NaturalNumber NaturalNumber2 implements NaturalNumber- Kernel extends NaturalNumberSecondary extends Object extends Standard extends Comparable extends

Example revisited: NaturalNumber represented using a Stack.

slide-13
SLIDE 13

8 February 2019 OSU CSE 13

NaturalNumber NaturalNumber2 implements NaturalNumber- Kernel extends NaturalNumberSecondary extends Object extends Standard extends Comparable extends Stack uses

The class NaturalNumber2 uses the interface Stack.

slide-14
SLIDE 14

8 February 2019 OSU CSE 14

NaturalNumber NaturalNumber2 implements NaturalNumber- Kernel extends NaturalNumberSecondary extends Object extends Standard extends Comparable extends Stack uses

The two levels

  • f the tower of

abstractions for NN2 are this level with NN ...

slide-15
SLIDE 15

8 February 2019 OSU CSE 15

NaturalNumber NaturalNumber2 implements NaturalNumber- Kernel extends NaturalNumberSecondary extends Object extends Standard extends Comparable extends Stack uses

... and this level with Stack.

slide-16
SLIDE 16

Example: NaturalNumber2 on Stack

public class NaturalNumber2 extends NaturalNumberSecondary { private Stack<Integer> digits; ... }

8 February 2019 OSU CSE 16

slide-17
SLIDE 17

public class NaturalNumber2 extends NaturalNumberSecondary { private Stack<Integer> digits; ... }

8 February 2019 OSU CSE 17

There is one instance variable (a.k.a. data member) in this representation of a NaturalNumber: a Stack<Integer> called digits.

Example: NaturalNumber2 on Stack

slide-18
SLIDE 18

Picture: NaturalNumber2 on Stack

8 February 2019 OSU CSE 18

724 What the client sees: a NaturalNumber whose value is 724.

slide-19
SLIDE 19

8 February 2019 OSU CSE 19

724 < 4, 2, 7 > What the implementer can manipulate: a Stack whose value is < 4, 2, 7 >.

Picture: NaturalNumber2 on Stack

slide-20
SLIDE 20

Two-Level Thinking

8 February 2019 OSU CSE 20

client view: mathematical model, method contracts implementer view: data representation, algorithms

slide-21
SLIDE 21

Two-Level Thinking

8 February 2019 OSU CSE 21

client view: mathematical model, method contracts implementer view: data representation, algorithms See the kernel interface for a description of what the software behaves like, what the client sees.

slide-22
SLIDE 22

Two-Level Thinking

8 February 2019 OSU CSE 22

client view: mathematical model, method contracts implementer view: data representation, algorithms See the kernel class for a description of how the software achieves its behavior.

slide-23
SLIDE 23

Two-Level Thinking

8 February 2019 OSU CSE 23

client view: mathematical model, method contracts implementer view: data structure, algorithms The ovals denote sets

  • f mathematical

model values, this

  • ne for the client’s

view of the new type ...

slide-24
SLIDE 24

Two-Level Thinking

8 February 2019 OSU CSE 24

client view: mathematical model, method contracts implementer view: data structure, algorithms ... and this one for the kernel-class implementer’s view of the chosen data representation.

slide-25
SLIDE 25

Two-Level Thinking

8 February 2019 OSU CSE 25

This is the abstract state space: the set of all possible math model values as seen by a client.

slide-26
SLIDE 26

Two-Level Thinking

8 February 2019 OSU CSE 26

This is the concrete state space: the set of all possible math model values of the data representation.

slide-27
SLIDE 27

Two-Level Thinking

8 February 2019 OSU CSE 27

This is the interpretation of each concrete value (below) as an abstract value (above).

slide-28
SLIDE 28

Two-Level Thinking

8 February 2019 OSU CSE 28

Example: For interface NaturalNumber this is the set of all integer values that are non- negative. 7 165 29

slide-29
SLIDE 29

Two-Level Thinking

8 February 2019 OSU CSE 29

Example: For class NaturalNumber2 this is the set of all string of integer values that satisfy the properties we want in the data representation. 7 165 29 <> <7> <9,2> <5,6,1>

slide-30
SLIDE 30

Two-Level Thinking

8 February 2019 OSU CSE 30

Example: For class NaturalNumber2 the interpretation maps a string of integer to the integer whose decimal digits are those in the string, in reverse order. 7 165 29 <> <7> <9,2> <5,6,1>

slide-31
SLIDE 31

Implementing a Kernel Class

  • Recall, there are two major questions to

address:

– Q1: What data representation (a.k.a. data structure) should be used to represent a value of the new type being implemented? – Q2: What algorithms should be used to manipulate that data representation to implement the contracts of the kernel methods?

8 February 2019 OSU CSE 31

slide-32
SLIDE 32

How To Think About Q2

  • One-step thinking: Restrict your attention

to the states just before and just after a method call

  • Combined with two-level thinking about

the data representation, this leads to a device called a commutative diagram

8 February 2019 OSU CSE 32

slide-33
SLIDE 33

Commutative Diagram

8 February 2019 OSU CSE 33

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1> before the method call after the method call

slide-34
SLIDE 34

Commutative Diagram

8 February 2019 OSU CSE 34

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1> This is the abstract transition: for each state before the call, where it might end up according to the method’s contract.

slide-35
SLIDE 35

Commutative Diagram

8 February 2019 OSU CSE 35

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1> This is the concrete transition: for each state before the call, where it might end up according to the method’s body.

slide-36
SLIDE 36

8 February 2019 OSU CSE 36

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1> The contract says that, if n = 0, then the call n.multiplyBy10(7); will result in n = 7.

Example: n.multiplyBy10(7)

slide-37
SLIDE 37

Example: n.multiplyBy10(7)

8 February 2019 OSU CSE 37

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1> So, given any data representation of n = 0, the method body must change it so that after the call it has some data representation of n = 7.

slide-38
SLIDE 38

Over-Up and Up-Over “Commute”

8 February 2019 OSU CSE 38

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1>

slide-39
SLIDE 39

Over-Up and Up-Over “Commute”

8 February 2019 OSU CSE 39

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1> Starting here, going over then up ...

slide-40
SLIDE 40

Over-Up and Up-Over “Commute”

8 February 2019 OSU CSE 40

7 165 29 <> <7> <9,2> <5,6,1> 7 165 29 <> <7> <9,2> <5,6,1> ... is the same as going up then over.

slide-41
SLIDE 41

Correctness

  • The kernel class correctly implements

the kernel interface if and only if, for every method and for every legal input state for that method, the method body (over-then- up) always results in a state that satisfies the method contract (up-then-over)

  • The commutative diagram lets you think

clearly about how to make your code correct

8 February 2019 OSU CSE 41

slide-42
SLIDE 42

Some Questions To Think About

  • Given a kernel class, can there ever be

more than one data representation for a given abstract value? In other words, can there be more than one concrete value that can be interpreted as the same abstract value?

– If so, what would it look like in the diagram?

8 February 2019 OSU CSE 42

slide-43
SLIDE 43

Some Questions To Think About

  • Given a kernel interface, can there ever be

more than one possible correct (abstract) result of a call, according to a method’s contract?

– If so, what would it look like in the diagram?

8 February 2019 OSU CSE 43

slide-44
SLIDE 44

Some Questions To Think About

  • Given a kernel class, can there ever be

more than one possible correct (concrete) result of a call, according to a method’s body?

– If so, what would it look like in the diagram?

8 February 2019 OSU CSE 44