factor/library/generic/generic.factor

224 lines
5.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: generic
USING: errors hashtables kernel kernel-internals lists
namespaces parser sequences strings words vectors math
math-internals ;
! A simple single-dispatch generic word system.
2005-05-16 01:15:48 -04:00
: predicate-word ( word -- word )
2005-07-28 18:20:31 -04:00
word-name "?" append create-in ;
: define-predicate ( class predicate quot -- )
dupd define-compound
2005-07-28 18:20:31 -04:00
2dup unit "predicate" set-word-prop
swap "predicating" set-word-prop ;
: metaclass ( class -- metaclass )
"metaclass" word-prop ;
2005-08-14 18:13:16 -04:00
: types ( class -- types )
dup "types" word-prop [ ] [
"superclass" word-prop [ types ] [ [ ] ] ifte*
] ?ifte ;
: set-vtable ( definition class vtable -- )
2005-08-14 18:13:16 -04:00
>r types first r> set-nth ;
2005-08-14 23:26:40 -04:00
: 2types ( class class -- seq seq ) swap types swap types ;
2005-07-31 23:38:33 -04:00
2005-08-14 23:26:40 -04:00
: (class<) 2types contained? ;
: superclass? ( cls1 cls2 -- ? )
#! Is cls1 a superclass of cls2?
2005-08-14 17:33:45 -04:00
2dup eq? [
2drop t
2004-12-29 18:01:23 -05:00
] [
2005-08-14 23:26:40 -04:00
"superclass" word-prop dup [
superclass?
2005-08-14 17:33:45 -04:00
] [
2005-08-14 23:26:40 -04:00
2drop f
2005-08-14 17:33:45 -04:00
] ifte
2004-12-29 18:01:23 -05:00
] ifte ;
2005-08-14 23:26:40 -04:00
: child-has-super? ( cls1 cls2 -- ? )
swap "superclass" word-prop not
swap "superclass" word-prop and ;
: both-have-super? ( cls1 cls2 -- ? )
swap "superclass" word-prop
swap "superclass" word-prop and ;
: custom-class< metaclass "class<" word-prop ;
: class< ( cls1 cls2 -- ? )
#! Test if class1 is a subclass of class2.
{
{ [ 2dup eq? ] [ 2drop t ] }
{ [ dup custom-class< ] [ dup custom-class< call ] }
{ [ t ] [ (class<) ] }
} cond ;
: class-compare ( cls1 cls2 -- -1/0/1 )
2dup eq? [ 2drop 0 ] [ class< 1 -1 ? ] ifte ;
2004-12-29 18:01:23 -05:00
: methods ( generic -- alist )
"methods" word-prop hash>alist [ 2car class-compare ] sort ;
2005-04-24 20:57:37 -04:00
: order ( generic -- list )
"methods" word-prop hash-keys [ class-compare ] sort ;
2005-04-24 20:57:37 -04:00
2005-08-14 23:26:40 -04:00
: min-class ( class seq -- class/f )
#! Is this class the smallest class in the sequence?
[ dupd class-and null = not ] subset
[ class-compare neg ] sort
tuck [ class< ] all-with? [ first ] [ drop f ] ifte ;
: add-method ( generic vtable definition class -- )
#! Add the method entry to the vtable. Unlike define-method,
#! this is called at vtable build time, and in the sorted
#! order.
dup metaclass "add-method" word-prop [
[ "Metaclass is missing add-method" throw ]
] unless* call ;
: picker% "picker" word-prop % ;
: dispatcher% "dispatcher" word-prop % ;
2005-05-23 00:25:52 -04:00
: error-method ( generic -- method )
[ dup picker% literalize , \ no-method , ] make-list ;
2005-05-23 00:25:52 -04:00
2005-08-14 23:26:40 -04:00
DEFER: delegate
: empty-method ( generic -- method )
2005-05-23 00:25:52 -04:00
dup "picker" word-prop [ dup ] = [
[
[ dup delegate ] %
[ dup , ] make-list ,
error-method ,
\ ?ifte ,
] make-list
] [
error-method
] ifte ;
: <empty-vtable> ( generic -- vtable )
empty-method num-types swap <repeated> >vector ;
2004-12-29 18:01:23 -05:00
: <vtable> ( generic -- vtable )
dup <empty-vtable> over methods [
2004-12-29 18:01:23 -05:00
( generic vtable method )
>r 2dup r> unswons add-method
] each nip ;
: (small-generic) ( word methods -- quot )
[
2dup cdr (small-generic) [
>r >r picker%
r> car unswons "predicate" word-prop %
, r> , \ ifte ,
] make-list
] [
empty-method
] ifte* ;
: small-generic ( word -- def )
dup methods reverse (small-generic) ;
: big-generic ( word -- def )
[
dup picker%
dup dispatcher%
<vtable> ,
\ dispatch ,
] make-list ;
: small-generic? ( word -- ? )
dup "methods" word-prop hash-size 3 <=
swap "dispatcher" word-prop [ type ] = and ;
: make-generic ( word -- )
dup dup small-generic? [
small-generic
] [
big-generic
] ifte (define-compound) ;
: define-method ( class generic definition -- )
-rot
over metaclass word? [
over word-name " is not a class" append throw
] unless
[ "methods" word-prop set-hash ] keep make-generic ;
: forget-method ( class generic -- )
[ "methods" word-prop remove-hash ] keep make-generic ;
2004-12-29 18:01:23 -05:00
: init-methods ( word -- )
dup "methods" word-prop [
2004-12-29 18:01:23 -05:00
drop
] [
<namespace> "methods" set-word-prop
2004-12-29 18:01:23 -05:00
] ifte ;
! Defining generic words
: define-generic* ( picker dispatcher word -- )
[ swap "dispatcher" set-word-prop ] keep
[ swap "picker" set-word-prop ] keep
dup init-methods make-generic ;
: define-generic ( word -- )
>r [ dup ] [ type ] r> define-generic* ;
PREDICATE: compound generic ( word -- ? )
"dispatcher" word-prop ;
M: generic definer drop \ G: ;
2005-08-01 16:22:53 -04:00
PREDICATE: generic simple-generic ( word -- ? )
"picker" word-prop [ dup ] = ;
PREDICATE: generic 2generic ( word -- ? )
"dispatcher" word-prop [ arithmetic-type ] = ;
! Maps lists of builtin type numbers to class objects.
2005-04-10 18:58:30 -04:00
SYMBOL: typemap
SYMBOL: object
2004-12-27 22:58:43 -05:00
: lookup-union ( typelist -- class )
[ - ] sort typemap get hash [ object ] unless* ;
2004-12-27 22:58:43 -05:00
: class-or ( class class -- class )
#! Return a class that both classes are subclasses of.
2005-07-31 23:38:33 -04:00
2dup class< [
nip
] [
2dup swap class< [
drop
] [
2types seq-union lookup-union
] ifte
] ifte ;
: class-and ( class class -- class )
2005-03-07 22:11:36 -05:00
#! Return a class that is a subclass of both, or null in
#! the degenerate case.
2005-07-31 23:38:33 -04:00
2dup class< [
drop
] [
2dup swap class< [
nip
] [
2types seq-intersect lookup-union
] ifte
] ifte ;
: define-class ( class metaclass -- )
dupd "metaclass" set-word-prop
2005-08-14 18:13:16 -04:00
dup types [ - ] sort typemap get set-hash ;
2005-04-10 18:58:30 -04:00
typemap get [ <namespace> typemap set ] unless