]> gitweb.factorcode.org Git - factor.git/blob - core/continuations/continuations-docs.factor
f5ebc2a3389f3145da505e5d918baa3a1c1acb8d
[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 ;
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 { $subsection throw-restarts }
9 { $subsection rethrow-restarts }
10 "The list of restarts from the most recently-thrown error is stored in a global variable:"
11 { $subsection restarts }
12 "To invoke restarts, see " { $link "debugger" } "." ;
13
14 ARTICLE: "errors-post-mortem" "Post-mortem error inspection"
15 "The most recently thrown error, together with the continuation at that point, are stored in a pair of global variables:"
16 { $subsection error }
17 { $subsection error-continuation }
18 "Developer tools for inspecting these values are found in " { $link "debugger" } "." ;
19
20 ARTICLE: "errors-anti-examples" "Common error handling pitfalls"
21 "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."
22 { $heading "Anti-pattern #1: Ignoring errors" }
23 "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."
24 { $heading "Anti-pattern #2: Catching errors too early" }
25 "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."
26 $nl
27 "In most other cases, " { $link cleanup } " should be used instead to handle an error and rethrow it automatically."
28 { $heading "Anti-pattern #3: Dropping and rethrowing" }
29 "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."
30 { $heading "Anti-pattern #4: Logging and rethrowing" }
31 "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." ;
32
33 ARTICLE: "errors" "Error handling"
34 "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."
35 $nl
36 "Two words raise an error in the innermost error handler for the current dynamic extent:"
37 { $subsection throw }
38 { $subsection rethrow }
39 "Words for establishing an error handler:"
40 { $subsection cleanup }
41 { $subsection recover }
42 { $subsection ignore-errors }
43 "Syntax sugar for defining errors:"
44 { $subsection POSTPONE: ERROR: }
45 "Unhandled errors are reported in the listener and can be debugged using various tools. See " { $link "debugger" } "."
46 { $subsection "errors-restartable" }
47 { $subsection "debugger" }
48 { $subsection "errors-post-mortem" }
49 { $subsection "errors-anti-examples" }
50 "When Factor encouters a critical error, it calls the following word:"
51 { $subsection die } ;
52
53 ARTICLE: "continuations.private" "Continuation implementation details"
54 "A continuation is simply a tuple holding the contents of the five stacks:"
55 { $subsection continuation }
56 { $subsection >continuation< }
57 "The five stacks can be read and written:"
58 { $subsection datastack }
59 { $subsection set-datastack }
60 { $subsection retainstack }
61 { $subsection set-retainstack }
62 { $subsection callstack }
63 { $subsection set-callstack }
64 { $subsection namestack }
65 { $subsection set-namestack }
66 { $subsection catchstack }
67 { $subsection set-catchstack } ;
68
69 ARTICLE: "continuations" "Continuations"
70 "At any point in the execution of a program, the " { $emphasis "current continuation" } " represents the future of the computation."
71 $nl
72 "Words for working with continuations are found in the " { $vocab-link "continuations" } " vocabulary; implementation details are in " { $vocab-link "continuations.private" } "."
73 $nl
74 "Continuations can be reified with the following two words:"
75 { $subsection callcc0 }
76 { $subsection callcc1 }
77 "Another two words resume continuations:"
78 { $subsection continue }
79 { $subsection continue-with }
80 "Continuations as control-flow:"
81 { $subsection attempt-all }
82 { $subsection with-return }
83 "Reflecting the datastack:"
84 { $subsection with-datastack }
85 "Continuations serve as the building block for a number of higher-level abstractions, such as " { $link "errors" } " and " { $link "threads" } "."
86 { $subsection "continuations.private" } ;
87
88 ABOUT: "continuations"
89
90 HELP: catchstack*
91 { $values { "catchstack" "a vector of continuations" } }
92 { $description "Outputs the current catchstack." } ;
93
94 HELP: catchstack
95 { $values { "catchstack" "a vector of continuations" } }
96 { $description "Outputs a copy of the current catchstack." } ;
97
98 HELP: set-catchstack
99 { $values { "catchstack" "a vector of continuations" } }
100 { $description "Replaces the catchstack with a copy of the given vector." } ;
101
102 HELP: continuation
103 { $values { "continuation" continuation } }
104 { $description "Reifies the current continuation from the point immediately after which the caller returns." } ;
105
106 HELP: >continuation<
107 { $values { "continuation" continuation } { "data" vector } { "retain" vector } { "call" vector } { "name" vector } { "catch" vector } }
108 { $description "Takes a continuation apart into its constituents." } ;
109
110 HELP: ifcc
111 { $values { "capture" "a quotation with stack effect " { $snippet "( continuation -- )" } } { "restore" quotation } }
112 { $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." } ;
113
114 { callcc0 continue callcc1 continue-with ifcc } related-words
115
116 HELP: callcc0
117 { $values { "quot" "a quotation with stack effect " { $snippet "( continuation -- )" } } }
118 { $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." } ;
119
120 HELP: callcc1
121 { $values { "quot" "a quotation with stack effect " { $snippet "( continuation -- )" } } { "obj" "an object provided when resuming the continuation" } }
122 { $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." } ;
123
124 HELP: continue
125 { $values { "continuation" continuation } }
126 { $description "Resumes a continuation reified by " { $link callcc0 } "." } ;
127
128 HELP: continue-with
129 { $values { "obj" "an object to pass to the continuation's execution context" } { "continuation" continuation } }
130 { $description "Resumes a continuation reified by " { $link callcc1 } ". The object will be placed on the data stack when the continuation resumes." } ;
131
132 HELP: error
133 { $description "Global variable holding most recently thrown error." }
134 { $notes "Only updated by " { $link throw } ", not " { $link rethrow } "." } ;
135
136 HELP: error-continuation
137 { $description "Global variable holding current continuation of most recently thrown error." }
138 { $notes "Only updated by " { $link throw } ", not " { $link rethrow } "." } ;
139
140 HELP: restarts
141 { $var-description "Global variable holding the set of possible restarts for the most recently thrown error." }
142 { $notes "Only updated by " { $link throw } ", not " { $link rethrow } "." } ;
143
144 HELP: >c
145 { $values { "continuation" continuation } }
146 { $description "Pushes an exception handler continuation on the catch stack. The continuation must have been reified by " { $link callcc1 } "." } ;
147
148 HELP: c>
149 { $values { "continuation" continuation } }
150 { $description "Pops an exception handler continuation from the catch stack." } ;
151
152 HELP: throw
153 { $values { "error" object } }
154 { $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." } ;
155
156 { cleanup recover } related-words
157
158 HELP: cleanup
159 { $values { "try" quotation } { "cleanup-always" quotation } { "cleanup-error" quotation } }
160 { $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." } ;
161
162 HELP: recover
163 { $values { "try" quotation } { "recovery" "a quotation with stack effect " { $snippet "( error -- )" } } }
164 { $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." } ;
165
166 HELP: ignore-errors
167 { $values { "quot" quotation } }
168 { $description "Calls the quotation. If an exception is thrown in the dynamic extent of the quotation, restores the data stack and returns." } ;
169
170 HELP: rethrow
171 { $values { "error" object } }
172 { $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." }
173 { $notes
174     "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."
175 }
176 { $examples
177     "The " { $link with-lexer } " word catches errors, annotates them with the current line and column number, and rethrows them:"
178     { $see with-lexer }
179 } ;
180
181 HELP: throw-restarts
182 { $values { "error" object } { "restarts" "a sequence of " { $snippet "{ string object }" } " pairs" } { "restart" object } }
183 { $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." }
184 { $examples
185     "Try invoking one of the two restarts which are offered after the below code throws an error:"
186     { $code
187         ": restart-test"
188         "    \"Oops!\" { { \"One\" 1 } { \"Two\" 2 } } condition"
189         "    \"You restarted: \" write . ;"
190         "restart-test"
191     }
192 } ;
193
194 HELP: rethrow-restarts
195 { $values { "error" object } { "restarts" "a sequence of " { $snippet "{ string object }" } " pairs" } { "restart" object } }
196 { $description "Throws a restartable error using " { $link rethrow } ". Otherwise, this word is identical to " { $link throw-restarts } "." } ;
197
198 { throw rethrow throw-restarts rethrow-restarts } related-words
199
200 HELP: compute-restarts
201 { $values { "error" object } { "seq" "a sequence" } }
202 { $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 } "."
203 $nl
204 "This word recursively travels up the delegation chain to collate restarts from nested and wrapped conditions." } ;
205
206 HELP: save-error
207 { $values { "error" "an error" } }
208 { $description "Called by the error handler to set the " { $link error } " and " { $link restarts } " global variables after an error was thrown." }
209 $low-level-note ;
210
211 HELP: with-datastack
212 { $values { "stack" sequence } { "quot" quotation } { "newstack" sequence } }
213 { $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. Does not affect the data stack in surrounding code, other than consuming the two inputs and pushing the output." }
214 { $examples
215     { $example "USING: continuations math prettyprint ;" "{ 3 7 } [ + ] with-datastack ." "{ 10 }" }
216 } ;
217
218 HELP: <continuation>
219 { $description "Constructs a new continuation." }
220 { $notes "User code should call " { $link continuation } " instead." } ;
221
222 HELP: attempt-all
223 { $values
224      { "seq" sequence } { "quot" quotation }
225      { "obj" object } }
226 { $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." }
227 { $examples "The first two numbers throw, the last one doesn't:"
228     { $example
229     "USING: prettyprint continuations kernel math ;"
230     "{ 1 3 6 } [ dup odd? [ \"Odd\" throw ] when ] attempt-all ."
231     "6" }
232     "All quotations throw, the last exception is rethrown:"
233     { $example
234     "USING: prettyprint continuations kernel math ;"
235     "[ { 1 3 5 } [ dup odd? [ throw ] when ] attempt-all ] [ ] recover ."
236     "5"
237     }
238 } ;
239
240 HELP: return
241 { $description "Returns early from a quotation by reifying the continuation captured by " { $link with-return } " ; execution is resumed starting immediately after " { $link with-return } "." } ;
242
243 HELP: with-return
244 { $values
245      { "quot" quotation } }
246 { $description "Captures a continuation that can be reified by calling the " { $link return } " word. If so, it will resume execution immediatly after the " { $link with-return } " word. If " { $link return } " is not called, then execution proceeds as if this word were simply " { $link call } "." }
247 { $examples
248     "Only \"Hi\" will print:"
249     { $example
250     "USING: prettyprint continuations io ;"
251     "[ \"Hi\" print return \"Bye\" print ] with-return"
252     "Hi"
253 } } ;
254
255 { return with-return } related-words