]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic.factor
Make M\ throw an error upon lookup failure. Change method -> ?lookup-method, lookup...
[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 words kernel sequences namespaces make assocs
4 hashtables definitions kernel.private classes classes.private
5 classes.algebra quotations arrays vocabs effects combinators
6 sets ;
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 TUPLE: check-method class generic ;
93
94 : check-method ( class generic -- class generic )
95     2dup [ class? ] [ generic? ] bi* and [
96         \ check-method boa throw
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     [ 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 : with-implementors ( class generic quot -- )
131     [ swap implementors-map get at ] dip call ; inline
132
133 : reveal-method ( method class generic -- )
134     [ [ conjoin ] with-implementors ]
135     [ [ set-at ] with-methods ]
136     2bi ;
137
138 : create-method ( class generic -- method )
139     2dup ?lookup-method dup [ 2nip dup reset-generic ] [
140         drop
141         [ <method> dup ] 2keep
142         reveal-method
143         reset-caches
144     ] if ;
145
146 PREDICATE: default-method < word "default" word-prop ;
147
148 : <default-method> ( generic combination -- method )
149     [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
150     [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
151
152 : define-default-method ( generic combination -- )
153     dupd <default-method> "default-method" set-word-prop ;
154
155 ! Definition protocol
156 M: method definer
157     drop \ M: \ ; ;
158
159 M: method forget*
160     dup "forgotten" word-prop [ drop ] [
161         [
162             dup default-method? [ drop ] [
163                 [
164                     [ "method-class" word-prop ]
165                     [ "method-generic" word-prop ] bi
166                     2dup ?lookup-method
167                 ] keep eq?
168                 [
169                     [ [ delete-at ] with-methods ]
170                     [ [ delete-at ] with-implementors ] 2bi
171                     reset-caches
172                 ] [ 2drop ] if
173             ] if
174         ]
175         [ call-next-method ] bi
176     ] if ;
177
178 : define-generic ( word combination effect -- )
179     [ nip swap set-stack-effect ]
180     [
181         drop
182         2dup [ "combination" word-prop ] dip = [ 2drop ] [
183             {
184                 [ drop reset-generic ]
185                 [ "combination" set-word-prop ]
186                 [ drop H{ } clone "methods" set-word-prop ]
187                 [ define-default-method ]
188             }
189             2cleave
190         ] if
191     ]
192     [ 2drop remake-generic ] 3tri ;
193
194 M: generic subwords
195     [
196         [ "default-method" word-prop , ]
197         [ "methods" word-prop values % ]
198         [ "engines" word-prop % ]
199         tri
200     ] { } make ;
201
202 M: class forget-methods
203     [ implementors ] [ [ swap ?lookup-method ] curry ] bi map forget-all ;