* Fix doc typo.
[bpt/guile.git] / doc / ref / intro.texi
1 @c $Id: intro.texi,v 1.7 2002-01-07 20:48:02 ossau Exp $
2
3
4 @page
5 @node What is Guile?
6 @chapter What is Guile?
7
8 Guile is an interpreter for the Scheme programming language, packaged
9 for use in a wide variety of environments. Guile implements Scheme as
10 described in the
11 @tex
12 Revised$^5$
13 @end tex
14 @ifinfo
15 Revised^5
16 @end ifinfo
17 Report on the Algorithmic Language Scheme (usually known as R5RS),
18 providing clean and general data and control structures. Guile goes
19 beyond the rather austere language presented in R5RS, extending it with
20 a module system, full access to POSIX system calls, networking support,
21 multiple threads, dynamic linking, a foreign function call interface,
22 powerful string processing, and many other features needed for
23 programming in the real world.
24
25 Like a shell, Guile can run interactively, reading expressions from the
26 user, evaluating them, and displaying the results, or as a script
27 interpreter, reading and executing Scheme code from a file. However,
28 Guile is also packaged as an object library, allowing other applications
29 to easily incorporate a complete Scheme interpreter. An application can
30 use Guile as an extension language, a clean and powerful configuration
31 language, or as multi-purpose ``glue'', connecting primitives provided
32 by the application. It is easy to call Scheme code from C code and vice
33 versa, giving the application designer full control of how and when to
34 invoke the interpreter. Applications can add new functions, data types,
35 control structures, and even syntax to Guile, creating a domain-specific
36 language tailored to the task at hand, but based on a robust language
37 design.
38
39 Guile's module system allows one to break up a large program into
40 manageable sections with well-defined interfaces between them. Modules
41 may contain a mixture of interpreted and compiled code; Guile can use
42 either static or dynamic linking to incorporate compiled code. Modules
43 also encourage developers to package up useful collections of routines
44 for general distribution; as of this writing, one can find Emacs
45 interfaces, database access routines, compilers, GUI toolkit interfaces,
46 and HTTP client functions, among others.
47
48 In the future, we hope to expand Guile to support other languages like
49 Tcl and Perl by translating them to Scheme code. This means that users
50 can program applications which use Guile in the language of their
51 choice, rather than having the tastes of the application's author
52 imposed on them.
53
54
55 @page
56 @node Whirlwind Tour
57 @chapter A Whirlwind Tour
58
59 This chapter presents a quick tour of all the ways that Guile can be
60 used.
61
62 @menu
63 * Running Guile Interactively::
64 * Guile Scripts::
65 * Linking Programs With Guile::
66 * Writing Extensions for Guile::
67 * Guile Modules::
68 @end menu
69
70
71 @node Running Guile Interactively
72 @section Running Guile Interactively
73
74 In its simplest form, Guile acts as an interactive interpreter for the
75 Scheme programming language, reading and evaluating Scheme expressions
76 the user enters from the terminal. Here is a sample interaction between
77 Guile and a user; the user's input appears after the @code{$} and
78 @code{guile>} prompts:
79
80 @example
81 $ guile
82 guile> (+ 1 2 3) ; add some numbers
83 6
84 guile> (define (factorial n) ; define a function
85 (if (zero? n) 1 (* n (factorial (- n 1)))))
86 guile> (factorial 20)
87 2432902008176640000
88 guile> (getpwnam "jimb") ; find my entry in /etc/passwd
89 #("jimb" ".0krIpK2VqNbU" 4008 10 "Jim Blandy" "/u/jimb"
90 "/usr/local/bin/bash")
91 guile> @kbd{C-d}
92 $
93 @end example
94
95 @c [[When we get a fancier read-eval-print loop, with features for bouncing
96 @c around among modules, referring to the value of the last expression,
97 @c etc. then this section will get longer.]]
98
99
100 @node Guile Scripts
101 @section Guile Scripts
102
103 Like AWK, Perl, or any shell, Guile can interpret script files. A Guile
104 script is simply a file of Scheme code with some extra information at
105 the beginning which tells the operating system how to invoke Guile, and
106 then tells Guile how to handle the Scheme code.
107
108 Before we present the details, here is a trivial Guile script:
109
110 @example
111 #!/usr/local/bin/guile -s
112 !#
113 (display "Hello, world!")
114 (newline)
115 @end example
116
117 @menu
118 * The Top of a Script File:: How to start a Guile script.
119 * Scripting Examples:: Simple Guile scripts, explained.
120 @end menu
121
122
123 @node The Top of a Script File
124 @subsection The Top of a Script File
125
126 The first line of a Guile script must tell the operating system to use
127 Guile to evaluate the script, and then tell Guile how to go about doing
128 that. Here is the simplest case:
129
130 @itemize @bullet
131
132 @item
133 The first two characters of the file must be @samp{#!}.
134
135 The operating system interprets this to mean that the rest of the line
136 is the name of an executable that can interpret the script. Guile,
137 however, interprets these characters as the beginning of a multi-line
138 comment, terminated by the characters @samp{!#} on a line by themselves.
139 (This is an extension to the syntax described in R5RS, added to support
140 shell scripts.)
141
142 @item
143 Immediately after those two characters must come the full pathname to
144 the Guile interpreter. On most systems, this would be
145 @samp{/usr/local/bin/guile}.
146
147 @item
148 Then must come a space, followed by a command-line argument to pass to
149 Guile; this should be @samp{-s}. This switch tells Guile to run a
150 script, instead of soliciting the user for input from the terminal.
151 There are more elaborate things one can do here; see @ref{The Meta
152 Switch}.
153
154 @item
155 Follow this with a newline.
156
157 @item
158 The second line of the script should contain only the characters
159 @samp{!#} --- just like the top of the file, but reversed. The
160 operating system never reads this far, but Guile treats this as the end
161 of the comment begun on the first line by the @samp{#!} characters.
162
163 @item
164 The rest of the file should be a Scheme program.
165
166 @end itemize
167
168 Guile reads the program, evaluating expressions in the order that they
169 appear. Upon reaching the end of the file, Guile exits.
170
171 The function @code{command-line} returns the name of the script file and
172 any command-line arguments passed by the user, as a list of strings.
173
174 For example, consider the following script file:
175 @example
176 #!/usr/local/bin/guile -s
177 !#
178 (write (command-line))
179 (newline)
180 @end example
181
182 If you put that text in a file called @file{foo} in the current
183 directory, then you could make it executable and try it out like this:
184 @example
185 $ chmod a+x foo
186 $ ./foo
187 ("./foo")
188 $ ./foo bar baz
189 ("./foo" "bar" "baz")
190 $
191 @end example
192
193 As another example, here is a simple replacement for the POSIX
194 @code{echo} command:
195 @example
196 #!/usr/local/bin/guile -s
197 !#
198 (for-each (lambda (s) (display s) (display " "))
199 (cdr (command-line)))
200 (newline)
201 @end example
202
203 @deffn {Scheme Procedure} command-line
204 @deffnx {Scheme Procedure} program-arguments
205 Return a list of the command-line arguments passed to the currently
206 running program. If the program invoked Guile with the @samp{-s},
207 @samp{-c} or @samp{--} switches, these procedures ignore everything up
208 to and including those switches.
209 @end deffn
210
211
212 @node Scripting Examples
213 @subsection Scripting Examples
214
215 To start with, here are some examples of invoking Guile directly:
216
217 @table @code
218
219 @item guile -- a b c
220 Run Guile interactively; @code{(command-line)} will return @*
221 @code{("/usr/local/bin/guile" "a" "b" "c")}.
222
223 @item guile -s /u/jimb/ex2 a b c
224 Load the file @file{/u/jimb/ex2}; @code{(command-line)} will return @*
225 @code{("/u/jimb/ex2" "a" "b" "c")}.
226
227 @item guile -c '(write %load-path) (newline)'
228 Write the value of the variable @code{%load-path}, print a newline,
229 and exit.
230
231 @item guile -e main -s /u/jimb/ex4 foo
232 Load the file @file{/u/jimb/ex4}, and then call the function
233 @code{main}, passing it the list @code{("/u/jimb/ex4" "foo")}.
234
235 @item guile -l first -ds -l last -s script
236 Load the files @file{first}, @file{script}, and @file{last}, in that
237 order. The @code{-ds} switch says when to process the @code{-s}
238 switch. For a more motivated example, see the scripts below.
239
240 @end table
241
242
243 Here is a very simple Guile script:
244 @example
245 #!/usr/local/bin/guile -s
246 !#
247 (display "Hello, world!")
248 (newline)
249 @end example
250 The first line marks the file as a Guile script. When the user invokes
251 it, the system runs @file{/usr/local/bin/guile} to interpret the script,
252 passing @code{-s}, the script's filename, and any arguments given to the
253 script as command-line arguments. When Guile sees @code{-s
254 @var{script}}, it loads @var{script}. Thus, running this program
255 produces the output:
256 @example
257 Hello, world!
258 @end example
259
260 Here is a script which prints the factorial of its argument:
261 @example
262 #!/usr/local/bin/guile -s
263 !#
264 (define (fact n)
265 (if (zero? n) 1
266 (* n (fact (- n 1)))))
267
268 (display (fact (string->number (cadr (command-line)))))
269 (newline)
270 @end example
271 In action:
272 @example
273 $ fact 5
274 120
275 $
276 @end example
277
278 However, suppose we want to use the definition of @code{fact} in this
279 file from another script. We can't simply @code{load} the script file,
280 and then use @code{fact}'s definition, because the script will try to
281 compute and display a factorial when we load it. To avoid this problem,
282 we might write the script this way:
283
284 @example
285 #!/usr/local/bin/guile \
286 -e main -s
287 !#
288 (define (fact n)
289 (if (zero? n) 1
290 (* n (fact (- n 1)))))
291
292 (define (main args)
293 (display (fact (string->number (cadr args))))
294 (newline))
295 @end example
296 This version packages the actions the script should perform in a
297 function, @code{main}. This allows us to load the file purely for its
298 definitions, without any extraneous computation taking place. Then we
299 used the meta switch @code{\} and the entry point switch @code{-e} to
300 tell Guile to call @code{main} after loading the script.
301 @example
302 $ fact 50
303 30414093201713378043612608166064768844377641568960512000000000000
304 @end example
305
306 Suppose that we now want to write a script which computes the
307 @code{choose} function: given a set of @var{m} distinct objects,
308 @code{(choose @var{n} @var{m})} is the number of distinct subsets
309 containing @var{n} objects each. It's easy to write @code{choose} given
310 @code{fact}, so we might write the script this way:
311 @example
312 #!/usr/local/bin/guile \
313 -l fact -e main -s
314 !#
315 (define (choose n m)
316 (/ (fact m) (* (fact (- m n)) (fact n))))
317
318 (define (main args)
319 (let ((n (string->number (cadr args)))
320 (m (string->number (caddr args))))
321 (display (choose n m))
322 (newline)))
323 @end example
324
325 The command-line arguments here tell Guile to first load the file
326 @file{fact}, and then run the script, with @code{main} as the entry
327 point. In other words, the @code{choose} script can use definitions
328 made in the @code{fact} script. Here are some sample runs:
329 @example
330 $ choose 0 4
331 1
332 $ choose 1 4
333 4
334 $ choose 2 4
335 6
336 $ choose 3 4
337 4
338 $ choose 4 4
339 1
340 $ choose 50 100
341 100891344545564193334812497256
342 @end example
343
344
345 @node Linking Programs With Guile
346 @section Linking Programs With Guile
347
348 The Guile interpreter is available as an object library, to be linked
349 into applications using Scheme as a configuration or extension
350 language. This chapter covers the mechanics of linking your program
351 with Guile on a typical POSIX system.
352
353 Parts III and IV of this manual describe the C functions Guile provides.
354 Furthermore, any Scheme function described in this manual as a
355 ``Primitive'' is also callable from C; see @ref{Primitives}.
356
357 The header file @code{<libguile.h>} provides declarations for all of
358 Guile's functions and constants. You should @code{#include} it at the
359 head of any C source file that uses identifiers described in this
360 manual. Once you've compiled your source files, you need to link them
361 against the Guile object code library, @code{libguile}.
362
363 On most systems, you should not need to tell the compiler and linker
364 explicitly where they can find @file{libguile.h} and @file{libguile}.
365 When Guile has been installed in a peculiar way, or when you are on a
366 peculiar system, things might not be so easy and you might need to pass
367 additional @code{-I} or @code{-L} options to the compiler. Guile
368 provides the utility program @code{guile-config} to help you find the
369 right values for these options. You would typically run
370 @code{guile-config} during the configuration phase of your program and
371 use the obtained information in the Makefile.
372
373 @menu
374 * Guile Initialization Functions:: What to call first.
375 * A Sample Guile Main Program:: Sources and makefiles.
376 @end menu
377
378
379 @node Guile Initialization Functions
380 @subsection Guile Initialization Functions
381
382 To initialize Guile, you can use one of two functions. The first,
383 @code{scm_boot_guile}, is the most portable way to initialize Guile. It
384 should be used whenever you have control over the main function of your
385 program because it never returns. The second function,
386 @code{scm_init_guile}, does return and can thus be used in more
387 situations. However, @code{scm_init_guile} is not as widely available
388 as @code{scm_boot_guile} because it needs to rely on non-portable code
389 to find the stack bounds. When Guile does not know how to find these
390 bounds on your system, it will not provide @code{scm_init_guile}.
391
392 When you can tolerate the limits of @code{scm_boot_guile}, you should
393 use it in favor of @code{scm_init_guile} since that will make your
394 program more portable.
395
396 @deftypefun void scm_boot_guile (int @var{argc}, char **@var{argv}, void (*@var{main_func}) (), void *@var{closure})
397 Initialize the Guile Scheme interpreter. Then call @var{main_func},
398 passing it @var{closure}, @var{argc}, and @var{argv}. @var{main_func}
399 should do all the work of the program (initializing other packages,
400 defining application-specific functions, reading user input, and so on)
401 before returning. When @var{main_func} returns, call @code{exit (0)};
402 @code{scm_boot_guile} never returns. If you want some other exit value,
403 have @var{main_func} call exit itself.
404
405 @code{scm_boot_guile} arranges for the Scheme @code{command-line}
406 function to return the strings given by @var{argc} and @var{argv}. If
407 @var{main_func} modifies @var{argc} or @var{argv}, it should call
408 @code{scm_set_program_arguments} with the final list, so Scheme code
409 will know which arguments have been processed.
410
411 Why must the caller do all the real work from @var{main_func}? Guile's
412 garbage collector scans the stack to find all local variables that
413 reference Scheme objects. To do this, it needs to know the bounds of
414 the stack that might contain such references. Because there is no
415 portable way in C to find the base of the stack, @code{scm_boot_guile}
416 assumes that all references are above its own stack frame. If you try
417 to manipulate Scheme objects after this function returns, it's the luck
418 of the draw whether Guile's storage manager will be able to find the
419 objects you allocate. So, @code{scm_boot_guile} function exits, rather
420 than returning, to discourage you from making that mistake.
421
422 See @code{scm_init_guile}, below, for a function that can find the real
423 base of the stack, but not in a portable way.
424 @end deftypefun
425
426 @deftypefun void scm_init_guile ()
427 Initialize the Guile Scheme interpreter.
428
429 In contrast to @code{scm_boot_guile}, this function knows how to find
430 the true base of the stack and thus does not need to usurp the control
431 flow of your program. However, since finding the stack base can not be
432 done portably, this function might not be available in all installations
433 of Guile. If you can, you should use @code{scm_boot_guile} instead.
434
435 Note that @code{scm_init_guile} does not inform Guile about the command
436 line arguments that should be returned by the Scheme function
437 @code{command-line}. You can use @code{scm_set_program_arguments} to do
438 this.
439 @end deftypefun
440
441 One common way to use Guile is to write a set of C functions which
442 perform some useful task, make them callable from Scheme, and then link
443 the program with Guile. This yields a Scheme interpreter just like
444 @code{guile}, but augmented with extra functions for some specific
445 application --- a special-purpose scripting language.
446
447 In this situation, the application should probably process its
448 command-line arguments in the same manner as the stock Guile
449 interpreter. To make that straightforward, Guile provides this
450 function:
451
452 @deftypefun void scm_shell (int @var{argc}, char **@var{argv})
453 Process command-line arguments in the manner of the @code{guile}
454 executable. This includes loading the normal Guile initialization
455 files, interacting with the user or running any scripts or expressions
456 specified by @code{-s} or @code{-e} options, and then exiting.
457 @xref{Invoking Guile}, for more details.
458
459 Since this function does not return, you must do all
460 application-specific initialization before calling this function.
461 @end deftypefun
462
463
464 @node A Sample Guile Main Program
465 @subsection A Sample Guile Main Program
466
467 Here is @file{simple-guile.c}, source code for a @code{main} and an
468 @code{inner_main} function that will produce a complete Guile
469 interpreter.
470
471 @example
472 /* simple-guile.c --- how to start up the Guile
473 interpreter from C code. */
474
475 /* Get declarations for all the scm_ functions. */
476 #include <libguile.h>
477
478 static void
479 inner_main (void *closure, int argc, char **argv)
480 @{
481 /* module initializations would go here */
482 scm_shell (argc, argv);
483 @}
484
485 int
486 main (int argc, char **argv)
487 @{
488 scm_boot_guile (argc, argv, inner_main, 0);
489 return 0; /* never reached */
490 @}
491 @end example
492
493 The @code{main} function calls @code{scm_boot_guile} to initialize
494 Guile, passing it @code{inner_main}. Once @code{scm_boot_guile} is
495 ready, it invokes @code{inner_main}, which calls @code{scm_shell} to
496 process the command-line arguments in the usual way.
497
498 Here is a Makefile which you can use to compile the above program. It
499 uses @code{guile-config} to learn about the necessary compiler and
500 linker flags.
501 @example
502 # Use GCC, if you have it installed.
503 CC=gcc
504
505 # Tell the C compiler where to find <libguile.h>
506 CFLAGS=`guile-config compile`
507
508 # Tell the linker what libraries to use and where to find them.
509 LIBS=`guile-config link`
510
511 simple-guile: simple-guile.o
512 $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile
513
514 simple-guile.o: simple-guile.c
515 $@{CC@} -c $@{CFLAGS@} simple-guile.c
516 @end example
517
518 If you are using the GNU Autoconf package to make your application more
519 portable, Autoconf will settle many of the details in the Makefile above
520 automatically, making it much simpler and more portable; we recommend
521 using Autoconf with Guile. Guile also provides the @code{GUILE_FLAGS}
522 macro for autoconf that performs all necessary checks. Here is a
523 @file{configure.in} file for @code{simple-guile} that uses this macro.
524 Autoconf can use as this file as template to generate a @code{configure}
525 script. In order for Autoconf to find the @code{GUILE_FLAGS} macro, you
526 will need to run @code{aclocal} first. This is not really Guile
527 specific, so you should refer to the Autoconf documentation REFFIXME
528 when in doubt.
529 @example
530 AC_INIT(simple-guile.c)
531
532 # Find a C compiler.
533 AC_PROG_CC
534
535 # Check for Guile
536 GUILE_FLAGS
537
538 # Generate a Makefile, based on the results.
539 AC_OUTPUT(Makefile)
540 @end example
541
542 Here is a @code{Makefile.in} template, from which the @code{configure}
543 script produces a Makefile customized for the host system:
544 @example
545 # The configure script fills in these values.
546 CC=@@CC@@
547 CFLAGS=@@GUILE_CFLAGS@@
548 LIBS=@@GUILE_LDFLAGS@@
549
550 simple-guile: simple-guile.o
551 $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile
552 simple-guile.o: simple-guile.c
553 $@{CC@} -c $@{CFLAGS@} simple-guile.c
554 @end example
555
556 The developer should use Autoconf to generate the @file{configure}
557 script from the @file{configure.in} template, and distribute
558 @file{configure} with the application. Here's how a user might go about
559 building the application:
560
561 @example
562 $ ls
563 Makefile.in configure* configure.in simple-guile.c
564 $ ./configure
565 creating cache ./config.cache
566 checking for gcc... (cached) gcc
567 checking whether the C compiler (gcc ) works... yes
568 checking whether the C compiler (gcc ) is a cross-compiler... no
569 checking whether we are using GNU C... (cached) yes
570 checking whether gcc accepts -g... (cached) yes
571 checking for Guile... yes
572 creating ./config.status
573 creating Makefile
574 $ make
575 gcc -c -I/usr/local/include simple-guile.c
576 gcc simple-guile.o -L/usr/local/lib -lguile -lqthreads -lpthread -lm -o simple-guile
577 $ ./simple-guile
578 guile> (+ 1 2 3)
579 6
580 guile> (getpwnam "jimb")
581 #("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb"
582 "/usr/local/bin/bash")
583 guile> (exit)
584 $
585 @end example
586
587
588 @node Writing Extensions for Guile
589 @section Writing Extensions for Guile
590
591 The previous sections have briefly explained how to write programs that
592 make use of an embedded Guile interpreter. But sometimes, all you want
593 to do is make new primitive procedures and data types available to the
594 Scheme programmer. Writing a new version of @code{guile} is
595 inconvenient in this case and it would in fact make the life of the
596 users of your new features needlessly hard.
597
598 @c [[ the following is probably a bit long-winded ]]
599
600 For example, suppose that there is a program @code{guile-db} that is a
601 version of Guile with additional features for accessing a database.
602 People who want to write Scheme programs that use these features would
603 have to use @code{guile-db} instead of the usual @code{guile} program.
604 Now suppose that there is also a program @code{guile-gtk} that extends
605 Guile with access to the popular Gtk+ toolkit for graphical user
606 interfaces. People who want to write GUIs in Scheme would have to use
607 @code{guile-gtk}. Now, what happens when you want to write a Scheme
608 application that uses a GUI to let the user access a database? You
609 would have to write a @emph{third} program that incorporates both the
610 database stuff and the GUI stuff. This might not be easy (because
611 @code{guile-gtk} might be a quite obscure program, say) and taking this
612 example further makes it easy to see that this approach can not work in
613 practice.
614
615 It would have been much better if both the database features and the GUI
616 feature had been provided as libraries that can just be linked with
617 @code{guile}. Guile makes it easy to do just this, and we encourage you
618 to make your extensions to Guile available as libraries whenever
619 possible.
620
621 You write the new primitive procedures and data types in the normal
622 fashion, and link them into a shared library instead of into a
623 stand-alone program. The shared library can then be loaded dynamically
624 by Guile.
625
626 @menu
627 * A Sample Guile Extension::
628 @end menu
629
630
631 @node A Sample Guile Extension
632 @subsection A Sample Guile Extension
633
634 This section explains how to make the Bessel functions of the C library
635 available to Scheme. First we need to write the appropriate glue code
636 to convert the arguments and return values of the functions from Scheme
637 to C and back. Additionally, we need a function that will add them to
638 the set of Guile primitives. Because this is just an example, we will
639 only implement this for the @code{j0} function.
640
641 Consider the following file @file{bessel.c}.
642
643 @smallexample
644 #include <math.h>
645 #include <libguile.h>
646
647 SCM
648 j0_wrapper (SCM x)
649 @{
650 return scm_make_real (j0 (scm_num2dbl (x, "j0")));
651 @}
652
653 void
654 init_bessel ()
655 @{
656 scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
657 @}
658 @end smallexample
659
660 This C source file needs to be compiled into a shared library. Here is
661 how to do it on GNU/Linux:
662
663 @smallexample
664 gcc -shared -o libguile-bessel.so -fPIC bessel.c
665 @end smallexample
666
667 For creating shared libraries portably, we recommend the use of
668 @code{GNU Libtool}.
669
670 A shared library can be loaded into a running Guile process with the
671 function @code{load-extension}. In addition to the name of the
672 library to load, this function also expects the name of function from
673 that library that will be called to initialize it. For our example,
674 we are going to call the function @code{init_bessel} which will make
675 @code{j0_wrapper} available to Scheme programs with the name
676 @code{j0}. Note that we do not specify a filename extension such as
677 @file{.so} when invoking @code{load-extension}. The right extension for
678 the host platform will be provided automatically.
679
680 @smalllisp
681 (load-extension "libguile-bessel" "init_bessel")
682 (j0 2)
683 @result{} 0.223890779141236
684 @end smalllisp
685
686 For this to work, @code{load-extension} must be able to find
687 @file{libguile-bessel}, of course. It will look in the places that
688 are usual for your operating system, and it will additionally look
689 into the directories listed in the @code{LTDL_LIBRRAY_PATH}
690 environment variable.
691
692 To see how these Guile extensions via shared libraries relate to the
693 module system, see below @xref{Intro to Modules and Extensions}.
694
695
696 @node Guile Modules
697 @section Guile Modules
698
699 Guile has support for dividing a program into @dfn{modules}. By using
700 modules, you can group related code together and manage the
701 composition of complete programs from largely independent parts.
702
703 (Although the module system implementation is in flux, feel free to use it
704 anyway. Guile will provide reasonable backwards compatibility.)
705
706 Details on the module system beyond this introductory material can be found in
707 @xref{Modules}.
708
709 @menu
710 * Intro to Using Guile Modules::
711 * Intro to Writing New Modules::
712 * Intro to Modules and Extensions::
713 @end menu
714
715
716 @node Intro to Using Guile Modules
717 @subsection Intro to Using Existing Modules
718
719 Guile comes with a lot of useful modules, for example for string
720 processing or command line parsing. Additionally, there exist many
721 Guile modules written by other Guile hackers, but which have to be
722 installed manually.
723
724 Existing modules have to be placed in places where Guile looks for them
725 by default or in colon-separated directories in the environment variable
726 @code{GUILE_LOAD_PATH}. When this variable is set, those directories
727 are searched first, then the the default. The following command
728 shows the complete list of directories searched:
729
730 @smallexample
731 guile -c '(write %load-path) (newline)'
732 @end smallexample
733
734 Suppose you want to use the procedures and variables exported by the
735 module @code{(ice-9 popen)}, which provides the means for communicating
736 with other processes over pipes. Add the following line to your
737 currently running Guile REPL or the top of your script file.
738
739 @lisp
740 (use-modules (ice-9 popen))
741 @end lisp
742
743 This will load the module and make the procedures exported by
744 @code{(ice-9 popen)} automatically available. The next step could be to
745 open a pipe to @file{ls} and read the contents of the current directory,
746 one line at a time.
747
748 @lisp
749 (define p (open-input-pipe "ls -l"))
750 (read-line p)
751 @result{}
752 "total 30"
753 (read-line p)
754 @result{}
755 "drwxr-sr-x 2 mgrabmue mgrabmue 1024 Mar 29 19:57 CVS"
756 @end lisp
757
758
759 @node Intro to Writing New Modules
760 @subsection Intro to Writing New Modules
761
762 Of course it is possible to write modules yourself. Using modules for
763 structuring your programs makes them more readable and lets you
764 distribute them more easily. Also, explicitly defining the procedures
765 and variables which are exported from a module adds documentation to the
766 source and specifies the interface a module provides.
767
768 In Guile, you can create new modules and switch to existing modules in
769 order to add bindings to them using the syntactic form
770 @code{define-module}.
771
772 @lisp
773 (define-module (foo bar))
774
775 (define (frob x) x)
776 @end lisp
777
778 Will create the module @code{(foo bar)}.@footnote{It is only convention
779 that the module names in this section have two elements. One or more
780 than two elements are perfectly fine, such as @code{(foo)} or @code{(foo
781 bar braz)}} All definitions following this statement will add bindings
782 to the module @code{(foo bar)}, and these bindings will not be visible
783 outside of the module. To make the bindings accessible to other
784 modules, you have to export them explicitly using one of the following
785 means:
786
787 @itemize @bullet
788 @item
789 Export them with the @code{export} form:
790 @lisp
791 (export frob)
792 @end lisp
793
794 @item
795 Include them into the @code{define-module} form with the keyword
796 @code{export}:
797 @lisp
798 (define-module (foo bar)
799 #:export (frob))
800 @end lisp
801
802 @item
803 Change the definition of @code{frob} to use @code{define-public}, which
804 is a combination of @code{define} and @code{export}.
805 @lisp
806 (define-public (frob x) x)
807 @end lisp
808 @end itemize
809
810 After exporting, other modules can access the exported items simply by
811 using @code{use-modules} to load the module @code{(foo bar)}.
812
813
814 @node Intro to Modules and Extensions
815 @subsection Intro to Modules and Extensions
816
817 In addition to Scheme code you can also put things that are defined in
818 C into a module.
819
820 You do this by writing a small Scheme file that defines the module.
821 That Scheme file in turn invokes @code{load-extension} to make the
822 features defined in C available. This works since all definitions
823 made by @code{scm_c_define_gsubr} etc. go into the @emph{current
824 module} and @code{define-module} causes the newly defined module to be
825 current while the code that follows it is executed.
826
827 Suppose we want to put the Bessel function @code{j0} from the example
828 extension into a module called @code{(math bessel)}. We would have to
829 write a Scheme file with this contents
830
831 @smallexample
832 (define-module (math bessel))
833
834 (export j0)
835
836 (load-extension "libguile-bessel" "init_bessel")
837 @end smallexample
838
839 This file should of course be saved in the right place for
840 autoloading, for example as
841 @file{/usr/local/share/guile/math/bessel.scm}.
842
843 When @code{init_bessel} is called, the new @code{(math bessel)} module
844 is the current one. Thus, the call to @code{scm_c_define_gsubr} will
845 put the new definition for @code{j0} into it, just as we want it.
846
847 The definitions made in the C code are not automatically exported from
848 a module. You need to explicitly list the ones you want to export in
849 @code{export} statements or with the @code{:export} option of
850 @code{define-module}.
851
852 There is also a way to manipulate the module system from C but only
853 Scheme files can be autoloaded. Thus, we recommend that you define
854 your modules in Scheme.
855
856
857 @page
858 @node Obtaining and Installing Guile
859 @chapter Obtaining and Installing Guile
860
861 Here is the information you will need to get and install Guile and extra
862 packages and documentation you might need or find interesting.
863
864 @menu
865 * The Basic Guile Package::
866 * Packages not shipped with Guile::
867 @end menu
868
869
870 @node The Basic Guile Package
871 @section The Basic Guile Package
872
873 Guile can be obtained from the main GNU archive site
874 @url{ftp://prep.ai.mit.edu/pub/gnu} or any of its mirrors. The file
875 will be named guile-version.tar.gz. The current version is
876 @value{VERSION}, so the file you should grab is:
877
878 @url{ftp://prep.ai.mit.edu/pub/gnu/guile-@value{VERSION}.tar.gz}
879
880 To unbundle Guile use the instruction
881 @example
882 zcat guile-@value{VERSION}.tar.gz | tar xvf -
883 @end example
884 which will create a directory called @file{guile-@value{VERSION}} with
885 all the sources. You can look at the file @file{INSTALL} for detailed
886 instructions on how to build and install Guile, but you should be able
887 to just do
888 @example
889 cd guile-@value{VERSION}
890 ./configure
891 make install
892 @end example
893
894 This will install the Guile executable @file{guile}, the Guile library
895 @file{libguile.a} and various associated header files and support
896 libraries. It will also install the Guile tutorial and reference manual.
897
898 @c [[include instructions for getting R5RS]]
899
900 Since this manual frequently refers to the Scheme ``standard'', also
901 known as R5RS, or the
902 @iftex
903 ``Revised$^5$ Report on the Algorithmic Language Scheme'',
904 @end iftex
905 @ifinfo
906 ``Revised^5 Report on the Algorithmic Language Scheme'',
907 @end ifinfo
908 we have included the report in the Guile distribution;
909 @xref{Top, , Introduction, r5rs, Revised(5) Report on the Algorithmic
910 Language Scheme}.
911 This will also be installed in your info directory.
912
913
914 @node Packages not shipped with Guile
915 @section Packages not shipped with Guile
916
917 We ship the Guile tutorial and reference manual with the Guile
918 distribution. Since the Scheme standard (R5RS) is a stable document, we
919 ship that too.
920
921 Here are references (usually World Wide Web URLs) to some other freely
922 redistributable documents and packages which you might find useful if
923 you are using Guile.
924
925 @table @strong
926 @item SCSH
927 the Scheme Shell. Gary Houston has ported SCSH to Guile. The relevant
928 chapter (@pxref{The Scheme shell (scsh)}) has references to the SCSH web
929 page with all its documentation.
930
931 @item SLIB
932 a portable Scheme library maintained by Aubrey Jaffer. SLIB can be
933 obtained by ftp from @url{ftp://prep.ai.mit.edu/pub/gnu/jacal/}.
934
935 The SLIB package should be unpacked somewhere in Guile's load path. It
936 will typically be unpacked in @file{/usr/local/share/guile/site}, so
937 that it will be @file{/usr/local/share/guile/site/slib}.
938
939 Guile might have been installed with a different prefix, in which case
940 the load path can be checked from inside the interpreter with:
941
942 @smalllisp
943 guile> %load-path
944 ("/usr/local/share/guile/site" "/usr/local/share/guile/1.3a" "/usr/local/share/guile" ".")
945 @end smalllisp
946
947 The relevant chapter (@pxref{SLIB}) has details on how to use SLIB with
948 Guile.
949
950 @item JACAL
951 a symbolic math package by Aubrey Jaffer. The latest version of Jacal
952 can be obtained from @url{ftp://prep.ai.mit.edu/pub/gnu/jacal/}, and
953 should be unpacked in @file{/usr/local/share/guile/site/slib} so that
954 it will be in @file{/usr/local/share/guile/site/slib/jacal}.
955
956 The relevant section (@pxref{JACAL}) has details on how to use Jacal.
957 @end table
958
959
960 @page
961 @node Reporting Bugs
962 @chapter Reporting Bugs
963
964 Any problems with the installation should be reported to
965 @email{bug-guile@@gnu.org}.
966
967 Whenever you have found a bug in Guile you are encouraged to report it
968 to the Guile developers, so they can fix it. They may also be able to
969 suggest workarounds when it is not possible for you to apply the bug-fix
970 or install a new version of Guile yourself.
971
972 Before sending in bug reports, please check with the following list that
973 you really have found a bug.
974
975 @itemize @bullet
976 @item
977 Whenever documentation and actual behavior differ, you have certainly
978 found a bug, either in the documentation or in the program.
979
980 @item
981 When Guile crashes, it is a bug.
982
983 @item
984 When Guile hangs or takes forever to complete a task, it is a bug.
985
986 @item
987 When calculations produce wrong results, it is a bug.
988
989 @item
990 When Guile signals an error for valid Scheme programs, it is a bug.
991
992 @item
993 When Guile does not signal an error for invalid Scheme programs, it may
994 be a bug, unless this is explicitly documented.
995
996 @item
997 When some part of the documentation is not clear and does not make sense
998 to you even after re-reading the section, it is a bug.
999 @end itemize
1000
1001 When you write a bug report, please make sure to include as much of the
1002 information described below in the report. If you can't figure out some
1003 of the items, it is not a problem, but the more information we get, the
1004 more likely we can diagnose and fix the bug.
1005
1006 @itemize @bullet
1007 @item
1008 The version number of Guile. Without this, we won't know whether there
1009 is any point in looking for the bug in the current version of Guile.
1010
1011 You can get the version number by invoking the command
1012
1013 @example
1014 $ guile --version
1015 Guile 1.4.1
1016 Copyright (c) 1995, 1996, 1997, 2000 Free Software Foundation
1017 Guile may be distributed under the terms of the GNU General Public License;
1018 certain other uses are permitted as well. For details, see the file
1019 `COPYING', which is included in the Guile distribution.
1020 There is no warranty, to the extent permitted by law.
1021 @end example
1022
1023 @item
1024 The type of machine you are using, and the operating system name and
1025 version number. On GNU systems, you can get it with @file{uname}.
1026
1027 @example
1028 $ uname -a
1029 Linux tortoise 2.2.17 #1 Thu Dec 21 17:29:05 CET 2000 i586 unknown
1030 @end example
1031
1032 @item
1033 The operands given to the @file{configure} command when Guile was
1034 installed. It's often useful to augment this with the output of the
1035 command @code{guile-config info}.
1036
1037 @item
1038 A complete list of any modifications you have made to the Guile source.
1039 (We may not have time to investigate the bug unless it happens in an
1040 unmodified Guile. But if you've made modifications and you don't tell
1041 us, you are sending us on a wild goose chase.)
1042
1043 Be precise about these changes. A description in English is not
1044 enough---send a context diff for them.
1045
1046 Adding files of your own, or porting to another machine, is a
1047 modification of the source.
1048
1049 @item
1050 Details of any other deviations from the standard procedure for
1051 installing Guile.
1052
1053 @item
1054 The complete text of any source files needed to reproduce the bug.
1055
1056 If you can tell us a way to cause the problem without loading any source
1057 files, please do so. This makes it much easier to debug. If you do
1058 need files, make sure you arrange for us to see their exact contents.
1059
1060 @item
1061 The precise Guile invocation command line we need to type to reproduce
1062 the bug.
1063
1064 @item
1065 A description of what behavior you observe that you believe is
1066 incorrect. For example, "The Guile process gets a fatal signal," or,
1067 "The resulting output is as follows, which I think is wrong."
1068
1069 Of course, if the bug is that Guile gets a fatal signal, then one can't
1070 miss it. But if the bug is incorrect results, the maintainer might fail
1071 to notice what is wrong. Why leave it to chance?
1072
1073 If the manifestation of the bug is a Guile error message, it is
1074 important to report the precise text of the error message, and a
1075 backtrace showing how the Scheme program arrived at the error.
1076
1077 This can be done using the procedure @code{backtrace} in the REPL.
1078
1079 @item
1080 Check whether any programs you have loaded into Guile, including your
1081 @file{.guile} file, set any variables that may affect the functioning of
1082 Guile. Also, see whether the problem happens in a freshly started Guile
1083 without loading your @file{.guile} file (start Guile with the @code{-q}
1084 switch to prevent loading the init file). If the problem does
1085 @emph{not} occur then, you must report the precise contents of any
1086 programs that you must load into Guile in order to cause the problem to
1087 occur.
1088
1089 @item
1090 If the problem does depend on an init file or other Scheme programs that
1091 are not part of the standard Guile distribution, then you should make
1092 sure it is not a bug in those programs by complaining to their
1093 maintainers first. After they verify that they are using Guile in a way
1094 that is supposed to work, they should report the bug.
1095
1096 @item
1097 If you wish to mention something in the Guile source, show the line of
1098 code with a few lines of context. Don't just give a line number.
1099
1100 The line numbers in the development sources might not match those in your
1101 sources. It would take extra work for the maintainers to determine what
1102 code is in your version at a given line number, and we could not be
1103 certain.
1104
1105 @item
1106 Additional information from a C debugger such as GDB might enable
1107 someone to find a problem on a machine which he does not have available.
1108 If you don't know how to use GDB, please read the GDB manual---it is not
1109 very long, and using GDB is easy. You can find the GDB distribution,
1110 including the GDB manual in online form, in most of the same places you
1111 can find the Guile distribution. To run Guile under GDB, you should
1112 switch to the @file{libguile} subdirectory in which Guile was compiled, then
1113 do @code{gdb guile} or @code{gdb .libs/guile} (if using GNU Libtool).
1114
1115 However, you need to think when you collect the additional information
1116 if you want it to show what causes the bug.
1117
1118 For example, many people send just a backtrace, but that is not very
1119 useful by itself. A simple backtrace with arguments often conveys
1120 little about what is happening inside Guile, because most of the
1121 arguments listed in the backtrace are pointers to Scheme objects. The
1122 numeric values of these pointers have no significance whatever; all that
1123 matters is the contents of the objects they point to (and most of the
1124 contents are themselves pointers).
1125 @end itemize
1126
1127
1128
1129 @c Local Variables:
1130 @c TeX-master: "guile.texi"
1131 @c End: