IPD: Incremental Program Decomposition

When beginning to write slightly larger programs than single purpose “functions”, you might find yourself in a situation where you know what the program should do and also how it should do it. In principle, this should be sufficient for you to declare the program as “done” and LLMs might actually take you from there to a working program. But the real task in front of you is in how to express the program in such a way that it is legible to another one reading it?

What we’re calling “incremental program decomposition” here is an approach you can use to take your understanding of the what and the how and, step by step, translate it into a full program. The facilities provided by languages like Racket [2] are expressive in ways you might not yet appreciate, but will become clearer as you write programs in this style.

Principle and process

When developing a program using IPD, your focus is on narrating how the task is to be accomplished. The goal is that the what must be as apparent as possible in the code without having to explain all that much more. A comment here and a comment there is ok within your program, but if you’re having to write a very long comment that talks about the contents of your program only and seeks to explain its working, you’re not doing a good job of writing the program legibly.

  1. Start by naming the activity your program must accomplish and what it needs in order to do that. This is easily modelled as an ordinary abstraction (a.k.a. “procedure”, “function”).

  2. Write expressions that appear to define that abstraction using Racket syntax and inventing identifiers that stand for concepts you need as you go along, without worrying about whether those concepts are available or not. The goal in such a step is to add some non-zero level of detail about the abstraction you need.

  3. For each of the identifiers you used above which remain undefined, add a new definition – either as a value in simple cases, or (usually) an abstraction again. For abstractions, look at the way you’ve referred to the identifier to gauge things like how many arguments it must have, what kinds of values must those arguments admit and so on.

  4. Apply IPD to the definition of each of these new as yet undefined abstractions! Remember that at each step, you must ensure you’re adding some non-zero level of detail. Otherwise this process will never finish!

  5. As you’re developing the definitions, you might want to check your understanding in the interactions window. Since Racket will complain about undefined identifiers, you can define simple but on-character implementations. For example, if you needed a random number in the range 1 to some max-number, you can define it to always produce 1 since that is valid in all usage contexts. Later on, you can figure out which of Racket’s provided words you can use to capture that concept.

At the end of this process, you should have a working program that ought to be relatively easy [3] for someone reading it to understand without doing much.

Number guessing game (again)

Since it is a simple enough example, we’ll go through this process for the number guessing game which we know how it should work and therefore meets the criterion for starting IPD.

Our first level definition will start out like this –

(define (play-number-guessing-game max-number)
     ...)

There may be different values we might want for max-number and so it makes sense to express this program as an abstraction over the max-number.

We might elaborate our thinking like this below to the next level of detail –

(define (play-number-guessing-game max-number)
   (introduce-game max-number)
   (define secret (choose-secret-number max-number))
   (play-rounds-until player-guesses-secret secret))

We can now get some of the easier definitions out of our way. Note that we know that playing a round requires knowledge of the secret and so we pass the secret as a parameter.

(define (introduce-game max-number)
     (displayln "Welcome to the number guessing game.")
     (display "I'll choose a number from 1 to ") (display max-number)
     (displayln " at random and keep it a secret.")
     (displayln "You have to guess the secret in the fewest attempts you can."))

 (define (choose-secret-number max-number)
     (random 1 (+ 1 max-number)))

If you didn’t know about the random word provided by Racket yet, you can still proceed by temporarily defining choose-secret-number like below –

(define (choose-secret-number max-number) (- max-number 1))

Now we’re left with two intriguing words play-rounds-until and player-guesses-secret.

It is good to first clarify what the “parameters” (the values that map to arguments of an abstraction) are before proceeding with abstractions which use them. In this case, that’s player-guesses-secret. That looks like an abstraction because you need to know two things to determine whether the player has correctly guessed the secret – the secret and a guess given by the player.

(define (player-guesses-secret secret guess)
     ...)

We know that the player guessed right if the secret equals the guess. This abstraction therefore simple seems to be a boolean comparison.

(define (player-guesses-secret secret guess)
    (= secret guess))

Now let’s tackle play-rounds-until.

(define (play-rounds-until win-condition? secret)
    ...)

Note how we’re giving a somewhat abstract term for the specific player-guesses-secret. The constraint on win-condition? is that it must now be an abstraction that takes two arguments – the secret and the player’s guess. Here, we’re using the “end with ?” naming convention to suggest to the reader that win-condition? when used in a context means a truth value.

(define (play-rounds-until win-condition? secret)
     (define guess (ask-players-guess-within max-number))
     (if (win-condition? secret guess)
         (player-won guess)
         (begin (give-hint-to-player secret guess)
                (play-rounds-until win-condition? secret))))

Giving a hint to the player is easy -

; Assumes secret is not equal to guess.
(define (give-hint-to-player secret guess)
   (if (< secret guess)
      (displayln "Guess lower")
      (displayln "Guess higher")))

We know that (win-condition? secret guess) is either #true or #false, so we must take a call based on this. This leads us to using (if ...). But if you noticed, we’ve missed something – ask-players-guess-within needs max-number and we don’t have it available in this context. We should add it to the argument list and that changes our program a bit. So we have this so far –

(define (play-number-guessing-game max-number)
   (introduce-game max-number)
   (define secret (choose-secret-number max-number))
   (play-rounds-until player-guesses-secret secret max-number))

(define (introduce-game max-number)
     (displayln "Welcome to the number guessing game.")
     (display "I'll choose a number from 1 to ") (display max-number)
     (displayln " at random and keep it a secret.")
     (displayln "You have to guess the secret in the fewest attempts you can."))

 (define (choose-secret-number max-number)
     (random 1 (+ 1 max-number)))

 (define (player-guesses-secret secret guess)
     (= secret guess))

(define (play-rounds-until win-condition? secret max-number)
     (define guess (ask-players-guess-within max-number))
     (if (win-condition? secret guess)
         (player-won guess)
         (begin (give-hint-to-player secret guess)
                (play-rounds-until win-condition? secret max-number))))

; Assumes secret is not equal to guess.
(define (give-hint-to-player secret guess)
   (if (< secret guess)
      (displayln "Guess lower")
      (displayln "Guess higher")))

Now we can proceed to define the missing words –

(define (ask-players-guess-within max-number)
    (tell-player-to-guess-within max-number)
    (define guess (read))
    (if (guess-valid? guess max-number)
        guess
        (begin (tell-player-to-type-valid-value max-number)
               (ask-players-guess-within max-number))))

(define (tell-player-to-guess-within max-number)
    (display "What's your guess? (max ")
    (display max-number)
    (display "): "))

(define (guess-valid? guess max-number)
    (and (number? guess)
         (integer? guess)
         (>= guess 1)
         (<= guess max-number)))

(define (tell-player-to-type-valid-value max-number)
    (display "Hey! Type a number between 1 and ")
    (display max-number)
    (displayln "."))

Here I’ve skipped a few steps to define the new words. Work these out for yourself and convince yourself of their validity. Note here that we’ve defined ask-players-guess-within to always result in a valid guess. If the player fails to provide a valid guess (by typing, say woof instead of a number) this will keep asking the player until they provide a valid one.

The only remaining word is player-won.

(define (player-won guess)
    (display "Your guess ")
    (display guess)
    (displayln " is correct! You won!"))

The final program

So putting all that together gives us the following program –

Listing 9 The final complete program
 1(define (play-number-guessing-game max-number)
 2   (introduce-game max-number)
 3   (define secret (choose-secret-number max-number))
 4   (play-rounds-until player-guesses-secret secret max-number))
 5
 6(define (introduce-game max-number)
 7     (displayln "Welcome to the number guessing game.")
 8     (display "I'll choose a number from 1 to ") (display max-number)
 9     (displayln " at random and keep it a secret.")
10     (displayln "You have to guess the secret in the fewest attempts you can."))
11
12 (define (choose-secret-number max-number)
13     (random 1 (+ 1 max-number)))
14
15 (define (player-guesses-secret secret guess)
16     (= secret guess))
17
18 (define (play-rounds-until win-condition? secret max-number)
19     (define guess (ask-players-guess-within max-number))
20     (if (win-condition? secret guess)
21         (player-won guess)
22         (begin (give-hint-to-player secret guess)
23                (play-rounds-until win-condition? secret max-number))))
24
25 ; Assumes secret is not equal to guess.
26 (define (give-hint-to-player secret guess)
27     (if (< secret guess)
28         (displayln "Guess lower")
29         (displayln "Guess higher")))
30
31 (define (ask-players-guess-within max-number)
32     (tell-player-to-guess-within max-number)
33     (define guess (read))
34     (if (guess-valid? guess max-number)
35         guess
36         (begin (tell-player-to-type-valid-value max-number)
37                (ask-players-guess-within max-number))))
38
39 (define (tell-player-to-guess-within max-number)
40     (display "What's your guess? (max ")
41     (display max-number)
42     (display "): "))
43
44 (define (guess-valid? guess max-number)
45     (and (number? guess)
46          (integer? guess)
47          (>= guess 1)
48          (<= guess max-number)))
49
50 (define (tell-player-to-type-valid-value max-number)
51     (display "Hey! Type a number between 1 and ")
52     (display max-number)
53     (displayln "."))
54
55 (define (player-won guess)
56     (display "Your guess ")
57     (display guess)
58     (displayln " is correct! You won!"))

Some tasks

Task 1

Put all that together and check whether the program flow and purpose is easy to follow.

Task 2

We want to tell the player how many incorrect guesses they’ve made so far, and also once they’ve guessed right, tell them the total number of guesses they took. Modify the program (using IPD) to add this “feature” to the game. Before starting to implement, make sure you know all the details of this feature and how a game will look like with this feature. Remember that that’s the precondition to start IPD.

Task 3

Modify the game to also take a max-guesses argument and have the game terminate with a “you lost!” if the player took more than these many guesses.

Task 4

If you start writing this same program again, you might come up with another way to express it. Try it as many times as you want.

Notes

  1. What if we had written (play-rounds-until-player-guesses-secret secret max-number) initially instead of (play-rounds-until player-guesses-secret secret max-number)? Yes we can. However we’ll soon be required to add more detail. Splitting it right here into the two required concepts loses no clarity of expression and therefore it is worth doing it right here. There is nothing wrong with using one more definition step though and you’ll know where to split a concept without compromising readability as you practice more.

  2. The concept play-rounds-until is defined in terms of itself. What we’re saying with that is that if the player has not won, it is as though we’re back to square one and must play another round. A similar pattern also occurs with ask-players-guess-within. Familiarize yourself with this pattern and understand how it can work. In particular, there must be something within the definition that will take a “different path to the finish line” in some cases while in other cases it “tries again”. These are respectively referred to as the “termination condition” [4] and the “recursion step”, formally. (You don’t need to know these terms for now.)

  3. When we defined ask-players-guess-within, we defined the abstraction to be complete – in the sense that in any specific usage context, the expression using the abstraction is always guaranteed to stand for a valid guess. This is a desirable trait of abstractions. However it is not possible to do this in all cases and some abstractions must be limited in the contexts in which they can be applied.

  4. When we incrementally develop abstractions like this, it is important to test them as we go along. For this, as noted, you can define “dummy definitions” a.k.a. “stubs” for these abstractions that do something that just about meets their usage contract. For example,

    (define (ask-players-guess-within max-number)
       (random 1 (+ 1 max-number)))
    

    can be a useful substitute until you actually work out the user input steps.

  1. An important language feature we’re relying on here is that when defining abstractions, the ordering does not matter for abstractions in a common shared context. This lets us first write our top level abstraction and then gradually “drill down” into the details and define new words. In Racket, this is alright because the definition of an abstraction is only needed at the time at which it is used. As long as an abstraction remains unused [1], you can postpone its definition for later. Not all languages provide for this. C/C++ doesn’t, for instance. There, you’ll have to provide a “declaration” for how an abstraction is to be interpreted (without defining it) beforehand and then you can delay the full definition. Obviously, languages like Racket, Python, Pyret, Ruby, Haskell etc. are better suited for IPD for this reason than C/C++, though it can still be useful with a bit more work.