]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic.factor
Move make to its own vocabulary, remove fry _ feature
[factor.git] / core / generic / generic.factor
1 ! Copyright (C) 2006, 2008 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 IN: generic
8
9 ! Method combination protocol
10 GENERIC: perform-combination ( word combination -- )
11
12 GENERIC: make-default-method ( generic combination -- method )
13
14 PREDICATE: generic < word
15     "combination" word-prop >boolean ;
16
17 M: generic definition drop f ;
18
19 : make-generic ( word -- )
20     [ { "unannotated-def" } reset-props ]
21     [ dup "combination" word-prop perform-combination ]
22     bi ;
23
24 : method ( class generic -- method/f )
25     "methods" word-prop at ;
26
27 PREDICATE: method-spec < pair
28     first2 generic? swap class? and ;
29
30 : order ( generic -- seq )
31     "methods" word-prop keys sort-classes ;
32
33 : specific-method ( class generic -- method/f )
34     tuck order min-class dup [ swap method ] [ 2drop f ] if ;
35
36 GENERIC: effective-method ( generic -- method )
37
38 : next-method-class ( class generic -- class/f )
39     order [ class<= ] with filter reverse dup length 1 =
40     [ drop f ] [ second ] if ;
41
42 : next-method ( class generic -- method/f )
43     [ next-method-class ] keep method ;
44
45 GENERIC: next-method-quot* ( class generic combination -- quot )
46
47 : next-method-quot ( class generic -- quot )
48     dup "combination" word-prop next-method-quot* ;
49
50 : (call-next-method) ( class generic -- )
51     next-method-quot call ;
52
53 TUPLE: check-method class generic ;
54
55 : check-method ( class generic -- class generic )
56     2dup [ class? ] [ generic? ] bi* and [
57         \ check-method boa throw
58     ] unless ; inline
59
60 : with-methods ( class generic quot -- )
61     [ drop changed-generic ]
62     [ [ "methods" word-prop ] dip call ]
63     [ drop make-generic drop ]
64     3tri ; inline
65
66 : method-word-name ( class word -- string )
67     [ name>> ] bi@ "=>" swap 3append ;
68
69 PREDICATE: method-body < word
70     "method-generic" word-prop >boolean ;
71
72 M: method-body inline?
73     "method-generic" word-prop inline? ;
74
75 M: method-body stack-effect
76     "method-generic" word-prop stack-effect ;
77
78 M: method-body crossref?
79     "forgotten" word-prop not ;
80
81 : method-word-props ( class generic -- assoc )
82     [
83         "method-generic" set
84         "method-class" set
85     ] H{ } make-assoc ;
86
87 : <method> ( class generic -- method )
88     check-method
89     [ method-word-props ] 2keep
90     method-word-name f <word>
91     swap >>props ;
92
93 : with-implementors ( class generic quot -- )
94     [ swap implementors-map get at ] dip call ; inline
95
96 : reveal-method ( method class generic -- )
97     [ [ conjoin ] with-implementors ]
98     [ [ set-at ] with-methods ]
99     2bi ;
100
101 : create-method ( class generic -- method )
102     2dup method dup [
103         2nip
104     ] [
105         drop [ <method> dup ] 2keep reveal-method
106     ] if ;
107
108 PREDICATE: default-method < word "default" word-prop ;
109
110 M: default-method irrelevant? drop t ;
111
112 : <default-method> ( generic combination -- method )
113     [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
114     [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
115
116 : define-default-method ( generic combination -- )
117     dupd <default-method> "default-method" set-word-prop ;
118
119 ! Definition protocol
120 M: method-spec where
121     dup first2 method [ ] [ second ] ?if where ;
122
123 M: method-spec set-where
124     first2 method set-where ;
125
126 M: method-spec definer
127     first2 method definer ;
128
129 M: method-spec definition
130     first2 method definition ;
131
132 M: method-spec forget*
133     first2 method [ forgotten-definition ] [ forget* ] bi ;
134
135 M: method-spec smart-usage
136     second smart-usage ;
137
138 M: method-body definer
139     drop \ M: \ ; ;
140
141 M: method-body forget*
142     dup "forgotten" word-prop [ drop ] [
143         [
144             dup default-method? [ drop ] [
145                 [
146                     [ "method-class" word-prop ]
147                     [ "method-generic" word-prop ] bi
148                     2dup method
149                 ] keep eq?
150                 [
151                     [ [ delete-at ] with-methods ]
152                     [ [ delete-at ] with-implementors ]
153                     2bi
154                 ] [ 2drop ] if
155             ] if
156         ]
157         [ call-next-method ] bi
158     ] if ;
159
160 M: method-body smart-usage
161     "method-generic" word-prop smart-usage ;
162
163 M: sequence update-methods ( class seq -- )
164     implementors [
165         [ changed-generic ] [ make-generic drop ] 2bi
166     ] with each ;
167
168 : define-generic ( word combination -- )
169     over "combination" word-prop over = [ drop ] [
170         2dup "combination" set-word-prop
171         over "methods" word-prop values forget-all
172         over H{ } clone "methods" set-word-prop
173         dupd define-default-method
174     ] if make-generic ;
175
176 M: generic subwords
177     [
178         [ "default-method" word-prop , ]
179         [ "methods" word-prop values % ]
180         [ "engines" word-prop % ]
181         tri
182     ] { } make ;
183
184 M: generic forget*
185     [ subwords forget-all ] [ call-next-method ] bi ;
186
187 : xref-generics ( -- )
188     all-words [ subwords [ xref ] each ] each ;