Big reorganization of the whole manual to give it a simpler structure.
[bpt/guile.git] / doc / ref / intro.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @node Introduction to Guile
8 @chapter Introduction to Guile
9
10 @menu
11 * What is Guile?::
12 * Obtaining and Installing Guile::
13 * Whirlwind Tour::
14 * Reporting Bugs::
15 @end menu
16
17 @node What is Guile?
18 @section What is Guile?
19
20 Guile is an interpreter for the Scheme programming language, packaged
21 for use in a wide variety of environments. Guile implements Scheme as
22 described in the
23 @tex
24 Revised$^5$
25 @end tex
26 @ifnottex
27 Revised^5
28 @end ifnottex
29 Report on the Algorithmic Language Scheme (usually known as @acronym{R5RS}),
30 providing clean and general data and control structures. Guile goes
31 beyond the rather austere language presented in @acronym{R5RS}, extending it with
32 a module system, full access to @acronym{POSIX} system calls, networking support,
33 multiple threads, dynamic linking, a foreign function call interface,
34 powerful string processing, and many other features needed for
35 programming in the real world.
36
37 Like a shell, Guile can run interactively, reading expressions from the
38 user, evaluating them, and displaying the results, or as a script
39 interpreter, reading and executing Scheme code from a file. However,
40 Guile is also packaged as an object library, allowing other applications
41 to easily incorporate a complete Scheme interpreter. An application can
42 then use Guile as an extension language, a clean and powerful configuration
43 language, or as multi-purpose ``glue'', connecting primitives provided
44 by the application. It is easy to call Scheme code from C code and vice
45 versa, giving the application designer full control of how and when to
46 invoke the interpreter. Applications can add new functions, data types,
47 control structures, and even syntax to Guile, creating a domain-specific
48 language tailored to the task at hand, but based on a robust language
49 design.
50
51 Guile's module system allows one to break up a large program into
52 manageable sections with well-defined interfaces between them.
53 Modules may contain a mixture of interpreted and compiled code; Guile
54 can use either static or dynamic linking to incorporate compiled code.
55 Modules also encourage developers to package up useful collections of
56 routines for general distribution; as of this writing, one can find
57 Emacs interfaces, database access routines, compilers, @acronym{GUI}
58 toolkit interfaces, and @acronym{HTTP} client functions, among others.
59
60 In the future, we hope to expand Guile to support other languages like
61 Tcl and Perl by translating them to Scheme code. This means that users
62 can program applications which use Guile in the language of their
63 choice, rather than having the tastes of the application's author
64 imposed on them.
65
66 @node Obtaining and Installing Guile
67 @section Obtaining and Installing Guile
68
69 Guile can be obtained from the main GNU archive site
70 @url{ftp://ftp.gnu.org} or any of its mirrors. The file will be named
71 guile-version.tar.gz. The current version is @value{VERSION}, so the
72 file you should grab is:
73
74 @url{ftp://ftp.gnu.org/pub/gnu/guile-@value{VERSION}.tar.gz}
75
76 To unbundle Guile use the instruction
77
78 @example
79 zcat guile-@value{VERSION}.tar.gz | tar xvf -
80 @end example
81
82 which will create a directory called @file{guile-@value{VERSION}} with
83 all the sources. You can look at the file @file{INSTALL} for detailed
84 instructions on how to build and install Guile, but you should be able
85 to just do
86
87 @example
88 cd guile-@value{VERSION}
89 ./configure
90 make
91 make install
92 @end example
93
94 This will install the Guile executable @file{guile}, the Guile library
95 @file{-lguile} and various associated header files and support
96 libraries. It will also install the Guile tutorial and reference
97 manual.
98
99 @c [[include instructions for getting R5RS]]
100
101 Since this manual frequently refers to the Scheme ``standard'', also
102 known as R5RS, or the
103 @iftex
104 ``Revised$^5$ Report on the Algorithmic Language Scheme'',
105 @end iftex
106 @ifnottex
107 ``Revised^5 Report on the Algorithmic Language Scheme'',
108 @end ifnottex
109 we have included the report in the Guile distribution;
110 @xref{Top, , Introduction, r5rs, Revised(5) Report on the Algorithmic
111 Language Scheme}.
112 This will also be installed in your info directory.
113
114 @node Whirlwind Tour
115 @section A Whirlwind Tour
116
117 This chapter presents a quick tour of all the ways that Guile can be
118 used. There are additional examples in the @file{examples/}
119 directory in the Guile source distribution.
120
121 The following examples assume that Guile has been installed in
122 @code{/usr/local/}.
123
124 @menu
125 * Running Guile Interactively::
126 * Running Guile Scripts::
127 * Linking Guile into Programs::
128 * Writing Guile Extensions::
129 * Using the Guile Module System::
130 @end menu
131
132
133 @node Running Guile Interactively
134 @subsection Running Guile Interactively
135
136 In its simplest form, Guile acts as an interactive interpreter for the
137 Scheme programming language, reading and evaluating Scheme expressions
138 the user enters from the terminal. Here is a sample interaction between
139 Guile and a user; the user's input appears after the @code{$} and
140 @code{guile>} prompts:
141
142 @example
143 $ guile
144 guile> (+ 1 2 3) ; add some numbers
145 6
146 guile> (define (factorial n) ; define a function
147 (if (zero? n) 1 (* n (factorial (- n 1)))))
148 guile> (factorial 20)
149 2432902008176640000
150 guile> (getpwnam "jimb") ; find my entry in /etc/passwd
151 #("jimb" ".0krIpK2VqNbU" 4008 10 "Jim Blandy" "/u/jimb"
152 "/usr/local/bin/bash")
153 guile> @kbd{C-d}
154 $
155 @end example
156
157
158 @node Running Guile Scripts
159 @subsection Running Guile Scripts
160
161 Like AWK, Perl, or any shell, Guile can interpret script files. A Guile
162 script is simply a file of Scheme code with some extra information at
163 the beginning which tells the operating system how to invoke Guile, and
164 then tells Guile how to handle the Scheme code.
165
166 Here is a trivial Guile script, for more details @xref{Guile Scripting}.
167
168 @example
169 #!/usr/local/bin/guile -s
170 !#
171 (display "Hello, world!")
172 (newline)
173 @end example
174
175
176 @node Linking Guile into Programs
177 @subsection Linking Guile into Programs
178
179 The Guile interpreter is available as an object library, to be linked
180 into applications using Scheme as a configuration or extension
181 language.
182
183 Here is @file{simple-guile.c}, source code for a program that will
184 produce a complete Guile interpreter. In addition to all usual
185 functions provided by Guile, it will also offer the function
186 @code{my-hostname}.
187
188 @example
189 #include <stdlib.h>
190 #include <libguile.h>
191
192 static SCM
193 my_hostname (void)
194 @{
195 return scm_str2string (getenv ("HOSTNAME"));
196 @}
197
198 static void
199 inner_main (void *data, int argc, char **argv)
200 @{
201 scm_c_define_gsubr ("my-hostname", 0, 0, 0, my_hostname);
202 scm_shell (argc, argv);
203 @}
204
205 int
206 main (int argc, char **argv)
207 @{
208 scm_boot_guile (argc, argv, inner_main, 0);
209 return 0; /* never reached */
210 @}
211 @end example
212
213 When Guile is correctly installed on your system, the above program
214 can be compiled and linked like this:
215
216 @example
217 $ gcc -o simple-guile simple-guile.c -lguile
218 @end example
219
220 When it is run, it behaves just like the @code{guile} program except
221 that you can also call the new @code{my-hostname} function.
222
223 @example
224 $ ./simple-guile
225 guile> (+ 1 2 3)
226 6
227 guile> (my-hostname)
228 "burns"
229 @end example
230
231 @node Writing Guile Extensions
232 @subsection Writing Guile Extensions
233
234 You can link Guile into your program and make Scheme available to the
235 users of your program. You can also link your library into Guile and
236 make its functionality available to all users of Guile.
237
238 A library that is linked into Guile is called an @dfn{extensions}, but
239 it really just is an ordinary object library.
240
241 The following example shows how to write a simple extension for Guile
242 that makes the @code{j0} function available to Scheme code.
243
244 @smallexample
245 #include <math.h>
246 #include <libguile.h>
247
248 SCM
249 j0_wrapper (SCM x)
250 @{
251 return scm_make_real (j0 (scm_num2dbl (x, "j0")));
252 @}
253
254 void
255 init_bessel ()
256 @{
257 scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
258 @}
259 @end smallexample
260
261 This C source file needs to be compiled into a shared library. Here is
262 how to do it on GNU/Linux:
263
264 @smallexample
265 gcc -shared -o libguile-bessel.so -fPIC bessel.c
266 @end smallexample
267
268 For creating shared libraries portably, we recommend the use of
269 @code{GNU Libtool}.
270
271 A shared library can be loaded into a running Guile process with the
272 function @code{load-extension}. The @code{j0} is then immediately
273 available:
274
275 @smallexample
276 $ guile
277 guile> (load-extension "./libguile-bessel" "init_bessel")
278 guile> (j0 2)
279 0.223890779141236
280 @end smallexample
281
282
283 @node Using the Guile Module System
284 @subsection Using the Guile Module System
285
286 Guile has support for dividing a program into @dfn{modules}. By using
287 modules, you can group related code together and manage the
288 composition of complete programs from largely independent parts.
289
290 (Although the module system implementation is in flux, feel free to use it
291 anyway. Guile will provide reasonable backwards compatibility.)
292
293 Details on the module system beyond this introductory material can be found in
294 @xref{Modules}.
295
296 @menu
297 * Using Modules::
298 * Writing new Modules::
299 * Putting Extensions into Modules::
300 @end menu
301
302
303 @node Using Modules
304 @subsubsection Using Modules
305
306 Guile comes with a lot of useful modules, for example for string
307 processing or command line parsing. Additionally, there exist many
308 Guile modules written by other Guile hackers, but which have to be
309 installed manually.
310
311 Here is a sample interactive session that shows how to use the
312 @code{(ice-9 popen)} module which provides the means for communicating
313 with other processes over pipes together with the @code{(ice-9
314 rdelim)} module that provides the function @code{read-line}.
315
316 @smallexample
317 $ guile
318 guile> (use-modules (ice-9 popen))
319 guile> (use-modules (ice-9 rdelim))
320 guile> (define p (open-input-pipe "ls -l"))
321 guile> (read-line p)
322 "total 30"
323 guile> (read-line p)
324 "drwxr-sr-x 2 mgrabmue mgrabmue 1024 Mar 29 19:57 CVS"
325 @end smallexample
326
327 @node Writing new Modules
328 @subsubsection Writing new Modules
329
330 You can create new modules using the syntactic form
331 @code{define-module}. All definitions following this form until the
332 next @code{define-module} are placed into the new module.
333
334 One module is usually placed into one file, and that file is installed
335 in a location where Guile can automatically find it. The following
336 session shows a simple example.
337
338 @smallexample
339 $ cat /usr/local/share/guile/foo/bar.scm
340
341 (define-module (foo bar))
342 (export frob)
343
344 (define (frob x) (* 2 x))
345
346 $ guile
347 guile> (use-modules (foo bar))
348 guile> (frob 12)
349 24
350 @end smallexample
351
352 @node Putting Extensions into Modules
353 @subsubsection Putting Extensions into Modules
354
355 In addition to Scheme code you can also put things that are defined in
356 C into a module.
357
358 You do this by writing a small Scheme file that defines the module and
359 call @code{load-extension} directly in the body of the module.
360
361 @smallexample
362 $ cat /usr/local/share/guile/math/bessel.scm
363
364 (define-module (math bessel))
365 (export j0)
366
367 (load-extension "libguile-bessel" "init_bessel")
368
369 $ file /usr/local/lib/libguile-bessel.so
370 @dots{} ELF 32-bit LSB shared object @dots{}
371 $ guile
372 guile> (use-modules (math bessel))
373 guile> (j0 2)
374 0.223890779141236
375 @end smallexample
376
377 There is also a way to manipulate the module system from C but only
378 Scheme files can be autoloaded. Thus, we recommend that you define
379 your modules in Scheme.
380
381 @node Reporting Bugs
382 @section Reporting Bugs
383
384 Any problems with the installation should be reported to
385 @email{bug-guile@@gnu.org}.
386
387 Whenever you have found a bug in Guile you are encouraged to report it
388 to the Guile developers, so they can fix it. They may also be able to
389 suggest workarounds when it is not possible for you to apply the bug-fix
390 or install a new version of Guile yourself.
391
392 Before sending in bug reports, please check with the following list that
393 you really have found a bug.
394
395 @itemize @bullet
396 @item
397 Whenever documentation and actual behavior differ, you have certainly
398 found a bug, either in the documentation or in the program.
399
400 @item
401 When Guile crashes, it is a bug.
402
403 @item
404 When Guile hangs or takes forever to complete a task, it is a bug.
405
406 @item
407 When calculations produce wrong results, it is a bug.
408
409 @item
410 When Guile signals an error for valid Scheme programs, it is a bug.
411
412 @item
413 When Guile does not signal an error for invalid Scheme programs, it may
414 be a bug, unless this is explicitly documented.
415
416 @item
417 When some part of the documentation is not clear and does not make sense
418 to you even after re-reading the section, it is a bug.
419 @end itemize
420
421 When you write a bug report, please make sure to include as much of the
422 information described below in the report. If you can't figure out some
423 of the items, it is not a problem, but the more information we get, the
424 more likely we can diagnose and fix the bug.
425
426 @itemize @bullet
427 @item
428 The version number of Guile. Without this, we won't know whether there
429 is any point in looking for the bug in the current version of Guile.
430
431 You can get the version number by invoking the command
432
433 @example
434 $ guile --version
435 Guile 1.4.1
436 Copyright (c) 1995, 1996, 1997, 2000 Free Software Foundation
437 Guile may be distributed under the terms of the GNU General Public License;
438 certain other uses are permitted as well. For details, see the file
439 `COPYING', which is included in the Guile distribution.
440 There is no warranty, to the extent permitted by law.
441 @end example
442
443 @item
444 The type of machine you are using, and the operating system name and
445 version number. On GNU systems, you can get it with @file{uname}.
446
447 @example
448 $ uname -a
449 Linux tortoise 2.2.17 #1 Thu Dec 21 17:29:05 CET 2000 i586 unknown
450 @end example
451
452 @item
453 The operands given to the @file{configure} command when Guile was
454 installed. It's often useful to augment this with the output of the
455 command @code{guile-config info}.
456
457 @item
458 A complete list of any modifications you have made to the Guile source.
459 (We may not have time to investigate the bug unless it happens in an
460 unmodified Guile. But if you've made modifications and you don't tell
461 us, you are sending us on a wild goose chase.)
462
463 Be precise about these changes. A description in English is not
464 enough---send a context diff for them.
465
466 Adding files of your own, or porting to another machine, is a
467 modification of the source.
468
469 @item
470 Details of any other deviations from the standard procedure for
471 installing Guile.
472
473 @item
474 The complete text of any source files needed to reproduce the bug.
475
476 If you can tell us a way to cause the problem without loading any source
477 files, please do so. This makes it much easier to debug. If you do
478 need files, make sure you arrange for us to see their exact contents.
479
480 @item
481 The precise Guile invocation command line we need to type to reproduce
482 the bug.
483
484 @item
485 A description of what behavior you observe that you believe is
486 incorrect. For example, "The Guile process gets a fatal signal," or,
487 "The resulting output is as follows, which I think is wrong."
488
489 Of course, if the bug is that Guile gets a fatal signal, then one can't
490 miss it. But if the bug is incorrect results, the maintainer might fail
491 to notice what is wrong. Why leave it to chance?
492
493 If the manifestation of the bug is a Guile error message, it is
494 important to report the precise text of the error message, and a
495 backtrace showing how the Scheme program arrived at the error.
496
497 This can be done using the procedure @code{backtrace} in the REPL.
498
499 @item
500 Check whether any programs you have loaded into Guile, including your
501 @file{.guile} file, set any variables that may affect the functioning of
502 Guile. Also, see whether the problem happens in a freshly started Guile
503 without loading your @file{.guile} file (start Guile with the @code{-q}
504 switch to prevent loading the init file). If the problem does
505 @emph{not} occur then, you must report the precise contents of any
506 programs that you must load into Guile in order to cause the problem to
507 occur.
508
509 @item
510 If the problem does depend on an init file or other Scheme programs that
511 are not part of the standard Guile distribution, then you should make
512 sure it is not a bug in those programs by complaining to their
513 maintainers first. After they verify that they are using Guile in a way
514 that is supposed to work, they should report the bug.
515
516 @item
517 If you wish to mention something in the Guile source, show the line of
518 code with a few lines of context. Don't just give a line number.
519
520 The line numbers in the development sources might not match those in your
521 sources. It would take extra work for the maintainers to determine what
522 code is in your version at a given line number, and we could not be
523 certain.
524
525 @item
526 Additional information from a C debugger such as GDB might enable
527 someone to find a problem on a machine which he does not have available.
528 If you don't know how to use GDB, please read the GDB manual---it is not
529 very long, and using GDB is easy. You can find the GDB distribution,
530 including the GDB manual in online form, in most of the same places you
531 can find the Guile distribution. To run Guile under GDB, you should
532 switch to the @file{libguile} subdirectory in which Guile was compiled, then
533 do @code{gdb guile} or @code{gdb .libs/guile} (if using GNU Libtool).
534
535 However, you need to think when you collect the additional information
536 if you want it to show what causes the bug.
537
538 For example, many people send just a backtrace, but that is not very
539 useful by itself. A simple backtrace with arguments often conveys
540 little about what is happening inside Guile, because most of the
541 arguments listed in the backtrace are pointers to Scheme objects. The
542 numeric values of these pointers have no significance whatever; all that
543 matters is the contents of the objects they point to (and most of the
544 contents are themselves pointers).
545 @end itemize
546
547
548
549 @c Local Variables:
550 @c TeX-master: "guile.texi"
551 @c End: