1 USING: arrays help.markup help.syntax strings sbufs vectors
2 kernel quotations generic generic.standard classes
3 math assocs sequences sequences.private ;
6 ARTICLE: "combinators-quot" "Quotation construction utilities"
7 "Some words for creating quotations which can be useful for implementing method combinations and compiler transforms:"
8 { $subsection cond>quot }
9 { $subsection case>quot }
10 { $subsection alist>quot } ;
12 ARTICLE: "combinators" "Additional combinators"
13 "The " { $vocab-link "combinators" } " vocabulary provides a few useful combinators."
15 "Generalization of " { $link bi } " and " { $link tri } ":"
16 { $subsection cleave }
17 "Generalization of " { $link 2bi } " and " { $link 2tri } ":"
18 { $subsection 2cleave }
19 "Generalization of " { $link 3bi } " and " { $link 3tri } ":"
20 { $subsection 3cleave }
21 "Generalization of " { $link bi* } " and " { $link tri* } ":"
22 { $subsection spread }
23 "Two combinators which abstract out nested chains of " { $link if } ":"
26 "The " { $vocab-link "combinators" } " also provides some less frequently-used features."
28 "A combinator which can help with implementing methods on " { $link hashcode* } ":"
29 { $subsection recursive-hashcode }
30 { $subsection "combinators-quot" }
31 { $see-also "quotations" "dataflow" } ;
36 { $values { "x" object } { "seq" "a sequence of quotations with stack effect " { $snippet "( x -- ... )" } } }
37 { $description "Applies each quotation to the object in turn." }
39 "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:"
42 "{ [ p ] [ q ] [ r ] [ s ] } cleave"
43 "[ p ] keep [ q ] keep [ r ] keep s"
48 { $values { "x" object } { "y" object }
49 { "seq" "a sequence of quotations with stack effect " { $snippet "( x y -- ... )" } } }
50 { $description "Applies each quotation to the two objects in turn." } ;
53 { $values { "x" object } { "y" object } { "z" object }
54 { "seq" "a sequence of quotations with stack effect " { $snippet "( x y z -- ... )" } } }
55 { $description "Applies each quotation to the three objects in turn." } ;
57 { bi tri cleave } related-words
60 { $values { "objs..." "objects" } { "seq" "a sequence of quotations with stack effect " { $snippet "( x -- ... )" } } }
61 { $description "Applies each quotation to the object in turn." }
63 "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:"
66 "{ [ p ] [ q ] [ r ] [ s ] } spread"
67 "[ [ [ p ] dip q ] dip r ] dip s"
71 { bi* tri* spread } related-words
74 { $values { "default" "a quotation" } { "assoc" "a sequence of quotation pairs" } { "quot" "a new quotation" } }
75 { $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." }
76 { $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." } ;
79 { $values { "assoc" "a sequence of quotation pairs and an optional quotation" } }
81 "Calls the second quotation in the first pair whose first quotation yields a true value. A single quotation will always yield a true value."
83 "The following two phrases are equivalent:"
84 { $code "{ { [ X ] [ Y ] } { [ Z ] [ T ] } } cond" }
85 { $code "X [ Y ] [ Z [ T ] [ no-cond ] if ] if" }
87 { $errors "Throws a " { $link no-cond } " error if none of the test quotations yield a true value." }
91 " { [ dup 0 > ] [ \"positive\" ] }"
92 " { [ dup 0 < ] [ \"negative\" ] }"
99 { $description "Throws a " { $link no-cond } " error." }
100 { $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." } ;
103 { $values { "obj" object } { "assoc" "a sequence of object/word,quotation pairs, with an optional quotation at the end" } }
105 "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."
107 "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."
109 "The following two phrases are equivalent:"
110 { $code "{ { X [ Y ] } { Z [ T ] } } case" }
111 { $code "dup X = [ drop Y ] [ dup Z = [ drop T ] [ no-case ] if ] if" }
115 "SYMBOL: yes SYMBOL: no SYMBOL: maybe"
117 " { yes [ ] } ! Do nothing"
118 " { no [ \"No way!\" throw ] }"
119 " { maybe [ \"Make up your mind!\" print ] }"
120 " [ \"Invalid input; try again.\" print ]"
126 { $description "Throws a " { $link no-case } " error." }
127 { $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." } ;
129 HELP: recursive-hashcode
130 { $values { "n" integer } { "obj" object } { "quot" { $quotation "( n obj -- code )" } } { "code" integer } }
131 { $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." } ;
134 { $values { "assoc" "a sequence of pairs of quotations" } { "quot" quotation } }
135 { $description "Creates a quotation that when called, has the same effect as applying " { $link cond } " to " { $snippet "assoc" } "."
137 "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." }
138 { $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." } ;
141 { $values { "assoc" "a sequence of pairs of quotations" } { "default" quotation } { "quot" quotation } }
142 { $description "Creates a quotation that when called, has the same effect as applying " { $link case } " to " { $snippet "assoc" } "."
144 "This word uses three strategies:"
146 "If the assoc only has a few keys, a linear search is generated."
147 { "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." }
148 "Otherwise, an open-coded hashtable dispatch is generated."
151 HELP: distribute-buckets
152 { $values { "alist" "an alist" } { "initial" object } { "quot" { $quotation "( obj -- assoc )" } } { "buckets" "a new array" } }
153 { $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." }
154 { $notes "This word is used in the implemention of " { $link hash-case-quot } " and " { $link standard-combination } "." } ;
156 HELP: dispatch ( n array -- )
157 { $values { "n" "a fixnum" } { "array" "an array of quotations" } }
158 { $description "Calls the " { $snippet "n" } "th quotation in the array." }
159 { $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." } ;