]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel-docs.factor
f3c57776af4f71dcda51049fd75e5c322fb62d03
[factor.git] / core / kernel / kernel-docs.factor
1 USING: alien arrays classes combinators heaps help.markup help.syntax
2 kernel.private layouts math quotations sequences system threads words
3 ;
4 IN: kernel
5
6 HELP: OBJ-CURRENT-THREAD
7 { $description "Contains a reference to the running " { $link thread } " instance." } ;
8
9 HELP: JIT-PUSH-LITERAL
10 { $description "JIT code template for pushing literals unto the datastack." } ;
11
12 HELP: OBJ-SAMPLE-CALLSTACKS
13 { $description "A " { $link sequence } " that contains all call frames that is being captured during sampling profiling. See the " { $vocab-link "tools.profiler.sampling" } " vocab." } ;
14
15 HELP: OBJ-SLEEP-QUEUE
16 { $description "A " { $link min-heap } " containing sleeping threads." }
17 { $see-also sleep-queue } ;
18
19 HELP: OBJ-UNDEFINED
20 { $description "Default definition for undefined words" } ;
21
22 HELP: WIN-EXCEPTION-HANDLER
23 { $description "This special object is an " { $link alien } " containing a pointer to the processes global exception handler. Only applicable on " { $link windows } "." } ;
24
25 HELP: eq?
26 { $values { "obj1" object } { "obj2" object } { "?" boolean } }
27 { $description "Tests if two references point at the same object." } ;
28
29 HELP: drop  $shuffle ;
30 HELP: 2drop $shuffle ;
31 HELP: 3drop $shuffle ;
32 HELP: 4drop $shuffle ;
33 HELP: dup   $shuffle ;
34 HELP: 2dup  $shuffle ;
35 HELP: 3dup  $shuffle ;
36 HELP: 4dup  $shuffle ;
37 HELP: nip   $shuffle ;
38 HELP: 2nip  $shuffle ;
39 HELP: over  $shuffle ;
40 HELP: 2over $shuffle ;
41 HELP: pick  $shuffle ;
42 HELP: swap  $shuffle ;
43
44 HELP: rot   $complex-shuffle ;
45 HELP: -rot  $complex-shuffle ;
46 HELP: dupd  $complex-shuffle ;
47 HELP: swapd $complex-shuffle ;
48
49 HELP: callstack>array
50 { $values { "callstack" callstack } { "array" array } }
51 { $description "Converts the callstack to an array containing groups of three elements. The array is in reverse order so that the innermost frame comes first." } ;
52
53 HELP: get-datastack
54 { $values { "array" array } }
55 { $description "Outputs an array containing a copy of the data stack contents right before the call to this word, with the top of the stack at the end of the array." } ;
56
57 HELP: set-datastack
58 { $values { "array" array } }
59 { $description "Replaces the data stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
60
61 HELP: get-retainstack
62 { $values { "array" array } }
63 { $description "Outputs an array containing a copy of the retain stack contents right before the call to this word, with the top of the stack at the end of the array." } ;
64
65 HELP: set-retainstack
66 { $values { "array" array } }
67 { $description "Replaces the retain stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
68
69 HELP: get-callstack
70 { $values { "callstack" callstack } }
71 { $description "Outputs a copy of the call stack contents, with the top of the stack at the end of the vector. The stack frame of the caller word is " { $emphasis "not" } " included. Each group of three elements in the callstack is frame:"
72   { $list
73     "The first element is the executing word or quotation."
74     "The second element is the executing quotation."
75     "The third element is the offset in the executing quotation, or -1 if the offset can't be determined."
76   }
77 } ;
78
79 HELP: set-callstack
80 { $values { "callstack" callstack } }
81 { $description "Replaces the call stack contents. Control flow is transferred immediately to the innermost frame of the new call stack." } ;
82
83 HELP: clear
84 { $description "Clears the data stack." } ;
85
86 HELP: build
87 { $values { "n" integer } }
88 { $description "The current build number. Factor increments this number whenever a new boot image is created." } ;
89
90 HELP: leaf-signal-handler
91 { $description "A word called by the VM when a VM error occurs." } ;
92
93 HELP: hashcode*
94 { $values { "depth" integer } { "obj" object } { "code" fixnum } }
95 { $contract "Outputs the hashcode of an object. The hashcode operation must satisfy the following properties:"
96 { $list
97     { "If two objects are equal under " { $link = } ", they must have equal hashcodes." }
98     { "If the hashcode of an object depends on the values of its slots, the hashcode of the slots must be computed recursively by calling " { $link hashcode* } " with a " { $snippet "level" } " parameter decremented by one. This avoids excessive work while still computing well-distributed hashcodes. The " { $link recursive-hashcode } " combinator can help with implementing this logic," }
99     { "The hashcode should be a " { $link fixnum } ", however returning a " { $link bignum } " will not cause any problems other than potential performance degradation." }
100     { "The hashcode is only permitted to change between two invocations if the object or one of its slot values was mutated." }
101 }
102 "If mutable objects are used as hashtable keys, they must not be mutated in such a way that their hashcode changes. Doing so will violate bucket sorting invariants and result in undefined behavior. See " { $link "hashtables.keys" } " for details." } ;
103
104 HELP: hashcode
105 { $values { "obj" object } { "code" fixnum } }
106 { $description "Computes the hashcode of an object with a default hashing depth. See " { $link hashcode* } " for the hashcode contract." } ;
107
108 HELP: identity-hashcode
109 { $values { "obj" object } { "code" fixnum } }
110 { $description "Outputs the identity hashcode of an object. The identity hashcode is not guaranteed to be unique, however it will not change during the object's lifetime." } ;
111
112 { hashcode hashcode* identity-hashcode } related-words
113
114 HELP: =
115 { $values { "obj1" object } { "obj2" object } { "?" boolean } }
116 { $description
117     "Tests if two objects are equal. If " { $snippet "obj1" } " and " { $snippet "obj2" } " point to the same object, outputs " { $link t } ". Otherwise, calls the " { $link equal? } " generic word."
118 }
119 { $examples
120     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
121     { $example "USING: kernel prettyprint ;" "5 005 = ." "t" }
122     { $example "USING: kernel prettyprint ;" "5 5.0 = ." "f" }
123     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } \"a\" \"b\" 2array = ." "t" }
124     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } [ \"a\" \"b\" ] = ." "f" }
125 } ;
126
127 HELP: equal?
128 { $values { "obj1" object } { "obj2" object } { "?" boolean } }
129 { $contract
130     "Tests if two objects are equal."
131     $nl
132     "User code should call " { $link = } " instead; that word first tests the case where the objects are " { $link eq? } ", and so by extension, methods defined on " { $link equal? } " assume they are never called on " { $link eq? } " objects."
133     $nl
134     "Method definitions should ensure that this is an equality relation, modulo the assumption that the two objects are not " { $link eq? } ". That is, for any three non-" { $link eq? } " objects " { $snippet "a" } ", " { $snippet "b" } " and " { $snippet "c" } ", we must have:"
135     { $list
136         { { $snippet "a = b" } " implies " { $snippet "b = a" } }
137         { { $snippet "a = b" } " and " { $snippet "b = c" } " implies " { $snippet "a = c" } }
138     }
139     "If a class defines a custom equality comparison test, it should also define a compatible method for the " { $link hashcode* } " generic word."
140 }
141 { $examples
142     "An example demonstrating why this word should only be used to define methods on, and never called directly:"
143     { $example "USING: kernel prettyprint ;" "5 5 equal? ." "f" }
144     "Using " { $link = } " gives the expected behavior:"
145     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
146 } ;
147
148 HELP: identity-tuple
149 { $class-description "A class defining an " { $link equal? } " method which always returns f." }
150 { $examples
151     "To define a tuple class such that two instances are only equal if they are both the same instance, inherit from the " { $link identity-tuple } " class. This class defines a method on " { $link equal? } " which always returns " { $link f } ". Since " { $link = } " handles the case where the two objects are " { $link eq? } ", this method will never be called with two " { $link eq? } " objects, so such a definition is valid:"
152     { $code "TUPLE: foo < identity-tuple ;" }
153     "By calling " { $link = } " on instances of " { $snippet "foo" } " we get the results we expect:"
154     { $unchecked-example "T{ foo } dup = ." "t" }
155     { $unchecked-example "T{ foo } dup clone = ." "f" }
156 } ;
157
158 HELP: clone
159 { $values { "obj" object } { "cloned" "a new object" } }
160 { $contract "Outputs a new object equal to the given object. This is not guaranteed to actually copy the object; it does nothing with immutable objects, and does not copy words either. However, sequences and tuples can be cloned to obtain a shallow copy of the original." } ;
161
162 HELP: ?
163 { $values { "?" "a generalized boolean" } { "true" object } { "false" object } { "true/false" { { $snippet "true" } " or " { $snippet "false" } } } }
164 { $description "Chooses between two values depending on the boolean value of " { $snippet "cond" } "." } ;
165
166 HELP: boolean
167 { $class-description "A union of the " { $link POSTPONE: t } " and " { $link POSTPONE: f } " classes." } ;
168
169 HELP: >boolean
170 { $values { "obj" "a generalized boolean" } { "?" boolean } }
171 { $description "Convert a generalized boolean into a boolean. That is, " { $link f } " retains its value, whereas anything else becomes " { $link t } "." } ;
172
173 HELP: not
174 { $values { "obj" "a generalized boolean" } { "?" boolean } }
175 { $description "For " { $link f } " outputs " { $link t } " and for anything else outputs " { $link f } "." }
176 { $notes "This word implements boolean not, so applying it to integers will not yield useful results (all integers have a true value). Bitwise not is the " { $link bitnot } " word." } ;
177
178 HELP: and
179 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
180 { $description "If both inputs are true, outputs " { $snippet "obj2" } ". otherwise outputs " { $link f } "." }
181 { $notes "This word implements boolean and, so applying it to integers will not yield useful results (all integers have a true value). Bitwise and is the " { $link bitand } " word." }
182 { $examples
183     "Usually only the boolean value of the result is used, however you can also explicitly rely on the behavior that if both inputs are true, the second is output:"
184     { $example "USING: kernel prettyprint ;" "t f and ." "f" }
185     { $example "USING: kernel prettyprint ;" "t 7 and ." "7" }
186     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 and ." "12.0" }
187 } ;
188
189 HELP: or
190 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
191 { $description "If both inputs are false, outputs " { $link f } ". otherwise outputs the first of " { $snippet "obj1" } " and " { $snippet "obj2" } " which is true." }
192 { $notes "This word implements boolean inclusive or, so applying it to integers will not yield useful results (all integers have a true value). Bitwise inclusive or is the " { $link bitor } " word." }
193 { $examples
194     "Usually only the boolean value of the result is used, however you can also explicitly rely on the behavior that the result will be the first true input:"
195     { $example "USING: kernel prettyprint ;" "t f or ." "t" }
196     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 or ." "\"hi\"" }
197 } ;
198
199 HELP: xor
200 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
201 { $description "If exactly one input is false, outputs the other input. Otherwise outputs " { $link f } "." }
202 { $notes "This word implements boolean exclusive or, so applying it to integers will not yield useful results (all integers have a true value). Bitwise exclusive or is the " { $link bitxor } " word." } ;
203
204 HELP: both?
205 { $values { "x" object } { "y" object } { "quot" { $quotation ( ... obj -- ... ? ) } } { "?" boolean } }
206 { $description "Tests if the quotation yields a true value when applied to both " { $snippet "x" } " and " { $snippet "y" } "." }
207 { $examples
208     { $example "USING: kernel math prettyprint ;" "3 5 [ odd? ] both? ." "t" }
209     { $example "USING: kernel math prettyprint ;" "12 7 [ even? ] both? ." "f" }
210 } ;
211
212 HELP: either?
213 { $values { "x" object } { "y" object } { "quot" { $quotation ( ... obj -- ... ? ) } } { "?" boolean } }
214 { $description "Tests if the quotation yields a true value when applied to either " { $snippet "x" } " or " { $snippet "y" } "." }
215 { $examples
216     { $example "USING: kernel math prettyprint ;" "3 6 [ odd? ] either? ." "t" }
217     { $example "USING: kernel math prettyprint ;" "5 7 [ even? ] either? ." "f" }
218 } ;
219
220 HELP: same?
221 { $values { "x" object } { "y" object } { "quot" { $quotation ( ... obj -- ... obj' ) } } { "?" boolean } }
222 { $description "Applies the quotation to both " { $snippet "x" } " and " { $snippet "y" } ", and then checks if the results are equal." }
223 { $examples
224     { $example "USING: kernel math prettyprint ;" "4 5 [ 2/ ] same? ." "t" }
225     { $example "USING: kernel math prettyprint ;" "3 7 [ sq ] same? ." "f" }
226 } ;
227
228 HELP: execute
229 { $values { "word" word } }
230 { $description "Executes a word. Words which " { $link execute } " an input parameter must be declared " { $link POSTPONE: inline } " so that a caller which passes in a literal word can have a static stack effect." }
231 { $examples
232     { $example "USING: kernel io words ;" "IN: scratchpad" ": twice ( word -- ) dup execute execute ; inline\n: hello ( -- ) \"Hello\" print ;\n\\ hello twice" "Hello\nHello" }
233 } ;
234
235 { execute POSTPONE: execute( } related-words
236
237 HELP: (execute)
238 { $values { "word" word } }
239 { $description "Executes a word without checking if it is a word first." }
240 { $warning "This word is in the " { $vocab-link "kernel.private" } " vocabulary because it is unsafe. Calling with a parameter that is not a word will crash Factor. Use " { $link execute } " instead." } ;
241
242 HELP: call
243 { $values { "callable" callable } }
244 { $description "Calls a quotation. Words which " { $link call } " an input parameter must be declared " { $link POSTPONE: inline } " so that a caller which passes in a literal quotation can have a static stack effect." }
245 { $examples
246     "The following two lines are equivalent:"
247     { $code "2 [ 2 + 3 * ] call" "2 2 + 3 *" }
248 } ;
249
250 { call POSTPONE: call( } related-words
251
252 HELP: keep
253 { $values { "x" object } { "quot" { $quotation ( ..a x -- ..b ) } } }
254 { $description "Call a quotation with a value on the stack, restoring the value when the quotation returns." }
255 { $examples
256     { $example "USING: arrays kernel prettyprint ;" "2 \"greetings\" [ <array> ] keep 2array ." "{ { \"greetings\" \"greetings\" } \"greetings\" }" }
257 } ;
258
259 HELP: 2keep
260 { $values { "x" object } { "y" object } { "quot" { $quotation ( ..a x y -- ..b ) } } }
261 { $description "Call a quotation with two values on the stack, restoring the values when the quotation returns." } ;
262
263 HELP: 3keep
264 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( ..a x y z -- ..b ) } } }
265 { $description "Call a quotation with three values on the stack, restoring the values when the quotation returns." } ;
266
267 HELP: bi
268 { $values { "x" object } { "p" { $quotation ( ..a x -- ..b ) } } { "q" { $quotation ( ..c x -- ..d ) } } }
269 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } "." }
270 { $examples
271     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
272     { $code
273         "[ p ] [ q ] bi"
274         "dup p q"
275     }
276     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
277     { $code
278         "[ p ] [ q ] bi"
279         "dup p swap q"
280     }
281     "In general, the following two lines are equivalent:"
282     { $code
283         "[ p ] [ q ] bi"
284         "[ p ] keep q"
285     }
286
287 } ;
288
289 HELP: 2bi
290 { $values { "x" object } { "y" object } { "p" { $quotation ( x y -- ... ) } } { "q" { $quotation ( x y -- ... ) } } }
291 { $description "Applies " { $snippet "p" } " to the two input values, then applies " { $snippet "q" } " to the two input values." }
292 { $examples
293     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
294     { $code
295         "[ p ] [ q ] 2bi"
296         "2dup p q"
297     }
298     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- z )" } ", then the following two lines are equivalent:"
299     { $code
300         "[ p ] [ q ] 2bi"
301         "2dup p -rot q"
302     }
303     "In general, the following two lines are equivalent:"
304     { $code
305         "[ p ] [ q ] 2bi"
306         "[ p ] 2keep q"
307     }
308 } ;
309
310 HELP: 3bi
311 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation ( x y z -- ... ) } } { "q" { $quotation ( x y z -- ... ) } } }
312 { $description "Applies " { $snippet "p" } " to the three input values, then applies " { $snippet "q" } " to the three input values." }
313 { $examples
314     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
315     { $code
316         "[ p ] [ q ] 3bi"
317         "3dup p q"
318     }
319     "In general, the following two lines are equivalent:"
320     { $code
321         "[ p ] [ q ] 3bi"
322         "[ p ] 3keep q"
323     }
324 } ;
325
326 HELP: tri
327 { $values { "x" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( x -- ... ) } } { "r" { $quotation ( x -- ... ) } } }
328 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } ", and finally applies " { $snippet "r" } " to " { $snippet "x" } "." }
329 { $examples
330     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
331     { $code
332         "[ p ] [ q ] [ r ] tri"
333         "dup p dup q r"
334     }
335     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
336     { $code
337         "[ p ] [ q ] [ r ] tri"
338         "dup p over q rot r"
339     }
340     "In general, the following two lines are equivalent:"
341     { $code
342         "[ p ] [ q ] [ r ] tri"
343         "[ p ] keep [ q ] keep r"
344     }
345 } ;
346
347 HELP: 2tri
348 { $values { "x" object } { "y" object } { "p" { $quotation ( x y -- ... ) } } { "q" { $quotation ( x y -- ... ) } } { "r" { $quotation ( x y -- ... ) } } }
349 { $description "Applies " { $snippet "p" } " to the two input values, then applies " { $snippet "q" } " to the two input values, and finally applies " { $snippet "r" } " to the two input values." }
350 { $examples
351     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
352     { $code
353         "[ p ] [ q ] [ r ] 2tri"
354         "2dup p 2dup q r"
355     }
356     "In general, the following two lines are equivalent:"
357     { $code
358         "[ p ] [ q ] [ r ] 2tri"
359         "[ p ] 2keep [ q ] 2keep r"
360     }
361 } ;
362
363 HELP: 3tri
364 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation ( x y z -- ... ) } } { "q" { $quotation ( x y z -- ... ) } } { "r" { $quotation ( x y z -- ... ) } } }
365 { $description "Applies " { $snippet "p" } " to the three input values, then applies " { $snippet "q" } " to the three input values, and finally applies " { $snippet "r" } " to the three input values." }
366 { $examples
367     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
368     { $code
369         "[ p ] [ q ] [ r ] 3tri"
370         "3dup p 3dup q r"
371     }
372     "In general, the following two lines are equivalent:"
373     { $code
374         "[ p ] [ q ] [ r ] 3tri"
375         "[ p ] 3keep [ q ] 3keep r"
376     }
377 } ;
378
379
380 HELP: bi*
381 { $values { "x" object } { "y" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( y -- ... ) } } }
382 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } "." }
383 { $examples
384     "The following two lines are equivalent:"
385     { $code
386         "[ p ] [ q ] bi*"
387         "[ p ] dip q"
388     }
389 } ;
390
391 HELP: 2bi*
392 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "p" { $quotation ( w x -- ... ) } } { "q" { $quotation ( y z -- ... ) } } }
393 { $description "Applies " { $snippet "p" } " to " { $snippet "w" } " and " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "z" } "." }
394 { $examples
395     "The following two lines are equivalent:"
396     { $code
397         "[ p ] [ q ] 2bi*"
398         "[ p ] 2dip q"
399     }
400 } ;
401
402 HELP: 2tri*
403 { $values { "u" object } { "v" object } { "w" object } { "x" object } { "y" object } { "z" object } { "p" { $quotation ( u v -- ... ) } } { "q" { $quotation ( w x -- ... ) } } { "r" { $quotation ( y z -- ... ) } } }
404 { $description "Applies " { $snippet "p" } " to " { $snippet "u" } " and " { $snippet "v" } ", then applies " { $snippet "q" } " to " { $snippet "w" } " and " { $snippet "x" } ", and finally applies " { $snippet "r" } " to " { $snippet "y" } " and " { $snippet "z" } "." }
405 { $examples
406     "The following two lines are equivalent:"
407     { $code
408         "[ p ] [ q ] [ r ] 2tri*"
409         "[ [ p ] 2dip q ] 2dip r"
410     }
411 } ;
412
413 HELP: tri*
414 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( y -- ... ) } } { "r" { $quotation ( z -- ... ) } } }
415 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } ", and finally applies " { $snippet "r" } " to " { $snippet "z" } "." }
416 { $examples
417     "The following two lines are equivalent:"
418     { $code
419         "[ p ] [ q ] [ r ] tri*"
420         "[ [ p ] dip q ] dip r"
421     }
422 } ;
423
424 HELP: bi@
425 { $values { "x" object } { "y" object } { "quot" { $quotation ( obj -- ... ) } } }
426 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } "." }
427 { $examples
428     "The following two lines are equivalent:"
429     { $code
430         "[ p ] bi@"
431         "[ p ] dip p"
432     }
433     "The following two lines are also equivalent:"
434     { $code
435         "[ p ] bi@"
436         "[ p ] [ p ] bi*"
437     }
438 } ;
439
440 HELP: 2bi@
441 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( obj1 obj2 -- ... ) } } }
442 { $description "Applies the quotation to " { $snippet "w" } " and " { $snippet "x" } ", then to " { $snippet "y" } " and " { $snippet "z" } "." }
443 { $examples
444     "The following two lines are equivalent:"
445     { $code
446         "[ p ] 2bi@"
447         "[ p ] 2dip p"
448     }
449     "The following two lines are also equivalent:"
450     { $code
451         "[ p ] 2bi@"
452         "[ p ] [ p ] 2bi*"
453     }
454 } ;
455
456 HELP: tri@
457 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( obj -- ... ) } } }
458 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } ", and finally to " { $snippet "z" } "." }
459 { $examples
460     "The following two lines are equivalent:"
461     { $code
462         "[ p ] tri@"
463         "[ [ p ] dip p ] dip p"
464     }
465     "The following two lines are also equivalent:"
466     { $code
467         "[ p ] tri@"
468         "[ p ] [ p ] [ p ] tri*"
469     }
470 } ;
471
472 HELP: 2tri@
473 { $values { "u" object } { "v" object } { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation ( obj1 obj2 -- ... ) } } }
474 { $description "Applies the quotation to " { $snippet "u" } " and " { $snippet "v" } ", then to " { $snippet "w" } " and " { $snippet "x" } ", and then to " { $snippet "y" } " and " { $snippet "z" } "." }
475 { $examples
476     "The following two lines are equivalent:"
477     { $code
478         "[ p ] 2tri@"
479         "[ [ p ] 2dip p ] 2dip p"
480     }
481     "The following two lines are also equivalent:"
482     { $code
483         "[ p ] 2tri@"
484         "[ p ] [ p ] [ p ] 2tri*"
485     }
486 } ;
487
488 HELP: bi-curry
489 { $values { "x" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( x -- ... ) } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ x q ]" } } }
490 { $description "Partially applies " { $snippet "p" } " and " { $snippet "q" } " to " { $snippet "x" } "." }
491 { $notes
492   "The following two lines are equivalent:"
493   { $code
494     "[ p ] [ q ] bi-curry [ call ] bi@"
495     "[ p ] [ q ] bi"
496   }
497   "Higher-arity variants of " { $link bi } " can be built from " { $link bi-curry } ":"
498   { $code
499     "[ p ] [ q ] bi-curry bi == [ p ] [ q ] 2bi"
500     "[ p ] [ q ] bi-curry bi-curry bi == [ p ] [ q ] 3bi"
501   }
502   "The combination " { $snippet "bi-curry bi*" } " cannot be expressed with the non-currying dataflow combinators alone; it is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
503   { $code
504     "[ p ] [ q ] bi-curry bi*"
505     "[ swap ] keep [ p ] [ q ] 2bi*"
506   }
507   "To put it another way, " { $snippet "bi-curry bi*" } " handles the case where you have three values " { $snippet "a b c" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a c" } " and " { $snippet "q" } " to " { $snippet "b c" } "."
508 } ;
509
510 HELP: tri-curry
511 { $values
512   { "x" object }
513   { "p" { $quotation ( x -- ... ) } }
514   { "q" { $quotation ( x -- ... ) } }
515   { "r" { $quotation ( x -- ... ) } }
516   { "p'" { $snippet "[ x p ]" } }
517   { "q'" { $snippet "[ x q ]" } }
518   { "r'" { $snippet "[ x r ]" } }
519 }
520 { $description "Partially applies " { $snippet "p" } ", " { $snippet "q" } " and " { $snippet "r" } " to " { $snippet "x" } "." }
521 { $notes
522   "The following two lines are equivalent:"
523   { $code
524     "[ p ] [ q ] [ r ] tri-curry [ call ] tri@"
525     "[ p ] [ q ] [ r ] tri"
526   }
527   "Higher-arity variants of " { $link tri } " can be built from " { $link tri-curry } ":"
528   { $code
529     "[ p ] [ q ] [ r ] tri-curry tri == [ p ] [ q ] [ r ] 2tri"
530     "[ p ] [ q ] [ r ] tri-curry tri-curry bi == [ p ] [ q ] [ r ] 3tri"
531   }
532   "The combination " { $snippet "tri-curry tri*" } " cannot be expressed with the non-currying dataflow combinators alone; it handles the case where you have four values " { $snippet "a b c d" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a d" } ", " { $snippet "q" } " to " { $snippet "b d" } " and " { $snippet "r" } " to " { $snippet "c d" } "." } ;
533
534 HELP: bi-curry*
535 { $values { "x" object } { "y" object } { "p" { $quotation ( x -- ... ) } } { "q" { $quotation ( y -- ... ) } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ y q ]" } } }
536 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", and " { $snippet "q" } " to " { $snippet "y" } "." }
537 { $notes
538   "The following two lines are equivalent:"
539   { $code
540     "[ p ] [ q ] bi-curry* [ call ] bi@"
541     "[ p ] [ q ] bi*"
542   }
543   "The combination " { $snippet "bi-curry* bi" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
544   { $code
545     "[ p ] [ q ] bi-curry* bi"
546     "[ over ] dip [ p ] [ q ] 2bi*"
547   }
548   "In other words, " { $snippet "bi-curry* bi" } " handles the case where you have the three values " { $snippet "a b c" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a b" } " and " { $snippet "q" } " to " { $snippet "a c" } "."
549   $nl
550   "The combination " { $snippet "bi-curry* bi*" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
551   { $code
552     "[ p ] [ q ] bi-curry* bi*"
553     "[ swap ] dip [ p ] [ q ] 2bi*"
554   }
555   "In other words, " { $snippet "bi-curry* bi*" } " handles the case where you have the four values " { $snippet "a b c d" } " on the stack, and you wish to apply " { $snippet "p" } " to " { $snippet "a c" } " and " { $snippet "q" } " to " { $snippet "b d" } "."
556
557 } ;
558
559 HELP: tri-curry*
560 { $values
561   { "x" object }
562   { "y" object }
563   { "z" object }
564   { "p" { $quotation ( x -- ... ) } }
565   { "q" { $quotation ( y -- ... ) } }
566   { "r" { $quotation ( z -- ... ) } }
567   { "p'" { $snippet "[ x p ]" } }
568   { "q'" { $snippet "[ y q ]" } }
569   { "r'" { $snippet "[ z r ]" } }
570 }
571 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "r" } " to " { $snippet "z" } "." }
572 { $notes
573   "The following two lines are equivalent:"
574   { $code
575     "[ p ] [ q ] [ r ] tri-curry* [ call ] tri@"
576     "[ p ] [ q ] [ r ] tri*"
577   }
578   "The combination " { $snippet "tri-curry* tri" } " is equivalent to a stack shuffle preceding " { $link 2tri* } ":"
579   { $code
580     "[ p ] [ q ] [ r ] tri-curry* tri"
581     "[ [ over ] dip over ] dip [ p ] [ q ] [ r ] 2tri*"
582   }
583 } ;
584
585 HELP: bi-curry@
586 { $values { "x" object } { "y" object } { "q" { $quotation ( obj -- ... ) } } { "p'" { $snippet "[ x q ]" } } { "q'" { $snippet "[ y q ]" } } }
587 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } " and " { $snippet "y" } "." }
588 { $notes
589   "The following two lines are equivalent:"
590   { $code
591     "[ q ] bi-curry@"
592     "[ q ] [ q ] bi-curry*"
593   }
594 } ;
595
596 HELP: tri-curry@
597 { $values
598   { "x" object }
599   { "y" object }
600   { "z" object }
601   { "q" { $quotation ( obj -- ... ) } }
602   { "p'" { $snippet "[ x q ]" } }
603   { "q'" { $snippet "[ y q ]" } }
604   { "r'" { $snippet "[ z q ]" } }
605 }
606 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } "." }
607 { $notes
608   "The following two lines are equivalent:"
609   { $code
610     "[ q ] tri-curry@"
611     "[ q ] [ q ] [ q ] tri-curry*"
612   }
613 } ;
614
615 HELP: if
616 { $values { "?" "a generalized boolean" } { "true" quotation } { "false" quotation } }
617 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation. Otherwise calls the " { $snippet "true" } " quotation."
618 $nl
619 "The " { $snippet "cond" } " value is removed from the stack before either quotation is called." }
620 { $examples
621     { $example
622         "USING: io kernel math ;"
623         "10 3 < [ \"Math is broken\" print ] [ \"Math is good\" print ] if"
624         "Math is good"
625     }
626 }
627 { $notes { $snippet "if" } " is executed as a primitive when preceded by two literal quotations. The below definition is not executed unless one of its arguments is a non-literal quotation, such as a quotation constructed with " { $link curry } " or " { $link compose } ", or for " { $link "fry" } " or quotations including " { $link "locals" } "." } ;
628
629 HELP: when
630 { $values { "?" "a generalized boolean" } { "true" quotation } }
631 { $description "If " { $snippet "cond" } " is not " { $link f } ", calls the " { $snippet "true" } " quotation."
632 $nl
633 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." }
634 { $examples
635     { $example
636         "USING: kernel math prettyprint ;"
637         "-5 dup 0 < [ 3 + ] when ."
638         "-2"
639     }
640 } ;
641
642 HELP: unless
643 { $values { "?" "a generalized boolean" } { "false" quotation } }
644 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation."
645 $nl
646 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." }
647 { $examples
648     { $example
649         "USING: kernel math prettyprint sequences ;"
650         "IN: scratchpad"
651         ""
652         "CONSTANT: american-cities {"
653         "    \"San Francisco\""
654         "    \"Los Angeles\""
655         "    \"New York\""
656         "}"
657         ""
658         ": add-tax ( price city -- price' )"
659         "    american-cities member? [ 1.1 * ] unless ;"
660         ""
661         "123 \"Ottawa\" add-tax ."
662         "135.3"
663     }
664 } ;
665
666 HELP: if*
667 { $values { "?" "a generalized boolean" } { "true" { $quotation ( ..a ? -- ..b ) } } { "false" { $quotation ( ..a -- ..b ) } } }
668 { $description "Alternative conditional form that preserves the " { $snippet "cond" } " value if it is true."
669 $nl
670 "If the condition is true, it is retained on the stack before the " { $snippet "true" } " quotation is called. Otherwise, the condition is removed from the stack and the " { $snippet "false" } " quotation is called."
671 $nl
672 "The following two lines are equivalent:"
673 { $code "X [ Y ] [ Z ] if*" "X dup [ Y ] [ drop Z ] if" } }
674 { $examples
675     "Notice how in this example, the same value is tested by the conditional, and then used in the true branch; the false branch does not need to drop the value because of how " { $link if* } " works:"
676     { $example
677         "USING: assocs io kernel math.parser ;"
678         "IN: scratchpad"
679         ""
680         ": curry-price ( meat -- price )
681     {
682         { \"Beef\" 10 }
683         { \"Chicken\" 12 }
684         { \"Lamb\" 13 }
685     } at ;
686
687 : order-curry ( meat -- )
688     curry-price [
689         \"Your order will be \" write
690         number>string write
691         \" dollars.\" write
692     ] [ \"Invalid order.\" print ] if* ;"
693         ""
694         "\"Deer\" order-curry"
695         "Invalid order."
696     }
697 } ;
698
699 HELP: when*
700 { $values { "?" "a generalized boolean" } { "true" { $quotation ( ..a ? -- ..a ) } } }
701 { $description "Variant of " { $link if* } " with no false quotation."
702 $nl
703 "The following two lines are equivalent:"
704 { $code "X [ Y ] when*" "X dup [ Y ] [ drop ] if" } } ;
705
706 HELP: unless*
707 { $values { "?" "a generalized boolean" } { "false" { $quotation ( ..a -- ..a x ) } } { "x" object } }
708 { $description "Variant of " { $link if* } " with no true quotation." }
709 { $notes
710 "The following two lines are equivalent:"
711 { $code "X [ Y ] unless*" "X dup [ ] [ drop Y ] if" } } ;
712
713 HELP: ?if
714 { $values { "default" object } { "cond" "a generalized boolean" } { "true" { $quotation ( ..a cond -- ..b ) } } { "false" { $quotation ( ..a default -- ..b ) } } }
715 { $description "If the condition is " { $link f } ", the " { $snippet "false" } " quotation is called with the " { $snippet "default" } " value on the stack. Otherwise, the " { $snippet "true" } " quotation is called with the condition on the stack." }
716 { $notes
717 "The following two lines are equivalent:"
718 { $code "[ X ] [ Y ] ?if" "dup [ nip X ] [ drop Y ] if" }
719 "The following two lines are equivalent:"
720 { $code "[ ] [ ] ?if" "swap or" } } ;
721
722 HELP: die
723 { $description "Starts the front-end processor (FEP), which is a low-level debugger which can inspect memory addresses and the like. The FEP is also entered when a critical error occurs." }
724 { $notes
725     "The term FEP originates from the Lisp machines of old. According to the Jargon File,"
726     $nl
727     { $strong "fepped out" } " /fept owt/ " { $emphasis "adj." } " The Symbolics 3600 LISP Machine has a Front-End Processor called a `FEP' (compare sense 2 of box). When the main processor gets wedged, the FEP takes control of the keyboard and screen. Such a machine is said to have `fepped out' or `dropped into the fep'."
728     $nl
729     { $url "http://www.jargon.net/jargonfile/f/feppedout.html" }
730 } ;
731
732 HELP: (clone)
733 { $values { "obj" object } { "newobj" "a shallow copy" } }
734 { $description "Outputs a byte-by-byte copy of the given object. User code should call " { $link clone } " instead." } ;
735
736 HELP: declare
737 { $values { "spec" "an array of class words" } }
738 { $description "Declares that the elements at the top of the stack are instances of the classes in " { $snippet "spec" } "." }
739 { $warning "The compiler blindly trusts declarations, and false declarations can lead to crashes, memory corruption and other undesirable behavior." }
740 { $examples
741     "The optimizer cannot do anything with the below code:"
742     { $code "2 + 10 *" }
743     "However, if we declare that the top of the stack is a " { $link float } ", then type checks and generic dispatch are eliminated, and the compiler can use unsafe intrinsics:"
744     { $code "{ float } declare 2 + 10 *" }
745 } ;
746
747 HELP: tag
748 { $values { "object" object } { "n" "a tag number" } }
749 { $description "Outputs an object's tag number, between zero and one less than " { $link num-types } ". This is implementation detail and user code should call " { $link class-of } " instead." } ;
750
751 HELP: special-object
752 { $values { "n" "a non-negative integer" } { "obj" object } }
753 { $description "Reads an object from the Factor VM's special object table. User code never has to read the special object table directly; instead, use one of the callers of this word." } ;
754
755 HELP: set-special-object
756 { $values { "obj" object } { "n" "a non-negative integer" } }
757 { $description "Writes an object to the Factor VM's special object table. User code never has to write to the special object table directly; instead, use one of the callers of this word." } ;
758
759 HELP: object
760 { $class-description
761     "The class of all objects. If a generic word defines a method specializing on this class, the method is used as a fallback, if no other applicable method is found. For instance:"
762     { $code "GENERIC: enclose ( number -- array )" "M: number enclose 1array ;" "M: object enclose ;" }
763 } ;
764
765 HELP: null
766 { $class-description
767     "The canonical empty class with no instances."
768 }
769 { $notes
770     "Unlike " { $snippet "null" } " in Java or " { $snippet "NULL" } " in C++, this is not a value signifying empty, or nothing. Use " { $link f } " for this purpose."
771 } ;
772
773 HELP: most
774 { $values { "x" object } { "y" object } { "quot" { $quotation ( x y -- ? ) } } { "z" "either " { $snippet "x" } " or " { $snippet "y" } } }
775 { $description "If the quotation yields a true value when applied to " { $snippet "x" } " and " { $snippet "y" } ", outputs " { $snippet "x" } ", otherwise outputs " { $snippet "y" } "." } ;
776
777 HELP: curry
778 { $values { "obj" object } { "quot" callable } { "curry" curry } }
779 { $description "Partial application. Outputs a " { $link callable } " which first pushes " { $snippet "obj" } " and then calls " { $snippet "quot" } "." }
780 { $class-description "The class of objects created by " { $link curry } ". These objects print identically to quotations and implement the sequence protocol, however they only use two cells of storage; a reference to the object and a reference to the underlying quotation." }
781 { $notes "Even if " { $snippet "obj" } " is a word, it will be pushed as a literal."
782 $nl
783 "This operation is efficient and does not copy the quotation." }
784 { $examples
785     { $example "USING: kernel prettyprint ;" "5 [ . ] curry ." "[ 5 . ]" }
786     { $example "USING: kernel prettyprint see ;" "\\ = [ see ] curry ." "[ \\ = see ]" }
787     { $example "USING: kernel math prettyprint sequences ;" "{ 1 2 3 } 2 [ - ] curry map ." "{ -1 0 1 }" }
788 } ;
789
790 HELP: 2curry
791 { $values { "obj1" object } { "obj2" object } { "quot" callable } { "curry" curry } }
792 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } " and " { $snippet "obj2" } " and then calls " { $snippet "quot" } "." }
793 { $notes "This operation is efficient and does not copy the quotation." }
794 { $examples
795     { $example "USING: kernel math prettyprint ;" "5 4 [ + ] 2curry ." "[ 5 4 + ]" }
796 } ;
797
798 HELP: 3curry
799 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "quot" callable } { "curry" curry } }
800 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } ", " { $snippet "obj2" } " and " { $snippet "obj3" } ", and then calls " { $snippet "quot" } "." }
801 { $notes "This operation is efficient and does not copy the quotation." } ;
802
803 HELP: with
804 { $values { "param" object } { "obj" object } { "quot" { $quotation ( param elt -- ... ) } } { "curry" curry } }
805 { $description "Partial application on the left. The following two lines are equivalent:"
806     { $code "swap [ swap A ] curry B" }
807     { $code "[ A ] with B" }
808
809 }
810 { $notes "This operation is efficient and does not copy the quotation." }
811 { $examples
812     { $example "USING: kernel math prettyprint sequences ;" "1 { 1 2 3 } [ / ] with map ." "{ 1 1/2 1/3 }" }
813     { $example "USING: kernel math prettyprint sequences ;" "1000 100 5 iota [ sq + + ] 2with map ." "{ 1100 1101 1104 1109 1116 }" }
814 } ;
815
816 HELP: 2with
817 { $values
818   { "param1" object }
819   { "param2" object }
820   { "obj" object }
821   { "quot" { $quotation ( param1 param2 elt -- ... ) } }
822   { "curry" curry }
823 }
824 { $description "Partial application on the left of two parameters." } ;
825
826 HELP: compose
827 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
828 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot1" } " followed by " { $snippet "quot2" } "." }
829 { $notes
830     "The following two lines are equivalent:"
831     { $code
832         "compose call"
833         "append call"
834     }
835     "However, " { $link compose } " runs in constant time, and the optimizing compiler is able to compile code which calls composed quotations."
836 } ;
837
838
839 HELP: prepose
840 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
841 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot2" } " followed by " { $snippet "quot1" } "." }
842 { $notes "See " { $link compose } " for details." } ;
843
844 { compose prepose } related-words
845
846 HELP: dip
847 { $values { "x" object } { "quot" quotation } }
848 { $description "Removes " { $snippet "x" } " from the datastack, calls " { $snippet "quot" } ", and restores " { $snippet "x" } " to the top of the datastack when " { $snippet "quot" } " is finished." }
849 { $examples
850     { $example "USING: arrays kernel math prettyprint ;" "10 20 30 [ / ] dip 2array ." "{ 1/2 30 }" }
851 }
852 { $notes { $snippet "dip" } " is executed as a primitive when preceded by a literal quotation. The below definition is not executed unless its argument is a non-literal quotation, such as a quotation constructed with " { $link curry } " or " { $link compose } ", or for " { $link "fry" } " or quotations including " { $link "locals" } "." } ;
853
854 HELP: 2dip
855 { $values { "x" object } { "y" object } { "quot" quotation } }
856 { $description "Removes " { $snippet "x" } " and " { $snippet "y" } " from the datastack, calls " { $snippet "quot" } ", and restores the removed objects to the top of the datastack when " { $snippet "quot" } " is finished." }
857 { $notes "The following are equivalent:"
858     { $code "[ [ foo bar ] dip ] dip" }
859     { $code "[ foo bar ] 2dip" }
860 } ;
861
862 HELP: 3dip
863 { $values { "x" object } { "y" object } { "z" object } { "quot" quotation } }
864 { $description "Removes " { $snippet "x" } ", " { $snippet "y" } ", and " { $snippet "z" } " from the datastack, calls " { $snippet "quot" } ", and restores the removed objects to the top of the datastack when " { $snippet "quot" } " is finished." }
865 { $notes "The following are equivalent:"
866     { $code "[ [ [ foo bar ] dip ] dip ] dip" }
867     { $code "[ foo bar ] 3dip" }
868 } ;
869
870 HELP: 4dip
871 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" quotation } }
872 { $description "Removes " { $snippet "w" } ", " { $snippet "x" } ", " { $snippet "y" } ", and " { $snippet "z" } " from the datastack, calls " { $snippet "quot" } ", and restores the removed objects to the top of the datastack when " { $snippet "quot" } " is finished." }
873 { $notes "The following are equivalent:"
874     { $code "[ [ [ [ foo bar ] dip ] dip ] dip ] dip" }
875     { $code "[ foo bar ] 4dip" }
876 } ;
877
878 HELP: while
879 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "body" { $quotation ( ..b -- ..a ) } } }
880 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link f } "." } ;
881
882 HELP: until
883 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "body" { $quotation ( ..b -- ..a ) } } }
884 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link t } "." } ;
885
886 HELP: do
887 { $values { "pred" { $quotation ( ..a -- ..b ? ) } } { "body" { $quotation ( ..b -- ..a ) } } }
888 { $description "Executes one iteration of a " { $link while } " or " { $link until } " loop." } ;
889
890 HELP: loop
891 { $values
892      { "pred" quotation } }
893      { $description "Calls the quotation repeatedly until it outputs " { $link f } "." }
894 { $examples "Loop until we hit a zero:"
895     { $unchecked-example "USING: kernel random math io ; "
896     " [ \"hi\" write bl 10 random zero? not ] loop"
897     "hi hi hi" }
898     "A fun loop:"
899     { $example "USING: kernel prettyprint math ; "
900     "3 [ dup . 7 + 11 mod dup 3 = not ] loop drop"
901     "3\n10\n6\n2\n9\n5\n1\n8\n4\n0\n7" }
902 } ;
903
904 ARTICLE: "looping-combinators" "Looping combinators"
905 "In most cases, loops should be written using high-level combinators (such as " { $link "sequences-combinators" } ") or tail recursion. However, sometimes, the best way to express intent is with a loop."
906 { $subsections
907     while
908     until
909 }
910 "To execute one iteration of a loop, use the following word:"
911 { $subsections do }
912 "This word is intended as a modifier. The normal " { $link while } " loop never executes the body if the predicate returns false on the first iteration. To ensure the body executes at least once, use " { $link do } ":"
913 { $code
914     "[ P ] [ Q ] do while"
915 }
916 "A simpler looping combinator which executes a single quotation until it returns " { $link f } ":"
917 { $subsections loop } ;
918
919 HELP: assert
920 { $values { "got" "the obtained value" } { "expect" "the expected value" } }
921 { $description "Throws an " { $link assert } " error." }
922 { $error-description "Thrown when a unit test or other assertion fails." } ;
923
924 HELP: assert=
925 { $values { "a" object } { "b" object } }
926 { $description "Throws an " { $link assert } " error if " { $snippet "a" } " does not equal " { $snippet "b" } "." } ;
927
928 HELP: become
929 { $values { "old" array } { "new" array } }
930 { $description "Replaces all references to objects in " { $snippet "old" } " with the corresponding object in " { $snippet "new" } ". This word is used to implement tuple reshaping. See " { $link "tuple-redefinition" } "." } ;
931
932 ARTICLE: "shuffle-words-complex" "Complex shuffle words"
933 "These shuffle words tend to make code difficult to read and to reason about. Code that uses them should almost always be rewritten using " { $link "locals" } " or " { $link "dataflow-combinators" } "."
934 $nl
935 "Duplicating stack elements deep in the stack:"
936 { $subsections
937     dupd
938 }
939 "Permuting stack elements deep in the stack:"
940 { $subsections
941     swapd
942     rot
943     -rot
944 } ;
945
946 ARTICLE: "shuffle-words" "Shuffle words"
947 "Shuffle words rearrange items at the top of the data stack as indicated by their stack effects. They provide simple data flow control between words. More complex data flow control is available with the " { $link "dataflow-combinators" } " and with " { $link "locals" } "."
948 $nl
949 "Removing stack elements:"
950 { $subsections
951     drop
952     2drop
953     3drop
954     nip
955     2nip
956 }
957 "Duplicating stack elements:"
958 { $subsections
959     dup
960     2dup
961     3dup
962     over
963     2over
964     pick
965 }
966 "Permuting stack elements:"
967 { $subsections
968     swap
969 }
970 "There are additional, more complex stack shuffling words whose use is not recommended."
971 { $subsections
972     "shuffle-words-complex"
973 } ;
974
975 ARTICLE: "equality" "Equality"
976 "There are two distinct notions of “sameness” when it comes to objects."
977 $nl
978 "You can test if two references point to the same object (" { $emphasis "identity comparison" } "). This is rarely used; it is mostly useful with large, mutable objects where the object identity matters but the value is transient:"
979 { $subsections eq? }
980 "You can test if two objects are equal in a domain-specific sense, usually by being instances of the same class, and having equal slot values (" { $emphasis "value comparison" } "):"
981 { $subsections = }
982 "A third form of equality is provided by " { $link number= } ". It compares numeric value while disregarding types."
983 $nl
984 "Custom value comparison methods for use with " { $link = } " can be defined on a generic word:"
985 { $subsections equal? }
986 "Utility class:"
987 { $subsections identity-tuple }
988 "An object can be cloned; the clone has distinct identity but equal value:"
989 { $subsections clone } ;
990
991 ARTICLE: "assertions" "Assertions"
992 "Some words to make assertions easier to enforce:"
993 { $subsections
994     assert
995     assert=
996 } ;