JavaでのUnlambda実装

Unlambda実装セットの中に入っていたのを読んでみる。600行程度の1ファイル。

// This is an implementation of the Unlambda programming language.
// This one is in Java (the Master One is in Scheme).
// $Id: unlambda.java,v 1.12 1999/11/03 21:05:47 madore Exp $

// Copyright (C) 1999 by David A. Madore

長いコメント文を要約すると「まずevalとapplyの相互再帰で作った」「でもcall/ccは入ってなかった」「call/ccを入れるためにCPSで書き直した」「でもJavaは末尾呼び出しを最適化しないからスタックあふれまくりんぐ」「最終的にタスクの概念を導入した」

This time, the Java stack is no longer used
(*at all* - in other words, its depth is bounded by a constant
function of the program complexity), and the Unlambda ``stack'' (no
longer a truly pertinent notion in the presence of first-class
continuations) is part of the Java heap (as chains of
Continuations), so that stack frames are no longer ``popped'', they are garbage-collected

Continuationのチェインという形で自前でスタックを作ったんだそうな。結局そういうことになるみたい。

  • Task Continuation#invoke(Function)
  • Task Task#run()
  • Task Function#applyTo(Function, Continuation)
  • Task Expression#eval(Continuation)