Translated on 2016-11-25 by Google Neuralnet Machine Translation
Lisp in Web-Based Applications
WebベースのアプリケーションにおけるLisp
Paul Graham
ポールグラハム
(This is an excerpt of a talk given at BBN Labs in Cambridge, MA, in April 2001.)
(これは2001年4月、ケンブリッジのBBN Labsで講演された講演の抜粋です)
Any Language You Want
あなたが望むすべての言語
One of the reasons to use Lisp in writing Web-based applications is that you can use Lisp. When you’re writing software that is only going to run on your own servers, you can use whatever language you want.
Webベースのアプリケーションを書く際にLispを使う理由の1つは、Lispを使うことができるということです。 あなた自身のサーバー上で実行されるだけのソフトウェアを書くときは、どんな言語でも使えます。
For a long time programmers didn’t have a lot of choice about what language to use for writing application programs. Until recently, writing application programs meant writing software to run on desktop computers. In desktop software there was a strong bias toward writing the application in the same language as the operating system. Ten years ago, for all practical purposes, applications were written in C.
長い間、プログラマーは、アプリケーションプログラムの作成にどの言語を使用するかについて多くの選択肢がありませんでした。 最近まで、アプリケーションプログラムを書くことは、デスクトップコンピュータ上で実行するソフトウェアを書くことを意味していました。 デスクトップソフトウェアでは、オペレーティングシステムと同じ言語でアプリケーションを書くことに強いバイアスがありました。 10年前、すべての実用的な目的のために、アプリケーションはC言語で書かれました。
With Web-based applications, that changes. You control the servers, and you can write your software in any language you want. You can take it for granted now that you have the source code of both your operating system and your compilers. If there does turn out to be any kind of problem between the language and the OS, you can fix it yourself.
Webベースのアプリケーションでは、それが変化します。 あなたはサーバーを制御し、必要な言語でソフトウェアを書くことができます。 あなたはあなたのオペレーティングシステムとコンパイラの両方のソースコードを手に入れたので、それは当然のことです。 言語とOSの間に何らかの問題が発生した場合は、自分で修正することができます。
This new freedom is a double-edged sword, however. Having more choices means that you now have to think about which choice to make. It was easier in the old days. If you were in charge of a software project, and some troublesome person suggested writing the software in a different language from whatever you usually used, you could just tell them that it would be impractical, and that would be the end of it.
しかし、この新しい自由は両刃の剣です。 より多くの選択肢があるということは、あなたが今どの選択をするかについて考える必要があることを意味します。 それは昔のほうが簡単でした。 あなたがソフトウェアプロジェクトを担当していて、面倒な人の中には、通常使用している言語とは異なる言語でソフトウェアを書くことが提案されていれば、実用的ではないと言えるかもしれません。
Now, with server-based applications, everything is changed. You’re now subject to market forces in what language you choose. If you try to pretend that nothing has changed, and just use C and C++, like most of our competitors did, you are setting yourself up for a fall. A little startup using a more powerful language will eat your lunch.
現在、サーバーベースのアプリケーションでは、すべてが変更されています。 あなたは現在、あなたが選んだ言語で市場の力に服しています。 何も変わっていないと思って、CやC ++を使うだけなら、ほとんどの競合他社と同じように、 あなたは秋のために自分自身を設定しています。 より強力な言語を使用して少しスタートアップはあなたのランチを食べるでしょう。
Incremental Development
インクリメンタル開発
There is a certain style of software development associated with Lisp. One of its traditions is incremental development: you start by writing, as quickly as possible, a program that does almost nothing. Then you gradually add features to it, but at every step you have working code.
Lispにはある種のソフトウェア開発スタイルがあります。 その伝統の一つは、漸進的な開発です。あなたはできるだけ早く、ほとんど何もしないプログラムを書くことから始まります。 その後徐々に機能を追加しますが、すべてのステップで作業コードがあります。
I think this way you get better software, written faster. Everything about Lisp is tuned to this style of programming, because Lisp programmers have worked this way for at least thirty years.
私はあなたがより速く書かれたより良いソフトウェアを得るこの方法だと思います。 Lispのプログラマは少なくとも30年間この方法で作業してきたので、Lispに関するすべてのことがこのスタイルのプログラミングに調整されています。
The Viaweb editor must be one of the most extreme cases of incremental development. It began with a 120-line program for generating Web sites that I had used in an example in a book that I finished just before we started Viaweb. The Viaweb editor, which eventually grew to be about 25,000 lines of code, grew incrementally from this program. I never once sat down and rewrote the whole thing. I don’t think I was ever more than a day or two without running code. The whole development process was one long series of gradual changes.
Viawebエディタは、インクリメンタル開発の最も極端なケースの1つでなければなりません。 それは、私がViawebを始める直前に完成した本の例で使ったWebサイトを生成するための120行のプログラムから始まりました。 最終的に約25,000行に拡大したViawebエディタは、このプログラムから段階的に成長しました。 私は一度座って全体を書き直したことはありません。 私はコードを実行していない1日か2日以上はなかったと思います。 全体の開発プロセスは、徐々に変化する長い一連のものでした。
This style of development fits well with the rolling releases that are possible with Web-based software. It’s also a faster way to get software written generally.
このような開発スタイルは、Webベースのソフトウェアで可能なローリングリリースに適しています。 また、ソフトウェアを一般的に書くためのより速い方法です。
Interactive Toplevel
インタラクティブトップレベル
Lisp’s interactive toplevel is a great help in developing software rapidly. But the biggest advantage for us was probably in finding bugs. As I mentioned before, with Web-based applications you have the users’ data on your servers and can usually reproduce bugs.
Lispのインタラクティブなトップレベルは、ソフトウェアを急速に開発する大きな助けとなります。 しかし、私たちの最大のメリットはおそらくバグを発見することでした。 先に述べたように、Webベースのアプリケーションでは、サーバー上にユーザーのデータがあり、通常はバグを再現できます。
When one of the customer support people came to me with a report of a bug in the editor, I would load the code into the Lisp interpreter and log into the user’s account. If I was able to reproduce the bug I’d get an actual break loop, telling me exactly what was going wrong. Often I could fix the code and release a fix right away. And when I say right away, I mean while the user was still on the phone.
顧客サポート担当者の1人がエディタでバグの報告を受けて私に来たとき、私はそのコードをLispインタプリタにロードし、ユーザのアカウントにログインします。 バグを再現できたら、実際のブレークループが発生し、何がうまくいかないのか正確に教えてくれます。 しばしば、コードを修正して直ちに修正をリリースすることができました。 私がすぐに言うとき、私はユーザーがまだ電話にいた間に意味する。
Such fast turnaround on bug fixes put us into an impossibly tempting position. If we could catch and fix a bug while the user was still on the phone, it was very tempting for us to give the user the impression that they were imagining it. And so we sometimes (to their delight) had the customer support people tell the user to just try logging in again and see if they still had the problem. And of course when the user logged back in they’d get the newly released version of the software with the bug fixed, and everything would work fine. I realize this was a bit sneaky of us, but it was also a lot of fun.
このようなバグ修正の迅速な回帰は、私たちを非常に魅力的な立場に陥りました。 ユーザーがまだ電話をかけている間にバグを見つけて修正できれば、ユーザーに想像しているような印象を与えることが非常に魅力的でした。 そして、時には(喜んで)顧客サポート担当者がユーザーに再度ログインしても、まだ問題があるかどうかを確認するように指示することがありました。 もちろん、ユーザーがログインしたときに、バグを修正したばかりの新しくリリースされたバージョンのソフトウェアを入手すると、すべて正常に動作します。 私はこれが私たちのちょっと卑劣だったことを理解していますが、それはまたとても楽しいものでした。
Macros for Html
HTML用マクロ
Lisp macros were another big win for us. We used them very extensively in the Viaweb editor. It could accurately be described as one big macro. And that gives you an idea of how much we depended on Lisp, because no other language has macros in the sense that Lisp does.
Lispマクロは私たちのもう一つの大きな勝利でした。 私たちはViawebエディタで非常に幅広く使用しました。 それは正確に1つの大きなマクロとして記述することができます。 そしてそれはLispにどれくらい依存しているのかを知ることができます。なぜなら、他の言語にはLispの意味でのマクロがないからです。
One way we used macros was to generate Html. There is a very natural fit between macros and Html, because Html is a prefix notation like Lisp, and Html is recursive like Lisp. So we had macro calls within macro calls, generating the most complicated Html, and it was all still very manageable.
マクロを使用する一つの方法は、Htmlを生成することでした。 HtmlはLispのような接頭辞表記であり、HtmlはLispのように再帰的であるので、マクロとHtmlの間には非常に自然な適合があります。 だから私たちはマクロ呼び出しの中でマクロ呼び出しを行い、最も複雑なHTMLを生成しました。
Embedded Languages
埋め込み言語
Another big use for macros was the embedded language we had for describing pages, called Rtml. (We made up various explanations for what Rtml was supposed to stand for, but actually I named it after Robert Morris, the other founder of Viaweb, whose username is Rtm.)
マクロのもう1つの大きな用途は、Rtmlというページを記述するための埋め込み言語でした。 (私たちは、Rtmlが何を意味するかについて様々な説明をしましたが、実際にはユーザー名がRtmのViawebの創設者であるRobert Morrisの名前を付けました)。
Every page made by our software was generated by a program written in Rtml. We called these programs templates to make them less frightening, but they were real programs. In fact, they were Lisp programs. Rtml was a combination of macros and the built-in Lisp operators.
私たちのソフトウェアによって作られたすべてのページは、Rtmlで書かれたプログラムによって生成されました。 私たちはこれらのプログラムテンプレートを、それほど恐ろしいものにしないように呼びましたが、実際のプログラムでした。 実際、彼らはLispプログラムでした。 Rtmlはマクロと組み込みのLisp演算子の組み合わせです。
Users could write their own Rtml templates to describe what they wanted their pages to look like. We had a structure editor for manipulating these templates, a lot like the structure editor they had in Interlisp. Instead of typing free-form text, you cut and pasted bits of code together. This meant that it was impossible to get syntax errors. It also meant that we didn’t have to display the parentheses in the underlying s-expressions: we could show structure by indentation. By this means we made the language look a lot less threatening.
ユーザーは自分のページをどのように見せたいかを記述する独自のRtmlテンプレートを書くことができます。 これらのテンプレートを操作するための構造エディタがありました.Interlispで使用していた構造エディタとよく似ています。 自由形式のテキストを入力するのではなく、複数のコードをカットアンドペーストします。 これは、構文エラーを得ることが不可能であることを意味しました。 また、基礎となるs式にかっこを表示する必要がないことも意味しました。字下げによって構造を表示することができました。 これにより、私たちは言語の見た目をあまり脅かさないようにしました。
We also designed Rtml so that there could be no errors at runtime: every Rtml program yielded some kind of Web page, and you could debug it by hacking it until it produced the page you meant it to.
また、実行時にエラーが発生しないようにRtmlを設計しました。すべてのRtmlプログラムは何らかのWebページを生成し、それをハックしてデバッグすることで、意図したページを生成します。
Initially we expected our users to be Web consultants, and we expected them to use Rtml a lot. We provided some default templates for section pages and item pages and so on, and the idea was that the users could take them and modify them to make whatever pages they wanted.
当初は、ユーザーがWebコンサルタントになることを期待していましたが、Rtmlを多く使用することを期待していました。 セクションページやアイテムページなどのデフォルトのテンプレートをいくつか用意しました。ユーザーはそれらを持ってきて、必要なページを作成できるように変更することができました。
In fact it turned out that Web consultants didn’t like Viaweb. Consultants, as a general rule, like to use products that are too hard for their clients to use, because it guarantees them ongoing employment. Consultants would come to our Web site, which said all over it that our software was so easy to use that it would let anyone make an online store in five minutes, and they’d say, there’s no way we’re using that. So we didn’t get a lot of interest from Web consultants. Instead the users all tended to be end-users, the actual merchants themselves. They loved the idea of being in control of their own Web sites. And this kind of user did not want to do any kind of programming. They just used the default templates.
実際、WebコンサルタントはViawebを好まなかったことが判明しました。 コンサルタントは、原則として、継続的な雇用を保証するため、顧客が使用するにはあまりにも困難な製品を使用することを好む。 コンサルタントは私たちのウェブサイトに来て、私たちのソフトウェアは誰もが5分でオンラインストアを作れるように使いやすく、それを使用する方法はないと言っています。 だから我々はWebコンサルタントから多くの関心を得ていない。 その代わりに、ユーザーはすべて実際の販売者であるエンドユーザーである傾向がありました。 彼らは自分のWebサイトを管理するという考えを愛していました。 そして、この種のユーザーはどんな種類のプログラミングもしたくなかった。 彼らはちょうどデフォルトテンプレートを使用しました。
So Rtml didn’t end up being the main interface to the program. It ended up playing two roles. First of all, it was an escape valve for the really sophisticated users, who wanted something our built-in templates couldn’t provide. Somewhere in the course of doing Viaweb, someone gave me a very useful piece of advice: users always want an upgrade path, even though as a rule they’ll never take it. Rtml was our upgrade path. If you wanted to, you could get absolute control over everything on your pages.
だからRtmlはプログラムの主なインターフェースになったわけではありません。 それは2つの役割を果たした。 まず、本当に洗練されたユーザーのためのエスケープバルブでしたが、私たちの組み込みのテンプレートでは提供できなかったことを望んでいました。 Viawebをやっている途中で、誰かが私に非常に有用なアドバイスをくれました。ユーザーは常にアップグレードの道を求めています。 Rtmlが私たちのアップグレードパスでした。 あなたがしたい場合は、あなたのページのすべてを完全に制御することができます。
Only one out of every couple hundred users actually wrote their own templates. And this led to the second advantage of Rtml. By looking at the way these users modified our built-in templates, we knew what we needed to add to them. Eventually we made it our goal that no one should ever have to use Rtml. Our built-in templates should do everything people wanted. In this new approach, Rtml served us as a warning sign that something was missing in our software.
数百人に1人のユーザーしか実際に自分のテンプレートを書きませんでした。 そしてこれがRtmlの第2の利点につながった。 これらのユーザーが組み込みのテンプレートを変更する方法を見ることで、追加する必要があったことがわかりました。 最終的に私たちは誰もRtmlを使う必要がないという目標を達成しました。 私たちの組み込みのテンプレートは、人々が望むすべてを実行する必要があります。 この新しいアプローチでは、Rtmlは私たちのソフトウェアに何かが欠けていたという警告兆候として役立っていました。
The third and biggest win from using Rtml was the advantage we ourselves got from it. Even if we had been the only people who used Rtml, it would have been very much worth while writing the software that way. Having that extra layer of abstraction in our software gave us a big advantage over competitors. It made the design of our software much cleaner, for one thing. Instead of just having bits of actual C or Perl code that generated our Web pages, like our competitors, we had a very high-level language for generating Web pages, and our page styles specified in that. It made the code much cleaner and easier to modify. I’ve already mentioned that Web-based applications get released as a series of many small modifications. When you do that you want to be able to know how serious any given modification is. By dividing your code into layers, you get a better handle on this. Modifying stuff in lower layers (Rtml itself) was a serious matter to be done rarely, and after much thought. Whereas modifying the top layers (template code) was something you could do quickly without worrying too much about the consequences.
Rtmlを使用することから3番目に大きな利益を得たのは、私たち自身が得た利点でした。私たちがRtmlを使った唯一の人であったとしても、そのような方法でソフトウェアを書くのは非常に価値があったでしょう。ソフトウェアでこのような抽象レイヤーを追加することで、競合他社に勝る大きな利点がもたらされました。それは、私たちのソフトウェアの設計をずっときれいにしました。 Webページを生成する実際のCコードやPerlコードを競合他社と同じように使うのではなく、Webページを生成するための非常に高度な言語とそのページスタイルを指定しました。コードをよりクリーンで簡単に修正できました。 Webベースのアプリケーションは、一連の多くの小さな変更としてリリースされると私はすでに述べました。あなたがそうするとき、あなたは与えられた修正がどんなに深刻であるかを知ることができるようにしたいと思っています。コードをレイヤーに分割することで、これをよりよく処理できます。下層(Rtml自体)のものを変更することは、まれにしかやりたくない深刻な問題でした。トップレイヤー(テンプレートコード)を変更することは、結果にはあまり心配することなく、すばやく行うことができました。
Rtml was a very Lispy proposition. It was mostly Lisp macros, to start with. The online editor was, behind the scenes, manipulating s-expressions. And when people ran templates, they got compiled into Lisp functions by calling compile at runtime.
Rtmlは非常にLispyの命題でした。 それは主にLispのマクロであった。 オンラインエディタは、舞台裏でs式を操作していました。 テンプレートを実行すると、実行時にcompileを呼び出すことでLisp関数にコンパイルされました。
Rtml even depended heavily on keyword parameters, which up to that time I had always considered one of the more dubious features of Common Lisp. Because of the way Web-based software gets released, you have to design the software so that it’s easy to change. And Rtml itself had to be easy to change, just like any other part of the software. Most of the operators in Rtml were designed to take keyword parameters, and what a help that turned out to be. If I wanted to add another dimension to the behavior of one of the operators, I could just add a new keyword parameter, and everyone’s existing templates would continue to work. A few of the Rtml operators didn’t take keyword parameters, because I didn’t think I’d ever need to change them, and almost every one I ended up kicking myself about later. If I could go back and start over from scratch, one of the things I’d change would be that I’d make every Rtml operator take keyword parameters.
Rtmlはキーワードパラメータに大きく依存していました。私はこれまで、Common Lispのより怪しげな機能の1つと考えていました。 Webベースのソフトウェアがリリースされるため、変更するのが簡単になるようにソフトウェアを設計する必要があります。 そしてRtml自体は、ソフトウェアの他の部分と同様に、簡単に変更することができなければなりませんでした。 Rtmlの演算子のほとんどは、キーワードパラメータを取るように設計されており、何が助けになっているかが分かりました。 演算子の1つの振る舞いに別の次元を追加したい場合は、新しいキーワードパラメータを追加するだけで済みます。既存のテンプレートもすべて動作し続けます。 Rtml演算子のいくつかは、キーワードのパラメータを取っていませんでした。なぜなら、私はそれらを変更する必要はないと思っていたからです。 私が最初からやり直すことができれば、私が変えるべきことの1つは、すべてのRtmlオペレータにキーワードパラメータを取らせることです。
We had a couple embedded languages within the editor, in fact. Another one, which we didn’t expose directly to the users, was for describing images. Viaweb included an image generator, written in C, that could take a description of an image, create that image, and return its url. We used s-expressions to describe these images as well.
実際、エディタ内に組み込みの言語がいくつかありました。 私たちがユーザーに直接公開していなかったもう1つは、画像を記述することでした。 Viawebには、Cで書かれた画像ジェネレータが含まれており、画像の記述、その画像の作成、およびそのURLの返却が可能でした。 これらの画像も同様に記述するためにs式を使用しました。
Closures Simulate Subroutines
クロージャがサブルーチンをシミュレートする
One of the problems with using Web pages as a UI is the inherent statelessness of Web sessions. We got around this by using lexical closures to simulate subroutine-like behavior. If you understand about continuations, one way to explain what we did would be to say that we wrote our software in continuation-passing style.
WebページをUIとして使用する際の問題の1つは、Webセッションの本質的なステートレス性です。 これを回避するには、サブルーチンのような振る舞いをシミュレートするために字句解析を使用します。 継続について理解しているなら、私たちが何をしたのかを説明する一つの方法は、私たちが継続的に渡すスタイルでソフトウェアを書いたと言うことでしょう。
When most web-based software generates a link on a page, it tends to be thinking, if the user clicks on this link, I want to call this cgi script with these arguments. When our software generated a link, it could think, if the user clicks on this link, I want to run this piece of code. And the piece of code could an arbitrary piece of code, possibly (in fact, usually) containing free variables whose value came from the surrounding context.
ほとんどのWebベースのソフトウェアがページ上にリンクを生成するとき、ユーザーがこのリンクをクリックすると、これらの引数を使ってこのcgiスクリプトを呼び出すことが考えられます。 私たちのソフトウェアがリンクを生成したとき、ユーザーがこのリンクをクリックすると、このコードを実行したいと思うかもしれません。 そしてコード部分は、周囲の文脈から値が得られる自由変数を含む可能性がある(実際には、通常は)任意のコードである可能性があります。
The way we did this was to write a macro that took an initial argument expected to be a closure, followed by a body of code. The code would then get stored in a global hash table under a unique id, and whatever output was generated by the code in the body would appear within a link whose url contained that hash key. If that link was the next one clicked on, our software would find and call the corresponding bit of code, and the chain would continue. Effectively we were writing cgi scripts on the fly, except that they were closures that could refer to the surrounding context.
これをやったやり方は、最初の議論が閉鎖であると予想されるマクロを書くことでした。その後にコードが続きました。 コードは、一意のIDの下にグローバルハッシュテーブルに格納され、本体のコードによって生成された出力は、そのハッシュキーを含むURLを持つリンク内に表示されます。 そのリンクがクリックされた次のリンクだった場合、私たちのソフトウェアは対応するビットのコードを見つけて呼び出し、チェーンは続行されます。 効果的に、私たちはその場でcgiスクリプトを書いていましたが、それは周囲の文脈を参照できるクロージャーでした。
So far this sounds very theoretical, so let me give you an example of where this technique made an obvious difference. One of the things you often want to do in Web-based applications is edit an object with various types of properties. Many of the properties of an object can be represented as form fields or menus. If you’re editing an object representing a person, for example, you might get a field, for their name, a menu choice for their title, and so on.
これまでのところ、これは非常に理論的に聞こえるので、このテクニックが明白な違いを生む場所の例を挙げておきましょう。 Webベースのアプリケーションでしばしばやりたいことの1つは、さまざまな種類のプロパティを持つオブジェクトを編集することです。 オブジェクトの多くのプロパティは、フォームフィールドまたはメニューとして表現できます。 たとえば、人物を表すオブジェクトを編集している場合は、フィールド名、そのタイトルのメニュー選択肢などが得られます。
Now what happens when some object has a property that is a color? If you use ordinary cgi scripts, where everything has to happen on one form, with an Update button at the bottom, you are going to have a hard time. You could use a text field and make the user type an rgb number into it, but end-users don’t like that. Or you could have a menu of possible colors, but then you have to limit the possible colors, or otherwise even to offer just the standard Web colormap, you’d need 256 menu items with barely distinguishable names.
今、いくつかのオブジェクトが色であるプロパティを持っているとどうなりますか? 通常のcgiスクリプトを使用する場合は、一番下の「更新」ボタンを使用してすべてを1つのフォームで実行する必要があります。困難な場合があります。 テキストフィールドを使用して、ユーザがそれにrgb番号を入力させることができますが、エンドユーザはそれを気に入らないことができます。 または可能な色のメニューを持つことができますが、可能な色を制限する必要があります。そうでなければ、標準のWebカラーマップだけを提供する場合でも、ほとんど区別できる名前の256のメニュー項目が必要になります。
What we were able to do, in Viaweb, was display a color as a swatch representing the current value, followed by a button that said “Change.” If the user clicked on the Change button they’d go to a page with an imagemap of colors to choose among. And after they chose a color, they’d be back on the page where they were editing the object’s properties, with that color changed. This is what I mean about simulating subroutine-like behavior. The software could behave as if it were returning from having chosen a color. It wasn’t, of course; it was making a new cgi call that looked like going back up a stack. But by using closures, we could make it look to the user, and to ourselves, as if we were just doing a subroutine call. We could write the code to say, if the user clicks on this link, go to the color selection page, and then come back here. This was just one of the places were we took advantage of this possibility. It made our software visibly more sophisticated than that of our competitors.
Viawebでは、現在の値を表す見本として色を表示し、続いて「変更」と表示されたボタンが表示されました。ユーザーが変更ボタンをクリックした場合、それらの中から選択する色のイメージマップを持つページに移動します。彼らが色を選んだら、オブジェクトのプロパティを編集していたページに戻り、その色が変わります。これは私がサブルーチンのような振る舞いをシミュレートすることを意味しています。ソフトウェアは、色を選択して戻ってきたかのように振る舞います。もちろん、そうではありませんでした。それはスタックのように見える新しいcgiコールを作っていました。しかし、クロージャを使うことで、あたかもサブルーチンコールをしているかのように、ユーザーと自分自身に見えるようにすることができます。ユーザーがこのリンクをクリックすると、色選択ページに移動してから、ここに戻ってくるというコードを書くことができます。これは私たちがこの可能性を利用した場所の一つに過ぎませんでした。これにより、当社のソフトウェアは競合他社よりも洗練されたものになりました。