Commit | Line | Data |
---|---|---|
38a93523 NJ |
1 | @c $Id: intro.texi,v 1.1 2001-03-09 08:21:59 ossau Exp $ |
2 | ||
3 | @page | |
4 | @node What is Guile? | |
5 | @chapter What is Guile? | |
6 | ||
7 | Guile is an interpreter for the Scheme programming language, packaged | |
8 | for use in a wide variety of environments. Guile implements Scheme as | |
9 | described in the | |
10 | @tex | |
11 | Revised$^5$ | |
12 | @end tex | |
13 | @ifinfo | |
14 | Revised^5 | |
15 | @end ifinfo | |
16 | Report on the Algorithmic Language Scheme (usually known as R5RS), | |
17 | providing clean and general data and control structures. Guile goes | |
18 | beyond the rather austere language presented in R5RS, extending it with | |
19 | a module system, full access to POSIX system calls, networking support, | |
20 | multiple threads, dynamic linking, a foreign function call interface, | |
21 | powerful string processing, and many other features needed for | |
22 | programming in the real world. | |
23 | ||
24 | Like a shell, Guile can run interactively, reading expressions from the | |
25 | user, evaluating them, and displaying the results, or as a script | |
26 | interpreter, reading and executing Scheme code from a file. However, | |
27 | Guile is also packaged as an object library, allowing other applications | |
28 | to easily incorporate a complete Scheme interpreter. An application can | |
29 | use Guile as an extension language, a clean and powerful configuration | |
30 | language, or as multi-purpose ``glue'', connecting primitives provided | |
31 | by the application. It is easy to call Scheme code from C code and vice | |
32 | versa, giving the application designer full control of how and when to | |
33 | invoke the interpreter. Applications can add new functions, data types, | |
34 | control structures, and even syntax to Guile, creating a domain-specific | |
35 | language tailored to the task at hand, but based on a robust language | |
36 | design. | |
37 | ||
38 | Guile's module system allows one to break up a large program into | |
39 | manageable sections with well-defined interfaces between them. Modules | |
40 | may contain a mixture of interpreted and compiled code; Guile can use | |
41 | either static or dynamic linking to incorporate compiled code. Modules | |
42 | also encourage developers to package up useful collections of routines | |
43 | for general distribution; as of this writing, one can find Emacs | |
44 | interfaces, database access routines, compilers, GUI toolkit interfaces, | |
45 | and HTTP client functions, among others. | |
46 | ||
47 | In the future, we hope to expand Guile to support other languages like | |
48 | Tcl and Perl by translating them to Scheme code. This means that users | |
49 | can program applications which use Guile in the language of their | |
50 | choice, rather than having the tastes of the application's author | |
51 | imposed on them. | |
52 | ||
53 | ||
54 | @page | |
55 | @node Whirlwind Tour | |
56 | @chapter A Whirlwind Tour | |
57 | ||
58 | This chapter presents a quick tour of all the ways that Guile can be | |
59 | used. | |
60 | ||
61 | @menu | |
62 | * Running Guile Interactively:: | |
63 | * Guile Scripts:: | |
64 | * Linking Programs With Guile:: | |
65 | * Writing Guile Modules:: | |
66 | @end menu | |
67 | ||
68 | ||
69 | @node Running Guile Interactively | |
70 | @section Running Guile Interactively | |
71 | ||
72 | In its simplest form, Guile acts as an interactive interpreter for the | |
73 | Scheme programming language, reading and evaluating Scheme expressions | |
74 | the user enters from the terminal. Here is a sample interaction between | |
75 | Guile and a user; the user's input appears after the @code{$} and | |
76 | @code{guile>} prompts: | |
77 | ||
78 | @example | |
79 | $ guile | |
80 | guile> (+ 1 2 3) ; add some numbers | |
81 | 6 | |
82 | guile> (define (factorial n) ; define a function | |
83 | (if (zero? n) 1 (* n (factorial (- n 1))))) | |
84 | guile> (factorial 20) | |
85 | 2432902008176640000 | |
86 | guile> (getpwnam "jimb") ; find my entry in /etc/passwd | |
87 | #("jimb" ".0krIpK2VqNbU" 4008 10 "Jim Blandy" "/u/jimb" | |
88 | "/usr/local/bin/bash") | |
89 | guile> @kbd{C-d} | |
90 | $ | |
91 | @end example | |
92 | ||
93 | @c [[When we get a fancier read-eval-print loop, with features for bouncing | |
94 | @c around among modules, referring to the value of the last expression, | |
95 | @c etc. then this section will get longer.]] | |
96 | ||
97 | ||
98 | @node Guile Scripts | |
99 | @section Guile Scripts | |
100 | ||
101 | Like AWK, Perl, or any shell, Guile can interpret script files. A Guile | |
102 | script is simply a file of Scheme code with some extra information at | |
103 | the beginning which tells the operating system how to invoke Guile, and | |
104 | then tells Guile how to handle the Scheme code. | |
105 | ||
106 | Before we present the details, here is a trivial Guile script: | |
107 | ||
108 | @example | |
109 | #!/usr/local/bin/guile -s | |
110 | !# | |
111 | (display "Hello, world!") | |
112 | (newline) | |
113 | @end example | |
114 | ||
115 | @menu | |
116 | * The Top of a Script File:: How to start a Guile script. | |
117 | * Scripting Examples:: Simple Guile scripts, explained. | |
118 | @end menu | |
119 | ||
120 | ||
121 | @node The Top of a Script File | |
122 | @subsection The Top of a Script File | |
123 | ||
124 | The first line of a Guile script must tell the operating system to use | |
125 | Guile to evaluate the script, and then tell Guile how to go about doing | |
126 | that. Here is the simplest case: | |
127 | ||
128 | @itemize @bullet | |
129 | ||
130 | @item | |
131 | The first two characters of the file must be @samp{#!}. | |
132 | ||
133 | The operating system interprets this to mean that the rest of the line | |
134 | is the name of an executable that can interpret the script. Guile, | |
135 | however, interprets these characters as the beginning of a multi-line | |
136 | comment, terminated by the characters @samp{!#} on a line by themselves. | |
137 | (This is an extension to the syntax described in R5RS, added to support | |
138 | shell scripts.) | |
139 | ||
140 | @item | |
141 | Immediately after those two characters must come the full pathname to | |
142 | the Guile interpreter. On most systems, this would be | |
143 | @samp{/usr/local/bin/guile}. | |
144 | ||
145 | @item | |
146 | Then must come a space, followed by a command-line argument to pass to | |
147 | Guile; this should be @samp{-s}. This switch tells Guile to run a | |
148 | script, instead of soliciting the user for input from the terminal. | |
149 | There are more elaborate things one can do here; see @ref{The Meta | |
150 | Switch}. | |
151 | ||
152 | @item | |
153 | Follow this with a newline. | |
154 | ||
155 | @item | |
156 | The second line of the script should contain only the characters | |
157 | @samp{!#} --- just like the top of the file, but reversed. The | |
158 | operating system never reads this far, but Guile treats this as the end | |
159 | of the comment begun on the first line by the @samp{#!} characters. | |
160 | ||
161 | @item | |
162 | The rest of the file should be a Scheme program. | |
163 | ||
164 | @end itemize | |
165 | ||
166 | Guile reads the program, evaluating expressions in the order that they | |
167 | appear. Upon reaching the end of the file, Guile exits. | |
168 | ||
169 | The function @code{command-line} returns the name of the script file and | |
170 | any command-line arguments passed by the user, as a list of strings. | |
171 | ||
172 | For example, consider the following script file: | |
173 | @example | |
174 | #!/usr/local/bin/guile -s | |
175 | !# | |
176 | (write (command-line)) | |
177 | (newline) | |
178 | @end example | |
179 | ||
180 | If you put that text in a file called @file{foo} in the current | |
181 | directory, then you could make it executable and try it out like this: | |
182 | @example | |
183 | $ chmod a+x foo | |
184 | $ ./foo | |
185 | ("./foo") | |
186 | $ ./foo bar baz | |
187 | ("./foo" "bar" "baz") | |
188 | $ | |
189 | @end example | |
190 | ||
191 | As another example, here is a simple replacement for the POSIX | |
192 | @code{echo} command: | |
193 | @example | |
194 | #!/usr/local/bin/guile -s | |
195 | !# | |
196 | (for-each (lambda (s) (display s) (display " ")) | |
197 | (cdr (command-line))) | |
198 | (newline) | |
199 | @end example | |
200 | ||
201 | @deffn procedure command-line | |
202 | @deffnx primitive program-arguments | |
203 | Return a list of the command-line arguments passed to the currently | |
204 | running program. If the program invoked Guile with the @samp{-s}, | |
205 | @samp{-c} or @samp{--} switches, these procedures ignore everything up | |
206 | to and including those switches. | |
207 | @end deffn | |
208 | ||
209 | ||
210 | @node Scripting Examples | |
211 | @subsection Scripting Examples | |
212 | ||
213 | To start with, here are some examples of invoking Guile directly: | |
214 | ||
215 | @table @code | |
216 | ||
217 | @item guile -- a b c | |
218 | Run Guile interactively; @code{(command-line)} will return @* | |
219 | @code{("/usr/local/bin/guile" "a" "b" "c")}. | |
220 | ||
221 | @item guile -s /u/jimb/ex2 a b c | |
222 | Load the file @file{/u/jimb/ex2}; @code{(command-line)} will return @* | |
223 | @code{("/u/jimb/ex2" "a" "b" "c")}. | |
224 | ||
225 | @item guile -c '(write %load-path) (newline)' | |
226 | Write the value of the variable @code{%load-path}, print a newline, | |
227 | and exit. | |
228 | ||
229 | @item guile -e main -s /u/jimb/ex4 foo | |
230 | Load the file @file{/u/jimb/ex4}, and then call the function | |
231 | @code{main}, passing it the list @code{("/u/jimb/ex4" "foo")}. | |
232 | ||
233 | @item guile -l first -ds -l last -s script | |
234 | Load the files @file{first}, @file{script}, and @file{last}, in that | |
235 | order. The @code{-ds} switch says when to process the @code{-s} | |
236 | switch. For a more motivated example, see the scripts below. | |
237 | ||
238 | @end table | |
239 | ||
240 | ||
241 | Here is a very simple Guile script: | |
242 | @example | |
243 | #!/usr/local/bin/guile -s | |
244 | !# | |
245 | (display "Hello, world!") | |
246 | (newline) | |
247 | @end example | |
248 | The first line marks the file as a Guile script. When the user invokes | |
249 | it, the system runs @file{/usr/local/bin/guile} to interpret the script, | |
250 | passing @code{-s}, the script's filename, and any arguments given to the | |
251 | script as command-line arguments. When Guile sees @code{-s | |
252 | @var{script}}, it loads @var{script}. Thus, running this program | |
253 | produces the output: | |
254 | @example | |
255 | Hello, world! | |
256 | @end example | |
257 | ||
258 | Here is a script which prints the factorial of its argument: | |
259 | @example | |
260 | #!/usr/local/bin/guile -s | |
261 | !# | |
262 | (define (fact n) | |
263 | (if (zero? n) 1 | |
264 | (* n (fact (- n 1))))) | |
265 | ||
266 | (display (fact (string->number (cadr (command-line))))) | |
267 | (newline) | |
268 | @end example | |
269 | In action: | |
270 | @example | |
271 | $ fact 5 | |
272 | 120 | |
273 | $ | |
274 | @end example | |
275 | ||
276 | However, suppose we want to use the definition of @code{fact} in this | |
277 | file from another script. We can't simply @code{load} the script file, | |
278 | and then use @code{fact}'s definition, because the script will try to | |
279 | compute and display a factorial when we load it. To avoid this problem, | |
280 | we might write the script this way: | |
281 | ||
282 | @example | |
283 | #!/usr/local/bin/guile \ | |
284 | -e main -s | |
285 | !# | |
286 | (define (fact n) | |
287 | (if (zero? n) 1 | |
288 | (* n (fact (- n 1))))) | |
289 | ||
290 | (define (main args) | |
291 | (display (fact (string->number (cadr args)))) | |
292 | (newline)) | |
293 | @end example | |
294 | This version packages the actions the script should perform in a | |
295 | function, @code{main}. This allows us to load the file purely for its | |
296 | definitions, without any extraneous computation taking place. Then we | |
297 | used the meta switch @code{\} and the entry point switch @code{-e} to | |
298 | tell Guile to call @code{main} after loading the script. | |
299 | @example | |
300 | $ fact 50 | |
301 | 30414093201713378043612608166064768844377641568960512000000000000 | |
302 | @end example | |
303 | ||
304 | Suppose that we now want to write a script which computes the | |
305 | @code{choose} function: given a set of @var{m} distinct objects, | |
306 | @code{(choose @var{n} @var{m})} is the number of distinct subsets | |
307 | containing @var{n} objects each. It's easy to write @code{choose} given | |
308 | @code{fact}, so we might write the script this way: | |
309 | @example | |
310 | #!/usr/local/bin/guile \ | |
311 | -l fact -e main -s | |
312 | !# | |
313 | (define (choose n m) | |
314 | (/ (fact m) (* (fact (- m n)) (fact n)))) | |
315 | ||
316 | (define (main args) | |
317 | (let ((n (string->number (cadr args))) | |
318 | (m (string->number (caddr args)))) | |
319 | (display (choose n m)) | |
320 | (newline))) | |
321 | @end example | |
322 | ||
323 | The command-line arguments here tell Guile to first load the file | |
324 | @file{fact}, and then run the script, with @code{main} as the entry | |
325 | point. In other words, the @code{choose} script can use definitions | |
326 | made in the @code{fact} script. Here are some sample runs: | |
327 | @example | |
328 | $ choose 0 4 | |
329 | 1 | |
330 | $ choose 1 4 | |
331 | 4 | |
332 | $ choose 2 4 | |
333 | 6 | |
334 | $ choose 3 4 | |
335 | 4 | |
336 | $ choose 4 4 | |
337 | 1 | |
338 | $ choose 50 100 | |
339 | 100891344545564193334812497256 | |
340 | @end example | |
341 | ||
342 | ||
343 | @node Linking Programs With Guile | |
344 | @section Linking Programs With Guile | |
345 | ||
346 | The Guile interpreter is available as an object library, to be linked | |
347 | into applications using Scheme as a configuration or extension | |
348 | language. This chapter covers the mechanics of linking your program | |
349 | with Guile on a typical POSIX system. | |
350 | ||
351 | Parts III and IV of this manual describe the C functions Guile provides. | |
352 | Furthermore, any Scheme function described in this manual as a | |
353 | ``Primitive'' is also callable from C; see @ref{Scheme Primitives}. | |
354 | ||
355 | The header file @code{<libguile.h>} provides declarations for all of | |
356 | Guile's functions and constants. You should @code{#include} it at the | |
357 | head of any C source file that uses identifiers described in this | |
358 | manual. | |
359 | ||
360 | Once you've compiled your source files, you can link them against Guile | |
361 | by passing the flag @code{-lguile} to your linker. If you installed | |
362 | Guile with multi-thread support (by passing @code{--enable-threads} to | |
363 | the @code{configure} script), you may also need to link against the | |
364 | QuickThreads library, @code{-lqt}. Guile refers to various mathematical | |
365 | functions, so you will probably need to link against the mathematical | |
366 | library, @code{-lm}, as well. | |
367 | ||
368 | @menu | |
369 | * Guile Initialization Functions:: What to call first. | |
370 | * A Sample Guile Main Program:: Sources and makefiles. | |
371 | @end menu | |
372 | ||
373 | ||
374 | @node Guile Initialization Functions | |
375 | @subsection Guile Initialization Functions | |
376 | ||
377 | To initialize Guile, use this function: | |
378 | ||
379 | @deftypefun void scm_boot_guile (int @var{argc}, char **@var{argv}, void (*@var{main_func}) (), void *@var{closure}) | |
380 | Initialize the Guile Scheme interpreter. Then call @var{main_func}, | |
381 | passing it @var{closure}, @var{argc}, and @var{argv}. @var{main_func} | |
382 | should do all the work of the program (initializing other packages, | |
383 | defining application-specific functions, reading user input, and so on) | |
384 | before returning. When @var{main_func} returns, call @code{exit (0)}; | |
385 | @code{scm_boot_guile} never returns. If you want some other exit value, | |
386 | have @var{main_func} call exit itself. | |
387 | ||
388 | @code{scm_boot_guile} arranges for the Scheme @code{command-line} | |
389 | function to return the strings given by @var{argc} and @var{argv}. If | |
390 | @var{main_func} modifies @var{argc} or @var{argv}, it should call | |
391 | @code{scm_set_program_arguments} with the final list, so Scheme code | |
392 | will know which arguments have been processed. | |
393 | ||
394 | @code{scm_boot_guile} establishes a catch-all error handler which prints | |
395 | an error message and exits the process. This means that Guile exits in | |
396 | a coherent way if a system error occurs and the user isn't prepared to | |
397 | handle it. If the user doesn't like this behavior, they can establish | |
398 | their own universal catcher in @var{main_func} to shadow this one. | |
399 | ||
400 | Why must the caller do all the real work from @var{main_func}? Guile's | |
401 | garbage collector assumes that all local variables which reference | |
402 | Scheme objects will be above @code{scm_boot_guile}'s stack frame on the | |
403 | stack. If you try to manipulate Scheme objects after this function | |
404 | returns, it's the luck of the draw whether Guile's storage manager will | |
405 | be able to find the objects you allocate. So, @code{scm_boot_guile} | |
406 | function exits, rather than returning, to discourage you from making | |
407 | that mistake. | |
408 | @end deftypefun | |
409 | ||
410 | One common way to use Guile is to write a set of C functions which | |
411 | perform some useful task, make them callable from Scheme, and then link | |
412 | the program with Guile. This yields a Scheme interpreter just like | |
413 | @code{guile}, but augmented with extra functions for some specific | |
414 | application --- a special-purpose scripting language. | |
415 | ||
416 | In this situation, the application should probably process its | |
417 | command-line arguments in the same manner as the stock Guile | |
418 | interpreter. To make that straightforward, Guile provides this | |
419 | function: | |
420 | ||
421 | @deftypefun void scm_shell (int @var{argc}, char **@var{argv}) | |
422 | Process command-line arguments in the manner of the @code{guile} | |
423 | executable. This includes loading the normal Guile initialization | |
424 | files, interacting with the user or running any scripts or expressions | |
425 | specified by @code{-s} or @code{-e} options, and then exiting. | |
426 | @xref{Invoking Guile}, for more details. | |
427 | ||
428 | Since this function does not return, you must do all | |
429 | application-specific initialization before calling this function. | |
430 | ||
431 | If you do not use this function to start Guile, you are responsible for | |
432 | making sure Guile's usual initialization files, @file{init.scm} and | |
433 | @file{ice-9/boot-9.scm}, get loaded. This will change soon. | |
434 | @end deftypefun | |
435 | ||
436 | ||
437 | @node A Sample Guile Main Program | |
438 | @subsection A Sample Guile Main Program | |
439 | ||
440 | Here is @file{simple-guile.c}, source code for a @code{main} and an | |
441 | @code{inner_main} function that will produce a complete Guile | |
442 | interpreter. | |
443 | ||
444 | @example | |
445 | /* simple-guile.c --- how to start up the Guile | |
446 | interpreter from C code. */ | |
447 | ||
448 | /* Get declarations for all the scm_ functions. */ | |
449 | #include <libguile.h> | |
450 | ||
451 | static void | |
452 | inner_main (void *closure, int argc, char **argv) | |
453 | @{ | |
454 | /* module initializations would go here */ | |
455 | scm_shell (argc, argv); | |
456 | @} | |
457 | ||
458 | int | |
459 | main (int argc, char **argv) | |
460 | @{ | |
461 | scm_boot_guile (argc, argv, inner_main, 0); | |
462 | return 0; /* never reached */ | |
463 | @} | |
464 | @end example | |
465 | ||
466 | The @code{main} function calls @code{scm_boot_guile} to initialize | |
467 | Guile, passing it @code{inner_main}. Once @code{scm_boot_guile} is | |
468 | ready, it invokes @code{inner_main}, which calls @code{scm_shell} to | |
469 | process the command-line arguments in the usual way. | |
470 | ||
471 | Here is a Makefile which you can use to compile the above program. | |
472 | @example | |
473 | # Use GCC, if you have it installed. | |
474 | CC=gcc | |
475 | ||
476 | # Tell the C compiler where to find <libguile.h> and -lguile. | |
477 | CFLAGS=-I/usr/local/include -L/usr/local/lib | |
478 | ||
479 | # Include -lqt and -lrx if they are present on your system. | |
480 | LIBS=-lguile -lqt -lrx -lm | |
481 | ||
482 | simple-guile: simple-guile.o | |
483 | $@{CC@} $@{CFLAGS@} simple-guile.o $@{LIBS@} -o simple-guile | |
484 | simple-guile.o: simple-guile.c | |
485 | $@{CC@} -c $@{CFLAGS@} simple-guile.c | |
486 | @end example | |
487 | ||
488 | If you are using the GNU Autoconf package to make your application more | |
489 | portable, Autoconf will settle many of the details in the Makefile above | |
490 | automatically, making it much simpler and more portable; we recommend | |
491 | using Autoconf with Guile. Here is a @file{configure.in} file for | |
492 | @code{simple-guile}, which Autoconf can use as a template to generate a | |
493 | @code{configure} script: | |
494 | @example | |
495 | AC_INIT(simple-guile.c) | |
496 | ||
497 | # Find a C compiler. | |
498 | AC_PROG_CC | |
499 | ||
500 | # Check for libraries. | |
501 | AC_CHECK_LIB(m, sin) | |
502 | AC_CHECK_LIB(rx, regcomp) | |
503 | AC_CHECK_LIB(qt, main) | |
504 | AC_CHECK_LIB(guile, scm_boot_guile) | |
505 | ||
506 | # Generate a Makefile, based on the results. | |
507 | AC_OUTPUT(Makefile) | |
508 | @end example | |
509 | ||
510 | Here is a @code{Makefile.in} template, from which the @code{configure} | |
511 | script produces a Makefile customized for the host system: | |
512 | @example | |
513 | # The configure script fills in these values. | |
514 | CC=@@CC@@ | |
515 | CFLAGS=@@CFLAGS@@ | |
516 | LIBS=@@LIBS@@ | |
517 | ||
518 | simple-guile: simple-guile.o | |
519 | $@{CC@} $@{CFLAGS@} simple-guile.o $@{LIBS@} -o simple-guile | |
520 | simple-guile.o: simple-guile.c | |
521 | $@{CC@} -c $@{CFLAGS@} simple-guile.c | |
522 | @end example | |
523 | ||
524 | The developer should use Autoconf to generate the @file{configure} | |
525 | script from the @file{configure.in} template, and distribute | |
526 | @file{configure} with the application. Here's how a user might go about | |
527 | building the application: | |
528 | ||
529 | @example | |
530 | $ ls | |
531 | Makefile.in configure* configure.in simple-guile.c | |
532 | $ ./configure | |
533 | creating cache ./config.cache | |
534 | checking for gcc... gcc | |
535 | checking whether the C compiler (gcc ) works... yes | |
536 | checking whether the C compiler (gcc ) is a cross-compiler... no | |
537 | checking whether we are using GNU C... yes | |
538 | checking whether gcc accepts -g... yes | |
539 | checking for sin in -lm... yes | |
540 | checking for regcomp in -lrx... yes | |
541 | checking for main in -lqt... yes | |
542 | checking for scm_boot_guile in -lguile... yes | |
543 | updating cache ./config.cache | |
544 | creating ./config.status | |
545 | creating Makefile | |
546 | $ make | |
547 | gcc -c -g -O2 simple-guile.c | |
548 | gcc -g -O2 simple-guile.o -lguile -lqt -lrx -lm -o simple-guile | |
549 | $ ./simple-guile | |
550 | guile> (+ 1 2 3) | |
551 | 6 | |
552 | guile> (getpwnam "jimb") | |
553 | #("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb" | |
554 | "/usr/local/bin/bash") | |
555 | guile> (exit) | |
556 | $ | |
557 | @end example | |
558 | ||
559 | ||
560 | @node Writing Guile Modules | |
561 | @section Writing Guile Modules | |
562 | ||
563 | [to be written] | |
564 | ||
565 | ||
566 | @page | |
567 | @node Reporting Bugs | |
568 | @chapter Reporting Bugs | |
569 | ||
570 | Any problems with the installation should be reported to | |
571 | @email{bug-guile@@gnu.org}. | |
572 | ||
573 | [[how about an explanation of what makes a good bug report?]] | |
574 | [[don't complain to us about problems with contributed modules?]] | |
575 | ||
576 | ||
577 | @c Local Variables: | |
578 | @c TeX-master: "guile.texi" | |
579 | @c End: |