On Writing Itself

Now that appropriate auxiliary variables have been introduced, the process of outputting table p almost writes itself.

What does it mean for code to ‘write itself’. On a commonsensical level, it is obvious what Knuth means. When appropriate variables have been defined, then it is plain how they should be combined to express the programmer’s intent. Knuth effectively makes a prediction: any programmer worth their salt could see how the next part of the code should be written. As one of our members observed, however, lurking beneath this commonsensical supposition is a host of metaphysical presuppositions. What makes the code obvious? Why does the writer feel that the code has become autonomous, and is running off in its own direction?

There are many different agents in the ‘assemblage’ of the WEB system as Knuth writes. The PASCAL language, with its grammar and vocabulary. The WEB system itself, with its various (rather quirky) affordances. The presumed reader of the Computer Journal for whom is code is really being written. The logical force of mathematics and the algorithm. The beauty of the program, which is put there by the programmer but then turns back on its creator to bewitch their senses. When the the ‘code writes itself’, it is really some combination of these forces acting together to educe the code.

What code? This code:

〈 Print table p 8 〉≡
    begin page_number ← 1; page_offset ← 1;
    while page_offset ≤ m do
        begin 〈 Output a page of answers 9 〉;
        page_number ← page_number + 1;
        page_offset ← page_offset + cc * rr;
        end;
    end;

The code that has ‘written itself’ has offloaded its job to another part of the program! The actual code that ‘writes itself’ is essentially just a piece of housekeeping that keeps track of which page we’re up to in the printing loop. We discussed for some time Knuth’s decision to leave the actual outputting of a ‘page of numbers’ to another part of the program. No doubt his judgement is good. He strives for a certain kind of modularity, where each module is highly concrete and can be summed up in a verb phrase (“Output a page of answers”).

Reading the code again, it does have a feature I find disquieting. Presumably 〈 Output a page of answers 9 〉 uses some of the variables we have already encountered, including page_offset, but the way this code is written, you have no idea which variables will be used or altered by 〈 Output a page of answers 9 〉. Is this a deficiency of PASCAL, or of Knuth’s code, or of WEB itself? Pascal supports subroutines with named parameters (at least, it does so today). Why not write a subroutine here that explicitly shows in its signature which variables will be usd to 〈 Output a page of answers 9 〉?

This raises the additional question of ‘readability’. This is one of the major aesthetic criteria of modern programming, but what it means is debatable. Knuth argues that his code above is readable because it clearly states the programmer’s intention, and states it in (roughly) the order that the programmer thought up their intention. But from another perspective, the code above is not especially ‘readable’ because it doesn’t clearly indicate the flow of data through different parts of the program.

Knuth’s judgments about what to include, exclude, abstract or concretise are in every case fascinating. As another member noted, he seems to strive for ‘modularity’ in all aspects of the WEB code, even the prose components, which are carefully fenced off into discrete modules by square brackets and section numbers.

We resume next session at section 9 of his program for printing the first 1000 prime numbers.

Explanation and Motivation

We continue our reading of Literate Programming, by Donald Knuth.

In this session, we completed up to the end of section 5. Knuth has by now given the ‘plan’ of his program to print the first m prime numbers, and has commenced his explanation of the program’s ‘output phase’.

Our discussion focussed on the link between ‘motivation’ and explanation in Knuth’s vision for WEB. Knuth essentially argues that the prose of a WEB program ought to ‘motivate’ the programming decisions expressed in the code. His model for this process is the essay, which he perceives as a literary form in which the writer records their own thought-process. The source of a WEB program thus ought to reflect the programmer’s own creative process.

This is a powerful model of composition. From a literary studies perspective, it recalls the approach of the late Helen Vendler, who famously interpreted poems as records of their own creative process.

But many in the group objected to aspects of Knuth’s argument.

In the first case, does an essay ever accurately reflect the writer’s thought-process? Is there not an element of revision, polishing, and rhetoric in any essay? This doesn’t invalidate Knuth’s point, but thus far he does not seem to recognise the fictionality of the creative process. His WEB code is a story about how he wrote the program. What is the relationship between this story and the reality? Is there any reality that is not the story?

In the second case, his particular pattern of explanation didn’t work for everyone. In Knuth’s view, each part of the code should be preceded by the prose that ‘motivates’ the decision. Some in the group felt that it would be better to present the code, and follow it with a commentary, at least in some cases. But this would undermine the fiction that that code is emerging from a reasoning process that is reflected in the source.

Of course, we in the group bring our own assumptions and biases to the discussion. Knuth’s idea of the ‘essay’ may be perfectly valid, although it conflicts with ideas about literary composition that are popular in English departments. In particular, we dicussed the familiar nostrum of the ‘intentional fallcy’. Knuth makes little distinction between the programmer’s intention (or motivation) and the ‘meaning’ of the program. In Literature departments, we habitually distinguish the intention and the meaning: what the writer intended to say has really nothing to do with what they actually manage to say. As one member of the group observed, this notion of the ‘intentional fallacy’ reflects certain limitations in literary scholarship, especially the dominance of the nineteenth-century novel as a model for literature in general. Perhaps Knuth is right to tightly couple meaning and intention in the context of computer programming. The ‘author’ of a program is typically many people in conversation, unlike the distant ‘author’ of a triple-decker novel.

Against against intention: Amerikkkka, by Peli Grietzer.

An entire book written in WEB (or rather CWEB): MMIXware: A RISC Computer for the Third Millenium

The useless web?

We continue our reading of Literate Programming, by Donald Knuth.

We completed up to section 2 of Knuth’s web program to print the first n prime numbers. Having encountered the program definition, we have in some sense read the entire thing.

We did discuss whether modern code editors render Knuth’s entire notion of the ‘web of code’ otiose. We in particular considered EMACS, a code editor that was becoming popular at the time the paper was written.

EMACS stands for “editor macros,” and it basically allows a coder to write little programs that let them interact with their source code. The particular thing I was thinking of is the way you can use EMACS or similar programs to navigate your codebase. Say you come across a function called “check_if_prime” and you don’t know how it works. In EMACS you should be able to hit a keyboard shortcut, and be taken to the part of the source code where “check_if_prime” is defined, so you can see what it does. You should then be able to hit another keyboard shortcut to go back to where you were.

If you have a good code editor, is all the elaborate cross-referencing of a WEB program required?

The Quirk

We begin our reading of Literate Programming, by Donald Knuth.

For those who had to skip this meeting, we completed up to paragraph 2 on page 98. We will recommence in a fortnight at the line “Document formatting languages are newcomers to …”.

In answer to a query: Wikipedia informs me that LaTeX is a set of macros for the underlying TeX language. A macro is a procedure that modifies the text of a file. Thus the LaTeX program allows you to write directives into your document (e.g. \documentclass), which will then be translated by the LaTeX macros into the more basic TeX code. I was wrong to say that Knuth invented LaTeX – he invented TeX. LaTeX, with its increased functionality, was invented by Leslie Lamport. I can only assume that the “La” is for “Lamport”!

The ‘illiterate’ pascal files we saw at the start of the session can be seen here.