A Descent into Limbo

Brian W. Kernighan

bwk@bell-labs.com

Revised April 2005 by Vita Nuova

ABSTRACT

‘‘If, reader, you are slow now to believe

What I shall tell, that is no cause for wonder,

For I who saw it hardly can accept it.’’

    Dante Alighieri, Inferno, Canto XXV.

Limbo is a new programming language, designed by Sean Dorward, Phil Winterbottom, and Rob Pike. Limbo borrows from, among other things, C (expression syntax and control flow), Pascal (declarations), Winterbottom’s Alef (abstract data types and channels), and Hoare’s CSP and Pike’s Newsqueak (processes). Limbo is strongly typed, provides automatic garbage collection, supports only very restricted pointers, and compiles into machine-independent byte code for execution on a virtual machine.

This paper is a brief introduction to Limbo. Since Limbo is an integral part of the Inferno system, the examples here illustrate not only the language but also a certain amount about how to write programs to run within Inferno.

1. Introduction

This document is a quick look at the basics of Limbo; it is not a replacement for the reference manual. The first section is a short overview of concepts and constructs; subsequent sections illustrate the language with examples. Although Limbo is intended to be used in Inferno, which emphasizes networking and graphical interfaces, the discussion here begins with standard text-manipulation examples, since they require less background to understand.

Modules:

A Limbo program is a set of modules that cooperate to perform a task. In source form, a module consists of a module declaration that specifies the public interface - the functions, abstract data types, and constants that the module makes visible to other modules - and an implementation that provides the actual code. By convention, the module declaration is usually placed in a separate .m file so it can be included by other modules, and the implementation is stored in a .b file. Modules may have multiple implementations, each in a separate implementation file.

Modules are always loaded dynamically, at run time: the Limbo load operator fetches the code and performs run-time type checking. Once a module has been loaded, its functions can be called. Several instances of the same module type can be in use at once, with possibly different implementations.

Limbo is strongly typed; programs are checked at compile time, and further when modules are loaded. The Limbo compiler compiles each source file into a machine-independent byte-coded .dis file that can be loaded at run time.

Functions and variables:

Functions are associated with specific modules, either directly or as members of abstract data types within a module. Functions are visible outside their module only if they are part of the module interface. If the target module is loaded, specific names can be used in a qualified form like sys->print or without the qualifier if imported with an explicit import statement.

Besides normal block structure within functions, variables may have global scope within a module; module data can be accessed via the module pointer.

Data:

The numeric types are:

spacer

The size and signedness of integral types are as specified above, and will be the same everywhere. Character constants are enclosed in single quotes and may use escapes like ’\n’ or ’\udddd’, but the characters themselves are in Unicode and have type int. There is no enumeration type, but there is a con declaration that creates a named constant, and a special iota operation that can be used to generate unique values.

Limbo also provides Unicode strings, arrays of arbitrary types, lists of arbitrary types, tuples (in effect, unnamed structures with unnamed members of arbitrary types), abstract data types or adt’s (in effect, named structures with function members as well as data members), reference types (in effect, restricted pointers that can point only to adt objects), and typed channels (for passing objects between processes).

A channel is a mechanism for synchronized communication. It provides a place for one process to send or receive an object of a specific type; the attempt to send or receive blocks until a matching receive or send is attempted by another process. The alt statement selects randomly but fairly among channels that are ready to read or write. The spawn statement creates a new process that, except for its stack, shares memory with other processes. Processes are pre-emptively scheduled by the Inferno kernel. (Inferno processes are sometimes called ‘‘threads’’ in other operating systems.)

Limbo performs automatic garbage collection, so there is no need to free dynamically created objects. Objects are deleted and their resources freed when the last reference to them goes away. This release of resources happens immediately (‘‘instant free’’) for non-cyclic structures; release of cyclic data structures might be delayed but will happen eventually. (The language allows the programmer to ensure a given structure is non-cyclic when required.)

Operators and expressions:

Limbo provides many of C’s operators, but not the ?: or ‘comma’ (sequential execution) operators. Pointers, or ‘references’, created with ref, are restricted compared to C: they can only refer to adt values on the heap. There is no & (address of) operator, nor is address arithmetic possible. Arrays are also reference types, however, and since array slicing is supported, that replaces many of C’s pointer constructions.

There are no implicit coercions between types, and only a handful of explicit casts. The numeric types byte, int, etc., can be used to convert a numeric expression, as in

nl := byte 10;

and string can be used as a unary operator to convert any numeric expression to a string (in %g format) and to convert an array of bytes in UTF-8 format to a Limbo string value. In the other direction, the cast array of byte converts a string to its UTF-8 representation in an array of bytes.

Statements:

Statements and control flow in Limbo are similar to those in C. A statement is an expression followed by a semicolon, or a sequence of statements enclosed in braces. The similar control flow statements are

if (exprstat

if (exprstat else stat

while (exprstat

for (exprexprexprstat

do stat while (expr) ;

return expr ;

exit ;

The exit statement terminates a process and frees its resources. There is also a case statement analogous to C’s switch, but it differs in that it also supports string and range tests, and more critically, control flow does not ‘‘flow through’’ one arm of the case to another but stops without requiring an explicit break (in that respect it is closer to Pascal’s case statement, hence the change of name). A break or continue followed by a label causes a break out of, or the next iteration of, the enclosing construct that is labeled with the same label.

Comments begin with # and extend to the end of the line. There is no preprocessor, but an include statement can be used to include source code, usually module declaration files.

Libraries:

Limbo has an extensive and growing set of standard libraries, each implemented as a module. A handful of these (notably Sys, Draw, and Tk) are included in the Inferno kernel because they will be needed to support almost any Limbo program. Among the others are Bufio, a buffered I/O package based on Plan 9’s Bio; Regex, for regular expressions; and Math, for mathematical functions. Some of the examples that follow provide the sort of functionality that might be a suitable module.

2. Examples

The examples in this section are each complete, in the sense that they will run as presented; I have tried to avoid code fragments that merely illustrate syntax.

2.1. Hello, World

The first example is the traditional ‘‘hello, world’’, in the file hello.b:

implement Hello;

include "sys.m";

    sys: Sys;

include "draw.m";

Hello: module

{

    init:   fn(ctxt: ref Draw->Context, args: list of string);

};

init(ctxt: ref Draw->Context, args: list of string)

{

    sys = load Sys Sys->PATH;

    sys->print("hello, world\n");

}

An implementation file implements a single module, named in the implement declaration at the top of the file. The two include lines copy interface definitions from two other modules, Sys (which describes a variety of system functions like print), and Draw (which describes a variety of graphics types and functions, only one of which, Context, is used here).

The module declaration defines the external interface that this module presents to the rest of the world. In this case, it’s a single function named init. Since this module is to be called from a command interpreter (shell), by convention its init function takes two arguments, the graphical context and a list of strings, the command-line arguments, though neither is used here. This is like main in a C program. Essentially all of the other examples begin with this standard code. Commands are unusual, though, in that a command’s module declaration appears in the same file as its implementation.

Most modules have a more extensive set of declarations; for example, draw.m is 298 lines of constants, function prototypes, and type declarations for graphics types like Point and Rect, and sys.m is 160 lines of declarations for functions like open, read, and print. Most module declarations are therefore stored in separate files, conventionally suffixed with .m, so they can be included in other modules. The system library module declaration files are collected in the module directory at the root of the Inferno source tree. Modules that are components of a single program are typically stored in that program’s source directory.

The last few lines of hello.b are the implementation of the init function, which loads the Sys module, then calls its print function. By convention, each module declaration includes a pathname constant that points to the code for the module; this is the second parameter Sys->PATH of the load statement. Note that the Draw module is not loaded because none of its functions is used, but it is included to define the type Draw->Context.

Compiling and Running Limbo Programs

With this much of the language described, we can compile and run this program. On Unix or Windows, the command

$ limbo -g hello.b

creates hello.dis, a byte-coded version of the program for the Dis virtual machine. The -g argument adds a symbol table, useful for subsequent debugging. (Another common option is -w, which causes the compiler to produce helpful warnings about possible errors.) The program can then be run as hello in Inferno; this shows execution under the Inferno emulator on a Unix system:

$ limbo -g hello.b

$ emu

; /usr/bwk/hello

hello, world

From within Inferno, it’s also possible to run a program by selecting it from a menu. In any case, as the program runs, it loads as necessary other modules that it uses.

2.2. A Graphical "Hello World"

The following module creates and displays a window containing only a button with the label ‘‘hello, world’’ as shown in the screen shot in Figure 1.

implement Hello2;

include "sys.m";

    sys: Sys;

include "draw.m";

    draw: Draw;

include "tk.m";

    tk: Tk;

include "tkclient.m";

    tkclient: Tkclient;

Hello2: module

{

        init:   fn(ctxt: ref Draw->Context, args: list of string);

};

init(ctxt: ref Draw->Context, args: list of string)

{

    sys = load Sys Sys->PATH;

    tk = load Tk Tk->PATH;

    tkclient = load Tkclient Tkclient->PATH;

    tkclient->init();

    (t, nil) := tkclient->toplevel(ctxt, "", "Hello", Tkclient->Plain);

    tk->cmd(t, "button .b -text {hello, world}");

    tk->cmd(t, "pack .b");

    tk->cmd(t, "update");

    tkclient->onscreen(t, nil);

    sys->sleep(10000);  # wait 10 seconds

}

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.