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 combinators.private definitions effects generic
5 hashtables kernel layouts make math namespaces quotations
7 FROM: assocs => change-at ;
10 ERROR: no-method object generic ;
12 ERROR: inconsistent-next-method class generic ;
14 TUPLE: single-combination ;
16 PREDICATE: single-generic < generic
17 "combination" word-prop single-combination? ;
19 M: single-generic make-inline cannot-be-inline ;
21 GENERIC: dispatch# ( word -- n )
23 M: generic dispatch# "combination" word-prop dispatch# ;
30 : with-combination ( combination quot -- )
31 [ combination ] dip with-variable ; inline
33 HOOK: picker combination ( -- quot )
35 M: single-combination next-method-quot* ( class generic combination -- quot )
37 2dup next-method dup [
41 [ inconsistent-next-method ] 2curry ,
43 ] [ ] make picker prepend
47 : method-for-object ( obj word -- method )
49 [ method-classes [ instance? ] with filter smallest-class ] keep
51 ] [ "default-method" word-prop ]
54 M: single-combination make-default-method
55 [ [ picker ] dip [ no-method ] curry append ] with-combination ;
57 ! ! ! Build an engine ! ! !
59 : find-default ( methods -- default )
60 #! Side-effects methods.
61 [ object bootstrap-word ] dip delete-at* [
62 drop generic-word get "default-method" word-prop
66 TUPLE: predicate-engine class methods ;
68 C: <predicate-engine> predicate-engine
70 : push-method ( method class atomic assoc -- )
72 [ ] [ H{ } clone <predicate-engine> ] ?if
73 [ methods>> set-at ] keep
76 : flatten-method ( method class assoc -- )
77 over flatten-class keys
78 [ swap push-method ] 2with with each ;
80 : flatten-methods ( assoc -- assoc' )
81 H{ } clone [ [ swapd flatten-method ] curry assoc-each ] keep ;
84 : split-methods ( assoc class -- first second )
85 [ [ nip class<= not ] curry assoc-filter ]
86 [ [ nip class<= ] curry assoc-filter ] 2bi ;
88 : convert-methods ( assoc class word -- assoc' )
89 over [ split-methods ] 2dip pick assoc-empty?
90 [ 3drop ] [ [ execute ] dip pick set-at ] if ; inline
92 ! 2.1 Convert tuple methods
93 TUPLE: echelon-dispatch-engine n methods ;
95 C: <echelon-dispatch-engine> echelon-dispatch-engine
97 TUPLE: tuple-dispatch-engine echelons ;
99 : push-echelon ( class method assoc -- )
100 [ swap dup "layout" word-prop third ] dip
101 [ ?set-at ] change-at ;
103 : echelon-sort ( assoc -- assoc' )
104 #! Convert an assoc mapping classes to methods into an
105 #! assoc mapping echelons to assocs. The first echelon
107 H{ { 0 f } } clone [ [ push-echelon ] curry assoc-each ] keep ;
109 : copy-superclass-methods ( engine superclass assoc -- )
110 at* [ [ methods>> ] bi@ assoc-union! drop ] [ 2drop ] if ;
112 : copy-superclasses-methods ( class engine assoc -- )
113 [ superclasses ] 2dip
114 [ swapd copy-superclass-methods ] 2curry each ;
116 : convert-tuple-inheritance ( assoc -- assoc' )
117 #! A method on a superclass A might have a higher precedence
118 #! than a method on a subclass B, if the methods are
119 #! defined on incomparable classes that happen to contain
120 #! A and B, respectively. Copy A's methods into B's set so
121 #! that they can be sorted and selected properly.
122 dup dup [ copy-superclasses-methods ] curry assoc-each ;
124 : <tuple-dispatch-engine> ( methods -- engine )
125 convert-tuple-inheritance echelon-sort
126 [ dupd <echelon-dispatch-engine> ] assoc-map
127 tuple-dispatch-engine boa ;
129 : convert-tuple-methods ( assoc -- assoc' )
131 \ <tuple-dispatch-engine> convert-methods ;
134 TUPLE: tag-dispatch-engine methods ;
136 C: <tag-dispatch-engine> tag-dispatch-engine
138 : <engine> ( assoc -- engine )
140 convert-tuple-methods
141 <tag-dispatch-engine> ;
143 ! ! ! Compile engine ! ! !
144 GENERIC: compile-engine ( engine -- obj )
146 : compile-engines ( assoc -- assoc' )
147 [ compile-engine ] assoc-map ;
149 : compile-engines* ( assoc -- assoc' )
150 [ over assumed [ compile-engine ] with-variable ] assoc-map ;
152 : direct-dispatch-table ( assoc n -- table )
153 default get <array> <enum> swap assoc-union! seq>> ;
155 : tag-number ( class -- n ) "type" word-prop ;
157 M: tag-dispatch-engine compile-engine
158 methods>> compile-engines*
159 [ [ tag-number ] dip ] assoc-map
160 num-types get direct-dispatch-table ;
162 : build-fast-hash ( methods -- buckets )
163 >alist V{ } clone [ hashcode 1array ] distribute-buckets
164 [ compile-engines* >alist concat ] map ;
166 M: echelon-dispatch-engine compile-engine
168 methods>> dup assoc-size {
169 { 0 [ drop default get ] }
170 { 1 [ >alist first second compile-engine ] }
173 methods>> compile-engines* build-fast-hash
176 M: tuple-dispatch-engine compile-engine
178 echelons>> compile-engines
179 dup keys supremum 1 + f <array>
180 <enum> swap assoc-union! seq>>
183 PREDICATE: predicate-engine-word < word "owner-generic" word-prop ;
185 SYMBOL: predicate-engines
187 : sort-methods ( assoc -- assoc' )
188 >alist [ keys sort-classes ] keep extract-keys ;
190 : quote-methods ( assoc -- assoc' )
191 [ 1quotation \ drop prefix ] assoc-map ;
193 : find-predicate-engine ( classes -- word )
194 predicate-engines get [ at ] curry map-find drop ;
196 : next-predicate-engine ( engine -- word )
198 find-predicate-engine
201 : methods-with-default ( engine -- assoc )
202 [ methods>> clone ] [ next-predicate-engine ] bi
203 object bootstrap-word pick set-at ;
205 : keep-going? ( assoc -- ? )
206 assumed get swap second first class<= ;
210 : prune-redundant-predicates ( assoc -- default assoc' )
212 { [ dup empty? ] [ drop [ unreachable ] { } ] }
213 { [ dup length 1 = ] [ first second { } ] }
214 { [ dup keep-going? ] [ rest-slice prune-redundant-predicates ] }
215 [ [ first second ] [ rest-slice ] bi ]
218 : class-predicates ( assoc -- assoc )
219 [ [ predicate-def [ dup ] prepend ] dip ] assoc-map ;
221 : <predicate-engine-word> ( -- word )
222 generic-word get name>> "/predicate-engine" append f <word>
223 dup generic-word get "owner-generic" set-word-prop ;
225 M: predicate-engine-word stack-effect "owner-generic" word-prop stack-effect ;
227 : define-predicate-engine ( alist -- word )
228 [ <predicate-engine-word> ] dip
229 [ define ] [ drop generic-word get "engines" word-prop push ] [ drop ] 2tri ;
231 : compile-predicate-engine ( engine -- word )
235 prune-redundant-predicates
237 [ last ] [ alist>quot picker prepend define-predicate-engine ] if-empty ;
239 M: predicate-engine compile-engine
240 [ compile-predicate-engine ] [ class>> ] bi
241 [ drop ] [ predicate-engines get set-at ] 2bi ;
243 M: word compile-engine ;
245 M: f compile-engine ;
247 : build-decision-tree ( generic -- methods )
248 [ "engines" word-prop forget-all ]
249 [ V{ } clone "engines" set-word-prop ]
251 "methods" word-prop clone
252 [ find-default default set ]
253 [ <engine> compile-engine ] bi
256 HOOK: inline-cache-quots combination ( word methods -- pic-quot/f pic-tail-quot/f )
258 M: single-combination inline-cache-quots 2drop f f ;
260 : define-inline-cache-quot ( word methods -- )
261 [ drop ] [ inline-cache-quots ] 2bi
262 [ >>pic-def ] [ >>pic-tail-def ] bi*
265 HOOK: mega-cache-quot combination ( methods -- quot/f )
267 M: single-combination perform-combination
269 H{ } clone predicate-engines set
271 dup build-decision-tree
272 [ "decision-tree" set-word-prop ]
273 [ mega-cache-quot define ]
274 [ define-inline-cache-quot ]