factor/extra/multi-methods/multi-methods.factor

283 lines
7.3 KiB
Factor
Raw Normal View History

2008-01-06 10:22:26 -05:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-03-26 02:30:44 -04:00
USING: kernel math sequences vectors classes classes.algebra
2008-09-10 23:11:40 -04:00
combinators arrays words assocs parser namespaces make
2008-12-08 21:06:44 -05:00
definitions prettyprint prettyprint.backend prettyprint.custom
quotations generalizations debugger io compiler.units
kernel.private effects accessors hashtables sorting shuffle
math.order sets ;
IN: multi-methods
2008-04-08 19:51:56 -04:00
! PART I: Converting hook specializers
: canonicalize-specializer-0 ( specializer -- specializer' )
[ \ f or ] map ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
SYMBOL: args
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
SYMBOL: hooks
SYMBOL: total
: canonicalize-specializer-1 ( specializer -- specializer' )
[
[ class? ] filter
2008-04-08 19:51:56 -04:00
[ length <reversed> [ 1+ neg ] map ] keep zip
[ length args [ max ] change ] keep
]
[
[ pair? ] filter
2008-05-25 20:44:37 -04:00
[ keys [ hooks get adjoin ] each ] keep
2008-04-08 19:51:56 -04:00
] bi append ;
: canonicalize-specializer-2 ( specializer -- specializer' )
[
>r
{
{ [ dup integer? ] [ ] }
{ [ dup word? ] [ hooks get index ] }
} cond args get + r>
] assoc-map ;
: canonicalize-specializer-3 ( specializer -- specializer' )
>r total get object <array> dup <enum> r> update ;
: canonicalize-specializers ( methods -- methods' hooks )
[
[ >r canonicalize-specializer-0 r> ] assoc-map
0 args set
V{ } clone hooks set
[ >r canonicalize-specializer-1 r> ] assoc-map
hooks [ natural-sort ] change
[ >r canonicalize-specializer-2 r> ] assoc-map
args get hooks get length + total set
[ >r canonicalize-specializer-3 r> ] assoc-map
hooks get
] with-scope ;
: drop-n-quot ( n -- quot ) \ drop <repetition> >quotation ;
: prepare-method ( method n -- quot )
[ 1quotation ] [ drop-n-quot ] bi* prepend ;
: prepare-methods ( methods -- methods' prologue )
canonicalize-specializers
[ length [ prepare-method ] curry assoc-map ] keep
[ [ get ] curry ] map concat [ ] like ;
! Part II: Topologically sorting specializers
: maximal-element ( seq quot -- n elt )
dupd [
2008-04-27 23:44:42 -04:00
swapd [ call +lt+ = ] 2curry filter empty?
] 2curry find [ "Topological sort failed" throw ] unless* ;
inline
: topological-sort ( seq quot -- newseq )
>r >vector [ dup empty? not ] r>
[ dupd maximal-element >r over delete-nth r> ] curry
[ ] produce nip ; inline
2008-04-27 23:44:42 -04:00
: classes< ( seq1 seq2 -- lt/eq/gt )
[
{
2008-04-27 23:44:42 -04:00
{ [ 2dup eq? ] [ +eq+ ] }
2008-05-02 03:51:38 -04:00
{ [ 2dup [ class<= ] [ swap class<= ] 2bi and ] [ +eq+ ] }
{ [ 2dup class<= ] [ +lt+ ] }
{ [ 2dup swap class<= ] [ +gt+ ] }
2008-04-27 23:44:42 -04:00
[ +eq+ ]
} cond 2nip
2008-05-02 03:51:38 -04:00
] 2map [ +eq+ eq? not ] find nip +eq+ or ;
2008-04-08 19:51:56 -04:00
: sort-methods ( alist -- alist' )
[ [ first ] bi@ classes< ] topological-sort ;
! PART III: Creating dispatch quotation
2008-01-06 10:22:26 -05:00
: picker ( n -- quot )
{
{ 0 [ [ dup ] ] }
{ 1 [ [ over ] ] }
{ 2 [ [ pick ] ] }
2008-12-06 19:58:05 -05:00
[ 1- picker [ >r ] [ r> swap ] surround ]
2008-01-06 10:22:26 -05:00
} case ;
2008-01-06 11:13:54 -05:00
: (multi-predicate) ( class picker -- quot )
swap "predicate" word-prop append ;
: multi-predicate ( classes -- quot )
2008-01-06 11:13:54 -05:00
dup length <reversed>
[ picker 2array ] 2map
[ drop object eq? not ] assoc-filter
2008-09-06 18:15:25 -04:00
[ [ t ] ] [
2008-01-06 11:13:54 -05:00
[ (multi-predicate) ] { } assoc>map
unclip [ swap [ f ] \ if 3array append [ ] like ] reduce
2008-09-06 18:15:25 -04:00
] if-empty ;
2008-01-06 11:13:54 -05:00
2008-04-08 20:12:48 -04:00
: argument-count ( methods -- n )
keys 0 [ length max ] reduce ;
ERROR: no-method arguments generic ;
: make-default-method ( methods generic -- quot )
>r argument-count r> [ >r narray r> no-method ] 2curry ;
2008-04-08 19:51:56 -04:00
: multi-dispatch-quot ( methods generic -- quot )
2008-04-08 20:12:48 -04:00
[ make-default-method ]
[ drop [ >r multi-predicate r> ] assoc-map reverse ]
2bi alist>quot ;
2008-04-08 19:51:56 -04:00
! Generic words
PREDICATE: generic < word
"multi-methods" word-prop >boolean ;
2008-01-06 11:13:54 -05:00
: methods ( word -- alist )
"multi-methods" word-prop >alist ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
: make-generic ( generic -- quot )
2008-02-08 02:08:23 -05:00
[
2008-04-08 19:51:56 -04:00
[ methods prepare-methods % sort-methods ] keep
multi-dispatch-quot %
2008-02-08 02:08:23 -05:00
] [ ] make ;
2008-04-08 19:51:56 -04:00
: update-generic ( word -- )
dup make-generic define ;
2008-02-08 02:08:23 -05:00
2008-04-08 19:51:56 -04:00
! Methods
2008-03-26 19:23:19 -04:00
PREDICATE: method-body < word
2008-04-08 19:51:56 -04:00
"multi-method-generic" word-prop >boolean ;
2008-02-08 02:08:23 -05:00
M: method-body stack-effect
2008-04-08 19:51:56 -04:00
"multi-method-generic" word-prop stack-effect ;
2008-02-08 02:08:23 -05:00
2008-04-06 21:09:20 -04:00
M: method-body crossref?
2008-06-11 03:58:38 -04:00
"forgotten" word-prop not ;
2008-04-06 21:09:20 -04:00
2008-04-08 19:51:56 -04:00
: method-word-name ( specializer generic -- string )
[ name>> % "-" % unparse % ] "" make ;
2008-04-08 19:51:56 -04:00
: method-word-props ( specializer generic -- assoc )
2008-02-08 02:08:23 -05:00
[
2008-04-08 19:51:56 -04:00
"multi-method-generic" set
"multi-method-specializer" set
] H{ } make-assoc ;
: <method> ( specializer generic -- word )
[ method-word-props ] 2keep
2008-02-08 02:08:23 -05:00
method-word-name f <word>
2008-06-30 17:06:14 -04:00
swap >>props ;
2008-02-08 02:08:23 -05:00
2008-04-08 19:51:56 -04:00
: with-methods ( word quot -- )
over >r >r "multi-methods" word-prop
r> call r> update-generic ; inline
2008-04-08 19:51:56 -04:00
: reveal-method ( method classes generic -- )
[ set-at ] with-methods ;
2008-04-08 19:51:56 -04:00
: method ( classes word -- method )
"multi-methods" word-prop at ;
2008-01-06 11:13:54 -05:00
2008-04-08 19:51:56 -04:00
: create-method ( classes generic -- method )
2dup method dup [
2nip
] [
drop [ <method> dup ] 2keep reveal-method
2008-01-06 11:13:54 -05:00
] if ;
: niceify-method ( seq -- seq )
[ dup \ f eq? [ drop f ] when ] map ;
2008-01-06 11:13:54 -05:00
M: no-method error.
"Type check error" print
nl
2008-04-08 19:51:56 -04:00
"Generic word " write dup generic>> pprint
2008-01-06 11:13:54 -05:00
" does not have a method applicable to inputs:" print
2008-04-08 19:51:56 -04:00
dup arguments>> short.
2008-01-06 11:13:54 -05:00
nl
"Inputs have signature:" print
2008-04-08 19:51:56 -04:00
dup arguments>> [ class ] map niceify-method .
2008-01-06 11:13:54 -05:00
nl
2008-04-08 19:51:56 -04:00
"Available methods: " print
2008-04-08 20:12:48 -04:00
generic>> methods canonicalize-specializers drop sort-methods
keys [ niceify-method ] map stack. ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
: forget-method ( specializer generic -- )
[ delete-at ] with-methods ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
: method>spec ( method -- spec )
[ "multi-method-specializer" word-prop ]
[ "multi-method-generic" word-prop ] bi prefix ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
: define-generic ( word -- )
dup "multi-methods" word-prop [
drop
2008-02-08 02:08:23 -05:00
] [
2008-04-08 19:51:56 -04:00
[ H{ } clone "multi-methods" set-word-prop ]
[ update-generic ]
2008-04-08 20:12:48 -04:00
bi
2008-02-08 02:08:23 -05:00
] if ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
! Syntax
: GENERIC:
2008-04-08 19:51:56 -04:00
CREATE define-generic ; parsing
2008-04-08 19:51:56 -04:00
: parse-method ( -- quot classes generic )
parse-definition [ 2 tail ] [ second ] [ first ] tri ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
: create-method-in ( specializer generic -- method )
create-method dup save-location f set-word ;
2008-01-06 10:22:26 -05:00
: CREATE-METHOD ( -- method )
2008-04-08 19:51:56 -04:00
scan-word scan-object swap create-method-in ;
2008-02-08 02:08:23 -05:00
: (METHOD:) ( -- method def ) CREATE-METHOD parse-definition ;
2008-04-08 19:51:56 -04:00
: METHOD: (METHOD:) define ; parsing
2008-01-06 10:22:26 -05:00
! For compatibility
: M:
2008-04-08 19:51:56 -04:00
scan-word 1array scan-word create-method-in
parse-definition
define ; parsing
2008-01-06 10:22:26 -05:00
! Definition protocol. We qualify core generics here
USE: qualified
QUALIFIED: syntax
2008-04-08 19:51:56 -04:00
syntax:M: generic definer drop \ GENERIC: f ;
2008-01-06 10:22:26 -05:00
2008-04-08 19:51:56 -04:00
syntax:M: generic definition drop f ;
2008-01-06 10:22:26 -05:00
2008-03-26 19:23:19 -04:00
PREDICATE: method-spec < array
2008-01-06 10:22:26 -05:00
unclip generic? >r [ class? ] all? r> and ;
syntax:M: method-spec where
2008-04-08 19:51:56 -04:00
dup unclip method [ ] [ first ] ?if where ;
2008-01-06 10:22:26 -05:00
syntax:M: method-spec set-where
2008-04-08 19:51:56 -04:00
unclip method set-where ;
2008-01-06 10:22:26 -05:00
syntax:M: method-spec definer
2008-04-08 19:51:56 -04:00
unclip method definer ;
2008-01-06 10:22:26 -05:00
syntax:M: method-spec definition
2008-04-08 19:51:56 -04:00
unclip method definition ;
2008-01-06 10:22:26 -05:00
syntax:M: method-spec synopsis*
2008-04-08 19:51:56 -04:00
unclip method synopsis* ;
2008-01-06 10:22:26 -05:00
2008-01-14 19:11:32 -05:00
syntax:M: method-spec forget*
2008-04-08 19:51:56 -04:00
unclip method forget* ;
syntax:M: method-body definer
drop \ METHOD: \ ; ;
syntax:M: method-body synopsis*
dup definer.
[ "multi-method-generic" word-prop pprint-word ]
[ "multi-method-specializer" word-prop pprint* ] bi ;