Hi all,

I thought I'd share the results of a fun little excercise I did this morning: running Clojure on a VM with native support for tail call optimization and first class continuations. Any Schemers on this list will probably appreciate it.

The VM in question is Avian (http://oss.readytalk.com/avian/), built with optional tail call and continuation features enabled and using the OpenJDK class library. It's not nearly as fast or sophisticated as e.g. Hotspot, but it has some features that make it interesting for running non-Java languages. I'm using the latest commit from the Git repository.

For example, here's what happens if you recurse too deeply using Hotspot:

 $ java -cp clojure.jar clojure.main
Clojure 1.2.1
user=> ((fn this [n] (if (= n 1000000) n (this (+ n 1)))) 0)
java.lang.StackOverflowError (NO_SOURCE_FILE:0)
user=>

With Avian, there is no overflow, and we also get to play with continuations:

 $ ../avian/build/linux-x86_64-tails-continuations-openjdk-src/avian -Xmx512m 
-cp clojure.jar clojure.main
Clojure 1.2.1
user=> ((fn this [n] (if (= n 1000000) n (this (+ n 1)))) 0)
1000000
user=> (defn call-cc [function]
   (avian.Continuations/callWithCurrentContinuation
     (reify avian.CallbackReceiver
       (receive [_ continuation]
        (function (fn [result] (.handleResult continuation result)))))))
#'user/call-cc
user=> (call-cc (fn [continuation] (continuation "hello, world!")))
"hello, world!"
user=>

For more information about how continuations work in Avian, please see http://oss.readytalk.com/avian/javadoc/avian/Continuations.html.

Anyway, I think this is an interesting experiment since it shows that if/when tail call and continuation support are integrated into the JVM officially (http://openjdk.java.net/projects/mlvm/), Clojure could benefit with few or no modifications.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to