Ch.6 Introduction to ML programming

§ 6.12. Imperative features of ML

ML is a language where programs are defined by composing expressions, and is suitable for declarative programming. So far we have used numerical functions such as , but as pointed out in Section 6.2,the intention of declarative programming is to write what the program should express directly as a code, and not intend to define a program as a mathematical function.

If the actual problem is best expressed as a sequence of procedure, then directly writing done that sequence would be most declarative. For example, displaying a sentence on the video display you are looking at is the process to override the previous sentence with the new sequence of characters. In an actual implementation, this is done by modifying the frame buffer of the video screen hardware. This process is inherently procedural, and therefore best described as a process to update the buffer. Input from and output to external world inevitably have this property. For those problems, procedural representation would be most direct and therefore concise and readable, thus declarative.

Integrating imperative features in a functional language requires the following.

  1. Introduction of updating a mutable state. In the previous display example, display contents can be understood as the state of the display. The basis of imperative programming is to update states to obtain the desired state. For this, the introduction of mutable data structures is required.

  2. A predictable evaluation order. In the display example, the order of updates determine the image and its motion in the display. To write a procedural program, it is necessary to control the order of update operations.

ML introduces these two in a framework of functional programming.