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