]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations-docs.factor
Merge branch 'master' of git://github.com/slavapestov/factor
[factor.git] / core / continuations / continuations-docs.factor
1 USING: help.markup help.syntax kernel kernel.private
2 continuations.private vectors arrays namespaces
3 assocs words quotations lexer sequences math ;
4 IN: continuations
5
6 ARTICLE: "errors-restartable" "Restartable errors"
7 "Support for restartable errors is built on top of the basic error handling facility. The following words signals recoverable errors:"
8 { $subsections
9     throw-restarts
10     rethrow-restarts
11 }
12 "A utility word using the above:"
13 { $subsections
14     throw-continue
15 }
16 "The list of restarts from the most recently-thrown error is stored in a global variable:"
17 { $subsections restarts }
18 "To invoke restarts, use " { $link "debugger" } "." ;
19
20 ARTICLE: "errors-post-mortem" "Post-mortem error inspection"
21 "The most recently thrown error, together with the continuation at that point, are stored in a pair of global variables:"
22 { $subsections
23     error
24     error-continuation
25 }
26 "Developer tools for inspecting these values are found in " { $link "debugger" } "." ;
27
28 ARTICLE: "errors-anti-examples" "Common error handling pitfalls"
29 "When used correctly, exception handling can lead to more robust code with less duplication of error handling logic. However, there are some pitfalls to keep in mind."
30 { $heading "Anti-pattern #1: Ignoring errors" }
31 "The " { $link ignore-errors } " word should almost never be used. Ignoring errors does not make code more robust and in fact makes it much harder to debug if an intermittent error does show up when the code is run under previously unforseen circumstances. Never ignore unexpected errors; always report them to the user."
32 { $heading "Anti-pattern #2: Catching errors too early" }
33 "A less severe form of the previous anti-pattern is code that makes overly zealous use of " { $link recover } ". It is almost always a mistake to catch an error, log a message, and keep going. The only exception is network servers and other long-running processes that must remain running even if individual tasks fail. In these cases, place the " { $link recover } " as high up in the call stack as possible."
34 $nl
35 "In most other cases, " { $link cleanup } " should be used instead to handle an error and rethrow it automatically."
36 { $heading "Anti-pattern #3: Dropping and rethrowing" }
37 "Do not use " { $link recover } " to handle an error by dropping it and throwing a new error. By losing the original error message, you signal to the user that something failed without leaving any indication of what actually went wrong. Either wrap the error in a new error containing additional information, or rethrow the original error. A more subtle form of this is using " { $link throw } " instead of " { $link rethrow } ". The " { $link throw } " word should only be used when throwing new errors, and never when rethrowing errors that have been caught."
38 { $heading "Anti-pattern #4: Logging and rethrowing" }
39 "If you are going to rethrow an error, do not log a message. If you do so, the user will see two log messages for the same error, which will clutter logs without adding any useful information." ;
40
41 ARTICLE: "errors" "Exception handling"
42 "Support for handling exceptional situations such as bad user input, implementation bugs, and input/output errors is provided by a set of words built using continuations."
43 $nl
44 "Two words raise an error in the innermost error handler for the current dynamic extent:"
45 { $subsections
46     throw
47     rethrow
48 }
49 "Words for establishing an error handler:"
50 { $subsections
51     cleanup
52     recover
53     ignore-errors
54 }
55 "Syntax sugar for defining errors:"
56 { $subsections POSTPONE: ERROR: }
57 "Unhandled errors are reported in the listener and can be debugged using various tools. See " { $link "debugger" } "."
58 { $subsections
59     "errors-restartable"
60     "debugger"
61     "errors-post-mortem"
62     "errors-anti-examples"
63 }
64 "When Factor encouters a critical error, it calls the following word:"
65 { $subsections die } ;
66
67 ARTICLE: "continuations.private" "Continuation implementation details"
68 "A continuation is simply a tuple holding the contents of the five stacks:"
69 { $subsections
70     continuation
71     >continuation<
72 }
73 "The five stacks can be read and written:"
74 { $subsections
75     datastack
76     set-datastack
77     retainstack
78     set-retainstack
79     callstack
80     set-callstack
81     namestack
82     set-namestack
83     catchstack
84     set-catchstack
85 } ;
86
87 ARTICLE: "continuations" "Continuations"
88 "At any point in the execution of a program, the " { $emphasis "current continuation" } " represents the future of the computation."
89 $nl
90 "Words for working with continuations are found in the " { $vocab-link "continuations" } " vocabulary; implementation details are in " { $vocab-link "continuations.private" } "."
91 $nl
92 "Continuations can be reified with the following two words:"
93 { $subsections
94     callcc0
95     callcc1
96 }
97 "Another two words resume continuations:"
98 { $subsections
99     continue
100     continue-with
101 }
102 "Continuations as control-flow:"
103 { $subsections
104     attempt-all
105     with-return
106 }
107 "Continuations serve as the building block for a number of higher-level abstractions, such as " { $link "errors" } " and " { $link "threads" } "."
108 { $subsections "continuations.private" } ;
109
110 ABOUT: "continuations"
111
112 HELP: catchstack*
113 { $values { "catchstack" "a vector of continuations" } }
114 { $description "Outputs the current catchstack." } ;
115
116 HELP: catchstack
117 { $values { "catchstack" "a vector of continuations" } }
118 { $description "Outputs a copy of the current catchstack." } ;
119
120 HELP: set-catchstack
121 { $values { "catchstack" "a vector of continuations" } }
122 { $description "Replaces the catchstack with a copy of the given vector." } ;
123
124 HELP: continuation
125 { $values { "continuation" continuation } }
126 { $description "Reifies the current continuation from the point immediately after which the caller returns." } ;
127
128 HELP: >continuation<
129 { $values { "continuation" continuation } { "data" vector } { "call" vector } { "retain" vector } { "name" vector } { "catch" vector } }
130 { $description "Takes a continuation apart into its constituents." } ;
131
132 HELP: ifcc
133 { $values { "capture" { $quotation "( continuation -- )" } } { "restore" quotation } }
134 { $description "Reifies a continuation from the point immediately after which this word returns, and passes it to " { $snippet "capture" } ". When the continuation is restored, execution resumes and " { $snippet "restore" } " is called." } ;
135
136 { callcc0 continue callcc1 continue-with ifcc } related-words
137
138 HELP: callcc0
139 { $values { "quot" { $quotation "( continuation -- )" } } }
140 { $description "Applies the quotation to the current continuation, which is reified from the point immediately after which the caller returns. The " { $link continue } " word resumes the continuation." } ;
141
142 HELP: callcc1
143 { $values { "quot" { $quotation "( continuation -- )" } } { "obj" "an object provided when resuming the continuation" } }
144 { $description "Applies the quotation to the current continuation, which is reified from the point immediately after which the caller returns. The " { $link continue-with } " word resumes the continuation, passing a value back to the original execution context." } ;
145
146 HELP: continue
147 { $values { "continuation" continuation } }
148 { $description "Resumes a continuation reified by " { $link callcc0 } "." } ;
149
150 HELP: continue-with
151 { $values { "obj" "an object to pass to the continuation's execution context" } { "continuation" continuation } }
152 { $description "Resumes a continuation reified by " { $link callcc1 } ". The object will be placed on the data stack when the continuation resumes." } ;
153
154 HELP: error
155 { $description "Global variable holding most recently thrown error." }
156 { $notes "Only updated by " { $link throw } ", not " { $link rethrow } "." } ;
157
158 HELP: error-continuation
159 { $description "Global variable holding current continuation of most recently thrown error." }
160 { $notes "Only updated by " { $link throw } ", not " { $link rethrow } "." } ;
161
162 HELP: restarts
163 { $var-description "Global variable holding the set of possible restarts for the most recently thrown error." }
164 { $notes "Only updated by " { $link throw } ", not " { $link rethrow } "." } ;
165
166 HELP: >c
167 { $values { "continuation" continuation } }
168 { $description "Pushes an exception handler continuation on the catch stack. The continuation must have been reified by " { $link callcc1 } "." } ;
169
170 HELP: c>
171 { $values { "continuation" continuation } }
172 { $description "Pops an exception handler continuation from the catch stack." } ;
173
174 HELP: throw
175 { $values { "error" object } }
176 { $description "Saves the current continuation in the " { $link error-continuation } " global variable and throws an error. Execution does not continue at the point after the " { $link throw } " call. Rather, the innermost catch block is invoked, and execution continues at that point." } ;
177
178 { cleanup recover } related-words
179
180 HELP: cleanup
181 { $values { "try" quotation } { "cleanup-always" quotation } { "cleanup-error" quotation } }
182 { $description "Calls the " { $snippet "try" } " quotation. If no error is thrown, calls " { $snippet "cleanup-always" } " without restoring the data stack. If an error is thrown, restores the data stack, calls " { $snippet "cleanup-always" } " followed by " { $snippet "cleanup-error" } ", and rethrows the error." } ;
183
184 HELP: recover
185 { $values { "try" { $quotation "( ..a -- ..b )" } } { "recovery" { $quotation "( ..a error -- ..b )" } } }
186 { $description "Calls the " { $snippet "try" } " quotation. If an exception is thrown in the dynamic extent of the " { $snippet "try" } " quotation, restores the data stack and calls the " { $snippet "recovery" } " quotation to handle the error." } ;
187
188 HELP: ignore-errors
189 { $values { "quot" quotation } }
190 { $description "Calls the quotation. If an exception is thrown in the dynamic extent of the quotation, restores the data stack and returns." } ;
191
192 HELP: rethrow
193 { $values { "error" object } }
194 { $description "Throws an error without saving the current continuation in the " { $link error-continuation } " global variable. This is done so that inspecting the error stacks sheds light on the original cause of the exception, rather than the point where it was rethrown." }
195 { $notes
196     "This word is intended to be used in conjunction with " { $link recover } " to implement error handlers which perform an action and pass the error to the next outermost error handler."
197 }
198 { $examples
199     "The " { $link with-lexer } " word catches errors, annotates them with the current line and column number, and rethrows them:"
200     { $see with-lexer }
201 } ;
202
203 HELP: throw-restarts
204 { $values { "error" object } { "restarts" "a sequence of " { $snippet "{ string object }" } " pairs" } { "restart" object } }
205 { $description "Throws a restartable error using " { $link throw } ". The " { $snippet "restarts" } " parameter is a sequence of pairs where the first element in each pair is a human-readable description and the second is an arbitrary object. If the error reaches the top-level error handler, the user will be presented with the list of possible restarts, and upon invoking one, execution will continue after the call to " { $link throw-restarts } " with the object associated to the chosen restart on the stack." }
206 { $examples
207     "Try invoking one of the two restarts which are offered after the below code throws an error:"
208     { $code
209         ": restart-test ( -- )"
210         "    \"Oops!\" { { \"One\" 1 } { \"Two\" 2 } } throw-restarts"
211         "    \"You restarted: \" write . ;"
212         "restart-test"
213     }
214 } ;
215
216 HELP: rethrow-restarts
217 { $values { "error" object } { "restarts" "a sequence of " { $snippet "{ string object }" } " pairs" } { "restart" object } }
218 { $description "Throws a restartable error using " { $link rethrow } ". Otherwise, this word is identical to " { $link throw-restarts } "." } ;
219
220 { throw rethrow throw-restarts rethrow-restarts throw-continue } related-words
221
222 HELP: throw-continue
223 { $values { "error" object } }
224 { $description "Throws a resumable error. If the user elects to continue execution, this word returns normally." } ;
225
226 HELP: compute-restarts
227 { $values { "error" object } { "seq" "a sequence" } }
228 { $description "Outputs a sequence of triples, where each triple consists of a human-readable string, an object, and a continuation. Resuming a continuation with the corresponding object restarts execution immediately after the corresponding call to " { $link condition } "."
229 $nl
230 "This word recursively travels up the delegation chain to collate restarts from nested and wrapped conditions." } ;
231
232 HELP: save-error
233 { $values { "error" "an error" } }
234 { $description "Called by the error handler to set the " { $link error } " and " { $link restarts } " global variables after an error was thrown." }
235 $low-level-note ;
236
237 HELP: with-datastack
238 { $values { "stack" sequence } { "quot" quotation } { "new-stack" sequence } }
239 { $description "Executes the quotation with the given data stack contents, and outputs the new data stack after the word returns. The input sequence is not modified; a new sequence is produced. Does not affect the data stack in surrounding code, other than consuming the two inputs and pushing the output." }
240 { $examples
241     { $example "USING: continuations math prettyprint ;" "{ 3 7 } [ + ] with-datastack ." "{ 10 }" }
242 } ;
243
244 HELP: attempt-all
245 { $values
246      { "seq" sequence } { "quot" quotation }
247      { "obj" object } }
248 { $description "Applies the quotation to elements in a sequence and returns the value from the first quotation that does not throw an error. If all quotations throw an error, returns the last error thrown." }
249 { $examples "The first two numbers throw, the last one doesn't:"
250     { $example
251     "USING: prettyprint continuations kernel math ;"
252     "{ 1 3 6 } [ dup odd? [ \"Odd\" throw ] when ] attempt-all ."
253     "6" }
254     "All quotations throw, the last exception is rethrown:"
255     { $example
256     "USING: prettyprint continuations kernel math ;"
257     "[ { 1 3 5 } [ dup odd? [ throw ] when ] attempt-all ] [ ] recover ."
258     "5"
259     }
260 } ;
261
262 HELP: return
263 { $description "Returns early from a quotation by reifying the continuation captured by " { $link with-return } " ; execution is resumed starting immediately after " { $link with-return } "." } ;
264
265 HELP: with-return
266 { $values
267      { "quot" quotation } }
268 { $description "Captures a continuation that can be reified by calling the " { $link return } " word. If so, it will resume execution immediately after the " { $link with-return } " word. If " { $link return } " is not called, then execution proceeds as if this word were simply " { $link call } "." }
269 { $examples
270     "Only \"Hi\" will print:"
271     { $example
272     "USING: prettyprint continuations io ;"
273     "[ \"Hi\" print return \"Bye\" print ] with-return"
274     "Hi"
275 } } ;
276
277 { return with-return } related-words
278
279 HELP: restart
280 { $values { "restart" restart } }
281 { $description "Invokes a restart." }
282 { $class-description "The class of restarts." } ;