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