3 :Copyright: 2006 Guenter Milde.
4 Released under the terms of the GNU General Public License
10 The Philosophy of Literate Programming
11 ======================================
13 The philosophy of *literate programming* (LP) and the term itself were the
14 creation of Donald Knuth, the author of :title-reference:`The Art of
15 Computer Programming`, the TeX typesetting system, and other works of the
18 I believe that the time is ripe for significantly better documentation of
19 programs, and that we can best achieve this by considering programs to be
20 works of literature. Hence, my title: "Literate Programming."
22 Let us change our traditional attitude to the construction of programs:
23 Instead of imagining that our main task is to instruct a computer what to
24 do, let us concentrate rather on explaining to humans what we want the
27 -- Donald E. Knuth, `Literate Programming`_, 1984
31 The `Literate Programming FAQ`_ gives the following definition:
33 *Literate programming* is the combination of documentation and source
34 together in a fashion suited for reading by human beings.
36 and the `Wikipedia`_ says:
38 *Literate programming* is a philosophy of computer programming based on
39 the premise that a computer program should be written with human
40 readability as a primary goal, similarly to a work of literature.
42 According to this philosophy, programmers should aim for a "literate"
43 style in their programming just as writers aim for an intelligible and
44 articulate style in their writing. This philosophy contrasts with the
45 mainstream view that the programmer's primary or sole objective is to
46 create source code and that documentation should only be a secondary
49 John Max Skaller put the philosophy of literate programming in one sentence
51 The idea is that you do not document programs (after the fact), but
52 write documents that *contain* the programs.
54 -- John Max Skaller in a `Charming Python interview`_.
57 .. _`Literate Programming FAQ`:
58 http://www.literateprogramming.com/lpfaq.pdf
59 .. _Literate Programming: WEB_
60 .. _Charming Python interview:
61 http://www.ibm.com/developerworks/library/l-pyth7.html
64 LP Technique and Systems
65 ==========================================
67 Donald Knuth not only conceived the *philosophy* of literate programming,
68 he also wrote the first LP *system* `WEB`_ that
69 implements the *technique* of literate programming:
71 .. _`technique of literate programming`:
73 WEB itself is chiefly a combination of two other languages:
75 (1) a document formatting language and
76 (2) a programming language.
78 My prototype WEB system uses TEX as the document formatting language
79 and PASCAL as the programming language, but the same principles would
80 apply equally well if other languages were substituted. [...]. The main
81 point is that WEB is inherently bilingual, and that such a combination
82 of languages proves to be much more powerful than either single
83 language by itself. WEB does not make the other languages obsolete; on
84 the contrary, it enhances them.
86 -- Donald E. Knuth, `Literate Programming`_, 1984
88 .. _`literate programming system`:
91 There exists a famous *statement of requirements* for a LP
94 .. _statement of requirements:
96 *Literate programming systems* have the following properties:
98 1. Code and extended, detailed comments are intermingled.
100 2. The code sections can be written in whatever order is best for people
101 to understand, and are re-ordered automatically when the computer needs
104 3. The program and its documentation can be handsomely typeset into a
105 single article that explains the program and how it works. Indices and
106 cross-references are generated automatically.
108 .. POD only does task 1, but the other tasks are much more important.
110 -- Mark-Jason Dominus `POD is not Literate Programming`_
113 The article [contemporary-literate-programming]_ provides a survey on
114 different implementations of the literate programming technique.
117 LP with reStructuredText
118 ==========================================
120 PyLit fails the `statement of requirements`_ for a LP
121 system, as it does not provide for code re-ordering, automatic indices and
124 According to the Wikipedia, PyLit qualifies as *semi-literate* (while POD is
125 classified as `embedded documentation`_):
127 It is the fact that documentation can be written freely whereas code must
128 be marked in a special way that makes the difference between semi-literate
129 programming and excessive documenting, where the documentation is embedded
130 into the code as comments.
132 -- `Wikipedia entry on literate programming`_
134 However, it is argued that PyLit is an *educated choice* as tool for the
135 `technique of literate programming`_:
137 * PyLit is inherently bilingual. The document containing the program is
140 (1) a document formatting language (`reStructuredText`_) and
141 (2) a programming language.
143 * Program and documentation can be typeset in a single article as well as
144 converted to a hyperlinked HTML document representing the program's
145 "web-like" structure. For indexing and cross-references, PyLit relies
146 on the rich features of the `reStructuredText`_ document formatting
147 language. Auto-generating of cross references and indices is
148 possible if the reStructuredText sources are converted with Sphinx_
150 Dispensing with code re-ordering enables the `dual-source`_ concept. Code
151 re-ordering is considered less important when LP is not
152 seen as an *alternative* to structured programming but a technique
153 *orthogonal* to structured, object oriented or functional programming
156 The computer science and programming pioneers, like Wirth and Knuth,
157 used another programming style than we recommend today. One notable
158 difference is the use of procedure abstractions. When Wirth and Knuth
159 wrote their influential books and programming, procedure calls were used
160 with care, partly because they were relatively expensive. Consequently,
161 they needed an extra level of structuring within a program or procedure.
162 'Programming by stepwise refinement' as well as 'literate programming'
163 can be seen as techniques and representations to remedy the problem. The
164 use of many small (procedure) abstractions goes in another direction. If
165 you have attempted to use 'literate programming' on a program (maybe an
166 object-oriented program) with lots of small abstractions, you will
167 probably have realized that there is a misfit between, on the one hand,
168 being forced to name literal program fragments (scraps) and on the
169 other, named program abstractions. We do not want to leave the
170 impression that this is a major conceptual problem, but it is the
171 experience of the author that we need a variation of literate
172 programming, which is well-suited to programs with many, small (named)
175 -- Kurt Nørmark: `Literate Programming - Issues and Problems`_
177 Ordering code for optimal presentation to humans is still a problem in
178 many "traditional" programming languages. Pythons dynamic name resolution
179 allows a great deal of flexibility in the order of code.
181 .. _POD is not Literate Programming:
182 http://www.perl.com/pub/a/tchrist/litprog.html
183 .. _Wikipedia entry on literate programming:
184 .. _Wikipedia: http://en.wikipedia.org/wiki/Literate_programming
185 .. _embedded documentation:
186 https://en.wikipedia.org/wiki/Literate_programming
187 #Contrast_with_documentation_generation
188 .. _`Literate Programming - Issues and Problems`:
189 http://www.cs.aau.dk/~normark/litpro/issues-and-problems.html
190 .. _dual-source: /features/index.html#dual-source
191 .. _Sphinx: https://www.sphinx-doc.org/
197 There are many LP systems available, both sophisticated
198 and lightweight. This non-comprehensive list compares them to PyLit.
204 The first published literate programming system was WEB_ by Donald Knuth. It
205 uses Pascal as programming language and TeX as document formatting language.
207 CWEB_ is a newer version of WEB for the C programming language. Other
208 implementations of the concept are the "multi-lingual" noweb_ and
209 FunnelWeb_. All of them use TeX as document formatting language.
211 Some of the problems with WEB-like systems are:
213 * The steep learning curve for mastering TeX, special WEB commands and
214 the actual programming language in parallel discourages potential literate
217 * While a printout has the professional look of a TeX-typeset essay, the
218 programmer will spend most time viewing and editing the source. A WEB
219 source in a text editor is not "suited for reading by human beings":
221 In Knuth's work, beautiful literate programs were woven from ugly mixes of
222 markups in combined program and documentation files. The literate program
223 was directly targeted towards paper and books. Of obvious reasons,
224 programmers do not very often print their programs on paper. (And as
225 argued above, few of us intend to publish our programs in a book). Within
226 hours, such a program print out is no longer up-to-date. And furthermore,
227 being based on a paper representation, we cannot use the computers dynamic
228 potentials, such as outlining and searching.
230 -- Kurt Nørmark: `Literate Programming - Issues and Problems`_
232 How does PyLit differ from the web-like LP frameworks
233 (For a full discussion of PyLit's concepts see the Features_ page.):
235 * PyLit is not a complete `LP system`_ but a simple tool.
236 It does not support re-arranging of named code chunks.
238 * PyLit uses `reStructuredText`_ as document formatting language.
239 The literate program source is legible in any text editor.
241 Even the code source is "in a fashion suited for reading by human beings",
242 in opposition to source code produced by a full grown web system:
244 Just consider the output of ``tangle`` to be object code: You can run
245 it, but you don't want to look at it.
247 -- http://www.perl.com/pub/a/tchrist/litprog.html
249 * PyLit is a *bidirectional* text <-> code converter.
251 + You can start a literate program from a "normal" code source by
252 converting to a text source and adding the documentation parts.
253 + You can edit code in its "native" mode in any decent text editor or IDE.
254 + You can debug and fix code with the standard tools using the code source
255 Line numbers are the same in text and code format. Once ready, forward
256 your changes to the text version with a code->text conversion.
258 - PyLit cannot support export to several code files from one text source.
261 .. _Docutils: https://docutils.sourceforge.io/
262 .. _Features: /features/index.html
264 .. _WEB: http://www.literateprogramming.com/knuthweb.pdf
265 .. _CWEB: https://tug.ctan.org/web/cweb/cwebman.pdf
266 .. _noweb: https://www.cs.tufts.edu/~nr/noweb/
267 .. _FunnelWeb: http://www.ross.net/funnelweb/
274 Interscript_ is a complex framework that uses Python both for its
275 implementation and to supply scripting services to client sourceware.
277 Interscript is different, because the fundamental paradigm is extended so
278 that there are three kinds of LP sections in file:
281 2. Documentation sections
282 3. Scripting sections
284 where the scripting sections [...] control and possibly generate both
285 documentation and code.
287 -- Interscript_ homepage
289 .. _Interscript: http://interscript.sourceforge.net/
294 Ly_, the "lyterate programming thingy." is an engine for Literate Programming.
295 The design considerations look very much like the ones for PyLit:
297 So why would anybody use Ly instead of the established Literate
298 Programming tools like WEB or noweb?
300 * Elegance. I do not consider the source code of WEB or noweb to be
301 particularly nice. I especially wanted a syntax that allows me to easily
302 write a short paragraph, then two or three lines of code, then another
303 short paragraph. Ly accomplishes this by distinguishing code from text
305 * HTML. WEB and CWEB are targeted at outputting TeX. This is a valid
306 choice, but not mine, as I want to create documents with WWW links, and
307 as I want to re-create documents each time I change something-- both of
308 these features don't go well together with a format suited to printout,
309 like TeX is. (Of course you can create HTML from LaTeX, but if my output
310 format is HTML, I want to be able to write HTML directly.)
311 * Python. I originally created Ly for a Python-based project, and
312 therefore I wanted it to run everywhere where Python runs. WEB and noweb
313 are mostly targeted at UNIX-like platforms.
317 However, Ly introduces its own language with
319 * a syntax for code chunks,
320 * some special ways for entering HTML tags ("currently undocumented").
322 whereas PyLit relies on the established `reStructuredText`_ for
323 documentation markup and linking.
325 .. _Ly: http://lyterate.sourceforge.net/intro.html
326 .. _reStructuredText: https://docutils.sourceforge.io/rst.html
332 XMLTangle_ is an generic tangle program written in Python. It differs from
333 PyLit mainly by its choice of XML as documenting language (making the source
334 document harder to read for a human being).
336 .. _XMLTangle: http://literatexml.sourceforge.net/
342 Pyreport_ is quite similar to PyLit in its focus on Python and the use of
343 reStructuredText. It is a combination of `documentation generator`__
344 (processing embedded documentation) and report tool (processing Python
347 __ http://en.wikipedia.org/wiki/Documentation_generator
349 pyreport is a program that runs a python script and captures its output,
350 compiling it to a pretty report in a PDF or an HTML file. It can display
351 the output embedded in the code that produced it and can process special
352 comments (literate comments) according to markup languages (rst or LaTeX)
353 to compile a very readable document.
355 This allows for extensive literate programming in python, for generating
356 reports out of calculations written in python, and for making nice
359 However, it is not a tool to "write documents that contain programs".
362 http://gael-varoquaux.info/programming/
363 pyreport-literate-programming-in-python.html
369 .. [contemporary-literate-programming]
370 Vreda Pieterse, Derrick G. Kourie, and Andrew Boake:
371 `A case for contemporary literate programming`,
372 in SAICSIT, Vol. 75, Pages: 2-9,
373 Stellenbosch, Western Cape, South Africa: 2004,
374 available at: http://portal.acm.org/citation.cfm?id=1035054