]> gitweb.factorcode.org Git - factor.git/blob - basis/help/handbook/handbook.factor
6296eb5ab126cc67053ed9a0a27e7dace761c7c2
[factor.git] / basis / help / handbook / handbook.factor
1 USING: help help.markup help.syntax help.definitions help.topics
2 namespaces words sequences classes assocs vocabs kernel arrays
3 prettyprint.backend prettyprint.custom kernel.private io generic
4 math system strings sbufs vectors byte-arrays quotations
5 io.streams.byte-array classes.builtin parser lexer
6 classes.predicate classes.union classes.intersection
7 classes.singleton classes.tuple help.vocabs math.parser
8 accessors definitions sets ;
9 IN: help.handbook
10
11 ARTICLE: "conventions" "Conventions"
12 "Various conventions are used throughout the Factor documentation and source code."
13 { $heading "Glossary of terms" }
14 "Common terminology and abbreviations used throughout Factor and its documentation:"
15 { $table
16     { "Term" "Definition" }
17     { "alist" { "an association list; see " { $link "alists" } } }
18     { "assoc" { "an associative mapping; see " { $link "assocs" } } }
19     { "associative mapping" { "an object whose class implements the " { $link "assocs-protocol" } } }
20     { "boolean"               { { $link t } " or " { $link f } } }
21     { "class"                 { "a set of objects identified by a " { $emphasis "class word" } " together with a discriminating predicate. See " { $link "classes" } } }
22     { "combinator"            { "a word taking a quotation or another word as input; a higher-order function. See " { $link "combinators" } } }
23     { "definition specifier"  { "an instance of " { $link definition } " which implements the " { $link "definition-protocol" } } }
24     { "generalized boolean"   { "an object interpreted as a boolean; a value of " { $link f } " denotes false and anything else denotes true" } }
25     { "generic word"          { "a word whose behavior depends can be specialized on the class of one of its inputs. See " { $link "generic" } } }
26     { "method"                { "a specialized behavior of a generic word on a class. See " { $link "generic" } } }
27     { "object"                { "any datum which can be identified" } }
28     { "ordering specifier"    { "see " { $link "order-specifiers" } } }
29     { "pathname string"       { "an OS-specific pathname which identifies a file" } }
30     { "quotation"             { "an anonymous function; an instance of the " { $link quotation } " class. More generally, instances of the " { $link callable } " class can be used in many places documented to expect quotations" } }
31     { "sequence" { "a sequence; see " { $link "sequence-protocol" } } }
32     { "slot"                  { "a component of an object which can store a value" } }
33     { "stack effect"          { "a pictorial representation of a word's inputs and outputs, for example " { $snippet "+ ( x y -- z )" } ". See " { $link "effects" } } }
34     { "true value"            { "any object not equal to " { $link f } } }
35     { { "vocabulary " { $strong "or" } " vocab" } { "a named set of words. See " { $link "vocabularies" } } }
36     { "vocabulary specifier"  { "a " { $link vocab } ", " { $link vocab-link } " or a string naming a vocabulary" } }
37     { "word"                  { "the basic unit of code, analogous to a function or procedure in other programming languages. See " { $link "words" } } }
38
39 { $heading "Documentation conventions" }
40 "Factor documentation consists of two distinct bodies of text. There is a hierarchy of articles, much like this one, and there is word documentation. Help articles reference word documentation, and vice versa, but not every documented word is referenced from some help article."
41 $nl
42 "The browser, completion popups and other tools use a common set of " { $link "definitions.icons" } "."
43 $nl
44 "Every article has links to parent articles at the top. Explore these if the article you are reading is too specific."
45 $nl
46 "Some generic words have " { $strong "Description" } " headings, and others have " { $strong "Contract" } " headings. A distinction is made between words which are not intended to be extended with user-defined methods, and those that are."
47 { $heading "Vocabulary naming conventions" }
48 "A vocabulary name ending in " { $snippet ".private" } " contains words which are either implementation details, unsafe, or both. For example, the " { $snippet "sequences.private" } " vocabulary contains words which access sequence elements without bounds checking (" { $link "sequences-unsafe" } "). You should avoid using private words from the Factor library unless absolutely necessary. Similarly, your own code can place words in private vocabularies using " { $link POSTPONE: <PRIVATE } " if you do not want other people using them without good reason."
49 { $heading "Word naming conventions" }
50 "These conventions are not hard and fast, but are usually a good first step in understanding a word's behavior:"
51 { $table
52     { "General form" "Description" "Examples" }
53     { { $snippet { $emphasis "foo" } "?" } "outputs a boolean" { { $link empty? } } }
54     { { $snippet { $emphasis "foo" } "!" } { "a variant of " { $snippet "foo" } " which mutates one of its arguments" } { { $link append! } } }
55     { { $snippet "?" { $emphasis "foo" } } { "conditionally performs " { $snippet { $emphasis "foo" } } } { { $links ?nth } } }
56     { { $snippet "<" { $emphasis "foo" } ">" } { "creates a new " { $snippet "foo" } } { { $link <array> } } }
57     { { $snippet ">" { $emphasis "foo" } } { "converts the top of the stack into a " { $snippet "foo" } } { { $link >array } } }
58     { { $snippet { $emphasis "foo" } ">" { $emphasis "bar" } } { "converts a " { $snippet "foo" } " into a " { $snippet "bar" } } { { $link number>string } } }
59     { { $snippet "new-" { $emphasis "foo" } } { "creates a new " { $snippet "foo" } ", taking some kind of parameter from the stack which determines the type of the object to be created" } { { $link new-sequence } ", " { $link new-lexer } ", " { $link new } } }
60     { { $snippet { $emphasis "foo" } "*" } { "alternative form of " { $snippet "foo" } ", or a generic word called by " { $snippet "foo" } } { { $links at* pprint* } } }
61     { { $snippet "(" { $emphasis "foo" } ")" } { "implementation detail word used by " { $snippet "foo" } } { { $link (clone) } } }
62     { { $snippet "set-" { $emphasis "foo" } } { "sets " { $snippet "foo" } " to a new value" } { $links set-length } }
63     { { $snippet { $emphasis "foo" } ">>" } { "gets the " { $snippet "foo" } " slot of the tuple at the top of the stack; see " { $link "accessors" } } { { $link name>> } } }
64     { { $snippet ">>" { $emphasis "foo" } } { "sets the " { $snippet "foo" } " slot of the tuple at the top of the stack; see " { $link "accessors" } } { { $link >>name } } }
65     { { $snippet "with-" { $emphasis "foo" } } { "performs some kind of initialization and cleanup related to " { $snippet "foo" } ", usually in a new dynamic scope" } { $links with-scope with-input-stream with-output-stream } }
66     { { $snippet "$" { $emphasis "foo" } } { "help markup" } { $links $heading $emphasis } }
67 }
68 { $heading "Stack effect conventions" }
69 "Stack effect conventions are documented in " { $link "effects" } "."
70 ;
71
72 ARTICLE: "tail-call-opt" "Tail-call optimization"
73 "If the last action performed is the execution of a word, the current quotation is not saved on the call stack; this is known as " { $emphasis "tail-call optimization" } " and the Factor implementation guarantees that it will be performed."
74 $nl
75 "Tail-call optimization allows iterative algorithms to be implemented in an efficient manner using recursion, without the need for any kind of primitive looping construct in the language. However, in practice, most iteration is performed via combinators such as " { $link while } ", " { $link each } ", " { $link map } ", " { $link assoc-each } ", and so on. The definitions of these combinators do bottom-out in recursive words, however." ;
76
77 ARTICLE: "evaluator" "Stack machine model"
78 { $link "quotations" } " are evaluated sequentially from beginning to end. When the end is reached, the quotation returns to its caller. As each object in the quotation is evaluated in turn, an action is taken based on its type:"
79 { $list
80     { "a " { $link word } " - the word's definition quotation is called. See " { $link "words" } }
81     { "a " { $link wrapper } " - the wrapped object is pushed on the data stack. Wrappers are used to push word objects directly on the stack when they would otherwise execute. See the " { $link POSTPONE: \ } " parsing word." }
82     { "All other types of objects are pushed on the data stack." }
83 }
84 { $subsections "tail-call-opt" }
85 { $see-also "compiler" } ;
86
87 ARTICLE: "objects" "Objects"
88 "An " { $emphasis "object" } " is any datum which may be identified. All values are objects in Factor. Each object carries type information, and types are checked at runtime; Factor is dynamically typed."
89 { $subsections
90     "equality"
91     "math.order"
92     "classes"
93     "tuples"
94     "generic"
95 }
96 "Advanced features:"
97 { $subsections
98     "delegate"
99     "mirrors"
100     "slots"
101 } ;
102
103 ARTICLE: "numbers" "Numbers"
104 { $subsections
105     "arithmetic"
106     "math-constants"
107     "math-functions"
108     "number-strings"
109 }
110 "Number implementations:"
111 { $subsections
112     "integers"
113     "rationals"
114     "floats"
115     "complex-numbers"
116 }
117 "Advanced features:"
118 { $subsections
119     "math-vectors"
120     "math-intervals"
121 } ;
122
123 USE: io.buffers
124
125 ARTICLE: "collections" "Collections" 
126 { $heading "Sequences" }
127 { $subsections
128     "sequences"
129     "virtual-sequences"
130     "namespaces-make"
131 }
132 "Fixed-length sequences:"
133 { $subsections
134     "arrays"
135     "quotations"
136     "strings"
137     "byte-arrays"
138     "specialized-arrays"
139 }
140 "Resizable sequences:"
141 { $subsections
142     "vectors"
143     "byte-vectors"
144     "sbufs"
145     "growable"
146 }
147 { $heading "Associative mappings" }
148 { $subsections
149     "assocs"
150     "linked-assocs"
151     "biassocs"
152     "refs"
153 }
154 "Implementations:"
155 { $subsections
156     "hashtables"
157     "alists"
158     "enums"
159 }
160 { $heading "Double-ended queues" }
161 { $subsections "deques" }
162 "Implementations:"
163 { $subsections
164     "dlists"
165     "search-deques"
166 }
167 { $heading "Other collections" }
168 { $subsections
169     "sets"
170     "lists"
171     "disjoint-sets"
172     "interval-maps"
173     "heaps"
174     "boxes"
175     "graphs"
176     "buffers"
177 }
178 "There are also many other vocabularies tagged " { $link T{ vocab-tag { name "collections" } } } " in the library." ;
179
180 USING: io.encodings.utf8 io.encodings.binary io.files ;
181
182 ARTICLE: "encodings-introduction" "An introduction to encodings"
183 "In order to express text in terms of binary, some sort of encoding has to be used. In a modern context, this is understood as a two-way mapping between Unicode code points (characters) and some amount of binary. Since English isn't the only language in the world, ASCII is not sufficient as a mapping from binary to Unicode; it can't even express em-dashes or curly quotes. Unicode was designed as a universal character set that could potentially represent everything." $nl
184 "Not all encodings can represent all Unicode code points, but Unicode can represent basically everything that exists in modern encodings. Some encodings are language-specific, and some can represent everything in Unicode. Though the world is moving toward Unicode and UTF-8, the reality today is that there are several encodings which must be taken into account." $nl
185 "Factor uses a system of encoding descriptors to denote encodings. Encoding descriptors are objects which describe encodings. Examples are " { $link utf8 } " and " { $link binary } ". Encoding descriptors can be passed around independently. Each encoding descriptor has some method for constructing an encoded or decoded stream, and the resulting stream has an encoding descriptor stored which has methods for reading or writing characters." $nl
186 "Constructors for streams which deal with bytes usually take an encoding as an explicit parameter. For example, to open a text file for reading whose contents are in UTF-8, use the following"
187 { $code "\"file.txt\" utf8 <file-reader>" }
188 "If there is an error in the encoded stream, a replacement character (0xFFFD) will be inserted. To throw an exception upon error, use a strict encoding as follows"
189 { $code "USE: io.encodings.strict" "\"file.txt\" utf8 strict <file-reader>" }
190 "In a similar way, encodings can be specified when opening a file for writing."
191 { $code "USE: io.encodings.ascii" "\"file.txt\" ascii <file-writer>" }
192 "An encoding is also needed for some words that don't return streams, such as " { $link file-contents } ", for example"
193 { $code "USE: io.encodings.utf16" "\"file.txt\" utf16 file-contents" }
194 "Encoding descriptors are also used by " { $link "io.streams.byte-array" } " and taken by combinators like " { $link with-file-writer } " and " { $link with-byte-reader } " which deal with streams. It is " { $emphasis "not" } " used with " { $link "io.streams.string" } " because these deal with abstract text."
195 $nl
196 "When the " { $link binary } " encoding is used, a " { $link byte-array } " is expected for writing and returned for reading, since the stream deals with bytes. All other encodings deal with strings, since they are used to represent text." ;
197
198 ARTICLE: "io" "Input and output"
199 { $heading "Streams" }
200 { $subsections
201     "streams"
202     "io.files"
203 }
204 { $heading "The file system" }
205 { $subsections
206     "io.pathnames"
207     "io.files.info"
208     "io.files.links"
209     "io.directories"
210 }
211 { $heading "Encodings" }
212 { $subsections
213     "encodings-introduction"
214     "io.encodings"
215 }
216 { $heading "Wrapper streams" }
217 { $subsections
218     "io.streams.duplex"
219     "io.streams.plain"
220     "io.streams.string"
221     "io.streams.byte-array"
222 }
223 { $heading "Utilities" }
224 { $subsections
225     "stream-binary"
226     "io.styles"
227     "checksums"
228 }
229 { $heading "Implementation" }
230 { $subsections
231     "io.streams.c"
232     "io.ports"
233 }
234 { $see-also "destructors" } ;
235
236 ARTICLE: "article-index" "Article index"
237 { $index [ articles get keys ] } ;
238
239 ARTICLE: "primitive-index" "Primitive index"
240 { $index [ all-words [ primitive? ] filter ] } ;
241
242 ARTICLE: "error-index" "Error index"
243 { $index [ all-errors ] } ;
244
245 ARTICLE: "class-index" "Class index"
246 { $heading "Built-in classes" }
247 { $index [ classes [ builtin-class? ] filter ] }
248 { $heading "Tuple classes" }
249 { $index [ classes [ tuple-class? ] filter ] }
250 { $heading "Singleton classes" }
251 { $index [ classes [ singleton-class? ] filter ] }
252 { $heading "Union classes" }
253 { $index [ classes [ union-class? ] filter ] }
254 { $heading "Intersection classes" }
255 { $index [ classes [ intersection-class? ] filter ] }
256 { $heading "Predicate classes" }
257 { $index [ classes [ predicate-class? ] filter ] } ;
258
259 USING: help.cookbook help.tutorial ;
260
261 ARTICLE: "handbook-language-reference" "The language"
262 { $heading "Fundamentals" }
263 { $subsections
264     "conventions"
265     "syntax"
266 }
267 { $heading "The stack" }
268 { $subsections
269     "evaluator"
270     "effects"
271     "inference"
272 }
273 { $heading "Basic data types" }
274 { $subsections
275     "booleans"
276     "numbers"
277     "collections"
278 }
279 { $heading "Evaluation" }
280 { $subsections
281     "words"
282     "shuffle-words"
283     "combinators"
284     "threads"
285 }
286 { $heading "Named values" }
287 { $subsections
288     "locals"
289     "namespaces"
290     "namespaces-global"
291 }
292 { $heading "Abstractions" }
293 { $subsections
294     "fry"
295     "objects"
296     "errors"
297     "destructors"
298     "memoize"
299     "parsing-words"
300     "macros"
301     "continuations"
302 }
303 { $heading "Program organization" }
304 { $subsections "vocabs.loader" }
305 "Vocabularies tagged " { $link T{ vocab-tag { name "extensions" } } } " implement various additional language abstractions." ;
306
307 ARTICLE: "handbook-system-reference" "The implementation"
308 { $heading "Parse time and compile time" }
309 { $subsections
310     "parser"
311     "definitions"
312     "vocabularies"
313     "source-files"
314     "compiler"
315     "tools.errors"
316 }
317 { $heading "Virtual machine" }
318 { $subsections
319     "images"
320     "command-line"
321     "rc-files"
322     "init"
323     "system"
324     "layouts"
325 } ;
326
327 ARTICLE: "handbook-tools-reference" "Developer tools"
328 "The below tools are text-based. " { $link "ui-tools" } " are documented separately."
329 { $heading "Workflow" }
330 { $subsections
331     "listener"
332     "editor"
333     "vocabs.refresh"
334     "tools.test"
335     "help"
336 }
337 { $heading "Debugging" }
338 { $subsections
339     "prettyprint"
340     "inspector"
341     "tools.inference"
342     "tools.annotations"
343     "tools.deprecation"
344 }
345 { $heading "Browsing" }
346 { $subsections
347     "see"
348     "tools.crossref"
349     "vocabs.hierarchy"
350 }
351 { $heading "Performance" }
352 { $subsections
353     "timing"
354     "tools.profiler.sampling"
355     "tools.memory"
356     "tools.threads"
357     "tools.destructors"
358     "tools.disassembler"
359 }
360 { $heading "Deployment" }
361 { $subsections "tools.deploy" } ;
362
363 ARTICLE: "handbook-library-reference" "Libraries"
364 "This index lists articles from loaded vocabularies which are not subsections of any other article. To explore more vocabularies, see " { $link "vocab-index" } "."
365 { $index [ orphan-articles { "help.home" "handbook" } diff ] } ;
366
367 ARTICLE: "handbook" "Factor handbook"
368 { $heading "Getting started" }
369 { $subsections
370     "cookbook"
371     "first-program"
372 }
373 { $heading "Reference" }
374 { $subsections
375     "handbook-language-reference"
376     "io"
377     "ui"
378     "handbook-system-reference"
379     "handbook-tools-reference"
380     "ui-tools"
381     "alien"
382     "handbook-library-reference"
383 }
384 { $heading "Index" }
385 { $subsections
386   "vocab-index"
387   "article-index"
388   "primitive-index"
389   "error-index"
390   "class-index"
391 }
392 ;
393
394 ABOUT: "handbook"