| 
									
										
										
										
											2011-10-29 20:09:56 -04:00
										 |  |  | ! Copyright (C) 2004, 2011 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2011-11-22 05:44:58 -05:00
										 |  |  | USING: accessors assocs classes classes.algebra | 
					
						
							| 
									
										
										
										
											2013-03-05 13:48:14 -05:00
										 |  |  | classes.algebra.private classes.builtin classes.private | 
					
						
							|  |  |  | combinators definitions kernel kernel.private math math.private | 
					
						
							| 
									
										
										
										
											2015-08-12 11:37:06 -04:00
										 |  |  | quotations sequences sets words ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: classes.union | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: union-class < class | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     "metaclass" word-prop union-class eq? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 06:44:34 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-29 20:09:56 -04:00
										 |  |  | GENERIC: union-of-builtins? ( class -- ? )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: builtin-class union-of-builtins? drop t ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: union-class union-of-builtins? | 
					
						
							| 
									
										
										
										
											2015-07-20 03:32:42 -04:00
										 |  |  |     class-members [ union-of-builtins? ] all? ;
 | 
					
						
							| 
									
										
										
										
											2011-10-29 20:09:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: class union-of-builtins? | 
					
						
							|  |  |  |     drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : fast-union-mask ( class -- n )
 | 
					
						
							|  |  |  |     [ 0 ] dip flatten-class | 
					
						
							|  |  |  |     [ drop class>type 2^ bitor ] assoc-each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : empty-union-predicate-quot ( class -- quot )
 | 
					
						
							|  |  |  |     drop [ drop f ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : fast-union-predicate-quot ( class -- quot )
 | 
					
						
							|  |  |  |     fast-union-mask 1quotation | 
					
						
							|  |  |  |     [ tag 1 swap fixnum-shift-fast ] | 
					
						
							|  |  |  |     [ fixnum-bitand 0 eq? not ] | 
					
						
							|  |  |  |     surround ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : slow-union-predicate-quot ( class -- quot )
 | 
					
						
							| 
									
										
										
										
											2015-07-20 03:32:42 -04:00
										 |  |  |     class-members [ predicate-def ] map unclip swap
 | 
					
						
							| 
									
										
										
										
											2011-10-29 20:09:56 -04:00
										 |  |  |     [ [ dup ] prepend [ drop t ] ] { } map>assoc alist>quot ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : union-predicate-quot ( class -- quot )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-20 03:32:42 -04:00
										 |  |  |         { [ dup class-members empty? ] [ empty-union-predicate-quot ] } | 
					
						
							| 
									
										
										
										
											2011-10-29 20:09:56 -04:00
										 |  |  |         { [ dup union-of-builtins? ] [ fast-union-predicate-quot ] } | 
					
						
							|  |  |  |         [ slow-union-predicate-quot ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-union-predicate ( class -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-29 20:09:56 -04:00
										 |  |  |     dup union-predicate-quot define-predicate ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 03:46:29 -04:00
										 |  |  | M: union-class update-class define-union-predicate ;
 | 
					
						
							| 
									
										
										
										
											2008-01-31 01:49:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : (define-union-class) ( class members -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  |     f swap f union-class make-class-props (define-class) ;
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 16:17:29 -04:00
										 |  |  | ERROR: cannot-reference-self class members ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 04:10:54 -04:00
										 |  |  | GENERIC: classes-contained-by ( obj -- members )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: union-class classes-contained-by ( union -- members )
 | 
					
						
							|  |  |  |     "members" word-prop [ f ] when-empty ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object classes-contained-by | 
					
						
							| 
									
										
										
										
											2013-03-23 17:57:09 -04:00
										 |  |  |     "members" word-prop [ f ] when-empty ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 16:17:29 -04:00
										 |  |  | : check-self-reference ( class members -- class members )
 | 
					
						
							| 
									
										
										
										
											2013-03-23 17:57:09 -04:00
										 |  |  |     2dup [ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:10:54 -04:00
										 |  |  |         dup dup [ classes-contained-by ] map concat sift append
 | 
					
						
							| 
									
										
										
										
											2013-03-23 17:57:09 -04:00
										 |  |  |         2dup set= [ 2drop f ] [ nip ] if
 | 
					
						
							|  |  |  |     ] follow concat
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     member-eq? [ cannot-reference-self ] when ;
 | 
					
						
							| 
									
										
										
										
											2013-03-23 16:17:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 06:44:34 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : define-union-class ( class members -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-23 16:17:29 -04:00
										 |  |  |     [ check-self-reference (define-union-class) ] | 
					
						
							| 
									
										
										
										
											2010-01-29 08:58:39 -05:00
										 |  |  |     [ drop changed-conditionally ] | 
					
						
							| 
									
										
										
										
											2010-01-20 10:23:20 -05:00
										 |  |  |     [ drop update-classes ] | 
					
						
							|  |  |  |     2tri ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-29 19:57:23 -04:00
										 |  |  | M: union-class rank-class drop 7 ;
 | 
					
						
							| 
									
										
										
										
											2008-06-29 03:12:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: union-class instance? | 
					
						
							| 
									
										
										
										
											2009-01-29 23:19:07 -05:00
										 |  |  |     "members" word-prop [ instance? ] with any? ;
 | 
					
						
							| 
									
										
										
										
											2008-07-05 18:08:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-22 02:00:52 -05:00
										 |  |  | M: anonymous-union instance? | 
					
						
							|  |  |  |     members>> [ instance? ] with any? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-03 22:17:41 -04:00
										 |  |  | M: anonymous-union class-name | 
					
						
							|  |  |  |     members>> [ class-name ] map " " join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | M: union-class normalize-class | 
					
						
							| 
									
										
										
										
											2015-07-20 03:32:42 -04:00
										 |  |  |     class-members <anonymous-union> normalize-class ;
 | 
					
						
							| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-05 18:08:01 -04:00
										 |  |  | M: union-class (flatten-class) | 
					
						
							| 
									
										
										
										
											2015-07-20 03:32:42 -04:00
										 |  |  |     class-members <anonymous-union> (flatten-class) ;
 |