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 math namespaces sequences
9 ! Method combination protocol
10 GENERIC: perform-combination ( word combination -- )
12 GENERIC: make-default-method ( generic combination -- method )
14 PREDICATE: generic < word
15 "combination" word-prop >boolean ;
17 M: generic definition drop f ;
19 : make-generic ( word -- )
20 [ "unannotated-def" remove-word-prop ]
21 [ dup "combination" word-prop perform-combination ]
24 PREDICATE: method < word
25 "method-generic" word-prop >boolean ;
27 ERROR: method-lookup-failed class generic ;
29 : ?lookup-method ( class generic -- method/f )
30 "methods" word-prop at ;
32 : lookup-method ( class generic -- method )
33 2dup ?lookup-method [ 2nip ] [ method-lookup-failed ] if* ;
37 : interesting-class? ( class1 class2 -- ? )
39 ! Case 1: no intersection. Discard and keep going
40 { [ 2dup classes-intersect? not ] [ 2drop t ] }
41 ! Case 2: class1 contained in class2. Add to
42 ! interesting set and keep going.
43 { [ 2dup class<= ] [ nip , t ] }
44 ! Case 3: class1 and class2 are incomparable. Give up
48 : interesting-classes ( class classes -- interesting/f )
49 [ [ interesting-class? ] with all? ] { } make and ;
53 : method-classes ( generic -- classes )
54 "methods" word-prop keys ;
56 : order ( generic -- seq )
57 method-classes sort-classes ;
59 : nearest-class ( class generic -- class/f )
60 method-classes interesting-classes smallest-class ;
62 : method-for-class ( class generic -- method/f )
63 [ nip ] [ nearest-class ] 2bi
64 [ swap ?lookup-method ] [ drop 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 outdated-generics get add-to-unit ;
102 : remake-generics ( -- )
103 outdated-generics get members [ generic? ] filter
104 [ make-generic ] each ;
106 GENERIC: update-generic ( class generic -- )
108 : with-methods ( class generic quot -- )
109 [ "methods" word-prop ] prepose [ update-generic ] 2bi ; inline
111 : method-word-name ( class generic -- string )
112 [ class-name ] [ name>> ] bi* "=>" glue ;
114 M: method parent-word
115 "method-generic" word-prop ;
118 "forgotten" word-prop not ;
120 : method-word-props ( class generic -- assoc )
126 : <method> ( class generic -- method )
128 [ method-word-name f <word> ] [ method-word-props ] 2bi
131 GENERIC: implementor-classes ( obj -- class )
133 M: maybe implementor-classes class>> 1array ;
135 M: class implementor-classes 1array ;
137 M: anonymous-union implementor-classes members>> ;
139 M: anonymous-intersection implementor-classes participants>> ;
141 : with-implementors ( class generic quot -- )
142 [ swap implementor-classes [ implementors-map get at ] map ] dip call ; inline
144 : reveal-method ( method classes generic -- )
145 [ [ [ adjoin ] with each ] with-implementors ]
146 [ [ set-at ] with-methods ]
149 : create-method ( class generic -- method )
150 2dup ?lookup-method dup [ 2nip dup reset-generic ] [
152 [ <method> dup ] 2keep
157 PREDICATE: default-method < word "default" word-prop ;
159 : <default-method> ( generic combination -- method )
160 [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
161 [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
163 : define-default-method ( generic combination -- )
164 dupd <default-method> "default-method" set-word-prop ;
166 ! Definition protocol
171 dup "forgotten" word-prop [ drop ] [
173 dup default-method? [ drop ] [
175 [ "method-class" word-prop ]
176 [ "method-generic" word-prop ] bi
180 [ [ delete-at ] with-methods ]
181 [ [ [ delete ] with each ] with-implementors ]
186 [ call-next-method ] bi
189 GENERIC# check-combination-effect 1 ( combination effect -- )
191 M: object check-combination-effect 2drop ;
193 : define-generic ( word combination effect -- )
194 [ [ check-combination-effect ] keep swap set-stack-effect ]
197 2dup [ "combination" word-prop ] dip = [ 2drop ] [
199 [ drop reset-generic ]
200 [ "combination" set-word-prop ]
201 [ drop H{ } clone "methods" set-word-prop ]
202 [ define-default-method ]
207 [ 2drop remake-generic ] 3tri ;
211 [ "default-method" word-prop , ]
212 [ "methods" word-prop values % ]
213 [ "engines" word-prop % ]
217 M: class forget-methods
218 [ implementors ] [ [ swap ?lookup-method ] curry ] bi map forget-all ;