Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / doc / ref / scheme-using.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Guile Reference Manual.
3 @c Copyright (C) 2006, 2010, 2011
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @node Using Guile Interactively
8 @section Using Guile Interactively
9
10 When you start up Guile by typing just @code{guile}, without a
11 @code{-c} argument or the name of a script to execute, you get an
12 interactive interpreter where you can enter Scheme expressions, and
13 Guile will evaluate them and print the results for you. Here are some
14 simple examples.
15
16 @lisp
17 scheme@@(guile-user)> (+ 3 4 5)
18 $1 = 12
19 scheme@@(guile-user)> (display "Hello world!\n")
20 Hello world!
21 scheme@@(guile-user)> (values 'a 'b)
22 $2 = a
23 $3 = b
24 @end lisp
25
26 @noindent
27 This mode of use is called a @dfn{REPL}, which is short for
28 ``Read-Eval-Print Loop'', because the Guile interpreter first reads the
29 expression that you have typed, then evaluates it, and then prints the
30 result.
31
32 The prompt shows you what language and module you are in. In this case, the
33 current language is @code{scheme}, and the current module is
34 @code{(guile-user)}. @xref{Other Languages}, for more information on Guile's
35 support for languages other than Scheme.
36
37 @menu
38 * Init File::
39 * Readline::
40 * Value History::
41 * REPL Commands::
42 * Error Handling::
43 * Interactive Debugging::
44 @end menu
45
46
47 @node Init File
48 @subsection The Init File, @file{~/.guile}
49
50 @cindex .guile
51 When run interactively, Guile will load a local initialization file from
52 @file{~/.guile}. This file should contain Scheme expressions for
53 evaluation.
54
55 This facility lets the user customize their interactive Guile
56 environment, pulling in extra modules or parameterizing the REPL
57 implementation.
58
59 To run Guile without loading the init file, use the @code{-q}
60 command-line option.
61
62
63 @node Readline
64 @subsection Readline
65
66 To make it easier for you to repeat and vary previously entered
67 expressions, or to edit the expression that you're typing in, Guile
68 can use the GNU Readline library. This is not enabled by default
69 because of licensing reasons, but all you need to activate Readline is
70 the following pair of lines.
71
72 @lisp
73 scheme@@(guile-user)> (use-modules (ice-9 readline))
74 scheme@@(guile-user)> (activate-readline)
75 @end lisp
76
77 It's a good idea to put these two lines (without the
78 @code{scheme@@(guile-user)>} prompts) in your @file{.guile} file.
79 @xref{Init File}, for more on @file{.guile}.
80
81
82 @node Value History
83 @subsection Value History
84
85 Just as Readline helps you to reuse a previous input line, @dfn{value
86 history} allows you to use the @emph{result} of a previous evaluation in
87 a new expression. When value history is enabled, each evaluation result
88 is automatically assigned to the next in the sequence of variables
89 @code{$1}, @code{$2}, @dots{}. You can then use these variables in
90 subsequent expressions.
91
92 @lisp
93 scheme@@(guile-user)> (iota 10)
94 $1 = (0 1 2 3 4 5 6 7 8 9)
95 scheme@@(guile-user)> (apply * (cdr $1))
96 $2 = 362880
97 scheme@@(guile-user)> (sqrt $2)
98 $3 = 602.3952191045344
99 scheme@@(guile-user)> (cons $2 $1)
100 $4 = (362880 0 1 2 3 4 5 6 7 8 9)
101 @end lisp
102
103 Value history is enabled by default, because Guile's REPL imports the
104 @code{(ice-9 history)} module. Value history may be turned off or on within the
105 repl, using the options interface:
106
107 @lisp
108 scheme@@(guile-user)> ,option value-history #f
109 scheme@@(guile-user)> 'foo
110 foo
111 scheme@@(guile-user)> ,option value-history #t
112 scheme@@(guile-user)> 'bar
113 $5 = bar
114 @end lisp
115
116 Note that previously recorded values are still accessible, even if value history
117 is off. In rare cases, these references to past computations can cause Guile to
118 use too much memory. One may clear these values, possibly enabling garbage
119 collection, via the @code{clear-value-history!} procedure, described below.
120
121 The programmatic interface to value history is in a module:
122
123 @lisp
124 (use-modules (ice-9 history))
125 @end lisp
126
127 @deffn {Scheme Procedure} value-history-enabled?
128 Return true iff value history is enabled.
129 @end deffn
130
131 @deffn {Scheme Procedure} enable-value-history!
132 Turn on value history, if it was off.
133 @end deffn
134
135 @deffn {Scheme Procedure} disable-value-history!
136 Turn off value history, if it was on.
137 @end deffn
138
139 @deffn {Scheme Procedure} clear-value-history!
140 Clear the value history. If the stored values are not captured by some other
141 data structure or closure, they may then be reclaimed by the garbage collector.
142 @end deffn
143
144
145 @node REPL Commands
146 @subsection REPL Commands
147
148 @cindex commands
149 The REPL exists to read expressions, evaluate them, and then print their
150 results. But sometimes one wants to tell the REPL to evaluate an
151 expression in a different way, or to do something else altogether. A
152 user can affect the way the REPL works with a @dfn{REPL command}.
153
154 The previous section had an example of a command, in the form of
155 @code{,option}.
156
157 @lisp
158 scheme@@(guile-user)> ,option value-history #t
159 @end lisp
160
161 @noindent
162 Commands are distinguished from expressions by their initial comma
163 (@samp{,}). Since a comma cannot begin an expression in most languages,
164 it is an effective indicator to the REPL that the following text forms a
165 command, not an expression.
166
167 REPL commands are convenient because they are always there. Even if the
168 current module doesn't have a binding for @code{pretty-print}, one can
169 always @code{,pretty-print}.
170
171 The following sections document the various commands, grouped together
172 by functionality. Many of the commands have abbreviations; see the
173 online help (@code{,help}) for more information.
174
175 @menu
176 * Help Commands::
177 * Module Commands::
178 * Language Commands::
179 * Compile Commands::
180 * Profile Commands::
181 * Debug Commands::
182 * Inspect Commands::
183 * System Commands::
184 @end menu
185
186 @node Help Commands
187 @subsubsection Help Commands
188
189 When Guile starts interactively, it notifies the user that help can be
190 had by typing @samp{,help}. Indeed, @code{help} is a command, and a
191 particularly useful one, as it allows the user to discover the rest of
192 the commands.
193
194 @deffn {REPL Command} help [@code{all} | group | @code{[-c]} command]
195 Show help.
196
197 With one argument, tries to look up the argument as a group name, giving
198 help on that group if successful. Otherwise tries to look up the
199 argument as a command, giving help on the command.
200
201 If there is a command whose name is also a group name, use the @samp{-c
202 @var{command}} form to give help on the command instead of the group.
203
204 Without any argument, a list of help commands and command groups
205 are displayed.
206 @end deffn
207
208 @deffn {REPL Command} show [topic]
209 Gives information about Guile.
210
211 With one argument, tries to show a particular piece of information;
212 currently supported topics are `warranty' (or `w'), `copying' (or `c'),
213 and `version' (or `v').
214
215 Without any argument, a list of topics is displayed.
216 @end deffn
217
218 @deffn {REPL Command} apropos regexp
219 Find bindings/modules/packages.
220 @end deffn
221
222 @deffn {REPL Command} describe obj
223 Show description/documentation.
224 @end deffn
225
226 @node Module Commands
227 @subsubsection Module Commands
228
229 @deffn {REPL Command} module [module]
230 Change modules / Show current module.
231 @end deffn
232
233 @deffn {REPL Command} import [module ...]
234 Import modules / List those imported.
235 @end deffn
236
237 @deffn {REPL Command} load file
238 Load a file in the current module.
239 @end deffn
240
241 @deffn {REPL Command} reload [module]
242 Reload the given module, or the current module if none was given.
243 @end deffn
244
245 @deffn {REPL Command} binding
246 List current bindings.
247 @end deffn
248
249 @deffn {REPL Command} in module expression
250 @deffnx {REPL Command} in module command [args ...]
251 Evaluate an expression, or alternatively, execute another meta-command
252 in the context of a module. For example, @samp{,in (foo bar) ,binding}
253 will show the bindings in the module @code{(foo bar)}.
254 @end deffn
255
256 @node Language Commands
257 @subsubsection Language Commands
258
259 @deffn {REPL Command} language language
260 Change languages.
261 @end deffn
262
263 @node Compile Commands
264 @subsubsection Compile Commands
265
266 @deffn {REPL Command} compile exp
267 Generate compiled code.
268 @end deffn
269
270 @deffn {REPL Command} compile-file file
271 Compile a file.
272 @end deffn
273
274 @deffn {REPL Command} disassemble exp
275 Disassemble a compiled procedure.
276 @end deffn
277
278 @deffn {REPL Command} disassemble-file file
279 Disassemble a file.
280 @end deffn
281
282 @node Profile Commands
283 @subsubsection Profile Commands
284
285 @deffn {REPL Command} time exp
286 Time execution.
287 @end deffn
288
289 @deffn {REPL Command} profile exp
290 Profile execution.
291 @end deffn
292
293 @deffn {REPL Command} trace exp
294 Trace execution.
295 @end deffn
296
297 @node Debug Commands
298 @subsubsection Debug Commands
299
300 These debugging commands are only available within a recursive REPL;
301 they do not work at the top level.
302
303 @deffn {REPL Command} backtrace [count] [#:width w] [#:full? f]
304 Print a backtrace.
305
306 Print a backtrace of all stack frames, or innermost @var{COUNT} frames.
307 If @var{count} is negative, the last @var{count} frames will be shown.
308 @end deffn
309
310 @deffn {REPL Command} up [count]
311 Select a calling stack frame.
312
313 Select and print stack frames that called this one.
314 An argument says how many frames up to go.
315 @end deffn
316
317 @deffn {REPL Command} down [count]
318 Select a called stack frame.
319
320 Select and print stack frames called by this one.
321 An argument says how many frames down to go.
322 @end deffn
323
324 @deffn {REPL Command} frame [idx]
325 Show a frame.
326
327 Show the selected frame. With an argument, select a frame by index,
328 then show it.
329 @end deffn
330
331 @deffn {REPL Command} procedure
332 Print the procedure for the selected frame.
333 @end deffn
334
335 @deffn {REPL Command} locals
336 Show local variables.
337
338 Show locally-bound variables in the selected frame.
339 @end deffn
340
341 @deffn {REPL Command} error-message
342 @deffnx {REPL Command} error
343 Show error message.
344
345 Display the message associated with the error that started the current
346 debugging REPL.
347 @end deffn
348
349 @deffn {REPL Command} registers
350 Show the VM registers associated with the current frame.
351
352 @xref{Stack Layout}, for more information on VM stack frames.
353 @end deffn
354
355 @deffn {REPL Command} width [cols]
356 Sets the number of display columns in the output of @code{,backtrace}
357 and @code{,locals} to @var{cols}. If @var{cols} is not given, the width
358 of the terminal is used.
359 @end deffn
360
361 The next 3 commands work at any REPL.
362
363 @deffn {REPL Command} break proc
364 Set a breakpoint at @var{proc}.
365 @end deffn
366
367 @deffn {REPL Command} break-at-source file line
368 Set a breakpoint at the given source location.
369 @end deffn
370
371 @deffn {REPL Command} tracepoint proc
372 Set a tracepoint on the given procedure. This will cause all calls to
373 the procedure to print out a tracing message. @xref{Tracing Traps}, for
374 more information.
375 @end deffn
376
377 The rest of the commands in this subsection all apply only when the
378 stack is @dfn{continuable} --- in other words when it makes sense for
379 the program that the stack comes from to continue running. Usually this
380 means that the program stopped because of a trap or a breakpoint.
381
382 @deffn {REPL Command} step
383 Tell the debugged program to step to the next source location.
384 @end deffn
385
386 @deffn {REPL Command} next
387 Tell the debugged program to step to the next source location in the
388 same frame. (See @ref{Traps} for the details of how this works.)
389 @end deffn
390
391 @deffn {REPL Command} finish
392 Tell the program being debugged to continue running until the completion
393 of the current stack frame, and at that time to print the result and
394 reenter the REPL.
395 @end deffn
396
397
398 @node Inspect Commands
399 @subsubsection Inspect Commands
400
401 @deffn {REPL Command} inspect EXP
402 Inspect the result(s) of evaluating @var{exp}.
403 @end deffn
404
405 @deffn {REPL Command} pretty-print EXP
406 Pretty-print the result(s) of evaluating @var{exp}.
407 @end deffn
408
409 @node System Commands
410 @subsubsection System Commands
411
412 @deffn {REPL Command} gc
413 Garbage collection.
414 @end deffn
415
416 @deffn {REPL Command} statistics
417 Display statistics.
418 @end deffn
419
420 @deffn {REPL Command} option [key value]
421 List/show/set options.
422 @end deffn
423
424 @deffn {REPL Command} quit
425 Quit this session.
426 @end deffn
427
428 Current REPL options include:
429
430 @table @code
431 @item compile-options
432 The options used when compiling expressions entered at the REPL.
433 @xref{Compilation}, for more on compilation options.
434 @item interp
435 Whether to interpret or compile expressions given at the REPL, if such a
436 choice is available. Off by default (indicating compilation).
437 @item prompt
438 A customized REPL prompt. @code{#f} by default, indicating the default
439 prompt.
440 @item value-history
441 Whether value history is on or not. @xref{Value History}.
442 @item on-error
443 What to do when an error happens. By default, @code{debug}, meaning to
444 enter the debugger. Other values include @code{backtrace}, to show a
445 backtrace without entering the debugger, or @code{report}, to simply
446 show a short error printout.
447 @end table
448
449 Default values for REPL options may be set using
450 @code{repl-default-option-set!} from @code{(system repl common)}:
451
452 @deffn {Scheme Procedure} repl-set-default-option! key value
453 Set the default value of a REPL option. This function is particularly
454 useful in a user's init file. @xref{Init File}.
455 @end deffn
456
457
458 @node Error Handling
459 @subsection Error Handling
460
461 When code being evaluated from the REPL hits an error, Guile enters a
462 new prompt, allowing you to inspect the context of the error.
463
464 @lisp
465 scheme@@(guile-user)> (map string-append '("a" "b") '("c" #\d))
466 ERROR: In procedure string-append:
467 ERROR: Wrong type (expecting string): #\d
468 Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
469 scheme@@(guile-user) [1]>
470 @end lisp
471
472 The new prompt runs inside the old one, in the dynamic context of the
473 error. It is a recursive REPL, augmented with a reified representation
474 of the stack, ready for debugging.
475
476 @code{,backtrace} (abbreviated @code{,bt}) displays the Scheme call
477 stack at the point where the error occurred:
478
479 @lisp
480 scheme@@(guile-user) [1]> ,bt
481 1 (map #<procedure string-append _> ("a" "b") ("c" #\d))
482 0 (string-append "b" #\d)
483 @end lisp
484
485 In the above example, the backtrace doesn't have much source
486 information, as @code{map} and @code{string-append} are both
487 primitives. But in the general case, the space on the left of the
488 backtrace indicates the line and column in which a given procedure calls
489 another.
490
491 You can exit a recursive REPL in the same way that you exit any REPL:
492 via @samp{(quit)}, @samp{,quit} (abbreviated @samp{,q}), or
493 @kbd{C-d}, among other options.
494
495
496 @node Interactive Debugging
497 @subsection Interactive Debugging
498
499 A recursive debugging REPL exposes a number of other meta-commands that
500 inspect the state of the computation at the time of the error. These
501 commands allow you to
502
503 @itemize @bullet
504 @item
505 display the Scheme call stack at the point where the error occurred;
506
507 @item
508 move up and down the call stack, to see in detail the expression being
509 evaluated, or the procedure being applied, in each @dfn{frame}; and
510
511 @item
512 examine the values of variables and expressions in the context of each
513 frame.
514 @end itemize
515
516 @noindent
517 @xref{Debug Commands}, for documentation of the individual
518 commands. This section aims to give more of a walkthrough of a typical
519 debugging session.
520
521 First, we're going to need a good error. Let's try to macroexpand the
522 expression @code{(unquote foo)}, outside of a @code{quasiquote} form,
523 and see how the macroexpander reports this error.
524
525 @lisp
526 scheme@@(guile-user)> (macroexpand '(unquote foo))
527 ERROR: In procedure macroexpand:
528 ERROR: unquote: expression not valid outside of quasiquote in (unquote foo)
529 Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
530 scheme@@(guile-user) [1]>
531 @end lisp
532
533 The @code{backtrace} command, which can also be invoked as @code{bt},
534 displays the call stack (aka backtrace) at the point where the debugger
535 was entered:
536
537 @lisp
538 scheme@@(guile-user) [1]> ,bt
539 In ice-9/psyntax.scm:
540 1130:21 3 (chi-top (unquote foo) () ((top)) e (eval) (hygiene #))
541 1071:30 2 (syntax-type (unquote foo) () ((top)) #f #f (# #) #f)
542 1368:28 1 (chi-macro #<procedure de9360 at ice-9/psyntax.scm...> ...)
543 In unknown file:
544 0 (scm-error syntax-error macroexpand "~a: ~a in ~a" # #f)
545 @end lisp
546
547 A call stack consists of a sequence of stack @dfn{frames}, with each
548 frame describing one procedure which is waiting to do something with the
549 values returned by another. Here we see that there are four frames on
550 the stack.
551
552 Note that @code{macroexpand} is not on the stack -- it must have made a
553 tail call to @code{chi-top}, as indeed we would find if we searched
554 @code{ice-9/psyntax.scm} for its definition.
555
556 When you enter the debugger, the innermost frame is selected, which
557 means that the commands for getting information about the ``current''
558 frame, or for evaluating expressions in the context of the current
559 frame, will do so by default with respect to the innermost frame. To
560 select a different frame, so that these operations will apply to it
561 instead, use the @code{up}, @code{down} and @code{frame} commands like
562 this:
563
564 @lisp
565 scheme@@(guile-user) [1]> ,up
566 In ice-9/psyntax.scm:
567 1368:28 1 (chi-macro #<procedure de9360 at ice-9/psyntax.scm...> ...)
568 scheme@@(guile-user) [1]> ,frame 3
569 In ice-9/psyntax.scm:
570 1130:21 3 (chi-top (unquote foo) () ((top)) e (eval) (hygiene #))
571 scheme@@(guile-user) [1]> ,down
572 In ice-9/psyntax.scm:
573 1071:30 2 (syntax-type (unquote foo) () ((top)) #f #f (# #) #f)
574 @end lisp
575
576 Perhaps we're interested in what's going on in frame 2, so we take a
577 look at its local variables:
578
579 @lisp
580 scheme@@(guile-user) [1]> ,locals
581 Local variables:
582 $1 = e = (unquote foo)
583 $2 = r = ()
584 $3 = w = ((top))
585 $4 = s = #f
586 $5 = rib = #f
587 $6 = mod = (hygiene guile-user)
588 $7 = for-car? = #f
589 $8 = first = unquote
590 $9 = ftype = macro
591 $10 = fval = #<procedure de9360 at ice-9/psyntax.scm:2817:2 (x)>
592 $11 = fe = unquote
593 $12 = fw = ((top))
594 $13 = fs = #f
595 $14 = fmod = (hygiene guile-user)
596 @end lisp
597
598 All of the values are accessible by their value-history names
599 (@code{$@var{n}}):
600
601 @lisp
602 scheme@@(guile-user) [1]> $10
603 $15 = #<procedure de9360 at ice-9/psyntax.scm:2817:2 (x)>
604 @end lisp
605
606 We can even invoke the procedure at the REPL directly:
607
608 @lisp
609 scheme@@(guile-user) [1]> ($10 'not-going-to-work)
610 ERROR: In procedure macroexpand:
611 ERROR: source expression failed to match any pattern in not-going-to-work
612 Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
613 @end lisp
614
615 Well at this point we've caused an error within an error. Let's just
616 quit back to the top level:
617
618 @lisp
619 scheme@@(guile-user) [2]> ,q
620 scheme@@(guile-user) [1]> ,q
621 scheme@@(guile-user)>
622 @end lisp
623
624 Finally, as a word to the wise: hackers close their REPL prompts with
625 @kbd{C-d}.
626
627
628 @node Using Guile in Emacs
629 @section Using Guile in Emacs
630
631 @cindex Emacs
632 Any text editor can edit Scheme, but some are better than others. Emacs
633 is the best, of course, and not just because it is a fine text editor.
634 Emacs has good support for Scheme out of the box, with sensible
635 indentation rules, parenthesis-matching, syntax highlighting, and even a
636 set of keybindings for structural editing, allowing navigation,
637 cut-and-paste, and transposition operations that work on balanced
638 S-expressions.
639
640 As good as it is, though, two things will vastly improve your experience
641 with Emacs and Guile.
642
643 @cindex Paredit
644 The first is Taylor Campbell's
645 @uref{http://www.emacswiki.org/emacs/ParEdit, Paredit}. You should not
646 code in any dialect of Lisp without Paredit. (They say that
647 unopinionated writing is boring---hence this tone---but it's the
648 truth, regardless.) Paredit is the bee's knees.
649
650 @cindex Geiser
651 The second is
652 @iftex
653 Jos@'e
654 @end iftex
655 @ifnottex
656 José
657 @end ifnottex
658 Antonio Ortega Ruiz's
659 @uref{http://www.nongnu.org/geiser/, Geiser}. Geiser complements Emacs'
660 @code{scheme-mode} with tight integration to running Guile processes via
661 a @code{comint-mode} REPL buffer.
662
663 Of course there are keybindings to switch to the REPL, and a good REPL
664 environment, but Geiser goes beyond that, providing:
665
666 @itemize @bullet
667 @item
668 Form evaluation in the context of the current file's module.
669 @item
670 Macro expansion.
671 @item
672 File/module loading and/or compilation.
673 @item
674 Namespace-aware identifier completion (including local bindings, names
675 visible in the current module, and module names).
676 @item
677 Autodoc: the echo area shows information about the signature of the
678 procedure/macro around point automatically.
679 @item
680 Jump to definition of identifier at point.
681 @item
682 Access to documentation (including docstrings when the implementation
683 provides it).
684 @item
685 Listings of identifiers exported by a given module.
686 @item
687 Listings of callers/callees of procedures.
688 @item
689 Rudimentary support for debugging and error navigation.
690 @item
691 Support for multiple, simultaneous REPLs.
692 @end itemize
693
694 See Geiser's web page at @uref{http://www.nongnu.org/geiser/}, for more
695 information.
696
697
698 @node Using Guile Tools
699 @section Using Guile Tools
700
701 @cindex guild
702 @cindex guile-tools
703 @cindex wizards
704 Guile also comes with a growing number of command-line utilities: a
705 compiler, a disassembler, some module inspectors, and in the future, a
706 system to install Guile packages from the internet. These tools may be
707 invoked using the @code{guild} program.
708
709 @example
710 $ guild compile -o foo.go foo.scm
711 wrote `foo.go'
712 @end example
713
714 This program used to be called @code{guile-tools} up to
715 Guile version 2.0.1, and for backward
716 compatibility it still may be called as such. However we changed the
717 name to @code{guild}, not only because it is pleasantly shorter and
718 easier to read, but also because this tool will serve to bind Guile
719 wizards together, by allowing hackers to share code with each other
720 using a CPAN-like system.
721
722 @xref{Compilation}, for more on @code{guild compile}.
723
724 A complete list of guild scripts can be had by invoking @code{guild
725 list}, or simply @code{guild}.
726
727 @c Local Variables:
728 @c TeX-master: "guile.texi"
729 @c End: