1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays definitions assocs kernel kernel.private
4 slots.private namespaces make sequences strings words words.symbol
5 vectors math quotations combinators sorting effects graphs
10 SYMBOL: class-not-cache
11 SYMBOL: classes-intersect-cache
12 SYMBOL: class-and-cache
13 SYMBOL: class-or-cache
14 SYMBOL: next-method-quot-cache
17 H{ } clone class<=-cache set
18 H{ } clone class-not-cache set
19 H{ } clone classes-intersect-cache set
20 H{ } clone class-and-cache set
21 H{ } clone class-or-cache set
22 H{ } clone next-method-quot-cache set ;
25 class<=-cache get clear-assoc
26 class-not-cache get clear-assoc
27 classes-intersect-cache get clear-assoc
28 class-and-cache get clear-assoc
29 class-or-cache get clear-assoc
30 next-method-quot-cache get clear-assoc ;
34 SYMBOL: implementors-map
36 PREDICATE: class < word "class" word-prop ;
38 : classes ( -- seq ) implementors-map get keys ;
40 : predicate-word ( word -- predicate )
41 [ name>> "?" append ] [ vocabulary>> ] bi create ;
43 PREDICATE: predicate < word "predicating" word-prop >boolean ;
45 M: predicate reset-word
46 [ call-next-method ] [ { "predicating" } reset-props ] bi ;
48 : define-predicate ( class quot -- )
49 [ "predicate" word-prop first ] dip
50 (( object -- ? )) define-declared ;
52 : superclass ( class -- super )
53 #! Output f for non-classes to work with algebra code
54 dup class? [ "superclass" word-prop ] [ drop f ] if ;
56 : superclasses ( class -- supers )
57 [ superclass ] follow reverse ;
59 : members ( class -- seq )
60 #! Output f for non-classes to work with algebra code
61 dup class? [ "members" word-prop ] [ drop f ] if ;
63 : participants ( class -- seq )
64 #! Output f for non-classes to work with algebra code
65 dup class? [ "participants" word-prop ] [ drop f ] if ;
67 GENERIC: rank-class ( class -- n )
69 GENERIC: reset-class ( class -- )
81 M: word reset-class drop ;
83 GENERIC: implementors ( class/classes -- seq )
86 : class-uses ( class -- seq )
90 [ superclass [ , ] when* ]
94 : class-usage ( class -- seq ) update-map get at ;
96 : class-usages ( class -- seq ) [ class-usage ] closure keys ;
100 : update-map+ ( class -- )
101 dup class-uses update-map get add-vertex ;
103 : update-map- ( class -- )
104 dup class-uses update-map get remove-vertex ;
106 M: class implementors implementors-map get at keys ;
108 M: sequence implementors [ implementors ] gather ;
110 : implementors-map+ ( class -- )
111 H{ } clone swap implementors-map get set-at ;
113 : implementors-map- ( class -- )
114 implementors-map get delete-at ;
116 : make-class-props ( superclass members participants metaclass -- assoc )
119 [ dup [ bootstrap-word ] when "superclass" set ]
120 [ [ bootstrap-word ] map "members" set ]
121 [ [ bootstrap-word ] map "participants" set ]
126 : (define-class) ( word props -- )
128 dup class? [ dup [ implementors-map+ ] [ new-class ] bi ] unless
130 dup deferred? [ dup define-symbol ] when
133 ] dip assoc-union >>props
135 [ 1quotation "predicate" set-word-prop ]
136 [ swap "predicating" set-word-prop ]
137 [ drop t "class" set-word-prop ]
142 GENERIC: update-class ( class -- )
144 M: class update-class drop ;
146 GENERIC: update-methods ( class seq -- )
148 : update-classes ( class -- )
150 [ nip [ update-class ] each ] [ update-methods ] 2bi ;
152 : define-class ( word superclass members participants metaclass -- )
153 #! If it was already a class, update methods after.
161 : forget-predicate ( class -- )
162 dup "predicate" word-prop
165 tuck "predicating" word-prop =
166 [ forget ] [ drop ] if
169 : forget-methods ( class -- )
170 [ implementors ] [ [ swap 2array ] curry ] bi map forget-all ;
172 GENERIC: class-forgotten ( use class -- )
174 : forget-class ( class -- )
176 [ dup class-usage keys [ class-forgotten ] with each ]
179 [ implementors-map- ]
185 M: class class-forgotten
188 M: class forget* ( class -- )
189 [ call-next-method ] [ forget-class ] bi ;
191 GENERIC: class ( object -- class )
193 GENERIC: instance? ( object class -- ? ) flushable