]> gitweb.factorcode.org Git - factor.git/blob - core/generic/single/single.factor
Resolved merge.
[factor.git] / core / generic / single / single.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.algebra
4 combinators definitions generic hashtables kernel
5 kernel.private layouts math namespaces quotations
6 sequences words generic.single.private effects make
7 combinators.private ;
8 IN: generic.single
9
10 ERROR: no-method object generic ;
11
12 ERROR: inconsistent-next-method class generic ;
13
14 TUPLE: single-combination ;
15
16 PREDICATE: single-generic < generic
17     "combination" word-prop single-combination? ;
18
19 GENERIC: dispatch# ( word -- n )
20
21 M: generic dispatch# "combination" word-prop dispatch# ;
22
23 SYMBOL: assumed
24 SYMBOL: default
25 SYMBOL: generic-word
26 SYMBOL: combination
27
28 : with-combination ( combination quot -- )
29     [ combination ] dip with-variable ; inline
30
31 HOOK: picker combination ( -- quot )
32
33 M: single-combination next-method-quot* ( class generic combination -- quot )
34     [
35         2dup next-method dup [
36             [
37                 pick "predicate" word-prop %
38                 1quotation ,
39                 [ inconsistent-next-method ] 2curry ,
40                 \ if ,
41             ] [ ] make picker prepend
42         ] [ 3drop f ] if
43     ] with-combination ;
44
45 : (effective-method) ( obj word -- method )
46     [ [ order [ instance? ] with find-last nip ] keep method ]
47     [ "default-method" word-prop ]
48     bi or ;
49
50 M: single-combination make-default-method
51     [ [ picker ] dip [ no-method ] curry append ] with-combination ;
52
53 ! ! ! Build an engine ! ! !
54
55 : find-default ( methods -- default )
56     #! Side-effects methods.
57     [ object bootstrap-word ] dip delete-at* [
58         drop generic-word get "default-method" word-prop
59     ] unless ;
60
61 ! 1. Flatten methods
62 TUPLE: predicate-engine class methods ;
63
64 C: <predicate-engine> predicate-engine
65
66 : push-method ( method specializer atomic assoc -- )
67     dupd [
68         [ ] [ H{ } clone <predicate-engine> ] ?if
69         [ methods>> set-at ] keep
70     ] change-at ;
71
72 : flatten-method ( class method assoc -- )
73     [ [ flatten-class keys ] keep ] 2dip [
74         [ spin ] dip push-method
75     ] 3curry each ;
76
77 : flatten-methods ( assoc -- assoc' )
78     H{ } clone [ [ flatten-method ] curry assoc-each ] keep ;
79
80 ! 2. Convert methods
81 : split-methods ( assoc class -- first second )
82     [ [ nip class<= not ] curry assoc-filter ]
83     [ [ nip class<=     ] curry assoc-filter ] 2bi ;
84
85 : convert-methods ( assoc class word -- assoc' )
86     over [ split-methods ] 2dip pick assoc-empty?
87     [ 3drop ] [ [ execute ] dip pick set-at ] if ; inline
88
89 ! 2.1 Convert tuple methods
90 TUPLE: echelon-dispatch-engine n methods ;
91
92 C: <echelon-dispatch-engine> echelon-dispatch-engine
93
94 TUPLE: tuple-dispatch-engine echelons ;
95
96 : push-echelon ( class method assoc -- )
97     [ swap dup "layout" word-prop third ] dip
98     [ ?set-at ] change-at ;
99
100 : echelon-sort ( assoc -- assoc' )
101     #! Convert an assoc mapping classes to methods into an
102     #! assoc mapping echelons to assocs. The first echelon
103     #! is always there
104     H{ { 0 f } } clone [ [ push-echelon ] curry assoc-each ] keep ;
105
106 : <tuple-dispatch-engine> ( methods -- engine )
107     echelon-sort
108     [ dupd <echelon-dispatch-engine> ] assoc-map
109     \ tuple-dispatch-engine boa ;
110
111 : convert-tuple-methods ( assoc -- assoc' )
112     tuple bootstrap-word
113     \ <tuple-dispatch-engine> convert-methods ;
114
115 ! 2.2 Convert hi-tag methods
116 TUPLE: hi-tag-dispatch-engine methods ;
117
118 C: <hi-tag-dispatch-engine> hi-tag-dispatch-engine
119
120 : convert-hi-tag-methods ( assoc -- assoc' )
121     \ hi-tag bootstrap-word
122     \ <hi-tag-dispatch-engine> convert-methods ;
123
124 ! 3 Tag methods
125 TUPLE: tag-dispatch-engine methods ;
126
127 C: <tag-dispatch-engine> tag-dispatch-engine
128
129 : <engine> ( assoc -- engine )
130     flatten-methods
131     convert-tuple-methods
132     convert-hi-tag-methods
133     <tag-dispatch-engine> ;
134
135 ! ! ! Compile engine ! ! !
136 GENERIC: compile-engine ( engine -- obj )
137
138 : compile-engines ( assoc -- assoc' )
139     [ compile-engine ] assoc-map ;
140
141 : compile-engines* ( assoc -- assoc' )
142     [ over assumed [ compile-engine ] with-variable ] assoc-map ;
143
144 : direct-dispatch-table ( assoc n -- table )
145     default get <array> [ <enum> swap update ] keep ;
146
147 : lo-tag-number ( class -- n )
148     "type" word-prop dup num-tags get member?
149     [ drop object tag-number ] unless ;
150
151 M: tag-dispatch-engine compile-engine
152     methods>> compile-engines*
153     [ [ lo-tag-number ] dip ] assoc-map
154     num-tags get direct-dispatch-table ;
155
156 : num-hi-tags ( -- n ) num-types get num-tags get - ;
157
158 : hi-tag-number ( class -- n ) "type" word-prop ;
159
160 M: hi-tag-dispatch-engine compile-engine
161     methods>> compile-engines*
162     [ [ hi-tag-number num-tags get - ] dip ] assoc-map
163     num-hi-tags direct-dispatch-table ;
164
165 : build-fast-hash ( methods -- buckets )
166     >alist V{ } clone [ hashcode 1array ] distribute-buckets
167     [ compile-engines* >alist { } join ] map ;
168
169 M: echelon-dispatch-engine compile-engine
170     dup n>> 0 = [
171         methods>> dup assoc-size {
172             { 0 [ drop default get ] }
173             { 1 [ >alist first second compile-engine ] }
174         } case
175     ] [
176         methods>> compile-engines* build-fast-hash
177     ] if ;
178
179 M: tuple-dispatch-engine compile-engine
180     tuple assumed [
181         echelons>> compile-engines
182         dup keys supremum 1 + f <array>
183         [ <enum> swap update ] keep
184     ] with-variable ;
185
186 PREDICATE: predicate-engine-word < word "owner-generic" word-prop ;
187
188 SYMBOL: predicate-engines
189
190 : sort-methods ( assoc -- assoc' )
191     >alist [ keys sort-classes ] keep extract-keys ;
192
193 : quote-methods ( assoc -- assoc' )
194     [ 1quotation \ drop prefix ] assoc-map ;
195
196 : find-predicate-engine ( classes -- word )
197     predicate-engines get [ at ] curry map-find drop ;
198
199 : next-predicate-engine ( engine -- word )
200     class>> superclasses
201     find-predicate-engine
202     default get or ;
203
204 : methods-with-default ( engine -- assoc )
205     [ methods>> clone ] [ next-predicate-engine ] bi
206     object bootstrap-word pick set-at ;
207
208 : keep-going? ( assoc -- ? )
209     assumed get swap second first class<= ;
210
211 : prune-redundant-predicates ( assoc -- default assoc' )
212     {
213         { [ dup empty? ] [ drop [ "Unreachable" throw ] { } ] }
214         { [ dup length 1 = ] [ first second { } ] }
215         { [ dup keep-going? ] [ rest-slice prune-redundant-predicates ] }
216         [ [ first second ] [ rest-slice ] bi ]
217     } cond ;
218
219 : class-predicates ( assoc -- assoc )
220     [ [ "predicate" word-prop [ dup ] prepend ] dip ] assoc-map ;
221
222 : <predicate-engine-word> ( -- word )
223     generic-word get name>> "/predicate-engine" append f <word>
224     dup generic-word get "owner-generic" set-word-prop ;
225
226 M: predicate-engine-word stack-effect "owner-generic" word-prop stack-effect ;
227
228 : define-predicate-engine ( alist -- word )
229     [ <predicate-engine-word> ] dip
230     [ define ] [ drop generic-word get "engines" word-prop push ] [ drop ] 2tri ;
231
232 : compile-predicate-engine ( engine -- word )
233     methods-with-default
234     sort-methods
235     quote-methods
236     prune-redundant-predicates
237     class-predicates
238     [ last ] [ alist>quot picker prepend define-predicate-engine ] if-empty ;
239
240 M: predicate-engine compile-engine
241     [ compile-predicate-engine ] [ class>> ] bi
242     [ drop ] [ predicate-engines get set-at ] 2bi ;
243
244 M: word compile-engine ;
245
246 M: f compile-engine ;
247
248 : build-decision-tree ( generic -- methods )
249     [ "engines" word-prop forget-all ]
250     [ V{ } clone "engines" set-word-prop ]
251     [
252         "methods" word-prop clone
253         [ find-default default set ]
254         [ <engine> compile-engine ] bi
255     ] tri ;
256
257 HOOK: inline-cache-quots combination ( word methods -- pic-quot/f pic-tail-quot/f )
258
259 M: single-combination inline-cache-quots 2drop f f ;
260
261 : define-inline-cache-quot ( word methods -- )
262     [ drop ] [ inline-cache-quots ] 2bi
263     [ >>pic-def ] [ >>pic-tail-def ] bi*
264     drop ;
265
266 HOOK: mega-cache-quot combination ( methods -- quot/f )
267
268 M: single-combination perform-combination
269     [
270         H{ } clone predicate-engines set
271         dup generic-word set
272         dup build-decision-tree
273         [ "decision-tree" set-word-prop ]
274         [ mega-cache-quot define ]
275         [ define-inline-cache-quot ]
276         2tri
277     ] with-combination ;