]> gitweb.factorcode.org Git - factor.git/blob - core/combinators/combinators-docs.factor
Create basis vocab root
[factor.git] / core / combinators / combinators-docs.factor
1 USING: arrays help.markup help.syntax strings sbufs vectors
2 kernel quotations generic generic.standard classes
3 math assocs sequences sequences.private ;
4 IN: combinators
5
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 } ;
11
12 ARTICLE: "combinators" "Additional combinators"
13 "The " { $vocab-link "combinators" } " vocabulary provides a few useful combinators."
14 $nl
15 "A looping combinator:"
16 { $subsection while }
17 "Generalization of " { $link bi } " and " { $link tri } ":"
18 { $subsection cleave }
19 "Generalization of " { $link bi* } " and " { $link tri* } ":"
20 { $subsection spread }
21 "Two combinators which abstract out nested chains of " { $link if } ":"
22 { $subsection cond }
23 { $subsection case }
24 "The " { $vocab-link "combinators" } " also provides some less frequently-used features."
25 $nl
26 "A combinator which can help with implementing methods on " { $link hashcode* } ":"
27 { $subsection recursive-hashcode }
28 { $subsection "assertions" }
29 { $subsection "combinators-quot" }
30 { $see-also "quotations" "dataflow" } ;
31
32 ARTICLE: "assertions" "Assertions"
33 "Some words to make assertions easier to enforce:"
34 { $subsection assert }
35 { $subsection assert= }
36 "Runtime stack depth checking:"
37 { $subsection assert-depth } ;
38
39 ABOUT: "combinators"
40
41 HELP: cleave
42 { $values { "x" object } { "seq" "a sequence of quotations with stack effect " { $snippet "( x -- ... )" } } }
43 { $description "Applies each quotation to the object in turn." }
44 { $examples
45     "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:"
46     { $code
47         "! Equivalent"
48         "{ [ p ] [ q ] [ r ] [ s ] } cleave"
49         "[ p ] keep [ q ] keep [ r ] keep s"
50     }
51 } ;
52
53 { bi tri cleave } related-words
54
55 HELP: spread
56 { $values { "objs..." "objects" } { "seq" "a sequence of quotations with stack effect " { $snippet "( x -- ... )" } } }
57 { $description "Applies each quotation to the object in turn." }
58 { $examples
59     "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:"
60     { $code
61         "! Equivalent"
62         "{ [ p ] [ q ] [ r ] [ s ] } spread"
63         ">r >r >r p r> q r> r r> s"
64     }
65 } ;
66
67 { bi* tri* spread } related-words
68
69 HELP: alist>quot
70 { $values { "default" "a quotation" } { "assoc" "a sequence of quotation pairs" } { "quot" "a new quotation" } }
71 { $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." }
72 { $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." } ;
73
74 HELP: cond
75 { $values { "assoc" "a sequence of quotation pairs and an optional quotation" } }
76 { $description
77     "Calls the second quotation in the first pair whose first quotation yields a true value. A single quotation will always yield a true value."
78     $nl
79     "The following two phrases are equivalent:"
80     { $code "{ { [ X ] [ Y ] } { [ Z ] [ T ] } } cond" }
81     { $code "X [ Y ] [ Z [ T ] [ no-cond ] if ] if" }
82 }
83 { $errors "Throws a " { $link no-cond } " error if none of the test quotations yield a true value." }
84 { $examples
85     { $code
86         "{"
87         "    { [ dup 0 > ] [ \"positive\" ] }"
88         "    { [ dup 0 < ] [ \"negative\" ] }"
89         "    [ \"zero\" ]"
90         "} cond"
91     }
92 } ;
93
94 HELP: no-cond
95 { $description "Throws a " { $link no-cond } " error." }
96 { $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." } ;
97
98 HELP: case
99 { $values { "obj" object } { "assoc" "a sequence of object/word,quotation pairs, with an optional quotation at the end" } }
100 { $description
101     "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."
102     $nl
103     "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."
104     $nl
105     "The following two phrases are equivalent:"
106     { $code "{ { X [ Y ] } { Z [ T ] } } case" }
107     { $code "dup X = [ drop Y ] [ dup Z = [ drop T ] [ no-case ] if ] if" }
108 }
109 { $examples
110     { $code
111         "SYMBOL: yes  SYMBOL: no  SYMBOL: maybe"
112         "maybe {"
113         "    { yes [ ] } ! Do nothing"
114         "    { no [ \"No way!\" throw ] }"
115         "    { maybe [ \"Make up your mind!\" print ] }"
116         "    [ \"Invalid input; try again.\" print ]"
117         "} case"
118     }
119 } ;
120
121 HELP: no-case
122 { $description "Throws a " { $link no-case } " error." }
123 { $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." } ;
124
125 HELP: recursive-hashcode
126 { $values { "n" integer } { "obj" object } { "quot" "a quotation with stack effect " { $snippet "( n obj -- code )" } } { "code" integer } }
127 { $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." } ;
128
129 HELP: cond>quot
130 { $values { "assoc" "a sequence of pairs of quotations" } { "quot" quotation } }
131 { $description  "Creates a quotation that when called, has the same effect as applying " { $link cond } " to " { $snippet "assoc" } "."
132 $nl
133 "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." }
134 { $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." } ;
135
136 HELP: case>quot
137 { $values { "assoc" "a sequence of pairs of quotations" } { "default" quotation } { "quot" quotation } }
138 { $description "Creates a quotation that when called, has the same effect as applying " { $link case } " to " { $snippet "assoc" } "."
139 $nl
140 "This word uses three strategies:"
141 { $list
142     "If the assoc only has a few keys, a linear search is generated."
143     { "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." }
144     "Otherwise, an open-coded hashtable dispatch is generated."
145 } } ;
146
147 HELP: distribute-buckets
148 { $values { "assoc" "an alist" } { "initial" object } { "quot" "a quotation with stack effect " { $snippet "( obj -- assoc )" } } { "buckets" "a new array" } }
149 { $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." }
150 { $notes "This word is used in the implemention of " { $link hash-case-quot } " and " { $link standard-combination } "." } ;
151
152 HELP: dispatch ( n array -- )
153 { $values { "n" "a fixnum" } { "array" "an array of quotations" } }
154 { $description "Calls the " { $snippet "n" } "th quotation in the array." }
155 { $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." } ;
156
157 HELP: assert-depth
158 { $values { "quot" "a quotation" } }
159 { $description "Runs a quotation. Throws an error if the total number of elements on the stack is not the same before and after the quotation runs." } ;