1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs classes classes.algebra combinators
4 definitions generic kernel kernel.private math math.order
5 math.private namespaces quotations sequences words ;
8 PREDICATE: math-class < class
9 dup null bootstrap-word eq? [
12 number bootstrap-word class<=
17 : bootstrap-words ( classes -- classes' )
18 [ bootstrap-word ] map ;
20 : math-precedence ( class -- pair )
22 { fixnum integer rational real number object } bootstrap-words
23 swap [ swap class<= ] curry find drop -1 or
25 { fixnum bignum ratio float complex object } bootstrap-words
26 swap [ class<= ] curry find drop -1 or
29 : (math-upgrade) ( max class -- quot )
30 dupd = [ drop [ ] ] [ "coercer" word-prop [ ] or ] if ;
34 : math-class-max ( class1 class2 -- class )
35 [ [ math-precedence ] bi@ after? ] most ;
37 : math-upgrade ( class1 class2 -- quot )
38 [ math-class-max ] 2keep
41 dup empty? [ [ dip ] curry [ ] like ] unless
45 ERROR: no-math-method left right generic ;
47 : default-math-method ( generic -- quot )
48 [ throw-no-math-method ] curry [ ] like ;
52 : (math-method) ( generic class -- quot )
55 [ default-math-method ] ?if ;
59 : object-method ( generic -- quot )
60 object bootstrap-word (math-method) ;
62 : math-method ( word class1 class2 -- quot )
64 [ 2array [ declare ] curry nip ]
66 [ math-class-max over nearest-class (math-method) ]
76 : make-math-method-table ( classes quot: ( ... class -- ... quot ) -- alist )
77 [ bootstrap-words ] dip
78 [ [ drop ] [ call ] 2bi ] curry { } map>assoc ; inline
80 : math-alist>quot ( alist -- quot )
81 [ generic-word get object-method ] dip alist>quot ;
83 : tag-dispatch-entry ( tag picker -- quot )
84 [ "type" word-prop 1quotation [ tag ] [ eq? ] surround ] dip prepend ;
86 : tag-dispatch ( picker alist -- alist' )
87 swap [ [ tag-dispatch-entry ] curry dip ] curry assoc-map math-alist>quot ;
89 : tuple-dispatch-entry ( class picker -- quot )
90 [ 1quotation [ { tuple } declare class-of ] [ eq? ] surround ] dip prepend ;
92 : tuple-dispatch ( picker alist -- alist' )
93 swap [ [ tuple-dispatch-entry ] curry dip ] curry assoc-map math-alist>quot ;
95 : math-dispatch-step ( picker quot: ( ... class -- ... quot ) -- quot )
96 [ [ { bignum float fixnum } ] dip make-math-method-table ]
97 [ [ { ratio complex } ] dip make-math-method-table tuple-dispatch ] 2bi
98 tuple swap 2array prefix tag-dispatch ; inline
100 : fixnum-optimization ( word quot -- word quot' )
101 [ dup fixnum bootstrap-word dup math-method ]
103 ! remove redundant fixnum check since we know
104 ! both can't be fixnums in this branch
105 dup length 3 - cut unclip
106 [ length 2 - ] [ nth ] bi prefix append
108 [ if ] 2curry [ 2dup both-fixnums? ] prepend ;
112 SINGLETON: math-combination
114 M: math-combination make-default-method
115 drop default-math-method ;
117 M: math-combination perform-combination
118 drop dup generic-word [
121 [ dup ] [ math-method ] 2with math-dispatch-step
125 ] with math-dispatch-step
130 PREDICATE: math-generic < generic
131 "combination" word-prop math-combination? ;
133 M: math-generic definer drop \ MATH: f ;