1 ! Copyright (C) 2009, 2010 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
8 FROM: assocs => change-at ;
11 ERROR: no-method object generic ;
13 ERROR: inconsistent-next-method class generic ;
15 TUPLE: single-combination ;
17 PREDICATE: single-generic < generic
18 "combination" word-prop single-combination? ;
20 M: single-generic make-inline cannot-be-inline ;
22 GENERIC: dispatch# ( word -- n )
24 M: generic dispatch# "combination" word-prop dispatch# ;
31 : with-combination ( combination quot -- )
32 [ combination ] dip with-variable ; inline
34 HOOK: picker combination ( -- quot )
36 M: single-combination next-method-quot* ( class generic combination -- quot )
38 2dup next-method dup [
40 pick "predicate" word-prop %
42 [ inconsistent-next-method ] 2curry ,
44 ] [ ] make picker prepend
48 : method-for-object ( obj word -- method )
50 [ method-classes [ instance? ] with filter smallest-class ] keep
52 ] [ "default-method" word-prop ]
55 M: single-combination make-default-method
56 [ [ picker ] dip [ no-method ] curry append ] with-combination ;
58 ! ! ! Build an engine ! ! !
60 : find-default ( methods -- default )
61 #! Side-effects methods.
62 [ object bootstrap-word ] dip delete-at* [
63 drop generic-word get "default-method" word-prop
67 TUPLE: predicate-engine class methods ;
69 C: <predicate-engine> predicate-engine
71 : push-method ( method class atomic assoc -- )
73 [ ] [ H{ } clone <predicate-engine> ] ?if
74 [ methods>> set-at ] keep
77 : flatten-method ( method class assoc -- )
78 over flatten-class keys
79 [ swap push-method ] with with with each ;
81 : flatten-methods ( assoc -- assoc' )
82 H{ } clone [ [ swapd flatten-method ] curry assoc-each ] keep ;
85 : split-methods ( assoc class -- first second )
86 [ [ nip class<= not ] curry assoc-filter ]
87 [ [ nip class<= ] curry assoc-filter ] 2bi ;
89 : convert-methods ( assoc class word -- assoc' )
90 over [ split-methods ] 2dip pick assoc-empty?
91 [ 3drop ] [ [ execute ] dip pick set-at ] if ; inline
93 ! 2.1 Convert tuple methods
94 TUPLE: echelon-dispatch-engine n methods ;
96 C: <echelon-dispatch-engine> echelon-dispatch-engine
98 TUPLE: tuple-dispatch-engine echelons ;
100 : push-echelon ( class method assoc -- )
101 [ swap dup "layout" word-prop third ] dip
102 [ ?set-at ] change-at ;
104 : echelon-sort ( assoc -- assoc' )
105 #! Convert an assoc mapping classes to methods into an
106 #! assoc mapping echelons to assocs. The first echelon
108 H{ { 0 f } } clone [ [ push-echelon ] curry assoc-each ] keep ;
110 : copy-superclass-methods ( engine superclass assoc -- )
111 at* [ [ methods>> ] bi@ assoc-union! drop ] [ 2drop ] if ;
113 : copy-superclasses-methods ( class engine assoc -- )
114 [ superclasses ] 2dip
115 [ swapd copy-superclass-methods ] 2curry each ;
117 : convert-tuple-inheritance ( assoc -- assoc' )
118 #! A method on a superclass A might have a higher precedence
119 #! than a method on a subclass B, if the methods are
120 #! defined on incomparable classes that happen to contain
121 #! A and B, respectively. Copy A's methods into B's set so
122 #! that they can be sorted and selected properly.
123 dup dup [ copy-superclasses-methods ] curry assoc-each ;
125 : <tuple-dispatch-engine> ( methods -- engine )
126 convert-tuple-inheritance echelon-sort
127 [ dupd <echelon-dispatch-engine> ] assoc-map
128 \ tuple-dispatch-engine boa ;
130 : convert-tuple-methods ( assoc -- assoc' )
132 \ <tuple-dispatch-engine> convert-methods ;
135 TUPLE: tag-dispatch-engine methods ;
137 C: <tag-dispatch-engine> tag-dispatch-engine
139 : <engine> ( assoc -- engine )
141 convert-tuple-methods
142 <tag-dispatch-engine> ;
144 ! ! ! Compile engine ! ! !
145 GENERIC: compile-engine ( engine -- obj )
147 : compile-engines ( assoc -- assoc' )
148 [ compile-engine ] assoc-map ;
150 : compile-engines* ( assoc -- assoc' )
151 [ over assumed [ compile-engine ] with-variable ] assoc-map ;
153 : direct-dispatch-table ( assoc n -- table )
154 default get <array> <enum> swap assoc-union! seq>> ;
156 : tag-number ( class -- n ) "type" word-prop ;
158 M: tag-dispatch-engine compile-engine
159 methods>> compile-engines*
160 [ [ tag-number ] dip ] assoc-map
161 num-types get direct-dispatch-table ;
163 : build-fast-hash ( methods -- buckets )
164 >alist V{ } clone [ hashcode 1array ] distribute-buckets
165 [ compile-engines* >alist { } join ] map ;
167 M: echelon-dispatch-engine compile-engine
169 methods>> dup assoc-size {
170 { 0 [ drop default get ] }
171 { 1 [ >alist first second compile-engine ] }
174 methods>> compile-engines* build-fast-hash
177 M: tuple-dispatch-engine compile-engine
179 echelons>> compile-engines
180 dup keys supremum 1 + f <array>
181 <enum> swap assoc-union! seq>>
184 PREDICATE: predicate-engine-word < word "owner-generic" word-prop ;
186 SYMBOL: predicate-engines
188 : sort-methods ( assoc -- assoc' )
189 >alist [ keys sort-classes ] keep extract-keys ;
191 : quote-methods ( assoc -- assoc' )
192 [ 1quotation \ drop prefix ] assoc-map ;
194 : find-predicate-engine ( classes -- word )
195 predicate-engines get [ at ] curry map-find drop ;
197 : next-predicate-engine ( engine -- word )
199 find-predicate-engine
202 : methods-with-default ( engine -- assoc )
203 [ methods>> clone ] [ next-predicate-engine ] bi
204 object bootstrap-word pick set-at ;
206 : keep-going? ( assoc -- ? )
207 assumed get swap second first class<= ;
211 : prune-redundant-predicates ( assoc -- default assoc' )
213 { [ dup empty? ] [ drop [ unreachable ] { } ] }
214 { [ dup length 1 = ] [ first second { } ] }
215 { [ dup keep-going? ] [ rest-slice prune-redundant-predicates ] }
216 [ [ first second ] [ rest-slice ] bi ]
219 : class-predicates ( assoc -- assoc )
220 [ [ "predicate" word-prop [ dup ] prepend ] dip ] assoc-map ;
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 ;
226 M: predicate-engine-word stack-effect "owner-generic" word-prop stack-effect ;
228 : define-predicate-engine ( alist -- word )
229 [ <predicate-engine-word> ] dip
230 [ define ] [ drop generic-word get "engines" word-prop push ] [ drop ] 2tri ;
232 : compile-predicate-engine ( engine -- word )
236 prune-redundant-predicates
238 [ last ] [ alist>quot picker prepend define-predicate-engine ] if-empty ;
240 M: predicate-engine compile-engine
241 [ compile-predicate-engine ] [ class>> ] bi
242 [ drop ] [ predicate-engines get set-at ] 2bi ;
244 M: word compile-engine ;
246 M: f compile-engine ;
248 : build-decision-tree ( generic -- methods )
249 [ "engines" word-prop forget-all ]
250 [ V{ } clone "engines" set-word-prop ]
252 "methods" word-prop clone
253 [ find-default default set ]
254 [ <engine> compile-engine ] bi
257 HOOK: inline-cache-quots combination ( word methods -- pic-quot/f pic-tail-quot/f )
259 M: single-combination inline-cache-quots 2drop f f ;
261 : define-inline-cache-quot ( word methods -- )
262 [ drop ] [ inline-cache-quots ] 2bi
263 [ >>pic-def ] [ >>pic-tail-def ] bi*
266 HOOK: mega-cache-quot combination ( methods -- quot/f )
268 M: single-combination perform-combination
270 H{ } clone predicate-engines set
272 dup build-decision-tree
273 [ "decision-tree" set-word-prop ]
274 [ mega-cache-quot define ]
275 [ define-inline-cache-quot ]