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