blob: b2ad1df5200d62a735ee5a8d4114126e32cca833 (
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
235
236
237
238
239
240
241
242
243
244
245
246
|
## Copyright (c) Eduardo Julian. All rights reserved.
## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
## If a copy of the MPL was not distributed with this file,
## You can obtain one at http://mozilla.org/MPL/2.0/.
(;module: {#;doc "Implementations of common structures for Lux's primitive number types."}
lux
(lux (control number
monoid
eq
hash
[ord]
enum
bounded
codec)
(data error)))
## [Structures]
(do-template [<type> <test>]
[(struct: #export _ (Eq <type>)
(def: = <test>))]
[ Nat n.=]
[ Int i.=]
[Frac f.=]
[Real r.=]
)
(do-template [<type> <eq> <lt> <lte> <gt> <gte>]
[(struct: #export _ (ord;Ord <type>)
(def: eq <eq>)
(def: < <lt>)
(def: <= <lte>)
(def: > <gt>)
(def: >= <gte>))]
[ Nat Eq<Nat> n.< n.<= n.> n.>=]
[ Int Eq<Int> i.< i.<= i.> i.>=]
[Frac Eq<Frac> f.< f.<= f.> f.>=]
[Real Eq<Real> r.< r.<= r.> r.>=]
)
(struct: #export _ (Number Nat)
(def: ord Ord<Nat>)
(def: + n.+)
(def: - n.-)
(def: * n.*)
(def: / n./)
(def: % n.%)
(def: negate id)
(def: abs id)
(def: (signum x)
(case x
+0 +0
_ +1))
)
(do-template [<type> <ord> <+> <-> <*> </> <%> <=> <<> <0> <1> <-1>]
[(struct: #export _ (Number <type>)
(def: ord <ord>)
(def: + <+>)
(def: - <->)
(def: * <*>)
(def: / </>)
(def: % <%>)
(def: negate (<*> <-1>))
(def: (abs x)
(if (<<> <0> x)
(<*> <-1> x)
x))
(def: (signum x)
(cond (<=> <0> x) <0>
(<<> <0> x) <-1>
## else
<1>))
)]
[ Int Ord<Int> i.+ i.- i.* i./ i.% i.= i.< 0 1 -1]
[Real Ord<Real> r.+ r.- r.* r./ r.% r.= r.< 0.0 1.0 -1.0]
)
(struct: #export _ (Number Frac)
(def: ord Ord<Frac>)
(def: + f.+)
(def: - f.-)
(def: * f.*)
(def: / f./)
(def: % f.%)
(def: (negate x) (f.- x (_lux_proc ["frac" "max-value"] [])))
(def: abs id)
(def: (signum x)
(_lux_proc ["frac" "max-value"] []))
)
(do-template [<type> <ord> <succ> <pred>]
[(struct: #export _ (Enum <type>)
(def: ord <ord>)
(def: succ <succ>)
(def: pred <pred>))]
[Nat Ord<Nat> n.inc n.dec]
[Int Ord<Int> i.inc i.dec]
)
(do-template [<type> <top> <bottom>]
[(struct: #export _ (Bounded <type>)
(def: top <top>)
(def: bottom <bottom>))]
[ Nat (_lux_proc ["nat" "max-value"] []) (_lux_proc ["nat" "min-value"] [])]
[ Int (_lux_proc ["jvm" "getstatic:java.lang.Long:MAX_VALUE"] []) (_lux_proc ["jvm" "getstatic:java.lang.Long:MIN_VALUE"] [])]
[Real (_lux_proc ["jvm" "getstatic:java.lang.Double:MAX_VALUE"] []) (_lux_proc ["jvm" "getstatic:java.lang.Double:MIN_VALUE"] [])]
[Frac (_lux_proc ["frac" "max-value"] []) (_lux_proc ["frac" "min-value"] [])])
(do-template [<name> <type> <unit> <append>]
[(struct: #export <name> (Monoid <type>)
(def: unit <unit>)
(def: (append x y) (<append> x y)))]
[ Add@Monoid<Nat> Nat +0 n.+]
[ Mul@Monoid<Nat> Nat +1 n.*]
[ Max@Monoid<Nat> Nat (:: Bounded<Nat> bottom) n.max]
[ Min@Monoid<Nat> Nat (:: Bounded<Nat> top) n.min]
[ Add@Monoid<Int> Int 0 i.+]
[ Mul@Monoid<Int> Int 1 i.*]
[ Max@Monoid<Int> Int (:: Bounded<Int> bottom) i.max]
[ Min@Monoid<Int> Int (:: Bounded<Int> top) i.min]
[Add@Monoid<Real> Real 0.0 r.+]
[Mul@Monoid<Real> Real 1.0 r.*]
[Max@Monoid<Real> Real (:: Bounded<Real> bottom) r.max]
[Min@Monoid<Real> Real (:: Bounded<Real> top) r.min]
[Add@Monoid<Frac> Frac (:: Bounded<Frac> bottom) f.+]
[Mul@Monoid<Frac> Frac (:: Bounded<Frac> top) f.*]
[Max@Monoid<Frac> Frac (:: Bounded<Frac> bottom) f.max]
[Min@Monoid<Frac> Frac (:: Bounded<Frac> top) f.min]
)
(def: (text.replace pattern value template)
(-> Text Text Text Text)
(_lux_proc ["jvm" "invokevirtual:java.lang.String:replace:java.lang.CharSequence,java.lang.CharSequence"] [template pattern value]))
(do-template [<type> <encoder> <decoder> <error>]
[(struct: #export _ (Codec Text <type>)
(def: (encode x)
(_lux_proc <encoder> [x]))
(def: (decode input)
(case (_lux_proc <decoder> [input])
(#;Some value)
(#;Right value)
#;None
(#;Left <error>))))]
[Nat ["nat" "encode"] ["nat" "decode"] "Couldn't decode Nat"]
[Frac ["frac" "encode"] ["frac" "decode"] "Couldn't decode Frac"]
)
(def: clean-number
(-> Text Text)
(text.replace "_" ""))
(do-template [<type> <encode> <decode> <error>]
[(struct: #export _ (Codec Text <type>)
(def: (encode x)
(_lux_proc ["jvm" <encode>] [x]))
(def: (decode input)
(_lux_proc ["jvm" "try"]
[(#;Right (_lux_proc ["jvm" <decode>] [(clean-number input)]))
(lambda [e] (#;Left <error>))])))]
[ Int "invokevirtual:java.lang.Object:toString:" "invokestatic:java.lang.Long:parseLong:java.lang.String" "Couldn't parse Int"]
[Real "invokevirtual:java.lang.Object:toString:" "invokestatic:java.lang.Double:parseDouble:java.lang.String" "Couldn't parse Real"]
)
(struct: #export _ (Hash Nat)
(def: eq Eq<Nat>)
(def: hash id))
(struct: #export _ (Hash Int)
(def: eq Eq<Int>)
(def: hash int-to-nat))
(struct: #export _ (Hash Real)
(def: eq Eq<Real>)
(def: hash
(|>. (:: Codec<Text,Real> encode)
[]
(_lux_proc ["jvm" "invokevirtual:java.lang.Object:hashCode:"])
[]
(_lux_proc ["jvm" "i2l"])
int-to-nat)))
## [Values & Syntax]
(do-template [<struct> <to-proc> <radix> <macro> <error> <doc>]
[(struct: #export <struct> (Codec Text Nat)
(def: (encode value)
(_lux_proc ["jvm" <to-proc>] [(nat-to-int value)]))
(def: (decode repr)
(_lux_proc ["jvm" "try"]
[(#;Right (int-to-nat (_lux_proc ["jvm" "invokestatic:java.lang.Long:parseUnsignedLong:java.lang.String,int"] [repr (_lux_proc ["jvm" "l2i"] [<radix>])])))
(lambda [ex] (#;Left <error>))])))
(macro: #export (<macro> tokens state)
{#;doc <doc>}
(case tokens
(#;Cons [meta (#;TextS repr)] #;Nil)
(case (:: <struct> decode repr)
(#;Right value)
(#;Right [state (list [meta (#;NatS value)])])
(#;Left error)
(#;Left error))
_
(#;Left <error>)))]
[Binary@Codec<Text,Nat> "invokestatic:java.lang.Long:toBinaryString:long" 2 bin "Invalid binary syntax."
(doc "Given syntax for a binary number, generates a Nat."
(bin "11001001"))]
[Octal@Codec<Text,Nat> "invokestatic:java.lang.Long:toOctalString:long" 8 oct "Invalid octal syntax."
(doc "Given syntax for an octal number, generates a Nat."
(oct "615243"))]
[Hex@Codec<Text,Nat> "invokestatic:java.lang.Long:toHexString:long" 16 hex "Invalid hexadecimal syntax."
(doc "Given syntax for a hexadecimal number, generates a Nat."
(hex "deadBEEF"))]
)
(do-template [<name> <field> <doc>]
[(def: #export <name>
{#;doc <doc>}
Real
(_lux_proc ["jvm" <field>] []))]
[nan "getstatic:java.lang.Double:NaN" "not-a-number"]
[+inf "getstatic:java.lang.Double:POSITIVE_INFINITY" "positive infinity"]
[-inf "getstatic:java.lang.Double:NEGATIVE_INFINITY" "negative infinity"]
)
(def: #export (nan? number)
{#;doc "Tests whether a real is actually not-a-number."}
(-> Real Bool)
(not (r.= number number)))
|