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
4 combinators.smart combinators.short-circuit locals
5 unicode.categories sequences fry macros arrays assocs sets
6 classes unicode.script unicode.data ;
7 FROM: ascii => ascii? ;
10 SINGLETONS: dot letter-class LETTER-class Letter-class digit-class
11 alpha-class non-newline-blank-class
12 ascii-class punctuation-class java-printable-class blank-class
13 control-character-class hex-digit-class java-blank-class c-identifier-class
14 unmatchable-class terminator-class word-boundary-class ;
16 SINGLETONS: beginning-of-input ^ end-of-input $ end-of-file
17 ^unix $unix word-break ;
19 TUPLE: range-class { from read-only } { to read-only } ;
20 C: <range-class> range-class
22 TUPLE: primitive-class { class read-only } ;
23 C: <primitive-class> primitive-class
25 TUPLE: category-class { category read-only } ;
26 C: <category-class> category-class
28 TUPLE: category-range-class { category read-only } ;
29 C: <category-range-class> category-range-class
31 TUPLE: script-class { script read-only } ;
32 C: <script-class> script-class
34 GENERIC: class-member? ( obj class -- ? )
36 M: t class-member? ( obj class -- ? ) 2drop t ; inline
38 M: integer class-member? ( obj class -- ? ) = ; inline
40 M: range-class class-member? ( obj class -- ? )
41 [ from>> ] [ to>> ] bi between? ; inline
43 M: letter-class class-member? ( obj class -- ? )
46 M: LETTER-class class-member? ( obj class -- ? )
49 M: Letter-class class-member? ( obj class -- ? )
52 M: ascii-class class-member? ( obj class -- ? )
55 M: digit-class class-member? ( obj class -- ? )
58 : c-identifier-char? ( ch -- ? )
59 { [ alpha? ] [ CHAR: _ = ] } 1|| ;
61 M: c-identifier-class class-member? ( obj class -- ? )
62 drop c-identifier-char? ; inline
64 M: alpha-class class-member? ( obj class -- ? )
68 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
70 M: punctuation-class class-member? ( obj class -- ? )
73 : java-printable? ( ch -- ? )
74 { [ alpha? ] [ punct? ] } 1|| ;
76 M: java-printable-class class-member? ( obj class -- ? )
77 drop java-printable? ; inline
79 M: non-newline-blank-class class-member? ( obj class -- ? )
80 drop { [ blank? ] [ CHAR: \n = not ] } 1&& ; inline
82 M: control-character-class class-member? ( obj class -- ? )
83 drop control? ; inline
85 : hex-digit? ( ch -- ? )
87 [ CHAR: A CHAR: F between? ]
88 [ CHAR: a CHAR: f between? ]
89 [ CHAR: 0 CHAR: 9 between? ]
92 M: hex-digit-class class-member? ( obj class -- ? )
93 drop hex-digit? ; inline
95 : java-blank? ( ch -- ? )
97 CHAR: \s CHAR: \t CHAR: \n
101 M: java-blank-class class-member? ( obj class -- ? )
102 drop java-blank? ; inline
104 M: unmatchable-class class-member? ( obj class -- ? )
107 M: terminator-class class-member? ( obj class -- ? )
108 drop "\r\n\u000085\u002029\u002028" member? ; inline
110 M: f class-member? 2drop f ; inline
112 : same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
115 M: script-class class-member?
116 [ script-of ] [ script>> ] same? ; inline
118 M: category-class class-member?
119 [ category ] [ category>> ] same? ; inline
121 M: category-range-class class-member? inline
122 [ category first ] [ category>> ] same? ; inline
124 TUPLE: not-class { class read-only } ;
126 PREDICATE: not-integer < not-class class>> integer? ;
129 primitive-class range-class dot ;
130 PREDICATE: not-simple < not-class class>> simple-class? ;
132 M: not-class class-member?
133 class>> class-member? not ; inline
135 TUPLE: or-class { seq read-only } ;
137 M: or-class class-member?
138 seq>> [ class-member? ] with any? ; inline
140 TUPLE: and-class { seq read-only } ;
142 M: and-class class-member?
143 seq>> [ class-member? ] with all? ; inline
147 : flatten ( seq class -- newseq )
148 '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
150 :: sequence>instance ( seq empty class -- instance )
154 [ drop seq { } like class boa ]
157 TUPLE: class-partition integers not-integers simples not-simples and or other ;
159 : partition-classes ( seq -- class-partition )
161 [ integer? ] partition
162 [ not-integer? ] partition
163 [ simple-class? ] partition
164 [ not-simple? ] partition
165 [ and-class? ] partition
166 [ or-class? ] partition
167 class-partition boa ;
169 : class-partition>sequence ( class-partition -- seq )
178 } cleave>array concat ;
180 : repartition ( partition -- partition' )
181 ! This could be made more efficient; only and and or are effected
182 class-partition>sequence partition-classes ;
184 : filter-not-integers ( partition -- partition' )
186 [ simples>> ] [ not-simples>> ] [ or>> ] tri
187 3append and-class boa
188 '[ [ class>> _ class-member? ] filter ] change-not-integers ;
190 : answer-ors ( partition -- partition' )
191 dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
192 '[ [ _ [ t substitute ] each ] map ] change-or ;
194 : contradiction? ( partition -- ? )
196 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
197 [ other>> f swap member? ]
200 : make-and-class ( partition -- and-class )
201 answer-ors repartition
202 [ t swap remove ] change-other
205 [ filter-not-integers class-partition>sequence members t and-class sequence>instance ] if ;
207 : <and-class> ( seq -- class )
208 dup and-class flatten partition-classes
209 dup integers>> length {
210 { 0 [ nip make-and-class ] }
211 { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
215 : filter-integers ( partition -- partition' )
217 [ simples>> ] [ not-simples>> ] [ and>> ] tri
219 '[ [ _ class-member? ] reject ] change-integers ;
221 : answer-ands ( partition -- partition' )
222 dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
223 '[ [ _ [ f substitute ] each ] map ] change-and ;
225 : tautology? ( partition -- ? )
227 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
228 [ other>> t swap member? ]
231 : make-or-class ( partition -- and-class )
232 answer-ands repartition
233 [ f swap remove ] change-other
236 [ filter-integers class-partition>sequence members f or-class sequence>instance ] if ;
238 : <or-class> ( seq -- class )
239 dup or-class flatten partition-classes
240 dup not-integers>> length {
241 { 0 [ nip make-or-class ] }
244 [ class>> '[ _ swap class-member? ] any? ] keep or
249 GENERIC: <not-class> ( class -- inverse )
251 M: object <not-class>
254 M: not-class <not-class>
257 M: and-class <not-class>
258 seq>> [ <not-class> ] map <or-class> ;
260 M: or-class <not-class>
261 seq>> [ <not-class> ] map <and-class> ;
263 M: t <not-class> drop f ;
264 M: f <not-class> drop t ;
266 : <minus-class> ( a b -- a-b )
267 <not-class> 2array <and-class> ;
269 : <sym-diff-class> ( a b -- a~b )
270 2array [ <or-class> ] [ <and-class> ] bi <minus-class> ;
272 M: primitive-class class-member?
273 class>> class-member? ; inline
275 TUPLE: condition question yes no ;
276 C: <condition> condition
278 GENERIC# answer 2 ( class from to -- new-class )
280 M:: object answer ( class from to -- new-class )
281 class from = to class ? ;
283 : replace-compound ( class from to -- seq )
284 [ seq>> ] 2dip '[ _ _ answer ] map ;
287 replace-compound <and-class> ;
290 replace-compound <or-class> ;
293 [ class>> ] 2dip answer <not-class> ;
295 GENERIC# substitute 1 ( class from to -- new-class )
296 M: object substitute answer ;
297 M: not-class substitute [ <not-class> ] bi@ answer ;
299 : assoc-answer ( table question answer -- new-table )
300 '[ _ _ substitute ] assoc-map sift-values ;
302 : assoc-answers ( table questions answer -- new-table )
303 '[ _ assoc-answer ] each ;
305 DEFER: make-condition
307 : (make-condition) ( table questions question -- condition )
309 [ swap [ t assoc-answer ] dip make-condition ]
310 [ swap [ f assoc-answer ] dip make-condition ] 3tri
311 2dup = [ 2nip ] [ <condition> ] if ;
313 : make-condition ( table questions -- condition )
314 [ keys ] [ unclip (make-condition) ] if-empty ;
316 GENERIC: class>questions ( class -- questions )
317 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
318 M: or-class class>questions compound-questions ;
319 M: and-class class>questions compound-questions ;
320 M: not-class class>questions class>> class>questions ;
321 M: object class>questions 1array ;
323 : table>questions ( table -- questions )
324 values [ class>questions ] gather >array t swap remove ;
326 : table>condition ( table -- condition )
327 ! input table is state => class
328 >alist dup table>questions make-condition ;
330 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition )
332 [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
333 '[ _ condition-map ] bi@ <condition>
334 ] [ call ] if ; inline recursive
336 : condition-states ( condition -- states )
338 [ yes>> ] [ no>> ] bi
339 [ condition-states ] bi@ union
342 : condition-at ( condition assoc -- new-condition )
343 '[ _ at ] condition-map ;