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