.de EX.nr x \\$1v\\!h0c n \\nx 0...de FG \" start figure caption: .FG filename.ps verticalsize.KF.BP \\$1 \\$2.sp .5v.EX \\$2v.ps -1.vs -1...de fg \" end figure caption (yes, it is clumsy).ps.vs.br.KE...TLA Descent into Limbo.AUBrian W. Kernighan.AIbwk@bell-labs.com.brRevised April 2005 by Vita Nuova.AB.DS B.ps -2.vs -1``If, reader, you are slow now to believeWhat I shall tell, that is no cause for wonder,For I who saw it hardly can accept it.''.ft R Dante Alighieri, \fIInferno\fP, Canto XXV..ps +2.vs +1.DE.LPLimbo is a new programming language, designed bySean 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 ona virtual machine..LPThis paper is a brief introduction to Limbo.Since Limbo is an integral part of the Inferno system,the examples here illustrate not onlythe language but also a certain amount about how to writeprograms to run within Inferno..AE.NH 1Introduction.LPThis document is a quick look at the basicsof Limbo; it is not a replacement for the reference manual.The first section is a short overview ofconcepts 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-manipulationexamples, since they require less background to understand..SHModules:.LPA Limbo program is a set of modules that cooperateto perform a task.In source form, a module consists of a.CW "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.CW ".m" file so it can be included by other modules,and the implementation is stored in a.CW ".b" file.Modules may have multiple implementations,each in a separate implementation file..LPModules are always loaded dynamically, at run time: the Limbo.CW "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..LPLimbo is strongly typed; programs are checked at compile time,and further when modules are loaded.The Limbo compiler compiles each source file into amachine-independent byte-coded .CW ".dis" file that can be loaded at run time..SHFunctions and variables:.LPFunctions are associated with specific modules, either directly oras members of abstract data types within a module.Functions are visible outside their module onlyif they are part of the module interface.If the target module is loaded, specific namescan be used in a qualified form like.CW "sys->print" or without the qualifier if imported with an explicit.CW "import" statement..LPBesides normal block structure within functions,variables may have global scope within a module;module data can be accessed via the module pointer..SHData:.LPThe numeric types are:.RS.TSlf(CW) lf(R)w(3i) .byte unsigned, 8 bitsint signed, 32 bitsbig signed, 64 bitsreal IEEE long float, 64 bits.TE.REThe size and signedness of integral types areas specified above, and will be the same everywhere.Character constants are enclosed in single quotesand may use escapes like.CW "'\en'" or.CW "'\eudddd'" ,but the characters themselvesare in Unicode and have type.CW "int" .There is no enumeration type, but there is a.CW "con" declaration that creates a named constant, and a special.CW "iota"operation that can be used to generate unique values..LPLimbo also providesUnicode 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 functionmembers 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)..LPA channel is a mechanism for synchronized communication.It provides a place for one process to send or receivean object of a specific type;the attempt to send or receive blocks until a matching receive or sendis attempted by another process.The .CW "alt" statement selects randomly but fairly among channelsthat are ready to read or write.The.CW "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'' inother operating systems.).LPLimbo performs automatic garbage collection, so there is noneed to free dynamically created objects.Objects are deleted and their resources freed whenthe 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-cyclicwhen required.).SHOperators and expressions:.LPLimbo provides many of C's operators,but not the.CW "?:" or`comma' (sequential execution) operators.Pointers, or `references', created with.CW "ref" ,are restricted compared to C: they can only refer to adt values on the heap.There is no.CW "&" (address of) operator, nor is address arithmetic possible.Arrays are also reference types, however,and sincearray slicing is supported, that replacesmany of C's pointer constructions..LPThere are no implicit coercions between types,and only a handful of explicit casts.The numeric types.CW "byte" ,.CW "int" ,etc., can be used to convert a numeric expression, as in.P1nl := byte 10;.P2and.CW "string" can be used as a unary operator to convert any numeric expressionto a string (in.CW "%g" format) and to convert an array of bytes in UTF-8 format to a Limbo.CW stringvalue.In the other direction, the cast.CW "array of byte"converts a string to its UTF-8 representation in an array of bytes..SHStatements:.LPStatements 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.P1if (\fIexpr\fP) \fIstat\fPif (\fIexpr\fP) \fIstat\fP else \fIstat\fPwhile (\fIexpr\fP) \fIstat\fPfor (\fIexpr\fP; \fIexpr\fP; \fIexpr\fP) \fIstat\fPdo \fIstat\fP while (\fIexpr\fP) ;return \fIexpr\fP ;exit ;.P2The.CW "exit" statement terminates a process and frees its resources.There is also a .CW "case" statement analogous to C's.CW "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 anotherbut stops without requiring an explicit.CW break(in that respect it is closer to Pascal's.CW casestatement, hence the change of name).A.CW "break" or.CW "continue" followed by a labelcauses a break out of, or the next iteration of, the enclosingconstruct that is labeled with the same label..LPComments begin with.CW "#" and extend to the end of the line.There is no preprocessor, but an.CW "include" statement can be used to include source code, usually module declaration files..SHLibraries:.LPLimbo has an extensive and growing set of standard libraries,each implemented as a module.A handful of these(notably.CW "Sys" ,.CW "Draw" ,and.CW "Tk" )are included in the Inferno kernel because they will beneeded to support almost any Limbo program.Among the others are.CW "Bufio" ,a buffered I/O package based on Plan 9's Bio;.CW "Regex" ,for regular expressions;and.CW "Math" ,for mathematical functions.Some of the examples that follow provide the sortof functionality that might be a suitable module..NH 1Examples.LPThe examples in this section are each complete, in the sense that theywill run as presented; I have tried to avoid code fragmentsthat merely illustrate syntax..NH 2Hello, World.LPThe first example is the traditional ``hello, world'',in the file.CW "hello.b" :.P1implement 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\en");}.P2An implementation file implements a single module,named in the.CW "implement" declaration at the top of the file.The two .CW "include" lines copy interface definitions from two other modules,.CW "Sys" (which describes a variety of system functions like.CW "print" ),and.CW "Draw" (which describes a variety of graphics types and functions,only one of which,.CW "Context" ,is used here)..LPThe .CW "module" declaration defines the external interface that this modulepresents to the rest of the world.In this case, it's a single function named.CW "init" .Since this module is to be called from a command interpreter(shell), by convention its .CW "init" function takes two arguments,the graphical contextand a list of strings, the command-line arguments,though neither is used here.This is like.CW "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 declarationappears in the same file as its implementation..LPMost modules have a more extensive set of declarations; for example, .CW "draw.m" is 298 lines of constants, function prototypes, andtype declarations for graphics types like.CW "Point" and .CW "Rect" ,and.CW "sys.m" is 160 lines of declarations for functions like.CW "open" ,.CW "read" ,and.CW "print" .Most module declarations are therefore stored in separate files,conventionally suffixed with.CW ".m" ,so they can be included in other modules.The system library module declaration files are collected in the.CW moduledirectory at the root of the Inferno source tree.Modules that are components of a single program are typicallystored in that program's source directory..LPThe last few lines of.CW "hello.b" are the implementation of the.CW "init" function, which loads the.CW "Sys" module, then calls its .CW "print" function.By convention, each module declaration includes a pathname constantthat points to the code for the module; this is the second parameter.CW "Sys->PATH" of the.CW "load" statement.Note that the.CW Drawmodule is not loaded because none of its functions is used, butit is included to define the type.CW Draw->Context ..SHCompiling and Running Limbo Programs.LPWith this much of the language described,we can compile and run this program.On Unix or Windows, the command.P1$ limbo -g hello.b.P2creates.CW "hello.dis" ,a byte-coded version of the program for the Disvirtual machine.The.CW "-g" argument adds a symbol table, useful for subsequent debugging.(Another common option is.CW -w ,which causes the compiler to produce helpful warnings about possible errors.)The program can then be run as.CW "hello" in Inferno; this shows execution under the Inferno emulatoron a Unix system:.P1$ limbo -g hello.b$ emu; /usr/bwk/hellohello, world; .P2From within Inferno, it's also possible to run aprogram by selecting it from a menu.In any case, as the program runs, it loads as necessary other modules that it uses..NH 2A Graphical "Hello World".LPThe following module creates and displays a window containing onlya button with the label ``hello, world'' as shown in the screen shot in Figure 1..P1implement 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}.P2.FG "f1.ps" 3i.ce.I "Figure 1. `Hello, world' button.".fgThis is not very exciting, but it illustrates the absoluteminimum required to get a picture on the screen.The.CW "Tk" module is modeled closely after John Ousterhout's Tk interface toolkit,but Limbo is used as the programming language instead of Tcl.The Inferno versionis similar in functionality to the original Tkbut it does not support any Tcl constructs,such as variables, procedures, or expression evaluation,since all processing is done using Limbo.There are ten functions in the.CW "Tk" interface, only one of whichis used here:.CW "cmd" ,which executes a command string.(It is the most commonly used.CW Tkfunction.)Lines 1–500 of 2056. Load more
.de EX .nr x \\$1v \\!h0c n \\nx 0 .. .de FG \" start figure caption: .FG filename.ps verticalsize .KF .BP \\$1 \\$2 .sp .5v .EX \\$2v .ps -1 .vs -1 .. .de fg \" end figure caption (yes, it is clumsy) .ps .vs .br .KE .. .TL A Descent into Limbo .AU Brian W. Kernighan .AI bwk@bell-labs.com .br Revised April 2005 by Vita Nuova .AB .DS B .ps -2 .vs -1 ``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.'' .ft R Dante Alighieri, \fIInferno\fP, Canto XXV. .ps +2 .vs +1 .DE .LP 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. .LP 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. .AE .NH 1 Introduction .LP 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. .SH Modules: .LP A Limbo program is a set of modules that cooperate to perform a task. In source form, a module consists of a .CW "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 .CW ".m" file so it can be included by other modules, and the implementation is stored in a .CW ".b" file. Modules may have multiple implementations, each in a separate implementation file. .LP Modules are always loaded dynamically, at run time: the Limbo .CW "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. .LP 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 .CW ".dis" file that can be loaded at run time. .SH Functions and variables: .LP 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 .CW "sys->print" or without the qualifier if imported with an explicit .CW "import" statement. .LP Besides normal block structure within functions, variables may have global scope within a module; module data can be accessed via the module pointer. .SH Data: .LP The numeric types are: .RS .TS lf(CW) lf(R)w(3i) . byte unsigned, 8 bits int signed, 32 bits big signed, 64 bits real IEEE long float, 64 bits .TE .RE 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 .CW "'\en'" or .CW "'\eudddd'" , but the characters themselves are in Unicode and have type .CW "int" . There is no enumeration type, but there is a .CW "con" declaration that creates a named constant, and a special .CW "iota" operation that can be used to generate unique values. .LP 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). .LP 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 .CW "alt" statement selects randomly but fairly among channels that are ready to read or write. The .CW "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.) .LP 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.) .SH Operators and expressions: .LP Limbo provides many of C's operators, but not the .CW "?:" or `comma' (sequential execution) operators. Pointers, or `references', created with .CW "ref" , are restricted compared to C: they can only refer to adt values on the heap. There is no .CW "&" (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. .LP There are no implicit coercions between types, and only a handful of explicit casts. The numeric types .CW "byte" , .CW "int" , etc., can be used to convert a numeric expression, as in .P1 nl := byte 10; .P2 and .CW "string" can be used as a unary operator to convert any numeric expression to a string (in .CW "%g" format) and to convert an array of bytes in UTF-8 format to a Limbo .CW string value. In the other direction, the cast .CW "array of byte" converts a string to its UTF-8 representation in an array of bytes. .SH Statements: .LP 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 .P1 if (\fIexpr\fP) \fIstat\fP if (\fIexpr\fP) \fIstat\fP else \fIstat\fP while (\fIexpr\fP) \fIstat\fP for (\fIexpr\fP; \fIexpr\fP; \fIexpr\fP) \fIstat\fP do \fIstat\fP while (\fIexpr\fP) ; return \fIexpr\fP ; exit ; .P2 The .CW "exit" statement terminates a process and frees its resources. There is also a .CW "case" statement analogous to C's .CW "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 .CW break (in that respect it is closer to Pascal's .CW case statement, hence the change of name). A .CW "break" or .CW "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. .LP Comments begin with .CW "#" and extend to the end of the line. There is no preprocessor, but an .CW "include" statement can be used to include source code, usually module declaration files. .SH Libraries: .LP Limbo has an extensive and growing set of standard libraries, each implemented as a module. A handful of these (notably .CW "Sys" , .CW "Draw" , and .CW "Tk" ) are included in the Inferno kernel because they will be needed to support almost any Limbo program. Among the others are .CW "Bufio" , a buffered I/O package based on Plan 9's Bio; .CW "Regex" , for regular expressions; and .CW "Math" , for mathematical functions. Some of the examples that follow provide the sort of functionality that might be a suitable module. .NH 1 Examples .LP 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. .NH 2 Hello, World .LP The first example is the traditional ``hello, world'', in the file .CW "hello.b" : .P1 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\en"); } .P2 An implementation file implements a single module, named in the .CW "implement" declaration at the top of the file. The two .CW "include" lines copy interface definitions from two other modules, .CW "Sys" (which describes a variety of system functions like .CW "print" ), and .CW "Draw" (which describes a variety of graphics types and functions, only one of which, .CW "Context" , is used here). .LP The .CW "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 .CW "init" . Since this module is to be called from a command interpreter (shell), by convention its .CW "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 .CW "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. .LP Most modules have a more extensive set of declarations; for example, .CW "draw.m" is 298 lines of constants, function prototypes, and type declarations for graphics types like .CW "Point" and .CW "Rect" , and .CW "sys.m" is 160 lines of declarations for functions like .CW "open" , .CW "read" , and .CW "print" . Most module declarations are therefore stored in separate files, conventionally suffixed with .CW ".m" , so they can be included in other modules. The system library module declaration files are collected in the .CW 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. .LP The last few lines of .CW "hello.b" are the implementation of the .CW "init" function, which loads the .CW "Sys" module, then calls its .CW "print" function. By convention, each module declaration includes a pathname constant that points to the code for the module; this is the second parameter .CW "Sys->PATH" of the .CW "load" statement. Note that the .CW Draw module is not loaded because none of its functions is used, but it is included to define the type .CW Draw->Context . .SH Compiling and Running Limbo Programs .LP With this much of the language described, we can compile and run this program. On Unix or Windows, the command .P1 $ limbo -g hello.b .P2 creates .CW "hello.dis" , a byte-coded version of the program for the Dis virtual machine. The .CW "-g" argument adds a symbol table, useful for subsequent debugging. (Another common option is .CW -w , which causes the compiler to produce helpful warnings about possible errors.) The program can then be run as .CW "hello" in Inferno; this shows execution under the Inferno emulator on a Unix system: .P1 $ limbo -g hello.b $ emu ; /usr/bwk/hello hello, world ; .P2 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. .NH 2 A Graphical "Hello World" .LP 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. .P1 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 } .P2 .FG "f1.ps" 3i .ce .I "Figure 1. `Hello, world' button." .fg This is not very exciting, but it illustrates the absolute minimum required to get a picture on the screen. The .CW "Tk" module is modeled closely after John Ousterhout's Tk interface toolkit, but Limbo is used as the programming language instead of Tcl. The Inferno version is similar in functionality to the original Tk but it does not support any Tcl constructs, such as variables, procedures, or expression evaluation, since all processing is done using Limbo. There are ten functions in the .CW "Tk" interface, only one of which is used here: .CW "cmd" , which executes a command string. (It is the most commonly used .CW Tk function.)