| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | ! Copyright (C) 2004, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-05-10 19:09:05 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2013-03-05 13:48:14 -05:00
										 |  |  | USING: accessors assocs classes classes.algebra | 
					
						
							|  |  |  | classes.algebra.private classes.builtin classes.private | 
					
						
							|  |  |  | combinators kernel make sequences words ;
 | 
					
						
							| 
									
										
										
										
											2008-05-10 19:09:05 -04:00
										 |  |  | IN: classes.intersection | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PREDICATE: intersection-class < class | 
					
						
							|  |  |  |     "metaclass" word-prop intersection-class eq? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 06:44:34 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-10 19:09:05 -04:00
										 |  |  | : intersection-predicate-quot ( members -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-09-06 18:10:32 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ drop t ] | 
					
						
							| 
									
										
										
										
											2008-05-10 19:09:05 -04:00
										 |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2011-11-22 02:00:52 -05:00
										 |  |  |         unclip predicate-def swap [ | 
					
						
							|  |  |  |             predicate-def [ dup ] [ not ] surround
 | 
					
						
							| 
									
										
										
										
											2008-05-10 19:09:05 -04:00
										 |  |  |             [ drop f ] | 
					
						
							|  |  |  |         ] { } map>assoc alist>quot
 | 
					
						
							| 
									
										
										
										
											2008-09-06 18:10:32 -04:00
										 |  |  |     ] if-empty ;
 | 
					
						
							| 
									
										
										
										
											2008-05-10 19:09:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-intersection-predicate ( class -- )
 | 
					
						
							|  |  |  |     dup participants intersection-predicate-quot define-predicate ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: intersection-class update-class define-intersection-predicate ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-29 19:57:23 -04:00
										 |  |  | M: intersection-class rank-class drop 5 ;
 | 
					
						
							| 
									
										
										
										
											2008-06-29 03:12:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: intersection-class instance? | 
					
						
							|  |  |  |     "participants" word-prop [ instance? ] with all? ;
 | 
					
						
							| 
									
										
										
										
											2008-07-05 18:08:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-22 21:49:18 -05:00
										 |  |  | M: anonymous-intersection instance? | 
					
						
							|  |  |  |     participants>> [ instance? ] with all? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 02:26:47 -05:00
										 |  |  | M: intersection-class normalize-class | 
					
						
							|  |  |  |     participants <anonymous-intersection> normalize-class ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-05 18:08:01 -04:00
										 |  |  | M: intersection-class (flatten-class) | 
					
						
							|  |  |  |     participants <anonymous-intersection> (flatten-class) ;
 | 
					
						
							| 
									
										
										
										
											2009-08-07 23:30:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Horribly inefficient and inaccurate | 
					
						
							|  |  |  | : intersect-flattened-classes ( seq1 seq2 -- seq3 )
 | 
					
						
							|  |  |  |     ! Only keep those in seq1 that intersect something in seq2. | 
					
						
							|  |  |  |     [ [ classes-intersect? ] with any? ] curry filter ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: anonymous-intersection (flatten-class) | 
					
						
							|  |  |  |     participants>> [ full-cover ] [ | 
					
						
							|  |  |  |         [ flatten-class keys ] | 
					
						
							|  |  |  |         [ intersect-flattened-classes ] map-reduce
 | 
					
						
							| 
									
										
										
										
											2012-07-19 12:50:09 -04:00
										 |  |  |         [ dup ,, ] each
 | 
					
						
							| 
									
										
										
										
											2009-08-07 23:30:57 -04:00
										 |  |  |     ] if-empty ;
 | 
					
						
							| 
									
										
										
										
											2010-01-20 06:44:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-03 22:17:41 -04:00
										 |  |  | M: anonymous-intersection class-name | 
					
						
							|  |  |  |     participants>> [ class-name ] map " " join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 06:44:34 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-intersection-class ( class participants -- )
 | 
					
						
							|  |  |  |     [ [ f f ] dip intersection-class define-class ] | 
					
						
							|  |  |  |     [ drop update-classes ] | 
					
						
							|  |  |  |     2bi ;
 |