]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel-docs.factor
deprimitivize tuck and put it to pasture
[factor.git] / core / kernel / kernel-docs.factor
1 USING: generic help.markup help.syntax math memory
2 namespaces sequences kernel.private layouts classes
3 vectors combinators quotations strings words
4 assocs arrays math.order ;
5 IN: kernel
6
7 HELP: eq? ( obj1 obj2 -- ? )
8 { $values { "obj1" object } { "obj2" object } { "?" "a boolean" } }
9 { $description "Tests if two references point at the same object." } ;
10
11 HELP: drop  ( x -- )                 $shuffle ;
12 HELP: 2drop ( x y -- )               $shuffle ;
13 HELP: 3drop ( x y z -- )             $shuffle ;
14 HELP: dup   ( x -- x x )             $shuffle ;
15 HELP: 2dup  ( x y -- x y x y )       $shuffle ;
16 HELP: 3dup  ( x y z -- x y z x y z ) $shuffle ;
17 HELP: nip   ( x y -- y )             $shuffle ;
18 HELP: 2nip  ( x y z -- z )           $shuffle ;
19 HELP: over  ( x y -- x y x )         $shuffle ;
20 HELP: 2over                          $shuffle ;
21 HELP: pick  ( x y z -- x y z x )     $shuffle ;
22 HELP: swap  ( x y -- y x )           $shuffle ;
23
24 HELP: rot   ( x y z -- y z x ) $complex-shuffle ;
25 HELP: -rot  ( x y z -- z x y ) $complex-shuffle ;
26 HELP: dupd  ( x y -- x x y )   $complex-shuffle ;
27 HELP: swapd ( x y z -- y x z ) $complex-shuffle ;
28
29 HELP: datastack ( -- ds )
30 { $values { "ds" array } }
31 { $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." } ;
32
33 HELP: set-datastack ( ds -- )
34 { $values { "ds" array } }
35 { $description "Replaces the data stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
36
37 HELP: retainstack ( -- rs )
38 { $values { "rs" array } }
39 { $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." } ;
40
41 HELP: set-retainstack ( rs -- )
42 { $values { "rs" array } }
43 { $description "Replaces the retain stack contents with a copy of an array. The end of the array becomes the top of the stack." } ;
44
45 HELP: callstack ( -- cs )
46 { $values { "cs" callstack } }
47 { $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." } ;
48
49 HELP: set-callstack ( cs -- )
50 { $values { "cs" callstack } }
51 { $description "Replaces the call stack contents. The end of the vector becomes the top of the stack. Control flow is transferred immediately to the new call stack." } ;
52
53 HELP: clear
54 { $description "Clears the data stack." } ;
55
56 HELP: build
57 { $values { "n" integer } }
58 { $description "The current build number. Factor increments this number whenever a new boot image is created." } ;
59
60 HELP: hashcode*
61 { $values { "depth" integer } { "obj" object } { "code" fixnum } }
62 { $contract "Outputs the hashcode of an object. The hashcode operation must satisfy the following properties:"
63 { $list
64     { "If two objects are equal under " { $link = } ", they must have equal hashcodes." }
65     { "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," }
66     { "The hashcode should be a " { $link fixnum } ", however returning a " { $link bignum } " will not cause any problems other than potential performance degradation." }
67     { "The hashcode is only permitted to change between two invocations if the object or one of its slot values was mutated." }
68 }
69 "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." } ;
70
71 HELP: hashcode
72 { $values { "obj" object } { "code" fixnum } }
73 { $description "Computes the hashcode of an object with a default hashing depth. See " { $link hashcode* } " for the hashcode contract." } ;
74
75 { hashcode hashcode* } related-words
76
77 HELP: =
78 { $values { "obj1" object } { "obj2" object } { "?" "a boolean" } }
79 { $description
80     "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."
81 }
82 { $examples
83     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
84     { $example "USING: kernel prettyprint ;" "5 005 = ." "t" }
85     { $example "USING: kernel prettyprint ;" "5 5.0 = ." "f" }
86     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } \"a\" \"b\" 2array = ." "t" }
87     { $example "USING: arrays kernel prettyprint ;" "{ \"a\" \"b\" } [ \"a\" \"b\" ] = ." "f" }
88 } ;
89
90 HELP: equal?
91 { $values { "obj1" object } { "obj2" object } { "?" "a boolean" } }
92 { $contract
93     "Tests if two objects are equal."
94     $nl
95     "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."
96     $nl
97     "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:"
98     { $list
99         { { $snippet "a = b" } " implies " { $snippet "b = a" } }
100         { { $snippet "a = b" } " and " { $snippet "b = c" } " implies " { $snippet "a = c" } }
101     }
102     "If a class defines a custom equality comparison test, it should also define a compatible method for the " { $link hashcode* } " generic word."
103 }
104 { $examples
105     "An example demonstrating why this word should only be used to define methods on, and never called directly:"
106     { $example "USING: kernel prettyprint ;" "5 5 equal? ." "f" }
107     "Using " { $link = } " gives the expected behavior:"
108     { $example "USING: kernel prettyprint ;" "5 5 = ." "t" }
109 } ;
110
111 HELP: identity-tuple
112 { $class-description "A class defining an " { $link equal? } " method which always returns f." }
113 { $examples
114     "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:"
115     { $code "TUPLE: foo < identity-tuple ;" }
116     "By calling " { $link = } " on instances of " { $snippet "foo" } " we get the results we expect:"
117     { $unchecked-example "T{ foo } dup = ." "t" }
118     { $unchecked-example "T{ foo } dup clone = ." "f" }
119 } ;
120
121 HELP: clone
122 { $values { "obj" object } { "cloned" "a new object" } }
123 { $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." } ;
124
125 HELP: ?
126 { $values { "?" "a generalized boolean" } { "true" object } { "false" object } { "true/false" "one two input objects" } }
127 { $description "Chooses between two values depending on the boolean value of " { $snippet "cond" } "." } ;
128
129 HELP: boolean
130 { $class-description "A union of the " { $link POSTPONE: t } " and " { $link POSTPONE: f } " classes." } ;
131
132 HELP: >boolean
133 { $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
134 { $description "Convert a generalized boolean into a boolean. That is, " { $link f } " retains its value, whereas anything else becomes " { $link t } "." } ;
135
136 HELP: not
137 { $values { "obj" "a generalized boolean" } { "?" "a boolean" } }
138 { $description "For " { $link f } " outputs " { $link t } " and for anything else outputs " { $link f } "." }
139 { $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." } ;
140
141 HELP: and
142 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
143 { $description "If both inputs are true, outputs " { $snippet "obj2" } ". otherwise outputs " { $link f } "." }
144 { $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." }
145 { $examples
146     "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:"
147     { $example "USING: kernel prettyprint ;" "t f and ." "f" }
148     { $example "USING: kernel prettyprint ;" "t 7 and ." "7" }
149     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 and ." "12.0" }
150 } ;
151
152 HELP: or
153 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
154 { $description "If both inputs are false, outputs " { $link f } ". otherwise outputs the first of " { $snippet "obj1" } " and " { $snippet "obj2" } " which is true." }
155 { $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." }
156 { $examples
157     "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:"
158     { $example "USING: kernel prettyprint ;" "t f or ." "t" }
159     { $example "USING: kernel prettyprint ;" "\"hi\" 12.0 or ." "\"hi\"" }
160 } ;
161
162 HELP: xor
163 { $values { "obj1" "a generalized boolean" } { "obj2" "a generalized boolean" } { "?" "a generalized boolean" } }
164 { $description "If exactly one input is false, outputs the other input. Otherwise outputs " { $link f } "." }
165 { $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." } ;
166
167 HELP: both?
168 { $values { "x" object } { "y" object } { "quot" { $quotation "( obj -- ? )" } } { "?" "a boolean" } }
169 { $description "Tests if the quotation yields a true value when applied to both " { $snippet "x" } " and " { $snippet "y" } "." }
170 { $examples
171     { $example "USING: kernel math prettyprint ;" "3 5 [ odd? ] both? ." "t" }
172     { $example "USING: kernel math prettyprint ;" "12 7 [ even? ] both? ." "f" }
173 } ;
174
175 HELP: either?
176 { $values { "x" object } { "y" object } { "quot" { $quotation "( obj -- ? )" } } { "?" "a boolean" } }
177 { $description "Tests if the quotation yields a true value when applied to either " { $snippet "x" } " or " { $snippet "y" } "." }
178 { $examples
179     { $example "USING: kernel math prettyprint ;" "3 6 [ odd? ] either? ." "t" }
180     { $example "USING: kernel math prettyprint ;" "5 7 [ even? ] either? ." "f" }
181 } ;
182
183 HELP: execute
184 { $values { "word" word } }
185 { $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." }
186 { $examples
187     { $example "USING: kernel io words ;" "IN: scratchpad" ": twice ( word -- ) dup execute execute ; inline\n: hello ( -- ) \"Hello\" print ;\n\\ hello twice" "Hello\nHello" }
188 } ;
189
190 { execute POSTPONE: execute( } related-words
191
192 HELP: (execute)
193 { $values { "word" word } }
194 { $description "Executes a word without checking if it is a word first." }
195 { $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." } ;
196
197 HELP: call
198 { $values { "callable" callable } }
199 { $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." }
200 { $examples
201     "The following two lines are equivalent:"
202     { $code "2 [ 2 + 3 * ] call" "2 2 + 3 *" }
203 } ;
204
205 { call POSTPONE: call( } related-words
206
207 HELP: call-clear ( quot -- )
208 { $values { "quot" callable } }
209 { $description "Calls a quotation with an empty call stack. If the quotation returns, Factor will exit.." }
210 { $notes "Used to implement " { $link "threads" } "." } ;
211
212 HELP: keep
213 { $values { "x" object } { "quot" { $quotation "( x -- ... )" } } }
214 { $description "Call a quotation with a value on the stack, restoring the value when the quotation returns." }
215 { $examples
216     { $example "USING: arrays kernel prettyprint ;" "2 \"greetings\" [ <array> ] keep 2array ." "{ { \"greetings\" \"greetings\" } \"greetings\" }" }
217 } ;
218
219 HELP: 2keep
220 { $values { "x" object } { "y" object } { "quot" { $quotation "( x y -- ... )" } } }
221 { $description "Call a quotation with two values on the stack, restoring the values when the quotation returns." } ;
222
223 HELP: 3keep
224 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( x y z -- ... )" } } }
225 { $description "Call a quotation with three values on the stack, restoring the values when the quotation returns." } ;
226
227 HELP: bi
228 { $values { "x" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( x -- ... )" } } }
229 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } "." }
230 { $examples
231     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
232     { $code
233         "[ p ] [ q ] bi"
234         "dup p q"
235     }
236     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
237     { $code
238         "[ p ] [ q ] bi"
239         "dup p swap q"
240     }
241     "In general, the following two lines are equivalent:"
242     { $code
243         "[ p ] [ q ] bi"
244         "[ p ] keep q"
245     }
246     
247 } ;
248
249 HELP: 2bi
250 { $values { "x" object } { "y" object } { "p" { $quotation "( x y -- ... )" } } { "q" { $quotation "( x y -- ... )" } } }
251 { $description "Applies " { $snippet "p" } " to the two input values, then applies " { $snippet "q" } " to the two input values." }
252 { $examples
253     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
254     { $code
255         "[ p ] [ q ] 2bi"
256         "2dup p q"
257     }
258     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y -- z )" } ", then the following two lines are equivalent:"
259     { $code
260         "[ p ] [ q ] 2bi"
261         "2dup p -rot q"
262     }
263     "In general, the following two lines are equivalent:"
264     { $code
265         "[ p ] [ q ] 2bi"
266         "[ p ] 2keep q"
267     }
268 } ;
269
270 HELP: 3bi
271 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x y z -- ... )" } } { "q" { $quotation "( x y z -- ... )" } } }
272 { $description "Applies " { $snippet "p" } " to the three input values, then applies " { $snippet "q" } " to the three input values." }
273 { $examples
274     "If " { $snippet "[ p ]" } " and " { $snippet "[ q ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
275     { $code
276         "[ p ] [ q ] 3bi"
277         "3dup p q"
278     }
279     "In general, the following two lines are equivalent:"
280     { $code
281         "[ p ] [ q ] 3bi"
282         "[ p ] 3keep q"
283     }
284 } ;
285
286 HELP: tri
287 { $values { "x" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( x -- ... )" } } { "r" { $quotation "( x -- ... )" } } }
288 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } ", and finally applies " { $snippet "r" } " to " { $snippet "x" } "." }
289 { $examples
290     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
291     { $code
292         "[ p ] [ q ] [ r ] tri"
293         "dup p dup q r"
294     }
295     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
296     { $code
297         "[ p ] [ q ] [ r ] tri"
298         "dup p over q rot r"
299     }
300     "In general, the following two lines are equivalent:"
301     { $code
302         "[ p ] [ q ] [ r ] tri"
303         "[ p ] keep [ q ] keep r"
304     }
305 } ;
306
307 HELP: 2tri
308 { $values { "x" object } { "y" object } { "p" { $quotation "( x y -- ... )" } } { "q" { $quotation "( x y -- ... )" } } { "r" { $quotation "( x y -- ... )" } } }
309 { $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." }
310 { $examples
311     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
312     { $code
313         "[ p ] [ q ] [ r ] 2tri"
314         "2dup p 2dup q r"
315     }
316     "In general, the following two lines are equivalent:"
317     { $code
318         "[ p ] [ q ] [ r ] 2tri"
319         "[ p ] 2keep [ q ] 2keep r"
320     }
321 } ;
322
323 HELP: 3tri
324 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x y z -- ... )" } } { "q" { $quotation "( x y z -- ... )" } } { "r" { $quotation "( x y z -- ... )" } } }
325 { $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." }
326 { $examples
327     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
328     { $code
329         "[ p ] [ q ] [ r ] 3tri"
330         "3dup p 3dup q r"
331     }
332     "In general, the following two lines are equivalent:"
333     { $code
334         "[ p ] [ q ] [ r ] 3tri"
335         "[ p ] 3keep [ q ] 3keep r"
336     }
337 } ;
338
339
340 HELP: bi*
341 { $values { "x" object } { "y" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } }
342 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } "." }
343 { $examples
344     "The following two lines are equivalent:"
345     { $code
346         "[ p ] [ q ] bi*"
347         "[ p ] dip q"
348     }
349 } ;
350
351 HELP: 2bi*
352 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "p" { $quotation "( w x -- ... )" } } { "q" { $quotation "( y z -- ... )" } } }
353 { $description "Applies " { $snippet "p" } " to " { $snippet "w" } " and " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "z" } "." }
354 { $examples
355     "The following two lines are equivalent:"
356     { $code
357         "[ p ] [ q ] 2bi*"
358         "[ p ] 2dip q"
359     }
360 } ;
361
362 HELP: 2tri*
363 { $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 -- ... )" } } }
364 { $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" } "." }
365 { $examples
366     "The following two lines are equivalent:"
367     { $code
368         "[ p ] [ q ] [ r ] 2tri*"
369         "[ [ p ] 2dip q ] 2dip r"
370     }
371 } ;
372
373 HELP: tri*
374 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } { "r" { $quotation "( z -- ... )" } } }
375 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } ", and finally applies " { $snippet "r" } " to " { $snippet "z" } "." }
376 { $examples
377     "The following two lines are equivalent:"
378     { $code
379         "[ p ] [ q ] [ r ] tri*"
380         "[ [ p ] dip q ] dip r"
381     }
382 } ;
383
384 HELP: bi@
385 { $values { "x" object } { "y" object } { "quot" { $quotation "( obj -- ... )" } } }
386 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } "." }
387 { $examples
388     "The following two lines are equivalent:"
389     { $code
390         "[ p ] bi@"
391         "[ p ] dip p"
392     }
393     "The following two lines are also equivalent:"
394     { $code
395         "[ p ] bi@"
396         "[ p ] [ p ] bi*"
397     }
398 } ;
399
400 HELP: 2bi@
401 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj1 obj2 -- ... )" } } }
402 { $description "Applies the quotation to " { $snippet "w" } " and " { $snippet "x" } ", then to " { $snippet "y" } " and " { $snippet "z" } "." }
403 { $examples
404     "The following two lines are equivalent:"
405     { $code
406         "[ p ] 2bi@"
407         "[ p ] 2dip p"
408     }
409     "The following two lines are also equivalent:"
410     { $code
411         "[ p ] 2bi@"
412         "[ p ] [ p ] 2bi*"
413     }
414 } ;
415
416 HELP: tri@
417 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj -- ... )" } } }
418 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } ", and finally to " { $snippet "z" } "." }
419 { $examples
420     "The following two lines are equivalent:"
421     { $code
422         "[ p ] tri@"
423         "[ [ p ] dip p ] dip p"
424     }
425     "The following two lines are also equivalent:"
426     { $code
427         "[ p ] tri@"
428         "[ p ] [ p ] [ p ] tri*"
429     }
430 } ;
431
432 HELP: 2tri@
433 { $values { "u" object } { "v" object } { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj1 obj2 -- ... )" } } }
434 { $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" } "." }
435 { $examples
436     "The following two lines are equivalent:"
437     { $code
438         "[ p ] 2tri@"
439         "[ [ p ] 2dip p ] 2dip p"
440     }
441     "The following two lines are also equivalent:"
442     { $code
443         "[ p ] 2tri@"
444         "[ p ] [ p ] [ p ] 2tri*"
445     }
446 } ;
447
448 HELP: bi-curry
449 { $values { "x" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( x -- ... )" } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ x q ]" } } }
450 { $description "Partially applies " { $snippet "p" } " and " { $snippet "q" } " to " { $snippet "x" } "." }
451 { $notes
452   "The following two lines are equivalent:"
453   { $code
454     "[ p ] [ q ] bi-curry [ call ] bi@"
455     "[ p ] [ q ] bi"
456   }
457   "Higher-arity variants of " { $link bi } " can be built from " { $link bi-curry } ":"
458   { $code
459     "[ p ] [ q ] bi-curry bi == [ p ] [ q ] 2bi"
460     "[ p ] [ q ] bi-curry bi-curry bi == [ p ] [ q ] 3bi"
461   }
462   "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* } ":"
463   { $code
464     "[ p ] [ q ] bi-curry bi*"
465     "[ swap ] keep [ p ] [ q ] 2bi*"
466   }
467   "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" } "."
468 } ;
469
470 HELP: tri-curry
471 { $values
472   { "x" object }
473   { "p" { $quotation "( x -- ... )" } }
474   { "q" { $quotation "( x -- ... )" } }
475   { "r" { $quotation "( x -- ... )" } }
476   { "p'" { $snippet "[ x p ]" } }
477   { "q'" { $snippet "[ x q ]" } }
478   { "r'" { $snippet "[ x r ]" } }
479 }
480 { $description "Partially applies " { $snippet "p" } ", " { $snippet "q" } " and " { $snippet "r" } " to " { $snippet "x" } "." }
481 { $notes
482   "The following two lines are equivalent:"
483   { $code
484     "[ p ] [ q ] [ r ] tri-curry [ call ] tri@"
485     "[ p ] [ q ] [ r ] tri"
486   }
487   "Higher-arity variants of " { $link tri } " can be built from " { $link tri-curry } ":"
488   { $code
489     "[ p ] [ q ] [ r ] tri-curry tri == [ p ] [ q ] [ r ] 2tri"
490     "[ p ] [ q ] [ r ] tri-curry tri-curry bi == [ p ] [ q ] [ r ] 3tri"
491   }
492   "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" } "." } ;
493
494 HELP: bi-curry*
495 { $values { "x" object } { "y" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ y q ]" } } }
496 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", and " { $snippet "q" } " to " { $snippet "y" } "." }
497 { $notes
498   "The following two lines are equivalent:"
499   { $code
500     "[ p ] [ q ] bi-curry* [ call ] bi@"
501     "[ p ] [ q ] bi*"
502   }
503   "The combination " { $snippet "bi-curry* bi" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
504   { $code
505     "[ p ] [ q ] bi-curry* bi"
506     "[ over ] dip [ p ] [ q ] 2bi*"
507   }
508   "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" } "."
509   $nl
510   "The combination " { $snippet "bi-curry* bi*" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
511   { $code
512     "[ p ] [ q ] bi-curry* bi*"
513     "[ swap ] dip [ p ] [ q ] 2bi*"
514   }
515   "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" } "."
516   
517 } ;
518
519 HELP: tri-curry*
520 { $values
521   { "x" object }
522   { "y" object }
523   { "z" object }
524   { "p" { $quotation "( x -- ... )" } }
525   { "q" { $quotation "( y -- ... )" } }
526   { "r" { $quotation "( z -- ... )" } }
527   { "p'" { $snippet "[ x p ]" } }
528   { "q'" { $snippet "[ y q ]" } }
529   { "r'" { $snippet "[ z r ]" } }
530 }
531 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "r" } " to " { $snippet "z" } "." }
532 { $notes
533   "The following two lines are equivalent:"
534   { $code
535     "[ p ] [ q ] [ r ] tri-curry* [ call ] tri@"
536     "[ p ] [ q ] [ r ] tri*"
537   }
538   "The combination " { $snippet "tri-curry* tri" } " is equivalent to a stack shuffle preceding " { $link 2tri* } ":"
539   { $code
540     "[ p ] [ q ] [ r ] tri-curry* tri"
541     "[ [ over ] dip over ] dip [ p ] [ q ] [ r ] 2tri*"
542   }
543 } ;
544
545 HELP: bi-curry@
546 { $values { "x" object } { "y" object } { "q" { $quotation "( obj -- ... )" } } { "p'" { $snippet "[ x q ]" } } { "q'" { $snippet "[ y q ]" } } }
547 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } " and " { $snippet "y" } "." }
548 { $notes
549   "The following two lines are equivalent:"
550   { $code
551     "[ q ] bi-curry@"
552     "[ q ] [ q ] bi-curry*"
553   }
554 } ;
555
556 HELP: tri-curry@
557 { $values
558   { "x" object }
559   { "y" object }
560   { "z" object }
561   { "q" { $quotation "( obj -- ... )" } }
562   { "p'" { $snippet "[ x q ]" } }
563   { "q'" { $snippet "[ y q ]" } }
564   { "r'" { $snippet "[ z q ]" } }
565 }
566 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } "." }
567 { $notes
568   "The following two lines are equivalent:"
569   { $code
570     "[ q ] tri-curry@"
571     "[ q ] [ q ] [ q ] tri-curry*"
572   }
573 } ;
574
575 HELP: if
576 { $values { "?" "a generalized boolean" } { "true" quotation } { "false" quotation } }
577 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation. Otherwise calls the " { $snippet "true" } " quotation."
578 $nl
579 "The " { $snippet "cond" } " value is removed from the stack before either quotation is called." } ;
580
581 HELP: when
582 { $values { "?" "a generalized boolean" } { "true" quotation } }
583 { $description "If " { $snippet "cond" } " is not " { $link f } ", calls the " { $snippet "true" } " quotation."
584 $nl
585 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
586
587 HELP: unless
588 { $values { "?" "a generalized boolean" } { "false" quotation } }
589 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation."
590 $nl
591 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
592
593 HELP: if*
594 { $values { "?" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } { "false" quotation } }
595 { $description "Alternative conditional form that preserves the " { $snippet "cond" } " value if it is true."
596 $nl
597 "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."
598 $nl
599 "The following two lines are equivalent:"
600 { $code "X [ Y ] [ Z ] if*" "X dup [ Y ] [ drop Z ] if" } } ;
601
602 HELP: when*
603 { $values { "?" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } }
604 { $description "Variant of " { $link if* } " with no false quotation."
605 $nl
606 "The following two lines are equivalent:"
607 { $code "X [ Y ] when*" "X dup [ Y ] [ drop ] if" } } ;
608
609 HELP: unless*
610 { $values { "?" "a generalized boolean" } { "false" "a quotation " } }
611 { $description "Variant of " { $link if* } " with no true quotation." }
612 { $notes
613 "The following two lines are equivalent:"
614 { $code "X [ Y ] unless*" "X dup [ ] [ drop Y ] if" } } ;
615
616 HELP: ?if
617 { $values { "default" object } { "cond" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } { "false" { $quotation "( default -- ... )" } } }
618 { $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." }
619 { $notes
620 "The following two lines are equivalent:"
621 { $code "[ X ] [ Y ] ?if" "dup [ nip X ] [ drop Y ] if" }
622 "The following two lines are equivalent:"
623 { $code "[ ] [ ] ?if" "swap or" } } ;
624
625 HELP: die
626 { $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." }
627 { $notes
628     "The term FEP originates from the Lisp machines of old. According to the Jargon File,"
629     $nl
630     { $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'." 
631     $nl
632     { $url "http://www.jargon.net/jargonfile/f/feppedout.html" }
633 } ;
634
635 HELP: (clone) ( obj -- newobj )
636 { $values { "obj" object } { "newobj" "a shallow copy" } }
637 { $description "Outputs a byte-by-byte copy of the given object. User code should call " { $link clone } " instead." } ;
638
639 HELP: declare
640 { $values { "spec" "an array of class words" } }
641 { $description "Declares that the elements at the top of the stack are instances of the classes in " { $snippet "spec" } "." }
642 { $warning "The compiler blindly trusts declarations, and false declarations can lead to crashes, memory corruption and other undesirable behavior." }
643 { $examples
644     "The optimizer cannot do anything with the below code:"
645     { $code "2 + 10 *" }
646     "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:"
647     { $code "{ float } declare 2 + 10 *" }
648 } ;
649
650 HELP: tag ( object -- n )
651 { $values { "object" object } { "n" "a tag number" } }
652 { $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 } " instead." } ;
653
654 HELP: getenv ( n -- obj )
655 { $values { "n" "a non-negative integer" } { "obj" object } }
656 { $description "Reads an object from the Factor VM's environment table. User code never has to read the environment table directly; instead, use one of the callers of this word." } ;
657
658 HELP: setenv ( obj n -- )
659 { $values { "obj" object } { "n" "a non-negative integer" } }
660 { $description "Writes an object to the Factor VM's environment table. User code never has to write to the environment table directly; instead, use one of the callers of this word." } ;
661
662 HELP: object
663 { $class-description
664     "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:"
665     { $code "GENERIC: enclose" "M: number enclose 1array ;" "M: object enclose ;" }
666 } ;
667
668 HELP: null
669 { $class-description
670     "The canonical empty class with no instances."
671 } ;
672
673 HELP: most
674 { $values { "x" object } { "y" object } { "quot" { $quotation "( x y -- ? )" } } { "z" "either " { $snippet "x" } " or " { $snippet "y" } } }
675 { $description "If the quotation yields a true value when applied to " { $snippet "x" } " and " { $snippet "y" } ", outputs " { $snippet "x" } ", otherwise outputs " { $snippet "y" } "." } ;
676
677 HELP: curry
678 { $values { "obj" object } { "quot" callable } { "curry" curry } }
679 { $description "Partial application. Outputs a " { $link callable } " which first pushes " { $snippet "obj" } " and then calls " { $snippet "quot" } "." }
680 { $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." }
681 { $notes "Even if " { $snippet "obj" } " is a word, it will be pushed as a literal."
682 $nl
683 "This operation is efficient and does not copy the quotation." }
684 { $examples
685     { $example "USING: kernel prettyprint ;" "5 [ . ] curry ." "[ 5 . ]" }
686     { $example "USING: kernel prettyprint see ;" "\\ = [ see ] curry ." "[ \\ = see ]" }
687     { $example "USING: kernel math prettyprint sequences ;" "{ 1 2 3 } 2 [ - ] curry map ." "{ -1 0 1 }" }
688 } ;
689
690 HELP: 2curry
691 { $values { "obj1" object } { "obj2" object } { "quot" callable } { "curry" curry } }
692 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } " and " { $snippet "obj2" } " and then calls " { $snippet "quot" } "." }
693 { $notes "This operation is efficient and does not copy the quotation." }
694 { $examples
695     { $example "USING: kernel math prettyprint ;" "5 4 [ + ] 2curry ." "[ 5 4 + ]" }
696 } ;
697
698 HELP: 3curry
699 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "quot" callable } { "curry" curry } }
700 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } ", " { $snippet "obj2" } " and " { $snippet "obj3" } ", and then calls " { $snippet "quot" } "." }
701 { $notes "This operation is efficient and does not copy the quotation." } ;
702
703 HELP: with
704 { $values { "param" object } { "obj" object } { "quot" { $quotation "( param elt -- ... )" } } { "obj" object } { "curry" curry } }
705 { $description "Partial application on the left. The following two lines are equivalent:"
706     { $code "swap [ swap A ] curry B" }
707     { $code "[ A ] with B" }
708     
709 }
710 { $notes "This operation is efficient and does not copy the quotation." }
711 { $examples
712     { $example "USING: kernel math prettyprint sequences ;" "2 { 1 2 3 } [ - ] with map ." "{ 1 0 -1 }" }
713 } ;
714
715 HELP: compose
716 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
717 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot1" } " followed by " { $snippet "quot2" } "." }
718 { $notes
719     "The following two lines are equivalent:"
720     { $code
721         "compose call"
722         "append call"
723     }
724     "However, " { $link compose } " runs in constant time, and the optimizing compiler is able to compile code which calls composed quotations."
725 } ;
726
727
728 HELP: prepose
729 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
730 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot2" } " followed by " { $snippet "quot1" } "." }
731 { $notes "See " { $link compose } " for details." } ;
732
733 { compose prepose } related-words
734
735 HELP: dip
736 { $values { "x" object } { "quot" quotation } }
737 { $description "Calls " { $snippet "quot" } " with " { $snippet "obj" } " hidden on the retain stack." }
738 { $examples
739     { $example "USING: arrays kernel math prettyprint ;" "10 20 30 [ / ] dip 2array ." "{ 1/2 30 }" }
740 } ;
741
742 HELP: 2dip
743 { $values { "x" object } { "y" object } { "quot" quotation } }
744 { $description "Calls " { $snippet "quot" } " with " { $snippet "x" } " and " { $snippet "y" } " hidden on the retain stack." }
745 { $notes "The following are equivalent:"
746     { $code "[ [ foo bar ] dip ] dip" }
747     { $code "[ foo bar ] 2dip" }
748 } ;
749
750 HELP: 3dip
751 { $values { "x" object } { "y" object } { "z" object } { "quot" quotation } }
752 { $description "Calls " { $snippet "quot" } " with " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } " hidden on the retain stack." }
753 { $notes "The following are equivalent:"
754     { $code "[ [ [ foo bar ] dip ] dip ] dip" }
755     { $code "[ foo bar ] 3dip" }
756 } ;
757
758 HELP: 4dip
759 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" quotation } }
760 { $description "Calls " { $snippet "quot" } " with " { $snippet "w" } ", " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } " hidden on the retain stack." }
761 { $notes "The following are equivalent:"
762     { $code "[ [ [ [ foo bar ] dip ] dip ] dip ] dip" }
763     { $code "[ foo bar ] 4dip" }
764 } ;
765
766 HELP: while
767 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } }
768 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link f } "." } ;
769
770 HELP: until
771 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } }
772 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link t } "." } ;
773
774 HELP: do
775 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } }
776 { $description "Executes one iteration of a " { $link while } " or " { $link until } " loop." } ;
777
778 HELP: loop
779 { $values
780      { "pred" quotation } }
781      { $description "Calls the quotation repeatedly until it outputs " { $link f } "." }
782 { $examples "Loop until we hit a zero:"
783     { $unchecked-example "USING: kernel random math io ; "
784     " [ \"hi\" write bl 10 random zero? not ] loop"
785     "hi hi hi" }
786     "A fun loop:"
787     { $example "USING: kernel prettyprint math ; "
788     "3 [ dup . 7 + 11 mod dup 3 = not ] loop drop"
789     "3\n10\n6\n2\n9\n5\n1\n8\n4\n0\n7" }
790 } ;
791
792 ARTICLE: "looping-combinators" "Looping combinators"
793 "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."
794 { $subsections
795     while
796     until
797 }
798 "To execute one iteration of a loop, use the following word:"
799 { $subsections do }
800 "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 } ":"
801 { $code
802     "[ P ] [ Q ] do while"
803 }
804 "A simpler looping combinator which executes a single quotation until it returns " { $link f } ":"
805 { $subsections loop } ;
806
807 HELP: assert
808 { $values { "got" "the obtained value" } { "expect" "the expected value" } }
809 { $description "Throws an " { $link assert } " error." }
810 { $error-description "Thrown when a unit test or other assertion fails." } ;
811
812 HELP: assert=
813 { $values { "a" object } { "b" object } }
814 { $description "Throws an " { $link assert } " error if " { $snippet "a" } " does not equal " { $snippet "b" } "." } ;
815
816 ARTICLE: "shuffle-words-complex" "Complex shuffle words"
817 "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" } "."
818 $nl
819 "Duplicating stack elements deep in the stack:"
820 { $subsections
821     dupd
822 }
823 "Permuting stack elements deep in the stack:"
824 { $subsections
825     swapd
826     rot
827     -rot
828 } ;
829
830 ARTICLE: "shuffle-words" "Shuffle words"
831 "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" } "."
832 $nl
833 "Removing stack elements:"
834 { $subsections
835     drop
836     2drop
837     3drop
838     nip
839     2nip
840 }
841 "Duplicating stack elements:"
842 { $subsections
843     dup
844     2dup
845     3dup
846     over
847     2over
848     pick
849 }
850 "Permuting stack elements:"
851 { $subsections
852     swap
853 }
854 "There are additional, more complex stack shuffling words whose use is not recommended."
855 { $subsections
856     "shuffle-words-complex"
857 } ;
858
859 ARTICLE: "equality" "Equality"
860 "There are two distinct notions of “sameness” when it comes to objects."
861 $nl
862 "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:"
863 { $subsections eq? }
864 "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" } "):"
865 { $subsections = }
866 "A third form of equality is provided by " { $link number= } ". It compares numeric value while disregarding types."
867 $nl
868 "Custom value comparison methods for use with " { $link = } " can be defined on a generic word:"
869 { $subsections equal? }
870 "Utility class:"
871 { $subsections identity-tuple }
872 "An object can be cloned; the clone has distinct identity but equal value:"
873 { $subsections clone } ;
874
875 ARTICLE: "assertions" "Assertions"
876 "Some words to make assertions easier to enforce:"
877 { $subsections
878     assert
879     assert=
880 } ;
881