1 USING: arrays help.markup help.syntax strings sbufs vectors
2 kernel quotations generic generic.standard classes
3 math assocs sequences sequences.private combinators.private
7 ARTICLE: "cleave-shuffle-equivalence" "Expressing shuffle words with cleave combinators"
8 "Cleave combinators are defined in terms of shuffle words, and mappings from certain shuffle idioms to cleave combinators are discussed in the documentation for " { $link bi } ", " { $link 2bi } ", " { $link 3bi } ", " { $link tri } ", " { $link 2tri } " and " { $link 3tri } "."
10 "Certain shuffle words can also be expressed in terms of the cleave combinators. Internalizing such identities can help with understanding and writing code using cleave combinators:"
17 ": 2dup [ ] [ ] 2bi ;"
18 ": 3dup [ ] [ ] 3bi ;"
20 ": tuck [ nip ] [ ] 2bi ;"
21 ": swap [ nip ] [ drop ] 2bi ;"
23 ": over [ ] [ drop ] 2bi ;"
24 ": pick [ ] [ 2drop ] 3bi ;"
25 ": 2over [ ] [ drop ] 3bi ;"
28 ARTICLE: "cleave-combinators" "Cleave combinators"
29 "The cleave combinators apply multiple quotations to a single value."
43 "An array of quotations:"
50 "Technically, the cleave combinators are redundant because they can be simulated using shuffle words and other combinators, and in addition, they do not reduce token counts by much, if at all. However, they can make code more readable by expressing intention and exploiting any inherent symmetry. For example, a piece of code which performs three operations on the top of the stack can be written in one of two ways:"
52 "! First alternative; uses keep"
56 "! Second alternative: uses tri"
61 "The latter is more aesthetically pleasing than the former."
63 { $subsections "cleave-shuffle-equivalence" } ;
65 ARTICLE: "spread-shuffle-equivalence" "Expressing shuffle words with spread combinators"
66 "Spread combinators are defined in terms of shuffle words, and mappings from certain shuffle idioms to spread combinators are discussed in the documentation for " { $link bi* } ", " { $link 2bi* } ", " { $link tri* } ", and " { $link 2tri* } "."
68 "Certain shuffle words can also be expressed in terms of the spread combinators. Internalizing such identities can help with understanding and writing code using spread combinators:"
71 ": 2dip [ ] [ ] tri* ;"
73 ": nip [ drop ] [ ] bi* ;"
74 ": 2nip [ drop ] [ drop ] [ ] tri* ;"
77 " [ [ drop ] [ ] [ drop ] tri* ]"
78 " [ [ drop ] [ drop ] [ ] tri* ]"
79 " [ [ ] [ drop ] [ drop ] tri* ]"
83 " [ [ drop ] [ drop ] [ ] tri* ]"
84 " [ [ ] [ drop ] [ drop ] tri* ]"
85 " [ [ drop ] [ ] [ drop ] tri* ]"
89 " [ [ drop ] [ drop ] [ ] tri* ]"
90 " [ [ drop ] [ ] [ drop ] tri* ]"
91 " [ [ ] [ drop ] [ drop ] tri* ]"
95 ARTICLE: "spread-combinators" "Spread combinators"
96 "The spread combinators apply multiple quotations to multiple values. In this case, " { $snippet "*" } " suffix signify spreading."
99 { $subsections bi* 2bi* }
101 { $subsections tri* 2tri* }
102 "An array of quotations:"
103 { $subsections spread }
104 "Technically, the spread combinators are redundant because they can be simulated using shuffle words and other combinators, and in addition, they do not reduce token counts by much, if at all. However, they can make code more readable by expressing intention and exploiting any inherent symmetry. For example, a piece of code which performs three operations on three related values can be written in one of two ways:"
106 "! First alternative; uses dip"
107 "[ [ 1 + ] dip 1 - ] dip 2 *"
108 "! Second alternative: uses tri*"
109 "[ 1 + ] [ 1 - ] [ 2 * ] tri*"
111 "A generalization of the above combinators to any number of quotations can be found in " { $link "combinators" } "."
113 { $subsections "spread-shuffle-equivalence" } ;
115 ARTICLE: "apply-combinators" "Apply combinators"
116 "The apply combinators apply a single quotation to multiple values. The " { $snippet "@" } " suffix signifies application."
119 { $subsections bi@ 2bi@ }
121 { $subsections tri@ 2tri@ }
122 "A pair of utility words built from " { $link bi@ } ":"
123 { $subsections both? either? } ;
125 ARTICLE: "retainstack-combinators" "Retain stack combinators"
126 "Sometimes an additional storage area is needed to hold objects. The " { $emphasis "retain stack" } " is an auxilliary stack for this purpose. Objects can be moved between the data and retain stacks using a set of combinators."
128 "The dip combinators invoke the quotation at the top of the stack, hiding the values underneath:"
129 { $subsections dip 2dip 3dip 4dip }
130 "The keep combinators invoke a quotation which takes a number of values off the stack, and then they restore those values:"
131 { $subsections keep 2keep 3keep } ;
133 ARTICLE: "curried-dataflow" "Curried dataflow combinators"
134 "Curried cleave combinators:"
135 { $subsections bi-curry tri-curry }
136 "Curried spread combinators:"
137 { $subsections bi-curry* tri-curry* }
138 "Curried apply combinators:"
139 { $subsections bi-curry@ tri-curry@ }
140 { $see-also "dataflow-combinators" } ;
142 ARTICLE: "compositional-examples" "Examples of compositional combinator usage"
143 "Consider printing the same message ten times:"
144 { $code ": print-10 ( -- ) 10 [ \"Hello, world.\" print ] times ;" }
145 "if we wanted to abstract out the message into a parameter, we could keep it on the stack between iterations:"
146 { $code ": print-10 ( message -- ) 10 [ dup print ] times drop ;" }
147 "However, keeping loop-invariant values on the stack doesn't always work out nicely. For example, a word to subtract a value from each element of a sequence:"
148 { $code ": subtract-n ( seq n -- seq' ) swap [ over - ] map nip ;" }
149 "Three shuffle words are required to pass the value around. Instead, the loop-invariant value can be partially applied to a quotation using " { $link curry } ", yielding a new quotation that is passed to " { $link map } ":"
151 ": subtract-n ( seq n -- seq' ) [ - ] curry map ;"
152 "{ 10 20 30 } 5 subtract-n ."
155 "Now consider the word that is dual to the one above; instead of subtracting " { $snippet "n" } " from each stack element, it subtracts each element from " { $snippet "n" } "."
157 "One way to write this is with a pair of " { $link swap } "s:"
158 { $code ": n-subtract ( n seq -- seq' ) swap [ swap - ] curry map ;" }
159 "Since this pattern comes up often, " { $link with } " encapsulates it:"
161 ": n-subtract ( n seq -- seq' ) [ - ] with map ;"
162 "30 { 10 20 30 } n-subtract ."
165 { $see-also "fry.examples" } ;
167 ARTICLE: "compositional-combinators" "Compositional combinators"
168 "Certain combinators transform quotations to produce a new quotation."
169 { $subsections "compositional-examples" }
170 "Fundamental operations:"
171 { $subsections curry compose }
172 "Derived operations:"
173 { $subsections 2curry 3curry with prepose }
174 "These operations run in constant time, and in many cases are optimized out altogether by the " { $link "compiler" } ". " { $link "fry" } " are an abstraction built on top of these operations, and code that uses this abstraction is often clearer than direct calls to the below words."
176 "Curried dataflow combinators can be used to build more complex dataflow by combining cleave, spread and apply patterns in various ways."
177 { $subsections "curried-dataflow" }
178 "Quotations also implement the sequence protocol, and can be manipulated with sequence words; see " { $link "quotations" } ". However, such runtime quotation manipulation will not be optimized by the optimizing compiler." ;
180 ARTICLE: "booleans" "Booleans"
181 "In Factor, any object that is not " { $link f } " has a true value, and " { $link f } " has a false value. The " { $link t } " object is the canonical true value."
183 "A union class of the above:"
184 { $subsections boolean }
185 "There are some logical operations on booleans:"
193 "Boolean values are most frequently used for " { $link "conditionals" } "."
194 { $heading "The f object and f class" }
195 "The " { $link f } " object is the unique instance of the " { $link f } " class; the two are distinct objects. The latter is also a parsing word which adds the " { $link f } " object to the parse tree at parse time. To refer to the class itself you must use " { $link POSTPONE: POSTPONE: } " or " { $link POSTPONE: \ } " to prevent the parsing word from executing."
197 "Here is the " { $link f } " object:"
198 { $example "f ." "f" }
199 "Here is the " { $link f } " class:"
200 { $example "\\ f ." "POSTPONE: f" }
201 "They are not equal:"
202 { $example "f \\ f = ." "f" }
203 "Here is an array containing the " { $link f } " object:"
204 { $example "{ f } ." "{ f }" }
205 "Here is an array containing the " { $link f } " class:"
206 { $example "{ POSTPONE: f } ." "{ POSTPONE: f }" }
207 "The " { $link f } " object is an instance of the " { $link f } " class:"
208 { $example "USE: classes" "f class ." "POSTPONE: f" }
209 "The " { $link f } " class is an instance of " { $link word } ":"
210 { $example "USE: classes" "\\ f class ." "word" }
211 "On the other hand, " { $link t } " is just a word, and there is no class which it is a unique instance of."
212 { $example "t \\ t eq? ." "t" }
213 "Many words which search collections confuse the case of no element being present with an element being found equal to " { $link f } ". If this distinction is imporant, there is usually an alternative word which can be used; for example, compare " { $link at } " with " { $link at* } "." ;
215 ARTICLE: "conditionals-boolean-equivalence" "Expressing conditionals with boolean logic"
216 "Certain simple conditional forms can be expressed in a simpler manner using boolean logic."
218 "The following two lines are equivalent:"
219 { $code "[ drop f ] unless" "swap and" }
220 "The following two lines are equivalent:"
221 { $code "[ ] [ ] ?if" "swap or" }
222 "The following two lines are equivalent, where " { $snippet "L" } " is a literal:"
223 { $code "[ L ] unless*" "L or" } ;
225 ARTICLE: "conditionals" "Conditional combinators"
226 "The basic conditionals:"
227 { $subsections if when unless }
228 "Forms abstracting a common stack shuffle pattern:"
229 { $subsections if* when* unless* }
230 "Another form abstracting a common stack shuffle pattern:"
232 "Sometimes instead of branching, you just need to pick one of two values:"
234 "Two combinators which abstract out nested chains of " { $link if } ":"
235 { $subsections cond case }
236 { $subsections "conditionals-boolean-equivalence" }
237 { $see-also "booleans" "bitwise-arithmetic" both? either? } ;
239 ARTICLE: "dataflow-combinators" "Data flow combinators"
240 "Data flow combinators pass values between quotations:"
242 "retainstack-combinators"
247 { $see-also "curried-dataflow" } ;
249 ARTICLE: "combinators-quot" "Quotation construction utilities"
250 "Some words for creating quotations which can be useful for implementing method combinations and compiler transforms:"
251 { $subsections cond>quot case>quot alist>quot } ;
253 ARTICLE: "call-unsafe" "Unsafe combinators"
254 "Unsafe calls declare an effect statically without any runtime checking:"
255 { $subsections call-effect-unsafe execute-effect-unsafe } ;
257 ARTICLE: "call" "Fundamental combinators"
258 "The most basic combinators are those that take either a quotation or word, and invoke it immediately."
260 "There are two sets of combinators; they differ in whether or not the stack effect of the expected code is declared."
262 "The simplest combinators do not take an effect declaration. The compiler checks the stack effect at compile time, rejecting the program if this cannot be done:"
263 { $subsections call execute }
264 "The second set of combinators takes an effect declaration. Note that the opening parenthesis is actually part of the word name; these are parsing words, and they read a stack effect until the corresponding closing parenthesis. The stack effect of the quotation or word is then checked at runtime:"
265 { $subsections POSTPONE: call( POSTPONE: execute( }
266 "The above are syntax sugar. The underlying words are a bit more verbose but allow non-constant effects to be passed in:"
267 { $subsections call-effect execute-effect }
268 "The combinator variants that do not take an effect declaration can only be used if the compiler is able to infer the stack effect by other means. See " { $link "inference-combinators" } "."
269 { $subsections "call-unsafe" }
270 { $see-also "effects" "inference" } ;
272 ARTICLE: "combinators" "Combinators"
273 "A central concept in Factor is that of a " { $emphasis "combinator" } ", which is a word taking code as input."
276 "dataflow-combinators"
278 "looping-combinators"
279 "compositional-combinators"
280 "combinators.short-circuit"
285 "More combinators are defined for working on data structures, such as " { $link "sequences-combinators" } " and " { $link "assocs-combinators" } "."
286 { $see-also "quotations" } ;
291 { $values { "quot" quotation } { "effect" effect } }
292 { $description "Given a quotation and a stack effect, calls the quotation, asserting at runtime that it has the given stack effect. This is a macro which expands given a literal effect parameter, and an arbitrary quotation which is not required at compile time." }
294 "The following two lines are equivalent:"
297 "(( a b -- c )) call-effect"
302 { $values { "word" word } { "effect" effect } }
303 { $description "Given a word and a stack effect, executes the word, asserting at runtime that it has the given stack effect. This is a macro which expands given a literal effect parameter, and an arbitrary word which is not required at compile time." }
305 "The following two lines are equivalent:"
307 "execute( a b -- c )"
308 "(( a b -- c )) execute-effect"
312 HELP: execute-effect-unsafe
313 { $values { "word" word } { "effect" effect } }
314 { $description "Given a word and a stack effect, executes the word, blindly declaring at runtime that it has the given stack effect. This is a macro which expands given a literal effect parameter, and an arbitrary word which is not required at compile time." }
315 { $warning "If the word being executed has an incorrect stack effect, undefined behavior will result. User code should use " { $link POSTPONE: execute( } " instead." } ;
317 { call-effect call-effect-unsafe execute-effect execute-effect-unsafe } related-words
320 { $values { "x" object } { "seq" "a sequence of quotations with stack effect " { $snippet "( x -- ... )" } } }
321 { $description "Applies each quotation to the object in turn." }
323 "The " { $link bi } " combinator takes one value and two quotations; the " { $link tri } " combinator takes one value and three quotations. The " { $link cleave } " combinator takes one value and any number of quotations, and is essentially equivalent to a chain of " { $link keep } " forms:"
326 "{ [ p ] [ q ] [ r ] [ s ] } cleave"
327 "[ p ] keep [ q ] keep [ r ] keep s"
332 { $values { "x" object } { "y" object }
333 { "seq" "a sequence of quotations with stack effect " { $snippet "( x y -- ... )" } } }
334 { $description "Applies each quotation to the two objects in turn." } ;
337 { $values { "x" object } { "y" object } { "z" object }
338 { "seq" "a sequence of quotations with stack effect " { $snippet "( x y z -- ... )" } } }
339 { $description "Applies each quotation to the three objects in turn." } ;
341 { bi tri cleave } related-words
344 { $values { "objs..." "objects" } { "seq" "a sequence of quotations with stack effect " { $snippet "( x -- ... )" } } }
345 { $description "Applies each quotation to the object in turn." }
347 "The " { $link bi* } " combinator takes two values and two quotations; the " { $link tri* } " combinator takes three values and three quotations. The " { $link spread } " combinator takes " { $snippet "n" } " values and " { $snippet "n" } " quotations, where " { $snippet "n" } " is the length of the input sequence, and is essentially equivalent to series of retain stack manipulations:"
350 "{ [ p ] [ q ] [ r ] [ s ] } spread"
351 "[ [ [ p ] dip q ] dip r ] dip s"
355 { bi* tri* spread } related-words
358 { $values { "object" object } { "quot" { $quotation "( object(n) -- object(n+1) )" } } { "object(n)" object } }
359 { $description "Applies the quotation repeatedly with " { $snippet "object" } " as the initial input until the output of the quotation equals the input." }
362 "USING: combinators kernel math prettyprint sequences ;"
364 ": flatten ( sequence -- sequence' )"
365 " \"flatten\" over index"
366 " [ [ 1 + swap nth ] [ nip dup 2 + ] [ drop ] 2tri replace-slice ] when* ;"
368 "{ \"flatten\" { 1 { 2 3 } \"flatten\" { 4 5 } { 6 } } } [ flatten ] to-fixed-point ."
369 "{ 1 { 2 3 } 4 5 { 6 } }"
374 { $values { "default" "a quotation" } { "assoc" "a sequence of quotation pairs" } { "quot" "a new quotation" } }
375 { $description "Constructs a quotation which calls the first quotation in each pair of " { $snippet "assoc" } " until one of them outputs a true value, and then calls the second quotation in the corresponding pair. Quotations are called in reverse order, and if no quotation outputs a true value then " { $snippet "default" } " is called." }
376 { $notes "This word is used to implement compile-time behavior for " { $link cond } ", and it is also used by the generic word system. Note that unlike " { $link cond } ", the constructed quotation performs the tests starting from the end and not the beginning." } ;
379 { $values { "assoc" "a sequence of quotation pairs and an optional quotation" } }
381 "Calls the second quotation in the first pair whose first quotation yields a true value. A single quotation will always yield a true value."
383 "The following two phrases are equivalent:"
384 { $code "{ { [ X ] [ Y ] } { [ Z ] [ T ] } } cond" }
385 { $code "X [ Y ] [ Z [ T ] [ no-cond ] if ] if" }
387 { $errors "Throws a " { $link no-cond } " error if none of the test quotations yield a true value." }
391 " { [ dup 0 > ] [ \"positive\" ] }"
392 " { [ dup 0 < ] [ \"negative\" ] }"
399 { $description "Throws a " { $link no-cond } " error." }
400 { $error-description "Thrown by " { $link cond } " if none of the test quotations yield a true value. Some uses of " { $link cond } " include a default case where the test quotation is " { $snippet "[ t ]" } "; such a " { $link cond } " form will never throw this error." } ;
403 { $values { "obj" object } { "assoc" "a sequence of object/word,quotation pairs, with an optional quotation at the end" } }
405 "Compares " { $snippet "obj" } " against the first element of every pair, first evaluating the first element if it is a word. If some pair matches, removes " { $snippet "obj" } " from the stack and calls the second element of that pair, which must be a quotation."
407 "If there is no case matching " { $snippet "obj" } ", the default case is taken. If the last element of " { $snippet "cases" } " is a quotation, the quotation is called with " { $snippet "obj" } " on the stack. Otherwise, a " { $link no-cond } " error is rasied."
409 "The following two phrases are equivalent:"
410 { $code "{ { X [ Y ] } { Z [ T ] } } case" }
411 { $code "dup X = [ drop Y ] [ dup Z = [ drop T ] [ no-case ] if ] if" }
415 "SYMBOL: yes SYMBOL: no SYMBOL: maybe"
417 " { yes [ ] } ! Do nothing"
418 " { no [ \"No way!\" throw ] }"
419 " { maybe [ \"Make up your mind!\" print ] }"
420 " [ \"Invalid input; try again.\" print ]"
426 { $description "Throws a " { $link no-case } " error." }
427 { $error-description "Thrown by " { $link case } " if the object at the top of the stack does not match any case, and no default case is given." } ;
429 HELP: recursive-hashcode
430 { $values { "n" integer } { "obj" object } { "quot" { $quotation "( n obj -- code )" } } { "code" integer } }
431 { $description "A combinator used to implement methods for the " { $link hashcode* } " generic word. If " { $snippet "n" } " is less than or equal to zero, outputs 0, otherwise calls the quotation." } ;
434 { $values { "assoc" "a sequence of pairs of quotations" } { "quot" quotation } }
435 { $description "Creates a quotation that when called, has the same effect as applying " { $link cond } " to " { $snippet "assoc" } "."
437 "The generated quotation is more efficient than the naive implementation of " { $link cond } ", though, since it expands into a series of conditionals, and no iteration through " { $snippet "assoc" } " has to be performed." }
438 { $notes "This word is used behind the scenes to compile " { $link cond } " forms efficiently; it can also be called directly, which is useful for meta-programming." } ;
441 { $values { "assoc" "a sequence of pairs of quotations" } { "default" quotation } { "quot" quotation } }
442 { $description "Creates a quotation that when called, has the same effect as applying " { $link case } " to " { $snippet "assoc" } "."
444 "This word uses three strategies:"
446 "If the assoc only has a few keys, a linear search is generated."
447 { "If the assoc has a large number of keys which form a contiguous range of integers, a direct dispatch is generated using the " { $link dispatch } " word together with a bounds check." }
448 "Otherwise, an open-coded hashtable dispatch is generated."
451 HELP: distribute-buckets
452 { $values { "alist" "an alist" } { "initial" object } { "quot" { $quotation "( obj -- assoc )" } } { "buckets" "a new array" } }
453 { $description "Sorts the entries of " { $snippet "assoc" } " into buckets, using the quotation to yield a set of keys for each entry. The hashcode of each key is computed, and the entry is placed in all corresponding buckets. Each bucket is initially cloned from " { $snippet "initial" } "; this should either be an empty vector or a one-element vector containing a pair." }
454 { $notes "This word is used in the implemention of " { $link hash-case-quot } " and " { $link standard-combination } "." } ;
456 HELP: dispatch ( n array -- )
457 { $values { "n" "a fixnum" } { "array" "an array of quotations" } }
458 { $description "Calls the " { $snippet "n" } "th quotation in the array." }
459 { $warning "This word is in the " { $vocab-link "kernel.private" } " vocabulary because it is an implementation detail used by the generic word system to accelerate method dispatch. It does not perform type or bounds checks, and user code should not need to call it directly." } ;