| 
									
										
										
										
											2009-03-26 22:27:45 -04:00
										 |  |  | ! Copyright (C) 2006, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | USING: accessors words kernel sequences namespaces make assocs | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  | hashtables definitions kernel.private classes classes.private | 
					
						
							| 
									
										
										
										
											2008-06-12 06:49:46 -04:00
										 |  |  | classes.algebra quotations arrays vocabs effects combinators | 
					
						
							| 
									
										
										
										
											2009-03-13 20:39:32 -04:00
										 |  |  | sets ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: generic | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | ! Method combination protocol | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | GENERIC: perform-combination ( word combination -- )
 | 
					
						
							| 
									
										
										
										
											2007-10-09 17:41:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | GENERIC: make-default-method ( generic combination -- method )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: generic < word | 
					
						
							|  |  |  |     "combination" word-prop >boolean ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: generic definition drop f ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | : make-generic ( word -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-02 19:50:21 -04:00
										 |  |  |     [ { "unannotated-def" } reset-props ] | 
					
						
							|  |  |  |     [ dup "combination" word-prop perform-combination ] | 
					
						
							|  |  |  |     bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : method ( class generic -- method/f )
 | 
					
						
							|  |  |  |     "methods" word-prop at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : order ( generic -- seq )
 | 
					
						
							|  |  |  |     "methods" word-prop keys sort-classes ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 03:12:44 -04:00
										 |  |  | : specific-method ( class generic -- method/f )
 | 
					
						
							| 
									
										
										
										
											2009-01-23 19:20:47 -05:00
										 |  |  |     [ nip ] [ order min-class ] 2bi
 | 
					
						
							|  |  |  |     dup [ swap method ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-04-18 17:51:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 22:37:57 -04:00
										 |  |  | GENERIC: effective-method ( generic -- method )
 | 
					
						
							| 
									
										
										
										
											2008-04-03 18:33:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-13 00:01:14 -04:00
										 |  |  | \ effective-method t "no-compile" set-word-prop | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | : next-method-class ( class generic -- class/f )
 | 
					
						
							| 
									
										
										
										
											2008-05-02 03:51:38 -04:00
										 |  |  |     order [ class<= ] with filter reverse dup length 1 =
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  |     [ drop f ] [ second ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-29 22:31:58 -04:00
										 |  |  | : next-method ( class generic -- method/f )
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  |     [ next-method-class ] keep method ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-29 03:12:44 -04:00
										 |  |  | GENERIC: next-method-quot* ( class generic combination -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-04-05 21:07:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 20:57:25 -05:00
										 |  |  | : next-method-quot ( method -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-10-01 09:20:49 -04:00
										 |  |  |     next-method-quot-cache get [ | 
					
						
							| 
									
										
										
										
											2008-11-22 20:57:25 -05:00
										 |  |  |         [ "method-class" word-prop ] | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             "method-generic" word-prop | 
					
						
							|  |  |  |             dup "combination" word-prop | 
					
						
							|  |  |  |         ] bi next-method-quot* | 
					
						
							|  |  |  |     ] cache ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-28 02:08:16 -05:00
										 |  |  | ERROR: no-next-method method ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-22 20:57:25 -05:00
										 |  |  | : (call-next-method) ( method -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-28 02:08:16 -05:00
										 |  |  |     dup next-method-quot [ call ] [ no-next-method ] ?if ;
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | TUPLE: check-method class generic ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : check-method ( class generic -- class generic )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 02:34:00 -04:00
										 |  |  |     2dup [ class? ] [ generic? ] bi* and [ | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  |         \ check-method boa throw
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  |     ] unless ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 00:33:30 -05:00
										 |  |  | : changed-generic ( class generic -- )
 | 
					
						
							|  |  |  |     changed-generics get
 | 
					
						
							|  |  |  |     [ [ [ class-or ] when* ] change-at ] [ no-compilation-unit ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : remake-generic ( generic -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-13 20:39:32 -04:00
										 |  |  |     dup outdated-generics get set-in-unit ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : remake-generics ( -- )
 | 
					
						
							|  |  |  |     outdated-generics get keys [ generic? ] filter [ make-generic ] each ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 00:33:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : with-methods ( class generic quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 02:34:00 -04:00
										 |  |  |     [ drop changed-generic ] | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  |     [ [ "methods" word-prop ] dip call ] | 
					
						
							| 
									
										
										
										
											2008-11-03 04:51:28 -05:00
										 |  |  |     [ drop remake-generic drop ] | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     3tri ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | : method-word-name ( class word -- string )
 | 
					
						
							| 
									
										
										
										
											2008-12-03 20:12:48 -05:00
										 |  |  |     [ name>> ] bi@ "=>" glue ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: method-body < word | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  |     "method-generic" word-prop >boolean ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 20:38:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: method-body stack-effect | 
					
						
							| 
									
										
										
										
											2008-03-04 23:13:30 -05:00
										 |  |  |     "method-generic" word-prop stack-effect ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 20:38:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 19:50:21 -04:00
										 |  |  | M: method-body crossref? | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     "forgotten" word-prop not ;
 | 
					
						
							| 
									
										
										
										
											2008-04-02 19:50:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  | : method-word-props ( class generic -- assoc )
 | 
					
						
							| 
									
										
										
										
											2008-03-04 23:13:30 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         "method-generic" set
 | 
					
						
							|  |  |  |         "method-class" set
 | 
					
						
							|  |  |  |     ] H{ } make-assoc ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  | : <method> ( class generic -- method )
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  |     check-method | 
					
						
							| 
									
										
										
										
											2008-03-04 23:13:30 -05:00
										 |  |  |     [ method-word-props ] 2keep
 | 
					
						
							|  |  |  |     method-word-name f <word> | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  |     swap >>props ;
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-12 06:49:46 -04:00
										 |  |  | : with-implementors ( class generic quot -- )
 | 
					
						
							|  |  |  |     [ swap implementors-map get at ] dip call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  | : reveal-method ( method class generic -- )
 | 
					
						
							| 
									
										
										
										
											2008-06-12 06:49:46 -04:00
										 |  |  |     [ [ conjoin ] with-implementors ] | 
					
						
							|  |  |  |     [ [ set-at ] with-methods ] | 
					
						
							|  |  |  |     2bi ;
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : create-method ( class generic -- method )
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  |     2dup method dup [ 2nip dup reset-generic ] [ | 
					
						
							| 
									
										
										
										
											2008-10-01 09:20:49 -04:00
										 |  |  |         drop
 | 
					
						
							|  |  |  |         [ <method> dup ] 2keep
 | 
					
						
							|  |  |  |         reveal-method | 
					
						
							|  |  |  |         reset-caches | 
					
						
							| 
									
										
										
										
											2008-02-16 19:50:16 -05:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-01 05:32:26 -04:00
										 |  |  | PREDICATE: default-method < word "default" word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  | : <default-method> ( generic combination -- method )
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
 | 
					
						
							|  |  |  |     [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | : define-default-method ( generic combination -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  |     dupd <default-method> "default-method" set-word-prop ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Definition protocol | 
					
						
							| 
									
										
										
										
											2008-02-23 23:29:29 -05:00
										 |  |  | M: method-body definer | 
					
						
							|  |  |  |     drop \ M: \ ; ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: method-body forget* | 
					
						
							| 
									
										
										
										
											2008-03-18 22:43:29 -04:00
										 |  |  |     dup "forgotten" word-prop [ drop ] [ | 
					
						
							| 
									
										
										
										
											2008-04-02 19:50:21 -04:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-09-01 05:32:26 -04:00
										 |  |  |             dup default-method? [ drop ] [ | 
					
						
							| 
									
										
										
										
											2008-06-11 21:46:53 -04:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     [ "method-class" word-prop ] | 
					
						
							|  |  |  |                     [ "method-generic" word-prop ] bi
 | 
					
						
							|  |  |  |                     2dup method | 
					
						
							|  |  |  |                 ] keep eq?
 | 
					
						
							| 
									
										
										
										
											2008-06-12 06:49:46 -04:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     [ [ delete-at ] with-methods ] | 
					
						
							| 
									
										
										
										
											2008-10-01 09:38:50 -04:00
										 |  |  |                     [ [ delete-at ] with-implementors ] 2bi
 | 
					
						
							|  |  |  |                     reset-caches | 
					
						
							| 
									
										
										
										
											2008-06-12 06:49:46 -04:00
										 |  |  |                 ] [ 2drop ] if
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |             ] if
 | 
					
						
							| 
									
										
										
										
											2008-04-02 19:50:21 -04:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2008-06-11 21:46:53 -04:00
										 |  |  |         [ call-next-method ] bi
 | 
					
						
							| 
									
										
										
										
											2008-03-18 22:43:29 -04:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 18:40:33 -04:00
										 |  |  | M: sequence update-methods ( class seq -- )
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     implementors [ | 
					
						
							| 
									
										
										
										
											2008-11-03 04:51:28 -05:00
										 |  |  |         [ changed-generic ] [ remake-generic drop ] 2bi
 | 
					
						
							| 
									
										
										
										
											2008-06-11 03:58:38 -04:00
										 |  |  |     ] with each ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 19:00:26 -04:00
										 |  |  | : define-generic ( word combination effect -- )
 | 
					
						
							|  |  |  |     [ nip swap set-stack-effect ] | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |         2dup [ "combination" word-prop ] dip = [ 2drop ] [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 [ "combination" set-word-prop ] | 
					
						
							|  |  |  |                 [ drop "methods" word-prop values forget-all ] | 
					
						
							|  |  |  |                 [ drop H{ } clone "methods" set-word-prop ] | 
					
						
							|  |  |  |                 [ define-default-method ] | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             2cleave
 | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2009-03-22 21:16:31 -04:00
										 |  |  |     [ 2drop remake-generic ] 3tri ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 20:43:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-05 00:30:59 -05:00
										 |  |  | M: generic subwords | 
					
						
							| 
									
										
										
										
											2008-04-02 19:50:21 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ "default-method" word-prop , ] | 
					
						
							|  |  |  |         [ "methods" word-prop values % ] | 
					
						
							|  |  |  |         [ "engines" word-prop % ] | 
					
						
							|  |  |  |         tri
 | 
					
						
							|  |  |  |     ] { } make ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 20:43:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-03 06:57:20 -04:00
										 |  |  | M: generic forget* | 
					
						
							|  |  |  |     [ subwords forget-all ] [ call-next-method ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-02-06 13:09:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-06 05:16:39 -04:00
										 |  |  | M: class forget-methods | 
					
						
							|  |  |  |     [ implementors ] [ [ swap method ] curry ] bi map forget-all ;
 |