]> gitweb.factorcode.org Git - factor.git/blob - core/generic/single/single.factor
82d373c524515105266760297db882a2ee9c7f75
[factor.git] / core / generic / single / single.factor
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
7 combinators.private ;
8 FROM: assocs => change-at ;
9 IN: generic.single
10
11 ERROR: no-method object generic ;
12
13 ERROR: inconsistent-next-method class generic ;
14
15 TUPLE: single-combination ;
16
17 PREDICATE: single-generic < generic
18     "combination" word-prop single-combination? ;
19
20 M: single-generic make-inline cannot-be-inline ;
21
22 GENERIC: dispatch# ( word -- n )
23
24 M: generic dispatch# "combination" word-prop dispatch# ;
25
26 SYMBOL: assumed
27 SYMBOL: default
28 SYMBOL: generic-word
29 SYMBOL: combination
30
31 : with-combination ( combination quot -- )
32     [ combination ] dip with-variable ; inline
33
34 HOOK: picker combination ( -- quot )
35
36 M: single-combination next-method-quot* ( class generic combination -- quot )
37     [
38         2dup next-method dup [
39             [
40                 pick predicate-def %
41                 1quotation ,
42                 [ inconsistent-next-method ] 2curry ,
43                 \ if ,
44             ] [ ] make picker prepend
45         ] [ 3drop f ] if
46     ] with-combination ;
47
48 : method-for-object ( obj word -- method )
49     [
50         [ method-classes [ instance? ] with filter smallest-class ] keep
51         ?lookup-method
52     ] [ "default-method" word-prop ]
53     bi or ;
54
55 M: single-combination make-default-method
56     [ [ picker ] dip [ no-method ] curry append ] with-combination ;
57
58 ! ! ! Build an engine ! ! !
59
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
64     ] unless ;
65
66 ! 1. Flatten methods
67 TUPLE: predicate-engine class methods ;
68
69 C: <predicate-engine> predicate-engine
70
71 : push-method ( method class atomic assoc -- )
72     dupd [
73         [ ] [ H{ } clone <predicate-engine> ] ?if
74         [ methods>> set-at ] keep
75     ] change-at ;
76
77 : flatten-method ( method class assoc -- )
78     over flatten-class keys
79     [ swap push-method ] with with with each ;
80
81 : flatten-methods ( assoc -- assoc' )
82     H{ } clone [ [ swapd flatten-method ] curry assoc-each ] keep ;
83
84 ! 2. Convert methods
85 : split-methods ( assoc class -- first second )
86     [ [ nip class<= not ] curry assoc-filter ]
87     [ [ nip class<=     ] curry assoc-filter ] 2bi ;
88
89 : convert-methods ( assoc class word -- assoc' )
90     over [ split-methods ] 2dip pick assoc-empty?
91     [ 3drop ] [ [ execute ] dip pick set-at ] if ; inline
92
93 ! 2.1 Convert tuple methods
94 TUPLE: echelon-dispatch-engine n methods ;
95
96 C: <echelon-dispatch-engine> echelon-dispatch-engine
97
98 TUPLE: tuple-dispatch-engine echelons ;
99
100 : push-echelon ( class method assoc -- )
101     [ swap dup "layout" word-prop third ] dip
102     [ ?set-at ] change-at ;
103
104 : echelon-sort ( assoc -- assoc' )
105     #! Convert an assoc mapping classes to methods into an
106     #! assoc mapping echelons to assocs. The first echelon
107     #! is always there
108     H{ { 0 f } } clone [ [ push-echelon ] curry assoc-each ] keep ;
109
110 : copy-superclass-methods ( engine superclass assoc -- )
111     at* [ [ methods>> ] bi@ assoc-union! drop ] [ 2drop ] if ;
112
113 : copy-superclasses-methods ( class engine assoc -- )
114     [ superclasses ] 2dip
115     [ swapd copy-superclass-methods ] 2curry each ;
116
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 ;
124
125 : <tuple-dispatch-engine> ( methods -- engine )
126     convert-tuple-inheritance echelon-sort
127     [ dupd <echelon-dispatch-engine> ] assoc-map
128     \ tuple-dispatch-engine boa ;
129
130 : convert-tuple-methods ( assoc -- assoc' )
131     tuple bootstrap-word
132     \ <tuple-dispatch-engine> convert-methods ;
133
134 ! 3 Tag methods
135 TUPLE: tag-dispatch-engine methods ;
136
137 C: <tag-dispatch-engine> tag-dispatch-engine
138
139 : <engine> ( assoc -- engine )
140     flatten-methods
141     convert-tuple-methods
142     <tag-dispatch-engine> ;
143
144 ! ! ! Compile engine ! ! !
145 GENERIC: compile-engine ( engine -- obj )
146
147 : compile-engines ( assoc -- assoc' )
148     [ compile-engine ] assoc-map ;
149
150 : compile-engines* ( assoc -- assoc' )
151     [ over assumed [ compile-engine ] with-variable ] assoc-map ;
152
153 : direct-dispatch-table ( assoc n -- table )
154     default get <array> <enum> swap assoc-union! seq>> ;
155
156 : tag-number ( class -- n ) "type" word-prop ;
157
158 M: tag-dispatch-engine compile-engine
159     methods>> compile-engines*
160     [ [ tag-number ] dip ] assoc-map
161     num-types get direct-dispatch-table ;
162
163 : build-fast-hash ( methods -- buckets )
164     >alist V{ } clone [ hashcode 1array ] distribute-buckets
165     [ compile-engines* >alist { } join ] map ;
166
167 M: echelon-dispatch-engine compile-engine
168     dup n>> 0 = [
169         methods>> dup assoc-size {
170             { 0 [ drop default get ] }
171             { 1 [ >alist first second compile-engine ] }
172         } case
173     ] [
174         methods>> compile-engines* build-fast-hash
175     ] if ;
176
177 M: tuple-dispatch-engine compile-engine
178     tuple assumed [
179         echelons>> compile-engines
180         dup keys supremum 1 + f <array>
181         <enum> swap assoc-union! seq>>
182     ] with-variable ;
183
184 PREDICATE: predicate-engine-word < word "owner-generic" word-prop ;
185
186 SYMBOL: predicate-engines
187
188 : sort-methods ( assoc -- assoc' )
189     >alist [ keys sort-classes ] keep extract-keys ;
190
191 : quote-methods ( assoc -- assoc' )
192     [ 1quotation \ drop prefix ] assoc-map ;
193
194 : find-predicate-engine ( classes -- word )
195     predicate-engines get [ at ] curry map-find drop ;
196
197 : next-predicate-engine ( engine -- word )
198     class>> superclasses
199     find-predicate-engine
200     default get or ;
201
202 : methods-with-default ( engine -- assoc )
203     [ methods>> clone ] [ next-predicate-engine ] bi
204     object bootstrap-word pick set-at ;
205
206 : keep-going? ( assoc -- ? )
207     assumed get swap second first class<= ;
208
209 ERROR: unreachable ;
210
211 : prune-redundant-predicates ( assoc -- default assoc' )
212     {
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 ]
217     } cond ;
218
219 : class-predicates ( assoc -- assoc )
220     [ [ predicate-def [ 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 ;