1 #LyX 1.6.4 created this file. For more info see http://www.lyx.org/
7 % This file was converted from HTML to LaTeX with
8 % Tomasz Wegrzanowski's <maniek@beer.com> gnuhtml2latex program
11 \lstset{numbers=left, stepnumber=1, numbersep=5pt, breaklines=false,
12 basicstyle=\footnotesize\ttfamily,
13 %keywordstyle=\color{darkgreen},
14 numberstyle=\tiny,language=C,columns=fullflexible,
17 \use_default_options false
25 \font_typewriter default
26 \font_default_family default
33 \paperfontsize default
42 \paperorientation portrait
45 \paragraph_separation skip
47 \quotes_language english
50 \paperpagestyle default
51 \tracking_changes false
59 \begin_layout Section*
63 \begin_layout Subsubsection*
64 From Wikipedia, the free encyclopedia
67 \begin_layout Standard
72 is an approach to programming introduced by Donald Knuth as an alternative
73 to the structured programming paradigm of the 1970s.
77 \begin_layout Plain Layout
80 "Literate Programming" (PDF).
85 (British Computer Society)
90 doi:10.1093/comjnl/27.2.97.
91 http://www.literateprogramming.com/knuthweb.pdf.
92 Retrieved January 4, 2009.
93 \begin_inset CommandInset label
107 \begin_layout Standard
108 The literate programming paradigm, as conceived by Knuth, represents a move
109 away from writing programs in the manner and order imposed by the computer,
110 and instead enables programmers to develop programs in the order demanded
111 by the logic and flow of their thoughts.
115 \begin_layout Plain Layout
116 \begin_inset Quotes eld
119 I had the feeling that top-down and bottom-up were opposing methodologies:
120 one more suitable for program exposition and the other more suitable for
122 But after gaining experience with WEB, I have come to realize that there
123 is no need to choose once and for all between top-down and bottom-up, because
124 a program is best thought of as a web instead of a tree.
125 A hierarchical structure is present, but the most important thing about
126 a program is its structural relationships.
127 A complex piece of software consists of simple parts and simple relations
128 between those parts; the programmer's task is to state those parts and
129 those relationships, in whatever order is best for human comprehension
130 not in some rigidly determined order like top-down or bottom-up.
131 \begin_inset Quotes erd
137 Knuth, Literate Programming
143 \begin_layout Plain Layout
157 Literate programs are written as an uninterrupted exposition of logic in
158 an ordinary human language, much like the text of an essay, in which macros
159 which hide abstractions and traditional source code are included.
160 Literate programming tools are used to obtain two representations from
161 a literate source file: one suitable for further compilation or execution
163 \begin_inset Quotes eld
167 \begin_inset Quotes erd
170 code, and another for viewing as formatted documentation, which is said
172 \begin_inset Quotes eld
176 \begin_inset Quotes erd
179 from the literate source.
180 (If one remembers that the first version of the tool was called
184 , the amusing literary reference hidden by Knuth in these names becomes
186 \begin_inset Quotes eld
189 Oh, what a tangled web we weave when first we practise to deceive
190 \begin_inset Quotes erd
193 --- Sir Walter Scott, in Canto VI, Stanza 17 of
194 \begin_inset Quotes eld
198 \begin_inset Quotes erd
201 (1808) an epic poem about the Battle of Flodden Field in 1513.).
202 While the first generation of literate programming tools were computer
203 language-specific, the later ones are language-agnostic and exist above
204 the programming languages.
208 \begin_layout Subsection*
212 \begin_layout Standard
213 \begin_inset CommandInset toc
214 LatexCommand tableofcontents
221 \begin_layout Section
225 \begin_layout Standard
226 A literate program is an explanation of the program logic in a natural language,
227 such as English, interspersed with snippets of macros and traditional source
229 Macros in a literate source file are simply title-like or explanatory phrases
230 in a human language that describe human abstractions created while solving
231 the programming problem, and hiding chunks of code or lower-level macros.
232 These macros are similar to the algorithms in pseudocode typically used
233 in teaching computer science.
234 These arbitrary explanatory phrases become precise new operators, created
235 on the fly by the programmer, forming a
239 on top of the underlying programming language.
243 \begin_layout Standard
244 A preprocessor is used to substitute arbitrary hierarchies, or rather
245 \begin_inset Quotes eld
248 interconnected 'webs' of macros
249 \begin_inset Quotes erd
256 \begin_layout Plain Layout
257 \begin_inset Quotes eld
260 WEB's macros are allowed to have at most one parameter.
261 Again, I did this in the interests of simplicity, because I noticed that
262 most applications of multiple parameters could in fact be reduced to the
264 For example, suppose that you want to define something like...
265 In other words, the name of one macro can usefully be a parameter to another
267 This particular trick makes it possible to...
268 \begin_inset Quotes erd
274 Knuth, Literate Programming
280 \begin_layout Plain Layout
294 to produce the compilable source code with one command (
295 \begin_inset Quotes eld
299 \begin_inset Quotes erd
302 ), and documentation with another (
303 \begin_inset Quotes eld
307 \begin_inset Quotes erd
311 The preprocessor also provides an ability to write out the content of the
312 macros and to add to already created macros in any place in the text of
313 the literate program source file, thereby disposing of the need to keep
314 in mind the restrictions imposed by traditional programming languages or
315 to interrupt the flow of thought.
319 \begin_layout Subsection
320 Advantages of the method
323 \begin_layout Standard
328 \begin_layout Plain Layout
329 Knuth, Donald E.; Andrew Binstock (April 25, 2008).
330 "Interview with Donald Knuth".
331 http://www.informit.com/articles/article.aspx?p=1193856.
332 Retrieved January 4, 2009.
333 "Yet to me, literate programming is certainly the most important thing
334 that came out of the TeX project.
335 Not only has it enabled me to write and maintain programs faster and more
336 reliably than ever before, and been one of my greatest sources of joy since
337 the 1980s-it has actually been indispensable at times.
338 Some of my major programs, such as the MMIX meta-simulator, could not have
339 been written with any other methodology that I've ever heard of.
340 The complexity was simply too daunting for my limited brain to handle;
341 without literate programming, the whole enterprise would have flopped miserably.
343 Literate programming is what you need to rise above the ordinary level
353 \begin_layout Plain Layout
354 "Another surprising thing that I learned while using WEB was that traditional
355 programming languages had been causing me to write inferior programs, although
356 I hadn't realized what I was doing.
357 My original idea was that WEB would be merely a tool for documentation,
358 but I actually found that my WEB programs were better than the programs
359 I had been writing in other languages." ---
362 Knuth, Literate Programming
368 \begin_layout Plain Layout
382 literate programming provides for higher-quality programs, since it forces
383 programmers to explicitly state the thoughts behind the program, making
384 poorly thought-out design decisions more obvious.
385 Knuth also claims that literate programming provides a first-rate documentation
386 system, which is not an add-on, but is grown naturally in the process of
387 exposition of one's thoughts during a program creation.
391 \begin_layout Plain Layout
392 \begin_inset Quotes eld
395 Thus the WEB language allows a person to express programs in a
397 "stream of consciousness" order
400 TANGLE is able to scramble everything up into the arrangement that a PASCAL
402 This feature of WEB is perhaps its greatest asset; it makes a WEB-written
403 program much more readable than the same program written purely in PASCAL,
404 even if the latter program is well commented.
405 And the fact that there's no need to be hung up on the question of top-down
406 versus bottom-up, since a programmer can now
408 view a large program as a web, to be explored in a
410 psychologically correct order
412 is perhaps the greatest lesson
414 I have learned from my recent experiences.
415 \begin_inset Quotes erd
421 Knuth, Literate Programming
427 \begin_layout Plain Layout
441 The resulting documentation allows authors to restart their own thought
442 processes at any later time, and allows other programmers to more easily
443 understand the construction of the program.
444 This differs from traditional documentation, in which a programmer is presented
445 with source code that follows a compiler-imposed order, and must decipher
446 the thought process behind the program from the code and its associated
448 The meta-language capabilities of literate programming are also claimed
449 to facilitate thinking in general, giving a higher
450 \begin_inset Quotes eld
454 \begin_inset Quotes erd
457 of the code and increasing the number of concepts the mind can successfully
459 Applicability of the concept to programming on a large scale, that of commercia
460 l-grade programs is proven by an edition of TeX code as a literate program.
464 \begin_layout Subsection
468 \begin_layout Standard
469 Literate programming is very often misunderstood
473 \begin_layout Plain Layout
474 Dominus, Mark-Jason (March 20, 2000).
476 \begin_inset Quotes eld
479 POD is not Literate Programming
480 \begin_inset Quotes erd
489 http://www.perl.com/pub/a/tchrist/litprog.html.
490 Retrieved January 3, 2009.
491 \begin_inset CommandInset label
502 to refer only to formatted documentation produced from a common file with
503 both source code and comments, or to voluminous commentaries included with
505 This misconception has led to claims that comment-extraction tools, such
506 as the Perl Plain Old Documentation system, are
507 \begin_inset Quotes eld
510 literate programming tools
511 \begin_inset Quotes erd
515 However, because these tools do not implement the
516 \begin_inset Quotes eld
519 web of abstract concepts
520 \begin_inset Quotes erd
523 hiding behind the system of natural-language macros, or provide an ability
524 to change the order of the source code from a machine-imposed sequence
525 to one convenient to the human mind, they cannot properly be called literate
526 programming tools in the sense intended by Knuth.
530 \begin_layout Plain Layout
543 \begin_layout Plain Layout
544 \begin_inset Quotes eld
547 I chose the name WEB partly because it was one of the few three-letter words
548 of English that hadn't already been applied to computers.
549 But as time went on, I've become extremely pleased with the name, because
550 I think that a complex piece of software is, indeed, best regarded as a
551 web that has been delicately pieced together from simple materials.
552 We understand a complicated system by understanding its simple parts, and
553 by understanding the simple relations between those parts and their immediate
555 If we express a program as a web of ideas, we can emphasize its structural
556 properties in a natural and satisfying way.
557 \begin_inset Quotes erd
563 Knuth, Literate Programming
569 \begin_layout Plain Layout
586 \begin_layout Section
590 \begin_layout Standard
591 A classic example of literate programming is the literate implementation
596 word counting program.
597 Knuth presented a CWEB version of this example in Chapter 12 of his
602 The same example was later rewritten for the noweb literate programming
607 \begin_layout Plain Layout
608 Ramsey, Norman (May 13, 2008).
610 \begin_inset Quotes eld
614 \begin_inset Quotes erd
618 http://www.cs.tufts.edu/
619 \begin_inset space \space{}
622 nr/noweb/examples/wc.html.
623 Retrieved January 4, 2009.
624 \begin_inset CommandInset label
635 This example provides a good illustration of the basic elements of literate
640 \begin_layout Section
644 \begin_layout Standard
645 The following snippet of the
653 \begin_layout Plain Layout
662 shows how arbitrary descriptive phrases in a natural language are used
663 in a literate program to create macros, which act as new
664 \begin_inset Quotes eld
668 \begin_inset Quotes erd
671 in the literate programming language, and hide chunks of code or other
673 The mark-up notation consists of double angle brackets (
674 \begin_inset Quotes eld
678 \begin_inset Flex CharStyle:Code
681 \begin_layout Plain Layout
682 <<\SpecialChar \ldots{}
689 \begin_inset Quotes erd
692 ) that indicate macros, the
693 \begin_inset Quotes eld
697 \begin_inset Flex CharStyle:Code
700 \begin_layout Plain Layout
707 \begin_inset Quotes erd
710 symbol which indicates the end of the code section in a noweb file.
712 \begin_inset Quotes eld
716 \begin_inset Flex CharStyle:Code
719 \begin_layout Plain Layout
726 \begin_inset Quotes erd
729 symbol stands for the
730 \begin_inset Quotes eld
734 \begin_inset Quotes erd
737 , topmost node the literate programming tool will start expanding the web
739 Actually, writing out the expanded source code can be done from any section
741 a piece of code designated as
742 \begin_inset Quotes erd
746 \begin_inset Flex CharStyle:Code
749 \begin_layout Plain Layout
750 <<name of the chunk>>=
756 \begin_inset Quotes erd
759 , with the equal sign), so one literate program file can contain several
760 files with machine source code.
763 \begin_layout Standard
764 \begin_inset listings
768 \begin_layout Plain Layout
770 The purpose of wc is to count lines, words, and/or characters in a list
775 \begin_layout Plain Layout
777 number of lines in a file is ......../more explanations/
780 \begin_layout Plain Layout
784 \begin_layout Plain Layout
786 Here, then, is an overview of the file wc.c that is defined by the noweb
790 \begin_layout Plain Layout
795 \begin_layout Plain Layout
797 <<Header files to include>>
800 \begin_layout Plain Layout
805 \begin_layout Plain Layout
810 \begin_layout Plain Layout
815 \begin_layout Plain Layout
820 \begin_layout Plain Layout
825 \begin_layout Plain Layout
829 \begin_layout Plain Layout
831 We must include the standard I/O definitions, since we want to send formatted
835 \begin_layout Plain Layout
837 to stdout and stderr.
840 \begin_layout Plain Layout
842 <<Header files to include>>=
845 \begin_layout Plain Layout
850 \begin_layout Plain Layout
860 \begin_layout Standard
861 Note also that the unraveling of the chunks can be done in any place in
862 the literate program text file, not necessarily in the order they are sequenced
863 in the enclosing chunk, but as is demanded by the logic reflected in the
864 explanatory text that envelops the whole program.
868 \begin_layout Section
869 Program as a Web - Macros are not just section names
872 \begin_layout Standard
873 Macros are not the same as
874 \begin_inset Quotes eld
878 \begin_inset Quotes erd
881 in standard documentation.
882 Literate programming macros can hide any chunk of code behind themselves,
883 and be used inside any low-level machine language operators, often inside
884 logical operators such as
885 \begin_inset Quotes eld
889 \begin_inset Flex CharStyle:Code
892 \begin_layout Plain Layout
899 \begin_inset Quotes erd
903 \begin_inset Quotes eld
907 \begin_inset Flex CharStyle:Code
910 \begin_layout Plain Layout
917 \begin_inset Quotes erd
921 \begin_inset Quotes eld
925 \begin_inset Flex CharStyle:Code
928 \begin_layout Plain Layout
935 \begin_inset Quotes erd
939 This is illustrated by the following snippet of the
947 \begin_layout Plain Layout
959 \begin_layout Standard
960 \begin_inset listings
964 \begin_layout Plain Layout
968 \begin_layout Plain Layout
970 The present chunk, which does the counting that is wc's raison d'etre, was
974 \begin_layout Plain Layout
976 the simplest to write.
977 We look at each character and change state if it begins or ends
980 \begin_layout Plain Layout
985 \begin_layout Plain Layout
989 \begin_layout Plain Layout
994 \begin_layout Plain Layout
999 \begin_layout Plain Layout
1001 <<Fill buffer if it is empty; break at end of file>>
1004 \begin_layout Plain Layout
1009 \begin_layout Plain Layout
1011 if (c > ' ' && c < 0177) {
1014 \begin_layout Plain Layout
1016 /* visible ASCII codes */
1019 \begin_layout Plain Layout
1024 \begin_layout Plain Layout
1029 \begin_layout Plain Layout
1034 \begin_layout Plain Layout
1039 \begin_layout Plain Layout
1044 \begin_layout Plain Layout
1049 \begin_layout Plain Layout
1056 \begin_layout Plain Layout
1058 else if (c != ' ' && c != '
1063 \begin_layout Plain Layout
1068 \begin_layout Plain Layout
1070 /* c is newline, space, or tab */
1073 \begin_layout Plain Layout
1078 \begin_layout Plain Layout
1088 \begin_layout Standard
1089 In fact, macros can stand for any arbitrary chunk of code or other macros,
1090 and are thus more general than top-down or bottom-up
1091 \begin_inset Quotes eld
1095 \begin_inset Quotes erd
1098 , or than subsectioning.
1099 Knuth says that when he realized this, he began to think of a program as
1108 \begin_layout Plain Layout
1120 \begin_layout Subsection
1121 Order of human logic, not that of the compiler
1124 \begin_layout Standard
1125 In a noweb literate program besides the free order of their exposition,
1126 the chunks behind macros, once introduced with
1127 \begin_inset Quotes eld
1131 \begin_inset Flex CharStyle:Code
1134 \begin_layout Plain Layout
1135 <<\SpecialChar \ldots{}
1142 \begin_inset Quotes erd
1145 , can be grown later in any place in the file by simply writing
1146 \begin_inset Quotes eld
1150 \begin_inset Flex CharStyle:Code
1153 \begin_layout Plain Layout
1154 <<name of the chunk>>=
1160 \begin_inset Quotes erd
1163 and adding more content to it, as the following snippet illustrates.
1167 \begin_layout Plain Layout
1179 \begin_layout Standard
1180 \begin_inset listings
1184 \begin_layout Plain Layout
1186 The grand totals must be initialized to zero at the beginning of the program.
1189 \begin_layout Plain Layout
1191 If we made these variables local to main, we would have to do this initializatio
1195 \begin_layout Plain Layout
1197 explicitly; however, C globals are automatically zeroed.
1199 \begin_inset Quotes eld
1205 \begin_layout Plain Layout
1208 \begin_inset Quotes erd
1214 \begin_layout Plain Layout
1216 <<Global variables>>+=
1219 \begin_layout Plain Layout
1221 long totwordcount, totlinecount,
1224 \begin_layout Plain Layout
1229 \begin_layout Plain Layout
1231 /* total number of words, lines, chars */
1234 \begin_layout Plain Layout
1244 \begin_layout Subsection
1245 Record of the train of thought creates superior documentation
1248 \begin_layout Standard
1249 The documentation for a literate program is produced as part of writing
1251 Instead of comments provided as side notes to source code a literate program
1252 contains the explanation of concepts on each level, with lower level concepts
1253 deferred to their appropriate place, which allows for better communication
1255 The snippets of the literate
1256 \begin_inset Flex CharStyle:Code
1259 \begin_layout Plain Layout
1265 above show how an explanation of the program and its source code are interwoven.
1266 Such exposition of ideas creates the flow of thought that is like a literary
1268 Knuth famously wrote a
1269 \begin_inset Quotes eld
1273 \begin_inset Quotes erd
1276 which explains the code of the computer strategy game Colossal Cave Adventure,
1281 \begin_layout Plain Layout
1282 The game, also known as
1286 , was originally written by Crowther in about 700 lines of FORTRAN code;
1287 Knuth recast it into the WEB idiom.
1288 It is available at literateprogramming.com or on Knuth's website.
1297 \begin_layout Section
1298 Literate programming tools
1301 \begin_layout Standard
1302 The first published literate programming environment was WEB, introduced
1303 by Donald Knuth in 1981 for his TeX typesetting system; it uses Pascal
1304 as its underlying programming language and TeX for typesetting of the documenta
1306 The complete commented TeX source code was published in Knuth's
1310 , volume B of his 5-volume
1312 Computers and Typesetting
1315 Knuth had privately used a literate programming system called DOC as early
1317 He was inspired by the ideas of Pierre-Arnoul de Marneffe
1321 \begin_layout Plain Layout
1322 de Marneffe, Pierre Arnoul (December 1973).
1325 Holon Programming - Report PMAR 73-23
1328 University de Liège, Service d'Informatique.
1334 The free CWEB, written by Knuth and Silvio Levy, is WEB adapted for C and
1335 C++, runs on most operating systems and can produce TeX and PDF documentation.
1339 \begin_layout Standard
1340 Other implementations of the literate programming concept are noweb and
1341 FunnelWeb, both of which are independent of the programming language of
1343 Noweb is well-known for its simplicity: just 2 text markup conventions
1344 and 2 tool invocations are needed to use it, and it allows for text formatting
1345 in HTML rather than going through the TeX system.
1346 FunnelWeb is another program without dependency on TeX which can produce
1347 HTML documentation output.
1348 It has more complicated markup (with
1349 \begin_inset Quotes eld
1353 \begin_inset Flex CharStyle:Code
1356 \begin_layout Plain Layout
1363 \begin_inset Quotes erd
1366 escaping any FunnelWeb command), but has many more flexible options.
1370 \begin_layout Standard
1371 The Leo text editor is an
1375 editor which supports optional noweb and CWEB markup.
1376 The author of Leo actually mixes two different approaches: first, Leo is
1377 an outlining editor, which helps with management of large texts, second,
1378 Leo incorporates some of the ideas of literate programming, which in its
1380 the way it is used by Knuth Web tool and/or tools like
1381 \begin_inset Quotes eld
1385 \begin_inset Quotes erd
1388 ) is possible only with some degree of inventiveness and the use of the
1389 editor in a way not exactly envisioned by its author (in modified @root
1391 However this and other extensions (@file nodes) make outline programming
1392 and text management successful and easy and in some ways similar to literate
1397 \begin_layout Plain Layout
1399 (September 2, 2008).
1401 http://webpages.charter.net/edreamleo/front.html.
1402 Retrieved January 4, 2009.
1410 \begin_layout Standard
1411 The Haskell programming language has native support for semi-literate programmin
1412 g, inspired by CWEB but with a simpler implementation.
1413 When aiming for TeX output, one writes a plain LaTeX file where source
1414 code is marked by a given surrounding environment; LaTeX can be set up
1415 to handle that environment, while the Haskell compiler looks for the right
1416 markers to identify Haskell statements to compile, removing the TeX documentati
1417 on as if they were comments.
1418 However, as described above, this is not literate programming in the sense
1420 Haskell's functional, modular nature
1424 \begin_layout Plain Layout
1425 Hughes, John (January 9, 2002).
1428 Why Functional Programming Matters
1431 Institutionen för Datavetenskap, Chalmers Tekniska Högskola,.
1432 http://www.cs.chalmers.se/
1433 \begin_inset space ~
1436 rjmh/Papers/whyfp.pdf.
1437 Retrieved January 4, 2009.
1442 makes literate programming directly in the language somewhat easier, but
1443 it is not nearly as powerful as one of the a WEB tools where
1444 \begin_inset Quotes eld
1448 \begin_inset Quotes erd
1451 can reorganize in arbitrary ways.
1455 \begin_layout Section
1459 \begin_layout Standard
1460 Sweave - an example of use of the
1461 \begin_inset Quotes eld
1465 \begin_inset Quotes erd
1468 -like Literate Programming tool inside the R language for creation of dynamic
1472 \begin_layout Section
1476 \begin_layout Standard
1481 Literate Programming
1484 , California: Stanford University Center for the Study of Language and
1486 ISBN 978-0937073803.
1487 \begin_inset space ~
1493 \begin_layout Standard
1498 TeX & LaTeX: Drawing and Literate Programming
1503 \begin_inset space ~
1506 (includes software).
1510 \begin_layout Standard
1511 Nørmark, Kurt (August 13, 1998).
1512 "Literate Programming - Issues and Problems".
1513 University of Aalborg.
1514 http://www.cs.aau.dk/
1515 \begin_inset space ~
1518 normark/litpro/issues-and-problems.html.
1519 \begin_inset space ~
1525 \begin_layout Section
1529 \begin_layout Standard
1530 comp.programming.literate FAQ at Internet FAQ Archives
1533 \begin_layout Standard
1534 Literate Programming newsgroup
1537 \begin_layout Standard
1538 LiteratePrograms a literate programming wiki.
1542 \begin_layout Standard
1543 Select A literate programming example using noweb
1546 \begin_layout Standard
1547 Softpanorama page on literate programming
1550 \begin_layout Standard
1551 Haskell literate programming
1554 \begin_layout Standard
1555 Specification of literate programming in the Haskell Report the accepted
1559 \begin_layout Standard
1560 Noweb — A Simple, Extensible Tool for Literate Programming
1563 \begin_layout Standard
1564 Lp4all — A Simple Literate Programming Tool with a wiki-like markup syntax