Literate programming is an approach to programming which was introduced by Donald Knuth
Donald Knuth
Donald Ervin Knuth is a renowned computer science and Emeritus of the Art of Computer Programming at Stanford University.Author of the seminal multi-volume work The Art of Computer Programming , Knuth has been called the "father" of the run-time analysis, contributing to the development of, and systematizing formal mathematical techn... . Knuth conceived literate programming as an alternative to the structured programming
Structured programming
Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. It is most famous for removing or reducing reliance on the GOTO Statement .... paradigm of the 1970s.
The literate programming paradigm, as conceived by Knuth, represents a move away from writing programs
Computer program
Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running.... in the manner and order imposed by the computer, and instead enables programmer
Programmer
A programmer is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software.... s to develop programs in the order demanded by the logic and flow of their thoughts.
Discussion
Ask a question about 'Literate programming'
Start a new discussion about 'Literate programming'
Answer questions from other users
Full Discussion Forum
Encyclopedia
Literate programming is an approach to programming which was introduced by Donald Knuth
Donald Knuth
Donald Ervin Knuth is a renowned computer science and Emeritus of the Art of Computer Programming at Stanford University.Author of the seminal multi-volume work The Art of Computer Programming , Knuth has been called the "father" of the run-time analysis, contributing to the development of, and systematizing formal mathematical techn... . Knuth conceived literate programming as an alternative to the structured programming
Structured programming
Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. It is most famous for removing or reducing reliance on the GOTO Statement .... paradigm of the 1970s.
The literate programming paradigm, as conceived by Knuth, represents a move away from writing programs
Computer program
Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running.... in the manner and order imposed by the computer, and instead enables programmer
Programmer
A programmer is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software.... s to develop programs in the order demanded by the logic and flow of their thoughts. Literate programs are written as an uninterrupted exposition of logic in an ordinary human language, much like the text of an essay, in which macros which hide abstractions and traditional source code
Source code
In computer science, source code is any collection of statements or declarations written in some human-readable computer programming language.... are included. Literate programming tools are used to both en-tangle a literate program into a form suitable for further compilation or execution, and to weave the program into formatted documentation. While the first generation of literate programming tools were computer language-specific, the later ones are language-agnostic and exist above the programming languages.
Concept
A literate program is an explanation of the program logic in a natural language
Natural language
In the philosophy of language, a natural language is a language that is spoken, Sign language, or writing by humans for general-purpose communication, as distinguished from formal languages and from constructed languages.... , such as English, interspersed with snippets of macros and traditional source code. Macros in a literate source file are simply title-like or explanatory phrases in a human language that describe human abstractions created while solving the programming problem, and hiding chunks of code or lower-level macros. These macros are similar to the algorithm
Algorithm
In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing.... s in pseudocode
Pseudocode
Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading.... typically used in teaching computer science
Computer science
Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems.... . These arbitrary explanatory phrases become precise new operators, created on the fly by the programmer, forming a meta-language on top of the underlying programming language.
In computer science, a preprocessor is a Computer program that processes its input data to produce output that is used as input to another program.... is used to substitute arbitrary hierarchies, or rather "interconnected 'webs' of macros", to produce the compilable source code with one command ("tangle"), and documentation with another ("weave"). The preprocessor also provides an ability to write out the content of the macros and to add to already created macros in any place in the text of the literate program source file, thereby disposing of the need to keep in mind the restrictions imposed by traditional programming languages or to interrupt the flow of thought.
Advantages of the method
According to Knuth, literate programming provides for higher-quality programs, since it forces programmers to explicitly state the thoughts behind the program, making poorly thought-out design decisions more obvious. Knuth also claims that literate programming provides a first-rate documentation system, which is not an add-on, but is grown naturally in the process of exposition of one's thoughts during a program creation. The resulting documentation allows authors to restart their own thought processes at any later time, and allows other programmers to more easily understand the construction of the program. This differs from traditional documentation, in which a programmer is presented with source code that follows a compiler-imposed order, and must decipher the thought process behind the program from the code and its associated comments. The meta-language capabilities of literate programming are also claimed to facilitate thinking in general, giving a higher "bird's eye view" of the code and increasing the number of concepts the mind can successfully retain and process. Applicability of the concept to programming on a large scale, that of commercial-grade programs is proven by an edition of TeX code as a literate program.
Misconceptions
Literate programming is very often misunderstood to refer only to formatted documentation produced from a common file with both source code and comments, or to voluminous commentaries included with code. This misconception has lead to claims that comment-extraction tools, such as the Perl
Perl
In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language.... Plain Old Documentation
Plain Old Documentation
Plain Old Documentation, abbreviated pod, is a simplemarkup language used to document the Perl programming language.... system, are "literate programming tools". However, because these tools do not implement the "web of abstract concepts" hiding behind the system of natural-language macros, or provide an ability to the change the order the source code from a machine-imposed sequence to one convenient to the human mind, they cannot properly be called literate programming tools in the sense intended by Knuth.
Example
A classic example of literate programming is the literate implementation of the standard Unix
Unix
Unix is a computer operating system originally developed in 1969 by a group of American Telephone & Telegraph employees at Bell Labs, including Ken Thompson , Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.... wc
Wc (Unix)
wc is a command in Unix-like operating systems.The program reads either standard input or a list of files and generates one or more of the following statistics: number of bytes, word count, and number of lines .... word counting program. Knuth presented a CWEB
CWEB
CWEB is a computer programming system created by Donald Knuth and Silvio Levy as a follow up to Knuth's WEB literate programming system, using the C instead of Pascal .... version of this example in Chapter 12 of his Literate Programming book. The same example was later rewritten for the noweb
Noweb
noweb is a Free software literate programming tool,created in 1989-1999 by Norman Ramsey ,and designed to be simple, easily extensible and language independent.... literate programming tool. This example provides a good illustration of the basic elements of literate programming.
Creation of macros
The following snippet of the wc literate program shows how arbitrary descriptive phrases in a natural language are used in a literate program to create macros, which act as new "operators" in the literate programming language, and hide chunks of code or other macros. The mark-up notation consists of double angle brackets ("<<...>>")that indicate macros, the "@" symbol which indicates the end of the code section in a noweb file. The "<<*>>" symbol stands for the "root", topmost node the literate programming tool will start expanding the web of macros from. Actually, writing out the expanded source code can be done from any section or subsection (i.e. a piece of code designated as "<>=", with the equal sign), so one literate program file can contain several files with machine source code.
The purpose of wc is to count lines, words, and/or characters in a list of files. The
number of lines in a file is ......../more explanations/
Here, then, is an overview of the file wc.c that is defined by the noweb program wc.nw:
<<*>>=
<>
<>
<>
<>
<>
@
We must include the standard I/O definitions, since we want to send formatted output
to stdout and stderr.
<>=
#include
@
Note also that the unraveling of the chunks can be done in any place in the literate program text file, not necessarily in the order they are sequenced in the enclosing chunk, but as is demanded by the logic reflected in the explanatory text that envelops the whole program.
Program as a Web - Macros are not just section names
Macros are not the same as "section names" in standard documentation. Literate programming macros can hide any chunk of code behind themselves, and be used inside any low-level machine language operators, often inside logical operators such as "if", "while" or "case". This is illustrated by the following snippet of the wc literate program.
The present chunk, which does the counting that is wc's raison d'etre, was actually one of
the simplest to write. We look at each character and change state if it begins or ends
a word.
<>=
while (1)
@
In fact, macros can stand for any arbitrary chunk of code or other macros, and are thus more general than top-down or bottom-up "chunking", or than subsectioning. Knuth says that when he realized this, he began to think of a program as a web of various parts.
Order of human logic, not that of the compiler
In a noweb literate program besides the free order of their exposition, the chunks behind macros, once introduced with "<<...>>=", can be grown later in any place in the file by simply writing "<>+=" and adding more content to it, as the following snippet illustrates.
The grand totals must be initialized to zero at the beginning of the program.
If we made these variables local to main, we would have to do this initialization
explicitly; however, C globals are automatically zeroed. (Or rather,``statically
zeroed. (Get it?)
<>+=
long tot_word_count, tot_line_count,
tot_char_count;
/* total number of words, lines, chars */
@
Record of the train of thought creates superior documentation
The documentation for a literate program is produced as part of writing the program. Instead of comments provided as side notes to source code a literate program contains the explanation of concepts on each level, with lower level concepts deferred to their appropriate place, which allows for better communication of thought. The snippets of the literate wc above show how an explanation of the program and its source code are interwoven. Such exposition of ideas creates the flow of thought that is like a literary work. Knuth famously wrote a "novel" which explains the code of a computer strategy game, perfectly readable.
Literate programming tools
The first published literate programming environment was WEB
Donald Ervin Knuth is a renowned computer science and Emeritus of the Art of Computer Programming at Stanford University.Author of the seminal multi-volume work The Art of Computer Programming , Knuth has been called the "father" of the run-time analysis, contributing to the development of, and systematizing formal mathematical techn... in 1981 for his TeX
TeX
TeX is a typesetting system designed and mostly written by Donald Knuth. Together with the METAFONT language for font description and the Computer Modern typefaces, it was designed with two main goals in mind: to allow anybody to produce high-quality books using a reasonable amount of effort, and to provide a system that would give the exact... typesetting system; it uses Pascal
Pascal (programming language)
Pascal is an influential imperative programming and Procedural programming programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structure.... as its underlying programming language and TeX for typesetting of the documentation. The complete commented TeX source code was published in Knuth's TeX: The program, volume B of his 5-volume Computers and Typesetting
Computers and Typesetting
Computers and Typesetting is a 5-volume set of books by Donald Knuth published 1986 describing the TeX and METAFONT systems for digital typography.... . Knuth had privately used a literate programming system called DOC as early as 1979. He was inspired by the ideas of Pierre-Arnoul de Marneffe. The free CWEB
CWEB
CWEB is a computer programming system created by Donald Knuth and Silvio Levy as a follow up to Knuth's WEB literate programming system, using the C instead of Pascal .... , written by Knuth and Silvio Levy, is WEB adapted for C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... and C++
C++
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features.... , runs on most operating systems and can produce TeX and PDF
Portable Document Format
Portable Document Format is a file format created by Adobe Systems in 1993 for document exchange. PDF is used for representing two-dimensional documents in a manner independent of the application software, hardware, and operating system.... documentation.
Other implementations of the literate programming concept are noweb
Noweb
noweb is a Free software literate programming tool,created in 1989-1999 by Norman Ramsey ,and designed to be simple, easily extensible and language independent.... and FunnelWeb, both of which are independent of the programming language of the source code. Noweb is well-known for its simplicity: just 2 text markup conventions and 2 tool invocations are needed to use it, and it allows for text formatting in HTML rather than going through the TeX system. FunnelWeb is another program without dependency on TeX which can produce HTML documentation output. It has more complicated markup (with "@" escaping any FunnelWeb command), but has many more flexible options.
Leo is a text editor that features outliner with clones as its central tool of organization and navigation.... is an outlining editor which supports optional noweb and CWEB markup. The author of Leo actually mixes two different approaches: first, Leo is an outlining editor, which helps with management of large texts, second, Leo incorporates some of the ideas of literate programming, which in its pure form (i.e. the way it is used by Knuth Web tool and/or tools like "noweb") is possible only with some degree of inventiveness and the use of the editor in a way not exactly envisioned by its author (in modified @root nodes). However this and other extensions (@file nodes) make outline programming and text management successful and easy and in some ways similar to literate programming.
Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:... programming language has native support for semi-literate programming, inspired by CWEB but with a simpler implementation. When aiming for TeX output, one writes a plain LaTeX
LaTeX
LaTeX is a document markup language and Word processor for the TeX typesetting program. Within the typesetting system, its name is styled as .... file where source code is marked by a given surrounding environment; LaTeX can be set up to handle that environment, while the Haskell compiler looks for the right markers to identify Haskell statements to compile, removing the TeX documentation as if they were comments. However, as described above, this is not literate programming in the sense intended by Knuth. Haskell's functional, modular nature makes literate programming directly in the language somewhat easier, but it is not nearly as powerful as one of the a WEB tools where "tangle" can reorganize in arbitrary ways.
Sweave is a Function in the statistical programming language R that enables integration of R code into LaTeX documents. The purpose is "to create dynamic reports, which can be updated automatically if data or analysis change".... - an example of use of the "noweb"-like Literate Programming tool inside the R language for creation of dynamic statistical reports
noweb is a Free software literate programming tool,created in 1989-1999 by Norman Ramsey ,and designed to be simple, easily extensible and language independent....