]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic-docs.factor
Solution to Project Euler problem 65
[factor.git] / core / generic / generic-docs.factor
1 USING: help.markup help.syntax words classes classes.algebra
2 definitions kernel alien sequences math quotations
3 generic.single generic.standard generic.hook generic.math
4 combinators prettyprint effects ;
5 IN: generic
6
7 ARTICLE: "method-order" "Method precedence"
8 "Conceptually, method dispatch is implemented by testing the object against the predicate word for every class, in linear order (" { $link "class-linearization" } ")."
9 $nl
10 "Here is an example:"
11 { $code
12     "GENERIC: explain ( object -- )"
13     "M: object explain drop \"an object\" print ;"
14     "M: number explain drop \"a number\" print ;"
15     "M: sequence explain drop \"a sequence\" print ;"
16 }
17 "The linear order is the following, from least-specific to most-specific:"
18 { $code "{ object sequence number }" }
19 "Neither " { $link number } " nor " { $link sequence } " are subclasses of each other, yet their intersection is the non-empty " { $link integer } " class. Calling " { $snippet "explain" } " with an integer on the stack will print " { $snippet "a number" } " because " { $link number } " precedes " { $link sequence } " in the class linearization order. If this was not the desired outcome, define a method on the intersection:"
20 { $code "M: integer explain drop \"an integer\" print ;" }
21 "Now, the linear order is the following, from least-specific to most-specific:"
22 { $code "{ object sequence number integer }" }
23 "The " { $link order } " word can be useful to clarify method dispatch order:"
24 { $subsection order } ;
25
26 ARTICLE: "generic-introspection" "Generic word introspection"
27 "In most cases, generic words and methods are defined at parse time with " { $link POSTPONE: GENERIC: } " (or some other parsing word) and " { $link POSTPONE: M: } "."
28 $nl
29 "Sometimes, generic words need to be inspected defined at run time; words for performing these tasks are found in the " { $vocab-link "generic" } " vocabulary."
30 $nl
31 "The set of generic words is a class which implements the " { $link "definition-protocol" } ":"
32 { $subsection generic }
33 { $subsection generic? }
34 "New generic words can be defined:"
35 { $subsection define-generic }
36 { $subsection define-simple-generic }
37 "Methods can be added to existing generic words:"
38 { $subsection create-method }
39 "Method definitions can be looked up:"
40 { $subsection method }
41 "Finding the most specific method for an object:"
42 { $subsection effective-method }
43 "A generic word contains methods; the list of methods specializing on a class can also be obtained:"
44 { $subsection implementors }
45 "Low-level word which rebuilds the generic word after methods are added or removed, or the method combination is changed:"
46 { $subsection make-generic }
47 "Low-level method constructor:"
48 { $subsection <method> }
49 "Methods may be pushed on the stack with a literal syntax:"
50 { $subsection POSTPONE: M\ }
51 { $see-also "see" } ;
52
53 ARTICLE: "method-combination" "Custom method combination"
54 "Abstractly, a generic word can be thought of as a big chain of type conditional tests applied to the top of the stack, with methods as the bodies of each test. The " { $emphasis "method combination" } " is this control flow glue between the set of methods, and several aspects of it can be customized:"
55 { $list
56     "which stack item(s) the generic word dispatches upon,"
57     "which methods out of the set of applicable methods are called"
58 }
59 "A table of built-in method combination defining words, and the method combinations themselves:"
60 { $table
61     { { $link POSTPONE: GENERIC: } { $link standard-combination } }
62     { { $link POSTPONE: GENERIC# } { $link standard-combination } }
63     { { $link POSTPONE: HOOK: } { $link hook-combination } }
64     { { $link POSTPONE: MATH: } { $link math-combination } }
65 }
66 "Developing a custom method combination requires that a parsing word calling " { $link define-generic } " be defined; additionally, it is a good idea to implement the " { $link "definition-protocol" } " on the class of words having this method combination, to properly support developer tools."
67 $nl
68 "The combination quotation passed to " { $link define-generic } " has stack effect " { $snippet "( word -- quot )" } ". It's job is to call various introspection words, including at least obtaining the set of methods defined on the generic word, then combining these methods in some way to produce a quotation."
69 { $see-also "generic-introspection" } ;
70
71 ARTICLE: "call-next-method" "Calling less-specific methods"
72 "If a generic word is called with an object and multiple methods specialize on classes that this object is an instance of, usually the most specific method is called (" { $link "method-order" } ")."
73 $nl
74 "Less-specific methods can be called directly:"
75 { $subsection POSTPONE: call-next-method }
76 "A lower-level word which the above expands into:"
77 { $subsection (call-next-method) }
78 "To look up the next applicable method reflectively:"
79 { $subsection next-method }
80 "Errors thrown by improper calls to " { $link POSTPONE: call-next-method } ":"
81 { $subsection inconsistent-next-method }
82 { $subsection no-next-method } ;
83
84 ARTICLE: "generic" "Generic words and methods"
85 "A " { $emphasis "generic word" } " is composed of zero or more " { $emphasis "methods" } " together with a " { $emphasis "method combination" } ". A method " { $emphasis "specializes" } " on a class; when a generic word executed, the method combination chooses the most appropriate method and calls its definition."
86 $nl
87 "A generic word behaves roughly like a long series of class predicate conditionals in a " { $link cond } " form, however methods can be defined in independent source files, reducing coupling and increasing extensibility. The method combination determines which object the generic word will " { $emphasis "dispatch" } " on; this could be the top of the stack, or some other value."
88 $nl
89 "Generic words which dispatch on the object at the top of the stack:"
90 { $subsection POSTPONE: GENERIC: }
91 "A method combination which dispatches on a specified stack position:"
92 { $subsection POSTPONE: GENERIC# }
93 "A method combination which dispatches on the value of a variable at the time the generic word is called:"
94 { $subsection POSTPONE: HOOK: }
95 "A method combination which dispatches on a pair of stack values, which must be numbers, and upgrades both to the same type of number:"
96 { $subsection POSTPONE: MATH: }
97 "Method definition:"
98 { $subsection POSTPONE: M: }
99 "Generic words must declare their stack effect in order to compile. See " { $link "effects" } "."
100 { $subsection "method-order" }
101 { $subsection "call-next-method" }
102 { $subsection "method-combination" }
103 { $subsection "generic-introspection" }
104 "Generic words specialize behavior based on the class of an object; sometimes behavior needs to be specialized on the object's " { $emphasis "structure" } "; this is known as " { $emphasis "pattern matching" } " and is implemented in the " { $vocab-link "match" } " vocabulary." ;
105
106 ABOUT: "generic"
107
108 HELP: generic
109 { $class-description "The class of generic words, documented in " { $link "generic" } "." } ;
110
111 { generic define-generic define-simple-generic POSTPONE: GENERIC: POSTPONE: GENERIC# POSTPONE: MATH: POSTPONE: HOOK: } related-words
112
113 HELP: make-generic
114 { $values { "word" generic } }
115 { $description "Regenerates the definition of a generic word by applying the method combination to the set of defined methods." }
116 $low-level-note ;
117
118 HELP: define-generic
119 { $values { "word" word } { "effect" effect } { "combination" "a method combination" } }
120 { $description "Defines a generic word. A method combination is an object which responds to the " { $link perform-combination } " generic word." }
121 { $contract "The method combination quotation is called each time the generic word has to be updated (for example, when a method is added), and thus must be side-effect free." } ;
122
123 HELP: M\
124 { $syntax "M\\ class generic" }
125 { $class-description "Pushes a method on the stack." }
126 { $examples { $code "M\\ fixnum + see" } { $code "USING: ui.gadgets ui.gadgets.editors ;" "M\\ editor draw-gadget* edit" } } ;
127
128 HELP: method-body
129 { $class-description "The class of method bodies, which are words with special word properties set." } ;
130
131 HELP: method
132 { $values { "class" class } { "generic" generic } { "method/f" { $maybe method-body } } }
133 { $description "Looks up a method definition." } ;
134
135 { method create-method POSTPONE: M: } related-words
136
137 HELP: <method>
138 { $values { "class" class } { "generic" generic } { "method" "a new method definition" } }
139 { $description "Creates a new method." } ;
140
141 HELP: order
142 { $values { "generic" generic } { "seq" "a sequence of classes" } }
143 { $description "Outputs a sequence of classes for which methods have been defined on this generic word. The sequence is sorted in method dispatch order." } ;
144
145 HELP: check-method
146 { $values { "class" class } { "generic" generic } }
147 { $description "Asserts that " { $snippet "class" } " is a class word and " { $snippet "generic" } " is a generic word, throwing a " { $link check-method } " error if the assertion fails." }
148 { $error-description "Thrown if " { $link POSTPONE: M: } " or " { $link create-method } " is given an invalid class or generic word." } ;
149
150 HELP: with-methods
151 { $values { "class" class } { "generic" generic } { "quot" { $quotation "( methods -- )" } } }
152 { $description "Applies a quotation to the generic word's methods hashtable, and regenerates the generic word's definition when the quotation returns." }
153 $low-level-note ;
154
155 HELP: create-method
156 { $values { "class" class } { "generic" generic } { "method" method-body } }
157 { $description "Creates a method or returns an existing one. This is the runtime equivalent of " { $link POSTPONE: M: } "." }
158 { $notes "To define a method, pass the output value to " { $link define } "." } ;
159
160 HELP: forget-methods
161 { $values { "class" class } }
162 { $description "Remove all method definitions which specialize on the class." } ;
163
164 { sort-classes order } related-words
165
166 HELP: (call-next-method)
167 { $values { "method" method-body } }
168 { $description "Low-level word implementing " { $link POSTPONE: call-next-method } "." }
169 { $notes "In most cases, " { $link POSTPONE: call-next-method } " should be used instead." } ;
170
171 HELP: no-next-method
172 { $error-description "Thrown by " { $link POSTPONE: call-next-method } " if the current method is already the least specific method." }
173 { $examples
174     "The following code throws this error:"
175     { $code
176         "GENERIC: error-test ( object -- )"
177         ""
178         "M: number error-test 3 + call-next-method ;"
179         ""
180         "M: integer error-test recip call-next-method ;"
181         ""
182         "123 error-test"
183     }
184     "This results in the method on " { $link integer } " being called, which then calls the method on " { $link number } ". The latter then calls " { $link POSTPONE: call-next-method } ", however there is no method less specific than the method on " { $link number } " and so an error is thrown."
185 } ;