factor/core/generic/generic.factor

209 lines
5.3 KiB
Factor
Raw Normal View History

2008-02-04 20:38:31 -05:00
! Copyright (C) 2006, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: words kernel sequences namespaces assocs hashtables
definitions kernel.private classes classes.private
2008-05-10 17:28:02 -04:00
classes.algebra quotations arrays vocabs effects combinators ;
2007-09-20 18:09:08 -04:00
IN: generic
! Method combination protocol
GENERIC: perform-combination ( word combination -- )
2007-10-09 17:41:35 -04:00
GENERIC: make-default-method ( generic combination -- method )
2008-03-26 19:23:19 -04:00
PREDICATE: generic < word
"combination" word-prop >boolean ;
M: generic definition drop f ;
2007-09-20 18:09:08 -04: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 ;
2008-03-26 19:23:19 -04:00
PREDICATE: method-spec < pair
2007-09-20 18:09:08 -04:00
first2 generic? swap class? and ;
: order ( generic -- seq )
"methods" word-prop keys sort-classes ;
2008-04-18 17:51:09 -04:00
: specific-method ( class word -- class )
order min-class ;
GENERIC: effective-method ( ... generic -- method )
: next-method-class ( class generic -- class/f )
2008-05-02 03:51:38 -04:00
order [ class<= ] with filter reverse dup length 1 =
[ drop f ] [ second ] if ;
: next-method ( class generic -- class/f )
[ next-method-class ] keep method ;
2008-04-05 21:07:30 -04:00
GENERIC: next-method-quot* ( class generic -- quot )
: next-method-quot ( class generic -- quot )
dup "combination" word-prop next-method-quot* ;
: (call-next-method) ( class generic -- )
next-method-quot call ;
2007-09-20 18:09:08 -04:00
TUPLE: check-method class generic ;
: check-method ( class generic -- class generic )
over class? over generic? and [
\ check-method boa throw
2008-03-16 03:43:00 -04:00
] unless ; inline
2007-09-20 18:09:08 -04:00
: affected-methods ( class generic -- seq )
"methods" word-prop swap
2008-06-11 03:58:38 -04:00
[ nip [ classes-intersect? ] [ class<= ] 2bi or ] curry assoc-filter
values ;
: update-generic ( class generic -- )
2008-06-11 03:58:38 -04:00
affected-methods [ +called+ changed-definition ] each ;
: with-methods ( class generic quot -- )
2008-06-11 03:58:38 -04:00
[ drop update-generic ]
[ [ "methods" word-prop ] dip call ]
2008-06-11 03:58:38 -04:00
[ drop make-generic drop ]
3tri ; inline
2007-09-20 18:09:08 -04:00
: method-word-name ( class word -- string )
word-name "/" rot word-name 3append ;
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
"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 )
[
"method-generic" set
"method-class" set
] H{ } make-assoc ;
2008-03-16 03:43:00 -04:00
: <method> ( class generic -- method )
check-method
[ method-word-props ] 2keep
method-word-name f <word>
2008-03-16 03:43:00 -04:00
[ set-word-props ] keep ;
: reveal-method ( method class generic -- )
[ set-at ] with-methods ;
: create-method ( class generic -- method )
2dup method dup [
2nip
] [
2008-03-16 03:43:00 -04:00
drop [ <method> dup ] 2keep reveal-method
] if ;
2007-09-20 18:09:08 -04:00
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
: define-default-method ( generic combination -- )
2008-03-16 03:43:00 -04:00
dupd <default-method> "default-method" set-word-prop ;
2007-09-20 18:09:08 -04:00
! Definition protocol
M: method-spec where
dup first2 method [ ] [ second ] ?if where ;
2007-09-20 18:09:08 -04:00
2008-02-23 23:29:29 -05:00
M: method-spec set-where
first2 method set-where ;
2007-09-20 18:09:08 -04:00
2008-02-23 23:29:29 -05:00
M: method-spec definer
2008-03-16 03:43:00 -04:00
first2 method definer ;
2007-09-20 18:09:08 -04:00
2008-02-03 15:19:07 -05:00
M: method-spec definition
2008-03-16 03:43:00 -04:00
first2 method definition ;
2007-09-20 18:09:08 -04:00
2008-02-23 23:29:29 -05:00
M: method-spec forget*
2008-03-18 22:43:29 -04:00
first2 method forget* ;
2008-02-23 23:29:29 -05:00
M: method-spec smart-usage
second smart-usage ;
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-06-11 21:46:53 -04:00
dup "default" word-prop [ drop ] [
[
[ "method-class" word-prop ]
[ "method-generic" word-prop ] bi
2dup method
] keep eq?
[ [ delete-at ] with-methods ] [ 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
M: method-body smart-usage
"method-generic" word-prop smart-usage ;
GENERIC: implementors ( class/classes -- seq )
M: class implementors
all-words [ "methods" word-prop key? ] with filter ;
2008-06-11 18:40:33 -04:00
M: sequence implementors
2007-09-20 18:09:08 -04:00
all-words [
"methods" word-prop keys
2008-06-11 18:40:33 -04:00
swap [ memq? ] curry contains?
] with filter ;
2007-09-20 18:09:08 -04:00
: forget-methods ( class -- )
2008-04-02 19:50:21 -04:00
[ implementors ] [ [ swap 2array ] curry ] bi map forget-all ;
2007-09-20 18:09:08 -04:00
2008-06-11 18:40:33 -04:00
: forget-class ( class -- )
class-usages [
{
2008-06-11 21:46:53 -04:00
[ forget-predicate ]
2008-05-28 20:34:18 -04:00
[ forget-methods ]
[ update-map- ]
[ reset-class ]
2008-06-11 18:40:33 -04:00
} cleave
] each ;
M: class forget* ( class -- )
[ forget-class ] [ call-next-method ] bi ;
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-06-11 18:40:33 -04:00
[ update-generic ] [ make-generic drop ] 2bi
2008-06-11 03:58:38 -04:00
] with each ;
: define-generic ( word combination -- )
2008-02-04 20:38:31 -05:00
over "combination" word-prop over = [
2drop
] [
2dup "combination" set-word-prop
over H{ } clone "methods" set-word-prop
dupd define-default-method
make-generic
] if ;
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
2008-02-04 20:43:10 -05:00
: xref-generics ( -- )
2008-02-05 00:30:59 -05:00
all-words [ subwords [ xref ] each ] each ;