aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/number/int.lux
blob: 3aa65a4c49bbf9f1bcf8084dbbb713b958202b25 (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
(.module:
  [library
   [lux "*"
    [abstract
     [hash {"+" Hash}]
     [enum {"+" Enum}]
     [interval {"+" Interval}]
     [monoid {"+" Monoid}]
     [equivalence {"+" Equivalence}]
     [codec {"+" Codec}]
     [predicate {"+" Predicate}]
     ["[0]" order {"+" Order}]]
    [control
     ["[0]" maybe]
     ["[0]" try {"+" Try}]]
    [data
     [text {"+" Char}]]]]
  ["[0]" // "_"
   ["[1][0]" nat]
   ["[1][0]" i64]])

(def: .public (= reference sample)
  (-> Int Int Bit)
  ("lux i64 =" reference sample))

(def: .public (< reference sample)
  (-> Int Int Bit)
  ("lux i64 <" reference sample))

(def: .public (<= reference sample)
  (-> Int Int Bit)
  (if ("lux i64 <" reference sample)
    #1
    ("lux i64 =" reference sample)))

(def: .public (> reference sample)
  (-> Int Int Bit)
  ("lux i64 <" sample reference))

(def: .public (>= reference sample)
  (-> Int Int Bit)
  (if ("lux i64 <" sample reference)
    #1
    ("lux i64 =" reference sample)))

(template [<comparison> <name>]
  [(def: .public <name>
     (Predicate Int)
     (<comparison> +0))]

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

(template [<name> <test> <doc>]
  [(def: .public (<name> left right)
     (-> Int Int Int)
     (if (<test> right left)
       left
       right))]

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

(template [<name> <op> <doc>]
  [(def: .public (<name> param subject)
     (-> 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: .public (/% param subject)
  (-> Int Int [Int Int])
  [(../ param subject)
   (..% param subject)])

(def: .public (opposite it)
  (-> Int Int)
  (..- it +0))

(def: .public (abs it)
  (-> Int Int)
  (if (..< +0 it)
    (..* -1 it)
    it))

(def: .public (signum it)
  (-> Int Int)
  (cond (..= +0 it) +0
        (..< +0 it) -1
        ... else
        +1))

... https://rob.conery.io/2018/08/21/mod-and-remainder-are-not-the-same/
(def: .public (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: .public even?
  (-> Int Bit)
  (|>> (..% +2) ("lux i64 =" +0)))

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

... https://en.wikipedia.org/wiki/Greatest_common_divisor
(def: .public (gcd a b)
  (-> Int Int Int)
  (case b
    +0 a
    _ (gcd b (..% b a))))

(def: .public (co_prime? a b)
  (-> Int Int Bit)
  (..= +1 (..gcd a b)))

... https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
(def: .public (extended_gcd a b)
  (-> 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))))))

... https://en.wikipedia.org/wiki/Least_common_multiple
(def: .public (lcm a b)
  (-> Int Int Int)
  (case [a b]
    (^or [_ +0] [+0 _])
    +0

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

(def: .public frac
  (-> Int Frac)
  (|>> "lux i64 f64"))

(implementation: .public equivalence
  (Equivalence Int)
  
  (def: = ..=))

(implementation: .public order
  (Order Int)
  
  (def: &equivalence ..equivalence)
  (def: < ..<))

(implementation: .public enum
  (Enum Int)
  
  (def: &order ..order)
  (def: succ ++)
  (def: pred --))

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

(template [<name> <composite> <identity>]
  [(implementation: .public <name>
     (Monoid Int)
     
     (def: identity <identity>)
     (def: composite <composite>))]

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

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

(template [<struct> <codec> <error>]
  [(implementation: .public <struct>
     (Codec Text Int)
     
     (def: (encoded value)
       (if (..< +0 value)
         (|> value ++ ..opposite .nat ++ (# <codec> encoded) ("lux text concat" ..-sign))
         (|> value .nat (# <codec> encoded) ("lux text concat" ..+sign))))

     (def: (decoded 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 (-- input_size))
                 (# <codec> decoded)
                 (# try.functor each .int))
             
             (^ (static ..-sign))
             (|> repr
                 ("lux text clip" 1 (-- input_size))
                 (# <codec> decoded)
                 (# try.functor each (|>> -- .int ..opposite --)))
             
             _
             {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: "]
  )

(implementation: .public hash
  (Hash Int)
  
  (def: &equivalence ..equivalence)
  (def: hash .nat))

(def: .public (right_shifted parameter subject)
  (-> Nat Int Int)
  (//i64.or (//i64.and //i64.sign subject)
            (//i64.right_shifted parameter subject)))