编程语言流行之道
Paul Graham 从黑客视角剖析了编程语言流行的真正原因:不是语言设计者的理论完美,而是是否满足黑客的实际需求。语言需要免费实现、一本 K&R 式的书、以及一个可 hack 的系统(如 Unix 的脚本语言)。黑客喜欢简洁(如 z=x+y 而非冗长表述)和可 hack 性(能绕过抽象层)。一次性程序的写作能力至关重要,因为大系统往往从中演化。库函数的重要性日益超过核心语言。语法不是 Lisp 流行的主要障碍,而是缺乏强大的库和系统集成。最后提出黑客的梦想语言特征:简洁、可交互、透明抽象、高效分析器、良好库设计等。


May 2001(This article was written as a kind of business plan for a new language. So it is missing (because it takes for granted) the most important feature of a good programming language: very powerful abstractions.)
A friend of mine once told an eminent operating systems expert that he wanted to design a really good programming language. The expert told him that it would be a waste of time, that programming languages don't become popular or unpopular based on their merits, and so no matter how good his language was, no one would use it. At least, that was what had happened to the language he had designed.
What does make a language popular? Do popular languages deserve their popularity? Is it worth trying to define a good programming language? How would you do it?
I think the answers to these questions can be found by looking at hackers, and learning what they want. Programming languages are for hackers, and a programming language is good as a programming language (rather than, say, an exercise in denotational semantics or compiler design) if and only if hackers like it.
2001年5月(这篇文章原本是为一种新语言撰写的商业计划书。因此它遗漏了(因为预设了)优秀编程语言最重要的特征:非常强大的抽象能力。)
我曾有位朋友告诉一位杰出的操作系统专家,说他想要设计一门真正优秀的编程语言。专家告诉他这是浪费时间,因为编程语言的流行与否并不取决于其优点,所以无论他的语言有多好,都不会有人用。至少,专家自己设计的语言就是如此。
那么,是什么让一门语言流行起来?流行的语言真的实至名归吗?值得尝试定义一门好的编程语言吗?又该如何去做?
我认为,要回答这些问题,需要观察黑客,了解他们的需求。编程语言是为黑客服务的,一门语言作为编程语言(而非指称语义学或编译器设计的练习)是否优秀,唯一的判断标准就是黑客是否喜欢它。
It's true, certainly, that most people don't choose programming languages simply based on their merits. Most programmers are told what language to use by someone else. And yet I think the effect of such external factors on the popularity of programming languages is not as great as it's sometimes thought to be. I think a bigger problem is that a hacker's idea of a good programming language is not the same as most language designers'.
Between the two, the hacker's opinion is the one that matters. Programming languages are not theorems. They're tools, designed for people, and they have to be designed to suit human strengths and weaknesses as much as shoes have to be designed for human feet. If a shoe pinches when you put it on, it's a bad shoe, however elegant it may be as a piece of sculpture.
It may be that the majority of programmers can't tell a good language from a bad one. But that's no different with any other tool. It doesn't mean that it's a waste of time to try designing a good language. Expert hackers can tell a good language when they see one, and they'll use it. Expert hackers are a tiny minority, admittedly, but that tiny minority write all the good software, and their influence is such that the rest of the programmers will tend to use whatever language they use. Often, indeed, it is not merely influence but command: often the expert hackers are the very people who, as their bosses or faculty advisors, tell the other programmers what language to use.
The opinion of expert hackers is not the only force that determines the relative popularity of programming languages — legacy software (Cobol) and hype (Ada, Java) also play a role — but I think it is the most powerful force over the long term. Given an initial critical mass and enough time, a programming language probably becomes about as popular as it deserves to be. And popularity further separates good languages from bad ones, because feedback from real live users always leads to improvements. Look at how much any popular language has changed during its life. Perl and Fortran are extreme cases, but even Lisp has changed a lot. Lisp 1.5 didn't have macros, for example; these evolved later, after hackers at MIT had spent a couple years using Lisp to write real programs. [1]
So whether or not a language has to be good to be popular, I think a language has to be popular to be good. And it has to stay popular to stay good. The state of the art in programming languages doesn't stand still. And yet the Lisps we have today are still pretty much what they had at MIT in the mid-1980s, because that's the last time Lisp had a sufficiently large and demanding user base.
Of course, hackers have to know about a language before they can use it. How are they to hear? From other hackers. But there has to be some initial group of hackers using the language for others even to hear about it. I wonder how large this group has to be; how many users make a critical mass? Off the top of my head, I'd say twenty. If a language had twenty separate users, meaning twenty users who decided on their own to use it, I'd consider it to be real.
Getting there can't be easy. I would not be surprised if it is harder to get from zero to twenty than from twenty to a thousand. The best way to get those initial twenty users is probably to use a trojan horse: to give people an application they want, which happens to be written in the new language.
诚然,大多数人不会仅仅根据语言本身的优点来选择编程语言。多数程序员是别人告诉他们使用什么语言。但我认为,这些外部因素对编程语言流行度的影响,并没有人们有时想象的那么大。更大的问题在于,黑客眼中好的编程语言,与大多数语言设计者眼中的并不相同。
两者之中,黑客的意见才是关键。编程语言不是定理。它们是为人设计的工具,必须像鞋子适合人脚一样,去适应人类的优点和弱点。如果一双鞋穿上就夹脚,那就是一双坏鞋,无论它作为雕塑多么优雅。
也许大多数程序员分不清语言的好坏。但这与其他任何工具并无不同。这并不意味着尝试设计一门好的语言是浪费时间。专家黑客一眼就能看出语言的好坏,并且会使用它。专家黑客固然是极少数,但这极少数人写就了所有优秀的软件,他们的影响力使得其他程序员倾向于使用他们所用的语言。事实上,这往往不仅仅是影响,而是命令:专家黑客常常就是那些作为老板或导师、告诉其他程序员该用什么语言的人。
专家黑客的意见并非决定编程语言相对流行度的唯一力量——遗留软件(Cobol)和炒作(Ada、Java)也扮演了角色——但我认为从长远来看,这是最强大的力量。假以初始的临界质量和足够的时间,一门编程语言很可能达到它应得的流行度。而流行度进一步区分了好语言和坏语言,因为来自真实用户的反馈总会带来改进。看看任何一门流行语言在其生命周期中发生了多大的变化。Perl 和 Fortran 是极端例子,但即使 Lisp 也变化了很多。例如,Lisp 1.5 没有宏;这些是后来才演化出来的,当时 MIT 的黑客们花了几年的时间用 Lisp 编写真正的程序。[1]
所以,无论一门语言是否需要好才能流行,我认为一门语言需要流行才能变好。而且它必须保持流行才能保持优秀。编程语言的艺术不会停滞不前。然而我们今天拥有的 Lisp 仍然与 1980 年代中期 MIT 所使用的差不多,因为那是 Lisp 最后一次拥有足够庞大且要求严格的用户群。
当然,黑客在使用一门语言之前必须先知道它。他们如何听说?从其他黑客那里。但必须有一个初始的黑客群体在使用该语言,其他人才能听说它。我想知道这个群体需要多大;多少用户构成临界质量?凭直觉,我会说二十个。如果一门语言有二十个独立的用户,意思是二十个用户自愿决定使用它,我就认为它是真实的。
达到这个数量并不容易。如果从零到二十比从二十到一千更难,我也不会惊讶。获得最初的二十个用户的最佳方法可能是使用特洛伊木马:给人们一个他们想要的应用程序,而这个应用恰好是用新语言编写的。
Let's start by acknowledging one external factor that does affect the popularity of a programming language. To become popular, a programming language has to be the scripting language of a popular system. Fortran and Cobol were the scripting languages of early IBM mainframes. C was the scripting language of Unix, and so, later, was Perl. Tcl is the scripting language of Tk. Java and Javascript are intended to be the scripting languages of web browsers.
Lisp is not a massively popular language because it is not the scripting language of a massively popular system. What popularity it retains dates back to the 1960s and 1970s, when it was the scripting language of MIT. A lot of the great programmers of the day were associated with MIT at some point. And in the early 1970s, before C, MIT's dialect of Lisp, called MacLisp, was one of the only programming languages a serious hacker would want to use.
Today Lisp is the scripting language of two moderately popular systems, Emacs and Autocad, and for that reason I suspect that most of the Lisp programming done today is done in Emacs Lisp or AutoLisp.
Programming languages don't exist in isolation. To hack is a transitive verb — hackers are usually hacking something — and in practice languages are judged relative to whatever they're used to hack. So if you want to design a popular language, you either have to supply more than a language, or you have to design your language to replace the scripting language of some existing system.
Common Lisp is unpopular partly because it's an orphan. It did originally come with a system to hack: the Lisp Machine. But Lisp Machines (along with parallel computers) were steamrollered by the increasing power of general purpose processors in the 1980s. Common Lisp might have remained popular if it had been a good scripting language for Unix. It is, alas, an atrociously bad one.
One way to describe this situation is to say that a language isn't judged on its own merits. Another view is that a programming language really isn't a programming language unless it's also the scripting language of something. This only seems unfair if it comes as a surprise. I think it's no more unfair than expecting a programming language to have, say, an implementation. It's just part of what a programming language is.
A programming language does need a good implementation, of course, and this must be free. Companies will pay for software, but individual hackers won't, and it's the hackers you need to attract.
A language also needs to have a book about it. The book should be thin, well-written, and full of good examples. K&R is the ideal here. At the moment I'd almost say that a language has to have a book published by O'Reilly. That's becoming the test of mattering to hackers.
There should be online documentation as well. In fact, the book can start as online documentation. But I don't think that physical books are outmoded yet. Their format is convenient, and the de facto censorship imposed by publishers is a useful if imperfect filter. Bookstores are one of the most important places for learning about new languages.
首先承认一个确实影响编程语言流行度的外部因素。要变得流行,一门编程语言必须成为一种流行系统的脚本语言。Fortran 和 Cobol 是早期 IBM 大型机的脚本语言。C 是 Unix 的脚本语言,后来 Perl 也是。Tcl 是 Tk 的脚本语言。Java 和 Javascript 旨在成为 Web 浏览器的脚本语言。
Lisp 不是大众流行的语言,因为它不是大众流行系统的脚本语言。它保留的流行度可以追溯到 1960 和 1970 年代,当时它是 MIT 的脚本语言。当时许多伟大的程序员都与 MIT 有过关联。在 1970 年代早期,在 C 之前,MIT 的 Lisp 方言——MacLisp——是严肃黑客唯一想用的编程语言之一。
今天,Lisp 是两个中等流行系统(Emacs 和 Autocad)的脚本语言,因此我怀疑今天大多数 Lisp 编程都是在 Emacs Lisp 或 AutoLisp 中完成的。
编程语言并非孤立存在。黑客(hack)是一个及物动词——黑客通常在破解某个东西——实践中,语言是根据它们被用于破解的对象来评判的。因此,如果你想设计一门流行的语言,要么你需要提供的不止是语言本身,要么你必须将你的语言设计成替换某个现有系统的脚本语言。
Common Lisp 不受欢迎,部分原因在于它是孤儿。它最初确实附带了一个可以破解的系统:Lisp Machine。但 Lisp Machine(连同并行计算机)在 1980 年代被日益强大的通用处理器碾压了。如果 Common Lisp 曾是 Unix 的好脚本语言,它可能至今仍然流行。可惜,它糟糕透顶。
描述这种情况的一种说法是,语言不是凭其自身优点被评判的。另一种观点是,一门编程语言,除非它同时是某个东西的脚本语言,否则它就不是真正的编程语言。只有当这令人惊讶时,它才显得不公平。我认为这并不过分,就像要求一门编程语言要有实现一样。这只是编程语言定义的一部分。
当然,一门编程语言需要好的实现,而且必须是免费的。公司会为软件付费,但个人黑客不会,而你需要的正是黑客。
语言还需要有一本关于它的书。书应该薄,写得好,并且充满好例子。K&R 是理想榜样。目前我几乎要说,一门语言必须有一本 O'Reilly 出版的书。这已成为衡量它是否对黑客重要的标准。
还应该有在线文档。事实上,书可以从在线文档开始。但我认为纸质书尚未过时。其格式方便,出版商施加的事实审查是一种有用但不完美的过滤器。书店是了解新语言最重要的场所之一。
Given that you can supply the three things any language needs — a free implementation, a book, and something to hack — how do you make a language that hackers will like?
One thing hackers like is brevity. Hackers are lazy, in the same way that mathematicians and modernist architects are lazy: they hate anything extraneous. It would not be far from the truth to say that a hacker about to write a program decides what language to use, at least subconsciously, based on the total number of characters he'll have to type. If this isn't precisely how hackers think, a language designer would do well to act as if it were.
It is a mistake to try to baby the user with long-winded expressions that are meant to resemble English. Cobol is notorious for this flaw. A hacker would consider being asked to write
add x to y giving z
instead of
z = x+y
as something between an insult to his intelligence and a sin against God.
It has sometimes been said that Lisp should use first and rest instead of car and cdr, because it would make programs easier to read. Maybe for the first couple hours. But a hacker can learn quickly enough that car means the first element of a list and cdr means the rest. Using first and rest means 50% more typing. And they are also different lengths, meaning that the arguments won't line up when they're called, as car and cdr often are, in successive lines. I've found that it matters a lot how code lines up on the page. I can barely read Lisp code when it is set in a variable-width font, and friends say this is true for other languages too.
Brevity is one place where strongly typed languages lose. All other things being equal, no one wants to begin a program with a bunch of declarations. Anything that can be implicit, should be.
The individual tokens should be short as well. Perl and Common Lisp occupy opposite poles on this question. Perl programs can be almost cryptically dense, while the names of built-in Common Lisp operators are comically long. The designers of Common Lisp probably expected users to have text editors that would type these long names for them. But the cost of a long name is not just the cost of typing it. There is also the cost of reading it, and the cost of the space it takes up on your screen.
既然你能提供任何一门语言所需的三种东西——免费实现、一本书、以及可破解的东西——那么如何设计一门黑客会喜欢的语言呢?
黑客喜欢的一件事就是简洁。黑客很懒,就像数学家和现代主义建筑师那样懒:他们讨厌任何多余的东西。说一个即将写程序的 hacker 至少下意识地根据他必须输入的字符总数来决定使用哪种语言,这离真相并不远。如果这并非黑客的确切思维方式,语言设计者最好也假定如此。
试图用冗长的表达来讨好用户,使之类似英语,是一个错误。Cobol 因这个缺陷而臭名昭著。如果要求黑客写:
add x to y giving z
而不是:
z = x+y
他会认为这既是对他智力的侮辱,也是对上帝的冒犯。
有时人们说 Lisp 应该使用 first 和 rest 而不是 car 和 cdr,因为这样程序更容易阅读。也许在最初的几个小时如此。但黑客很快就能学会 car 表示列表的第一个元素,cdr 表示剩余部分。使用 first 和 rest 意味着多输入 50% 的字符。而且它们的长度不同,意味着在连续行中调用时(car 和 cdr 经常如此),参数无法对齐。我发现代码在页面上的对齐方式非常重要。当 Lisp 代码用等宽字体以外的字体显示时,我几乎无法阅读,朋友们说其他语言也是如此。
简洁是强类型语言的一个败笔。其他方面相同的情况下,没有人想用一堆声明来开始一个程序。任何可以隐式的东西,都应该是隐式的。
单独的标记也应该短。Perl 和 Common Lisp 在这个问题上处于对立两端。Perl 程序几乎可以做到密码般的密集,而 Common Lisp 内置操作符的名称却长得可笑。Common Lisp 的设计者大概期望用户有文本编辑器来帮他们键入这些长名字。但长名字的代价不仅仅是键入的代价。还有阅读的代价,以及它在屏幕上占用的空间。
There is one thing more important than brevity to a hacker: being able to do what you want. In the history of programming languages a surprising amount of effort has gone into preventing programmers from doing things considered to be improper. This is a dangerously presumptuous plan. How can the language designer know what the programmer is going to need to do? I think language designers would do better to consider their target user to be a genius who will need to do things they never anticipated, rather than a bumbler who needs to be protected from himself. The bumbler will shoot himself in the foot anyway. You may save him from referring to variables in another package, but you can't save him from writing a badly designed program to solve the wrong problem, and taking forever to do it.
Good programmers often want to do dangerous and unsavory things. By unsavory I mean things that go behind whatever semantic facade the language is trying to present: getting hold of the internal representation of some high-level abstraction, for example. Hackers like to hack, and hacking means getting inside things and second guessing the original designer.
Let yourself be second guessed. When you make any tool, people use it in ways you didn't intend, and this is especially true of a highly articulated tool like a programming language. Many a hacker will want to tweak your semantic model in a way that you never imagined. I say, let them; give the programmer access to as much internal stuff as you can without endangering runtime systems like the garbage collector.
In Common Lisp I have often wanted to iterate through the fields of a struct — to comb out references to a deleted object, for example, or find fields that are uninitialized. I know the structs are just vectors underneath. And yet I can't write a general purpose function that I can call on any struct. I can only access the fields by name, because that's what a struct is supposed to mean.
A hacker may only want to subvert the intended model of things once or twice in a big program. But what a difference it makes to be able to. And it may be more than a question of just solving a problem. There is a kind of pleasure here too. Hackers share the surgeon's secret pleasure in poking about in gross innards, the teenager's secret pleasure in popping zits. [2] For boys, at least, certain kinds of horrors are fascinating. Maxim magazine publishes an annual volume of photographs, containing a mix of pin-ups and grisly accidents. They know their audience.
Historically, Lisp has been good at letting hackers have their way. The political correctness of Common Lisp is an aberration. Early Lisps let you get your hands on everything. A good deal of that spirit is, fortunately, preserved in macros. What a wonderful thing, to be able to make arbitrary transformations on the source code.
Classic macros are a real hacker's tool — simple, powerful, and dangerous. It's so easy to understand what they do: you call a function on the macro's arguments, and whatever it returns gets inserted in place of the macro call. Hygienic macros embody the opposite principle. They try to protect you from understanding what they're doing. I have never heard hygienic macros explained in one sentence. And they are a classic example of the dangers of deciding what programmers are allowed to want. Hygienic macros are intended to protect me from variable capture, among other things, but variable capture is exactly what I want in some macros.
A really good language should be both clean and dirty: cleanly designed, with a small core of well understood and highly orthogonal operators, but dirty in the sense that it lets hackers have their way with it. C is like this. So were the early Lisps. A real hacker's language will always have a slightly raffish character.
A good programming language should have features that make the kind of people who use the phrase "software engineering" shake their heads disapprovingly. At the other end of the continuum are languages like Ada and Pascal, models of propriety that are good for teaching and not much else.
对黑客来说,有一件事比简洁更重要:能够为所欲为。在编程语言的历史上,有大量精力花在阻止程序员做被认为不恰当的事情上。这是一个危险且武断的计划。语言设计者如何知道程序员需要做什么?我认为语言设计者最好将目标用户视为一个天才,他需要做他们从未预料到的事情,而不是一个需要保护自己不受伤害的笨蛋。笨蛋无论如何都会搬起石头砸自己的脚。你也许能阻止他引用其他包中的变量,但无法阻止他编写一个设计糟糕的程序来解决错误的问题,并且花费无穷无尽的时间。
优秀的程序员经常想做危险且不光彩的事情。所谓不光彩,我指的是绕过语言试图呈现的任何语义表象:例如,获取某个高级抽象的内部表示。黑客喜欢动手,而动手意味着深入内部并质疑原始设计者。
允许自己被质疑。当你制造任何工具时,人们会以你未曾预料的方式使用它,对于编程语言这种高度关节化的工具尤其如此。许多黑客想要以你从未想象过的方式调整你的语义模型。我说,让他们去做;在不会危及运行时系统(如垃圾收集器)的前提下,尽可能让程序员访问内部内容。
在 Common Lisp 中,我经常想遍历一个结构体(struct)的字段——例如,清理对已删除对象的引用,或查找未初始化的字段。我知道结构体底层只是向量。然而我无法编写一个通用的函数来调用任何结构体。我只能通过名称访问字段,因为这就是结构体的本意。
黑客可能在整个大程序中只想颠覆一次或两次预期模型。但能够这样做带来的差别巨大。而且这可能不仅仅是解决问题的问题。其中还有一种乐趣。黑客分享着外科医生在肮脏内脏中探查的秘密乐趣,少年挤青春痘的秘密乐趣。[2]至少对男孩来说,某些恐怖的东西是迷人的。《Maxim》杂志每年出版一卷照片,混合了插页和恐怖事故。他们了解他们的读者。
历史上,Lisp 很擅长让黑客为所欲为。Common Lisp 的政治正确性是一种异常。早期的 Lisp 让你可以接触到一切。幸运的是,这种精神很大一部分保留在宏中。能够对源代码进行任意变换,这是多么美妙的事情。
经典宏是真正黑客的工具——简单、强大、危险。很容易理解它们的作用:你在宏的参数上调用一个函数,它返回的任何内容都会被插入到宏调用的位置。卫生宏则体现了相反的原则。它们试图保护你不去理解它们在做什么。我从未听说过卫生宏能用一句话解释清楚。它们是决定程序员可以想要什么这种危险方式的经典例子。卫生宏旨在保护我免受变量捕获等问题的影响,但在某些宏中,变量捕获正是我想要的。
一门真正优秀的语言应该既干净又肮脏:设计干净,有一个由理解透彻且高度正交的操作符组成的小核心,但肮脏在于它允许黑客随心所欲地使用它。C 就是这样。早期的 Lisp 也是如此。真正黑客的语言总会带有一点点放荡不羁的特质。
一门好的编程语言应该具有让那些使用“软件工程”这个词的人不以为然地摇头的特征。在这个连续体的另一端是 Ada 和 Pascal 这样的语言,它们是得体的典范,只适合教学,其他方面则无所作为。
To be attractive to hackers, a language must be good for writing the kinds of programs they want to write. And that means, perhaps surprisingly, that it has to be good for writing throwaway programs.
A throwaway program is a program you write quickly for some limited task: a program to automate some system administration task, or generate test data for a simulation, or convert data from one format to another. The surprising thing about throwaway programs is that, like the "temporary" buildings built at so many American universities during World War II, they often don't get thrown away. Many evolve into real programs, with real features and real users.
I have a hunch that the best big programs begin life this way, rather than being designed big from the start, like the Hoover Dam. It's terrifying to build something big from scratch. When people take on a project that's too big, they become overwhelmed. The project either gets bogged down, or the result is sterile and wooden: a shopping mall rather than a real downtown, Brasilia rather than Rome, Ada rather than C.
Another way to get a big program is to start with a throwaway program and keep improving it. This approach is less daunting, and the design of the program benefits from evolution. I think, if one looked, that this would turn out to be the way most big programs were developed. And those that did evolve this way are probably still written in whatever language they were first written in, because it's rare for a program to be ported, except for political reasons. And so, paradoxically, if you want to make a language that is used for big systems, you have to make it good for writing throwaway programs, because that's where big systems come from.
Perl is a striking example of this idea. It was not only designed for writing throwaway programs, but was pretty much a throwaway program itself. Perl began life as a collection of utilities for generating reports, and only evolved into a programming language as the throwaway programs people wrote in it grew larger. It was not until Perl 5 (if then) that the language was suitable for writing serious programs, and yet it was already massively popular.
What makes a language good for throwaway programs? To start with, it must be readily available. A throwaway program is something that you expect to write in an hour. So the language probably must already be installed on the computer you're using. It can't be something you have to install before you use it. It has to be there. C was there because it came with the operating system. Perl was there because it was originally a tool for system administrators, and yours had already installed it.
Being available means more than being installed, though. An interactive language, with a command-line interface, is more available than one that you have to compile and run separately. A popular programming language should be interactive, and start up fast.
Another thing you want in a throwaway program is brevity. Brevity is always attractive to hackers, and never more so than in a program they expect to turn out in an hour.
要吸引黑客,一门语言必须擅长编写他们想写的那种程序。这意味着,也许令人惊讶的是,它必须擅长编写一次性程序。
一次性程序是你为了某个有限任务快速编写的程序:一个自动化系统管理任务的程序,或为一个模拟生成测试数据,或将数据从一种格式转换为另一种格式。一次性程序令人惊讶之处在于,就像二战期间许多美国大学建造的“临时”建筑一样,它们通常不会被丢弃。许多会演变成真正的程序,拥有真正的功能和用户。
我直觉上认为,最好的大型程序都是以这种方式开始的,而不是从一开始就设计得像胡佛大坝那样庞大。从零开始建设庞大的东西是可怕的。当人们承担一个过于庞大的项目时,他们会不知所措。项目要么陷入停滞,要么结果僵化而无生气:一个购物中心而不是真正的市中心,巴西利亚而不是罗马,Ada 而不是 C。
获得大型程序的另一种方法是从一个一次性程序开始,并不断改进它。这种方法不那么令人生畏,而且程序的设计能从进化中获益。我认为,如果仔细审视,这会是大多数大型程序开发的方式。而那些以这种方式进化而来的程序,很可能仍然用它们最初编写的语言写成,因为程序很少被移植(除非出于政治原因)。因此,矛盾的是,如果你想制造一种用于大型系统的语言,你必须让它擅长编写一次性程序,因为大型系统正来源于此。
Perl 是这个想法的一个显著例子。它不仅是为编写一次性程序而设计的,而且它本身几乎就是一个一次性程序。Perl 最初是一组用于生成报告的实用工具,只有当人们用它所写的一次性程序变得越来越大时,它才演变成一种编程语言。直到 Perl 5(如果那时才算的话),该语言才适合编写严肃的程序,但那时它已经非常流行了。
是什么让一种语言适合一次性程序?首先,它必须随时可用。一次性程序是你期望在一小时内编写完成的东西。因此,该语言很可能必须已经安装在你正在使用的计算机上。它不能是你在使用前需要安装的东西。它必须就在那里。C 就在那里,因为它随操作系统一起提供。Perl 也在那里,因为它最初是为系统管理员设计的工具,你的系统管理员已经把它装好了。
不过,可用性不仅仅意味着已安装。具有命令行界面的交互式语言,比必须单独编译和运行的语言更可即用。流行的编程语言应该是交互式的,并且启动很快。
一次性程序需要的另一个特点是简洁。简洁总是吸引黑客,在他们期望一小时内完成的程序中尤其如此。
Of course the ultimate in brevity is to have the program already written for you, and merely to call it. And this brings us to what I think will be an increasingly important feature of programming languages: library functions. Perl wins because it has large libraries for manipulating strings. This class of library functions are especially important for throwaway programs, which are often originally written for converting or extracting data. Many Perl programs probably begin as just a couple library calls stuck together.
I think a lot of the advances that happen in programming languages in the next fifty years will have to do with library functions. I think future programming languages will have libraries that are as carefully designed as the core language. Programming language design will not be about whether to make your language strongly or weakly typed, or object oriented, or functional, or whatever, but about how to design great libraries. The kind of language designers who like to think about how to design type systems may shudder at this. It's almost like writing applications! Too bad. Languages are for programmers, and libraries are what programmers need.
It's hard to design good libraries. It's not simply a matter of writing a lot of code. Once the libraries get too big, it can sometimes take longer to find the function you need than to write the code yourself. Libraries need to be designed using a small set of orthogonal operators, just like the core language. It ought to be possible for the programmer to guess what library call will do what he needs.
Libraries are one place Common Lisp falls short. There are only rudimentary libraries for manipulating strings, and almost none for talking to the operating system. For historical reasons, Common Lisp tries to pretend that the OS doesn't exist. And because you can't talk to the OS, you're unlikely to be able to write a serious program using only the built-in operators in Common Lisp. You have to use some implementation-specific hacks as well, and in practice these tend not to give you everything you want. Hackers would think a lot more highly of Lisp if Common Lisp had powerful string libraries and good OS support.
当然,简洁的极致是程序已经为你写好了,你只需调用它。这引出了我认为编程语言未来会越来越重要的一个特性:库函数。Perl 之所以成功,是因为它有用于字符串操作的大型库。这类库函数对于一次性程序尤其重要,这些程序最初通常是为了转换或提取数据而写的。许多 Perl 程序可能只是一些库调用的组合。
我认为未来五十年编程语言的许多进步将与库函数有关。我认为未来的编程语言将拥有像核心语言一样精心设计的库。编程语言设计将不再围绕语言是强类型还是弱类型、面向对象还是函数式等,而是关于如何设计优秀的库。那些喜欢思考类型系统设计的语言设计者可能会对此不寒而栗。这几乎就像编写应用程序!可惜,语言是为程序员服务的,而程序员需要的是库。
设计好的库很难。这不仅仅是写很多代码的问题。一旦库变得太大,有时找到你需要的函数所花的时间比你自己写代码还要长。库需要使用一组小的正交操作符来设计,就像核心语言一样。程序员应该能够猜想出哪个库调用能满足他的需求。
库是 Common Lisp 的一个短板。它只有非常基础的字符串操作库,而且几乎没有与操作系统通信的库。由于历史原因,Common Lisp 试图假装操作系统不存在。因为你无法与操作系统对话,你不太可能仅使用 Common Lisp 的内置操作符来编写一个严肃的程序。你还必须借助一些特定实现的技巧,而在实践中,这些技巧往往不能提供你所需的一切。如果 Common Lisp 拥有强大的字符串库和良好的操作系统支持,黑客们会更高地评价 Lisp。
Could a language with Lisp's syntax, or more precisely, lack of syntax, ever become popular? I don't know the answer to this question. I do think that syntax is not the main reason Lisp isn't currently popular. Common Lisp has worse problems than unfamiliar syntax. I know several programmers who are comfortable with prefix syntax and yet use Perl by default, because it has powerful string libraries and can talk to the os.
There are two possible problems with prefix notation: that it is unfamiliar to programmers, and that it is not dense enough. The conventional wisdom in the Lisp world is that the first problem is the real one. I'm not so sure. Yes, prefix notation makes ordinary programmers panic. But I don't think ordinary programmers' opinions matter. Languages become popular or unpopular based on what expert hackers think of them, and I think expert hackers might be able to deal with prefix notation. Perl syntax can be pretty incomprehensible, but that has not stood in the way of Perl's popularity. If anything it may have helped foster a Perl cult.
A more serious problem is the diffuseness of prefix notation. For expert hackers, that really is a problem. No one wants to write (aref a x y) when they could write a[x,y].
In this particular case there is a way to finesse our way out of the problem. If we treat data structures as if they were functions on indexes, we could write (a x y) instead, which is even shorter than the Perl form. Similar tricks may shorten other types of expressions.
We can get rid of (or make optional) a lot of parentheses by making indentation significant. That's how programmers read code anyway: when indentation says one thing and delimiters say another, we go by the indentation. Treating indentation as significant would eliminate this common source of bugs as well as making programs shorter.
Sometimes infix syntax is easier to read. This is especially true for math expressions. I've used Lisp my whole programming life and I still don't find prefix math expressions natural. And yet it is convenient, especially when you're generating code, to have operators that take any number of arguments. So if we do have infix syntax, it should probably be implemented as some kind of read-macro.
I don't think we should be religiously opposed to introducing syntax into Lisp, as long as it translates in a well-understood way into underlying s-expressions. There is already a good deal of syntax in Lisp. It's not necessarily bad to introduce more, as long as no one is forced to use it. In Common Lisp, some delimiters are reserved for the language, suggesting that at least some of the designers intended to have more syntax in the future.
One of the most egregiously unlispy pieces of syntax in Common Lisp occurs in format strings; format is a language in its own right, and that language is not Lisp. If there were a plan for introducing more syntax into Lisp, format specifiers might be able to be included in it. It would be a good thing if macros could generate format specifiers the way they generate any other kind of code.
An eminent Lisp hacker told me that his copy of CLTL falls open to the section format. Mine too. This probably indicates room for improvement. It may also mean that programs do a lot of I/O.
一门具有 Lisp 式语法(更准确地说,缺乏语法)的语言,有可能变得流行吗?我不知道这个问题的答案。但我确实认为语法不是 Lisp 目前不流行的主要原因。Common Lisp 有比不熟悉的语法更糟糕的问题。我认识几个熟悉前缀语法的程序员,但他们默认使用 Perl,因为 Perl 有强大的字符串库并且能与操作系统通信。
前缀表示法有两个潜在问题:它对程序员来说不熟悉,并且不够密集。Lisp 界的传统观点认为第一个问题是真正的问题。我不太确定。是的,前缀表示法会让普通程序员恐慌。但我认为普通程序员的意见并不重要。语言的流行与否取决于专家黑客的看法,而我认为专家黑客可能能够处理前缀表示法。Perl 的语法可能相当难以理解,但这并未阻碍 Perl 的流行。如果说有什么影响的话,它反而可能助长了 Perl 的崇拜。
一个更严重的问题是前缀表示法的扩散性。对于专家黑客来说,这确实是个问题。没有人愿意写 (aref a x y) 当他们可以写 a[x,y] 的时候。
在这个特定情况下,有一种巧妙的方法可以解决问题。如果我们把数据结构视为索引上的函数,我们可以写成 (a x y),这甚至比 Perl 的形式更短。类似的技巧可以缩短其他类型的表达式。
我们可以通过使缩进具有意义来消除(或使之可选)很多括号。程序员就是这样阅读代码的:当缩进说一套而分隔符说另一套时,我们遵循缩进。将缩进视为有意义不仅可以消除这个常见的错误来源,还能使程序更短。
有时中缀语法更容易阅读。对于数学表达式尤其如此。我整个编程生涯都在使用 Lisp,但我仍然不觉得前缀数学表达式自然。然而,拥有可以接受任意数量参数的操作符是很方便的,尤其是在生成代码时。所以如果我们确实有中缀语法,它可能应该作为某种读宏(read-macro)来实现。
我认为我们不应该在宗教上反对向 Lisp 引入语法,只要它能以一种被充分理解的方式转化为底层的 s 表达式。Lisp 中已经有很多语法了。引入更多不一定是坏事,只要没有人被迫使用它。在 Common Lisp 中,一些分隔符是为语言保留的,这表明至少部分设计者曾打算在未来引入更多语法。
Common Lisp 中最不像 Lisp 的语法之一出现在 format 字符串中;format 本身是一门语言,而那门语言不是 Lisp。如果有一个向 Lisp 引入更多语法的计划,format 说明符应该可以包含在其中。如果宏能够像生成其他代码那样生成 format 说明符,那将是一件好事。
一位杰出的 Lisp 黑客告诉我,他那本 CLTL 总是自动翻到 format 章节。我的也是。这可能表明有改进空间。也可能意味着程序做了大量的 I/O。
A good language, as everyone knows, should generate fast code. But in practice I don't think fast code comes primarily from things you do in the design of the language. As Knuth pointed out long ago, speed only matters in certain critical bottlenecks. And as many programmers have observed since, one is very often mistaken about where these bottlenecks are.
So, in practice, the way to get fast code is to have a very good profiler, rather than by, say, making the language strongly typed. You don't need to know the type of every argument in every call in the program. You do need to be able to declare the types of arguments in the bottlenecks. And even more, you need to be able to find out where the bottlenecks are.
One complaint people have had with Lisp is that it's hard to tell what's expensive. This might be true. It might also be inevitable, if you want to have a very abstract language. And in any case I think good profiling would go a long way toward fixing the problem: you'd soon learn what was expensive.
Part of the problem here is social. Language designers like to write fast compilers. That's how they measure their skill. They think of the profiler as an add-on, at best. But in practice a good profiler may do more to improve the speed of actual programs written in the language than a compiler that generates fast code. Here, again, language designers are somewhat out of touch with their users. They do a really good job of solving slightly the wrong problem.
It might be a good idea to have an active profiler — to push performance data to the programmer instead of waiting for him to come asking for it. For example, the editor could display bottlenecks in red when the programmer edits the source code. Another approach would be to somehow represent what's happening in running programs. This would be an especially big win in server-based applications, where you have lots of running programs to look at. An active profiler could show graphically what's happening in memory as a program's running, or even make sounds that tell what's happening.
Sound is a good cue to problems. In one place I worked, we had a big board of dials showing what was happening to our web servers. The hands were moved by little servomotors that made a slight noise when they turned. I couldn't see the board from my desk, but I found that I could tell immediately, by the sound, when there was a problem with a server.
It might even be possible to write a profiler that would automatically detect inefficient algorithms. I would not be surprised if certain patterns of memory access turned out to be sure signs of bad algorithms. If there were a little guy running around inside the computer executing our programs, he would probably have as long and plaintive a tale to tell about his job as a federal government employee. I often have a feeling that I'm sending the processor on a lot of wild goose chases, but I've never had a good way to look at what it's doing.
A number of Lisps now compile into byte code, which is then executed by an interpreter. This is usually done to make the implementation easier to port, but it could be a useful language feature. It might be a good idea to make the byte code an official part of the language, and to allow programmers to use inline byte code in bottlenecks. Then such optimizations would be portable too.
The nature of speed, as perceived by the end-user, may be changing. With the rise of server-based applications, more and more programs may turn out to be i/o-bound. It will be worth making i/o fast. The language can help with straightforward measures like simple, fast, formatted output functions, and also with deep structural changes like caching and persistent objects.
Users are interested in response time. But another kind of efficiency will be increasingly important: the number of simultaneous users you can support per processor. Many of the interesting applications written in the near future will be server-based, and the number of users per server is the critical question for anyone hosting such applications. In the capital cost of a business offering a server-based application, this is the divisor.
For years, efficiency hasn't mattered much in most end-user applications. Developers have been able to assume that each user would have an increasingly powerful processor sitting on their desk. And by Parkinson's Law, software has expanded to use the resources available. That will change with server-based applications. In that world, the hardware and software will be supplied together. For companies that offer server-based applications, it will make a very big difference to the bottom line how many users they can support per server.
In some applications, the processor will be the limiting factor, and execution speed will be the most important thing to optimize. But often memory will be the limit; the number of simultaneous users will be determined by the amount of memory you need for each user's data. The language can help here too. Good support for threads will enable all the users to share a single heap. It may also help to have persistent objects and/or language level support for lazy loading.
众所周知,好的语言应该生成快速的代码。但实际上,我认为快速代码主要并非来自语言设计中的东西。正如 Knuth 很久以前指出的,速度只在某些关键瓶颈处才重要。而且正如许多程序员后来观察到的,人们常常错误地判断这些瓶颈在哪里。
因此,在实践中,获得快速代码的方法是拥有一个非常好的剖析器(profiler),而不是让语言变成强类型。你不需要知道程序中每次调用每个参数的类型。你只需要能够声明瓶颈处参数的类型。更重要的是,你需要能够找到瓶颈在哪里。
人们对 Lisp 的一个抱怨是,很难判断什么操作代价高昂。这可能属实。如果你想要一门非常抽象的语言,这也可能是不可避免的。无论如何,我认为良好的剖析对解决问题大有帮助:你很快就会知道什么昂贵。
这里部分问题在于社会因素。语言设计者喜欢编写快速的编译器。他们以此衡量自己的技能。他们最多将剖析器视为一个附加组件。但实际上,一个好的剖析器可能比生成快速代码的编译器更能提高用该语言编写的实际程序的速度。这里,语言设计者再次与用户有些脱节。他们很好地解决了一个略有偏差的问题。
拥有一个主动剖析器可能是一个好主意——将性能数据推送给程序员,而不是等着他来询问。例如,当程序员编辑源代码时,编辑器可以用红色显示瓶颈。另一种方法是设法表示正在运行的程序中发生了什么。这在服务器端应用程序中尤其有利,因为你有大量正在运行的程序需要观察。主动剖析器可以以图形方式显示程序运行时内存中的情况,甚至发出声音来告知正在发生的事情。
声音是发现问题的好线索。在我工作过的一个地方,我们有一个大仪表板,显示我们的 Web 服务器的情况。指针由小伺服电机驱动,转动时会发出轻微的声音。我在办公桌上看不到仪表板,但我发现通过声音可以立即判断出服务器何时出现问题。
甚至有可能编写一个自动检测低效算法的剖析器。如果某些内存访问模式被证明是糟糕算法的确切标志,我也不会感到惊讶。如果有一个小人在计算机内部跑来跑去执行我们的程序,他可能会像联邦政府雇员一样,有一长串关于他工作的辛酸故事。我经常感觉自己让处理器反复做无用功,但我从来没有一个好办法看看它在做什么。
许多 Lisp 实现现在编译成字节码,然后由解释器执行。这通常是为了使实现更容易移植,但它也可以成为一个有用的语言特性。将字节码作为语言的官方部分,并允许程序员在瓶颈处使用内联字节码,这可能是一个好主意。这样,这样的优化也将是可移植的。
最终用户感知到的速度的本质可能正在改变。随着基于服务器的应用程序的兴起,越来越多的程序可能变成 I/O 密集型。让 I/O 快起来是值得的。语言可以通过简单的措施(如简单、快速的格式化输出函数)以及深层的结构性改变(如缓存和持久化对象)来提供帮助。
用户关心响应时间。但另一种效率将变得越来越重要:每个处理器可以支持的并发用户数。在不久的将来,许多有趣的应用程序将是基于服务器的,对托管此类应用程序的任何人来说,每台服务器的用户数量是关键问题。在提供基于服务器的应用程序的企业的资本成本中,这是一个除数。
多年来,在大多数最终用户应用程序中,效率并不太重要。开发人员可以假设每个用户桌上都有一台越来越强大的处理器。根据帕金森定律,软件会膨胀以消耗可用资源。随着基于服务器的应用程序的出现,这种情况将改变。在那个世界里,硬件和软件将一起提供。对于提供基于服务器的应用程序的公司来说,每台服务器能支持多少用户将对底线产生巨大影响。
在某些应用中,处理器将成为限制因素,执行速度是最需要优化的。但通常内存是限制;并发用户数将由每个用户数据所需的内存量决定。语言在这方面也可以提供帮助。良好的线程支持将使所有用户共享一个堆。拥有持久化对象和/或语言级别的惰性加载支持也可能有所帮助。
The last ingredient a popular language needs is time. No one wants to write programs in a language that might go away, as so many programming languages do. So most hackers will tend to wait until a language has been around for a couple years before even considering using it.
Inventors of wonderful new things are often surprised to discover this, but you need time to get any message through to people. A friend of mine rarely does anything the first time someone asks him. He knows that people sometimes ask for things that they turn out not to want. To avoid wasting his time, he waits till the third or fourth time he's asked to do something; by then, whoever's asking him may be fairly annoyed, but at least they probably really do want whatever they're asking for.
Most people have learned to do a similar sort of filtering on new things they hear about. They don't even start paying attention until they've heard about something ten times. They're perfectly justified: the majority of hot new whatevers do turn out to be a waste of time, and eventually go away. By delaying learning VRML, I avoided having to learn it at all.
So anyone who invents something new has to expect to keep repeating their message for years before people will start to get it. We wrote what was, as far as I know, the first web-server based application, and it took us years to get it through to people that it didn't have to be downloaded. It wasn't that they were stupid. They just had us tuned out.
The good news is, simple repetition solves the problem. All you have to do is keep telling your story, and eventually people will start to hear. It's not when people notice you're there that they pay attention; it's when they notice you're still there.
It's just as well that it usually takes a while to gain momentum. Most technologies evolve a good deal even after they're first launched — programming languages especially. Nothing could be better, for a new techology, than a few years of being used only by a small number of early adopters. Early adopters are sophisticated and demanding, and quickly flush out whatever flaws remain in your technology. When you only have a few users you can be in close contact with all of them. And early adopters are forgiving when you improve your system, even if this causes some breakage.
There are two ways new technology gets introduced: the organic growth method, and the big bang method. The organic growth method is exemplified by the classic seat-of-the-pants underfunded garage startup. A couple guys, working in obscurity, develop some new technology. They launch it with no marketing and initially have only a few (fanatically devoted) users. They continue to improve the technology, and meanwhile their user base grows by word of mouth. Before they know it, they're big.
The other approach, the big bang method, is exemplified by the VC-backed, heavily marketed startup. They rush to develop a product, launch it with great publicity, and immediately (they hope) have a large user base.
Generally, the garage guys envy the big bang guys. The big bang guys are smooth and confident and respected by the VCs. They can afford the best of everything, and the PR campaign surrounding the launch has the side effect of making them celebrities. The organic growth guys, sitting in their garage, feel poor and unloved. And yet I think they are often mistaken to feel sorry for themselves. Organic growth seems to yield better technology and richer founders than the big bang method. If you look at the dominant technologies today, you'll find that most of them grew organically.
This pattern doesn't only apply to companies. You see it in sponsored research too. Multics and Common Lisp were big-bang projects, and Unix and MacLisp were organic growth projects.
一门流行语言需要的最后一个要素是时间。没有人愿意用一门可能会消失的语言编写程序,而很多编程语言正是如此。因此,大多数黑客倾向于等待一门语言存在几年之后,才开始考虑使用它。
美妙新事物的发明者常常惊讶地发现这一点,但你需要时间才能让人们接收到你的信息。我有一个朋友很少在别人第一次请求他时就去做某事。他知道人们有时会要求一些他们后来发现自己并不想要的东西。为了避免浪费时间,他会等到被请求第三或第四次才去做;到那时,请求他的人可能已经相当恼火,但至少他们可能真的想要他们请求的东西。
大多数人都学会了对自己听到的新事物进行类似的过滤。他们直到听说某件事十次之后才开始关注。他们完全有理由这样做:大多数热门的新事物最终都被证明是浪费时间,并最终消失。通过推迟学习 VRML,我完全避免了学习它的必要。
因此,任何发明新东西的人都必须准备好重复他们的信息多年,人们才会开始理解。我们编写了据我所知第一个基于 Web 服务器的应用程序,我们花了数年时间才让人们明白它不需要下载。不是因为他们笨,只是他们把我们屏蔽了。
好消息是,简单的重复解决了问题。你所要做的就是反复讲述你的故事,最终人们会开始听到。人们不是在注意到你存在的时候才关注你;而是在注意到你仍然存在的时候。
通常需要一段时间才能获得动力,这也未尝不是好事。大多数技术即使在首次发布后也会有相当大的发展——编程语言尤其如此。对于一项新技术来说,没有什么比最初几年只被少数早期采用者使用更好的了。早期采用者经验丰富且要求严格,能迅速暴露你技术中的任何缺陷。当你只有少数用户时,你可以与他们所有人保持密切联系。即使你改进系统导致了一些破坏,早期采用者也会宽容。
新技术有两种引入方式:有机增长法和大爆炸法。有机增长法的典型例子是经典的临时抱佛脚、资金不足的车库创业公司。几个家伙默默无闻地开发新技术。他们在没有营销的情况下推出,最初只有少数(狂热忠诚的)用户。他们继续改进技术,同时用户群通过口口相传增长。不知不觉,他们就做大了。
另一种方法,大爆炸法,以风投支持、大力营销的创业公司为代表。他们匆忙开发产品,大肆宣传推出,并(希望)立即拥有庞大的用户群。
通常,车库里的家伙羡慕大爆炸的家伙。大爆炸的家伙光鲜自信,受到风投的尊重。他们能负担最好的东西,发布时的公关活动也让他们成为名人。有机增长型的家伙坐在车库里,感到贫穷和不被爱。但我认为他们常常错误地自怜。有机增长似乎比大爆炸法能产生更好的技术和更富有的创始人。看看今天的主流技术,你会发现大多数都是有机增长起来的。
这种模式不仅适用于公司。在有赞助的研究中也能看到。Multics 和 Common Lisp 是大爆炸项目,而 Unix 和 MacLisp 是有机增长项目。
"The best writing is rewriting," wrote E. B. White. Every good writer knows this, and it's true for software too. The most important part of design is redesign. Programming languages, especially, don't get redesigned enough.
To write good software you must simultaneously keep two opposing ideas in your head. You need the young hacker's naive faith in his abilities, and at the same time the veteran's skepticism. You have to be able to think how hard can it be? with one half of your brain while thinking it will never work with the other.
The trick is to realize that there's no real contradiction here. You want to be optimistic and skeptical about two different things. You have to be optimistic about the possibility of solving the problem, but skeptical about the value of whatever solution you've got so far.
People who do good work often think that whatever they're working on is no good. Others see what they've done and are full of wonder, but the creator is full of worry. This pattern is no coincidence: it is the worry that made the work good.
If you can keep hope and worry balanced, they will drive a project forward the same way your two legs drive a bicycle forward. In the first phase of the two-cycle innovation engine, you work furiously on some problem, inspired by your confidence that you'll be able to solve it. In the second phase, you look at what you've done in the cold light of morning, and see all its flaws very clearly. But as long as your critical spirit doesn't outweigh your hope, you'll be able to look at your admittedly incomplete system, and think, how hard can it be to get the rest of the way?, thereby continuing the cycle.
It's tricky to keep the two forces balanced. In young hackers, optimism predominates. They produce something, are convinced it's great, and never improve it. In old hackers, skepticism predominates, and they won't even dare to take on ambitious projects.
Anything you can do to keep the redesign cycle going is good. Prose can be rewritten over and over until you're happy with it. But software, as a rule, doesn't get redesigned enough. Prose has readers, but software has users. If a writer rewrites an essay, people who read the old version are unlikely to complain that their thoughts have been broken by some newly introduced incompatibility.
Users are a double-edged sword. They can help you improve your language, but they can also deter you from improving it. So choose your users carefully, and be slow to grow their number. Having users is like optimization: the wise course is to delay it. Also, as a general rule, you can at any given time get away with changing more than you think. Introducing change is like pulling off a bandage: the pain is a memory almost as soon as you feel it.
Everyone knows that it's not a good idea to have a language designed by a committee. Committees yield bad design. But I think the worst danger of committees is that they interfere with redesign. It is so much work to introduce changes that no one wants to bother. Whatever a committee decides tends to stay that way, even if most of the members don't like it.
Even a committee of two gets in the way of redesign. This happens particularly in the interfaces between pieces of software written by two different people. To change the interface both have to agree to change it at once. And so interfaces tend not to change at all, which is a problem because they tend to be one of the most ad hoc parts of any system.
One solution here might be to design systems so that interfaces are horizontal instead of vertical — so that modules are always vertically stacked strata of abstraction. Then the interface will tend to be owned by one of them. The lower of two levels will either be a language in which the upper is written, in which case the lower level will own the interface, or it will be a slave, in which case the interface can be dictated by the upper level.
“最好的写作就是重写,”E. B. White 写道。每一位好作家都知道这一点,软件也是如此。设计最重要的部分是重新设计。编程语言尤其没有得到足够的重新设计。
要写出好的软件,你必须在脑中同时保持两种相反的想法。你需要年轻黑客对自己能力的天真信心,同时也要有老手的怀疑。你必须能用一半的大脑想“能有多难?”,同时用另一半想“这永远行不通”。
诀窍在于认识到这里并没有真正的矛盾。你需要在两件不同的事情上分别保持乐观和怀疑。你必须对解决问题的可能性保持乐观,但对目前所得到的任何解决方案的价值保持怀疑。
做出好作品的人通常认为他们正在做的东西并不好。别人看到他们的成果感到惊叹,但创作者却充满担忧。这种模式并非巧合:正是担忧让作品变得优秀。
如果你能让希望和担忧保持平衡,它们会像两条腿驱动自行车一样推动项目前进。在双周期创新引擎的第一阶段,你充满信心地以为能解决问题,从而狂热地工作。在第二阶段,你用清晨的冷静审视自己的成果,清晰地看到所有缺陷。但只要你的批判精神没有压过希望,你就能看着尚不完善的系统,想“完成剩下的部分能有多难?”,从而继续循环。
保持两股力量平衡很棘手。年轻的黑客乐观占主导,他们做出东西,坚信它很棒,从不改进。年长的黑客怀疑占主导,他们甚至不敢接手雄心勃勃的项目。
任何能维持重新设计循环的事情都是好的。散文可以反复重写直到满意。但软件通常没有得到足够的重新设计。散文有读者,但软件有用户。如果作者重写一篇文章,读过旧版本的人不太可能抱怨他们的思路被新引入的不兼容性打乱了。
用户是一把双刃剑。他们可以帮助你改进语言,但也会阻止你改进。所以要谨慎选择用户,缓慢增加用户数量。拥有用户就像优化:明智的做法是推迟。此外,一般来说,在任何时候你都能比你以为的做更多改变。引入改变就像撕创可贴:疼痛几乎一感觉到就变成了回忆。
众所周知,由委员会设计语言不是好主意。委员会产生糟糕的设计。但我认为委员会最大的危险是它们妨碍了重新设计。引入改变需要做大量的工作,以至于没人愿意费心。委员会决定的任何事情都倾向于保持不变,即使大多数成员也不喜欢。
即使是两人委员会也会妨碍重新设计。这在两个不同的人编写的软件之间的接口上尤其明显。要改变接口,两人必须同时同意改变。因此接口倾向于根本不改变,而问题在于它们往往是任何系统中最临时的部分之一。
一个解决方案可能是将系统设计为水平接口而非垂直接口——这样模块总是垂直堆叠的抽象层。然后接口往往会由其中一方拥有。两个层级中较低的那个,要么是编写较高层级的语言(此时低层级拥有接口),要么是奴隶(此时接口可由高层级决定)。
What all this implies is that there is hope for a new Lisp. There is hope for any language that gives hackers what they want, including Lisp. I think we may have made a mistake in thinking that hackers are turned off by Lisp's strangeness. This comforting illusion may have prevented us from seeing the real problem with Lisp, or at least Common Lisp, which is that it sucks for doing what hackers want to do. A hacker's language needs powerful libraries and something to hack. Common Lisp has neither. A hacker's language is terse and hackable. Common Lisp is not.
The good news is, it's not Lisp that sucks, but Common Lisp. If we can develop a new Lisp that is a real hacker's language, I think hackers will use it. They will use whatever language does the job. All we have to do is make sure this new Lisp does some important job better than other languages.
History offers some encouragement. Over time, successive new programming languages have taken more and more features from Lisp. There is no longer much left to copy before the language you've made is Lisp. The latest hot language, Python, is a watered-down Lisp with infix syntax and no macros. A new Lisp would be a natural step in this progression.
I sometimes think that it would be a good marketing trick to call it an improved version of Python. That sounds hipper than Lisp. To many people, Lisp is a slow AI language with a lot of parentheses. Fritz Kunze's official biography carefully avoids mentioning the L-word. But my guess is that we shouldn't be afraid to call the new Lisp Lisp. Lisp still has a lot of latent respect among the very best hackers — the ones who took 6.001 and understood it, for example. And those are the users you need to win.
In "How to Become a Hacker," Eric Raymond describes Lisp as something like Latin or Greek — a language you should learn as an intellectual exercise, even though you won't actually use it:
Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.
If I didn't know Lisp, reading this would set me asking questions. A language that would make me a better programmer, if it means anything at all, means a language that would be better for programming. And that is in fact the implication of what Eric is saying.As long as that idea is still floating around, I think hackers will be receptive enough to a new Lisp, even if it is called Lisp. But this Lisp must be a hacker's language, like the classic Lisps of the 1970s. It must be terse, simple, and hackable. And it must have powerful libraries for doing what hackers want to do now.
In the matter of libraries I think there is room to beat languages like Perl and Python at their own game. A lot of the new applications that will need to be written in the coming years will be server-based applications. There's no reason a new Lisp shouldn't have string libraries as good as Perl, and if this new Lisp also had powerful libraries for server-based applications, it could be very popular. Real hackers won't turn up their noses at a new tool that will let them solve hard problems with a few library calls. Remember, hackers are lazy.
It could be an even bigger win to have core language support for server-based applications. For example, explicit support for programs with multiple users, or data ownership at the level of type tags.
Server-based applications also give us the answer to the question of what this new Lisp will be used to hack. It would not hurt to make Lisp better as a scripting language for Unix. (It would be hard to make it worse.) But I think there are areas where existing languages would be easier to beat. I think it might be better to follow the model of Tcl, and supply the Lisp together with a complete system for supporting server-based applications. Lisp is a natural fit for server-based applications. Lexical closures provide a way to get the effect of subroutines when the ui is just a series of web pages. S-expressions map nicely onto html, and macros are good at generating it. There need to be better tools for writing server-based applications, and there needs to be a new Lisp, and the two would work very well together.
所有这些意味着新 Lisp 是有希望的。任何能给黑客所需的东西的语言都有希望,包括 Lisp。我认为我们可能犯了一个错误,以为黑客是被 Lisp 的陌生感所排斥。这种令人安慰的错觉可能阻碍了我们看到 Lisp(至少是 Common Lisp)的真正问题,那就是它对于黑客想做的事情来说很糟糕。黑客的语言需要强大的库和可破解的东西,Common Lisp 两者皆无。黑客的语言简洁且可破解,Common Lisp 不是。
好消息是,糟糕的不是 Lisp,而是 Common Lisp。如果我们能开发一种新的、真正黑客语言的 Lisp,我认为黑客会使用它。他们会使用任何能完成工作的语言。我们要做的就是确保这个新 Lisp 能比别的语言更好地完成某个重要工作。
历史提供了鼓励。随着时间的推移,不断出现的新编程语言从 Lisp 中汲取了越来越多的特性。在最终造出的语言成为 Lisp 之前,已经没有太多可复制的了。最新的热门语言 Python 是一种弱化的 Lisp,带有中缀语法但没有宏。新 Lisp 将是这一进程中的自然一步。
我有时认为,把它称为 Python 的改进版不失为一个好的营销策略。这听起来比 Lisp 更时髦。对许多人来说,Lisp 是一种缓慢的 AI 语言,括号很多。Fritz Kunze 的官方传记小心翼翼地避免提到 L 开头的词。但我的猜测是,我们不应该害怕把新 Lisp 称为 Lisp。Lisp 在最优秀的黑客中仍然拥有大量潜在的尊重——例如那些学过 6.001 并理解它的人。而这些正是你需要赢得的用户。
在《如何成为黑客》中,Eric Raymond 将 Lisp 描述为类似拉丁语或希腊语——一种你应该作为智力练习而学习的语言,即使你实际上不会使用它:
学习 Lisp 是值得的,因为当你最终掌握它时,你会获得深刻的启蒙体验;这种体验会让你在余生中成为更好的程序员,即使你实际上并不大量使用 Lisp 本身。
如果我不懂 Lisp,读到这里会让我产生疑问。一种能让我成为更好程序员的语言,如果它有任何意义,就意味着一种更适合编程的语言。而这正是 Eric 的话暗示的。只要这个想法还在流传,我认为黑客会对新 Lisp 足够接受,即使它被称为 Lisp。但这个 Lisp 必须是黑客的语言,像 1970 年代的经典 Lisp 一样。它必须简洁、简单、可破解。并且必须有强大的库来做黑客现在想做的事情。
在库的问题上,我认为有空间在 Perl 和 Python 自己的游戏中击败它们。未来几年需要编写的许多新应用将是基于服务器的。新的 Lisp 没有理由不能拥有像 Perl 那样好的字符串库,如果这个新 Lisp 还有用于服务器端应用的强大库,它可能会非常流行。真正的黑客不会鄙视一个能让他们通过几个库调用解决难题的新工具。记住,黑客很懒。
在核心语言层面支持服务器端应用可能会带来更大的胜利。例如,显式支持多用户程序,或在类型标签级别上的数据所有权。
服务器端应用也给了我们答案:这个新 Lisp 将用来破解什么。让 Lisp 更好地成为 Unix 的脚本语言没有坏处。(很难做得更差了。)但我认为有一些领域更容易击败现有语言。我认为最好遵循 Tcl 的模式,将 Lisp 与一套完整的服务器端应用支持系统一起提供。Lisp 天然适合服务器端应用。当用户界面只是一系列网页时,词法闭包提供了一种获得子例程效果的方法。S 表达式很好地映射到 HTML,宏擅长生成它。需要有更好的工具来编写服务器端应用,也需要有新 Lisp,两者将很好地协同工作。
By way of summary, let's try describing the hacker's dream language. The dream language is beautiful, clean, and terse. It has an interactive toplevel that starts up fast. You can write programs to solve common problems with very little code. Nearly all the code in any program you write is code that's specific to your application. Everything else has been done for you.
The syntax of the language is brief to a fault. You never have to type an unnecessary character, or even to use the shift key much.
Using big abstractions you can write the first version of a program very quickly. Later, when you want to optimize, there's a really good profiler that tells you where to focus your attention. You can make inner loops blindingly fast, even writing inline byte code if you need to.
There are lots of good examples to learn from, and the language is intuitive enough that you can learn how to use it from examples in a couple minutes. You don't need to look in the manual much. The manual is thin, and has few warnings and qualifications.
The language has a small core, and powerful, highly orthogonal libraries that are as carefully designed as the core language. The libraries all work well together; everything in the language fits together like the parts in a fine camera. Nothing is deprecated, or retained for compatibility. The source code of all the libraries is readily available. It's easy to talk to the operating system and to applications written in other languages.
The language is built in layers. The higher-level abstractions are built in a very transparent way out of lower-level abstractions, which you can get hold of if you want.
Nothing is hidden from you that doesn't absolutely have to be. The language offers abstractions only as a way of saving you work, rather than as a way of telling you what to do. In fact, the language encourages you to be an equal participant in its design. You can change everything about it, including even its syntax, and anything you write has, as much as possible, the same status as what comes predefined.
总结一下,让我们尝试描述黑客的梦想语言。梦想语言是美丽、干净、简洁的。它有一个启动快速的交互式顶层。你可以用很少的代码编写程序来解决常见问题。你编写的任何程序中几乎所有代码都是针对你的应用的。其他一切都已为你做好。
语言的语法简洁到极致。你永远不需要键入任何不必要的字符,甚至不需要频繁使用 Shift 键。
使用大的抽象,你可以非常快速地编写程序的第一个版本。之后,当你想要优化时,会有一个非常好的剖析器告诉你关注点在哪里。你可以让内循环快得惊人,甚至可以在需要时编写内联字节码。
有大量好的示例可供学习,而且语言足够直观,你可以在几分钟内通过示例学会如何使用它。你不需要经常查阅手册。手册很薄,警告和限定条件很少。
语言有一个小核心,以及强大、高度正交的库,这些库像核心语言一样精心设计。所有库都能很好地协同工作;语言中的一切都像精致相机中的部件一样契合。没有什么是弃用的,也没有为了兼容性而保留的。所有库的源代码都易于获取。与操作系统和用其他语言编写的应用程序通信很容易。
语言是分层构建的。高层抽象以非常透明的方式构建在低层抽象之上,如果你需要,可以接触到这些低层抽象。
没有什么是绝对有必要隐藏的。语言提供抽象仅仅是为了节省你的工作,而不是为了告诉你该做什么。事实上,语言鼓励你成为其设计的平等参与者。你可以改变关于它的任何东西,甚至包括其语法,你编写的任何东西都尽可能地与预定义的东西拥有相同的地位。
[1] Macros very close to the modern idea were proposed by Timothy Hart in 1964, two years after Lisp 1.5 was released. What was missing, initially, were ways to avoid variable capture and multiple evaluation; Hart's examples are subject to both.
[2] In When the Air Hits Your Brain, neurosurgeon Frank Vertosick recounts a conversation in which his chief resident, Gary, talks about the difference between surgeons and internists ("fleas"):
Gary and I ordered a large pizza and found an open booth. The chief lit a cigarette. "Look at those goddamn fleas, jabbering about some disease they'll see once in their lifetimes. That's the trouble with fleas, they only like the bizarre stuff. They hate their bread and butter cases. That's the difference between us and the fucking fleas. See, we love big juicy lumbar disc herniations, but they hate hypertension...."
It's hard to think of a lumbar disc herniation as juicy (except literally). And yet I think I know what they mean. I've often had a juicy bug to track down. Someone who's not a programmer would find it hard to imagine that there could be pleasure in a bug. Surely it's better if everything just works. In one way, it is. And yet there is undeniably a grim satisfaction in hunting down certain sorts of bugs.
[1] 非常接近现代思想的宏由 Timothy Hart 于 1964 年提出,即 Lisp 1.5 发布两年后。最初缺少的是避免变量捕获和多重求值的方法;Hart 的例子两者都有。
[2] 在《当空气击中你的大脑》一书中,神经外科医生 Frank Vertosick 讲述了他与主治医师 Gary 的一次对话,其中 Gary 谈到了外科医生和内科医生("跳蚤")的区别:
Gary 和我点了一个大披萨,找了个空卡座。主治医师点燃一支烟。“看看那些该死的跳蚤,叽叽喳喳地谈论某种他们一辈子只会见一次的疾病。这就是跳蚤的问题,他们只喜欢稀奇古怪的东西。他们讨厌自己的面包和黄油案例。这就是我们和那些他妈的跳蚤的区别。你看,我们喜欢多汁的腰椎间盘突出,但他们讨厌高血压....”
很难把腰椎间盘突出想象成多汁的(除了字面意思)。但我认为我理解他们的意思。我经常遇到一个多汁的 bug 需要追踪。不是程序员的人很难想象 bug 中会有乐趣。当然,如果一切正常更好。从一方面说,确实如此。然而,在追捕某些类型的 bug 时,确实有一种冷酷的满足感。