Download Data Abstraction and Abstract Data Types in Programming Languages - Prof. Elsa Gunter and more Papers Computer Science in PDF only on Docsity!
Basic Data Abstraction Abstract Data Types Parameterized ADTs
CS421 Lecture 18: Data Abstraction
Mark Hills
University of Illinois at Urbana-Champaign
July 20, 2006
1 Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Mark Hills CS421 Lecture 18: Data Abstraction
Basic Data Abstraction Abstract Data Types Parameterized ADTs
Basic Data Abstraction
Abstract Data Types
Parameterized ADTs
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Objectives
By the end of this lecture, you should
◮ be familiar with differences in basic data abstraction
constructs (tuples, records, etc)
◮ understand the programming language concept of abstract
data type
◮ know, at a high level, how several languages handle data
abstraction
◮ understand parameterized abstract data types
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Basic Data Abstraction Facilities
Most languages include at least one form of user-defined type.
We’ve already seen several examples in OCaml:
◮ tuples
◮ records
◮ variant records
Another is the array, available in OCaml but much more common
in imperative languages.
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Arrays
Arrays are simply ordered sequences of objects (in the non-OO
sense) all of the same type.
A[3] := 15;
◮ often of fixed size
◮ generally indexed by integers; occasionally by subrange or
enumeration types
◮ can be multidimensional, with one index per dimension
◮ l-value of any element much be calculated using formula
based on index ranges and element sizes
◮ goal: l-value calculation should be constant-time
◮ heavily used in numeric computation; many languages provide
extended support for arrays (slices, array reductions, etc)
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Tuples
Like most arrays, tuples are fixed size. Unlike arrays, tuples allow
different types in each position.
(1, ’Bob’, 3.17): (Int × String × Float)
◮ provide an easy way to group information
◮ do not provide a way to label individual items
◮ tuples are order dependent – flipping items in a tuple changes
the type
Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Records
Records provide functionality similar to tuples – we can group
multiple items of different types together. However, records
additionally provide a way to name each item, making them order
independent.
◮ generally referred to as records or structures
◮ lookup usually as var-name.field-name
◮ with ability to name fields, provide a way to model data in the
problem domain
◮ generally not recursive (at least without pointers)
Mark Hills CS421 Lecture 18: Data Abstraction
Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Variant Records
Variant records are like records, but allow different forms of data to
be stored in records of the same type, based (usually) on a
constructor or type tag.
type ’a mylist = Empty | Cons of ’a * ’a mylist
◮ provide different groups of data items in the same type
◮ uses type constructors in most functional languages
◮ variant records, disjoint unions, or just unions in most
non-functional languages (imperative, OO)
◮ valid items usually determined with either pattern matching
(functional) or tag/discriminator (imperative)
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Variant Records Example: Modula-
1 TYPE IntOrReal =
2 RECORD
3 CASE IsInt: BOOLEAN OF
4 TRUE: i : INTEGER |
5 FALSE: r : REAL
6 END;
7 END;
9 x.IsInt := TRUE;
10 x.i := 0;
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Limitations
While these different abstraction mechanisms are useful, they
provide no method for information hiding or encapsulation.
Consider the C++ code:
1 struct Rational {
2 int numerator;
3 int denominator;
5 Rational mk_rat (int n,int d) { ... }
6 Rational add_rat (Rational x, Rational y) { ... }
◮ Can we use Rationals without knowing the implementation
details?
◮ Can we access the implementation directly without going
through the interface?
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Aggregate Types Limitations
Limitations
The problem with the last example was raised in the second point
– structures give us the ability to group data, but the
implementation is not hidden.
◮ Users can create Rationals without using mk rat
◮ Users can directly alter the contents of a rational
◮ Because the implementation is not hidden, changes may break
user code that relies on it
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Abstract Data Types
How do we fix these limitations? Ideally:
◮ we could specify an interface through which the data is
manipulated;
◮ we could then prevent unsupported changes to the data
representation, hiding the representation completely.
To do this, we need abstract data types.
Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Several Quick Points
◮ The maximum size of the stack has been predefined with
Max Size
◮ The items in the stack are predefined as integers – we would
need another similar type for a stack of strings, or floats, for
instance
◮ Note the keywords limited private – this means that we
don’t have support for predefined operations, such as equality
testing or assignment, which may not make sense anyway
Mark Hills CS421 Lecture 18: Data Abstraction
Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Stack ADT in Ada – Body Package
1 with Ada.Text_IO; use Ada.Text_IO;
2 package body Stack_Pack is
3 function Empty (Stk : in Stack_Type) return Boolean is
4 begin
5 return Stk.Topsub = 0
6 end Empty;
8 procedure Push (Stk : in out Stack_Type;
9 Element : in Integer) is
10 begin
11 if Stk.Topsub >= Max_Size then
12 Put_Line("ERROR - Stack overflow");
13 else
14 Stk.Topsub := Stk.Topsub + 1;
15 Stk.List(Topsub) := Element;
16 end if
17 end Push;
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Stack ADT in Ada – Body Package
1 procedure Pop(Stk : in out Stack_Type) is
2 begin
3 if Stk.Topsub = 0
4 then Put_Line("ERROR - Stack underflow");
5 else Stk.Topsub := Stk.Topsub - 1;
6 end if;
7 end Pop;
9 function Top(Stk : in Stack_Type) return Integer is
10 begin
11 if Stk.Topsub = 0
12 then Put_Line("ERROR - Stack is empty");
13 else return Stk.List(Stk.Topsub);
14 end if;
15 end Top;
16 end Stack_Pack;
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Using the Stack
1 with Stack_Pack, Ada.Text_IO;
2 use Stack_Pack, Ada.Text_IO;
3 procedure Use_Stacks is
4 Topone : Integer;
5 Stack : Stack_Type;
6 begin
7 Push(Stack,42);
8 Push(Stack,17);
9 Topone := Top(Stack);
10 Pop(Stack);
12 end Use_Stacks;
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
ADTs in OO Languages
Abstract Data Types are now core to many imperative languages.
However, object-oriented languages also provide support for ADTs,
through the class mechanism. ADTs can be seen as a simplified
version of classes, without the extra features that make a language
object-oriented.
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Stack ADT in C++
1 #include<iostream.h>
2 class stack {
3 private:
4 int *stackPtr;
5 int maxLen;
6 int topPtr;
7 public:
8 stack() {
9 stackPtr = new int[100];
10 maxLen = 99;
11 topPtr = -1;
13 ~stack() { delete stackPtr; }
14 int top() { return (stackPtr[topPtr]); }
15 int empty() { return (topPtr == -1); }
Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Stack ADT in C++
1 void push(int number) {
2 if (topPtr == max_len)
3 cerr << "Error in push-stack is full" << endl;
4 else
5 stackPtr[++topPtr] = number;
7 void pop() {
8 if (topPtr == -1)
9 cerr << "Error in pop-stack is empty" << endl;
10 else
11 topPtr--;
Mark Hills CS421 Lecture 18: Data Abstraction
Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
Using the C++ ADT
1 void main() {
2 int topOne;
3 stack stk;
4 stk.push(42);
5 stk.push(17);
6 topOne = stk.top();
7 stk.pop();
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Abstract Data Types ADTs in Ada ADTs in C++
A Key Difference
The Ada and C++ examples are very similar. However, there is a
key difference:
◮ In Ada, the package includes the declaration of a type for the
ADT
◮ In C++, the class is the type of the ADT
◮ This means, in the C++ example, the type can initialize itself
– we don’t run the risk of using an uninitialized stack
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Parameterized ADTs Parameterized ADTs in Ada Parameterized ADTs in C++ Problems with ADTs
But what if I want a float stack?
One problem, noted earlier, is that the type of the stack is declared
in the ADT. What if I want a float stack instead? Also, what if I
want 101 elements?
◮ The Solution: Parameterized ADTs
◮ Allows data and type parameters to be specified to make the
ADT generic
Mark Hills CS421 Lecture 18: Data Abstraction
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Parameterized ADTs Parameterized ADTs in Ada Parameterized ADTs in C++ Problems with ADTs
Parameterized Stack ADT in Ada – Specification Package
1 generic
2 Max_Size : Positive;
3 type Element_Type is private;
4 package Generic_Stack is
5 type Stack_Type is limited private;
6 function Empty (Stk : in Stack_Type) return Boolean;
7 procedure Push (Stk : in out Stack_Type;
8 Element : in Element_Type);
9 procedure Pop (Stk : in out Stack_Type);
10 function Top (Stk : in Stack_Type) return Element_Type;
Outline Basic Data Abstraction Abstract Data Types Parameterized ADTs
Parameterized ADTs Parameterized ADTs in Ada Parameterized ADTs in C++ Problems with ADTs
Parameterized Stack ADT in Ada – Specification Package
1 private
2 type List_Type is array (1..Max_Size) of Element_Type;
3 type Stack_Type is
4 record
5 List : List_Type;
6 Topsub : Integer range 0..Max_Size := 0;
7 end record;
8 end Generic_Stack;