



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Susan Older and Jim Royer January 2008 revision for CIS 252
We shall use a general design recipe^1 when developing programs in this course. The intent of the design recipe is to provide you with three things:
Below we discuss the basic design recipe. We shall revise the recipe over time to cover new ways to construct programs. Right now we are presuming only the “high-school algebra” part of Haskell.^2 Here is a tiny, complete example of the end result of following the basic design recipe. (Recall that anything following a “--” is a comment in Haskell.)
-- CONTRACT -- areaOfRing : : number -> number -> number -- PURPOSE -- Takes two numbers, outer and inner, and computes the area of -- a ring with radius outer and with a hole with radius inner. -- EXAMPLES -- (areaOfRing 5 3) should return 50.24. -- (areaOfRing 5 4) should return 28.26. -- DEFINITION areaOfRing outer inner = (areaOfDisk outer) - (areaOfDisk inner) -- TESTS ans1 = areaOfRing 5 3 -- should be 50. ans2 = areaOfRing 5 4 -- should be 28.
We shall use the above as a running example in discussing the parts of the design recipe below.
(^1) This design recipe is adapted the text How to Design Programs by Felleisen, Findler, Flatt, and Krishna-
murthi [FFFK01]. Their book is on the web here, and their first discussion of the design recipe is here. Note that, instead of Haskell, they are the language Scheme (a second cousin to Haskell), so the finer details of that section will not apply here. We shall also use their “area of a ring” example in explaining the ingredients of the design recipe. (^2) Which is just like ordinary high school algebra with an enlightened view of syntax.
The design recipe is essentially a programming version of Polya’s How to Solve It scheme [Pol45]. Polya tells us that our first step is top understand the problem. With a program this usually comes down to answering:
Question 3 is the interesting one, but first let’s deal with 1 and 2.
Example. We want a program to compute the area of a ring. A ring consists of a disk with a circular hole in it. [Polya: Draw the picture!] Here is a picture: }v Our givens are: the radius of the outer disk and the radius of the inner disk. The area of the ring is going to be a number too. So the answer to Question 1 is that the inputs are two numbers and the answer to Question 2 is that output is a number.
In the program text we state our answer to Questions 1 and 2 with a contract. For our ring- area example, the contract pwould be:
-- areaOfRing : : number -> number -> number
The general form of a contract for an n-input program is:
-- 〈program-name〉 : : 〈type 1 〉 - > ... -> 〈typen〉 - > 〈typeout〉
(Later on, we shall generalize the stuffings out of this.) For the time being think of “: :” and “->” just as Haskell punctuation. The funny things of the form 〈name〉 are “fill in the blank” variables. The name of your program goes in the 〈program-name〉 slot. The type of the i-th input goes in the 〈typei〉 slot, and the type of the output goes in the 〈typeout〉 slot. Type is the formal name of “the kind of data.” Types can describe numbers, character strings, lists, pictures, trees, and on and on.
It turns out that Haskell cares intensely about contracts. We will later place our contracts outside of comments so the Haskell interpreter can check them for us.
contract gives you the general shape of the program. The purpose statement fills in the contents of that shape.
The second goal may seem silly since you are shortly going to write the program anyhow. The point of doing some hand calculations is to play computer with concrete data and to try to think about possible ways of doing the computations.
The example, continued. We decide to use 3.14 as an approximation to π. So we can compute some values of what areaOfRing should produce. Specifically:
-- EXAMPLES -- (areaOfRing 5 3) should return 50.24. -- (areaOfRing 5 4) should return 28.26.
The goal here is to produce concrete examples of what the program should do. In particular, you want to identify key examples that illustrate tricky cases that the program needs to handle.
What is the point of the examples?
The example, continued. To keep our story short, we suppose we just happen to have lying around the program
areaOfDisk r = 3.14 * r * r
that computes the area of a disk with radius r. So, based on step 2,
areaOfRing outer inner = (areaOfDisk outer) - (areaOfDisk inner)
will serve as our definition of areaOfRing.
The goal here is to produce a correct definition of our program based on the analysis of the previous three steps.
(^5) This approach to programming goes under the name testing-first programming and is much beloved by the extreme programming folks. For example, see here and here.
What is the point of the definition? Well, we are doing computing, not (just) mathematics! Our definition is a purported algorithmic solution to the original problem.
N.B.^6 This step of the design recipe will be greatly expanded in our refinements of the recipe. When dealing with “high-school algebra” programs, there isn’t much to say here.
This step corresponds to Polya’s question Can you check your result? in his looking-back step.
The example, concluded. Now we are ready to run some tests of our definition. All of the examples from step 3 become test examples.
-- TESTS ans1 = areaOfRing 5 3 -- should be 50. ans2 = areaOfRing 5 4 -- should be 28.
When we look at the values of ans1 and ans2 they agree with the target values. So, we declare success—we (more or less) trust our definition.
The goal here is to put your program through its paces on some well-chosen data.
What is the point of the tests? If there are difficulties with your definition and if you chose good test cases, then those difficulties are likely to show up here. (The goodness of your test conditions needs to be established in steps 2 and 3.) So if your program passes your test, you have good evidence for the correctness of your program.
N.B. To guarantee correctness with just testing, you have to check all possible inputs, and there are almost always too many inputs to do this. In the case of our pitiful little example, if we assume our inputs are 64-bit floating point numbers, then a little calculation shows us that there are 340, 282, 366, 920, 938, 463, 463, 374, 607, 431, 768, 211, 456
possible test cases.^7
Producing a well-documented, well-tested program is just one of the end results of the de- sign process. Another less obvious one is that we’ve learned something about the algorith- mic problem we’ve just solved—and you have often learned more than you have realized. So, as Polya suggests, can we look back and see, if we can solve the problem in different way or use what we’ve learned here in another place. This can be where the fun starts.
In the case of our little example, we’ll leave well enough alone.
(^6) “N.B.” abbreviates the latin phrase nota bene which means “note well”, that is, pay special attention. (^7) As a point of comparison, the age of the Earth in seconds is about 200,000,000,000,000,000 (= 2 × 1017 ).