Abstract data type

In computing, an abstract data type or abstract data structure is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics. An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.

For example, an abstract stack data structure could be defined by two operations:, that inserts some data item into the structure, and  , that extracts an item from it; with the constraint that each pop always returns the most recently pushed item that has not been popped yet. When analyzing the efficiency of algorithms that use stacks, one may also specify that both operations take the same time no matter how many items have been pushed into the stack, and that the stack uses a constant amount of storage for each element.

Abstract data types are purely theoretical entities, used (among other things) to simplify the description of abstract algorithms, to classify and evaluate data structures, and to formally describe the type systems of programming languages. However, an ADT may be implemented by specific data types or data structures, in many ways and in many programming languages; or described in a formal specification language. ADTs are often implemented as modules: the module's interface declares procedures that correspond to the ADT operations, sometimes with comments that describe the constraints. This information hiding strategy allows the implementation of the module to be changed without disturbing the client programs. Abstract data types are also an important conceptual tool in object-oriented programming and design by contract methodologies for software development.

The name "abstract data type" apparently was coined by researchers in software engineering and programming language design; while "abstract data structure" was coined by researchers in data structures and algorithms.

Since ADTs are theoretical concepts, there are no standard conventions for defining them. A broad division may be drawn between "imperative" and "functional" definition styles.

Imperative abstract data type definitions
In the "imperative" view, which is closer to the philosophy of imperative programming languages, an abstract data structure is conceived as an entity that is mutable — meaning that it may be in different states at different times. Some operations may change the state of the ADT; therefore, the order in which operations are evaluated is important, and the same operation on the same entities may have different effects if executed at different times — just like the instructions of a computer, or the commands and procedures of an imperative language. To underscore this view, it is customary to say that the operations are executed or applied, rather than evaluated. The imperative style is often used when describing abstract algorithms.

Abstract variable
Imperative ADT definitions often depend on the concept of an abstract variable, which may be regarded as the simplest non-trivial ADT. An abstract variable V is a mutable entity that admits two operations: with the constraint that
 * (V,x) where x is a value of unspecified nature; and
 * (V), that yields a value;
 * (V) always returns the value x used in the most recent (V,x) operation on the same variable V.

As in many programming languages, the operation (V,x) is often written V $$\leftarrow$$ x (or some similar notation), and  (V) is implied whenever a variable V is used in a context where a value is required. Thus, for example, V $$\leftarrow$$ V + 1 is commonly understood to be a shorthand for (V, (V) + 1).

In this definition, it is implicitly assumed that storing a value into a variable U has no effect on the state of a distinct variable V. To make this assumption explicit, one could add the constraint that
 * if U and V are distinct variables, the sequence { (U,x);  (V,y) } is equivalent to {  (V,y);  (U,x) }.

More generally, ADT definitions often assume that any operation that changes the state of one ADT instance has no effect on the state of any other instance (including other instances of the same ADT) — unless the ADT axioms imply that the two instances are connected (aliased) in that sense. For example, when extending the definition of abstract variable to include abstract records, the operation that selects a field from a record variable R must yield a variable V that is aliased to that part of R.

The definition of an abstract variable V may also restrict the stored values x to members of a specific set X, called the range or type of V. As in programming languages, such restrictions may simplify the description and analysis of algorithms, and improve their readability.

Note that this definition does not imply anything about the result of evaluating (V) when V is un-initialized, that is, before performing any   operation on V. An algorithm that does so is usually considered invalid, because its effect is not defined. (However, there are some important algorithms whose efficiency strongly depends on the assumption that such a  is legal, and returns some arbitrary value in the variable's range.)

Instance creation
Some algorithms need to create new instances of some ADT (such as new variables, or new stacks). To describe such algorithms, one usually includes in the ADT definition a  operation that yields an instance of the ADT, usually with axioms equivalent to This axiom may be strengthened to exclude also partial aliasing with other instances. On the other hand, this axiom still allows implementations of  to yield a previously created instance that has become inaccessible to the program.
 * the result of  is distinct from any instance S in use by the algorithm.

Preconditions, postconditions, and invariants
In imperative-style definitions, the axioms are often expressed by preconditions, that specify when an operation may be executed; postconditions, that relate the states of the ADT before and after the exceution of each operation; and invariants, that specify properties of the ADT that are not changed by the operations.

Example: abstract stack (imperative)
As another example, an imperative definition of an abstract stack could specify that the state of a stack S can be modified only by the operations with the constraint that:
 * (S ,x), where x is some value of unspecified nature; and
 * (S), that yields a value as a result;


 * For any value x and any abstract variable V, the sequence of operations { (S,x); V $$\leftarrow$$  (S) } is equivalent to { V $$\leftarrow$$ x };

Since the assignment { V $$\leftarrow$$ x }, by definition, cannot change the state of S, this condition implies that { V$$\leftarrow$$ (S) } restores S to the state it had before the {  (S,x) }. From this condition and from the properties of abstract variables, it follows, for example, that the sequence


 * { (S,x);  (S,y); U $$\leftarrow$$  (S);  (S,z); V$$\leftarrow$$  (S); W $$\leftarrow$$  (S); }

where x,y, and z are any values, and U, V, W are pairwise distinct variables, is equivalent to


 * { U $$\leftarrow$$ y; V$$\leftarrow$$ z; W$$\leftarrow$$ x }

Here it is implicitly assumed that operations on a stack instance do not modify the state of any other ADT instance, including other stacks; that is,
 * For any values x,y, and any distinct stacks S and T, the sequence { (S,x);  (T,y) } is equivalent to {  (T,y);  (S,x) }.

A stack ADT definition usually includes also a Boolean-valued function (S) and a   operation that returns a stack instance, with axioms equivalent to
 * ≠ S for any stack S (a newly created stack is distinct from all previous stacks)
 * ( ) (a newly created stack is empty)
 * ( (S,x)) (pushing something into a stack makes it non-empty)

Single-instance style
Sometimes an ADT is defined as if only one instance of it existed during the execution of the algorithm, and all operations were applied to that instance, which is not explicitly notated. For example, the abstract stack above could have been defined with operations (x) and , that operate on "the" only existing stack. ADT definitions in this style can be easily rewritten to admit mutiple coexisting instances of the ADT, by adding an explicit instance parameter (like S in the previous example) to every operation that uses or modifies the implicit instance.

On the other hand, some ADTs cannot be meaningfully defined without assuming multiple instances. This is the case when a single operation takes two distinct instances of the ADT as parameters. For an example, consider augmenting the definition of the stack ADT with an operation (S,T) that checks whether the stacks S and T contain the same items in the same order.

Functional ADT definitions
Another way to define an ADT, closer to the spirit of functional programming, is to consider each state of the structure as a separate entity. In this view, any operation that modifies the ADT is modeled as a mathematical function that takes the old state as an argument, and returns the new state as part of the result. Unlike the "imperative" operations, these functions have no side effects. Therefore, the order in which they are evaluated is immaterial, and the same operation applied to the same arguments (including the same input states) will always return the same results (and output states).

In the functional view, in particular, there is no way (or need) to define an "abstract variable" with the semantics of imperative variables (namely, with  and   operations). Instead of storing values into variables, one passes them as arguments to functions.

Example: abstract stack (functional)
For example, a complete functional-style definition of a stack ADT could use the three operations: with the following axioms:
 * : takes a stack state and an arbitrary value, returns a stack state;
 * : takes a stack state, returns a value;
 * : takes a stack state, returns a stack state;
 * ( (s,x)) = x (pushing an item onto a stack leaves it at the top)
 * ( (s,x)) = s (  undoes the effect of  )

In a functional-style definition there is no need for a  operation. Indeed, there is no notion of "stack instance". The stack states can be thought of as being potential states of a single stack structure, and two stack states that contain the same values in the same order are considered to be identical states. This view actually mirrors the behavior of some concrete implementations, such as linked lists with hash cons.

Instead of, a functional definition of a stack ADT may assume the existence of a special stack state, the empty stack, designted by a special symbol like Λ or ""; or define a   operation that takes no aguments and returns this special stack state. Note that the axioms imply that In a functional definition of a stack one does not need an  predicate: instead, one can test whether a stack is empty by testing whether it is equal to Λ.
 * (Λ,x) ≠ Λ

Note that these axioms do not define the effect of (s) or  (s), unless s is a stack state returned by a. Since  leaves the stack non-empty, those two operations are undefined (hence invalid) when s = Λ. On the other hand, the axioms (and the lack of side effects) imply that (s,x) =  (t,y) if and only if x = y and s = t.

As in some other branches of mathematics, it is customary to assume also that the stack states are only those whose existence can be proved from the axioms in a finite number of steps. In the stack ADT example above, this rule means that every stack is a finite sequence of values, that becomes the empty stack (Λ) after a finite number of s. By themselves, the axioms above do not exclude the existence of infinite stacks (that can be  ed forever, each time yielding a different state) or circular stacks (that return to the same state after a finite number of  s). In particular, they do not exclude states s such that (s) = s or  (s,x) = s for some x. However, since one cannot obtain such stack states with the given operations, they are assumed "not to exist".

Typical operations
Some operations that are often specified for ADTs (possibly under other names) are


 * (s,t), that tests whether two structures are equivalent in some sense;
 * (s), that computes some standard hash function from the instance's state;
 * (s)or (s), that produces a human-readable representation of the structure's state.

In imperative-style ADT definitions, one often finds also


 * , that yields a new instance of the ADT;
 * (s), that prepares a newly-created instance s for further operations, or resets it to some "initial state";
 * (s,t), that puts instance s in a state equivalent to that of t;
 * (t), that performs s ↝,   (s,t), and returns s;
 * (s) or (s), that reclaims the memory and other resources used by s;

The  operation is not normally relevant or meaningful, since ADTs are theoretical entities that do not "use memory". However, it may be necessary when one needs to analyze the storage used by an algorithm that uses the ADT. In that case one needs additional axioms that specify how much memory each ADT instance uses, as a function of its state, and how much of it is returned to the pool by.

Examples
Some common ADTs, which have proved useful in a great variety of applications, are
 * Container
 * Deque
 * List
 * Map
 * Multimap
 * Multiset
 * Priority queue
 * Queue
 * Set
 * Stack
 * String
 * Tree

Each of these ADTs may be defined in many ways and variants, not necessarily equivalent. For example, a stack ADT may or may not have a  operation that tells how many items have been pushed and not yet popped. This choice makes a difference not only for its clients but also for the implementation.

Implementation
Implementing an ADT means providing one procedure or function for each abstract operation. The ADT instances are represented by some concrete data structure that is manipulated by those procedures, according to the ADT's specifications.

Usually there are many ways to implement the same ADT, using several different concrete data structures. Thus, for example, an abstract stack can be implemented by a linked list or by an array.

An ADT implementation is often packaged as one or more modules, whose interface contains only the signature (number and types of the parameters and results) of the operations. The implementation of the module — namely, the bodies of the procedures and the concrete data structure used — can then be hidden from most clients of the module. This makes it possible to change the implementation without affecting the clients.

When implementing an ADT, each instance (in imperative-style definitions) or each state (in functional-style definitions) is usually represented by a handle of some sort.

In object-oriented programming languages, such as C++ and Java, an ADT is typically implemented as class, and each instance of the ADT is an object of that class. The module's interface typically declares the constructors as ordinary procedures, and most of the other ADT operations as methods of that class.

Example: implementation of the stack ADT
As an example, here is an implementation of the stack ADT above in the C programming language.

Imperative-style interface
An imperative-style interface might be:

This implementation could be used in the following manner:

This interface can be implemented in many ways. The implementation may be arbitrarily inefficient, since the formal definition of the ADT, above, does not specify how much space the stack may use, nor how long each operation should take. It also does not specify whether the stack state t continues to exist after a call s ↝ (t).

In practice the formal definition should specify that the space is proportional to the number of items pushed and not yet popped; and that every one of the operations above must finish in a constant amount of time, independently of that number. To comply with these additional specifications, the implementation could use a linked list, or an array (with dynamic resizing) together with two integers (an item count and the array size).

Functional-style interface
Functional-style ADT definitions are more appropriate for functional programming languages, and vice-versa. However, one can provide a functional style interface even in an imperative language like C. For example:

ADT libraries
Many modern programming languages,such as C++ and Java, come with standard libraries that implement several common ADTs, such as those listed above.

Built-in abstract data types
The specification of some programming languages is intentionally vague about the representation of certain built-in data types, defining only the operations that can be done on them. Therefore, those types can be viewed as "built-in ADTs". Examples are the arrays in many scripting languages, such as Awk, Lua, and Perl, which can be regarded as an implementation of the Map or Table ADT.