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