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