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