文学编程

title: 概念.
content: 文学程序是用自然语言(比如英语)写出来的对程序逻辑的解释,程序中交织点缀着宏和传统源代码段。在文学编程的源文件中,宏很简单,它或与标题类似,或是解决编程问题时用人类语言描述抽象的解释性短语。它把代码段或更低层次的宏隐藏了起来,且与计算机科学教学时经常用到的,用虚拟码写的算法相似。这些任意解释的短语成为新的精确的操作符,操作符由程序员在运行过程中创建,组成了在基本编程语言之上的“元语言”。 预处理器用于替换任意层级,说得更准确些是“在'网'和宏之间建立联系”,用命令"tangle"产生可编译源代码,用命令"weave"产生文档。预处理器还提供了写出宏的内容的能力和在文学程序源文件中的任何地方增加已创建的宏的能力,由此不必受传统编程语言强加的那些限制或是打断自己的思路。
title: 优点.
content: 根据高德纳本人所说,文学编程为高质量程序而生,因为它强迫程序员显式描述程序背后的思路,让不充分的设计决策无所遁形。高德纳还声称文学编程提供了一流的文档系统,它并非插件,而是随着编程思路的慢慢展现而不断自然发展的过程。产生出来的文档使作者能在以后的任何时间重新找到自己的思路,也能使其他程序员更容易理解程序的建构过程。这与传统文档不同,那里程序员必须和编译器规定的代码顺序写在一起,还必须从代码和注释中重现当时的思路。文学编程的元语言能力也普遍利于思考,能从更高的层次统观代码,也能增加人的智能可成功保持和处理的概念数量。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.
title: 误解.
content: 文学编程常常被误解为不过是从有源代码和注释的文件中产生格式化文档,或是在代码里写大量的注释。这一误解导致那些文档析出工具,如Perl的Plain Old Documentation系统也被称为“文学编程工具”。尽管如此,因为这些工具没有实现隐藏在自然语言宏系统背后的“抽象概念网”,或是提供把机器规定的源代码顺序变为人类思维更容易理解的顺序的能力,它们不能在高德纳提出的意义下被称作文学编程工具。
title: 例子.
content: 一个文学编程的经典例子是标准Unix单词计数程序codice_1的文学实现。高德纳在他的《文学编程》书中的第12章展示了这个例子的CWEB版本。后来它也为noweb文学编程工具而重写。这一例子漂亮地阐释了文学编程的基本元素。  下面这个codice_1的文学编程代码片断展示了在文学编程中用来创建宏的自然语言的描述性词组有多随意,宏作为文学编程语言中新的“操作符”,并且隐藏了代码块或其它的宏。由两个尖括号组成("codice_3")的标记符号表示宏,"codice_4"符号在noweb文件中表示一节代码的结束。"codice_5"符号表示“根”,即最上层节点,文学编程工具要从这里展开宏组成的网。实际上,扩展的源代码可通过任何节和小节(即标为"codice_6"的代码)写出来,所以一个文学程序文件可包括多个机器源代码文件。  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.  宏和标准文档中的“节名”不同。文学编程的宏能隐藏任何代码块,并且被用于任何低层次的机器语言操作符内,常常在如"codice_7", "codice_8"或 "codice_9"这样的逻辑操作符内。这会在下面这段文学程序codice_1的代码片断中解释。  实际上,宏能代表任意的代码块和其它宏,并且因此比自顶向下或自底向上的代码块或小节更通用。高德纳说当他意识到这一点后,他开始把程序想成不同部分组成的“网”。  在noweb文学程序中,除了可以任意顺序展现代码外,宏背后的代码块,一旦由"codice_11"引入,可以在文件后面的任何一个地方通过简单地写"codice_6"进行扩充并且往里添加更多的内容,如下面这个代码片断所示("+"为了可读性而被文档格式化器所添加,它并不在代码中)。  文学编程的文档作为写程序的一部分而产生。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 codice_1 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 the computer strategy game Colossal Cave Adventure, perfectly readable.
title: 文学编程工具.
content: 第一个发布的文学编程环境是WEB,由高德纳于1981年为他的TeX排版系统而引入。它使用Pascal作为其基础编程语言,使用TeX作为文档排版工具。The complete commented TeX source code was published in Knuth's "TeX: The program", volume B of his 5-volume "Computers and Typesetting". 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, written by Knuth and Silvio Levy, is WEB adapted for C and C++, runs on most operating systems and can produce TeX and PDF documentation.  其它的文学编程概念的实现有noweb和FunnelWeb,它们都是源代码独立的。Noweb以其简单而知名: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.  The Leo text editor 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编程语言对半文学编程有原生支持,其来源于CWEB但用了较简单的实现。如果想要TeX输出,你可以写 a plain LaTeX 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.

Summary: 文学式编程(英语:Literate programming)是由高德纳提出的编程方法,希望能用来取代结构化编程范型。

Literate programming: Literate programming is a programming paradigm introduced by Donald Knuth in which a computer program is given an explanation of its logic in a natural language, such as English, interspersed with snippets of macros and traditional source code, from which compilable source code can be generated. The approach is used in scientific computing and in data science routinely for reproducible research and open access purposes. Literate programming tools are used by millions of programmers today.