(Higher level thread procedures): Replace some
[bpt/guile.git] / doc / scheme-control.texi
CommitLineData
38a93523
NJ
1@page
2@node Control Mechanisms
3@chapter Controlling the Flow of Program Execution
4
5@menu
6* begin:: Evaluating a sequence of expressions.
7* if cond case:: Simple conditional evaluation.
8* and or:: Conditional evaluation of a sequence.
9* while do:: Iteration mechanisms.
10* Continuations:: Continuations.
11* Multiple Values:: Returning and accepting multiple values.
12* Exceptions:: Throwing and catching exceptions.
13* Error Reporting:: Procedures for signaling errors.
14* Dynamic Wind:: Guarding against non-local entrance/exit.
15@end menu
16
17
18@node begin
19@section Evaluating a Sequence of Expressions
20
21
22@node if cond case
23@section Simple Conditional Evaluation
24
25
26@node and or
27@section Conditional Evaluation of a Sequence of Expressions
28
29
30@node while do
31@section Iteration mechanisms
32
33
34@node Continuations
35@section Continuations
36
fcaedf99
MG
37@r5index call-with-current-continuation
38@c FIXME::martin: Document me!
39@deffn primitive call-with-current-continuation
40@end deffn
38a93523
NJ
41
42@node Multiple Values
43@section Returning and Accepting Multiple Values
44
fcaedf99 45@r5index values
38a93523
NJ
46@deffn primitive values . args
47Delivers all of its arguments to its continuation. Except for
48continuations created by the @code{call-with-values} procedure,
49all continuations take exactly one value. The effect of
50passing no value or more than one value to continuations that
51were not created by @code{call-with-values} is unspecified.
52@end deffn
53
fcaedf99 54@r5index call-with-values
38a93523
NJ
55@deffn primitive call-with-values producer consumer
56Calls its @var{producer} argument with no values and a
57continuation that, when passed some values, calls the
58@var{consumer} procedure with those values as arguments. The
59continuation for the call to @var{consumer} is the continuation
60of the call to @code{call-with-values}.
61
62@example
63(call-with-values (lambda () (values 4 5))
64 (lambda (a b) b))
65 ==> 5
66
67@end example
68@example
69(call-with-values * -) ==> -1
70@end example
71@end deffn
72
73
74@node Exceptions
75@section Exceptions
76@cindex error handling
77@cindex exception handling
78
79It is traditional in Scheme to implement exception systems using
80@code{call-with-current-continuation}. Guile does not do this, for
81performance reasons. The implementation of
82@code{call-with-current-continuation} is a stack copying implementation.
83This allows it to interact well with ordinary C code. Unfortunately, a
84stack-copying implementation can be slow -- creating a new continuation
85involves a block copy of the stack.
86
87Instead of using @code{call-with-current-continuation}, the exception
88primitives documented here are implemented as built-ins that take
89advantage of the @emph{upward only} nature of exceptions.
90
38a93523 91@c docstring begin (texi-doc-string "guile" "catch")
ae9f3a15 92@deffn primitive catch key thunk handler
38a93523 93Invoke @var{thunk} in the dynamic context of @var{handler} for
ae9f3a15
MG
94exceptions matching @var{key}. If thunk throws to the symbol
95@var{key}, then @var{handler} is invoked this way:
96@lisp
38a93523 97(handler key args ...)
ae9f3a15
MG
98@end lisp
99@var{key} is a symbol or @code{#t}.
100@var{thunk} takes no arguments. If @var{thunk} returns
101normally, that is the return value of @code{catch}.
102Handler is invoked outside the scope of its own @code{catch}.
103If @var{handler} again throws to the same key, a new handler
104from further up the call chain is invoked.
105If the key is @code{#t}, then a throw to @emph{any} symbol will
106match this call to @code{catch}.
38a93523
NJ
107@end deffn
108
109@c docstring begin (texi-doc-string "guile" "throw")
110@deffn primitive throw key . args
111Invoke the catch form matching @var{key}, passing @var{args} to the
112@var{handler}.
113
114@var{key} is a symbol. It will match catches of the same symbol or of
115#t.
116
117If there is no handler at all, an error is signaled.
118@end deffn
119
120@c docstring begin (texi-doc-string "guile" "lazy-catch")
ae9f3a15 121@deffn primitive lazy-catch key thunk handler
38a93523
NJ
122This behaves exactly like @code{catch}, except that it does
123not unwind the stack (this is the major difference), and if
124handler returns, its value is returned from the throw.
125@end deffn
126
127
128@node Error Reporting
129@section Procedures for Signaling Errors
130
131Guile provides a set of convenience procedures for signaling error
132conditions that are implemented on top of the exception primitives just
133described.
134
135@c begin (scm-doc-string "boot-9.scm" "error")
136@deffn procedure error msg args @dots{}
137Raise an error with key @code{misc-error} and a message constructed by
138displaying @var{msg} and writing @var{args}.
139@end deffn
140@c end
141
38a93523 142@c docstring begin (texi-doc-string "guile" "scm-error")
ae9f3a15
MG
143@deffn primitive scm-error key subr message args data
144Raise an error with key @var{key}. @var{subr} can be a string
145naming the procedure associated with the error, or @code{#f}.
146@var{message} is the error message string, possibly containing
147@code{~S} and @code{~A} escapes. When an error is reported,
148these are replaced by formatting the corresponding members of
149@var{args}: @code{~A} (was @code{%s} in older versions of
150Guile) formats using @code{display} and @code{~S} (was
151@code{%S}) formats using @code{write}. @var{data} is a list or
152@code{#f} depending on @var{key}: if @var{key} is
153@code{system-error} then it should be a list containing the
154Unix @code{errno} value; If @var{key} is @code{signal} then it
155should be a list containing the Unix signal number; otherwise
156it will usually be @code{#f}.
38a93523
NJ
157@end deffn
158
159@c docstring begin (texi-doc-string "guile" "strerror")
160@deffn primitive strerror err
ae9f3a15
MG
161Return the Unix error message corresponding to @var{err}, which
162must be an integer value.
38a93523
NJ
163@end deffn
164
165@c begin (scm-doc-string "boot-9.scm" "false-if-exception")
166@deffn syntax false-if-exception expr
167Returns the result of evaluating its argument; however
168if an exception occurs then @code{#f} is returned instead.
169@end deffn
170@c end
171
172
173@node Dynamic Wind
174@section Dynamic Wind
175
176[FIXME: this is pasted in from Tom Lord's original guile.texi and should
177be reviewed]
178
fcaedf99 179@r5index dynamic-wind
38a93523 180@c docstring begin (texi-doc-string "guile" "dynamic-wind")
ae9f3a15 181@deffn primitive dynamic-wind in_guard thunk out_guard
38a93523 182All three arguments must be 0-argument procedures.
ae9f3a15
MG
183@var{in_guard} is called, then @var{thunk}, then
184@var{out_guard}.
185If, any time during the execution of @var{thunk}, the
186continuation of the @code{dynamic_wind} expression is escaped
187non-locally, @var{out_guard} is called. If the continuation of
188the dynamic-wind is re-entered, @var{in_guard} is called. Thus
189@var{in_guard} and @var{out_guard} may be called any number of
190times.
191@lisp
38a93523
NJ
192(define x 'normal-binding)
193@result{} x
38a93523
NJ
194(define a-cont (call-with-current-continuation
195 (lambda (escape)
196 (let ((old-x x))
197 (dynamic-wind
198 ;; in-guard:
199 ;;
200 (lambda () (set! x 'special-binding))
38a93523
NJ
201 ;; thunk
202 ;;
203 (lambda () (display x) (newline)
204 (call-with-current-continuation escape)
205 (display x) (newline)
206 x)
38a93523
NJ
207 ;; out-guard:
208 ;;
209 (lambda () (set! x old-x)))))))
38a93523
NJ
210;; Prints:
211special-binding
212;; Evaluates to:
213@result{} a-cont
38a93523
NJ
214x
215@result{} normal-binding
38a93523
NJ
216(a-cont #f)
217;; Prints:
218special-binding
219;; Evaluates to:
220@result{} a-cont ;; the value of the (define a-cont...)
38a93523
NJ
221x
222@result{} normal-binding
38a93523
NJ
223a-cont
224@result{} special-binding
ae9f3a15 225@end lisp
38a93523
NJ
226@end deffn
227@c Local Variables:
228@c TeX-master: "guile.texi"
229@c End: