aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/math/number/int.lux
blob: b121fc216ee8317b092eff05852be1e60de34791 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
(.module:
  [lux #*
   [abstract
    [hash (#+ Hash)]
    [enum (#+ Enum)]
    [interval (#+ Interval)]
    [monoid (#+ Monoid)]
    [equivalence (#+ Equivalence)]
    [codec (#+ Codec)]
    [predicate (#+ Predicate)]
    ["." order (#+ Order)]]
   [control
    ["." try (#+ Try)]]
   [data
    [text (#+ Char)]
    ["." maybe]]]
  ["." // #_
   ["#." nat]
   ["#." i64]])

(def: #export (= reference sample)
  {#.doc "Int(eger) equivalence."}
  (-> Int Int Bit)
  ("lux i64 =" reference sample))

(def: #export (< reference sample)
  {#.doc "Int(eger) less-than."}
  (-> Int Int Bit)
  ("lux i64 <" reference sample))

(def: #export (<= reference sample)
  {#.doc "Int(eger) less-than or equal."}
  (-> Int Int Bit)
  (if ("lux i64 <" reference sample)
    #1
    ("lux i64 =" reference sample)))

(def: #export (> reference sample)
  {#.doc "Int(eger) greater-than."}
  (-> Int Int Bit)
  ("lux i64 <" sample reference))

(def: #export (>= reference sample)
  {#.doc "Int(eger) greater-than or equal."}
  (-> Int Int Bit)
  (if ("lux i64 <" sample reference)
    #1
    ("lux i64 =" reference sample)))

(template [<comparison> <name>]
  [(def: #export <name>
     (Predicate Int)
     (<comparison> +0))]

  [..> positive?]
  [..< negative?]
  [..= zero?]
  )

(template [<name> <test> <doc>]
  [(def: #export (<name> left right)
     {#.doc <doc>}
     (-> Int Int Int)
     (if (<test> right left)
       left
       right))]

  [min ..< "Int(eger) minimum."]
  [max ..> "Int(eger) maximum."]
  )

(template [<name> <op> <doc>]
  [(def: #export (<name> param subject)
     {#.doc <doc>}
     (-> Int Int Int)
     (<op> param subject))]

  [+ "lux i64 +" "Int(eger) addition."]
  [- "lux i64 -" "Int(eger) substraction."]
  [* "lux i64 *" "Int(eger) multiplication."]
  [/ "lux i64 /" "Int(eger) division."]
  [% "lux i64 %" "Int(eger) remainder."]
  )

(def: #export (/% param subject)
  (-> Int Int [Int Int])
  [(../ param subject)
   (..% param subject)])

(def: #export (negate value)
  (-> Int Int)
  (..- value +0))

(def: #export (abs x)
  (-> Int Int)
  (if (..< +0 x)
    (..* -1 x)
    x))

(def: #export (signum x)
  (-> Int Int)
  (cond (..= +0 x) +0
        (..< +0 x) -1
        ## else
        +1))

## https://rob.conery.io/2018/08/21/mod-and-remainder-are-not-the-same/
(def: #export (mod divisor dividend)
  (All [m] (-> Int Int Int))
  (let [remainder (..% divisor dividend)]
    (if (or (and (..< +0 divisor)
                 (..> +0 remainder))
            (and (..> +0 divisor)
                 (..< +0 remainder)))
      (..+ divisor remainder)
      remainder)))

(def: #export even?
  (-> Int Bit)
  (|>> (..% +2) ("lux i64 =" +0)))

(def: #export odd?
  (-> Int Bit)
  (|>> ..even? not))

(def: #export (gcd a b)
  {#.doc "Greatest Common Divisor."}
  (-> Int Int Int)
  (case b
    +0 a
    _ (gcd b (..% b a))))

(def: #export (co-prime? a b)
  (-> Int Int Bit)
  (..= +1 (..gcd a b)))

## https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
(def: #export (extended_gcd a b)
  {#.doc "Extended euclidean algorithm."}
  (-> Int Int [[Int Int] Int])
  (loop [x +1 x1 +0
         y +0 y1 +1
         a1 a b1 b]
    (case b1
      +0 [[x y] a1]
      _ (let [q (/ b1 a1)]
          (recur x1 (- (* q x1) x)
                 y1 (- (* q y1) y)
                 b1 (- (* q b1) a1))))))

(def: #export (lcm a b)
  {#.doc "Least Common Multiple."}
  (-> Int Int Int)
  (case [a b]
    (^or [_ +0] [+0 _])
    +0

    _
    (|> a (/ (gcd a b)) (* b))
    ))

(def: #export frac
  (-> Int Frac)
  (|>> "lux i64 f64"))

(structure: #export equivalence
  (Equivalence Int)
  
  (def: = ..=))

(structure: #export order
  (Order Int)
  
  (def: &equivalence ..equivalence)
  (def: < ..<))

(structure: #export enum
  (Enum Int)
  
  (def: &order ..order)
  (def: succ inc)
  (def: pred dec))

## TODO: Find out why the numeric literals fail during JS compilation.
(structure: #export interval
  (Interval Int)
  
  (def: &enum ..enum)
  (def: top
    ## +9,223,372,036,854,775,807
    (let [half (//i64.left_shift 62 +1)]
      (+ half
         (dec half))))
  (def: bottom
    ## -9,223,372,036,854,775,808
    (//i64.left_shift 63 +1)))

(template [<name> <compose> <identity>]
  [(structure: #export <name>
     (Monoid Int)
     
     (def: identity <identity>)
     (def: compose <compose>))]

  [addition       ..+   +0]
  [multiplication ..*   +1]
  [maximum        ..max (\ ..interval bottom)]
  [minimum        ..min (\ ..interval top)]
  )

(def: -sign "-")
(def: +sign "+")

(template [<struct> <codec> <error>]
  [(structure: #export <struct>
     (Codec Text Int)
     
     (def: (encode value)
       (if (..< +0 value)
         (|> value inc ..negate .nat inc (\ <codec> encode) ("lux text concat" ..-sign))
         (|> value .nat (\ <codec> encode) ("lux text concat" ..+sign))))

     (def: (decode repr)
       (let [input_size ("lux text size" repr)]
         (if (//nat.> 1 input_size)
           (case ("lux text clip" 0 1 repr)
             (^ (static ..+sign))
             (|> repr
                 ("lux text clip" 1 (dec input_size))
                 (\ <codec> decode)
                 (\ try.functor map .int))
             
             (^ (static ..-sign))
             (|> repr
                 ("lux text clip" 1 (dec input_size))
                 (\ <codec> decode)
                 (\ try.functor map (|>> dec .int ..negate dec)))
             
             _
             (#try.Failure <error>))
           (#try.Failure <error>)))))]

  [binary  //nat.binary  "Invalid binary syntax for Int: "]
  [octal   //nat.octal   "Invalid octal syntax for Int: "]
  [decimal //nat.decimal "Invalid syntax for Int: "]
  [hex     //nat.hex     "Invalid hexadecimal syntax for Int: "]
  )

(structure: #export hash
  (Hash Int)
  
  (def: &equivalence ..equivalence)
  (def: hash .nat))

(def: #export (right_shift parameter subject)
  {#.doc "Signed/arithmetic bitwise right-shift."}
  (-> Nat Int Int)
  (//i64.or (//i64.and //i64.sign subject)
            (//i64.right_shift parameter subject)))