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