1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math math.order words combinators locals
4 ascii unicode.categories combinators.short-circuit sequences
5 fry macros arrays assocs sets classes mirrors ;
8 SINGLETONS: any-char any-char-no-nl
9 letter-class LETTER-class Letter-class digit-class
10 alpha-class non-newline-blank-class
11 ascii-class punctuation-class java-printable-class blank-class
12 control-character-class hex-digit-class java-blank-class c-identifier-class
13 unmatchable-class terminator-class word-boundary-class ;
15 SINGLETONS: beginning-of-input ^ end-of-input $ end-of-file word-break ;
17 TUPLE: range from to ;
20 GENERIC: class-member? ( obj class -- ? )
22 M: t class-member? ( obj class -- ? ) 2drop t ;
24 M: integer class-member? ( obj class -- ? ) = ;
26 M: range class-member? ( obj class -- ? )
27 [ from>> ] [ to>> ] bi between? ;
29 M: any-char class-member? ( obj class -- ? )
32 M: any-char-no-nl class-member? ( obj class -- ? )
35 M: letter-class class-member? ( obj class -- ? )
38 M: LETTER-class class-member? ( obj class -- ? )
41 M: Letter-class class-member? ( obj class -- ? )
44 M: ascii-class class-member? ( obj class -- ? )
47 M: digit-class class-member? ( obj class -- ? )
50 : c-identifier-char? ( ch -- ? )
51 { [ alpha? ] [ CHAR: _ = ] } 1|| ;
53 M: c-identifier-class class-member? ( obj class -- ? )
54 drop c-identifier-char? ;
56 M: alpha-class class-member? ( obj class -- ? )
60 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
62 M: punctuation-class class-member? ( obj class -- ? )
65 : java-printable? ( ch -- ? )
66 { [ alpha? ] [ punct? ] } 1|| ;
68 M: java-printable-class class-member? ( obj class -- ? )
69 drop java-printable? ;
71 M: non-newline-blank-class class-member? ( obj class -- ? )
72 drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
74 M: control-character-class class-member? ( obj class -- ? )
77 : hex-digit? ( ch -- ? )
79 [ CHAR: A CHAR: F between? ]
80 [ CHAR: a CHAR: f between? ]
81 [ CHAR: 0 CHAR: 9 between? ]
84 M: hex-digit-class class-member? ( obj class -- ? )
87 : java-blank? ( ch -- ? )
89 CHAR: \s CHAR: \t CHAR: \n
90 HEX: b HEX: 7 CHAR: \r
93 M: java-blank-class class-member? ( obj class -- ? )
96 M: unmatchable-class class-member? ( obj class -- ? )
99 M: terminator-class class-member? ( obj class -- ? )
100 drop "\r\n\u000085\u002029\u002028" member? ;
102 M: ^ class-member? ( obj class -- ? )
105 M: $ class-member? ( obj class -- ? )
108 M: f class-member? 2drop f ;
110 TUPLE: primitive-class class ;
111 C: <primitive-class> primitive-class
113 TUPLE: not-class class ;
115 PREDICATE: not-integer < not-class class>> integer? ;
116 PREDICATE: not-primitive < not-class class>> primitive-class? ;
118 M: not-class class-member?
119 class>> class-member? not ;
121 TUPLE: or-class seq ;
123 M: or-class class-member?
124 seq>> [ class-member? ] with any? ;
126 TUPLE: and-class seq ;
128 M: and-class class-member?
129 seq>> [ class-member? ] with all? ;
133 : flatten ( seq class -- newseq )
134 '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
136 :: seq>instance ( seq empty class -- instance )
140 [ drop class new seq { } like >>seq ]
143 TUPLE: class-partition integers not-integers primitives not-primitives and or other ;
145 : partition-classes ( seq -- class-partition )
147 [ integer? ] partition
148 [ not-integer? ] partition
149 [ primitive-class? ] partition ! extend primitive-class to epsilon tags
150 [ not-primitive? ] partition
151 [ and-class? ] partition
152 [ or-class? ] partition
153 class-partition boa ;
155 : class-partition>seq ( class-partition -- seq )
156 make-mirror values concat ;
158 : repartition ( partition -- partition' )
159 ! This could be made more efficient; only and and or are effected
160 class-partition>seq partition-classes ;
162 : filter-not-integers ( partition -- partition' )
164 [ primitives>> ] [ not-primitives>> ] [ or>> ] tri
165 3append and-class boa
166 '[ [ class>> _ class-member? ] filter ] change-not-integers ;
168 : answer-ors ( partition -- partition' )
169 dup [ not-integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
170 '[ [ _ [ t substitute ] each ] map ] change-or ;
172 : contradiction? ( partition -- ? )
174 [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
175 [ other>> f swap member? ]
178 : make-and-class ( partition -- and-class )
179 answer-ors repartition
180 [ t swap remove ] change-other
183 [ filter-not-integers class-partition>seq prune t and-class seq>instance ] if ;
185 : <and-class> ( seq -- class )
186 dup and-class flatten partition-classes
187 dup integers>> length {
188 { 0 [ nip make-and-class ] }
189 { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
193 : filter-integers ( partition -- partition' )
195 [ primitives>> ] [ not-primitives>> ] [ and>> ] tri
197 '[ [ _ class-member? not ] filter ] change-integers ;
199 : answer-ands ( partition -- partition' )
200 dup [ integers>> ] [ not-primitives>> ] [ primitives>> ] tri 3append
201 '[ [ _ [ f substitute ] each ] map ] change-and ;
203 : tautology? ( partition -- ? )
205 [ [ primitives>> ] [ not-primitives>> ] bi intersects? ]
206 [ other>> t swap member? ]
209 : make-or-class ( partition -- and-class )
210 answer-ands repartition
211 [ f swap remove ] change-other
214 [ filter-integers class-partition>seq prune f or-class seq>instance ] if ;
216 : <or-class> ( seq -- class )
217 dup or-class flatten partition-classes
218 dup not-integers>> length {
219 { 0 [ nip make-or-class ] }
220 { 1 [ not-integers>> first [ class>> '[ _ swap class-member? ] any? ] keep or ] }
224 GENERIC: <not-class> ( class -- inverse )
226 M: object <not-class>
229 M: not-class <not-class>
232 M: and-class <not-class>
233 seq>> [ <not-class> ] map <or-class> ;
235 M: or-class <not-class>
236 seq>> [ <not-class> ] map <and-class> ;
238 M: t <not-class> drop f ;
239 M: f <not-class> drop t ;
241 M: primitive-class class-member?
242 class>> class-member? ;
244 UNION: class primitive-class not-class or-class and-class range ;
246 TUPLE: condition question yes no ;
247 C: <condition> condition
249 GENERIC# answer 2 ( class from to -- new-class )
251 M:: object answer ( class from to -- new-class )
252 class from = to class ? ;
254 : replace-compound ( class from to -- seq )
255 [ seq>> ] 2dip '[ _ _ answer ] map ;
258 replace-compound <and-class> ;
261 replace-compound <or-class> ;
264 [ class>> ] 2dip answer <not-class> ;
266 GENERIC# substitute 1 ( class from to -- new-class )
267 M: object substitute answer ;
268 M: not-class substitute [ <not-class> ] bi@ answer ;
270 : assoc-answer ( table question answer -- new-table )
271 '[ _ _ substitute ] assoc-map
272 [ nip ] assoc-filter ;
274 : assoc-answers ( table questions answer -- new-table )
275 '[ _ assoc-answer ] each ;
277 DEFER: make-condition
279 : (make-condition) ( table questions question -- condition )
281 [ swap [ t assoc-answer ] dip make-condition ]
282 [ swap [ f assoc-answer ] dip make-condition ] 3tri
283 2dup = [ 2nip ] [ <condition> ] if ;
285 : make-condition ( table questions -- condition )
286 [ keys ] [ unclip (make-condition) ] if-empty ;
288 GENERIC: class>questions ( class -- questions )
289 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
290 M: or-class class>questions compound-questions ;
291 M: and-class class>questions compound-questions ;
292 M: not-class class>questions class>> class>questions ;
293 M: object class>questions 1array ;
295 : table>questions ( table -- questions )
296 values [ class>questions ] gather >array t swap remove ;
298 : table>condition ( table -- condition )
299 ! input table is state => class
300 >alist dup table>questions make-condition ;
302 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition )
304 [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
305 '[ _ condition-map ] bi@ <condition>
306 ] [ call ] if ; inline recursive
308 : condition-states ( condition -- states )
310 [ yes>> ] [ no>> ] bi
311 [ condition-states ] bi@ append prune
314 : condition-at ( condition assoc -- new-condition )
315 '[ _ at ] condition-map ;