6

After reading lots of pages on various sites, I came to the conclusion that abstract data-type (ADT) helps to separate the use of a data-structure from its implementation. We can easily map the data into a data-structure by using an already available abstraction. And this abstraction is called ADT. Please tell me if I have understood this correctly. Also if you can give a small example, it will be very helpful. If you prefer to give any code in your example, C or Python would be helpful for me.

Addition: They are a very special implementation of data abstraction. What separates them from other instances of abstractions is that ADTs are theoretical constructs. Theoretical constructs that help map or represent data in a desired data structure. They have attributes or operations associated with them. These operations may be applied on the data that has been mapped into the ADT. How the data is actually stored inside the ADT or how the operations work are kept hidden.

EDIT: I had previously said, "ADT is like the blueprint of a data-structure". A few of the answers have explained why it's not. I didn't have a clear comprehension of what a 'blueprint' is and that was the reason for using the term. So, I've now removed it from my original text.

4
  • 1
    ADTs are based on (static) types, Python doesn't have them, and C's type system isn't powerful enough … both are bad languages to demonstrate ADTs in. CLU is a language that was explicitly designed for ADTs, it would be a much better choice. Commented Jan 1, 2014 at 7:37
  • 1
    @JörgWMittag I think you're demanding too much from an example. Not only is it perfectly possible to formalize ADTs in both languages, one can also give an instructional example while not teaching the language about the ADT at all.
    – user7043
    Commented Jan 1, 2014 at 10:15
  • While we're at it: is there a good discriminator between ADTs (in this sense) and interfaces? They seem very much the same, we only call the system implementing the interface a "data structure" and somehow limit the scope of available operations.
    – 9000
    Commented Jan 1, 2014 at 11:06
  • @9000 from my understanding, in a set of operations, if a type can be distinguished from a subset (ie. there are operations to create and manipulate a certain kind of data, and nothing else is known about it), than one can say that type is abstract and this subset is its interface. Modules and other kinds of services may also have interfaces, but these not necessarily manifest a new type, instead, they just operate on existing ones. Commented Jan 1, 2014 at 18:31

5 Answers 5

7

This is just a gist on the subject, I strongly suggest following the links herein.

"An abstract data type defines a class of abstract objects which is completely characterized by the operations available on those objects. This means that an abstract data type can be defined by defining the characterizing operations for that type." Liskov, Zilles: Programming with abstract data types

Given the informal definition above, the following pseudo-code can be seen as an ADT:

type stack;
stack stack_create();
void stack_push(stack, T);
T stack_pop(stack);

One point is that nothing is known about stack objects, except what can be inferred by the operations available for it -- in the case above, stack_create, stack_push and stack_pop. Every single bit of implementation and structure detail is left to the implementation side.

Now, take an OO/python class called Stack with the methods push and pop.

class Stack:

    def __init__(self):
       ...
    def push(self, element):
       ...
    def pop(self):
       ...

If its clients know that there is a class called Stack and they have access to it, one way to see it is that the objects are characterized by the fact that they are instances of the class Stack whose structure contains push and pop fields -- regardless of the fact that they are callable. Clients also know that they can inherit from Stack to create something else and so on.

That is, much more is known/exposed to clients. Therefore, this Stack type is much less abstract than the previous one (it is exposed as a concrete structure with two fields, has a class with which you can do inheritance, etc). Consequently, one could argue this Stack type is not an Abstract Data Type conforming to the given definition. To be more strict, one would have to write something like this:

def create_stack():
   ...
def stack_push(stack, element):
   ...
def stack_pop(stack):
   ...

And here is a possible implementation of it (in the case below, if you already have the class above and just want to make it an ADT, it is just a wrapper):

def create_stack():
   retur Stack()
def stack_push(stack, element):
   return stack.push(element)
def stack_pop(stack):
   return stack.pop()

Now the client only knows the operations and their interfaces and use them (and only them) to infer what is the type "stack". From outside, nothing is known about the actual implementation and structure of a stack (unless they breach the abstraction by inspecting the structure of the result of create_stack).

Similarly, in C, you could mimic this by putting these function declarations on a .h header file (using a forward declaration standing as the stack type, or simply void*), and then, putting the struct definition and function implementations in the .c file.

Note on type systems: All the above, of course, are not in the context of languages with static type checking and good type systems -- such language may need to provide certain instruments to make reasonable ADTs possible. Furthermore, the way this theme is exposed in the paper cited above, "strong" type checking appears to be a matter of author preference alone; I did not see them being discussed or shown to be a requirement of ADTs -- the text actually seems to be careful in discussing type checking as something related but not essential. Plus, the explanation of a "C approach to ADT" (see above) was given by William Cook, the researcher who wrote Object-Oriented Programming Versus Abstract Data Types. [edit] On the other hand, Cook writes:

"Abstract data types depend upon a static type system to enforce type abstraction. It is not an accident that dynamic languages use objects instead of user-defined abstract data types. Dynamic languages typically support built-in abstract data types for primitive types; the type abstraction here is enforced by the runtime system."

7

You have understood this correctly. ADTs are portable, theoretical constructs that can then be implemented in most any given programming language.

Example: Stack

A Stack as an ADT is a list of some type that provides Last In, First Out (LIFO) access to its data members. Thus it can only be accessed at one point, the "top" of the stack.

In Python that'd look something like:

class Stack:

    def __init__(self): # initializes the stack with an empty list to hold stuff
        self.list = []

    def push(n): # pushes item n onto the stack
        stack.list.append(n)

    def pop: # removes the "top" item from the stack
        del stack.list[-1]

    def peek: # looks at the top item of the stack
        view = stack.list[-1]
        return view

And that should about do it for one way to implement a basic stack. I've not tested this code so use at your own risk. Python in fact expects you to use lists as stacks by default so it's covered in the tutorial documentation. There's even a built in "pop" method.

Generally, you'd implement a stack as either an array or a linked list. The array can be useful if you want a little bit less memory overhead and it's generally easier to code. However, a list based stack can expand much more easily and quickly.

8
  • Thanks! Can you give me an example of a concrete datatype? Is array an CDT? If so, why?
    – aste123
    Commented Jan 1, 2014 at 0:15
  • The list I used to store things in the stack would be the concrete part, the ADT doesn't define what I store the data in, just what my operations (methods) are and how they work.
    – user28988
    Commented Jan 1, 2014 at 0:17
  • 1
    "ADTs are portable, theoretical constructs that can then be implemented in most any given programming language." – That's not true. ADTs perform abstraction through (static) types. They can only be implemented in a statically typed language. This is in contrast to the other kind of data abstraction in common use, objects, which perform data abstraction through procedures and thus can be implemented in any language with (for example) first-class closures, regardless of the type system. Commented Jan 1, 2014 at 7:35
  • 3
    @JörgWMittag Er, no, an abstract data typr is one that is accessible through a given interface, no matter what the underlying implementation happens to be. If you're lucky, there are even time and space guarantees on the type (like, say, "popping the stack is O(1), pushing onto the stack is amortized O(1), space is O(n) on items in the stack".
    – Vatine
    Commented Jan 1, 2014 at 13:08
  • 1
    I believe you make a typo when you said a Stack is a First-in, First-out list. It's normally considered a Last-in, First-out (LIFO) list. Commented Jan 2, 2014 at 11:58
4

Unfortunately there is a lot of confusion about Abstract Data Types and Objects.

I think that the OP is using the phrase "abstract datatype" as if it meant "data abstraction" or just in the general sense of defining abstract data.

Abstract Data Types are a very specific implementation of Data Abstraction, and is best realized in the language ML. Objects are another form of Data Abstraction.

You can read more about it in my papers here:
http://www.cs.utexas.edu/~wcook/publications.htm#oo

2
  • +1 for On Understanding Data Abstraction, Revisited. It was a real eye-opener for me.
    – Doval
    Commented Feb 10, 2014 at 0:09
  • @w7cook Actually I'm aware that there is difference between an ADT and general implementations of data abstraction. ADT is a theoretical construct. It has operations associated with it which are kept hidden. I do understand that it is a special implementation of the idea of abstraction. I have edited my initial post with the new ideas I developed. Thank you for the link. There are many interesting papers. I'll read them
    – aste123
    Commented Feb 10, 2014 at 2:04
3

An abstract data type is a mathematical abstraction used to describe algorithms and prove them. The idea of an algorithm or of an abstract data type is thus rather independant from any computer language or even from the availability of a computer. (I am not sure if this is your idea of a blueprint.)

Of course, computers and programming language are a very important application field of algorithms, and it is quite common to implement an algorithm in a given computer language. (Lines are even blurrier since many programmers will not even make a strong distinction between an abstract algorithm and its implementation in a given computer language.) So, in order to implement these abstract algorithms described in terms of abstract data types, programmers need to implement abstract data types.

It is a common case, that am implementation of an abstract data type has more properties than required, and one may accidentally use one in a program, which leads to a bug if the implementation is modified.

While formally proving a complex program is typically not worth the effort, it is important to get a confidence that a program accomplishes its purpose and functions correctly. (There is a parallel with mathematics, since mathematical proofs can be written at various levels of details, depending of the audience or the purpose.) The use of appropriate abstract data types is very important for this, because by introducing the adequate abstractions, one makes not only the program easier to reason about or to describe but one also demonstrates its good understanding of the problem and the solution brought by the program.

The computer language OCaml has the ability to expose abstract data types, which are types whose definition is kept private to some module, which helps understanding and using abstract data types.

3

I'll disagree with everyone else and say that you don't have it quite right. :-)

It is like the blueprint of the datastructure.

No, it's actually the opposite of that: it's a photograph from the outside of a device or a building showing the doors and knobs with instructions on what to expect when you use them.

Secondly, ADT's are a lingua franca: a common language for discussing one kind of frequently used software component.

IMO the best lesson on ADT's and their potential implementations is the Javadocs for the container classes. Look at the interfaces and abstract classes. These are the ADT's. For example, the Queue.

2
  • 1
    yeah I got it wrong. So, is it safe to say the datastructure is like the blueprint of the data that is like a logical model of the data and ADTs are like building blocks (more like abstractions or black boxes) for the datastructure?
    – aste123
    Commented Jan 2, 2014 at 12:54
  • I'm sorry, I'm having a hard time understanding because you're using lots of buzzwords which aren't too well defined: "datastructure, data, logical model of the data," etc. In a sense, there is no answer. Only blueprint IMO is very well defined. And I can define it without relying on other terms: a document showing the inner details of how to build something.
    – Dogweather
    Commented Feb 5, 2015 at 21:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.