renaming: contain? -> any?, deep-contains? -> deep-any?, pad-left -> pad-head, pad...
[factor/jcg.git] / core / classes / algebra / algebra.factor
blob825cd67a4de5f4e5c0e6d193bd1241db27a71dde
1 ! Copyright (C) 2004, 2008 Slava Pestov.\r
2 ! See http://factorcode.org/license.txt for BSD license.\r
3 USING: kernel classes combinators accessors sequences arrays\r
4 vectors assocs namespaces words sorting layouts math hashtables\r
5 kernel.private sets math.order ;\r
6 IN: classes.algebra\r
7 \r
8 TUPLE: anonymous-union members ;\r
9 \r
10 C: <anonymous-union> anonymous-union\r
12 TUPLE: anonymous-intersection participants ;\r
14 C: <anonymous-intersection> anonymous-intersection\r
16 TUPLE: anonymous-complement class ;\r
18 C: <anonymous-complement> anonymous-complement\r
20 GENERIC: valid-class? ( obj -- ? )\r
22 M: class valid-class? drop t ;\r
23 M: anonymous-union valid-class? members>> [ valid-class? ] all? ;\r
24 M: anonymous-intersection valid-class? participants>> [ valid-class? ] all? ;\r
25 M: anonymous-complement valid-class? class>> valid-class? ;\r
26 M: word valid-class? drop f ;\r
28 DEFER: (class<=)\r
30 : class<= ( first second -- ? )\r
31     class<=-cache get [ (class<=) ] 2cache ;\r
33 DEFER: (class-not)\r
35 : class-not ( class -- complement )\r
36     class-not-cache get [ (class-not) ] cache ;\r
38 GENERIC: (classes-intersect?) ( first second -- ? )\r
40 : normalize-class ( class -- class' )\r
41     {\r
42         { [ dup members ] [ members <anonymous-union> ] }\r
43         { [ dup participants ] [ participants <anonymous-intersection> ] }\r
44         [ ]\r
45     } cond ;\r
47 : classes-intersect? ( first second -- ? )\r
48     classes-intersect-cache get [\r
49         normalize-class (classes-intersect?)\r
50     ] 2cache ;\r
52 DEFER: (class-and)\r
54 : class-and ( first second -- class )\r
55     class-and-cache get [ (class-and) ] 2cache ;\r
57 DEFER: (class-or)\r
59 : class-or ( first second -- class )\r
60     class-or-cache get [ (class-or) ] 2cache ;\r
62 : superclass<= ( first second -- ? )\r
63     swap superclass dup [ swap class<= ] [ 2drop f ] if ;\r
65 : left-anonymous-union<= ( first second -- ? )\r
66     [ members>> ] dip [ class<= ] curry all? ;\r
68 : right-anonymous-union<= ( first second -- ? )\r
69     members>> [ class<= ] with any? ;\r
71 : left-anonymous-intersection<= ( first second -- ? )\r
72     [ participants>> ] dip [ class<= ] curry any? ;\r
74 : right-anonymous-intersection<= ( first second -- ? )\r
75     participants>> [ class<= ] with all? ;\r
77 : anonymous-complement<= ( first second -- ? )\r
78     [ class>> ] bi@ swap class<= ;\r
80 : normalize-complement ( class -- class' )\r
81     class>> normalize-class {\r
82         { [ dup anonymous-union? ] [\r
83             members>>\r
84             [ class-not normalize-class ] map\r
85             <anonymous-intersection> \r
86         ] }\r
87         { [ dup anonymous-intersection? ] [\r
88             participants>>\r
89             [ class-not normalize-class ] map\r
90             <anonymous-union>\r
91         ] }\r
92     } cond ;\r
94 : left-anonymous-complement<= ( first second -- ? )\r
95     [ normalize-complement ] dip class<= ;\r
97 PREDICATE: nontrivial-anonymous-complement < anonymous-complement\r
98     class>> {\r
99         [ anonymous-union? ]\r
100         [ anonymous-intersection? ]\r
101         [ members ]\r
102         [ participants ]\r
103     } cleave or or or ;\r
105 PREDICATE: empty-union < anonymous-union members>> empty? ;\r
107 PREDICATE: empty-intersection < anonymous-intersection participants>> empty? ;\r
109 : (class<=) ( first second -- -1/0/1 )\r
110     2dup eq? [ 2drop t ] [\r
111         2dup superclass<= [ 2drop t ] [\r
112             [ normalize-class ] bi@ {\r
113                 { [ dup empty-intersection? ] [ 2drop t ] }\r
114                 { [ over empty-union? ] [ 2drop t ] }\r
115                 { [ 2dup [ anonymous-complement? ] both? ] [ anonymous-complement<= ] }\r
116                 { [ over anonymous-union? ] [ left-anonymous-union<= ] }\r
117                 { [ over anonymous-intersection? ] [ left-anonymous-intersection<= ] }\r
118                 { [ over nontrivial-anonymous-complement? ] [ left-anonymous-complement<= ] }\r
119                 { [ dup anonymous-union? ] [ right-anonymous-union<= ] }\r
120                 { [ dup anonymous-intersection? ] [ right-anonymous-intersection<= ] }\r
121                 { [ dup anonymous-complement? ] [ class>> classes-intersect? not ] }\r
122                 [ 2drop f ]\r
123             } cond\r
124         ] if\r
125     ] if ;\r
127 M: anonymous-union (classes-intersect?)\r
128     members>> [ classes-intersect? ] with any? ;\r
130 M: anonymous-intersection (classes-intersect?)\r
131     participants>> [ classes-intersect? ] with all? ;\r
133 M: anonymous-complement (classes-intersect?)\r
134     class>> class<= not ;\r
136 : anonymous-union-and ( first second -- class )\r
137     members>> [ class-and ] with map <anonymous-union> ;\r
139 : anonymous-intersection-and ( first second -- class )\r
140     participants>> swap suffix <anonymous-intersection> ;\r
142 : (class-and) ( first second -- class )\r
143     {\r
144         { [ 2dup class<= ] [ drop ] }\r
145         { [ 2dup swap class<= ] [ nip ] }\r
146         { [ 2dup classes-intersect? not ] [ 2drop null ] }\r
147         [\r
148             [ normalize-class ] bi@ {\r
149                 { [ dup anonymous-union? ] [ anonymous-union-and ] }\r
150                 { [ dup anonymous-intersection? ] [ anonymous-intersection-and ] }\r
151                 { [ over anonymous-union? ] [ swap anonymous-union-and ] }\r
152                 { [ over anonymous-intersection? ] [ swap anonymous-intersection-and ] }\r
153                 [ 2array <anonymous-intersection> ]\r
154             } cond\r
155         ]\r
156     } cond ;\r
158 : anonymous-union-or ( first second -- class )\r
159     members>> swap suffix <anonymous-union> ;\r
161 : ((class-or)) ( first second -- class )\r
162     [ normalize-class ] bi@ {\r
163         { [ dup anonymous-union? ] [ anonymous-union-or ] }\r
164         { [ over anonymous-union? ] [ swap anonymous-union-or ] }\r
165         [ 2array <anonymous-union> ]\r
166     } cond ;\r
168 : anonymous-complement-or ( first second -- class )\r
169     2dup class>> swap class<= [ 2drop object ] [ ((class-or)) ] if ;\r
171 : (class-or) ( first second -- class )\r
172     {\r
173         { [ 2dup class<= ] [ nip ] }\r
174         { [ 2dup swap class<= ] [ drop ] }\r
175         { [ dup anonymous-complement? ] [ anonymous-complement-or ] }\r
176         { [ over anonymous-complement? ] [ swap anonymous-complement-or ] }\r
177         [ ((class-or)) ]\r
178     } cond ;\r
180 : (class-not) ( class -- complement )\r
181     {\r
182         { [ dup anonymous-complement? ] [ class>> ] }\r
183         { [ dup object eq? ] [ drop null ] }\r
184         { [ dup null eq? ] [ drop object ] }\r
185         [ <anonymous-complement> ]\r
186     } cond ;\r
188 : class< ( first second -- ? )\r
189     {\r
190         { [ 2dup class<= not ] [ 2drop f ] }\r
191         { [ 2dup swap class<= not ] [ 2drop t ] }\r
192         [ [ rank-class ] bi@ < ]\r
193     } cond ;\r
195 : class<=> ( first second -- ? )\r
196     {\r
197         { [ 2dup class<= not ] [ 2drop +gt+ ] }\r
198         { [ 2dup swap class<= not ] [ 2drop +lt+ ] }\r
199         [ [ rank-class ] bi@ <=> ]\r
200     } cond ;\r
202 : class= ( first second -- ? )\r
203     [ class<= ] [ swap class<= ] 2bi and ;\r
205 : largest-class ( seq -- n elt )\r
206     dup [ [ class< ] with any? not ] curry find-last\r
207     [ "Topological sort failed" throw ] unless* ;\r
209 : sort-classes ( seq -- newseq )\r
210     [ [ name>> ] compare ] sort >vector\r
211     [ dup empty? not ]\r
212     [ dup largest-class [ over delete-nth ] dip ]\r
213     [ ] produce nip ;\r
215 : min-class ( class seq -- class/f )\r
216     over [ classes-intersect? ] curry filter\r
217     [ drop f ] [\r
218         [ nip ] [ [ class<= ] with all? ] 2bi [ peek ] [ drop f ] if\r
219     ] if-empty ;\r
221 GENERIC: (flatten-class) ( class -- )\r
223 M: anonymous-union (flatten-class)\r
224     members>> [ (flatten-class) ] each ;\r
226 : flatten-class ( class -- assoc )\r
227     [ (flatten-class) ] H{ } make-assoc ;\r
229 : flatten-builtin-class ( class -- assoc )\r
230     flatten-class [\r
231         dup tuple class<= [ 2drop tuple tuple ] when\r
232     ] assoc-map ;\r
234 : class-types ( class -- seq )\r
235     flatten-builtin-class keys\r
236     [ "type" word-prop ] map natural-sort ;\r
238 : class-tags ( class -- seq )\r
239     class-types [\r
240         dup num-tags get >=\r
241         [ drop \ hi-tag tag-number ] when\r
242     ] map prune ;\r
244 : class-tag ( class -- tag/f )\r
245     class-tags dup length 1 = [ first ] [ drop f ] if ;\r