factor/library/generic/standard-combination.factor

56 lines
1.6 KiB
Factor
Raw Normal View History

IN: generic
USING: 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-08-25 15:27:38 -04:00
[ swap % literalize , \ no-method , ] [ ] make ;
2005-08-22 14:29:43 -04:00
: empty-method ( picker word -- method )
over [ dup ] = [
[
2005-08-22 14:29:43 -04:00
[ dup delegate ] % dup unit , error-method , \ ?ifte ,
2005-08-25 15:27:38 -04:00
] [ ] make
] [
error-method
] ifte ;
2005-08-22 14:29:43 -04:00
: class-predicates ( picker assoc -- assoc )
[ uncons >r "predicate" word-prop append r> cons ] map-with ;
2005-08-16 15:53:30 -04:00
: alist>quot ( default alist -- quot )
2005-08-25 15:27:38 -04:00
[ unswons [ % , , \ ifte , ] [ ] make ] each ;
2005-08-16 15:53:30 -04:00
: sort-methods ( assoc -- vtable )
#! Input is a predicate -> method association.
num-types [
type>class dup
[ swap [ car classes-intersect? ] subset-with ]
[ 2drop f ] ifte
] map-with ;
2005-08-22 14:29:43 -04:00
: <vtable> ( picker word -- vtable )
2dup methods sort-methods [ class-predicates ] map-with
2005-08-16 15:53:30 -04:00
>r empty-method r> [ alist>quot ] map-with ;
2005-08-22 14:29:43 -04:00
: small-generic ( picker word -- def )
2dup methods class-predicates >r empty-method r> alist>quot ;
2005-08-22 14:29:43 -04:00
: big-generic ( picker word -- def )
2005-08-25 15:27:38 -04:00
[ over % \ type , <vtable> , \ dispatch , ] [ ] make ;
: small-generic? ( word -- ? )
"methods" word-prop hash-size 3 <= ;
2005-08-22 14:29:43 -04:00
: standard-combination ( word picker -- quot )
swap dup small-generic?
[ small-generic ] [ big-generic ] ifte ;
: 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 ] = ;