Java の「インターフェイス」という機能の元ネタ


具体的な言語処理系というわけではありませんが、おそらくこれが元ネタだろうというアイデアが記された論文は見つかりました。もっともゴスリングが Java や Oak(Java の前身…というか初期バージョンそのもの。変わったのは名前だけなので)について書いたものに Javaインターフェイスは○○を参考にして考えた…という記述を見つけられずにいるので、これをFAとするには、もう少し調査が必要そうですが、とりあえずということで。


この論文中では、たとえば Point というインターフェイスを定義して、それを実装した polar_point というクラスを定義する例として、仮想言語を用いてこんなコードが示されています。

interface Point
    x() returns Real
    y() returns Real
    move(Rea1, Real) returns Point
    equal(Point) returns Boolean
class polar_point(x:Real, y:Real) implements Point
    variable rho:Real := sqrt((x*x)+(y*y))
    variable theta:Real := arctangent(y/x)
    method x() returns Real
        return rho * sinettheta)
    method y() returns Real
        return rho * cosine(theta)
    method move(dx:Real, dy:Real) returns Point
        return new myclass(self.x()+dx, self.y()+dy)
    method equal(p:Point) returns Boolean
        return (self.x() = p.x()) and (self.y() = p.y())


クラスに対するインターフェイスの実装の宣言に implements を使っているあたりに Java への影響の片鱗が見られると思うのはうがちすぎでしょうか。これは有名な Inheritance Is Not Subtyping (1990) [PDF] の元になった論文みたいなので、使われているサンプルコードや主張は両者でよく似ています。ロハで読みたいという人はこちらをどうぞ(ただ、のっけから式が出てきたりで、平易な Interfaces for... より少々難解です。ご注意あれかし)。



ところで、私のような Smalltalk ファンにはたいへん興味深いことに、どうやらこのクックらの「インターフェイス」のさらに元ネタのところで、なんと Smalltalk が一枚かんでいるらしいのです。と言うと、動的型付けの権化のような Smalltalk で「インターフェイス」?といぶかしがる人も多いでしょうね。もちろん当時の Smalltalk-80 そのものずばりではなく、それに静的型チェックを導入した 1982 年の実験的な実装についてです。これは Smalltalk の中の人であるインガルスと ThingLab(インスタンスベースな言語で有名な SELF の元ネタになったソフトウエア)の中の人であるボーニングによる仕事です。


追記:ここでことさらに「中の人」を強調すると誤解があるといけないなとあとで気づいたので念のため書きますが、Smalltalk は通常の言語処理系での常識とは違い、エンドユーザーレベルで言語仕様を変更できるちょっと(いやかなり…)変わった性格を有する処理系なので、こうした拡張をするのにいわゆる「中の人」である必要はまったくありません。具体的にどんなふうにしたらできるのかについては(ちょっと古いバージョン向けではありますが)Squeak Smalltalk に、くしくもインターフェイスそのものずばりを追加する話を umejava さんが書いてくださっている(Happy Squeaking!! -オブジェクト指向再入門- 第四回:メタ機能との出会い)ので、そちらをぜひ参考になさってください。Squeak2.2 は 本家アーカイブ などから入手できます。



Interfaces for... の既往研究のセクションに、この Smalltalk の「インターフェイス」についての言及がありますので抜粋して引用します。

Interfaces for strongly-typed object-oriented programming - 5. Related Work

There have been several attempts to introduce interfaces into object-oriented programming. Efforts within the object-oriented language community include the work of Borning and Ingalls [BI82], the Emerald group [BHJ87], and Johnson et al. [JGZ88], all of whom have a notion of interface as message protocol. [BI82] and [JGZ88] describe type systems for Smalltalk with types based both on classes and protocols. The [BI82] work is less formal and does not distinguish interface containment from inheritance; nevertheless, it captures much of the essential structure of object interfaces. The more recent [JGZ88] work is interesting for its mixing of class-based and interface types, and has a limited form of polymorphism as found in ML. Emerald introduces interfaces to provide uniformity in a distributed object system. However, the interface system supports only limited parametric polymorphism and there is no notion of inheritance. Similarly, Modula-3 [CDK89] has notions of object and interface, but without polymorphism.



In most typed object-oriented languages, including Simula, C++, and Eiffel, the notion of type is different from ours. In those languages, classes are types, so that a type gives information about how objects are implemented. This improves performance and also provides the benefits of static type-checking, but at a cost in flexibility, which is significant for system development. In addition to collapsing the distinction between interface and implementation, the notions of interface containment and inheritance are confused[Coo89]. The result is that programs that would be correct in Smalltalk cannot be type-checked in these languages.



[BI82] A. H. Borning and D. H. Ingalls. "A type declaration and inference system for Smalltalk". In ACM Symposium on Principles of Programming Languages, 1982.
[BHJ87] A. Black, N. Hutchinson, E. Jul, H. Levy, and L. Carter. "Distribution and abstract types in Emerald". IEEE Transactions on Software Engineering, SE-13(l), 1987
[JGZ88] R. Johnson, J. Graver, and L. Zurawski. "TS: An optimizing compiler for SmalltaW. In Object-Oriented Programming Systems, Languages, and Applications, 1988.
[CDK89] L. Cardelli, J. Donahue, B. Kaslow, and G. Nelson. "The Modula-3 type system". In ACM Symposium on Principles of Programming Languages, 1989.
[Coo89] W. R. Cook. "A proposal for making Eiffel type-safe". In European Conference on Object-oriented Programming, 1989.


つまるところ、インガルスたちの Smalltalk における「インターフェイス」(ちなみに彼らは「プロトコル」と呼んだ。おそらく Objective-C の「プロトコル」という呼び名はここからでしょう)は、はっきりとクラス継承から「インターフェイス」を分離しようとしたものではなかったけれど、れっきとしたエンティティとして存在し(まあ、Smalltalk での実装ですから当然そうなりますわな…(^_^;))、「インターフェイス」が持つべき素養はすでに備えていた、といったようなことのようです。

同時に抜粋の後半では、インターフェイスを実装と分けて考えること自体は静的型言語で比較的以前からなされていたけれど、クラスのようなエンティティとしての「インターフェイス」やその継承モデルまで言及し、それを提案したのはこの論文が初めてだという主張も(抜粋の前半と併せて)なされているようにとれました。


暇を見て、Java 側からも調査できれば、と思っています。