Computer Languages A to Z: Guile

by Gary L. Ratliff, Sr. (eronstuc)

Guile stands for: GNU's Ubiquitous Intelligent Language for Extensions. It is an outgrowth of the flame war started in 1994, when Richard M. Stallman wrote an article about why Tcl should not be used for programming. The system comes with a standard install of the various versions of PCLinuxOS. The current version is 1.8.6. I found this on the other computer, which has version 2007, and also on my Gnome 2009-2 install of the system. This acts as an interpreter for the Scheme dialect of Lisp. It also has a library which may be linked into the compile of various languages to allow it to be used to extend the language.

Because Scheme is a dialect of Lisp, I will take this opportunity to mention some Lisp items which directly relate to Common Lisp. I found and installed these systems when I was working on the Elisp segment of this series. The first item is called Lisp in a Box. This provides a Lisp Development system based on Slime. It provides an Emacs editor, which is separate from your emacs editor, and is for use with the "in the box" system. There are several code segments from the book "Practical Common Lisp," written by Peter Seibel. The text of the book is available online at http://www.gigamonkeys.com. The other is the free trial of LispWorks Personal Edition. This is version 5.1.1, and it is installed by completing a form and agreeing to its terms. This is available from http://www.lispworks.com. Once installed you will need to rename one of the libraries. However, these details will be covered once we reach lisp. Or by writing them, they will help you solve any problems you might have with the installation. If you enjoy the fact that lisp is most likely completely different than any other language with which you have worked, you will enjoy obtaining these items.

Also on the system, I found that the package umb-scheme seems to come with the default install. The system reports that it is using version 3.2, and there is also a message that there was a Fatal Error. However, the system appears to function fine, regardless of this statement. So either of these should be availavle to test the use of the scheme code you wish to try. To start, use a terminal and enter $guile to enter into the guile interpreter.

Or enter:

$umb<tab>

The tab key will expand this to the full name, "umb-scheme". These will begin their respective programs. Now, to exit guile, use: (quit), while to exit the umb-scheme system, press the Control key and then the d key. This was explained on the startup of the umb-scheme program. Offhand, umb stands for University of Massachusetts Boston. Both of these are based on the R5RS standard of the Scheme Lisp Dialect. Now the standard was recently updated, and the R6RS standard was adopted.

Learning to use the Scheme Dialect of Lisp and the new standard.

The newest standard of the dialect was adopted in July, 2009, and texts which are being published now are using this standard. The current versions of the system will be using the R5RS standard. I consider the biggest change to be that the new standard allows the case sensitivity of symbols, which the R5RS does not. This means that if you have a function called foo in your system, you could type in FOO, foo, FoO, etc., and they would all call the same function. Code based upon the newer standard would allow these to be completely different functions. One of the books I recommend using to learn Scheme is the book from MIT Press, The Scheme Programming Language, 4th Edition, or use The Scheme Programming Language, 3rd Edition. Both are written by R. Kent Dybvig. If you use the 4th Edition, and key in its examples while using either the guile or the umb-scheme system currently installed, you will receive several syntax errors which you will not experience while using the 3rd Edition, which uses the older standard. If you wish to try the latest features in the new standard, there is a free version of Chez Scheme available. This is available in a threaded and non threaded version, for both 32bit and 64bit Intel systems. The threaded version for 32 bit is: pcsv7.9.3-ti3le.tar.gz, while the non-threaded version is: pcsv7.9.3-i3le.tar.gz. They also stated that an rpm version was available. However, trying to install the rpm version yielded that the dependency to libinfo.so.5 was not meet. My idea is that the guile system will soon be upgraded to use the new standard anyways.

There are some interesting tools available. For example, let's explore the familiar factorial function. This is defined as 1 for factorial 0, and by multiplying all the integers from 1 to n for successive numbers. So the screen shot will show how to define the function in Scheme, and then show how to trace this function.

This is done by entering the following into guile:

(define fact
   (lambda (x)
      (if (zero? x)
          1
          (* x (fact (1- x))))))

(fact 5)
(trace fact)
(fact 5)
(fact 10)
pic1

This shows the steps which the process goes through to arrive at the answer. Also note that 1- is itself a separate function. If you wish to learn what the item is, you may enter it at the keyboard without any parentheses surrounding it. For example, + will yield <primitive-generic +>, lambda will yeild <primitive-builtin-macro! Lambda>, and 1- will yield <primitive-procedure 1->

Now one of the functions of the supplied library is to allow the ease of adding full functionality of different languages to be added to an existing language. Thus a compiled C++ or C program could have the Scheme interpreter provided by the

libguile.so library to be added to the program. An example of how easy this is, is provided in the tutorial and sited below:

/* test the new libgh.a (Guile High-level library) with a trivial program */
#include <stdio.h>
#include <guile/gh.h>

void main_prog(int argc, char *argv[]);

main(int argc, char *argv[])
   {
      gh_enter(argc, argv, main_prog);
   }

void main_prog(int argc, char *argv[])
   {
      int done;
      char input_str[200];
      gh_eval_str("(display \"hello Guile\")");
      gh_eval_str("(newline)");

/* for fun, evaluate some simple Scheme expressions here */
      gh_eval_str("(define (square x) (* x x))");
      gh_eval_str("(define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))");
      gh_eval_str("(square 9)");

/* now sit in a Scheme eval loop: I input the expressions, have Guile evaluate them, and then get another expression. */
      done = 0;
      fputs("learn0> ", stdout);
      while (fgets(input_str, 199, stdin) != NULL) {
      gh_eval_str(input_str);
      fputs("\nlearn0> ", stdout);
   }

exit(0);
}

This program should be named learn0.c, and it is compiled with these two passes of the gcc compiler:

$gcc -c learn0.c -o learn0.o
$gcc -o learn0 learn0.o -lguile -lm.

The -c flag is used to tell the compiler to stop after the object file has been created, instead of continuing till the executable file is created. The -o flag is used to tell the compiler to name the file produced learn0.o. In the second run of the compiler, this file is compiled, and the output named learn0, and is linked to two libraries the guile library and the math library. Once this has been done, you will find that ./learn0 will start a file which will function exactly as if you had entered the guile command to start guile itself.

pic2

Here is another excellent source for learning the Scheme dialect of Lisp. This book was developed at MIT Press, and is used in an introductory course in computer programming at many universities throughout the country. If you ever watched the movie Hackers, you may recall where the computer jock mentioned a litany of different named colored computer manuals. Well, this is known as the Purple Book.

pic3

By visiting this site and clicking on the slides link, you will be taken to a complete lecture that will introduce all the features of the scheme dialect of lisp. You do have quite a bit of power, as the top level definitions can easily be changed. For example: You learned that 2 + 3 = 5 and that this would be expressed in scheme or guile as: (+ 2 3.) However it is possible to enter (define + -) and now when you ask for (+ 2 3) the answer will be given as -1!! This would be the answer if you had entered (- 2 3) Also, if you now used the system to do your math homework, you would most likely flunk if you were performing a drill on addition.

The only remedy is to use (exit), or Control D, to exit the system and restart, which will kill your idea of the new addition primitive.

One of the major uses for guile is to allow programs to become extensible. However, the examples sited in the documentation for this do not compile correctly. However, this should be enough to wet your appetite for learning more about this system. The very latest on the language blogs was that Scheme would be split into two different languages. Only time will tell on this move.