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? ;
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 to ;
20 C: <range-class> range-class
22 TUPLE: primitive-class class ;
23 C: <primitive-class> primitive-class
25 TUPLE: category-class category ;
26 C: <category-class> category-class
28 TUPLE: category-range-class category ;
29 C: <category-range-class> category-range-class
31 TUPLE: script-class script ;
32 C: <script-class> script-class
34 GENERIC: class-member? ( obj class -- ? )
36 M: t class-member? ( obj class -- ? ) 2drop t ;
38 M: integer class-member? ( obj class -- ? ) = ;
40 M: range-class class-member? ( obj class -- ? )
41 [ from>> ] [ to>> ] bi between? ;
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? ;
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? ;
79 M: non-newline-blank-class class-member? ( obj class -- ? )
80 drop { [ blank? ] [ CHAR: \n = not ] } 1&& ;
82 M: control-character-class class-member? ( obj class -- ? )
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 -- ? )
95 : java-blank? ( ch -- ? )
97 CHAR: \s CHAR: \t CHAR: \n
98 HEX: b HEX: 7 CHAR: \r
101 M: java-blank-class class-member? ( obj class -- ? )
104 M: unmatchable-class class-member? ( obj class -- ? )
107 M: terminator-class class-member? ( obj class -- ? )
108 drop "\r\n\u000085\u002029\u002028" member? ;
110 M: f class-member? 2drop f ;
112 : same? ( obj1 obj2 quot1: ( obj1 -- val1 ) quot2: ( obj2 -- val2 ) -- ? )
115 M: script-class class-member?
116 [ script-of ] [ script>> ] same? ;
118 M: category-class class-member?
119 [ category ] [ category>> ] same? ;
121 M: category-range-class class-member?
122 [ category first ] [ category>> ] same? ;
124 TUPLE: not-class class ;
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 ;
135 TUPLE: or-class seq ;
137 M: or-class class-member?
138 seq>> [ class-member? ] with any? ;
140 TUPLE: and-class seq ;
142 M: and-class class-member?
143 seq>> [ class-member? ] with all? ;
147 : flatten ( seq class -- newseq )
148 '[ dup _ instance? [ seq>> ] [ 1array ] if ] map concat ; inline
150 :: seq>instance ( seq empty class -- instance )
154 [ drop class new seq { } like >>seq ]
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>seq ( class-partition -- seq )
170 make-mirror values concat ;
172 : repartition ( partition -- partition' )
173 ! This could be made more efficient; only and and or are effected
174 class-partition>seq partition-classes ;
176 : filter-not-integers ( partition -- partition' )
178 [ simples>> ] [ not-simples>> ] [ or>> ] tri
179 3append and-class boa
180 '[ [ class>> _ class-member? ] filter ] change-not-integers ;
182 : answer-ors ( partition -- partition' )
183 dup [ not-integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
184 '[ [ _ [ t substitute ] each ] map ] change-or ;
186 : contradiction? ( partition -- ? )
188 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
189 [ other>> f swap member? ]
192 : make-and-class ( partition -- and-class )
193 answer-ors repartition
194 [ t swap remove ] change-other
197 [ filter-not-integers class-partition>seq prune t and-class seq>instance ] if ;
199 : <and-class> ( seq -- class )
200 dup and-class flatten partition-classes
201 dup integers>> length {
202 { 0 [ nip make-and-class ] }
203 { 1 [ integers>> first [ '[ _ swap class-member? ] all? ] keep and ] }
207 : filter-integers ( partition -- partition' )
209 [ simples>> ] [ not-simples>> ] [ and>> ] tri
211 '[ [ _ class-member? not ] filter ] change-integers ;
213 : answer-ands ( partition -- partition' )
214 dup [ integers>> ] [ not-simples>> ] [ simples>> ] tri 3append
215 '[ [ _ [ f substitute ] each ] map ] change-and ;
217 : tautology? ( partition -- ? )
219 [ [ simples>> ] [ not-simples>> ] bi intersects? ]
220 [ other>> t swap member? ]
223 : make-or-class ( partition -- and-class )
224 answer-ands repartition
225 [ f swap remove ] change-other
228 [ filter-integers class-partition>seq prune f or-class seq>instance ] if ;
230 : <or-class> ( seq -- class )
231 dup or-class flatten partition-classes
232 dup not-integers>> length {
233 { 0 [ nip make-or-class ] }
236 [ class>> '[ _ swap class-member? ] any? ] keep or
241 GENERIC: <not-class> ( class -- inverse )
243 M: object <not-class>
246 M: not-class <not-class>
249 M: and-class <not-class>
250 seq>> [ <not-class> ] map <or-class> ;
252 M: or-class <not-class>
253 seq>> [ <not-class> ] map <and-class> ;
255 M: t <not-class> drop f ;
256 M: f <not-class> drop t ;
258 : <minus-class> ( a b -- a-b )
259 <not-class> 2array <and-class> ;
261 : <sym-diff-class> ( a b -- a~b )
262 2array [ <or-class> ] [ <and-class> ] bi <minus-class> ;
264 M: primitive-class class-member?
265 class>> class-member? ;
267 TUPLE: condition question yes no ;
268 C: <condition> condition
270 GENERIC# answer 2 ( class from to -- new-class )
272 M:: object answer ( class from to -- new-class )
273 class from = to class ? ;
275 : replace-compound ( class from to -- seq )
276 [ seq>> ] 2dip '[ _ _ answer ] map ;
279 replace-compound <and-class> ;
282 replace-compound <or-class> ;
285 [ class>> ] 2dip answer <not-class> ;
287 GENERIC# substitute 1 ( class from to -- new-class )
288 M: object substitute answer ;
289 M: not-class substitute [ <not-class> ] bi@ answer ;
291 : assoc-answer ( table question answer -- new-table )
292 '[ _ _ substitute ] assoc-map
293 [ nip ] assoc-filter ;
295 : assoc-answers ( table questions answer -- new-table )
296 '[ _ assoc-answer ] each ;
298 DEFER: make-condition
300 : (make-condition) ( table questions question -- condition )
302 [ swap [ t assoc-answer ] dip make-condition ]
303 [ swap [ f assoc-answer ] dip make-condition ] 3tri
304 2dup = [ 2nip ] [ <condition> ] if ;
306 : make-condition ( table questions -- condition )
307 [ keys ] [ unclip (make-condition) ] if-empty ;
309 GENERIC: class>questions ( class -- questions )
310 : compound-questions ( class -- questions ) seq>> [ class>questions ] gather ;
311 M: or-class class>questions compound-questions ;
312 M: and-class class>questions compound-questions ;
313 M: not-class class>questions class>> class>questions ;
314 M: object class>questions 1array ;
316 : table>questions ( table -- questions )
317 values [ class>questions ] gather >array t swap remove ;
319 : table>condition ( table -- condition )
320 ! input table is state => class
321 >alist dup table>questions make-condition ;
323 : condition-map ( condition quot: ( obj -- obj' ) -- new-condition )
325 [ [ question>> ] [ yes>> ] [ no>> ] tri ] dip
326 '[ _ condition-map ] bi@ <condition>
327 ] [ call ] if ; inline recursive
329 : condition-states ( condition -- states )
331 [ yes>> ] [ no>> ] bi
332 [ condition-states ] bi@ append prune
335 : condition-at ( condition assoc -- new-condition )
336 '[ _ at ] condition-map ;