aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/analyser/procedure/common.lux
blob: c1ca36b17c1ef77cde1cf4894df368a75dbe79c0 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
(;module:
  lux
  (lux (control [monad #+ do])
       (concurrency ["A" atom])
       (data [text]
             text/format
             (coll [list "list/" Functor<List>]
                   [array #+ Array]
                   ["d" dict]))
       [macro #+ Monad<Lux>]
       (type ["TC" check])
       [io])
  (luxc ["&" base]
        (lang ["la" analysis #+ Analysis])
        (analyser ["&;" common])))

## [Utils]
(type: #export Proc
  (-> &;Analyser (List Code) (Lux Analysis)))

(type: #export Bundle
  (d;Dict Text Proc))

(def: #export (install name unnamed)
  (-> Text (-> Text Proc)
      (-> Bundle Bundle))
  (d;put name (unnamed name)))

(def: #export (prefix prefix bundle)
  (-> Text Bundle Bundle)
  (|> bundle
      d;entries
      (list/map (function [[key val]] [(format prefix " " key) val]))
      (d;from-list text;Hash<Text>)))

(def: #export (wrong-amount-error proc expected actual)
  (-> Text Nat Nat Text)
  (format "Wrong number of arguments for " (%t proc) "\n"
          "Expected: " (|> expected nat-to-int %i) "\n"
          "  Actual: " (|> actual nat-to-int %i)))

(def: (simple proc input-types output-type)
  (-> Text (List Type) Type Proc)
  (let [num-expected (list;size input-types)]
    (function [analyse args]
      (let [num-actual (list;size args)]
        (if (n.= num-expected num-actual)
          (do Monad<Lux>
            [argsA (monad;map @
                              (function [[argT argC]]
                                (&;with-expected-type argT
                                  (analyse argC)))
                              (list;zip2 input-types args))
             expected macro;expected-type
             _ (&;within-type-env
                (TC;check expected output-type))]
            (wrap (#la;Procedure proc argsA)))
          (&;fail (wrong-amount-error proc num-expected num-actual)))))))

(def: #export (nullary valueT proc)
  (-> Type Text Proc)
  (simple proc (list) valueT))

(def: #export (unary inputT outputT proc)
  (-> Type Type Text Proc)
  (simple proc (list inputT) outputT))

(def: #export (binary subjectT paramT outputT proc)
  (-> Type Type Type Text Proc)
  (simple proc (list subjectT paramT) outputT))

(def: #export (trinary subjectT param0T param1T outputT proc)
  (-> Type Type Type Type Text Proc)
  (simple proc (list subjectT param0T param1T) outputT))

## [Analysers]
## "lux is" represents reference/pointer equality.
(def: (lux-is proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        ((binary varT varT Bool proc)
         analyse args)))))

## "lux try" provides a simple way to interact with the host platform's
## error-handling facilities.
(def: (lux-try proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list opC))
          (do Monad<Lux>
            [opA (&;with-expected-type (type (io;IO varT))
                   (analyse opC))
             outputT (&;within-type-env
                      (TC;clean var-id (type (Either Text varT))))
             expected macro;expected-type
             _ (&;within-type-env
                (TC;check expected outputT))]
            (wrap (#la;Procedure proc (list opA))))
          
          _
          (&;fail (wrong-amount-error proc +1 (list;size args))))))))

(def: lux-procs
  Bundle
  (|> (d;new text;Hash<Text>)
      (install "is" lux-is)
      (install "try" lux-try)))

(def: io-procs
  Bundle
  (<| (prefix "io")
      (|> (d;new text;Hash<Text>)
          (install "log" (unary Text Unit))
          (install "error" (unary Text Bottom))
          (install "exit" (unary Nat Bottom))
          (install "current-time" (nullary Int)))))

(def: bit-procs
  Bundle
  (<| (prefix "bit")
      (|> (d;new text;Hash<Text>)
          (install "count" (unary Nat Nat))
          (install "and" (binary Nat Nat Nat))
          (install "or" (binary Nat Nat Nat))
          (install "xor" (binary Nat Nat Nat))
          (install "shift-left" (binary Nat Nat Nat))
          (install "unsigned-shift-right" (binary Nat Nat Nat))
          (install "shift-right" (binary Int Nat Int))
          )))

(def: nat-procs
  Bundle
  (<| (prefix "nat")
      (|> (d;new text;Hash<Text>)
          (install "+" (binary Nat Nat Nat))
          (install "-" (binary Nat Nat Nat))
          (install "*" (binary Nat Nat Nat))
          (install "/" (binary Nat Nat Nat))
          (install "%" (binary Nat Nat Nat))
          (install "=" (binary Nat Nat Bool))
          (install "<" (binary Nat Nat Bool))
          (install "min" (nullary Nat))
          (install "max" (nullary Nat))
          (install "to-int" (unary Nat Int))
          (install "to-text" (unary Nat Text)))))

(def: int-procs
  Bundle
  (<| (prefix "int")
      (|> (d;new text;Hash<Text>)
          (install "+" (binary Int Int Int))
          (install "-" (binary Int Int Int))
          (install "*" (binary Int Int Int))
          (install "/" (binary Int Int Int))
          (install "%" (binary Int Int Int))
          (install "=" (binary Int Int Bool))
          (install "<" (binary Int Int Bool))
          (install "min" (nullary Int))
          (install "max" (nullary Int))
          (install "to-nat" (unary Int Nat))
          (install "to-frac" (unary Int Frac)))))

(def: deg-procs
  Bundle
  (<| (prefix "deg")
      (|> (d;new text;Hash<Text>)
          (install "+" (binary Deg Deg Deg))
          (install "-" (binary Deg Deg Deg))
          (install "*" (binary Deg Deg Deg))
          (install "/" (binary Deg Deg Deg))
          (install "%" (binary Deg Deg Deg))
          (install "=" (binary Deg Deg Bool))
          (install "<" (binary Deg Deg Bool))
          (install "scale" (binary Deg Nat Deg))
          (install "reciprocal" (binary Deg Nat Deg))
          (install "min" (nullary Deg))
          (install "max" (nullary Deg))
          (install "to-frac" (unary Deg Frac)))))

(def: frac-procs
  Bundle
  (<| (prefix "frac")
      (|> (d;new text;Hash<Text>)
          (install "+" (binary Frac Frac Frac))
          (install "-" (binary Frac Frac Frac))
          (install "*" (binary Frac Frac Frac))
          (install "/" (binary Frac Frac Frac))
          (install "%" (binary Frac Frac Frac))
          (install "=" (binary Frac Frac Bool))
          (install "<" (binary Frac Frac Bool))
          (install "smallest" (nullary Frac))
          (install "min" (nullary Frac))
          (install "max" (nullary Frac))
          (install "not-a-number" (nullary Frac))
          (install "positive-infinity" (nullary Frac))
          (install "negative-infinity" (nullary Frac))
          (install "to-deg" (unary Frac Deg))
          (install "to-int" (unary Frac Int))
          (install "encode" (unary Frac Text))
          (install "decode" (unary Text (type (Maybe Frac)))))))

(def: text-procs
  Bundle
  (<| (prefix "text")
      (|> (d;new text;Hash<Text>)
          (install "=" (binary Text Text Bool))
          (install "<" (binary Text Text Bool))
          (install "prepend" (binary Text Text Text))
          (install "index" (trinary Text Text Nat (type (Maybe Nat))))
          (install "size" (unary Text Nat))
          (install "hash" (unary Text Nat))
          (install "replace" (trinary Text Text Text Text))
          (install "char" (binary Text Nat Nat))
          (install "clip" (trinary Text Nat Nat Text))
          )))

(def: (array-get proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        ((binary Nat (type (Array varT)) varT proc)
         analyse args)))))

(def: (array-put proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        ((trinary Nat varT (type (Array varT)) (type (Array varT)) proc)
         analyse args)))))

(def: (array-remove proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        ((binary Nat (type (Array varT)) (type (Array varT)) proc)
         analyse args)))))

(def: array-procs
  Bundle
  (<| (prefix "array")
      (|> (d;new text;Hash<Text>)
          (install "new" (unary Nat Array))
          (install "get" array-get)
          (install "put" array-put)
          (install "remove" array-remove)
          (install "size" (unary (type (Ex [a] (Array a))) Nat))
          )))

(def: math-procs
  Bundle
  (<| (prefix "math")
      (|> (d;new text;Hash<Text>)
          (install "cos" (unary Frac Frac))
          (install "sin" (unary Frac Frac))
          (install "tan" (unary Frac Frac))
          (install "acos" (unary Frac Frac))
          (install "asin" (unary Frac Frac))
          (install "atan" (unary Frac Frac))
          (install "cosh" (unary Frac Frac))
          (install "sinh" (unary Frac Frac))
          (install "tanh" (unary Frac Frac))
          (install "exp" (unary Frac Frac))
          (install "log" (unary Frac Frac))
          (install "root2" (unary Frac Frac))
          (install "root3" (unary Frac Frac))
          (install "ceil" (unary Frac Frac))
          (install "floor" (unary Frac Frac))
          (install "round" (unary Frac Frac))
          (install "atan2" (binary Frac Frac Frac))
          (install "pow" (binary Frac Frac Frac))
          )))

(def: (atom-new proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list initC))
          (do Monad<Lux>
            [initA (&;with-expected-type varT
                     (analyse initC))
             outputT (&;within-type-env
                      (TC;clean var-id (type (A;Atom varT))))
             expected macro;expected-type
             _ (&;within-type-env
                (TC;check expected outputT))]
            (wrap (#la;Procedure proc (list initA))))
          
          _
          (&;fail (wrong-amount-error proc +1 (list;size args))))))))

(def: (atom-read proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        ((unary (type (A;Atom varT)) varT proc)
         analyse args)))))

(def: (atom-compare-and-swap proc)
  (-> Text Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        ((trinary varT varT (type (A;Atom varT)) Bool proc)
         analyse args)))))

(def: atom-procs
  Bundle
  (<| (prefix "atom")
      (|> (d;new text;Hash<Text>)
          (install "new" atom-new)
          (install "read" atom-read)
          (install "compare-and-swap" atom-compare-and-swap)
          )))

(def: process-procs
  Bundle
  (<| (prefix "process")
      (|> (d;new text;Hash<Text>)
          (install "concurrency-level" (nullary Nat))
          (install "future" (unary (type (io;IO Top)) Unit))
          (install "schedule" (binary Nat (type (io;IO Top)) Unit))
          )))

(def: #export procedures
  Bundle
  (<| (prefix "lux")
      (|> (d;new text;Hash<Text>)
          (d;merge lux-procs)
          (d;merge bit-procs)
          (d;merge nat-procs)
          (d;merge int-procs)
          (d;merge deg-procs)
          (d;merge frac-procs)
          (d;merge text-procs)
          (d;merge array-procs)
          (d;merge math-procs)
          (d;merge atom-procs)
          (d;merge process-procs)
          (d;merge io-procs))))