aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/number.lux
blob: cad152f2b7e398a5cab56d90543f3236d18f4d0e (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
(;module: {#;doc "Implementations of common structures for Lux's primitive number types."}
  lux
  (lux (control number
                monoid
                eq
                hash
                [ord]
                enum
                interval
                codec)
       (data error)))

## [Structures]
(do-template [<type> <test>]
  [(struct: #export _ (Eq <type>)
     (def: = <test>))]

  [ Nat n.=]
  [ Int i.=]
  [ Deg d.=]
  [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.>=]
  [Deg  Eq<Deg>  d.< d.<= d.> d.>=]
  [Real Eq<Real> r.< r.<= r.> r.>=]
  )

(struct: #export _ (Number 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: + <+>)
     (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 Deg)
  (def: + d.+)
  (def: - d.-)
  (def: * d.*)
  (def: / d./)
  (def: % d.%)
  (def: (negate x) (d.- x (_lux_proc ["deg" "max-value"] [])))
  (def: abs id)
  (def: (signum x)
    (_lux_proc ["deg" "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> <ord> <top> <bottom>]
  [(struct: #export _ (Interval <type>)
     (def: ord <ord>)
     (def: top <top>)
     (def: bottom <bottom>))]

  [ Nat Ord<Nat>  (_lux_proc [ "nat" "max-value"] []) (_lux_proc [ "nat" "min-value"] [])]
  [ Int Ord<Int>  (_lux_proc [ "int" "max-value"] []) (_lux_proc [ "int" "min-value"] [])]
  [Real Ord<Real> (_lux_proc ["real" "max-value"] []) (_lux_proc ["real" "min-value"] [])]
  [ Deg Ord<Deg>  (_lux_proc [ "deg" "max-value"] []) (_lux_proc [ "deg" "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 (:: Interval<Nat> bottom)  n.max]
  [ Min@Monoid<Nat>  Nat (:: Interval<Nat> top)     n.min]
  [ Add@Monoid<Int>  Int 0                          i.+]
  [ Mul@Monoid<Int>  Int 1                          i.*]
  [ Max@Monoid<Int>  Int (:: Interval<Int> bottom)  i.max]
  [ Min@Monoid<Int>  Int (:: Interval<Int> top)     i.min]
  [Add@Monoid<Real> Real 0.0                        r.+]
  [Mul@Monoid<Real> Real 1.0                        r.*]
  [Max@Monoid<Real> Real (:: Interval<Real> bottom) r.max]
  [Min@Monoid<Real> Real (:: Interval<Real> top)    r.min]
  [ Add@Monoid<Deg>  Deg (:: Interval<Deg> bottom)  d.+]
  [ Mul@Monoid<Deg>  Deg (:: Interval<Deg> top)     d.*]
  [ Max@Monoid<Deg>  Deg (:: Interval<Deg> bottom)  d.max]
  [ Min@Monoid<Deg>  Deg (:: Interval<Deg> top)     d.min]
  )

(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"]
  [ Int [ "int" "encode"] [ "int" "decode"] "Couldn't decode Int"]
  [ Deg [ "deg" "encode"] [ "deg" "decode"] "Couldn't decode Deg"]
  [Real ["real" "encode"] ["real" "decode"] "Couldn't decode 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 value)
    (_lux_proc ["real" "hash"] [value])))

(do-template [<name> <const> <doc>]
  [(def: #export <name>
     {#;doc <doc>}
     Real
     (_lux_proc ["real" <const>] []))]

  [not-a-number      "not-a-number"      "Not-a-number."]
  [positive-infinity "positive-infinity" "Positive infinity."]
  [negative-infinity "negative-infinity" "Negative infinity."]
  )

(def: #export (not-a-number? number)
  {#;doc "Tests whether a real is actually not-a-number."}
  (-> Real Bool)
  (not (r.= number number)))

## [Values & Syntax]
(do-template [<struct> <base> <macro> <error> <char-set> <doc>]
  [(struct: #export <struct> (Codec Text Nat)
     (def: (encode value)
       (loop [input value
              output ""]
         (let [digit (assume (_lux_proc ["text" "char"] [<char-set> (n.% <base> input)]))
               output' (_lux_proc ["text" "append"] [(_lux_proc ["char" "to-text"] [digit])
                                                     output])
               input' (n./ <base> input)]
           (if (n.= +0 input')
             output'
             (recur input' output')))))

     (def: (decode repr)
       (let [input-size (_lux_proc ["text" "size"] [repr])]
         (if (n.= +0 input-size)
           (#;Left "Empty input.")
           (let [input (_lux_proc ["text" "upper-case"] [repr])]
             (loop [idx +0
                    output +0]
               (if (n.< input-size idx)
                 (let [digit (assume (_lux_proc ["text" "char"] [input idx]))]
                   (case (_lux_proc ["text" "index"]
                                    [input
                                     (_lux_proc ["char" "to-text"] [digit])])
                     #;None
                     (#;Left <error>)

                     (#;Some index)
                     (recur (n.inc idx)
                            (|> output (n.* <base>) (n.* index)))))
                 (#;Right output))))))))

   (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> +2  bin "Invalid binary syntax."
   "01"
   (doc "Given syntax for a binary number, generates a Nat."
        (bin "11001001"))]
  [Octal@Codec<Text,Nat>  +8  oct "Invalid octal syntax."
   "01234567"
   (doc "Given syntax for an octal number, generates a Nat."
        (oct "615243"))]
  [Hex@Codec<Text,Nat>    +16 hex "Invalid hexadecimal syntax."
   "0123456789ABCDEF"
   (doc "Given syntax for a hexadecimal number, generates a Nat."
        (hex "deadBEEF"))]
  )