]> gitweb.factorcode.org Git - factor.git/blob - core/generic/math/math.factor
Switch to https urls
[factor.git] / core / generic / math / math.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See https://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 ;
6 IN: generic.math
7
8 PREDICATE: math-class < class
9     dup null bootstrap-word eq? [
10         drop f
11     ] [
12         number bootstrap-word class<=
13     ] if ;
14
15 <PRIVATE
16
17 : bootstrap-words ( classes -- classes' )
18     [ bootstrap-word ] map ;
19
20 : math-precedence ( class -- pair )
21     [
22         { fixnum integer rational real number object } bootstrap-words
23         swap [ swap class<= ] curry find drop -1 or
24     ] [
25         { fixnum bignum ratio float complex object } bootstrap-words
26         swap [ class<= ] curry find drop -1 or
27     ] bi 2array ;
28
29 : (math-upgrade) ( max class -- quot )
30     dupd = [ drop [ ] ] [ "coercer" word-prop [ ] or ] if ;
31
32 PRIVATE>
33
34 : math-class-max ( class1 class2 -- class )
35     [ [ math-precedence ] bi@ after? ] most ;
36
37 : math-upgrade ( class1 class2 -- quot )
38     [ math-class-max ] 2keep [ (math-upgrade) ] bi-curry@ bi
39     [ dup empty? [ [ dip ] curry ] unless ] dip [ ] append-as ;
40
41 ERROR: no-math-method left right generic ;
42
43 : default-math-method ( generic -- quot )
44     [ no-math-method ] curry [ ] like ;
45
46 <PRIVATE
47
48 : (math-method) ( generic class -- quot )
49     over ?lookup-method
50     [ 1quotation ]
51     [ default-math-method ] ?if ;
52
53 PRIVATE>
54
55 : object-method ( generic -- quot )
56     object bootstrap-word (math-method) ;
57
58 : math-method ( word class1 class2 -- quot )
59     2dup and [
60         [ 2array [ declare ] curry nip ]
61         [ math-upgrade nip ]
62         [ math-class-max over nearest-class (math-method) ]
63         3tri 3append
64     ] [
65         2drop object-method
66     ] if ;
67
68 <PRIVATE
69
70 SYMBOL: generic-word
71
72 : make-math-method-table ( classes quot: ( ... class -- ... quot ) -- alist )
73     [ bootstrap-words ] dip [ keep swap ] curry { } map>assoc ; inline
74
75 : math-alist>quot ( alist -- quot )
76     [ generic-word get object-method ] dip alist>quot ;
77
78 : tag-dispatch-entry ( tag picker -- quot )
79     [ "type" word-prop 1quotation [ tag ] [ eq? ] surround ] dip prepend ;
80
81 : tag-dispatch ( picker alist -- alist' )
82     swap [ [ tag-dispatch-entry ] curry dip ] curry assoc-map math-alist>quot ;
83
84 : tuple-dispatch-entry ( class picker -- quot )
85     [ 1quotation [ { tuple } declare class-of ] [ eq? ] surround ] dip prepend ;
86
87 : tuple-dispatch ( picker alist -- alist' )
88     swap [ [ tuple-dispatch-entry ] curry dip ] curry assoc-map math-alist>quot ;
89
90 : math-dispatch-step ( picker quot: ( ... class -- ... quot ) -- quot )
91     [ { bignum float fixnum } swap make-math-method-table ]
92     [ { ratio complex } swap make-math-method-table tuple-dispatch ] 2bi
93     tuple swap 2array prefix tag-dispatch ; inline
94
95 : fixnum-optimization ( word quot -- word quot' )
96     [ dup fixnum bootstrap-word dup math-method ]
97     [
98         ! remove redundant fixnum check since we know
99         ! both can't be fixnums in this branch
100         dup length 3 - cut unclip
101         [ length 2 - ] [ nth ] bi prefix append
102     ] bi*
103     [ if ] 2curry [ 2dup both-fixnums? ] prepend ;
104
105 PRIVATE>
106
107 SINGLETON: math-combination
108
109 M: math-combination make-default-method
110     drop default-math-method ;
111
112 M: math-combination perform-combination
113     drop dup generic-word [
114         dup [ over ] [
115             dup math-class? [
116                 [ dup ] [ math-method ] 2with math-dispatch-step
117             ] [
118                 drop object-method
119             ] if
120         ] with math-dispatch-step
121         fixnum-optimization
122         define
123     ] with-variable ;
124
125 PREDICATE: math-generic < generic
126     "combination" word-prop math-combination? ;
127
128 M: math-generic definer drop \ MATH: f ;