[racket] moby/wescheme & TCO

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Sun Jan 30 15:11:47 EST 2011

>> I am wondering if there are documentations on how moby/wescheme solve the tail call optimization issue to compile down to javascript.  My google'fu is failing me.

Hi YC,

The current runtime has an interpreter that's explicitly managing the
control stack, so it can do the tail-call optimization manually.



An approach to get tail calls in JavaScript is described in
"Exceptional Continuations in JavaScript":

    http://www.schemeworkshop.org/2007/procPaper4.pdf

where the restoration of the continuation skips tail frames (Section
4.2 of that paper).  So you could imagine saving and restoring the
continuation every so often, which will clear the control context of
tail call frames.



SICP Chapter 5.5 has a compiler that does the tail-call optimization
in its generated intermediate code.  I have a prototype that assembles
to JavaScript:

    https://github.com/dyoo/js-sicp-5-5/blob/master/cm.rkt

Example:

    http://hashcollision.org/tmp/gauss.html

The new version of the Moby compiler/runtime that I'm working on will
be based on this prototype.


Posted on the users mailing list.