2.5.2 Components of a Dylan program

We start with this simple Dylan expression:

format-out("Hello, world\n"); 

All Dylan expressions must be in a module. Therefore, we use a text editor to create a file that contains the expression within a module:

The program file: hello.dylan.

module: hello

format-out("Hello, world\n"); 

The hello.dylan file is the top-level file; you can think of it as the program itself. When you run this program, Dylan executes all the expressions in the file in the order that they appear in the file. There is only one expression in this program — the call to format-out.

The first line of this file declares that the expressions and definitions in this file are in the hello module. Before we can run (or even compile) this program, we need to define the hello module. All modules must be in a library, so we must also define a library for our hello module. We create a second file, called the library file, and define the hello module and hello library in the library file:

The library file: library.dylan.

module: dylan-user						

define library hello
  use dylan; 
  use format-out;
end library hello;
define module hello
  use dylan; 
  use format-out;
end module hello; 

The first line of library.dylan states that the expressions in this file are in the dylan-user module. Every Dylan expression and definition must be in a module, including the definitions of libraries and modules. The dylan-user module is the starting point — the predefined module that enables you to define the libraries and modules that your program uses.

In the file library.dylan, we define a library named hello, and a module named hello. We define the hello library to use the dylan library and the format-out library, and we define the hello module to use the dylan module and the format-out module.

One library uses another library to allow its modules to use the other library's exported modules. Most libraries need to use the dylan library, because it contains the dylan module. One module uses another module to allow its definitions to use the other module's exported definitions. Most modules need to use the dylan module in the dylan library, because that module contains the definitions of the core Dylan language. We also need to use the format-out module in the format-out library, because that module defines the format-out function, which we use in our program.

Finally, we create a LID file that enumerates the files that make up the library. This file does not contain Dylan expressions, but rather is simply a textual description of the library's files:

The LID file: hello.lid.

library: hello
files:   library 
         hello 

The LID file simply states that the library hello comprises two files, named library and hello. In other words, to build the hello library, the compiler must process the two files listed, in the order that they appear in the file. The order is significant, because a module must be defined before the code that is in the module can be analyzed and compiled.

You can consult the documentation of your Dylan implementation to find out how to build an executable program from these files, and how to run that program once it is built. Most Dylan environments produce executable programs that can be invoked in the same manner as any other program on the particular platform that you are using.

We incur a fair amount of overhead in setting up the files that make up a simple program. Most environments automate this process — some of the complexity shown here occurs because we are working with the lowest common denominator: interchange files. The advantages of libraries and modules are significant for larger programs. See Chapter 13, Libraries and Modules.