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