Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Development by Stepwise Refinement

Similar presentations


Presentation on theme: "Program Development by Stepwise Refinement"— Presentation transcript:

1 Program Development by Stepwise Refinement
Niklaus Wirth

2 Motivation Programming is usually taught via examples
Examples normally demonstrate what a computer can do Examples are not normally chosen by their suitability for a technique Programming is not just mastering of a language – more important to master the methods of design and construction.

3 Aim The paper aims to demonstrate the development of an example program through a sequence of refinement steps Today, we refer to this approach as top-down design (or top-down decomposition) The program and data specification are refined in parallel

4 Refinement Each refinement implies design decisions
It is important to make these explicit Be aware of the critieria for the refinement Be aware there are alternatives (after all, this might not work out) Decompose decisions as much as possible Defer decisions about data as long as possible

5 The 8-Queens Problem Given an 8X8 chessboard and 8 queens which are hostile to each other, find a position for each queen such that no queen may be taken by any other queen. NOTE: Eight-Queens on wikipedia has an animated example of the Recursive solution solving this problem

6 Brute Force Approach A = Set of candidates for the solution
One exists that satisfies condition p Repeat Generate the next element of A and call it x until p(x) OR (no more elements in A) if p(x) then x = solution

7 Issues Problem space is too large We need a way to preselect solutions
Computers at the time would take ~ 7 hrs We need a way to preselect solutions Criteria Smaller than A Elements easily generated Easier to test than p(x)

8 8-Queens: Unique Columns
Every column must have exactly 1 queen Now we are just testing rows and diagonals We also need the ability to “try” a row for a queen and backtrack if the test for the row/diagonal fails.

9 Program At This Point repeat trycolumn; if safe then
setqueen; considernextcolumn; else regress until lastcoldone OR regressoutoffirstcol

10 Define the Instructions
Considerfirstcolumn Considernextcolumn Reconsiderpriorcolumn Advancepointer Testsquare Setqueen removeQueen

11 Refining the Instructions
Upon realizing that we can store the indexes of occupied rows, and thru carefully choosing our array structures, we can reduce the instruction set to: testsquare setqueen regress movequeen

12 A recursive alternative
View the problem as starting with one column Extend the board by one column at each step

13 Generalizing the Problem
We often need to change our program based on new requirements… What if we want ALL possible solutions?

14 Conclusions Programs are constructed thru a sequence of refinement tasks Each task is decomposed into subtasks The degree of decomposition (modularity) will determine the ease with which we can modify the program

15 Conclusions Use a notation that is natural for the problem
Each refinement implies a design decision, criteria include efficiency, storage, clarity The development of this simple problem formed a long story, programming is not trivial – simplifying the problem is important.


Download ppt "Program Development by Stepwise Refinement"

Similar presentations


Ads by Google