| 
									
										
										
										
											2009-04-26 15:58:31 -04:00
										 |  |  | ! (c)2009 Joe Groff bsd license | 
					
						
							|  |  |  | USING: arrays assocs classes classes.tuple.private combinators | 
					
						
							|  |  |  | effects.parser generic.parser kernel math math.order parser | 
					
						
							|  |  |  | quotations sequences sorting words ;
 | 
					
						
							|  |  |  | IN: pair-methods | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ERROR: no-pair-method a b generic ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ?swap ( a b ? -- a/b b/a )
 | 
					
						
							|  |  |  |     [ swap ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : method-sort-key ( pair -- key )
 | 
					
						
							|  |  |  |     first2 [ tuple-layout third ] bi@ + ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : pair-match-condition ( pair -- quot )
 | 
					
						
							|  |  |  |     first2 [ [ instance? ] swap prefix ] bi@ [ ] 2sequence
 | 
					
						
							|  |  |  |     [ 2dup ] [ bi* and ] surround ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : pair-method-cond ( pair quot -- array )
 | 
					
						
							|  |  |  |     [ pair-match-condition ] [ ] bi* 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sorted-pair-methods ( word -- alist )
 | 
					
						
							|  |  |  |     "pair-generic-methods" word-prop >alist
 | 
					
						
							| 
									
										
										
										
											2009-08-02 21:14:56 -04:00
										 |  |  |     [ first method-sort-key ] inv-sort-with ;
 | 
					
						
							| 
									
										
										
										
											2009-04-26 15:58:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : pair-generic-definition ( word -- def )
 | 
					
						
							|  |  |  |     [ sorted-pair-methods [ first2 pair-method-cond ] map ] | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     [ [ no-pair-method ] curry suffix ] bi 1quotation | 
					
						
							| 
									
										
										
										
											2011-10-24 22:33:09 -04:00
										 |  |  |     [ 2dup [ class-of ] compare +gt+ eq? ?swap ] [ cond ] surround ;
 | 
					
						
							| 
									
										
										
										
											2009-04-26 15:58:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : make-pair-generic ( word -- )
 | 
					
						
							|  |  |  |     dup pair-generic-definition define ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-pair-generic ( word effect -- )
 | 
					
						
							|  |  |  |     [ swap set-stack-effect ] | 
					
						
							|  |  |  |     [ drop H{ } clone "pair-generic-methods" set-word-prop ] | 
					
						
							|  |  |  |     [ drop make-pair-generic ] 2tri ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (PAIR-GENERIC:) ( -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-17 01:50:30 -04:00
										 |  |  |     scan-new-generic scan-effect define-pair-generic ;
 | 
					
						
							| 
									
										
										
										
											2009-04-26 15:58:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: PAIR-GENERIC: (PAIR-GENERIC:) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-pair-method ( a b pair-generic definition -- )
 | 
					
						
							|  |  |  |     [ 2array ] 2dip swap
 | 
					
						
							| 
									
										
										
										
											2014-01-05 23:30:38 -05:00
										 |  |  |     [ "pair-generic-methods" word-prop swapd set-at ] | 
					
						
							| 
									
										
										
										
											2009-04-26 15:58:31 -04:00
										 |  |  |     [ make-pair-generic ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ?prefix-swap ( quot ? -- quot' )
 | 
					
						
							|  |  |  |     [ \ swap prefix ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (PAIR-M:) ( -- )
 | 
					
						
							|  |  |  |     scan-word scan-word 2dup <=> +gt+ eq? [ | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |         ?swap scan-word parse-definition | 
					
						
							| 
									
										
										
										
											2009-04-26 15:58:31 -04:00
										 |  |  |     ] keep ?prefix-swap define-pair-method ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: PAIR-M: (PAIR-M:) ;
 |