言語処理における「ファーストクラス」「第一級」とは

プログラミング言語、特にLispSchemeを勉強していると、たまに「ファーストクラスのオブジェクト」や「第一級の関数」などを目にすることがあります。
しかし、「ファーストクラス」とか「第一級」というのは、いったい何なのでしょう。

その答えは、『Structure and Interpretation of Computer Programs(計算機プログラムの構造と解釈)』の1.3.4にある「Procedures as Returned Values」に記述がありました。

In general, programming languages impose restrictions on the ways in which computational elements can be manipulated. Elements with the fewest restrictions are said to have first-class status. Some of the ``rights and privileges'' of first-class elements are:

  • They may be named by variables.
  • They may be passed as arguments to procedures.
  • They may be returned as the results of procedures.
  • They may be included in data structures.

Lisp, unlike other common programming languages, awards procedures full first-class status. This poses challenges for efficient implementation, but the resulting gain in expressive power is enormous.

翻訳すると以下のようになります。

一般に、プログラム言語には、計算要素を扱う方法において、様々な制限があります。このような制限がきわめて少ない要素を、第一級(ファーストクラス)の身分を持つ、と言います。第一級要素の「権利と特権」には、以下のようなものがあります:

  • 変数として名前がつけられる。
  • 手続きに引数として渡せる。
  • 手続きの結果として返される。
  • データ構造に組み込める。

Lispは他の通常のプログラム言語と違い、手続きに完全な第一級身分を与えました。そのため、効率のよい実装は困難になりましたが、結果として得た表現力はたいへん大きなものです。

非常に端的に言い当てていて、素晴らしいですね。
「ファーストクラス」「第一級」というのは、つまりソースコード中の多くの場面で、とても自由な扱いができるということなのですね。

たしかにLispの関数は、上記のことが全て可能です。

LispSchemeの文献を読んでいて、「ファーストクラス」「第一級」という言葉に出会っていたのは、この理由からだったようですね。