Gustavo Muñoz - justavo, kanchenjungo, zipi

August 26, 2005

Kawa, Emacs and Common Lisp

Filed under: Technology

This post, essencially is a transcript/translation of a chat that I had with Mago (Gerardo Horvilleur). He has been using Kawa for at least 2 years and Common Lisp also for a while. Here I share some insights about Kawa, Scheme and Common Lisp, along with a very convenient way to configure emacs for the Kawa REPL (Read Eval Print Loop).
From the Kawa home page we read:

  • A framework written in Java for implementing high-level and dynamic languages, compling them into Java bytecodes.
  • An implementation of Scheme, which is in the Lisp family of programming languages. Kawa is a featureful dialect in its own right, and additionally provides very useful integration with Java. It can be used as a “scripting language”, but includes a compiler and all the benefits of a “real” programming language, including optional static typing.

And it is not all the story: with Kawa you compile Scheme to Java bytecodes, from what Mago has seen, with quite good optimizations. With Kawa you get the opportunity to create classes, call Java objects methods and so on. So it provides an extraordinary way to combine the power of Scheme and the well-known benefits of Java. Additionally, Scheme is not the only high-level/dynamic language it implements, also XQuery (Qexo) and Emacs Lisp (JEmacs) among others.
Well, Mago was the guy who provided me a very convenient Emacs configuration for Kawa integration.

In some path I copied de kawa.jar file, say, ~/jars/kawa.jar
In some path included in $PATH I have the following shell script, named kawa:

export CLASSPATH=$CLASSPATH:$HOME/jars/kawa.jar:.
java -Xms256m -Xmx256m kawa.repl $*

In my .emacs file I have

(defun kawa ()
    "Run Kawa Scheme in an Emacs buffer."
    (interactive)
    (require 'cmuscheme)
    (let ((scheme-program-name "/Users/justavo/bin/kawa"))
        (run-scheme scheme-program-name)))
	
(defun my-scheme-mode-common-hook ()
    (put 'destructuring-bind 'scheme-indent-function 2)
    (put 'destructure-case 'scheme-indent-function 1))
	
(add-hook 'scheme-mode-hook 'my-scheme-mode-common-hook)

The first defun enables running Kawa inside Emacs with:
Alt-X kawa
The second defun and the add-hook is for Emacs to indent in a correct manner Mago’s Kawa partial port of Common Lisp’s destructuring-bind, and an implementation of destructure-case inspired by the one used in SLIME (you can get them directly from Mago, he distributes them under the LGPL license).

This also enables that in case you have Kawa running on an Emacs buffer and concurrently in other buffer any Kawa source (Scheme source) you can use the familiar Emacs commands within the Kawa source buffer:
Ctrl-C Ctrl-L: for loading the current Kawa source into Kawa.
Ctrl-C Ctrl-E: positioned at the start of any expression sends the expression to Kawa.

There are a lot of valuable links on Scheme and Common Lisp, but Mago recommends these for a newbie.
An excellent book about programming which has all examples in Scheme is:
Structure and Interpretation of Computer Programs
Another valuable book is The Scheme Programming Language

When I started studying Common Lisp I found that there were not two but a large number of Common Lisp implementations. It’s very hard when you don’t have any experience with a language to decide which implementations is going to be must usable for you. Mago’s preferred implementation (at least on Mac OS X) is Steel Bank Common Lisp.
Of course you must install SLIME, as well as read the book Practical Common Lisp. If you are still wondering why I wrote “of course” about SLIME you should watch Marco Beringer SLIME video.
Here’s the location of the movie (55min; 149mb; quicktime encoded mpeg-4) and torrent (due to the size of the file, please use the torrent if possible):
http://common-lisp.net/movies/slime.mov.
http://common-lisp.net/movies/slime.torrent.
Maybe one of the best (and simple) reasons for choosing SBCL is Planet SBCL, where you can check the results of the daily tests they run against the daily builds.
Additionally SBCL provides a lot of warnings that are very helpful in understanding our own mistakes when programming Common Lisp.
A very worth reading about Common Lisp, in general, and SBCL, in particular is the Juho Snellman’s Weblog. BTW today he published a benchmark among different CL implementations. But, as Mago told me, a very light analysis of the results points to the conclusion that Juho is comparing the native compilation of SBCL against interpreted code of all other CL implementations.
There is a lot activity in the SBCL development group. For instance, 6 hours after releasing version 0.9.4 this is the result of cvs diff:
% cvs -q diff -r sbcl_0_9_4 -r HEAD | diffstat | tail -1
82 files changed, 939 insertions(+), 570 deletions(-)

Talking about OO in Scheme makes me to mention Tiny CLOS, a mini-implementation of CLOS for Scheme that uses MOP.
Anyway, talking about MOP and CLOS worths another post. They are the ultimate word in OO implementation.

In resume, for a Lisp newbie it is better first not to work with CLOS, and begin reading Practical Common Lisp.
Other valuable not-on-line books are:
Paul Graham’s ANSI Common Lisp.
Peter Norvig’s Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp, but this one is out of print.

BTW, all the links I provide in this post are in my del.icio.us under tags like scheme, lisp, common-lisp, book.

2 Comments »

The URI to TrackBack this entry is: http://justavo.blogsome.com/2005/08/26/kawa-emacs-and-common-lisp/trackback/

  1. Just found this while doing some narcissistic googling ;-) Thanks for your kind words.

    For the record, the following speculation is incorrect:

    » But, as Mago told me, a very light analysis of the results points to the conclusion that Juho is comparing the native compilation of SBCL against interpreted code of all other CL implementations.

    The code was compiled with COMPILE-FILE on all implementations.

    However, GCL was unable to compile the code completely, and was interpreting some critical parts. (Specifically functions defined in a non-empty lexical environment caused problems. So for example in (macrolet ((foo (name ...) `(defun ,name ...))) (foo a ...) (foo b ...)) A and B would be interpreted, even though the MACROLET is on the toplevel and COMPILE-FILE is used).

    Comment by Juho Snellman — November 15, 2005 @ 10:23 pm

  2. In which country do you live? :)

    Comment by firefox mac — March 18, 2008 @ 4:51 pm

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>























Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham