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
7 FROM: namespaces => set ;
10 ! Method combination protocol
11 GENERIC: perform-combination ( word combination -- )
13 GENERIC: make-default-method ( generic combination -- method )
15 PREDICATE: generic < word
16 "combination" word-prop >boolean ;
18 M: generic definition drop f ;
20 : make-generic ( word -- )
21 [ { "unannotated-def" } reset-props ]
22 [ dup "combination" word-prop perform-combination ]
25 PREDICATE: method < word
26 "method-generic" word-prop >boolean ;
28 ERROR: method-lookup-failed class generic ;
30 : ?lookup-method ( class generic -- method/f )
31 "methods" word-prop at ;
33 : lookup-method ( class generic -- method )
34 2dup ?lookup-method [ 2nip ] [ method-lookup-failed ] if* ;
38 : interesting-class? ( class1 class2 -- ? )
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
49 : interesting-classes ( class classes -- interesting/f )
50 [ [ interesting-class? ] with all? ] { } make and ;
54 : method-classes ( generic -- classes )
55 "methods" word-prop keys ;
57 : order ( generic -- seq )
58 method-classes sort-classes ;
60 : nearest-class ( class generic -- class/f )
61 method-classes interesting-classes smallest-class ;
63 : method-for-class ( class generic -- method/f )
64 [ nip ] [ nearest-class ] 2bi dup [ swap ?lookup-method ] [ 2drop f ] if ;
66 GENERIC: effective-method ( generic -- method )
68 \ effective-method t "no-compile" set-word-prop
70 : next-method-class ( class generic -- class/f )
71 method-classes [ class< ] with filter smallest-class ;
73 : next-method ( class generic -- method/f )
74 [ next-method-class ] keep ?lookup-method ;
76 GENERIC: next-method-quot* ( class generic combination -- quot )
78 : next-method-quot ( method -- quot )
79 next-method-quot-cache get [
80 [ "method-class" word-prop ]
82 "method-generic" word-prop
83 dup "combination" word-prop
84 ] bi next-method-quot*
87 ERROR: no-next-method method ;
89 : (call-next-method) ( method -- )
90 dup next-method-quot [ call ] [ no-next-method ] ?if ;
92 ERROR: check-method-error class generic ;
94 : check-method ( classoid generic -- class generic )
95 2dup [ classoid? ] [ generic? ] bi* and [
99 : remake-generic ( generic -- )
100 dup outdated-generics get set-in-unit ;
102 : remake-generics ( -- )
103 outdated-generics get keys [ generic? ] filter [ make-generic ] each ;
105 GENERIC: update-generic ( class generic -- )
107 : with-methods ( class generic quot -- )
108 [ "methods" word-prop ] prepose [ update-generic ] 2bi ; inline
110 : method-word-name ( class generic -- string )
111 [ class-name ] [ name>> ] bi* "=>" glue ;
113 M: method parent-word
114 "method-generic" word-prop ;
117 "forgotten" word-prop not ;
119 : method-word-props ( class generic -- assoc )
125 : <method> ( class generic -- method )
127 [ method-word-name f <word> ] [ method-word-props ] 2bi
130 GENERIC: implementor-classes ( obj -- class )
132 M: maybe implementor-classes class>> 1array ;
134 M: class implementor-classes 1array ;
136 M: anonymous-union implementor-classes members>> ;
138 M: anonymous-intersection implementor-classes participants>> ;
140 : with-implementors ( class generic quot -- )
141 [ swap implementor-classes [ implementors-map get at ] map ] dip call ; inline
143 : reveal-method ( method classes generic -- )
144 [ [ [ conjoin ] with each ] with-implementors ]
145 [ [ set-at ] with-methods ]
148 : create-method ( class generic -- method )
149 2dup ?lookup-method dup [ 2nip dup reset-generic ] [
151 [ <method> dup ] 2keep
156 PREDICATE: default-method < word "default" word-prop ;
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 ;
162 : define-default-method ( generic combination -- )
163 dupd <default-method> "default-method" set-word-prop ;
165 ! Definition protocol
170 dup "forgotten" word-prop [ drop ] [
172 dup default-method? [ drop ] [
174 [ "method-class" word-prop ]
175 [ "method-generic" word-prop ] bi
179 [ [ delete-at ] with-methods ]
180 [ [ [ delete-at ] with each ] with-implementors ] 2bi
185 [ call-next-method ] bi
188 : define-generic ( word combination effect -- )
189 [ nip swap set-stack-effect ]
192 2dup [ "combination" word-prop ] dip = [ 2drop ] [
194 [ drop reset-generic ]
195 [ "combination" set-word-prop ]
196 [ drop H{ } clone "methods" set-word-prop ]
197 [ define-default-method ]
202 [ 2drop remake-generic ] 3tri ;
206 [ "default-method" word-prop , ]
207 [ "methods" word-prop values % ]
208 [ "engines" word-prop % ]
212 M: class forget-methods
213 [ implementors ] [ [ swap ?lookup-method ] curry ] bi map forget-all ;