7.1 State in Abstract Data Structures

One the one hand side, stateful data structures can support simpler algorithms and help to achieve better complexity. On the other hand side, stateful programming notoriously bears the dangers: It might easilly lead a programmer to confuse conceptual and implementation concernces during software devellopment: a program should never become more difficult to maintain or understand only because stateful programming is used to make it efficient.

Another independent problem with stateful programming is that it may quickly get painful in concurrent programs. The potential problem is that different orders of state changes may lead to different computation result.

Both problems can be addressed by encapsulate state manipulations into abstract data structures as we will always do in what follows. Abstract data structures separate the specification of stateful procedures from their implementation. Abstract data structure not only makes it easier to replace specific implementations of data structures. They also support top down programming devellopment, where one first specifies the functions one needs before implementing them.

We have already seen stateful programming with abstract data structures in the previous section. There, we used predefined stacks to implement agendas and bags. Agendas gave us a simple way to administrate the intermediate results of a recursive process. Bags allowed us to collect the set of results of a process with multiple return values.

Oz offers stateful concepts through four independent primitives: cells, dictionaries, arrays, and objects. All of them are useful, even though each one were sufficient as a basis of stateful programming in principle. Stacks and dictionaries will be most important throughout this book. Cells will only be used to implement stacks and queues.


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