]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel-docs.factor
8b9650fc31f9457b747e2a278dadf6db0e0f6700
[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. Control flow is transferred immediately to the innermost frame of 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 HELP: identity-hashcode
76 { $values { "obj" object } { "code" fixnum } }
77 { $description "Outputs the identity hashcode of an object. The identity hashcode is not guaranteed to be unique, however it will not change during the object's lifetime." } ;
78
79 { hashcode hashcode* identity-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     "In general, the following two lines are equivalent:"
284     { $code
285         "[ p ] [ q ] 3bi"
286         "[ p ] 3keep q"
287     }
288 } ;
289
290 HELP: tri
291 { $values { "x" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( x -- ... )" } } { "r" { $quotation "( x -- ... )" } } }
292 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "x" } ", and finally applies " { $snippet "r" } " to " { $snippet "x" } "." }
293 { $examples
294     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- )" } ", then the following two lines are equivalent:"
295     { $code
296         "[ p ] [ q ] [ r ] tri"
297         "dup p dup q r"
298     }
299     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x -- y )" } ", then the following two lines are equivalent:"
300     { $code
301         "[ p ] [ q ] [ r ] tri"
302         "dup p over q rot r"
303     }
304     "In general, the following two lines are equivalent:"
305     { $code
306         "[ p ] [ q ] [ r ] tri"
307         "[ p ] keep [ q ] keep r"
308     }
309 } ;
310
311 HELP: 2tri
312 { $values { "x" object } { "y" object } { "p" { $quotation "( x y -- ... )" } } { "q" { $quotation "( x y -- ... )" } } { "r" { $quotation "( x y -- ... )" } } }
313 { $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." }
314 { $examples
315     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y -- )" } ", then the following two lines are equivalent:"
316     { $code
317         "[ p ] [ q ] [ r ] 2tri"
318         "2dup p 2dup q r"
319     }
320     "In general, the following two lines are equivalent:"
321     { $code
322         "[ p ] [ q ] [ r ] 2tri"
323         "[ p ] 2keep [ q ] 2keep r"
324     }
325 } ;
326
327 HELP: 3tri
328 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x y z -- ... )" } } { "q" { $quotation "( x y z -- ... )" } } { "r" { $quotation "( x y z -- ... )" } } }
329 { $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." }
330 { $examples
331     "If " { $snippet "[ p ]" } ", " { $snippet "[ q ]" } " and " { $snippet "[ r ]" } " have stack effect " { $snippet "( x y z -- )" } ", then the following two lines are equivalent:"
332     { $code
333         "[ p ] [ q ] [ r ] 3tri"
334         "3dup p 3dup q r"
335     }
336     "In general, the following two lines are equivalent:"
337     { $code
338         "[ p ] [ q ] [ r ] 3tri"
339         "[ p ] 3keep [ q ] 3keep r"
340     }
341 } ;
342
343
344 HELP: bi*
345 { $values { "x" object } { "y" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } }
346 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } "." }
347 { $examples
348     "The following two lines are equivalent:"
349     { $code
350         "[ p ] [ q ] bi*"
351         "[ p ] dip q"
352     }
353 } ;
354
355 HELP: 2bi*
356 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "p" { $quotation "( w x -- ... )" } } { "q" { $quotation "( y z -- ... )" } } }
357 { $description "Applies " { $snippet "p" } " to " { $snippet "w" } " and " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "z" } "." }
358 { $examples
359     "The following two lines are equivalent:"
360     { $code
361         "[ p ] [ q ] 2bi*"
362         "[ p ] 2dip q"
363     }
364 } ;
365
366 HELP: 2tri*
367 { $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 -- ... )" } } }
368 { $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" } "." }
369 { $examples
370     "The following two lines are equivalent:"
371     { $code
372         "[ p ] [ q ] [ r ] 2tri*"
373         "[ [ p ] 2dip q ] 2dip r"
374     }
375 } ;
376
377 HELP: tri*
378 { $values { "x" object } { "y" object } { "z" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } { "r" { $quotation "( z -- ... )" } } }
379 { $description "Applies " { $snippet "p" } " to " { $snippet "x" } ", then applies " { $snippet "q" } " to " { $snippet "y" } ", and finally applies " { $snippet "r" } " to " { $snippet "z" } "." }
380 { $examples
381     "The following two lines are equivalent:"
382     { $code
383         "[ p ] [ q ] [ r ] tri*"
384         "[ [ p ] dip q ] dip r"
385     }
386 } ;
387
388 HELP: bi@
389 { $values { "x" object } { "y" object } { "quot" { $quotation "( obj -- ... )" } } }
390 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } "." }
391 { $examples
392     "The following two lines are equivalent:"
393     { $code
394         "[ p ] bi@"
395         "[ p ] dip p"
396     }
397     "The following two lines are also equivalent:"
398     { $code
399         "[ p ] bi@"
400         "[ p ] [ p ] bi*"
401     }
402 } ;
403
404 HELP: 2bi@
405 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj1 obj2 -- ... )" } } }
406 { $description "Applies the quotation to " { $snippet "w" } " and " { $snippet "x" } ", then to " { $snippet "y" } " and " { $snippet "z" } "." }
407 { $examples
408     "The following two lines are equivalent:"
409     { $code
410         "[ p ] 2bi@"
411         "[ p ] 2dip p"
412     }
413     "The following two lines are also equivalent:"
414     { $code
415         "[ p ] 2bi@"
416         "[ p ] [ p ] 2bi*"
417     }
418 } ;
419
420 HELP: tri@
421 { $values { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj -- ... )" } } }
422 { $description "Applies the quotation to " { $snippet "x" } ", then to " { $snippet "y" } ", and finally to " { $snippet "z" } "." }
423 { $examples
424     "The following two lines are equivalent:"
425     { $code
426         "[ p ] tri@"
427         "[ [ p ] dip p ] dip p"
428     }
429     "The following two lines are also equivalent:"
430     { $code
431         "[ p ] tri@"
432         "[ p ] [ p ] [ p ] tri*"
433     }
434 } ;
435
436 HELP: 2tri@
437 { $values { "u" object } { "v" object } { "w" object } { "x" object } { "y" object } { "z" object } { "quot" { $quotation "( obj1 obj2 -- ... )" } } }
438 { $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" } "." }
439 { $examples
440     "The following two lines are equivalent:"
441     { $code
442         "[ p ] 2tri@"
443         "[ [ p ] 2dip p ] 2dip p"
444     }
445     "The following two lines are also equivalent:"
446     { $code
447         "[ p ] 2tri@"
448         "[ p ] [ p ] [ p ] 2tri*"
449     }
450 } ;
451
452 HELP: bi-curry
453 { $values { "x" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( x -- ... )" } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ x q ]" } } }
454 { $description "Partially applies " { $snippet "p" } " and " { $snippet "q" } " to " { $snippet "x" } "." }
455 { $notes
456   "The following two lines are equivalent:"
457   { $code
458     "[ p ] [ q ] bi-curry [ call ] bi@"
459     "[ p ] [ q ] bi"
460   }
461   "Higher-arity variants of " { $link bi } " can be built from " { $link bi-curry } ":"
462   { $code
463     "[ p ] [ q ] bi-curry bi == [ p ] [ q ] 2bi"
464     "[ p ] [ q ] bi-curry bi-curry bi == [ p ] [ q ] 3bi"
465   }
466   "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* } ":"
467   { $code
468     "[ p ] [ q ] bi-curry bi*"
469     "[ swap ] keep [ p ] [ q ] 2bi*"
470   }
471   "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" } "."
472 } ;
473
474 HELP: tri-curry
475 { $values
476   { "x" object }
477   { "p" { $quotation "( x -- ... )" } }
478   { "q" { $quotation "( x -- ... )" } }
479   { "r" { $quotation "( x -- ... )" } }
480   { "p'" { $snippet "[ x p ]" } }
481   { "q'" { $snippet "[ x q ]" } }
482   { "r'" { $snippet "[ x r ]" } }
483 }
484 { $description "Partially applies " { $snippet "p" } ", " { $snippet "q" } " and " { $snippet "r" } " to " { $snippet "x" } "." }
485 { $notes
486   "The following two lines are equivalent:"
487   { $code
488     "[ p ] [ q ] [ r ] tri-curry [ call ] tri@"
489     "[ p ] [ q ] [ r ] tri"
490   }
491   "Higher-arity variants of " { $link tri } " can be built from " { $link tri-curry } ":"
492   { $code
493     "[ p ] [ q ] [ r ] tri-curry tri == [ p ] [ q ] [ r ] 2tri"
494     "[ p ] [ q ] [ r ] tri-curry tri-curry bi == [ p ] [ q ] [ r ] 3tri"
495   }
496   "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" } "." } ;
497
498 HELP: bi-curry*
499 { $values { "x" object } { "y" object } { "p" { $quotation "( x -- ... )" } } { "q" { $quotation "( y -- ... )" } } { "p'" { $snippet "[ x p ]" } } { "q'" { $snippet "[ y q ]" } } }
500 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", and " { $snippet "q" } " to " { $snippet "y" } "." }
501 { $notes
502   "The following two lines are equivalent:"
503   { $code
504     "[ p ] [ q ] bi-curry* [ call ] bi@"
505     "[ p ] [ q ] bi*"
506   }
507   "The combination " { $snippet "bi-curry* bi" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
508   { $code
509     "[ p ] [ q ] bi-curry* bi"
510     "[ over ] dip [ p ] [ q ] 2bi*"
511   }
512   "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" } "."
513   $nl
514   "The combination " { $snippet "bi-curry* bi*" } " is equivalent to a stack shuffle preceding " { $link 2bi* } ":"
515   { $code
516     "[ p ] [ q ] bi-curry* bi*"
517     "[ swap ] dip [ p ] [ q ] 2bi*"
518   }
519   "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" } "."
520   
521 } ;
522
523 HELP: tri-curry*
524 { $values
525   { "x" object }
526   { "y" object }
527   { "z" object }
528   { "p" { $quotation "( x -- ... )" } }
529   { "q" { $quotation "( y -- ... )" } }
530   { "r" { $quotation "( z -- ... )" } }
531   { "p'" { $snippet "[ x p ]" } }
532   { "q'" { $snippet "[ y q ]" } }
533   { "r'" { $snippet "[ z r ]" } }
534 }
535 { $description "Partially applies " { $snippet "p" } " to " { $snippet "x" } ", " { $snippet "q" } " to " { $snippet "y" } " and " { $snippet "r" } " to " { $snippet "z" } "." }
536 { $notes
537   "The following two lines are equivalent:"
538   { $code
539     "[ p ] [ q ] [ r ] tri-curry* [ call ] tri@"
540     "[ p ] [ q ] [ r ] tri*"
541   }
542   "The combination " { $snippet "tri-curry* tri" } " is equivalent to a stack shuffle preceding " { $link 2tri* } ":"
543   { $code
544     "[ p ] [ q ] [ r ] tri-curry* tri"
545     "[ [ over ] dip over ] dip [ p ] [ q ] [ r ] 2tri*"
546   }
547 } ;
548
549 HELP: bi-curry@
550 { $values { "x" object } { "y" object } { "q" { $quotation "( obj -- ... )" } } { "p'" { $snippet "[ x q ]" } } { "q'" { $snippet "[ y q ]" } } }
551 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } " and " { $snippet "y" } "." }
552 { $notes
553   "The following two lines are equivalent:"
554   { $code
555     "[ q ] bi-curry@"
556     "[ q ] [ q ] bi-curry*"
557   }
558 } ;
559
560 HELP: tri-curry@
561 { $values
562   { "x" object }
563   { "y" object }
564   { "z" object }
565   { "q" { $quotation "( obj -- ... )" } }
566   { "p'" { $snippet "[ x q ]" } }
567   { "q'" { $snippet "[ y q ]" } }
568   { "r'" { $snippet "[ z q ]" } }
569 }
570 { $description "Partially applies " { $snippet "q" } " to " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } "." }
571 { $notes
572   "The following two lines are equivalent:"
573   { $code
574     "[ q ] tri-curry@"
575     "[ q ] [ q ] [ q ] tri-curry*"
576   }
577 } ;
578
579 HELP: if
580 { $values { "?" "a generalized boolean" } { "true" quotation } { "false" quotation } }
581 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation. Otherwise calls the " { $snippet "true" } " quotation."
582 $nl
583 "The " { $snippet "cond" } " value is removed from the stack before either quotation is called." } ;
584
585 HELP: when
586 { $values { "?" "a generalized boolean" } { "true" quotation } }
587 { $description "If " { $snippet "cond" } " is not " { $link f } ", calls the " { $snippet "true" } " quotation."
588 $nl
589 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
590
591 HELP: unless
592 { $values { "?" "a generalized boolean" } { "false" quotation } }
593 { $description "If " { $snippet "cond" } " is " { $link f } ", calls the " { $snippet "false" } " quotation."
594 $nl
595 "The " { $snippet "cond" } " value is removed from the stack before the quotation is called." } ;
596
597 HELP: if*
598 { $values { "?" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } { "false" quotation } }
599 { $description "Alternative conditional form that preserves the " { $snippet "cond" } " value if it is true."
600 $nl
601 "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."
602 $nl
603 "The following two lines are equivalent:"
604 { $code "X [ Y ] [ Z ] if*" "X dup [ Y ] [ drop Z ] if" } } ;
605
606 HELP: when*
607 { $values { "?" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } }
608 { $description "Variant of " { $link if* } " with no false quotation."
609 $nl
610 "The following two lines are equivalent:"
611 { $code "X [ Y ] when*" "X dup [ Y ] [ drop ] if" } } ;
612
613 HELP: unless*
614 { $values { "?" "a generalized boolean" } { "false" "a quotation " } }
615 { $description "Variant of " { $link if* } " with no true quotation." }
616 { $notes
617 "The following two lines are equivalent:"
618 { $code "X [ Y ] unless*" "X dup [ ] [ drop Y ] if" } } ;
619
620 HELP: ?if
621 { $values { "default" object } { "cond" "a generalized boolean" } { "true" { $quotation "( cond -- ... )" } } { "false" { $quotation "( default -- ... )" } } }
622 { $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." }
623 { $notes
624 "The following two lines are equivalent:"
625 { $code "[ X ] [ Y ] ?if" "dup [ nip X ] [ drop Y ] if" }
626 "The following two lines are equivalent:"
627 { $code "[ ] [ ] ?if" "swap or" } } ;
628
629 HELP: die
630 { $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." }
631 { $notes
632     "The term FEP originates from the Lisp machines of old. According to the Jargon File,"
633     $nl
634     { $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'." 
635     $nl
636     { $url "http://www.jargon.net/jargonfile/f/feppedout.html" }
637 } ;
638
639 HELP: (clone) ( obj -- newobj )
640 { $values { "obj" object } { "newobj" "a shallow copy" } }
641 { $description "Outputs a byte-by-byte copy of the given object. User code should call " { $link clone } " instead." } ;
642
643 HELP: declare
644 { $values { "spec" "an array of class words" } }
645 { $description "Declares that the elements at the top of the stack are instances of the classes in " { $snippet "spec" } "." }
646 { $warning "The compiler blindly trusts declarations, and false declarations can lead to crashes, memory corruption and other undesirable behavior." }
647 { $examples
648     "The optimizer cannot do anything with the below code:"
649     { $code "2 + 10 *" }
650     "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:"
651     { $code "{ float } declare 2 + 10 *" }
652 } ;
653
654 HELP: tag ( object -- n )
655 { $values { "object" object } { "n" "a tag number" } }
656 { $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." } ;
657
658 HELP: special-object ( n -- obj )
659 { $values { "n" "a non-negative integer" } { "obj" object } }
660 { $description "Reads an object from the Factor VM's special object table. User code never has to read the special object table directly; instead, use one of the callers of this word." } ;
661
662 HELP: set-special-object ( obj n -- )
663 { $values { "obj" object } { "n" "a non-negative integer" } }
664 { $description "Writes an object to the Factor VM's special object table. User code never has to write to the special object table directly; instead, use one of the callers of this word." } ;
665
666 HELP: object
667 { $class-description
668     "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:"
669     { $code "GENERIC: enclose" "M: number enclose 1array ;" "M: object enclose ;" }
670 } ;
671
672 HELP: null
673 { $class-description
674     "The canonical empty class with no instances."
675 }
676 { $notes
677     "Unlike " { $snippet "null" } " in Java or " { $snippet "NULL" } " in C++, this is not a value signifying empty, or nothing. Use " { $link f } " for this purpose."
678 } ;
679
680 HELP: most
681 { $values { "x" object } { "y" object } { "quot" { $quotation "( x y -- ? )" } } { "z" "either " { $snippet "x" } " or " { $snippet "y" } } }
682 { $description "If the quotation yields a true value when applied to " { $snippet "x" } " and " { $snippet "y" } ", outputs " { $snippet "x" } ", otherwise outputs " { $snippet "y" } "." } ;
683
684 HELP: curry
685 { $values { "obj" object } { "quot" callable } { "curry" curry } }
686 { $description "Partial application. Outputs a " { $link callable } " which first pushes " { $snippet "obj" } " and then calls " { $snippet "quot" } "." }
687 { $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." }
688 { $notes "Even if " { $snippet "obj" } " is a word, it will be pushed as a literal."
689 $nl
690 "This operation is efficient and does not copy the quotation." }
691 { $examples
692     { $example "USING: kernel prettyprint ;" "5 [ . ] curry ." "[ 5 . ]" }
693     { $example "USING: kernel prettyprint see ;" "\\ = [ see ] curry ." "[ \\ = see ]" }
694     { $example "USING: kernel math prettyprint sequences ;" "{ 1 2 3 } 2 [ - ] curry map ." "{ -1 0 1 }" }
695 } ;
696
697 HELP: 2curry
698 { $values { "obj1" object } { "obj2" object } { "quot" callable } { "curry" curry } }
699 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } " and " { $snippet "obj2" } " and then calls " { $snippet "quot" } "." }
700 { $notes "This operation is efficient and does not copy the quotation." }
701 { $examples
702     { $example "USING: kernel math prettyprint ;" "5 4 [ + ] 2curry ." "[ 5 4 + ]" }
703 } ;
704
705 HELP: 3curry
706 { $values { "obj1" object } { "obj2" object } { "obj3" object } { "quot" callable } { "curry" curry } }
707 { $description "Outputs a " { $link callable } " which pushes " { $snippet "obj1" } ", " { $snippet "obj2" } " and " { $snippet "obj3" } ", and then calls " { $snippet "quot" } "." }
708 { $notes "This operation is efficient and does not copy the quotation." } ;
709
710 HELP: with
711 { $values { "param" object } { "obj" object } { "quot" { $quotation "( param elt -- ... )" } } { "curry" curry } }
712 { $description "Partial application on the left. The following two lines are equivalent:"
713     { $code "swap [ swap A ] curry B" }
714     { $code "[ A ] with B" }
715     
716 }
717 { $notes "This operation is efficient and does not copy the quotation." }
718 { $examples
719     { $example "USING: kernel math prettyprint sequences ;" "2 { 1 2 3 } [ - ] with map ." "{ 1 0 -1 }" }
720 } ;
721
722 HELP: compose
723 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
724 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot1" } " followed by " { $snippet "quot2" } "." }
725 { $notes
726     "The following two lines are equivalent:"
727     { $code
728         "compose call"
729         "append call"
730     }
731     "However, " { $link compose } " runs in constant time, and the optimizing compiler is able to compile code which calls composed quotations."
732 } ;
733
734
735 HELP: prepose
736 { $values { "quot1" callable } { "quot2" callable } { "compose" compose } }
737 { $description "Quotation composition. Outputs a " { $link callable } " which calls " { $snippet "quot2" } " followed by " { $snippet "quot1" } "." }
738 { $notes "See " { $link compose } " for details." } ;
739
740 { compose prepose } related-words
741
742 HELP: dip
743 { $values { "x" object } { "quot" quotation } }
744 { $description "Calls " { $snippet "quot" } " with " { $snippet "obj" } " hidden on the retain stack." }
745 { $examples
746     { $example "USING: arrays kernel math prettyprint ;" "10 20 30 [ / ] dip 2array ." "{ 1/2 30 }" }
747 } ;
748
749 HELP: 2dip
750 { $values { "x" object } { "y" object } { "quot" quotation } }
751 { $description "Calls " { $snippet "quot" } " with " { $snippet "x" } " and " { $snippet "y" } " hidden on the retain stack." }
752 { $notes "The following are equivalent:"
753     { $code "[ [ foo bar ] dip ] dip" }
754     { $code "[ foo bar ] 2dip" }
755 } ;
756
757 HELP: 3dip
758 { $values { "x" object } { "y" object } { "z" object } { "quot" quotation } }
759 { $description "Calls " { $snippet "quot" } " with " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } " hidden on the retain stack." }
760 { $notes "The following are equivalent:"
761     { $code "[ [ [ foo bar ] dip ] dip ] dip" }
762     { $code "[ foo bar ] 3dip" }
763 } ;
764
765 HELP: 4dip
766 { $values { "w" object } { "x" object } { "y" object } { "z" object } { "quot" quotation } }
767 { $description "Calls " { $snippet "quot" } " with " { $snippet "w" } ", " { $snippet "x" } ", " { $snippet "y" } " and " { $snippet "z" } " hidden on the retain stack." }
768 { $notes "The following are equivalent:"
769     { $code "[ [ [ [ foo bar ] dip ] dip ] dip ] dip" }
770     { $code "[ foo bar ] 4dip" }
771 } ;
772
773 HELP: while
774 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } }
775 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link f } "." } ;
776
777 HELP: until
778 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } }
779 { $description "Calls " { $snippet "body" } " until " { $snippet "pred" } " returns " { $link t } "." } ;
780
781 HELP: do
782 { $values { "pred" { $quotation "( -- ? )" } } { "body" "a quotation" } }
783 { $description "Executes one iteration of a " { $link while } " or " { $link until } " loop." } ;
784
785 HELP: loop
786 { $values
787      { "pred" quotation } }
788      { $description "Calls the quotation repeatedly until it outputs " { $link f } "." }
789 { $examples "Loop until we hit a zero:"
790     { $unchecked-example "USING: kernel random math io ; "
791     " [ \"hi\" write bl 10 random zero? not ] loop"
792     "hi hi hi" }
793     "A fun loop:"
794     { $example "USING: kernel prettyprint math ; "
795     "3 [ dup . 7 + 11 mod dup 3 = not ] loop drop"
796     "3\n10\n6\n2\n9\n5\n1\n8\n4\n0\n7" }
797 } ;
798
799 ARTICLE: "looping-combinators" "Looping combinators"
800 "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."
801 { $subsections
802     while
803     until
804 }
805 "To execute one iteration of a loop, use the following word:"
806 { $subsections do }
807 "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 } ":"
808 { $code
809     "[ P ] [ Q ] do while"
810 }
811 "A simpler looping combinator which executes a single quotation until it returns " { $link f } ":"
812 { $subsections loop } ;
813
814 HELP: assert
815 { $values { "got" "the obtained value" } { "expect" "the expected value" } }
816 { $description "Throws an " { $link assert } " error." }
817 { $error-description "Thrown when a unit test or other assertion fails." } ;
818
819 HELP: assert=
820 { $values { "a" object } { "b" object } }
821 { $description "Throws an " { $link assert } " error if " { $snippet "a" } " does not equal " { $snippet "b" } "." } ;
822
823 HELP: become
824 { $values { "old" array } { "new" array } }
825 { $description "Replaces all references to objects in " { $snippet "old" } " with the corresponding object in " { $snippet "new" } ". This word is used to implement tuple reshaping. See " { $link "tuple-redefinition" } "." } ;
826
827 ARTICLE: "shuffle-words-complex" "Complex shuffle words"
828 "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" } "."
829 $nl
830 "Duplicating stack elements deep in the stack:"
831 { $subsections
832     dupd
833 }
834 "Permuting stack elements deep in the stack:"
835 { $subsections
836     swapd
837     rot
838     -rot
839 } ;
840
841 ARTICLE: "shuffle-words" "Shuffle words"
842 "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" } "."
843 $nl
844 "Removing stack elements:"
845 { $subsections
846     drop
847     2drop
848     3drop
849     nip
850     2nip
851 }
852 "Duplicating stack elements:"
853 { $subsections
854     dup
855     2dup
856     3dup
857     over
858     2over
859     pick
860 }
861 "Permuting stack elements:"
862 { $subsections
863     swap
864 }
865 "There are additional, more complex stack shuffling words whose use is not recommended."
866 { $subsections
867     "shuffle-words-complex"
868 } ;
869
870 ARTICLE: "equality" "Equality"
871 "There are two distinct notions of “sameness” when it comes to objects."
872 $nl
873 "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:"
874 { $subsections eq? }
875 "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" } "):"
876 { $subsections = }
877 "A third form of equality is provided by " { $link number= } ". It compares numeric value while disregarding types."
878 $nl
879 "Custom value comparison methods for use with " { $link = } " can be defined on a generic word:"
880 { $subsections equal? }
881 "Utility class:"
882 { $subsections identity-tuple }
883 "An object can be cloned; the clone has distinct identity but equal value:"
884 { $subsections clone } ;
885
886 ARTICLE: "assertions" "Assertions"
887 "Some words to make assertions easier to enforce:"
888 { $subsections
889     assert
890     assert=
891 } ;
892