Stateful Programming

Despite the virtues of pure functional programming, there are times when you'd like to be able to update memory directly.

Some examples:

tex2html_wrap_inline115 Generating unique identifiers.

tex2html_wrap_inline115 Manipulating a potentially cyclic graph structure.

tex2html_wrap_inline115 Building a histogram of values or doing a bin sort.

tex2html_wrap_inline115 Recording global state.

To support such applications, ML includes references and arrays.

References

Think of a reference as a container for a value.

References are created using a special (built-in) data constructor ref, applied to the initial contents of the reference.

code49

The current value of a reference can be fetched using the ! operator.

code53

The current value of a reference can be updated using the := operator.

code57

Note that the type of a reference is formed by applying the (built-in) parameterized type constructor tex2html_wrap_inline123 ref to the type are:

code61

Examples

We can use refs for anything that an ordinary (imperative language) variable could be used for:

code65

No real advantage over functional code in this case.

Examples (cont.)

A better application: generating unique ids:

code68

First-class references

References are fully first-class values in their own right. This means they can be embedded in data structures, passed as arguments or returned as function results.

For example, references can be used to get the effect of call-by-reference parameters.

code72

Incorrect use of references

Type system prevents us from making the mistake of trying to pass ordinary values:

code76

Updateable dictionaries

code79

Arrays

There is a similar set of constructor and access functions for multi-element arrays, providing constant-time access and update to elements. To use arrays, must first say open Array;

Arrays are created dynamically; their size is fixed only when they are created, and is not part of their type. Array bounds are checked at runtime. Elements are indexed from 0.

code85

There is a similar datatype Vector which allows constant-time access but no update.

Histograms

code89

Doing without built-in state

What would it cost us to insist on being purely functional?

We can simulate the behavior of an n-element state at the cost of a factor of just tex2html_wrap_inline127 .

To see this, view the reference mechanism as a way of associating unique keys (the reference itself) with values (the reference's contents).

tex2html_wrap_inline115 The ref operation invents a new key and pairs it with an initial value. (We cannot examine the key, but we can compare it with other keys!)

tex2html_wrap_inline115 The ! operator fetches the value associated with a key.

tex2html_wrap_inline115 The := operator changes the value associated with a key.

Viewed as an abstract datatype, this is just a dictionary. It is well-known how to implement dictionaries of size n so that all operations take O(lg n) time.

Arrays are similar, except that array index becomes part of key.



Andrew P. Tolmach
Tue May 13 18:40:06 PDT 1997