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 http://en.wikipedia.org/wiki/Literate_programming#Embedded_documentation
187 .. _`Literate Programming - Issues and Problems`:
188 http://www.cs.aau.dk/~normark/litpro/issues-and-problems.html
189 .. _dual-source: /features/index.html#dual-source
190 .. _Sphinx: http://sphinx.pocoo.org/
196 There are many LP systems available, both sophisticated
197 and lightweight. This non-comprehensive list compares them to PyLit.
203 The first published literate programming system was WEB_ by Donald Knuth. It
204 uses Pascal as programming language and TeX as document formatting language.
206 CWEB_ is a newer version of WEB for the C programming language. Other
207 implementations of the concept are the "multi-lingual" noweb_ and
208 FunnelWeb_. All of them use TeX as document formatting language.
210 Some of the problems with WEB-like systems are:
212 * The steep learning curve for mastering TeX, special WEB commands and
213 the actual programming language in parallel discourages potential literate
216 * While a printout has the professional look of a TeX-typeset essay, the
217 programmer will spend most time viewing and editing the source. A WEB
218 source in a text editor is not "suited for reading by human beings":
220 In Knuth's work, beautiful literate programs were woven from ugly mixes of
221 markups in combined program and documentation files. The literate program
222 was directly targeted towards paper and books. Of obvious reasons,
223 programmers do not very often print their programs on paper. (And as
224 argued above, few of us intend to publish our programs in a book). Within
225 hours, such a program print out is no longer up-to-date. And furthermore,
226 being based on a paper representation, we cannot use the computers dynamic
227 potentials, such as outlining and searching.
229 -- Kurt Nørmark: `Literate Programming - Issues and Problems`_
231 How does PyLit differ from the web-like LP frameworks
232 (For a full discussion of PyLit's concepts see the Features_ page.):
234 * PyLit is not a complete `LP system`_ but a simple tool.
235 It does not support re-arranging of named code chunks.
237 * PyLit uses `reStructuredText`_ as document formatting language.
238 The literate program source is legible in any text editor.
240 Even the code source is "in a fashion suited for reading by human beings",
241 in opposition to source code produced by a full grown web system:
243 Just consider the output of ``tangle`` to be object code: You can run
244 it, but you don't want to look at it.
246 -- http://www.perl.com/pub/a/tchrist/litprog.html
248 * PyLit is a *bidirectional* text <-> code converter.
250 + You can start a literate program from a "normal" code source by
251 converting to a text source and adding the documentation parts.
252 + You can edit code in its "native" mode in any decent text editor or IDE.
253 + You can debug and fix code with the standard tools using the code source
254 Line numbers are the same in text and code format. Once ready, forward
255 your changes to the text version with a code->text conversion.
257 - PyLit cannot support export to several code files from one text source.
260 .. _Docutils: http://docutils.sourceforge.net/
261 .. _Features: /features/index.html
263 .. _WEB: http://www.literateprogramming.com/knuthweb.pdf
264 .. _CWEB: http://www-cs-faculty.stanford.edu/~knuth/cweb.html
265 .. _noweb: http://www.cs.tufts.edu/~nr/noweb/
266 .. _FunnelWeb: http://www.ross.net/funnelweb/
273 SourceBrowser_ is a complex tool generating a hyper-linked representation of
274 complex software projects.
276 SourceBrowser is a documentation meta-programming tool that generates a
277 Wiki representation of a source tree.
279 It provides a *Source Annotation Language* for writing a documentation
280 meta-program and a tool for generating a static Wiki for the host program.
282 -- SourceBrowser_ home page
284 .. _SourceBrowser: http://web.media.mit.edu/~vyzo/srcb/doc/index.html
290 Interscript_ is a complex framework that uses Python both for its
291 implementation and to supply scripting services to client sourceware.
293 Interscript is different, because the fundamental paradigm is extended so
294 that there are three kinds of LP sections in file:
297 2. Documentation sections
298 3. Scripting sections
300 where the scripting sections [...] control and possibly generate both
301 documentation and code.
303 -- Interscript_ homepage
305 .. _Interscript: http://interscript.sourceforge.net/
310 Ly_, the "lyterate programming thingy." is an engine for Literate Programming.
311 The design considerations look very much like the ones for PyLit:
313 So why would anybody use Ly instead of the established Literate
314 Programming tools like WEB or noweb?
316 * Elegance. I do not consider the source code of WEB or noweb to be
317 particularly nice. I especially wanted a syntax that allows me to easily
318 write a short paragraph, then two or three lines of code, then another
319 short paragraph. Ly accomplishes this by distinguishing code from text
321 * HTML. WEB and CWEB are targeted at outputting TeX. This is a valid
322 choice, but not mine, as I want to create documents with WWW links, and
323 as I want to re-create documents each time I change something-- both of
324 these features don't go well together with a format suited to printout,
325 like TeX is. (Of course you can create HTML from LaTeX, but if my output
326 format is HTML, I want to be able to write HTML directly.)
327 * Python. I originally created Ly for a Python-based project, and
328 therefore I wanted it to run everywhere where Python runs. WEB and noweb
329 are mostly targeted at UNIX-like platforms.
333 However, Ly introduces its own language with
335 * a syntax for code chunks,
336 * some special ways for entering HTML tags ("currently undocumented").
338 whereas PyLit relies on the established `reStructuredText`_ for
339 documentation markup and linking.
341 .. _Ly: http://lyterate.sourceforge.net/intro.html
342 .. _reStructuredText: http://docutils.sourceforge.net/rst.html
348 XMLTangle_ is an generic tangle program written in Python. It differs from
349 PyLit mainly by its choice of XML as documenting language (making the source
350 document harder to read for a human being).
352 .. _XMLTangle: http://literatexml.sourceforge.net/
355 Lightweight literate programming
356 --------------------------------
358 `Lightweight literate programming`_ is one more example of a Python based
359 LP framework. One XML source is processed to either text or
360 code. The XML source itself is rather hard to read for human beings.
362 .. _`Lightweight literate programming`:
363 http://infohost.nmt.edu/~shipman/soft/litprog/
369 Pyreport_ is quite similar to PyLit in its focus on Python and the use of
370 reStructuredText. It is a combination of `documentation generator`__
371 (processing embedded documentation) and report tool (processing Python
374 __ http://en.wikipedia.org/wiki/Documentation_generator
376 pyreport is a program that runs a python script and captures its output,
377 compiling it to a pretty report in a PDF or an HTML file. It can display
378 the output embedded in the code that produced it and can process special
379 comments (literate comments) according to markup languages (rst or LaTeX)
380 to compile a very readable document.
382 This allows for extensive literate programming in python, for generating
383 reports out of calculations written in python, and for making nice
386 However, it is not a tool to "write documents that contain programs".
389 .. _pyreport: http://gael-varoquaux.info/computers/pyreport/
394 .. [contemporary-literate-programming]
395 Vreda Pieterse, Derrick G. Kourie, and Andrew Boake:
396 `A case for contemporary literate programming`,
397 in SAICSIT, Vol. 75, Pages: 2-9,
398 Stellenbosch, Western Cape, South Africa: 2004,
399 available at: http://portal.acm.org/citation.cfm?id=1035054