factor/core/generic/math/math.factor

88 lines
2.2 KiB
Factor
Raw Normal View History

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.
USING: arrays generic hashtables kernel kernel.private
math namespaces sequences words quotations layouts combinators
2008-03-24 20:52:21 -04:00
sequences.private classes classes.algebra definitions ;
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
] [
number bootstrap-word class<
] if ;
: last/first ( seq -- pair ) [ peek ] [ first ] bi 2array ;
2007-09-20 18:09:08 -04:00
: math-precedence ( class -- pair )
2007-09-20 18:09:08 -04:00
{
2008-03-24 20:52:21 -04:00
{ [ dup null class< ] [ drop { -1 -1 } ] }
{ [ dup math-class? ] [ class-types last/first ] }
2007-09-20 18:09:08 -04:00
{ [ t ] [ drop { 100 100 } ] }
} cond ;
: math-class-max ( class class -- class )
[ [ math-precedence ] compare 0 > ] most ;
: (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
2007-09-27 04:50:24 -04:00
>r over r> (math-upgrade) >r (math-upgrade)
dup empty? [ [ dip ] curry [ ] like ] unless
2007-09-20 18:09:08 -04:00
r> append ;
2008-03-20 16:00:49 -04:00
ERROR: no-math-method left right generic ;
2007-09-20 18:09:08 -04: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 ]
[ 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 [
2dup math-upgrade >r
math-class-max over order min-class applicable-method
2008-03-19 20:15:43 -04:00
r> prepend
2007-09-20 18:09:08 -04:00
] [
2drop object-method
] if ;
2008-02-14 21:27:34 -05:00
: math-vtable ( picker quot -- quot )
2007-09-20 18:09:08 -04:00
[
2008-02-14 21:27:34 -05:00
>r
, \ tag ,
num-tags get [ bootstrap-type>class ]
r> compose map ,
2007-09-20 18:09:08 -04:00
\ dispatch ,
] [ ] make ; inline
TUPLE: math-combination ;
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? [
\ dup [ >r 2dup r> math-method ] math-vtable
] [
over object-method
] if nip
2008-04-02 19:50:21 -04: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 ;