Loading in 5 sec....

Week 4 – Implementer’s view, conventions and correspondence, representationPowerPoint Presentation

Week 4 – Implementer’s view, conventions and correspondence, representation

- 94 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Week 4 – Implementer’s view, conventions and correspondence, representation' - tallis

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### Week 4 – Implementer’s view, conventions and correspondence, representation

Jimmy Voss

Disclaimer: Not all material is original. Some is taken from the official course slides, and some is taken from Anna’s slides.

Implementer’s View correspondence, representation

- So far, we have taken “Kernel” code as a given for all data structures.
- Very few data types are naturally a part of the computer (essentially Integers, Reals, pointers, and arrays).
- Abstract data types (ADTs) are built using simpler underlying data structures and pointers.

Primitive Data Types correspondence, representation

- Computers naturally work with 2 data types: Integers and Floating Point numbers (Reals)
- Characters, Booleans, and Pointers are represented as Integer Type objects internally.

- All programming languages provide a small number of primitive data types.
- Resolve/C++ provides
- Integer, Real, Character, Text, Pointer, Boolean, Character_IStream, Character_OStream
- Trivia: C++ provides equivalents for all of these, C only for the boldface primitives.

- Representations and Reps can be used to combine data types.

Abstract Data Types correspondence, representation

- In Resolve/C++, Queues, Records, Stacks, Partial Maps, etc. are all Abstract Data Types
- In Object Oriented Languages, ADTs are built on top of other data types.
- We will mostly be looking at implementing ADTs from simpler ADTs.

Abstract correspondence, representationvsConcrete View

Abstract Space (Client View)

- Abstract View – Data structure modeled by the code.
- Concrete View – the underlying implementation in the Kernel code.
- There is an onto map from concrete space to abstract space.

“represents” relation

Concrete Space (Implementer View)

Example: Abstract Set correspondence, representation

Abstract Space (Set)

{0, 5}

{1, 2, 3}

<5, 0, 0>

<1, 2, 3>

<5, 0>

<0, 5>

Concrete Space (using Queue)

Example: Abstract Set correspondence, representation

Abstract Space (Set)

{0, 5}

{1, 2, 3}

“onto” map,

Need not be “one-to-one”

<5, 0, 0>

<1, 2, 3>

<5, 0>

<0, 5>

Concrete Space (using Queue)

Implementing Abstraction correspondence, representation

- The Client using a Set does not need to think about how it is implemented.
- The Implementer of a class is responsible for making abstract reasoning possible.
- Kernel – The class which implements the core functionality of an abstract data structure.
- The Kernel code Implementation encapsulates all logic required to create an abstract interface for the client.
- Layered Extensions of Kernels are implemented using only calls to Kernel member functions.

Convention and correspondence, representationCorrespondance

- Convention – Acts as ensures and requires clause for all Kernel member functions.
- Exceptions (Default Operations):
- Initialize (constructor) – ensures, not requires
- Finalize (destructor) – requires, not ensures
- Clear – ensures, not requires
- Swap

- Disallows calls to most Kernel functions by Kernel functions.

- Exceptions (Default Operations):
- Correspondence – Gives the mathematical relationship between the abstract and concrete representations of an object.

Example: Set_Kernel_1 correspondence, representation

classSet_Kernel_1 :

implements

abstract_instanceSet_Kernel <Item>,

encapsulates

concrete_instanceRep

{

private:

rep_field_name (Rep, 0, Queue_Of_Item, items);

/*!

convention

|self.items| = |elements (self.items)|

correspondence

self = elements (self.items)

!*/

public:

standard_concrete_operations (Set_Kernel_1);

Example: Set_Kernel_1 correspondence, representation

classSet_Kernel_1 :

implements

abstract_instanceSet_Kernel <Item>,

encapsulates

concrete_instanceRep

{

private:

rep_field_name (Rep, 0, Queue_Of_Item, items);

/*!

convention

|self.items| = |elements (self.items)|

correspondence

self = elements (self.items)

!*/

public:

standard_concrete_operations (Set_Kernel_1);

Constraints correspondence, representation

- Constraints – Things which must hold in the abstract space.
- Made up Example: Set of Natural Numbers:
/*!

math subtype NATURAL_MODEL is integer

exemplar n

constraint

n >= 0

!*/

- Note: Constraints can be implemented via conventions.

Example: Set of “natural numbers” correspondence, representation

Abstract space

Concrete space

Breaks Convention

<5, 0>

{0, 5}

<5, 0, 5>

Correspondence

{-2, 5}

Breaks Constraints

Default Operations correspondence, representation

- Constructor
- Invokes the special Procedure Initialize(), if it exists.
- Invoked when object is created / enters scope.

- Destructor
- Invoked when an object is destroyed / leaves scope.
- Invokes code in member function Finalize(), if it exists.

- Swap (&= operator)
- Clear
- Default operations are created via the following macro:
standard_concrete_operations (Kernel_Class_Name);

Example: Set_Kernel_1 correspondence, representation

concrete_template <

concrete_instanceclass Item,

concrete_instanceclass Queue_Of_Item =

Queue_Kernel_1a <Item>,

concrete_instance class Rep =

Representation <Queue_Of_Item>

>

classSet_Kernel_1 :

implements

abstract_instanceSet_Kernel <Item>,

encapsulates

concrete_instanceRep

{

private:

rep_field_name (Rep, 0, Queue_Of_Item, items);

public:

standard_concrete_operations (Set_Kernel_1);

Template Parameter

(type name place holder)

Provides 4 default operations.

Constructors and Destructors correspondence, representation

- Constructor is used to give each object its default value.
- Destructors are more important when dealing with pointers.
- Typically used to clean up memory allocations.
- Memory is allocated via pointers.
- This will become important later.

Representations correspondence, representation

- Encapsulated version of a Record used in Kernel code.
- Representation variables are defined in the concrete Kernel class implementation code.
- Representation contains all variables necessary for implementing the underlying logic / storage of the kernel.

Representations correspondence, representation

- Recall: the accessor mechanism:
variable[record_variable]

is used to access record variables.

- Representations likewise use the accessor to access representation variables.
- In kernel code, this looks like:
self[rep_variable]

- In specifications, we refer to the variables in the form:
self.rep_variable

Representations correspondence, representation

- It is a RESOLVE/C++ coding convention that we use the class name Rep for the inherited Representation.

Initialize() correspondence, representation

- If no Initialize() function is specified, then the default constructor for Rep is used.
- The default constructor for Rep recursively calls the default constructors for all fields. Sometimes this does not give the correct starting state.

Initialize() correspondence, representation

- Recall Sorting_Machine_Kernel_1? It uses an inserting flag. Internally, it must be set to true when the Sorting Machine is constructed. So, it would have an Initialize procedure similar to the following:
local_procedure_body Initialize ()

{

self[inserting_rep] = true;

}

Bringing it Together in Code correspondence, representation

concrete_template <

concrete_instance class Item,

/*!

implements

abstract_instanceGeneral_Is_Equal_To <Item>

!*/

concrete_instanceclassQueue_Of_Item =

Queue_Kernel_1a <Item>,

concrete_instanceclass Rep =

Representation <Queue_Of_Item>

>

class Set_Kernel_1 :

implements

abstract_instanceSet_Kernel <Item>,

encapsulates

concrete_instance Rep

http://www.cse.ohio-state.edu/sce/rcpp/RESOLVE_Catalog-HTML/CT/Set/Kernel_1.html

General_Is_Equal_To correspondence, representation

- Notice that the previous slide states that Item must implement the General_Is_Equal_To class.
- The == operator is NOT supplied by default for all RESOLVE/C++ types, so it is not guaranteed to be in a specified object type.
- Resolve objects need not inherit from General_Is_Equal_To, but we are requiring that that must be true for a type to be used in place of Item as a template.
- General_Is_Equal_To provides a member function Is_Equal_To
- Syntax: X.Is_Equal_To( Y )
- symantically, returns true if X and Y are “the same”, false otherwise.

Example Code (continued) correspondence, representation

{

private:

rep_field_name (Rep, 0, Queue_Of_Item, items);

/*!

convention

|self.items| = |elements (self.items)|

correspondence

self = elements (self.items)

!*/

Note: This particular code has no constraint.

Example Code (Continued) correspondence, representation

public:

standard_concrete_operations (Set_Kernel_1);

procedure_body Add (

consumes Item& x

)

{

self[items].Enqueue (x);

}

. . .

function_body Integer Size ()

{

return self[items].Length ();

}

};

Exercise correspondence, representation

- Set_Kernel_1 inherits the following Abstract Specification for member function Is_Member from Set_Kernel
function Boolean Is_Member (

preserves Item& x

) is_abstract;

/*!

ensures

Is_Member = (x is in self)

!*/

- Write the implementation for Is_Member in Set_Kernel_1
- Recall: Set_Kernel_1 inherits a Rep which contains only the field items which is of type Queue_Of_Item. Set_Kernel_1 has the convention: and the correspondence

function_body correspondence, representationBoolean Is_Member( preserves Item& x )

{

object Integer I;

object Boolean Success_State = false;

// note: Do not call self.Size()

while ( (I < self[items].Length()) and

(not Success_State))

{

if ( x.Is_Equal_To( self[items][current] ) )

{

Success_State = true;

}

// circulate the queue

objectcatalyst Item temp;

self[items].Dequeue( temp );

self[items].Enqueue( temp );

I = I + 1;

}

return Success_State;

}

Corrections from Yesterday correspondence, representation

- I introduced General_Is_Equal_To last time.
- General_Is_Equal_To is an abstract object, not a function. It provides a member function Is_Equal_To
- In Set_Kernel_1, we require that Item instantiates General_Is_Equal_To, giving access to the function Is_Equal_To.
- Syntax (given Item X, Y) would be:
if ( X.Is_Equal_To( Y ) ) {

output << “X and Y are \”the same\””;

}

else {

output << “X and Y are \“different\””;

}

Announcements correspondence, representation

- Lab 2 is due Tuesday January 31st (next week).
- You will be building the program from scratch. There is no skeleton code.
- Idea – you are taking in an input of words and definitions and creating an html file that allows the user to click the word and get taken to the definition.
- Whenever you create a procedure, you should provide specification comments, though the ensures / requires clauses can be written in plain English, encompassed as [ text ].

Abstract vs. Concrete View correspondence, representation

- In the Kernel, self has 2 meanings:
- If self is used in a specification, then it refers to the abstract object that the Kernel represents.
- In Set_Kernel_1 we have the correspondance:
/*! …

correspondence

self = elements (self.items)

!*/

- The correspondence is used to tell the user precisely what the abstract object is, and how it relates to the underlying concrete representation.
- In Set_Kernel_1 specifications, |self| refers to the number of elements in the set.

Abstract vs. Concrete View correspondence, representation

- The meaning of Self in Source Code:
- In source code, self is a special variable accessible in member functions.
- self refers to the object which calls the member function in Client Code.
- Suppose the client writes (with Integer_Set instantiating Set_Kernel_1):
objectInteger_SetMy_Set;

object Integer X = 1;

My_Set.Add( X );

- Then self in Set_Kernel_1 refers to My_Set on the call above.
- self[items] refers to the Queue of Integers which gives the concrete representation of the set self.

Question from Last Class correspondence, representation

- Set_Kernel_1 inherits the abstract interface for:
functionBoolean Is_Member (

preserves Item& x

) is_abstract;

/*!

ensures

Is_Member = (x is in self)

!*/

- How do we know that Is_Member preserves the set?

Functions correspondence, representation

- In functions, all arguments which are passed in are preserved.
- When calling member functions, self in its abstract meaning is considered to be passed in as an additional argument.
- What self corresponds to in the mathematical model space is preserved for member functions.

Procedures correspondence, representation

- In RESOLVE/C++ procedures, guarantees about whether variables change or stay the same need to be made explicit in the specifications.
- Set_Kernel_1 inherits the following specification from Set_Kernel for Remove
procedure Remove ( preserves Item& x,

produces Item& x_copy ) is_abstract;

/*!

requires

x is in self

ensures

self = #self - {x} and

x_copy = x

!*/

Representations and Encapsulation correspondence, representation

- Wikipedia Definition of Encapsulation:
In a programming language encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

- A language mechanism for restricting access to some of the object's components.
- A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

Use of Encapsulation correspondence, representation

- The Client wants to be able to use a set.
- The Client does not truly care about the sets underlying implementation, unless it is inefficient.
- By encapsulating the Representation for the set via Rep, the Implementer can work out the underlying details of how the set is implemented in such a way that the Client cannot directly access the concrete space implementation (i.e. the Queue of Item used to implement Set_Kernel_1).

Representation Usage correspondence, representation

- Every Resolve/C++ representation consists of exactly one internal object, its representation record.

Some notes on Closed Lab 4 correspondence, representation

- You will be implementing a Sequence using 2 Stacks.
/*!

correspondence

self = reverse (self.before) * self.after

!*/

- before and after are stacks.
- How would self.before and self.after need to look in order to access self[5] if self = <1, 2, 3, 4, 5, 6, 7, 8>?

You Can’t Call Kernel Functions but. . . correspondence, representation

- The following appears in Closed Lab 4:
local_procedure_bodySet_Length_Of_Before (

alters Stack_Of_Item& before_stack,

alters Stack_Of_Item& after_stack,

preserves Integer pos

)

/*!

requires

0 <= pos <= |before_stack| + |after_stack|

ensures

reverse (before_stack) * after_stack =

reverse (#before_stack) * #after_stackand

|before_stack| = pos

!*/

{ //-------- for students to fill in -------- }

What is the Difference? correspondence, representation

- Set_Length_Of_Before does not act directly on self
- Note that before_stack and after_stack are explicitly passed in. They need not refer to self[before] and self[after]
- The Kernel’s convention is irrelevant to Set_Length_Of_Before.

- Set_Length_Of_Before is a private member procedure of the Kernel class (not accessible by Client code).

Case Study: Tower of Hanoi correspondence, representation

- http://www.mazeworks.com/hanoi/
- Suppose we were to make a concrete class object that is used to provide a Tower of Hanoi game to the client. It allows the client to start new games, move disks (legally?), and print out the order of how disks are stacked on each rod.
- How would we represent this class in the concrete space?
- What would be the abstract, mathematical model of the Tower of Hanoi object?
- What functionality would we need to provide?

Download Presentation

Connecting to Server..