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 unicode.categories combinators.short-circuit sequences
5 fry macros arrays assocs sets classes mirrors unicode.script
7 FROM: ascii => ascii? ;
8 FROM: sets => members ;
11 SINGLETONS: dot letter-class LETTER-class Letter-class digit-class
12 alpha-class non-newline-blank-class
13 ascii-class punctuation-class java-printable-class blank-class
14 control-character-class hex-digit-class java-blank-class c-identifier-class
15 unmatchable-class terminator-class word-boundary-class ;
17 SINGLETONS: beginning-of-input ^ end-of-input $ end-of-file
18 ^unix $unix word-break ;
20 TUPLE: range-class from to ;
21 C: <range-class> range-class
23 TUPLE: primitive-class class ;
24 C: <primitive-class> primitive-class
26 TUPLE: category-class category ;
27 C: <category-class> category-class
29 TUPLE: category-range-class category ;
30 C: <category-range-class> category-range-class
32 TUPLE: script-class script ;
33 C: <script-class> script-class
35 GENERIC: class-member? ( obj class -- ? )
37 M: t class-member? ( obj class -- ? ) 2drop t ;
39 M: integer class-member? ( obj class -- ? ) = ;
41 M: range-class class-member? ( obj class -- ? )
42 [ from>> ] [ to>> ] bi between? ;
44 M: letter-class class-member? ( obj class -- ? )
47 M: LETTER-class class-member? ( obj class -- ? )
50 M: Letter-class class-member? ( obj class -- ? )
53 M: ascii-class class-member? ( obj class -- ? )
56 M: digit-class class-member? ( obj class -- ? )
59 : c-identifier-char? ( ch -- ? )
60 { [ alpha? ] [ CHAR: _ = ] } 1|| ;
62 M: c-identifier-class class-member? ( obj class -- ? )
63 drop c-identifier-char? ;
65 M: alpha-class class-member? ( obj class -- ? )
69 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
71 M: punctuation-class class-member? ( obj class -- ? )
74 : java-printable? ( ch -- ? )
75 { [ alpha? ] [ punct? ] } 1|| ;
77 M: java-printable-class class-member? ( obj class -- ? )
78 drop java-printable? ;
80 M: non-newline-blank-class class-member? ( obj class -- ? )
81 drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
83 M: control-character-class class-member? ( obj class -- ? )
86 : hex-digit? ( ch -- ? )
88 [ CHAR: A CHAR: F between? ]
89 [ CHAR: a CHAR: f between? ]
90 [ CHAR: 0 CHAR: 9 between? ]
93 M: hex-digit-class class-member? ( obj class -- ? )
96 : java-blank? ( ch -- ? )
98 CHAR: \s CHAR: \t CHAR: \n
99 HEX: b HEX: 7 CHAR: \r
102 M: java-blank-class class-member? ( obj class -- ? )
105 M: unmatchable-class class-member? ( obj class -- ? )
108 M: terminator-class class-member? ( obj class -- ? )
109 drop "\r\n\u000085\u002029\u002028" member? ;
111 M: f class-member? 2drop f ;
113 : same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
116 M: script-class class-member?
117 [ script-of ] [ script>> ] same? ;
119 M: category-class class-member?
120 [ category ] [ category>> ] same? ;
122 M: category-range-class class-member?
123 [ category first ] [ category>> ] same? ;
125 TUPLE: not-class class ;
127 PREDICATE: not-integer < not-class class>> integer? ;
130 primitive-class range-class dot ;
131 PREDICATE: not-simple < not-class class>> simple-class? ;
133 M: not-class class-member?
134 class>> class-member? not ;
136 TUPLE: or-class seq ;
138 M: or-class class-member?
139 seq>> [ class-member? ] with any? ;
141 TUPLE: and-class seq ;
143 M: and-class class-member?
144 seq>> [ class-member? ] with all? ;
148 : flatten ( seq class -- newseq )
149 '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
151 :: seq>instance ( seq empty class -- instance )
155 [ drop class new seq { } like >>seq ]
158 TUPLE: class-partition integers not-integers simples not-simples and or other ;
160 : partition-classes ( seq -- class-partition )
162 [ integer? ] partition
163 [ not-integer? ] partition
164 [ simple-class? ] partition
165 [ not-simple? ] partition
166 [ and-class? ] partition
167 [ or-class? ] partition
168 class-partition boa ;
170 : class-partition>seq ( class-partition -- seq )
171 make-mirror values concat ;
173 : repartition ( partition -- partition' )
174 ! This could be made more efficient; only and and or are effected
175 class-partition>seq partition-classes ;
177 : filter-not-integers ( partition -- partition' )
179 [ simples>> ] [ not-simples>> ] [ or>> ] tri
180 3append and-class boa
181 '[ [ class>> _ class-member? ] filter ] change-not-integers ;
183 : answer-ors ( partition -- partition' )
184 dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
185 '[ [ _ [ t substitute ] each ] map ] change-or ;
187 : contradiction? ( partition -- ? )
189 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
190 [ other>> f swap member? ]
193 : make-and-class ( partition -- and-class )
194 answer-ors repartition
195 [ t swap remove ] change-other
198 [ filter-not-integers class-partition>seq members t and-class seq>instance ] if ;
200 : <and-class> ( seq -- class )
201 dup and-class flatten partition-classes
202 dup integers>> length {
203 { 0 [ nip make-and-class ] }
204 { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
208 : filter-integers ( partition -- partition' )
210 [ simples>> ] [ not-simples>> ] [ and>> ] tri
212 '[ [ _ class-member? not ] filter ] change-integers ;
214 : answer-ands ( partition -- partition' )
215 dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
216 '[ [ _ [ f substitute ] each ] map ] change-and ;
218 : tautology? ( partition -- ? )
220 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
221 [ other>> t swap member? ]
224 : make-or-class ( partition -- and-class )
225 answer-ands repartition
226 [ f swap remove ] change-other
229 [ filter-integers class-partition>seq members f or-class seq>instance ] if ;
231 : <or-class> ( seq -- class )
232 dup or-class flatten partition-classes
233 dup not-integers>> length {
234 { 0 [ nip make-or-class ] }
237 [ class>> '[ _ swap class-member? ] any? ] keep or
242 GENERIC: <not-class> ( class -- inverse )
244 M: object <not-class>
247 M: not-class <not-class>
250 M: and-class <not-class>
251 seq>> [ <not-class> ] map <or-class> ;
253 M: or-class <not-class>
254 seq>> [ <not-class> ] map <and-class> ;
256 M: t <not-class> drop f ;
257 M: f <not-class> drop t ;
259 : <minus-class> ( a b -- a-b )
260 <not-class> 2array <and-class> ;
262 : <sym-diff-class> ( a b -- a~b )
263 2array [ <or-class> ] [ <and-class> ] bi <minus-class> ;
265 M: primitive-class class-member?
266 class>> class-member? ;
268 TUPLE: condition question yes no ;
269 C: <condition> condition
271 GENERIC# answer 2 ( class from to -- new-class )
273 M:: object answer ( class from to -- new-class )
274 class from = to class ? ;
276 : replace-compound ( class from to -- seq )
277 [ seq>> ] 2dip '[ _ _ answer ] map ;
280 replace-compound <and-class> ;
283 replace-compound <or-class> ;
286 [ class>> ] 2dip answer <not-class> ;
288 GENERIC# substitute 1 ( class from to -- new-class )
289 M: object substitute answer ;
290 M: not-class substitute [ <not-class> ] bi@ answer ;
292 : assoc-answer ( table question answer -- new-table )
293 '[ _ _ substitute ] assoc-map
294 [ nip ] assoc-filter ;
296 : assoc-answers ( table questions answer -- new-table )
297 '[ _ assoc-answer ] each ;
299 DEFER: make-condition
301 : (make-condition) ( table questions question -- condition )
303 [ swap [ t assoc-answer ] dip make-condition ]
304 [ swap [ f assoc-answer ] dip make-condition ] 3tri
305 2dup = [ 2nip ] [ <condition> ] if ;
307 : make-condition ( table questions -- condition )
308 [ keys ] [ unclip (make-condition) ] if-empty ;
310 GENERIC: class>questions ( class -- questions )
311 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
312 M: or-class class>questions compound-questions ;
313 M: and-class class>questions compound-questions ;
314 M: not-class class>questions class>> class>questions ;
315 M: object class>questions 1array ;
317 : table>questions ( table -- questions )
318 values [ class>questions ] gather >array t swap remove ;
320 : table>condition ( table -- condition )
321 ! input table is state => class
322 >alist dup table>questions make-condition ;
324 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition )
326 [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
327 '[ _ condition-map ] bi@ <condition>
328 ] [ call ] if ; inline recursive
330 : condition-states ( condition -- states )
332 [ yes>> ] [ no>> ] bi
333 [ condition-states ] bi@ union
336 : condition-at ( condition assoc -- new-condition )
337 '[ _ at ] condition-map ;