Move doc files into guile-core distribution (1)
[bpt/guile.git] / doc / intro.texi
CommitLineData
38a93523
NJ
1@c $Id: intro.texi,v 1.1 2001-03-09 08:21:59 ossau Exp $
2
3@page
4@node What is Guile?
5@chapter What is Guile?
6
7Guile is an interpreter for the Scheme programming language, packaged
8for use in a wide variety of environments. Guile implements Scheme as
9described in the
10@tex
11Revised$^5$
12@end tex
13@ifinfo
14Revised^5
15@end ifinfo
16Report on the Algorithmic Language Scheme (usually known as R5RS),
17providing clean and general data and control structures. Guile goes
18beyond the rather austere language presented in R5RS, extending it with
19a module system, full access to POSIX system calls, networking support,
20multiple threads, dynamic linking, a foreign function call interface,
21powerful string processing, and many other features needed for
22programming in the real world.
23
24Like a shell, Guile can run interactively, reading expressions from the
25user, evaluating them, and displaying the results, or as a script
26interpreter, reading and executing Scheme code from a file. However,
27Guile is also packaged as an object library, allowing other applications
28to easily incorporate a complete Scheme interpreter. An application can
29use Guile as an extension language, a clean and powerful configuration
30language, or as multi-purpose ``glue'', connecting primitives provided
31by the application. It is easy to call Scheme code from C code and vice
32versa, giving the application designer full control of how and when to
33invoke the interpreter. Applications can add new functions, data types,
34control structures, and even syntax to Guile, creating a domain-specific
35language tailored to the task at hand, but based on a robust language
36design.
37
38Guile's module system allows one to break up a large program into
39manageable sections with well-defined interfaces between them. Modules
40may contain a mixture of interpreted and compiled code; Guile can use
41either static or dynamic linking to incorporate compiled code. Modules
42also encourage developers to package up useful collections of routines
43for general distribution; as of this writing, one can find Emacs
44interfaces, database access routines, compilers, GUI toolkit interfaces,
45and HTTP client functions, among others.
46
47In the future, we hope to expand Guile to support other languages like
48Tcl and Perl by translating them to Scheme code. This means that users
49can program applications which use Guile in the language of their
50choice, rather than having the tastes of the application's author
51imposed on them.
52
53
54@page
55@node Whirlwind Tour
56@chapter A Whirlwind Tour
57
58This chapter presents a quick tour of all the ways that Guile can be
59used.
60
61@menu
62* Running Guile Interactively::
63* Guile Scripts::
64* Linking Programs With Guile::
65* Writing Guile Modules::
66@end menu
67
68
69@node Running Guile Interactively
70@section Running Guile Interactively
71
72In its simplest form, Guile acts as an interactive interpreter for the
73Scheme programming language, reading and evaluating Scheme expressions
74the user enters from the terminal. Here is a sample interaction between
75Guile and a user; the user's input appears after the @code{$} and
76@code{guile>} prompts:
77
78@example
79$ guile
80guile> (+ 1 2 3) ; add some numbers
816
82guile> (define (factorial n) ; define a function
83 (if (zero? n) 1 (* n (factorial (- n 1)))))
84guile> (factorial 20)
852432902008176640000
86guile> (getpwnam "jimb") ; find my entry in /etc/passwd
87#("jimb" ".0krIpK2VqNbU" 4008 10 "Jim Blandy" "/u/jimb"
88 "/usr/local/bin/bash")
89guile> @kbd{C-d}
90$
91@end example
92
93@c [[When we get a fancier read-eval-print loop, with features for bouncing
94@c around among modules, referring to the value of the last expression,
95@c etc. then this section will get longer.]]
96
97
98@node Guile Scripts
99@section Guile Scripts
100
101Like AWK, Perl, or any shell, Guile can interpret script files. A Guile
102script is simply a file of Scheme code with some extra information at
103the beginning which tells the operating system how to invoke Guile, and
104then tells Guile how to handle the Scheme code.
105
106Before we present the details, here is a trivial Guile script:
107
108@example
109#!/usr/local/bin/guile -s
110!#
111(display "Hello, world!")
112(newline)
113@end example
114
115@menu
116* The Top of a Script File:: How to start a Guile script.
117* Scripting Examples:: Simple Guile scripts, explained.
118@end menu
119
120
121@node The Top of a Script File
122@subsection The Top of a Script File
123
124The first line of a Guile script must tell the operating system to use
125Guile to evaluate the script, and then tell Guile how to go about doing
126that. Here is the simplest case:
127
128@itemize @bullet
129
130@item
131The first two characters of the file must be @samp{#!}.
132
133The operating system interprets this to mean that the rest of the line
134is the name of an executable that can interpret the script. Guile,
135however, interprets these characters as the beginning of a multi-line
136comment, terminated by the characters @samp{!#} on a line by themselves.
137(This is an extension to the syntax described in R5RS, added to support
138shell scripts.)
139
140@item
141Immediately after those two characters must come the full pathname to
142the Guile interpreter. On most systems, this would be
143@samp{/usr/local/bin/guile}.
144
145@item
146Then must come a space, followed by a command-line argument to pass to
147Guile; this should be @samp{-s}. This switch tells Guile to run a
148script, instead of soliciting the user for input from the terminal.
149There are more elaborate things one can do here; see @ref{The Meta
150Switch}.
151
152@item
153Follow this with a newline.
154
155@item
156The second line of the script should contain only the characters
157@samp{!#} --- just like the top of the file, but reversed. The
158operating system never reads this far, but Guile treats this as the end
159of the comment begun on the first line by the @samp{#!} characters.
160
161@item
162The rest of the file should be a Scheme program.
163
164@end itemize
165
166Guile reads the program, evaluating expressions in the order that they
167appear. Upon reaching the end of the file, Guile exits.
168
169The function @code{command-line} returns the name of the script file and
170any command-line arguments passed by the user, as a list of strings.
171
172For example, consider the following script file:
173@example
174#!/usr/local/bin/guile -s
175!#
176(write (command-line))
177(newline)
178@end example
179
180If you put that text in a file called @file{foo} in the current
181directory, then you could make it executable and try it out like this:
182@example
183$ chmod a+x foo
184$ ./foo
185("./foo")
186$ ./foo bar baz
187("./foo" "bar" "baz")
188$
189@end example
190
191As another example, here is a simple replacement for the POSIX
192@code{echo} command:
193@example
194#!/usr/local/bin/guile -s
195!#
196(for-each (lambda (s) (display s) (display " "))
197 (cdr (command-line)))
198(newline)
199@end example
200
201@deffn procedure command-line
202@deffnx primitive program-arguments
203Return a list of the command-line arguments passed to the currently
204running program. If the program invoked Guile with the @samp{-s},
205@samp{-c} or @samp{--} switches, these procedures ignore everything up
206to and including those switches.
207@end deffn
208
209
210@node Scripting Examples
211@subsection Scripting Examples
212
213To start with, here are some examples of invoking Guile directly:
214
215@table @code
216
217@item guile -- a b c
218Run Guile interactively; @code{(command-line)} will return @*
219@code{("/usr/local/bin/guile" "a" "b" "c")}.
220
221@item guile -s /u/jimb/ex2 a b c
222Load the file @file{/u/jimb/ex2}; @code{(command-line)} will return @*
223@code{("/u/jimb/ex2" "a" "b" "c")}.
224
225@item guile -c '(write %load-path) (newline)'
226Write the value of the variable @code{%load-path}, print a newline,
227and exit.
228
229@item guile -e main -s /u/jimb/ex4 foo
230Load the file @file{/u/jimb/ex4}, and then call the function
231@code{main}, passing it the list @code{("/u/jimb/ex4" "foo")}.
232
233@item guile -l first -ds -l last -s script
234Load the files @file{first}, @file{script}, and @file{last}, in that
235order. The @code{-ds} switch says when to process the @code{-s}
236switch. For a more motivated example, see the scripts below.
237
238@end table
239
240
241Here is a very simple Guile script:
242@example
243#!/usr/local/bin/guile -s
244!#
245(display "Hello, world!")
246(newline)
247@end example
248The first line marks the file as a Guile script. When the user invokes
249it, the system runs @file{/usr/local/bin/guile} to interpret the script,
250passing @code{-s}, the script's filename, and any arguments given to the
251script as command-line arguments. When Guile sees @code{-s
252@var{script}}, it loads @var{script}. Thus, running this program
253produces the output:
254@example
255Hello, world!
256@end example
257
258Here is a script which prints the factorial of its argument:
259@example
260#!/usr/local/bin/guile -s
261!#
262(define (fact n)
263 (if (zero? n) 1
264 (* n (fact (- n 1)))))
265
266(display (fact (string->number (cadr (command-line)))))
267(newline)
268@end example
269In action:
270@example
271$ fact 5
272120
273$
274@end example
275
276However, suppose we want to use the definition of @code{fact} in this
277file from another script. We can't simply @code{load} the script file,
278and then use @code{fact}'s definition, because the script will try to
279compute and display a factorial when we load it. To avoid this problem,
280we might write the script this way:
281
282@example
283#!/usr/local/bin/guile \
284-e main -s
285!#
286(define (fact n)
287 (if (zero? n) 1
288 (* n (fact (- n 1)))))
289
290(define (main args)
291 (display (fact (string->number (cadr args))))
292 (newline))
293@end example
294This version packages the actions the script should perform in a
295function, @code{main}. This allows us to load the file purely for its
296definitions, without any extraneous computation taking place. Then we
297used the meta switch @code{\} and the entry point switch @code{-e} to
298tell Guile to call @code{main} after loading the script.
299@example
300$ fact 50
30130414093201713378043612608166064768844377641568960512000000000000
302@end example
303
304Suppose that we now want to write a script which computes the
305@code{choose} function: given a set of @var{m} distinct objects,
306@code{(choose @var{n} @var{m})} is the number of distinct subsets
307containing @var{n} objects each. It's easy to write @code{choose} given
308@code{fact}, so we might write the script this way:
309@example
310#!/usr/local/bin/guile \
311-l fact -e main -s
312!#
313(define (choose n m)
314 (/ (fact m) (* (fact (- m n)) (fact n))))
315
316(define (main args)
317 (let ((n (string->number (cadr args)))
318 (m (string->number (caddr args))))
319 (display (choose n m))
320 (newline)))
321@end example
322
323The command-line arguments here tell Guile to first load the file
324@file{fact}, and then run the script, with @code{main} as the entry
325point. In other words, the @code{choose} script can use definitions
326made in the @code{fact} script. Here are some sample runs:
327@example
328$ choose 0 4
3291
330$ choose 1 4
3314
332$ choose 2 4
3336
334$ choose 3 4
3354
336$ choose 4 4
3371
338$ choose 50 100
339100891344545564193334812497256
340@end example
341
342
343@node Linking Programs With Guile
344@section Linking Programs With Guile
345
346The Guile interpreter is available as an object library, to be linked
347into applications using Scheme as a configuration or extension
348language. This chapter covers the mechanics of linking your program
349with Guile on a typical POSIX system.
350
351Parts III and IV of this manual describe the C functions Guile provides.
352Furthermore, any Scheme function described in this manual as a
353``Primitive'' is also callable from C; see @ref{Scheme Primitives}.
354
355The header file @code{<libguile.h>} provides declarations for all of
356Guile's functions and constants. You should @code{#include} it at the
357head of any C source file that uses identifiers described in this
358manual.
359
360Once you've compiled your source files, you can link them against Guile
361by passing the flag @code{-lguile} to your linker. If you installed
362Guile with multi-thread support (by passing @code{--enable-threads} to
363the @code{configure} script), you may also need to link against the
364QuickThreads library, @code{-lqt}. Guile refers to various mathematical
365functions, so you will probably need to link against the mathematical
366library, @code{-lm}, as well.
367
368@menu
369* Guile Initialization Functions:: What to call first.
370* A Sample Guile Main Program:: Sources and makefiles.
371@end menu
372
373
374@node Guile Initialization Functions
375@subsection Guile Initialization Functions
376
377To initialize Guile, use this function:
378
379@deftypefun void scm_boot_guile (int @var{argc}, char **@var{argv}, void (*@var{main_func}) (), void *@var{closure})
380Initialize the Guile Scheme interpreter. Then call @var{main_func},
381passing it @var{closure}, @var{argc}, and @var{argv}. @var{main_func}
382should do all the work of the program (initializing other packages,
383defining application-specific functions, reading user input, and so on)
384before returning. When @var{main_func} returns, call @code{exit (0)};
385@code{scm_boot_guile} never returns. If you want some other exit value,
386have @var{main_func} call exit itself.
387
388@code{scm_boot_guile} arranges for the Scheme @code{command-line}
389function to return the strings given by @var{argc} and @var{argv}. If
390@var{main_func} modifies @var{argc} or @var{argv}, it should call
391@code{scm_set_program_arguments} with the final list, so Scheme code
392will know which arguments have been processed.
393
394@code{scm_boot_guile} establishes a catch-all error handler which prints
395an error message and exits the process. This means that Guile exits in
396a coherent way if a system error occurs and the user isn't prepared to
397handle it. If the user doesn't like this behavior, they can establish
398their own universal catcher in @var{main_func} to shadow this one.
399
400Why must the caller do all the real work from @var{main_func}? Guile's
401garbage collector assumes that all local variables which reference
402Scheme objects will be above @code{scm_boot_guile}'s stack frame on the
403stack. If you try to manipulate Scheme objects after this function
404returns, it's the luck of the draw whether Guile's storage manager will
405be able to find the objects you allocate. So, @code{scm_boot_guile}
406function exits, rather than returning, to discourage you from making
407that mistake.
408@end deftypefun
409
410One common way to use Guile is to write a set of C functions which
411perform some useful task, make them callable from Scheme, and then link
412the program with Guile. This yields a Scheme interpreter just like
413@code{guile}, but augmented with extra functions for some specific
414application --- a special-purpose scripting language.
415
416In this situation, the application should probably process its
417command-line arguments in the same manner as the stock Guile
418interpreter. To make that straightforward, Guile provides this
419function:
420
421@deftypefun void scm_shell (int @var{argc}, char **@var{argv})
422Process command-line arguments in the manner of the @code{guile}
423executable. This includes loading the normal Guile initialization
424files, interacting with the user or running any scripts or expressions
425specified by @code{-s} or @code{-e} options, and then exiting.
426@xref{Invoking Guile}, for more details.
427
428Since this function does not return, you must do all
429application-specific initialization before calling this function.
430
431If you do not use this function to start Guile, you are responsible for
432making sure Guile's usual initialization files, @file{init.scm} and
433@file{ice-9/boot-9.scm}, get loaded. This will change soon.
434@end deftypefun
435
436
437@node A Sample Guile Main Program
438@subsection A Sample Guile Main Program
439
440Here is @file{simple-guile.c}, source code for a @code{main} and an
441@code{inner_main} function that will produce a complete Guile
442interpreter.
443
444@example
445/* simple-guile.c --- how to start up the Guile
446 interpreter from C code. */
447
448/* Get declarations for all the scm_ functions. */
449#include <libguile.h>
450
451static void
452inner_main (void *closure, int argc, char **argv)
453@{
454 /* module initializations would go here */
455 scm_shell (argc, argv);
456@}
457
458int
459main (int argc, char **argv)
460@{
461 scm_boot_guile (argc, argv, inner_main, 0);
462 return 0; /* never reached */
463@}
464@end example
465
466The @code{main} function calls @code{scm_boot_guile} to initialize
467Guile, passing it @code{inner_main}. Once @code{scm_boot_guile} is
468ready, it invokes @code{inner_main}, which calls @code{scm_shell} to
469process the command-line arguments in the usual way.
470
471Here is a Makefile which you can use to compile the above program.
472@example
473# Use GCC, if you have it installed.
474CC=gcc
475
476# Tell the C compiler where to find <libguile.h> and -lguile.
477CFLAGS=-I/usr/local/include -L/usr/local/lib
478
479# Include -lqt and -lrx if they are present on your system.
480LIBS=-lguile -lqt -lrx -lm
481
482simple-guile: simple-guile.o
483 $@{CC@} $@{CFLAGS@} simple-guile.o $@{LIBS@} -o simple-guile
484simple-guile.o: simple-guile.c
485 $@{CC@} -c $@{CFLAGS@} simple-guile.c
486@end example
487
488If you are using the GNU Autoconf package to make your application more
489portable, Autoconf will settle many of the details in the Makefile above
490automatically, making it much simpler and more portable; we recommend
491using Autoconf with Guile. Here is a @file{configure.in} file for
492@code{simple-guile}, which Autoconf can use as a template to generate a
493@code{configure} script:
494@example
495AC_INIT(simple-guile.c)
496
497# Find a C compiler.
498AC_PROG_CC
499
500# Check for libraries.
501AC_CHECK_LIB(m, sin)
502AC_CHECK_LIB(rx, regcomp)
503AC_CHECK_LIB(qt, main)
504AC_CHECK_LIB(guile, scm_boot_guile)
505
506# Generate a Makefile, based on the results.
507AC_OUTPUT(Makefile)
508@end example
509
510Here is a @code{Makefile.in} template, from which the @code{configure}
511script produces a Makefile customized for the host system:
512@example
513# The configure script fills in these values.
514CC=@@CC@@
515CFLAGS=@@CFLAGS@@
516LIBS=@@LIBS@@
517
518simple-guile: simple-guile.o
519 $@{CC@} $@{CFLAGS@} simple-guile.o $@{LIBS@} -o simple-guile
520simple-guile.o: simple-guile.c
521 $@{CC@} -c $@{CFLAGS@} simple-guile.c
522@end example
523
524The developer should use Autoconf to generate the @file{configure}
525script from the @file{configure.in} template, and distribute
526@file{configure} with the application. Here's how a user might go about
527building the application:
528
529@example
530$ ls
531Makefile.in configure* configure.in simple-guile.c
532$ ./configure
533creating cache ./config.cache
534checking for gcc... gcc
535checking whether the C compiler (gcc ) works... yes
536checking whether the C compiler (gcc ) is a cross-compiler... no
537checking whether we are using GNU C... yes
538checking whether gcc accepts -g... yes
539checking for sin in -lm... yes
540checking for regcomp in -lrx... yes
541checking for main in -lqt... yes
542checking for scm_boot_guile in -lguile... yes
543updating cache ./config.cache
544creating ./config.status
545creating Makefile
546$ make
547gcc -c -g -O2 simple-guile.c
548gcc -g -O2 simple-guile.o -lguile -lqt -lrx -lm -o simple-guile
549$ ./simple-guile
550guile> (+ 1 2 3)
5516
552guile> (getpwnam "jimb")
553#("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb"
554 "/usr/local/bin/bash")
555guile> (exit)
556$
557@end example
558
559
560@node Writing Guile Modules
561@section Writing Guile Modules
562
563[to be written]
564
565
566@page
567@node Reporting Bugs
568@chapter Reporting Bugs
569
570Any problems with the installation should be reported to
571@email{bug-guile@@gnu.org}.
572
573[[how about an explanation of what makes a good bug report?]]
574[[don't complain to us about problems with contributed modules?]]
575
576
577@c Local Variables:
578@c TeX-master: "guile.texi"
579@c End: