Literate programming: Knuth is doing it wrong (2014)
I&#x27;ve read a fair number of Knuth&#x27;s literate programs, and I&#x27;ve read this post a few times over the years, and my opinion is mixed.<p>The first point in the post is that many (non-Knuth) LP systems provide typesetting, but allowing arbitrary code re-ordering (as in Knuth&#x27;s WEB&#x2F;CWEB) is actually the more important feature. This is a good point.<p>But next it faults Knuth for putting #include statements at the top of the program, instead of using his system&#x27;s reordering feature to have an &quot;⟨includes⟩&quot; section that is later filled in when each #include is needed (the way he does for &quot;⟨Type definitions⟩&quot; and &quot;⟨Global variables⟩&quot; in those examples). This slightly misunderstands the purpose of literate programming, I think.<p>As far as Knuth is concerned, the main idea in literate programming is an attitude, an orientation: treating programming as <i>writing</i>, for a human reader. (This particularly makes sense for him as he is primarily, in a very deep way, a writer.) Now, writing is always (best) done with a specific reader in mind: you assume the reader has a certain background&#x2F;prerequisites: some things that don&#x27;t need explaining, and some things that do. Depending on the reader you&#x27;re targeting (e.g., yourself a few years from now), and how polished you&#x27;re trying to make your presentation, you may well choose to take it for granted and not bother explaining that a C program will have some obvious #includes at the top. Take it for granted, as understood between friends.<p>From this point of view, as long as you&#x27;re writing for a human reader, it is literate programming whether you&#x27;re at the extreme of explaining every part of every line of code, to a reader who does not even know the language, or at the other extreme of &quot;here&#x27;s my program: ⟨the entire code of the program⟩&quot;: it&#x27;s only a matter of <i>degree</i>, how much you want to explain. [This is actually one of the misconceptions about LP: it doesn&#x27;t necessarily require you to write lots of comments, or to explain everything verbosely; the main thing is to treat the activity as writing, and explain to whatever degree it makes sense given your intended reader. In Knuth&#x27;s own TeX program <a href="http:&#x2F;&#x2F;mirrors.ctan.org&#x2F;info&#x2F;knuth-pdf&#x2F;tex&#x2F;tex.pdf" rel="nofollow">http:&#x2F;&#x2F;mirrors.ctan.org&#x2F;info&#x2F;knuth-pdf&#x2F;tex&#x2F;tex.pdf</a> , the one he came up with literate programming for, there are sections where he explains things very thoroughly but see also section §315 where, after explaining, he says <i>&quot;This is easier to program than to explain&quot;</i>, and §69 where he says <i>&quot;Readers who like puzzles might enjoy trying to figure out how this tricky code works; therefore no explanation will be given&quot;</i> :-)]<p>A few other criticisms in the post are understandable given unfamiliarity with some further context about Knuth&#x27;s work and these programs:<p>• Many of the examples being criticized are from programs that Knuth wrote for himself, as he mentions at the top of <a href="https:&#x2F;&#x2F;cs.stanford.edu&#x2F;~knuth&#x2F;programs.html" rel="nofollow">https:&#x2F;&#x2F;cs.stanford.edu&#x2F;~knuth&#x2F;programs.html</a> — obviously the standard of polish and degree of explaining will be different when something is written up for public presentation rather than for oneself. (As I said, these examples also show that to write and enjoy LP you don&#x27;t need to overdo it, feel free to explain only as much as you want.)<p>• An example is criticized for the fact that &quot;GraphBase data structures&quot; are not described in-line in the program, but again, as the webpage says, this is a program that uses &quot;the conventions and library of The Stanford GraphBase&quot; — Knuth has published an entire book about these data structures, so it isn&#x27;t necessary (or realistic) to re-describe them inline. In fact, many of Knuth&#x27;s programs can be criticized for being monolithic rather than modular, but the Stanford GraphBase is a rare case of Knuth actually factoring out a library for use in many programs.<p>• There&#x27;s a criticism for &quot;a steep jump across several levels of abstraction&quot;, but, contrary to common opinion, Knuth has repeatedly mentioned this as a good thing:<p>&gt; <i>&quot;I have felt for a long time that a talent for programming consists largely of the ability to switch readily from microscopic to macroscopic views of things, i.e., to change levels of abstraction fluently.&quot;</i> — 1974, <i>Structured Programming with Go To Statements</i> (<a href="http:&#x2F;&#x2F;www.kohala.com&#x2F;start&#x2F;papers.others&#x2F;knuth.dec74.html" rel="nofollow">http:&#x2F;&#x2F;www.kohala.com&#x2F;start&#x2F;papers.others&#x2F;knuth.dec74.html</a>, <a href="https:&#x2F;&#x2F;pic.plover.com&#x2F;knuth-GOTO.pdf#page=32" rel="nofollow">https:&#x2F;&#x2F;pic.plover.com&#x2F;knuth-GOTO.pdf#page=32</a> )<p>&gt; <i>&quot;I believe the thing that marks a computer scientist most is an ability to jump across levels of abstraction: We see the small and the large and several things in between as a continuum, and we don&#x27;t even notice that we&#x27;re jumping levels.</i> — 2013 (<a href="https:&#x2F;&#x2F;programmingphilosophy.org&#x2F;posts&#x2F;jumping-across-levels-of-abstraction" rel="nofollow">https:&#x2F;&#x2F;programmingphilosophy.org&#x2F;posts&#x2F;jumping-across-level...</a>)<p>&gt; <i>&quot;after decades of observation I’ve come to believe that one particular talent stands out among the world-class programmers I’ve known— namely, an ability to move effortlessly between different levels of abstraction […]  A programmer must deal with high-level concepts related to a problem area, with low-level concepts related to basic steps of computation, and with numerous levels in between. We represent reality by creating structures that are composed of progressively simpler and simpler parts. We don’t only need to understand how those parts fit together; we also need to be able somehow to envision the whole show — to see everything in the large while seeing it simultaneously in the small and in the middle. Without blinking an eye, we need to understand why a major goal can be accomplished if we begin by increasing the contents of a lowly computer register by 1. The best way to enhance our level-jumping skills is to exercise them frequently.&quot;</i> — Foreword to <i>The MMIX Supplement</i>, published 2015.<p>• The second major criticism in the post is that the woven program (the readable output of the LP system: a typeset PDF file or book or whatever) is &quot;dead&quot;. This is reasonable given the majority of readers, including myself in the past. But in fact, the intention is the very opposite of <i>&quot;we can&#x27;t make changes to it while we work on the codebase. Everybody reads a pdf about a program at most once, when they first encounter it.&quot;</i> For Knuth, the printed&#x2F;woven version <i>is</i> the program, the one he thinks of as canonical and the one he uses whenever debugging or making changes to the program (with pencil on paper, I think). It serves him well: here he is in 2021:<p>&gt; <i>While I was preparing this round of updates, I was overjoyed to see how well the philosophy of literate programming has facilitated everything. This multifaceted program [TeX] was written 40 years ago, yet I could still get back into TeX’s darkest corners without trouble, just by rereading [B] and using its index and mini-indexes!</i> — <a href="https:&#x2F;&#x2F;tug.org&#x2F;tugboat&#x2F;tb42-1&#x2F;tb130knuth-tuneup21.pdf" rel="nofollow">https:&#x2F;&#x2F;tug.org&#x2F;tugboat&#x2F;tb42-1&#x2F;tb130knuth-tuneup21.pdf</a><p>And unlike the common criticism (not present in this post, to be fair) of the author&#x27;s order not being what a reader may want, in fact a book is not meant to be read linearly. (&quot;Books are not scrolls. […] Books are random access&quot; — Manuel Blum, <a href="https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~mblum&#x2F;research&#x2F;pdf&#x2F;grad.html" rel="nofollow">https:&#x2F;&#x2F;www.cs.cmu.edu&#x2F;~mblum&#x2F;research&#x2F;pdf&#x2F;grad.html</a>) Knuth also recommends reading in whatever order makes sense; he thinks that his indexes are enough to enable this, though readers like us may disagree.<p>His idea of reading (a book, or a program) <i>does</i> include &quot;poke at it and run what-if experiments&quot;, just that he does it in his head. See Peter Seibel&#x27;s post at <a href="https:&#x2F;&#x2F;gigamonkeys.com&#x2F;code-reading&#x2F;" rel="nofollow">https:&#x2F;&#x2F;gigamonkeys.com&#x2F;code-reading&#x2F;</a> where he describes how Knuth was one of the only two interviewees in his <i>Coders at Work</i> who do a lot of reading other people&#x27;s code, and he also includes some detail of <i>how</i> Knuth read some program. (See also this great post by Dylan Lederle-Ensign on Knuth&#x27;s reading practice: <a href="http:&#x2F;&#x2F;www.dlederle.com&#x2F;2018&#x2F;11&#x2F;25&#x2F;reading-programs.html" rel="nofollow">http:&#x2F;&#x2F;www.dlederle.com&#x2F;2018&#x2F;11&#x2F;25&#x2F;reading-programs.html</a> In fact, one of these programs Knuth &quot;read&quot; this way while spending a long time in hospital with a broken arm, he says somewhere IIRC.) So though I agree with the ideal of &quot;viewing live, modifiable, interactive code&quot;, the intention of Knuth-style woven output is far from &quot;passive reading&quot; as the post says.<p>Nevertheless, given most readers today (probably this was true in 1980s too, to be fair), the post is probably right that given a typeset version people <i>will</i> read it passively, so overall this criticism is probably worth making, at least as a warning.<p>There are lots of other criticisms that can be made about how Knuth does literate programming (here&#x27;s a discussion I found just now: <a href="https:&#x2F;&#x2F;wiki.c2.com&#x2F;?LongFunctionsByDonaldKnuth" rel="nofollow">https:&#x2F;&#x2F;wiki.c2.com&#x2F;?LongFunctionsByDonaldKnuth</a>), and I&#x27;ve made quite a few myself, but this comment is already too long (and too late). For the &quot;ADVENT[URE]&quot; program in particular, see this discussion from about a year ago: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=25597842" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=25597842</a>