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 unicode.script
9 SINGLETONS: dot 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
16 ^unix $unix word-break ;
18 TUPLE: range-class from to ;
19 C: <range-class> range-class
21 TUPLE: primitive-class class ;
22 C: <primitive-class> primitive-class
24 TUPLE: category-class category ;
25 C: <category-class> category-class
27 TUPLE: category-range-class category ;
28 C: <category-range-class> category-range-class
30 TUPLE: script-class script ;
31 C: <script-class> script-class
33 GENERIC: class-member? ( obj class -- ? )
35 M: t class-member? ( obj class -- ? ) 2drop t ;
37 M: integer class-member? ( obj class -- ? ) = ;
39 M: range-class class-member? ( obj class -- ? )
40 [ from>> ] [ to>> ] bi between? ;
42 M: letter-class class-member? ( obj class -- ? )
45 M: LETTER-class class-member? ( obj class -- ? )
48 M: Letter-class class-member? ( obj class -- ? )
51 M: ascii-class class-member? ( obj class -- ? )
54 M: digit-class class-member? ( obj class -- ? )
57 : c-identifier-char? ( ch -- ? )
58 { [ alpha? ] [ CHAR: _ = ] } 1|| ;
60 M: c-identifier-class class-member? ( obj class -- ? )
61 drop c-identifier-char? ;
63 M: alpha-class class-member? ( obj class -- ? )
67 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
69 M: punctuation-class class-member? ( obj class -- ? )
72 : java-printable? ( ch -- ? )
73 { [ alpha? ] [ punct? ] } 1|| ;
75 M: java-printable-class class-member? ( obj class -- ? )
76 drop java-printable? ;
78 M: non-newline-blank-class class-member? ( obj class -- ? )
79 drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
81 M: control-character-class class-member? ( obj class -- ? )
84 : hex-digit? ( ch -- ? )
86 [ CHAR: A CHAR: F between? ]
87 [ CHAR: a CHAR: f between? ]
88 [ CHAR: 0 CHAR: 9 between? ]
91 M: hex-digit-class class-member? ( obj class -- ? )
94 : java-blank? ( ch -- ? )
96 CHAR: \s CHAR: \t CHAR: \n
97 HEX: b HEX: 7 CHAR: \r
100 M: java-blank-class class-member? ( obj class -- ? )
103 M: unmatchable-class class-member? ( obj class -- ? )
106 M: terminator-class class-member? ( obj class -- ? )
107 drop "\r\n\u000085\u002029\u002028" member? ;
109 M: f class-member? 2drop f ;
111 : same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
114 M: script-class class-member?
115 [ script-of ] [ script>> ] same? ;
117 M: category-class class-member?
118 [ category ] [ category>> ] same? ;
120 M: category-range-class class-member?
121 [ category first ] [ category>> ] same? ;
123 TUPLE: not-class class ;
125 PREDICATE: not-integer < not-class class>> integer? ;
128 primitive-class range-class dot ;
129 PREDICATE: not-simple < not-class class>> simple-class? ;
131 M: not-class class-member?
132 class>> class-member? not ;
134 TUPLE: or-class seq ;
136 M: or-class class-member?
137 seq>> [ class-member? ] with any? ;
139 TUPLE: and-class seq ;
141 M: and-class class-member?
142 seq>> [ class-member? ] with all? ;
146 : flatten ( seq class -- newseq )
147 '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
149 :: seq>instance ( seq empty class -- instance )
153 [ drop class new seq { } like >>seq ]
156 TUPLE: class-partition integers not-integers simples not-simples and or other ;
158 : partition-classes ( seq -- class-partition )
160 [ integer? ] partition
161 [ not-integer? ] partition
162 [ simple-class? ] partition
163 [ not-simple? ] partition
164 [ and-class? ] partition
165 [ or-class? ] partition
166 class-partition boa ;
168 : class-partition>seq ( class-partition -- seq )
169 make-mirror values concat ;
171 : repartition ( partition -- partition' )
172 ! This could be made more efficient; only and and or are effected
173 class-partition>seq partition-classes ;
175 : filter-not-integers ( partition -- partition' )
177 [ simples>> ] [ not-simples>> ] [ or>> ] tri
178 3append and-class boa
179 '[ [ class>> _ class-member? ] filter ] change-not-integers ;
181 : answer-ors ( partition -- partition' )
182 dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
183 '[ [ _ [ t substitute ] each ] map ] change-or ;
185 : contradiction? ( partition -- ? )
187 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
188 [ other>> f swap member? ]
191 : make-and-class ( partition -- and-class )
192 answer-ors repartition
193 [ t swap remove ] change-other
196 [ filter-not-integers class-partition>seq prune t and-class seq>instance ] if ;
198 : <and-class> ( seq -- class )
199 dup and-class flatten partition-classes
200 dup integers>> length {
201 { 0 [ nip make-and-class ] }
202 { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
206 : filter-integers ( partition -- partition' )
208 [ simples>> ] [ not-simples>> ] [ and>> ] tri
210 '[ [ _ class-member? not ] filter ] change-integers ;
212 : answer-ands ( partition -- partition' )
213 dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
214 '[ [ _ [ f substitute ] each ] map ] change-and ;
216 : tautology? ( partition -- ? )
218 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
219 [ other>> t swap member? ]
222 : make-or-class ( partition -- and-class )
223 answer-ands repartition
224 [ f swap remove ] change-other
227 [ filter-integers class-partition>seq prune f or-class seq>instance ] if ;
229 : <or-class> ( seq -- class )
230 dup or-class flatten partition-classes
231 dup not-integers>> length {
232 { 0 [ nip make-or-class ] }
235 [ class>> '[ _ swap class-member? ] any? ] keep or
240 GENERIC: <not-class> ( class -- inverse )
242 M: object <not-class>
245 M: not-class <not-class>
248 M: and-class <not-class>
249 seq>> [ <not-class> ] map <or-class> ;
251 M: or-class <not-class>
252 seq>> [ <not-class> ] map <and-class> ;
254 M: t <not-class> drop f ;
255 M: f <not-class> drop t ;
257 : <minus-class> ( a b -- a-b )
258 <not-class> 2array <and-class> ;
260 : <sym-diff-class> ( a b -- a~b )
261 2array [ <or-class> ] [ <and-class> ] bi <minus-class> ;
263 M: primitive-class class-member?
264 class>> class-member? ;
266 TUPLE: condition question yes no ;
267 C: <condition> condition
269 GENERIC# answer 2 ( class from to -- new-class )
271 M:: object answer ( class from to -- new-class )
272 class from = to class ? ;
274 : replace-compound ( class from to -- seq )
275 [ seq>> ] 2dip '[ _ _ answer ] map ;
278 replace-compound <and-class> ;
281 replace-compound <or-class> ;
284 [ class>> ] 2dip answer <not-class> ;
286 GENERIC# substitute 1 ( class from to -- new-class )
287 M: object substitute answer ;
288 M: not-class substitute [ <not-class> ] bi@ answer ;
290 : assoc-answer ( table question answer -- new-table )
291 '[ _ _ substitute ] assoc-map
292 [ nip ] assoc-filter ;
294 : assoc-answers ( table questions answer -- new-table )
295 '[ _ assoc-answer ] each ;
297 DEFER: make-condition
299 : (make-condition) ( table questions question -- condition )
301 [ swap [ t assoc-answer ] dip make-condition ]
302 [ swap [ f assoc-answer ] dip make-condition ] 3tri
303 2dup = [ 2nip ] [ <condition> ] if ;
305 : make-condition ( table questions -- condition )
306 [ keys ] [ unclip (make-condition) ] if-empty ;
308 GENERIC: class>questions ( class -- questions )
309 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
310 M: or-class class>questions compound-questions ;
311 M: and-class class>questions compound-questions ;
312 M: not-class class>questions class>> class>questions ;
313 M: object class>questions 1array ;
315 : table>questions ( table -- questions )
316 values [ class>questions ] gather >array t swap remove ;
318 : table>condition ( table -- condition )
319 ! input table is state => class
320 >alist dup table>questions make-condition ;
322 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition )
324 [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
325 '[ _ condition-map ] bi@ <condition>
326 ] [ call ] if ; inline recursive
328 : condition-states ( condition -- states )
330 [ yes>> ] [ no>> ] bi
331 [ condition-states ] bi@ append prune
334 : condition-at ( condition assoc -- new-condition )
335 '[ _ at ] condition-map ;