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 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
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
Example Standard Iterable revisited: extends extends Queue represented QueueKernel using a extends Sequence . Object Queue implements extends QueueSecondary extends Queue3 8 February 2019 OSU CSE 4
Standard Iterable The class Queue3 extends extends uses the interface QueueKernel Sequence . extends Object Queue implements extends QueueSecondary extends Queue3 uses Sequence 8 February 2019 OSU CSE 5
Standard Iterable extends extends QueueKernel extends Object Queue The two levels implements of the tower of extends abstractions QueueSecondary for extends Queue3 Queue3 are this level with uses Queue ... Sequence 8 February 2019 OSU CSE 6
Standard Iterable extends extends QueueKernel extends Object Queue implements extends QueueSecondary ... and this level with extends Sequence . Queue3 uses Sequence 8 February 2019 OSU CSE 7
Example: Queue3 on Sequence public class Queue3<T> extends QueueSecondary<T> { private Sequence<T> entries; ... } 8 February 2019 OSU CSE 8
Example: Queue3 on Sequence public class Queue3<T> extends QueueSecondary<T> { private Sequence<T> entries; ... } There is one instance variable (a.k.a. data member ) in this representation of a Queue : a Sequence called entries . 8 February 2019 OSU CSE 9
Picture: Queue3 on Sequence What the client sees: a Queue whose value is < 4, 7, 3 > . < 4, 7, 3 > 8 February 2019 OSU CSE 10
Picture: Queue3 on Sequence What the implementer can manipulate: a Sequence whose value is < 4, 7, 3 > < 4, 7, 3 > . < 4, 7, 3 > 8 February 2019 OSU CSE 11
Example Standard revisited: extends NaturalNumber NaturalNumber- represented Comparable Kernel using a extends extends Stack . Object NaturalNumber implements extends NaturalNumberSecondary extends NaturalNumber2 8 February 2019 OSU CSE 12
Standard The class extends NaturalNumber2 NaturalNumber- Comparable uses the interface Kernel Stack . extends extends Object NaturalNumber implements extends NaturalNumberSecondary extends NaturalNumber2 uses Stack 8 February 2019 OSU CSE 13
Standard extends NaturalNumber- Comparable Kernel extends extends Object NaturalNumber The two levels implements of the tower of extends abstractions NaturalNumberSecondary for extends NN2 NaturalNumber2 are this level with uses NN ... Stack 8 February 2019 OSU CSE 14
Standard extends NaturalNumber- Comparable Kernel extends extends Object NaturalNumber implements extends NaturalNumberSecondary ... and this level with extends Stack . NaturalNumber2 uses Stack 8 February 2019 OSU CSE 15
Example: NaturalNumber2 on Stack public class NaturalNumber2 extends NaturalNumberSecondary { private Stack<Integer> digits; ... } 8 February 2019 OSU CSE 16
Example: NaturalNumber2 on Stack public class NaturalNumber2 extends NaturalNumberSecondary { private Stack<Integer> digits; ... There is one instance variable } (a.k.a. data member ) in this representation of a NaturalNumber : a Stack<Integer> called digits . 8 February 2019 OSU CSE 17
Picture: NaturalNumber2 on Stack What the client sees: a NaturalNumber whose value is 724 724 . 8 February 2019 OSU CSE 18
Picture: NaturalNumber2 on Stack What the implementer can manipulate: a Stack whose value is 724 < 4, 2, 7 > . < 4, 2, 7 > 8 February 2019 OSU CSE 19
Two-Level Thinking client view : mathematical model, method contracts implementer view : data representation, algorithms 8 February 2019 OSU CSE 20
Two-Level Thinking See the kernel interface for a description of what the software behaves client view : like, what the client sees. mathematical model, method contracts implementer view: data representation, algorithms 8 February 2019 OSU CSE 21
Two-Level Thinking client view: mathematical model, method contracts implementer view : See the kernel class data representation, for a description of how algorithms the software achieves its behavior. 8 February 2019 OSU CSE 22
Two-Level Thinking client view : The ovals denote sets mathematical model, of mathematical method contracts model values, this one for the client’s view of the new type implementer view : ... data structure, algorithms 8 February 2019 OSU CSE 23
Two-Level Thinking client view : mathematical model, ... and this one for the method contracts kernel-class implementer’s view of the chosen data implementer view : representation. data structure, algorithms 8 February 2019 OSU CSE 24
Two-Level Thinking This is the abstract state space : the set of all possible math model values as seen by a client. 8 February 2019 OSU CSE 25
Two-Level Thinking This is the concrete state space : the set of all possible math model values of the data representation. 8 February 2019 OSU CSE 26
Two-Level Thinking This is the interpretation of each concrete value (below) as an abstract value (above). 8 February 2019 OSU CSE 27
Two-Level Thinking 7 165 0 29 Example: For interface NaturalNumber this is the set of all integer values that are non- negative. 8 February 2019 OSU CSE 28
Two-Level Thinking 7 165 0 29 Example: For class NaturalNumber2 this is the set of all string of integer values that satisfy the properties we want in <9,2> the data representation. <7> <5,6,1> <> 8 February 2019 OSU CSE 29
Two-Level Thinking 7 165 0 29 Example: For class NaturalNumber2 the interpretation maps a string of integer to the integer <9,2> whose decimal digits are those in the string, <7> <5,6,1> in reverse order. <> 8 February 2019 OSU CSE 30
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
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
Commutative Diagram 7 7 165 165 0 0 29 29 before after the method the method call call <9,2> <9,2> <7> <7> <5,6,1> <5,6,1> <> <> 8 February 2019 OSU CSE 33
Commutative Diagram 7 7 165 165 0 0 29 29 This is the abstract transition : for each state before the <9,2> <9,2> call, where it might end <7> <7> up according to the <5,6,1> <5,6,1> <> <> method’s contract . 8 February 2019 OSU CSE 34
Commutative Diagram This is the 7 7 165 165 0 concrete transition : 0 for each state before the 29 29 call, where it might end up according to the method’s body . <9,2> <9,2> <7> <7> <5,6,1> <5,6,1> <> <> 8 February 2019 OSU CSE 35
Example: n.multiplyBy10(7) 7 7 165 165 0 0 29 29 The contract says that, if n = 0 , then the call <9,2> <9,2> n.multiplyBy10(7); <7> will result in n = 7 . <7> <5,6,1> <5,6,1> <> <> 8 February 2019 OSU CSE 36
Example: n.multiplyBy10(7) So, given any data 7 7 165 165 representation of n = 0 , 0 0 the method body must 29 29 change it so that after the call it has some data representation of n = 7 . <9,2> <9,2> <7> <7> <5,6,1> <5,6,1> <> <> 8 February 2019 OSU CSE 37
Over-Up and Up-Over “Commute” 7 7 165 165 0 0 29 29 <9,2> <9,2> <7> <7> <5,6,1> <5,6,1> <> <> 8 February 2019 OSU CSE 38
Over-Up and Up-Over “Commute” 7 7 165 165 0 0 29 29 Starting here, going over then up ... <9,2> <9,2> <7> <7> <5,6,1> <5,6,1> <> <> 8 February 2019 OSU CSE 39
Recommend
More recommend