Through the Looking Glass

Notes on Getting Started with OpenDylan

Posted on February 10, 2015

In this post, I’ll start with what I consider to be the most useful vantage point from my perspective: getting a basic “hello world” with an emphasis on

These are the steps I want to know before I start learning a language; it facilitates the learning process. Once I get this done, I can go back and understand why; I just need a platform to stand on. This is also being written as I am working on getting a basic OpenDylan setup working, so bear in mind that it may not always be the Right Way to do it. I’m hoping it’s enough to get me started in the right direction, though.

It may be useful to know a bit about my background to understand why I prioritise things so. I have spent most of my career doing Unix systems programming and platform security on both embedded systems and servers. What I look for is a way to understand a framework for exploring ideas, and I want to know how to have a minimal set up required to experiment with.

Installation

You can download the binary release directly. The current release, as of this writing, is 2014.1. It can be fetched from the OpenDylan site.

$ curl -O http://opendylan.org/downloads/opendylan/2014.1/opendylan-2014.1-x86-darwin.tar.bz2
$ tar xjf opendylan-2014.1-x86-darwin.tar.bz2 -C /usr/local
$ export PATH=$PATH:/usr/local/opendylan-2014.1/bin

You’ll probably want to add that last line into your profile.

I found that the 2014.1 build didn’t work on my x64-64 Linux system, but it was because I forgot to install the garbage collector.

$ sudo apt-get install libgc-dev
$ http://opendylan.org/downloads/opendylan/2014.1/opendylan-2014.1-x86_64-linux.tar.bz2
$ tar xjf opendylan-2014.1-x86_64-linux.tar.bz2 -C /usr/local
$ export PATH=$PATH:/usr/local/opendylan-2014.1/bin

That last line should probably be added to your profile.

Hello, world

The make-dylan-app builds an application. On my machine, my Dylan projects are in $HOME/code/dylan/; you’ll have to update paths as necessary.

$ cd code/dylan
$ make-dylan-app hellow
$ cd hellow
$ ls
hellow.dylan       	hellow.lid      	library.dylan      	registry

The Getting Started with the Open Dylan Command Line Tools document has a good explanation of these files:

make-dylan-app creates a directory with the same name as the application and three files:

  1. hello-world.lid – This says what other files are part of the project. The order in which the files are listed here determines the order in which the code in them is loaded.

  2. library.dylan contains simple library and module definitions. These can be extended as your project grows more complex.

  3. hello-world.dylan contains the main program.

The next step is to build this program.

$ dylan-compiler -build hellow.lid
$  ./_build/bin/hellow
Hello, world!

It’s useful to note that this produces a natively-compiled binary.

What’s in hello.dylan?

$ cat hellow.dylan
Module: hellow
Synopsis: 
Author: 
Copyright: 
 
define function main
(name :: <string>, arguments :: <vector>)
  format-out("Hello, world!\n");
  exit-application(0);
end function main;
 
main(application-name(), application-arguments());

The header here tells what module we’re in (main), and has room for some additional metadata.

The function definition is very similar to both the Common Lisp (with additional typing information normally found in a declare form) and Go mechanisms for doing this. For example, in Go this function would be:

func main(name string, arguments []interface{}) {
 fmt.Printf("Hello, world!\n")
 os.Exit(0)
}

The major difference is that main isn’t automatically called, like it would be in some other languages. Instead, a top-level function invocation needs to be performed.

The library.dylan file for the skeleton application contains

Module: dylan-user
 
define library hellow
  use common-dylan;
  use io;
end library hellow;
 
define module hellow
  use common-dylan;
  use format-out;
end module hellow;

A module contains a namespace, and is a basic unit of Dylan programs. A library is a collection of modules. The Introduction to Dylan has the following note about the role of libraries and modules:

Libraries import other libraries and export modules, whereas modules import other modules and export variables.

The registry

There’s another way to build Dylan code: the registry. This is a list of the libraries available. I haven’t quite worked out the naming scheme, but currently this is what works for me. Note that my code is $HOME/code/dylan.

$ cd $HOME/code/dylan
$ ls
hellow
$ mkdir -p registry/generic
$ echo "abstract://dylan/hellow/hellow.lid" > registry/generic/hellow
$ export OPEN_DYLAN_USER_REGISTRIES=$(pwd)/registry
$ cd /tmp
$ dylan-compiler -build hellow
$ /tmp/_build/bin/hellow
Hello, world!

See that now, instead of specifying a .lid file, the library name can be built instead. Instead of hellow, dylan could be built. This won’t produce an executable, but places the appropriately compiled libraries (in the context of this example) in /tmp/_build/lib/.

format-out

The format-out function uses virtually the same directives as the standard C printf(3) function. For example, the following is a trivial example of printing the slots in a class, added into the hellow library.

Module: hellow
Synopsis: 
Author: 
Copyright: 
 
define class <thing> (<object>)
  slot thing-name :: <string>,
init-keyword: name:;
end;
 
define function main
(name :: <string>, arguments :: <vector>)
  let thing1 = make(<thing>, name: "thing1");
  let thing2 = make(<thing>, name: "thing2");
  format-out("Hello, world!\n");
  format-out("Thing: %s\n", thing-name(thing1));
  format-out("Thing: %s\n", thing2.thing-name);
  exit-application(0);
end function main;
 
main(application-name(), application-arguments());

Getting set up with emacs

There’s an emacs major mode for Dylan called, appropriately, dylan-mode. This includes a SLiME-esque interaction mode called DIME. I have an elisp file in my personal emacs config named dylan.el that contains the following:

;;; dylan hacking stuff
 
(require 'dylan-mode)
(setq inferior-dylan-program "/usr/local/opendylan/bin/dswank")
(require 'dime)
(dime-setup '(dime-dylan dime-repl))
(setenv "OPEN_DYLAN_USER_REGISTRIES" "/home/kyle/code/dylan/registry")

Now, load up the hellow.dylan file that was produced earlier, and call M-x dime. Chapter Nine of [Getting Started with the Open Dylan Command Line Tools (http://opendylan.org/documentation/getting-started-cli/index.html) has a good introduction to using DIME.

Conclusions

At this point, I have a basic framework that I begin to use for learning Dylan.