Programming Notes – Programming Solutions Part 1
The goal of a program statement is to achieve some expected result. A programming solution is that combination of initial state and program statement that achieves the expected result. Suppose that R is an expected result. The Hoare Triple
P {r} R
is a programming solution in which the precondition in the Hoare Triple is a subset of the weakest precondition. Given the initial state P, proper termination of program statement r must lead to expected result R.
The process of designing a program is really the process of deriving a sequence of programming solutions, and the most basic process involves the derivation of a programming solution for one programming statement. This page contains a few simple rules that help to formalize the derivation of programming solutions for a single program statement.
The condition of an expected result is the set of program states that lead to the expected result. The form of the condition is an extension of set-relations concepts found in Theory of Conditions. The program statement condition uses the relation corresponding to the program statement, and may be defined as:
Definition 1: cond.r.R = dom.r.R – dom.r.~R
The domain of R contains all points that map into the set R. The condition contains the domain points less any points that also map into a point not in R. The condition of R forms the solution:
The set-condition also forms a solution.
When the relation corresponding to the program statement r is a function, the domain dom.r.R is also a condition cond.r.R. Therefore the domain of a program statement that behaves like a function is also a solution.
If the domain of a result is disjoint from the domain of the result’s complement, then the domain is also a solution.
Theorem 4: (dom.r.R Ç dom.r.~R = Æ) Þ dom.r.R {r} R
One can derive new solutions from existing solutions by using the properties of WP.r.R. If result R is a subset of set S, then solutions using result R are also solutions using result S.
If set P is a subset of precondition Q, then solutions using precondition Q are also solutions using precondition P.
Corollary: WP.r.R = WP.s.R Þ P {r} R º P {s} R
P Ç WP.r.R = P Ç WP.s.R
Theorem 16: Æ {r} R
Theorem 18: P = Æ º P {r} Æ
Bob Stanton’s clever Maze Problem as shown in Games Magazine provides an excellent opportunity to discover program statement solutions using a program statement very different from statements common to conventional programming languages.
The Maze Problem game consists of a square configuration of 36 cells plus an entry cell in the upper left-hand corner and an exit cell in the lower right-hand corner. The purpose of the game is to move the cell marker from the entry cell to the exit cell according to the rules of the game. The cell marker may move to any cell in the direction indicated by the marked cell. The direction of movement for the next move depends on the direction indicated by the target cell. The cell marker continues through the maze until it reaches the exit cell.
Activation of a Maze Problem program statement moves the cell marker the number of cells indicated by the instruction in the direction indicated by the cell. For example, a first move of 3 moves the cell marker down 3 cells. If the number of cells is greater than the available cells in the indicated direction, then the cell marker stops at the end of the row or column.
Each cell may be denoted by the row and column of the cell starting with row 0 and column 0. The starting cell in the upper left hand corner is cell(0,0). The bottom cell in the first column with an ‘R’ direction is cell(6,0), because the cell is in row 6 and column 0. The exit cell in the bottom right corner is in row 7 and column 5 and denoted by cell(7,5).
Suppose the first move is move(1). Then the cell marker would move down 1 cell to cell(1,0). The direction of cell(1,0) is R for right. A second move of move(3) sends the cell marker to cell(1,3).
There is only one result for any move in the Maze Problem game, so the relation for each move is a function. Theorem 3, above, shows that a solution to a program statement and expected result is the domain. Suppose that the target cell is cell(1,3) and the move is move(3). The domain contains any cell that is 3 cells away in the same row or column with a direction towards cell(1,3). Thus
dom.move(3),{cell(1,3)} = {cell(1,0),cell(4,3)}.
Problems:
1. Play the Maze Problem until you are familiar with the program.
2. dom.move(1).{cell(7,5)} = Æ. Why?
3. What cell or cells could lead to a move to cell(7,5)? What would be the move, and how would the domain be stated?
4. What cell or cells could lead to the domain of cell(7,5)? What would be the move, and how would the domain be stated?
References:
Dijkstra, Edsgar, 1976, A Discipline of Programming, Englewood Cliffs, NJ: Prentice-Hall
Gries, David, 1981, The Science of Programming, New York, NY: Springer-Verlag
Gries, David and Schneider, Fred B., 1993, A Logical Approach to Discrete Math, New York, NY: Springer-Verlag