factor/library/generic/standard-combination.factor

100 lines
3.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2006-05-15 01:01:47 -04:00
USING: arrays errors hashtables kernel kernel-internals
2005-11-27 17:45:48 -05:00
math namespaces sequences vectors words ;
IN: generic
: picker ( dispatch# -- quot )
{ [ dup ] [ over ] [ pick ] } nth ;
: unpicker ( dispatch# -- quot )
{ [ nip ] [ >r nip r> swap ] [ >r >r nip r> r> -rot ] } nth ;
TUPLE: no-method object generic ;
: no-method ( object generic -- ) <no-method> throw ;
: error-method ( dispatch# word -- method )
>r picker r> [ no-method ] curry append ;
: empty-method ( dispatch# word -- method )
[
over picker % [ delegate dup ] %
over unpicker over add ,
[ drop ] -rot error-method append , \ if ,
] [ ] make ;
2005-08-22 14:29:43 -04:00
: class-predicates ( picker assoc -- assoc )
2005-11-27 17:45:48 -05:00
[
first2 >r >r picker r> "predicate" word-prop append
r> 2array
2005-11-27 17:45:48 -05:00
] map-with ;
: sort-methods ( assoc n -- vtable )
2005-08-16 15:53:30 -04:00
#! Input is a predicate -> method association.
2005-11-27 17:45:48 -05:00
#! n is vtable size (either num-types or num-tags).
[
2005-11-24 19:02:20 -05:00
type>class [ object bootstrap-word ] unless*
2005-11-27 17:45:48 -05:00
swap [ first classes-intersect? ] subset-with
2005-08-16 15:53:30 -04:00
] map-with ;
: (simplify-alist) ( class i assoc -- default assoc )
2dup length 1- = [
nth second [ ] rot drop
] [
3dup >r 1+ r> nth first class< [
>r 1+ r> (simplify-alist)
] [
[ nth second ] 2keep swap 1+ tail rot drop
2005-09-24 15:21:17 -04:00
] if
] if ;
: simplify-alist ( class assoc -- default assoc )
0 swap (simplify-alist) ;
: methods* ( dispatch# word -- assoc )
#! Make a class->method association, together with a
#! default delegating method at the end.
empty-method object bootstrap-word swap 2array 1array
swap methods append ;
: small-generic ( dispatch# word -- def )
2dup methods* object bootstrap-word swap simplify-alist
swapd class-predicates alist>quot ;
: vtable-methods ( dispatch# alist-seq -- alist-seq )
dup length [
type>class
[ swap simplify-alist ] [ first second [ ] ] if*
2005-09-12 18:14:29 -04:00
>r over r> class-predicates alist>quot
] 2map nip ;
: <vtable> ( dispatch# word n -- vtable )
2005-09-12 18:14:29 -04:00
#! n is vtable size; either num-types or num-tags.
>r 2dup methods* r> sort-methods vtable-methods ;
: big-generic ( dispatch# word n dispatcher -- def )
[ >r pick picker % r> , <vtable> , \ dispatch , ] [ ] make ;
2005-09-12 18:14:29 -04:00
: tag-generic? ( word -- ? )
2006-05-29 20:03:06 -04:00
#! If all the types we dispatch upon can be identified
#! based on tag alone, we change the dispatcher primitive
#! from 'type' to 'tag'.
generic-tags [ tag-mask < ] all? ;
2006-05-29 20:03:06 -04:00
: small-generic? ( word -- ? ) generic-tags length 3 <= ;
: standard-combination ( word dispatch# -- quot )
2006-01-09 16:19:40 -05:00
swap {
{ [ dup tag-generic? ] [ num-tags \ tag big-generic ] }
{ [ dup small-generic? ] [ small-generic ] }
{ [ t ] [ num-types \ type big-generic ] }
} cond ;
2005-08-22 14:29:43 -04:00
: define-generic ( word -- )
[ 0 standard-combination ] define-generic* ;
2005-08-22 14:29:43 -04:00
PREDICATE: generic standard-generic
1 swap "combination" word-prop ?nth
\ standard-combination eq? ;