1 ! Copyright (C) 2005, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays errors generic hashtables kernel kernel-internals
5 math namespaces sequences words ;
7 PREDICATE: class math-class ( object -- ? )
8 dup null bootstrap-word eq? [
11 number bootstrap-word class<
14 : math-class-compare ( class class -- n )
17 [ types last/first ] [ drop { 100 100 } ] if
20 : math-class-max ( class class -- class )
21 [ math-class-compare 0 > ] 2keep ? ;
23 : (math-upgrade) ( max class -- quot )
27 "coercer" word-prop [ [ ] ] unless*
30 : math-upgrade ( class1 class2 -- quot )
31 [ math-class-max ] 2keep
32 >r over r> (math-upgrade)
33 >r (math-upgrade) dup empty? [ 1 make-dip ] unless
36 TUPLE: no-math-method left right generic ;
38 : no-math-method ( left right generic -- * )
39 <no-math-method> throw ;
41 : applicable-method ( generic class -- quot )
42 over method method-def
43 [ ] [ [ no-math-method ] curry ] ?if ;
45 : object-method ( generic -- quot )
46 object bootstrap-word applicable-method ;
48 : math-method ( word class1 class2 -- quot )
51 math-class-max over order min-class applicable-method
57 : math-vtable* ( picker max quot -- quot )
60 [ >r [ type>class ] map r> map % ] { } make ,
64 : math-vtable ( picker quot -- quot )
65 num-tags swap math-vtable* ; inline
67 : math-combination ( word -- quot )
70 \ dup [ >r 2dup r> math-method ] math-vtable
76 PREDICATE: generic 2generic ( word -- ? )
77 "combination" word-prop [ math-combination ] = ;