文学编程

source: https://zh.wikipedia.org/wiki/%E6%96%87%E5%AD%A6%E7%BC%96%E7%A8%8B


文学编程[编辑]

维基百科,自由的百科全书

文学编程英语:Literate programming)是由高德纳提出的编程方法,希望能用来替换结构化编程范型。[1]

正如高德纳所构想的那样,文学编程范型不同于传统的由计算机强加的编写程序的方式和顺序,而代之以让程序员用他们自己思维内在的逻辑和流程所要求的顺序开发程序。[2]文学编程自由地表达逻辑,而且它用人类日常使用的语言写出来,就好像一篇文章一样,文章里包括用来隐藏抽象的和传统的源代码。文学编程工具用来从文学源文件中获得两种表达方式,一种用于计算机进一步的编译和执行,称作“绕出”(tangled)的代码,一种用于格式化文档,称作从文学源代码中“织出”(woven)。[3]。虽然第一代文学编程工具特定于计算机语言,但后来的工具可以不依赖具体语言,并且存在于比编程语言更高的层次中。

概念[编辑]

文学程序是用自然语言(比如英语)写出来的对程序逻辑的解释,程序中交织点缀着宏和传统源代码段。在文学编程的源文件中,宏很简单,它或与标题类似,或是解决编程问题时用人类语言描述抽象的解释性短语。它把代码段或更低层次的宏隐藏了起来,且与计算机科学教学时经常用到的,用伪码写的算法相似。这些任意解释的短语成为新的精确的操作符,操作符由程序员在运行过程中创建,组成了在基本编程语言之上的“元语言”。 预处理器用于替换任意层级,说得更准确些是“在'网'和宏之间创建联系”,[4]用命令"tangle"产生可编译源代码,用命令"weave"产生文档。预处理器还提供了写出宏的内容的能力和在文学程序源文件中的任何地方增加已创建的宏的能力,由此不必受传统编程语言强加的那些限制或是打断自己的思路。

优点[编辑]

根据高德纳本人所说,[5][6]文学编程为高质量程序而生,因为它强迫程序员显式描述程序背后的思路,让不充分的设计决策无所遁形。高德纳还声称文学编程提供了一流的文档系统,它并非插件,而是随着编程思路的慢慢展现而不断自然发展的过程。[7]产生出来的文档使作者能在以后的任何时间重新找到自己的思路,也能使其他程序员更容易理解程序的建构过程。这与传统文档不同,那里程序员必须和编译器规定的代码顺序写在一起,还必须从代码和注释中重现当时的思路。文学编程的元语言能力也据称[谁?]普遍利于思考,能从更高的层次统观代码,也能增加人的智能可成功保持和处理的概念数量。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.

误解[编辑]

文学编程常常被误解[8]为不过是从有源代码和注释的文件中产生格式化文档,或是在代码里写大量的注释。这一误解导致那些文档析出工具,如PerlPlain Old Documentation系统也被称为“文学编程工具”。尽管如此,因为这些工具没有实现隐藏在自然语言宏系统背后的“抽象概念网”,或是提供把机器规定的源代码顺序变为人类思维更容易理解的顺序的能力,它们不能在高德纳提出的意义下被称作文学编程工具。[8][9]

例子[编辑]

一个文学编程的经典例子是标准Unix单词计数程序wc的文学实现。高德纳在他的《文学编程》书中的第12章展示了这个例子的CWEB版本。后来它也为noweb文学编程工具而重写。[10]这一例子漂亮地阐释了文学编程的基本元素。

创建宏

下面这个wc的文学编程[10]代码片断展示了在文学编程中用来创建宏的自然语言的描述性词组有多随意,宏作为文学编程语言中新的“操作符”,并且隐藏了代码块或其它的宏。由两个尖括号组成("<<...>>")的标记符号表示宏,"@"符号在noweb文件中表示一节代码的结束。"<<*>>"符号表示“根”,即最上层节点,文学编程工具要从这里展开宏组成的网。实际上,扩展的源代码可通过任何节和小节(即标为"<<代码块名>>="的代码)写出来,所以一个文学程序文件可包括多个机器源代码文件。

wc的目的是对多个文件中的行、单词和字母计数。文件中的行数是......../更多解释/

这里是由noweb程序wc.nw定义的文件wc.c的概述: 
    <<*>>=
    <<包含头文件>>
    <<定义>>
    <<全局变量>>
    <<函数>>
    <<主程序>>
    @
    
我们必须包含标准输入输出定义,因为我们想发送格式化的输出到stdoutstderr上。
    <<包含头文件>>=
    #include <stdio.h>
    @

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.

作为网的程序 - 宏不只是节的名字

宏和标准文档中的“节名”不同。文学编程的宏能隐藏任何代码块,并且被用于任何低层次的机器语言操作符内,常常在如"if", "while"或 "case"这样的逻辑操作符内。这会在下面这段文学程序wc的代码片断中解释。[10]

 
这里的代码块做了计数的工作,这正是wc存在的目的,实际上非常容易写。我们察看每一个字母并且如果它是一个单词的开始或结束,则会更改状态。

    <<扫描文件>>=
    while (1) {
      <<Fill buffer if it is empty; break at end of file>>
      c = *ptr++;
      if (c > ' ' && c < 0177) {
        /* visible ASCII codes */
        if (!in_word) {
          word_count++;
          in_word = 1;
        }
        continue;
      }
      if (c == '\n') line_count++;
      else if (c != ' ' && c != '\t') continue;
      in_word = 0;
        /* c is newline, space, or tab */
    }
    @

实际上,宏能代表任意的代码块和其它宏,并且因此比自顶向下或自底向上的代码块或小节更通用。高德纳说当他意识到这一点后,他开始把程序想成不同部分组成的“网”。[1]

人的逻辑顺序,而不是编译器的

在noweb文学程序中,除了可以任意顺序展现代码外,宏背后的代码块,一旦由"<<...>>="引入,可以在文件后面的任何一个地方通过简单地写"<<代码块名>>="进行扩充并且往里添加更多的内容,如下面这个代码片断所示("+"为了可读性而被文档格式化器所添加,它并不在代码中)。[10]

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?) 

    <<Global variables>>+=
    long tot_word_count, tot_line_count, 
         tot_char_count;
      /* total number of words, lines, chars */
    @
记录思想碰撞的火花,创建出类拔萃的文档

文学编程的文档作为写程序的一部分而产生。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 the computer strategy game Colossal Cave Adventure, perfectly readable.[11]

文学编程工具[编辑]

第一个发布的文学编程环境是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[12]. 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.[13]

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[14] 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 - an example of use of the "noweb"-like Literate Programming tool inside the R language for creation of dynamic statistical reports

参考资料[编辑]

  1. 1.0 1.1 v w x y z Knuth, Donald E. Literate Programming (PDF)The Computer Journal (British Computer Society). 1984, 27 (2): 97–111 [January 4, 2009].doi:10.1093/comjnl/27.2.97.
  2. ^

    "我感觉自顶向下和自底向上是两种截然相反的方法论:一个更适合分析程序,一个更适合创建程序。但是,使用WEB编程后,我意识到不必在自顶向下和自底向上之间艰难抉择,因为我们最好把程序的结构看作网状而不是树状的。层级结构是存在的,但对于程序来说,最重要的是其结构的关系。一个复杂的软件由多个简单部分和它们之间的简单关系构成。程序员的任务是用人类最易理解的顺序,而不是用像自顶向下或自顶向上之类刻板僵硬的顺序,来描述清楚这些部分和关系。"

    高德纳 , Literate Programming[1]

  3. ^ 如果有人记得该工具的第一个版本称作WEB,一段好玩的高德纳隐藏其中的文学作品引用不言自明:“啊,自从我们第一次撒谎,就开始编织(weave)一张错综复杂(tangled)的网” - 沃尔特·司各特, in Canto VI, Stanza 17 of Marmion (1808) 写于1513年的一部关于弗洛登战役的史诗。-- 该引文实际上出自1986年5月Jon Bentley和高德纳发表在"Communications of the ACM, vol 29 num 5 on p.365"的经典栏目“编程珠玑”上的一篇文章的引言中
  4. ^

    "WEB的宏至多允许带一个参数。同样地,我这么做是出于简单考虑,因为我注意到大多数带多参数的应用其实可归纳为单参数的情况。比如说,假如你想定义某个东西……换言之,一个宏的名字可以作为另一个宏的参数,这是很有用的。这个特殊的技巧使其成为可能……"

    高德纳 , Literate Programming[1]

  5. ^ Knuth, Donald E.; Andrew Binstock. Interview with Donald Knuth. April 25, 2008 [January 4, 2009]然而对我来说,文学编程确实是由TeX项目派生出来的最重要的东西。它不仅让我前所未有地更快地写和维护可靠性更高的程序,而且成为我自20世纪80年代以来的最大的快乐之源——它有时实际上是不可或缺的。我做的其它一些大程序,比如MMIX元模拟器,用我见过的任何一种其它的方法论是无法写出来的。其复杂性让我有限的智能望而却步。没有文学编程,我的整个事业规划就会轰然倒塌。……文学编程是你更上一层楼的必要工具。
  6. ^

    "我使用WEB时学到的另一令人惊奇的事情是传统编程语言导致我写出了低劣的程序,尽管我还没意识到我在做什么。我原来的想法是WEB仅作为写文档的工具,但我后来发现用WEB写的程序比我用其它语言写的程序都要优秀。"

    高德纳 , Literate Programming[1]

  7. ^

    "因此WEB语言允许人们用"意识流"顺序表达程序。TANGLE is able to scramble everything up into the arrangement that a PASCAL compiler demands. This feature of WEB is perhaps its greatest asset; it makes a WEB-written program much more readable than the same program written purely in PASCAL, even if the latter program is well commented. And the fact that there's no need to be hung up on the question of top-down versus bottom-up, since a programmer can now view a large program as a web, to be explored in a psychologically correct order is perhaps the greatest lesson I have learned from my recent experiences."

    高德纳 , Literate Programming[1]

  8. 8.0 8.1 Dominus, Mark-JasonPOD is not Literate Programming. Perl.com. March 20, 2000 [January 3, 2009].
  9. ^

    "我选择WEB作为名字,部分因为它是英语中很少的几个没有用于计算机的三字词语之一。但随着时间流逝,我越来越喜爱这个名字,因为我认为最好把一个复杂的软件确确实实地看成由简单材料以精巧的方式拼出来的一张网。我们通过理解复杂系统的各个简单部分和这些部分与其直接邻居的简单关系来理解复杂系统本身。如果我们用网的概念来表达程序,我们就能用一种自然且圆满地方式强调其结构上的属性。"

    高德纳 , Literate Programming[1]

  10. 10.0 10.1 10.2 10.3 Ramsey, Norman. An Example of noweb. May 13, 2008 [January 4, 2009].
  11. ^ The game, also known as ADVENT, was originally written by Crowther in about 700 lines of FORTRAN code; Knuth recast it into the WEB idiom. It is available atliterateprogramming.com or on Knuth's website.
  12. ^ de Marneffe, Pierre Arnoul. Holon Programming - Report PMAR 73-23. University de Liège, Service d'Informatique. December 1973.
  13. ^ Ream, Edward K. Leo's Home Page. September 2, 2008 [January 4, 2009].
  14. ^ Hughes, John. Why Functional Programming Matters (PDF). Institutionen för Datavetenskap, Chalmers Tekniska Högskola,. January 9, 2002 [January 4, 2009].

进一步阅读[编辑]

外部链接[编辑]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值