2008-03-24 20:52:21 -04:00
|
|
|
! Copyright (C) 2005, 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-09-10 21:07:00 -04:00
|
|
|
USING: arrays generic hashtables kernel kernel.private math
|
|
|
|
namespaces make sequences words quotations layouts combinators
|
2008-04-03 22:19:20 -04:00
|
|
|
sequences.private classes classes.builtin classes.algebra
|
2008-11-13 04:51:04 -05:00
|
|
|
definitions math.order math.private ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: generic.math
|
|
|
|
|
2008-03-26 19:23:19 -04:00
|
|
|
PREDICATE: math-class < class
|
2007-09-20 18:09:08 -04:00
|
|
|
dup null bootstrap-word eq? [
|
|
|
|
drop f
|
|
|
|
] [
|
2008-05-02 03:51:38 -04:00
|
|
|
number bootstrap-word class<=
|
2007-09-20 18:09:08 -04:00
|
|
|
] if ;
|
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
: last/first ( seq -- pair ) [ peek ] [ first ] bi 2array ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
: math-precedence ( class -- pair )
|
2007-09-20 18:09:08 -04:00
|
|
|
{
|
2008-05-02 03:51:38 -04:00
|
|
|
{ [ dup null class<= ] [ drop { -1 -1 } ] }
|
2008-03-24 20:52:21 -04:00
|
|
|
{ [ dup math-class? ] [ class-types last/first ] }
|
2008-04-11 13:53:22 -04:00
|
|
|
[ drop { 100 100 } ]
|
2007-09-20 18:09:08 -04:00
|
|
|
} cond ;
|
|
|
|
|
2008-07-22 02:27:52 -04:00
|
|
|
: math-class<=> ( class1 class2 -- class )
|
|
|
|
[ math-precedence ] compare +gt+ eq? ;
|
|
|
|
|
|
|
|
: math-class-max ( class1 class2 -- class )
|
|
|
|
[ math-class<=> ] most ;
|
|
|
|
|
|
|
|
: math-class-min ( class1 class2 -- class )
|
|
|
|
[ swap math-class<=> ] most ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: (math-upgrade) ( max class -- quot )
|
2007-09-27 04:50:24 -04:00
|
|
|
dupd = [ drop [ ] ] [ "coercer" word-prop [ ] or ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: math-upgrade ( class1 class2 -- quot )
|
|
|
|
[ math-class-max ] 2keep
|
2008-11-23 03:44:56 -05:00
|
|
|
[ over ] dip (math-upgrade) [
|
|
|
|
(math-upgrade)
|
|
|
|
dup empty? [ [ dip ] curry [ ] like ] unless
|
|
|
|
] dip append ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-03-20 16:00:49 -04:00
|
|
|
ERROR: no-math-method left right generic ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-04 17:20:07 -05:00
|
|
|
: default-math-method ( generic -- quot )
|
|
|
|
[ no-math-method ] curry [ ] like ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: applicable-method ( generic class -- quot )
|
2008-02-03 15:19:07 -05:00
|
|
|
over method
|
2008-03-16 03:43:00 -04:00
|
|
|
[ 1quotation ]
|
2008-02-04 17:20:07 -05:00
|
|
|
[ default-math-method ] ?if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: object-method ( generic -- quot )
|
|
|
|
object bootstrap-word applicable-method ;
|
|
|
|
|
|
|
|
: math-method ( word class1 class2 -- quot )
|
|
|
|
2dup and [
|
2008-11-23 03:44:56 -05:00
|
|
|
2dup math-upgrade
|
|
|
|
[ math-class-max over order min-class applicable-method ] dip
|
|
|
|
prepend
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
|
|
|
2drop object-method
|
|
|
|
] if ;
|
|
|
|
|
2008-11-13 04:51:04 -05:00
|
|
|
SYMBOL: picker
|
|
|
|
|
2008-02-14 21:27:34 -05:00
|
|
|
: math-vtable ( picker quot -- quot )
|
2007-09-20 18:09:08 -04:00
|
|
|
[
|
2008-11-13 04:51:04 -05:00
|
|
|
swap picker set
|
|
|
|
picker get , [ tag 0 eq? ] %
|
|
|
|
num-tags get swap [ bootstrap-type>class ] prepose map
|
|
|
|
unclip ,
|
|
|
|
[
|
|
|
|
picker get , [ tag 1 fixnum-fast ] % , \ dispatch ,
|
|
|
|
] [ ] make , \ if ,
|
2007-09-20 18:09:08 -04:00
|
|
|
] [ ] make ; inline
|
|
|
|
|
|
|
|
TUPLE: math-combination ;
|
|
|
|
|
2008-02-04 17:20:07 -05:00
|
|
|
M: math-combination make-default-method
|
|
|
|
drop default-math-method ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: math-combination perform-combination
|
|
|
|
drop
|
2008-04-02 19:50:21 -04:00
|
|
|
dup
|
2007-09-20 18:09:08 -04:00
|
|
|
\ over [
|
|
|
|
dup math-class? [
|
2008-11-23 03:44:56 -05:00
|
|
|
\ dup [ [ 2dup ] dip math-method ] math-vtable
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
|
|
|
over object-method
|
|
|
|
] if nip
|
2008-11-13 04:51:04 -05:00
|
|
|
] math-vtable nip define ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-03-26 19:23:19 -04:00
|
|
|
PREDICATE: math-generic < generic ( word -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
"combination" word-prop math-combination? ;
|
|
|
|
|
|
|
|
M: math-generic definer drop \ MATH: f ;
|