1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes combinators
4 combinators.short-circuit combinators.smart kernel math
5 math.order sequences sets unicode unicode.data ;
6 FROM: ascii => ascii? ;
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 ^crlf end-of-input $crlf end-of-file
16 ^unix $unix word-break ;
18 TUPLE: range-class { from read-only } { to read-only } ;
19 C: <range-class> range-class
21 TUPLE: primitive-class { class read-only } ;
22 C: <primitive-class> primitive-class
24 TUPLE: category-class { category read-only } ;
25 C: <category-class> category-class
27 TUPLE: category-range-class { category read-only } ;
28 C: <category-range-class> category-range-class
30 TUPLE: script-class { script read-only } ;
31 C: <script-class> script-class
33 GENERIC: class-member? ( obj class -- ? )
35 M: t class-member? 2drop t ; inline
37 M: integer class-member? = ; inline
39 M: range-class class-member?
40 [ from>> ] [ to>> ] bi between? ; inline
42 M: letter-class class-member?
45 M: LETTER-class class-member?
48 M: Letter-class class-member?
51 M: ascii-class class-member?
54 M: digit-class class-member?
57 : c-identifier-char? ( ch -- ? )
58 { [ alpha? ] [ CHAR: _ = ] } 1|| ;
60 M: c-identifier-class class-member?
61 drop c-identifier-char? ; inline
63 M: alpha-class class-member?
67 "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" member? ;
69 M: punctuation-class class-member?
72 : java-printable? ( ch -- ? )
73 { [ alpha? ] [ punct? ] } 1|| ;
75 M: java-printable-class class-member?
76 drop java-printable? ; inline
78 M: non-newline-blank-class class-member?
79 drop { [ blank? ] [ CHAR: \n = not ] } 1&& ; inline
81 M: control-character-class class-member?
82 drop control? ; inline
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?
92 drop hex-digit? ; inline
94 : java-blank? ( ch -- ? )
96 CHAR: \s CHAR: \t CHAR: \n
97 CHAR: \v CHAR: \a CHAR: \r
100 M: java-blank-class class-member?
101 drop java-blank? ; inline
103 M: unmatchable-class class-member?
106 M: terminator-class class-member?
107 drop "\r\n\u000085\u002029\u002028" member? ; inline
109 M: f class-member? 2drop f ; inline
111 : same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
114 M: script-class class-member?
115 [ script-of ] [ script>> ] same? ; inline
117 M: category-class class-member?
118 [ category ] [ category>> ] same? ; inline
120 M: category-range-class class-member? inline
121 [ category first ] [ category>> ] same? ; inline
123 TUPLE: not-class { class read-only } ;
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 ; inline
134 TUPLE: or-class { seq read-only } ;
136 M: or-class class-member?
137 seq>> [ class-member? ] with any? ; inline
139 TUPLE: and-class { seq read-only } ;
141 M: and-class class-member?
142 seq>> [ class-member? ] with all? ; inline
146 : flatten ( seq class -- newseq )
147 '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
149 :: sequence>instance ( seq empty class -- instance )
153 [ drop seq { } like class boa ]
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>sequence ( class-partition -- seq )
177 } cleave>array concat ;
179 : repartition ( partition -- partition' )
180 ! This could be made more efficient; only and and or are effected
181 class-partition>sequence partition-classes ;
183 : filter-not-integers ( partition -- partition' )
185 [ simples>> ] [ not-simples>> ] [ or>> ] tri
186 3append and-class boa
187 '[ [ class>> _ class-member? ] filter ] change-not-integers ;
189 : answer-ors ( partition -- partition' )
190 dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
191 '[ [ _ [ t substitute ] each ] map ] change-or ;
193 : contradiction? ( partition -- ? )
195 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
196 [ other>> f swap member? ]
199 : make-and-class ( partition -- and-class )
200 answer-ors repartition
201 [ t swap remove ] change-other
204 [ filter-not-integers class-partition>sequence members t and-class sequence>instance ] if ;
206 : <and-class> ( seq -- class )
207 dup and-class flatten partition-classes
208 dup integers>> length {
209 { 0 [ nip make-and-class ] }
210 { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
214 : filter-integers ( partition -- partition' )
216 [ simples>> ] [ not-simples>> ] [ and>> ] tri
218 '[ [ _ class-member? ] reject ] change-integers ;
220 : answer-ands ( partition -- partition' )
221 dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
222 '[ [ _ [ f substitute ] each ] map ] change-and ;
224 : tautology? ( partition -- ? )
226 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
227 [ other>> t swap member? ]
230 : make-or-class ( partition -- and-class )
231 answer-ands repartition
232 [ f swap remove ] change-other
235 [ filter-integers class-partition>sequence members f or-class sequence>instance ] if ;
237 : <or-class> ( seq -- class )
238 dup or-class flatten partition-classes
239 dup not-integers>> length {
240 { 0 [ nip make-or-class ] }
243 [ class>> '[ _ swap class-member? ] any? ] keep or
248 GENERIC: <not-class> ( class -- inverse )
250 M: object <not-class>
253 M: not-class <not-class>
256 M: and-class <not-class>
257 seq>> [ <not-class> ] map <or-class> ;
259 M: or-class <not-class>
260 seq>> [ <not-class> ] map <and-class> ;
262 M: t <not-class> drop f ;
263 M: f <not-class> drop t ;
265 : <minus-class> ( a b -- a-b )
266 <not-class> 2array <and-class> ;
268 : <sym-diff-class> ( a b -- a~b )
269 2array [ <or-class> ] [ <and-class> ] bi <minus-class> ;
271 M: primitive-class class-member?
272 class>> class-member? ; inline
274 TUPLE: condition question yes no ;
275 C: <condition> condition
277 GENERIC#: answer 2 ( class from to -- new-class )
279 M:: object answer ( class from to -- new-class )
280 class from = to class ? ;
282 : replace-compound ( class from to -- seq )
283 [ seq>> ] 2dip '[ _ _ answer ] map ;
286 replace-compound <and-class> ;
289 replace-compound <or-class> ;
292 [ class>> ] 2dip answer <not-class> ;
294 GENERIC#: substitute 1 ( class from to -- new-class )
295 M: object substitute answer ;
296 M: not-class substitute [ <not-class> ] bi@ answer ;
298 : assoc-answer ( table question answer -- new-table )
299 '[ _ _ substitute ] assoc-map sift-values ;
301 : assoc-answers ( table questions answer -- new-table )
302 '[ _ assoc-answer ] each ;
304 DEFER: make-condition
306 : (make-condition) ( table questions question -- condition )
308 [ swap [ t assoc-answer ] dip make-condition ]
309 [ swap [ f assoc-answer ] dip make-condition ] 3tri
310 2dup = [ 2nip ] [ <condition> ] if ;
312 : make-condition ( table questions -- condition )
313 [ keys ] [ unclip (make-condition) ] if-empty ;
315 GENERIC: class>questions ( class -- questions )
316 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
317 M: or-class class>questions compound-questions ;
318 M: and-class class>questions compound-questions ;
319 M: not-class class>questions class>> class>questions ;
320 M: object class>questions 1array ;
322 : table>questions ( table -- questions )
323 values [ class>questions ] gather >array t swap remove ;
325 : table>condition ( table -- condition )
326 ! input table is state => class
327 >alist dup table>questions make-condition ;
329 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition )
331 [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
332 '[ _ condition-map ] bi@ <condition>
333 ] [ call ] if ; inline recursive
335 : condition-states ( condition -- states )
337 [ yes>> ] [ no>> ] bi
338 [ condition-states ] bi@ union
341 : condition-at ( condition assoc -- new-condition )
342 '[ _ at ] condition-map ;