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