Behind the Scenes with CHICKEN Scheme and SPOCK (Part 2)

The CHICKEN Scheme logoLast month, I introduced you to Felix Winkelmann, creator of one of my favorite programming languages: CHICKEN Scheme, a mature, high-performance implemention of Scheme that compiles to C code.

In my previous post, I interviewed Felix about how he became interested in Scheme and programming, why he created and maintains CHICKEN, and what makes it special. We turn now to one of Felix’s other creations: SPOCK, a compiler and runtime system for compiling Scheme code into JavaScript.

SPOCK is not yet as mature as CoffeeScript or ClojureScript, two other popular languages that compile into JavaScript. But I know from personal experience that SPOCK already works, and has significant potential. While tinkering with SPOCK a few months ago, I had no difficulty creating two simple proofs of concept: one using HTML5 canvas to create 2D graphics, and one using THREE.js and WebGL to create 3D graphics.

In addition to using SPOCK to generate client-side scripts, both examples use CHICKEN Scheme to generate the HTML pages that house the scripts. Indeed, using the many CHICKEN Scheme extensions for web programming, databases access, and more, it’s possible to use CHICKEN Scheme to create and serve full-featured web apps, complete with client-side scripting!

Given Scheme’s extraordinary flexibility and CHICKEN’s solid performance and native library support, this could become a compelling toolchain for web developers willing to venture beyond the familiar landscapes of PHP, Ruby on Rails, CoffeeScript, and the like.

Read on as Felix tells us more about SPOCK, why he created it, how it relates to CHICKEN, and the curious origins of the names “CHICKEN” and “SPOCK”.


Let’s talk about SPOCK. What is it? Is it ready for people to use? Why would someone want to use it?

SPOCK is a compiler from a subset of R5RS Scheme to JavaScript. It uses a compilation strategy similar to CHICKEN, but it is more lightweight and cuts a few corners of the Scheme standard to be practical. It has not been used a lot so far, but it works, and I think it has some potential to be a useful glue language for Scheme-based web software. But, I’m not an expert in web programming, so my opinions must be taken with a grain of salt.

The interesting bit is that the distinction between server-side and client-side gets fuzzier — a Scheme server can emit Scheme code to run on the client, and Scheme’s powerful syntactic extension mechanisms can make this look like a single piece of code. SPOCK is not what I’d call ready for production yet. But I’d say there is potential.

What motivated you to create SPOCK?

Originally, I wanted to have a clean compiler core for Scheme, using the “Cheney on the M.T.A.” compilation strategy (which is also used in CHICKEN). JavaScript is an interesting and powerful target language that already takes care of a lot of things (garbage collection, dynamic typing, etc.), so it was a natural choice. After the usual frustration of getting it to work on all major browsers, the parts just fell into place.

Have you used it for any projects yet?

I have only done experiments with it. I’d love to do more, but I severely lack the experience in web programming.

You mentioned that CHICKEN and SPOCK both use Henry Baker’s “Cheney on the M.T.A.” compilation strategy. How much did your experience developing CHICKEN help with creating SPOCK? Are the implementations similar?

Baker’s method is really incredibly clever — naturally, I have to say that ;-) — but the code that it produces takes some getting used to. It’s a bit of a challenge to read code that has been converted to continuation-passing style (CPS) and translated to another language. Without the experience I gained from CHICKEN, SPOCK would have taken much more time. SPOCK is a good deal simpler and cleaner than CHICKEN, but of course it supports a much smaller language, it’s not fully R5RS compliant, and it doesn’t have to cope with the horrors of POSIX, the Windows API, or C compiler issues. And JavaScript takes care of a lot of dirty details, of course.

Much of Baker’s paper seems pretty specific to the memory management and function call conventions of C. What gave you the idea of applying it to JavaScript? Are the techniques described in the paper relevant in JavaScript?

I think they are relevant to every language. Baker’s compilation strategy is applicable to nearly every language that has activation frames with limited extent. It elegantly combines garbage collection with stack frame management and continuation creation, so static languages like C are a natural choice. JavaScript already provides garbage collection, but Baker’s method gives us tail-call optimization and first-class continuations. There has been at least one CPS-based Scheme-to-JavaScript compiler before, but it didn’t explicitly use Baker’s method, as far as I know.

SPOCK’s documentation includes a warning that it “stresses JavaScript implementations in unusual ways.” Are there significant performance issues with SPOCK? If so, do you think performance will improve as SPOCK matures?

That is possible, yes. SPOCK creates deeply nested functions, and this stresses existing JavaScript engines in unexpected ways. It even uncovered a bug in Mozilla’s JavaScript engine — which is fixed now, thanks to the engine’s maintainers. There may be corner cases that haven’t been thought of yet. SPOCK just needs more users and more testing.

What’s next for SPOCK? Are you going to continue developing it?

Currently, I’m just waiting for people to use it. ;-)

But, SPOCK is clean enough to be grokked by whoever wants to hack on it. It is not under active development at the moment, but it has a reasonable size and complexity, which makes it easier to maintain than, say, CHICKEN. It would be interesting to see how people use it, and I’ll be available in case something breaks.

Both CHICKEN and SPOCK are open source. If someone is interested in contributing, what is the best way to get started?

Just give it a try. Play with it, learn about it, write something useful, or even something useless. Then get in touch with the community, ask questions on the mailing list, or enjoy the daily fun on our IRC channel. Submitting a new CHICKEN library or extension module is very easy. There are endless things to do, even if it is just testing, and we are happy about every little bit of help we can get, and happy to provide help to those that need it themselves. Every line of code contributed makes CHICKEN better, increases our corpus of testing code, or at least gives us something to think about.

One last question: What inspired the names CHICKEN and SPOCK? Do they mean anything, aside from the bird and the well-known Star Trek character?

That question always comes up, sooner or later. ;-)

I had a plastic toy of Feathers McGraw on my desk, the evil penguin (disguised as a chicken!) from the Wallace and Gromit movie, “The Wrong Trousers.” Looking for a preliminary working title for the compiler, I used the first thing that came to my mind that day. I’m somewhat superstitious about names for software projects, and things were progressing well, so I didn’t dare to change the name.

Also, there is the old philosophical question: which came first, the chicken or the egg? This applies to CHICKEN, too. The compiler is written in Scheme, so you need CHICKEN in order to compile CHICKEN.

For SPOCK, the story is not that interesting. I just like whacky names, and it seemed nice to have some sort of “persona” to associate with the compiler. Like CHICKEN, “Spock” was just the first thing that came to mind.

After SPOCK, I worked for a while on a rudimentary compiler that produced C++ instead of JavaScript, but it was never finished. It was quite bare “bones,” so naturally I called it MCCOY.

Felix, it has been a pleasure. CHICKEN and SPOCK definitely seem to have a lot of potential. Thanks for taking the time to tell us more about them.

You’re very welcome.