]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic.factor
e81212bf181faccd7947cd2abf2c1858ec900bc3
[factor.git] / core / generic / generic.factor
1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.algebra
4 classes.algebra.private classes.maybe classes.private
5 combinators definitions kernel make namespaces sequences sets
6 words ;
7 FROM: namespaces => set ;
8 IN: generic
9
10 ! Method combination protocol
11 GENERIC: perform-combination ( word combination -- )
12
13 GENERIC: make-default-method ( generic combination -- method )
14
15 PREDICATE: generic < word
16     "combination" word-prop >boolean ;
17
18 M: generic definition drop f ;
19
20 : make-generic ( word -- )
21     [ { "unannotated-def" } reset-props ]
22     [ dup "combination" word-prop perform-combination ]
23     bi ;
24
25 PREDICATE: method < word
26     "method-generic" word-prop >boolean ;
27
28 ERROR: method-lookup-failed class generic ;
29
30 : ?lookup-method ( class generic -- method/f )
31     "methods" word-prop at ;
32
33 : lookup-method ( class generic -- method )
34     2dup ?lookup-method [ 2nip ] [ method-lookup-failed ] if* ;
35
36 <PRIVATE
37
38 : interesting-class? ( class1 class2 -- ? )
39     {
40         ! Case 1: no intersection. Discard and keep going
41         { [ 2dup classes-intersect? not ] [ 2drop t ] }
42         ! Case 2: class1 contained in class2. Add to
43         ! interesting set and keep going.
44         { [ 2dup class<= ] [ nip , t ] }
45         ! Case 3: class1 and class2 are incomparable. Give up
46         [ 2drop f ]
47     } cond ;
48
49 : interesting-classes ( class classes -- interesting/f )
50     [ [ interesting-class? ] with all? ] { } make and ;
51
52 PRIVATE>
53
54 : method-classes ( generic -- classes )
55     "methods" word-prop keys ;
56
57 : order ( generic -- seq )
58     method-classes sort-classes ;
59
60 : nearest-class ( class generic -- class/f )
61     method-classes interesting-classes smallest-class ;
62
63 : method-for-class ( class generic -- method/f )
64     [ nip ] [ nearest-class ] 2bi dup [ swap ?lookup-method ] [ 2drop f ] if ;
65
66 GENERIC: effective-method ( generic -- method )
67
68 \ effective-method t "no-compile" set-word-prop
69
70 : next-method-class ( class generic -- class/f )
71     method-classes [ class< ] with filter smallest-class ;
72
73 : next-method ( class generic -- method/f )
74     [ next-method-class ] keep ?lookup-method ;
75
76 GENERIC: next-method-quot* ( class generic combination -- quot )
77
78 : next-method-quot ( method -- quot )
79     next-method-quot-cache get [
80         [ "method-class" word-prop ]
81         [
82             "method-generic" word-prop
83             dup "combination" word-prop
84         ] bi next-method-quot*
85     ] cache ;
86
87 ERROR: no-next-method method ;
88
89 : (call-next-method) ( method -- )
90     dup next-method-quot [ call ] [ no-next-method ] ?if ;
91
92 ERROR: check-method-error class generic ;
93
94 : check-method ( classoid generic -- class generic )
95     2dup [ classoid? ] [ generic? ] bi* and [
96         check-method-error
97     ] unless ; inline
98
99 : remake-generic ( generic -- )
100     dup outdated-generics get set-in-unit ;
101
102 : remake-generics ( -- )
103     outdated-generics get keys [ generic? ] filter [ make-generic ] each ;
104
105 GENERIC: update-generic ( class generic -- )
106
107 : with-methods ( class generic quot -- )
108     [ "methods" word-prop ] prepose [ update-generic ] 2bi ; inline
109
110 : method-word-name ( class generic -- string )
111     [ class-name ] [ name>> ] bi* "=>" glue ;
112
113 M: method parent-word
114     "method-generic" word-prop ;
115
116 M: method crossref?
117     "forgotten" word-prop not ;
118
119 : method-word-props ( class generic -- assoc )
120     [
121         "method-generic" set
122         "method-class" set
123     ] H{ } make-assoc ;
124
125 : <method> ( class generic -- method )
126     check-method
127     [ method-word-name f <word> ] [ method-word-props ] 2bi
128     >>props ;
129
130 GENERIC: implementor-classes ( obj -- class )
131
132 M: maybe implementor-classes class>> 1array ;
133
134 M: class implementor-classes 1array ;
135
136 M: anonymous-union implementor-classes members>> ;
137
138 M: anonymous-intersection implementor-classes participants>> ;
139
140 : with-implementors ( class generic quot -- )
141     [ swap implementor-classes [ implementors-map get at ] map ] dip call ; inline
142
143 : reveal-method ( method classes generic -- )
144     [ [ [ conjoin ] with each ] with-implementors ]
145     [ [ set-at ] with-methods ]
146     2bi ;
147
148 : create-method ( class generic -- method )
149     2dup ?lookup-method dup [ 2nip dup reset-generic ] [
150         drop
151         [ <method> dup ] 2keep
152         reveal-method
153         reset-caches
154     ] if ;
155
156 PREDICATE: default-method < word "default" word-prop ;
157
158 : <default-method> ( generic combination -- method )
159     [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
160     [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
161
162 : define-default-method ( generic combination -- )
163     dupd <default-method> "default-method" set-word-prop ;
164
165 ! Definition protocol
166 M: method definer
167     drop \ M: \ ; ;
168
169 M: method forget*
170     dup "forgotten" word-prop [ drop ] [
171         [
172             dup default-method? [ drop ] [
173                 [
174                     [ "method-class" word-prop ]
175                     [ "method-generic" word-prop ] bi
176                     2dup ?lookup-method
177                 ] keep eq?
178                 [
179                     [ [ delete-at ] with-methods ]
180                     [ [ [ delete-at ] with each ] with-implementors ] 2bi
181                     reset-caches
182                 ] [ 2drop ] if
183             ] if
184         ]
185         [ call-next-method ] bi
186     ] if ;
187
188 : define-generic ( word combination effect -- )
189     [ nip swap set-stack-effect ]
190     [
191         drop
192         2dup [ "combination" word-prop ] dip = [ 2drop ] [
193             {
194                 [ drop reset-generic ]
195                 [ "combination" set-word-prop ]
196                 [ drop H{ } clone "methods" set-word-prop ]
197                 [ define-default-method ]
198             }
199             2cleave
200         ] if
201     ]
202     [ 2drop remake-generic ] 3tri ;
203
204 M: generic subwords
205     [
206         [ "default-method" word-prop , ]
207         [ "methods" word-prop values % ]
208         [ "engines" word-prop % ]
209         tri
210     ] { } make ;
211
212 M: class forget-methods
213     [ implementors ] [ [ swap ?lookup-method ] curry ] bi map forget-all ;