4.18 Towards State and Abstract Data Structures

Stateful programming is a useful concept known from imperative programming languages. It is also supported by many functional languages, where state is typically used to implement abstract data structures (ADS). These are called objects in object-oriented programming. Typical examples for abstract data structures are stacks, queues, and dictionaries.

The primitives needed for stateful programming are stateful references, i.e. references to mutable values. Logic variables are not stateful in that sense since their value can never be changed. Only the mode of a logic variable is mutable if the logic variable gets bound to its value.

Thoughout this book, we will frequently use stateful programming with abstract data structures. For now, we will only work with predefined abstract data structures and show how to use them. How to write your own abstract data structures will be discussed in the chapter on Stateful Data Structures.

The nice thing with abstract data structures is that they abstract away from their implementation. Therefore, one can also easily exchange the implementation of an abstract data structure without changing its usage.

Abstract data structures (that are called objects in object oriented programming) can only be accessed by a set of functions (that called methods in object oriented programming). It is thus sufficient to know these functions (and not their implementation) to use them. A record of such functions is returned when creating an abstract data structure. The types of these functions describe the interface to the abstract data structure. The creation is done by calling a creator function (that is called class in object oriented programming).


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)