factor/library/generic/standard-combination.factor

84 lines
2.3 KiB
Factor
Raw Normal View History

IN: generic
2005-11-27 17:45:48 -05:00
USING: arrays errors hashtables kernel kernel-internals lists
math namespaces sequences vectors words ;
2005-08-22 14:29:43 -04:00
: error-method ( picker word -- method )
2005-09-25 20:41:49 -04:00
[ no-method ] curry append ;
2005-08-22 14:29:43 -04:00
: empty-method ( picker word -- method )
over [ dup ] = [
[
2005-09-24 15:21:17 -04:00
[ dup delegate ] % dup unit , error-method , \ ?if ,
2005-08-25 15:27:38 -04:00
] [ ] make
] [
error-method
2005-09-24 15:21:17 -04:00
] if ;
2005-08-22 14:29:43 -04:00
: class-predicates ( picker assoc -- assoc )
2005-11-27 17:45:48 -05:00
[
first2 >r "predicate" word-prop append r> 2array
] 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 ;
2005-11-27 17:45:48 -05:00
: simplify-alist ( class assoc -- default assoc )
dup cdr [
2005-11-27 17:45:48 -05:00
2dup cdr car first class< [
cdr simplify-alist
] [
2005-11-27 17:45:48 -05:00
uncons >r second nip r>
2005-09-24 15:21:17 -04:00
] if
] [
2005-11-27 17:45:48 -05:00
nip car second [ ]
2005-09-24 15:21:17 -04:00
] if ;
: vtable-methods ( picker alist-seq -- alist-seq )
dup length [
2005-11-27 17:45:48 -05:00
type>class [ swap simplify-alist ] [ car second [ ] ] if*
2005-09-12 18:14:29 -04:00
>r over r> class-predicates alist>quot
] 2map nip ;
2005-09-12 18:14:29 -04:00
: <vtable> ( picker word n -- vtable )
#! n is vtable size; either num-types or num-tags.
2005-11-27 17:45:48 -05:00
>r 2dup empty-method \ object bootstrap-word swap 2array
>r methods >list r> swons r> sort-methods vtable-methods ;
2005-08-22 14:29:43 -04:00
: small-generic ( picker word -- def )
2dup methods class-predicates >r empty-method r> alist>quot ;
2005-09-12 18:14:29 -04:00
: big-generic ( picker word n dispatcher -- def )
[ >r pick % r> , <vtable> , \ dispatch , ] [ ] make ;
: tag-generic? ( word -- ? )
"methods" word-prop hash-keys [ types ] map concat
[ tag-mask < ] all? ;
: small-generic? ( word -- ? )
"methods" word-prop hash-size 3 <= ;
2005-08-22 14:29:43 -04:00
: standard-combination ( word picker -- quot )
2005-09-12 18:14:29 -04:00
swap dup tag-generic? [
num-tags \ tag big-generic
] [
dup small-generic? [
small-generic
] [
num-types \ type big-generic
2005-09-24 15:21:17 -04:00
] if
] if ;
2005-08-22 14:29:43 -04:00
: simple-combination ( word -- quot )
[ dup ] standard-combination ;
: define-generic ( word -- )
2005-08-22 14:29:43 -04:00
[ simple-combination ] define-generic* ;
PREDICATE: generic simple-generic ( word -- ? )
"combination" word-prop [ simple-combination ] = ;