aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math.lux
blob: fdc9ea946b90cd55a47dabce0e9f18b3526aff2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(.require
 [library
  [lux (.except)
   [extension (.only analysis)]
   ["[0]" static]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only exception)]]
   [data
    [text
     ["%" \\format]]
    [collection
     ["[0]" list (.use "[1]#[0]" mix)]]]
   [meta
    ["@" target]
    ["[0]" code
     ["<[1]>" \\parser]]
    [macro
     [syntax (.only syntax)]
     ["[0]" template]]
    [type
     ["[0]" check]]
    [compiler
     ["[0]" phase (.use "[1]#[0]" monad)]
     [language
      [lux
       ["[0]" analysis (.only Analysis Operation Phase)
        ["[0]" type]]]]
     [meta
      [archive (.only Archive)]]]]]]
 [/
  ["[0]" random]
  [number
   ["[0]" nat]
   ["[0]" int]
   ["[0]" rev]
   ["[0]" frac]
   ["[0]" ratio (.only Ratio)]
   ["[0]" complex (.only Complex)]]])

(exception (no_arithmetic_for [type Type])
  (exception.report
   "Type" (%.type type)))

(def (composite phase archive <+> last prevs)
  (-> Phase Archive Code Analysis (List Analysis) (Operation Analysis))
  (case <+>
    [_ {.#Text $}]
    (phase#in (list#mix (function (_ left right)
                          {analysis.#Extension $ (list left right)})
                        last
                        prevs))
    
    _
    (do phase.monad
      [[_ $] (type.inferring
              (phase archive <+>))]
      (in (list#mix (function (_ left right)
                      (analysis.reified [$ (list left right)]))
                    last
                    prevs)))))

(for @.old (these)
     (with_expansions [<@> (static.text (let [[@ _] (symbol .._)] @))
                       <ratio/0> [ratio.#numerator 0 ratio.#denominator 1]
                       <ratio/1> [ratio.#numerator 1 ratio.#denominator 1]
                       <complex/0> [complex.#real +0.0 complex.#imaginary +0.0]
                       <complex/1> [complex.#real +1.0 complex.#imaginary +0.0]]
       (these (with_template [<name> <scenarios>']
                [(with_expansions [<extension> (static.seed)
                                   <extension> (template.text [<@> " " <extension>])
                                   <scenarios> (template.spliced <scenarios>')]
                   (these (analysis (<extension> self phase archive [operands (<>.some <code>.any)])
                            (<| type.with_var
                                (function (_ [$it :it:]))
                                (do [! phase.monad]
                                  [operands (monad.each ! (|>> (phase archive) (type.expecting :it:))
                                                        operands)
                                   _ (type.inference :it:)
                                   :it: (type.check (check.identity (list) $it))]
                                  (case (list.reversed operands)
                                    (list single)
                                    (in single)

                                    (list)
                                    (`` (cond (check.subsumes? .I64 :it:)
                                              (phase.except ..no_arithmetic_for [:it:])
                                              
                                              (,, (with_template [<type> <0> <+>]
                                                    [(check.subsumes? <type> :it:)
                                                     <0>]

                                                    <scenarios>))

                                              ... else
                                              (phase.except ..no_arithmetic_for [:it:])))
                                    
                                    (list.partial last prevs)
                                    (`` (cond (check.subsumes? .I64 :it:)
                                              (phase.except ..no_arithmetic_for [:it:])

                                              (,, (with_template [<type> <0> <+>]
                                                    [(check.subsumes? <type> :it:)
                                                     (..composite phase archive (` <+>) last prevs)]

                                                    <scenarios>))

                                              ... else
                                              (phase.except ..no_arithmetic_for [:it:])))))))
                          (def .public <name>
                            (syntax (_ [operands (<>.some <code>.any)])
                              (in (list (` (<extension> (,* operands)))))))))]

                [+ [[.Nat (in (analysis.nat 0)) "lux i64 +"]
                    [.Int (in (analysis.int +0)) "lux i64 +"]
                    [.Rev (in (analysis.rev .0)) "lux i64 +"]
                    [.Frac (in (analysis.frac +0.0)) "lux f64 +"]
                    [Ratio (type.expecting Ratio (phase archive (` <ratio/0>))) ratio.+]
                    [Complex (type.expecting Complex (phase archive (` <complex/0>))) complex.+]]]
                [- [[.Nat (in (analysis.nat 0)) "lux i64 -"]
                    [.Int (in (analysis.int -0)) "lux i64 -"]
                    [.Rev (in (analysis.rev .0)) "lux i64 -"]
                    [.Frac (in (analysis.frac -0.0)) "lux f64 -"]
                    [Ratio (type.expecting Ratio (phase archive (` <ratio/0>))) ratio.-]
                    [Complex (type.expecting Complex (phase archive (` <complex/0>))) complex.-]]]
                [* [[.Nat (in (analysis.nat 1)) nat.*]
                    [.Int (in (analysis.int +1)) "lux i64 *"]
                    [.Rev (in (analysis.rev rev./1)) rev.*]
                    [.Frac (in (analysis.frac +1.0)) "lux f64 *"]
                    [Ratio (type.expecting Ratio (phase archive (` <ratio/1>))) ratio.*]
                    [Complex (type.expecting Complex (phase archive (` <complex/1>))) complex.*]]]
                [/ [[.Nat (in (analysis.nat 1)) nat./]
                    [.Int (in (analysis.int +1)) "lux i64 /"]
                    [.Rev (in (analysis.rev rev./1)) rev./]
                    [.Frac (in (analysis.frac +1.0)) "lux f64 /"]
                    [Ratio (type.expecting Ratio (phase archive (` <ratio/1>))) ratio./]
                    [Complex (type.expecting Complex (phase archive (` <complex/1>))) complex./]]]
                )
              (with_template [<name> <scenarios>']
                [(with_expansions [<extension> (static.seed)
                                   <extension> (template.text [<@> " " <extension>])
                                   <scenarios> (template.spliced <scenarios>')]
                   (these (analysis (<extension> self phase archive [left <code>.any
                                                                     right <code>.any])
                            (<| type.with_var
                                (function (_ [$it :it:]))
                                (do [! phase.monad]
                                  [left (type.expecting :it: (phase archive left))
                                   right (type.expecting :it: (phase archive right))
                                   _ (type.inference .Bit)
                                   :it: (type.check (check.identity (list) $it))]
                                  (`` (cond (check.subsumes? .I64 :it:)
                                            (phase.except ..no_arithmetic_for [:it:])

                                            (,, (with_template [<type> <+>]
                                                  [(check.subsumes? <type> :it:)
                                                   (..composite phase archive (` <+>) right (list left))]

                                                  <scenarios>))

                                            ... else
                                            (phase.except ..no_arithmetic_for [:it:]))))))
                          (def .public <name>
                            (syntax (_ [left <code>.any
                                        right <code>.any])
                              (in (list (` (<extension> (, left) (, right)))))))))]

                [= [[.Nat "lux i64 ="]
                    [.Int "lux i64 ="]
                    [.Rev "lux i64 ="]
                    [.Frac "lux f64 ="]
                    [Ratio ratio.=]
                    [Complex complex.=]]]
                [< [[.Nat nat.<]
                    [.Int "lux i64 <"]
                    [.Rev rev.<]
                    [.Frac "lux f64 <"]
                    [Ratio ratio.<]]]
                [> [[.Nat nat.>]
                    [.Int int.>]
                    [.Rev rev.>]
                    [.Frac frac.>]
                    [Ratio ratio.>]]]
                [<= [[.Nat nat.<=]
                     [.Int int.<=]
                     [.Rev rev.<=]
                     [.Frac frac.<=]
                     [Ratio ratio.<=]]]
                [>= [[.Nat nat.>=]
                     [.Int int.>=]
                     [.Rev rev.>=]
                     [.Frac frac.>=]
                     [Ratio ratio.>=]]]
                )
              (with_template [<name> <scenarios>']
                [(with_expansions [<extension> (static.seed)
                                   <extension> (template.text [<@> " " <extension>])
                                   <scenarios> (template.spliced <scenarios>')]
                   (these (analysis (<extension> self phase archive [left <code>.any
                                                                     right <code>.any])
                            (<| type.with_var
                                (function (_ [$it :it:]))
                                (do [! phase.monad]
                                  [left (type.expecting :it: (phase archive left))
                                   right (type.expecting :it: (phase archive right))
                                   _ (type.inference :it:)
                                   :it: (type.check (check.identity (list) $it))]
                                  (`` (cond (check.subsumes? .I64 :it:)
                                            (phase.except ..no_arithmetic_for [:it:])

                                            (,, (with_template [<type> <+>]
                                                  [(check.subsumes? <type> :it:)
                                                   (..composite phase archive (` <+>) right (list left))]

                                                  <scenarios>))

                                            ... else
                                            (phase.except ..no_arithmetic_for [:it:]))))))
                          (def .public <name>
                            (syntax (_ [left <code>.any
                                        right <code>.any])
                              (in (list (` (<extension> (, left) (, right)))))))))]

                [% [[.Nat nat.%]
                    [.Int "lux i64 %"]
                    [.Rev rev.%]
                    [.Frac "lux f64 %"]
                    [Ratio ratio.%]
                    [Complex complex.%]]]
                )
              )))