aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/compiler/extension/analysis/common.lux
blob: 6bd1a93bfd451e6d75179defaea46bf9ffa9d2ce (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:]
                [thread #+ Box])
       (concurrency [atom #+ Atom])
       (data [text]
             text/format
             (coll [list "list/" Functor<List>]
                   [array]
                   (dictionary ["dict" unordered #+ Dict])))
       [lang]
       (lang (type ["tc" check])
             (analysis [".A" type]
                       [".A" case]
                       [".A" function]))
       [io #+ IO])
  (//// [compiler]
        [analysis #+ Analysis])
  [///]
  [///bundle])

(type: Handler
  (///.Handler .Lux .Code Analysis))

## [Utils]
(def: (simple extension inputsT+ outputT)
  (-> Text (List Type) Type ..Handler)
  (let [num-expected (list.size inputsT+)]
    (function (_ analyse args)
      (let [num-actual (list.size args)]
        (if (n/= num-expected num-actual)
          (do compiler.Monad<Operation>
            [_ (typeA.infer outputT)
             argsA (monad.map @
                              (function (_ [argT argC])
                                (typeA.with-type argT
                                  (analyse argC)))
                              (list.zip2 inputsT+ args))]
            (wrap (#///.Extension extension argsA)))
          (lang.throw ///bundle.incorrect-arity [extension num-expected num-actual]))))))

(def: #export (nullary valueT extension)
  (-> Type Text ..Handler)
  (simple extension (list) valueT))

(def: #export (unary inputT outputT extension)
  (-> Type Type Text ..Handler)
  (simple extension (list inputT) outputT))

(def: #export (binary subjectT paramT outputT extension)
  (-> Type Type Type Text ..Handler)
  (simple extension (list subjectT paramT) outputT))

(def: #export (trinary subjectT param0T param1T outputT extension)
  (-> Type Type Type Type Text ..Handler)
  (simple extension (list subjectT param0T param1T) outputT))

## [Analysers]
## "lux is" represents reference/pointer equality.
(def: (lux//is extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[var-id varT] (typeA.with-env tc.var)]
      ((binary varT varT Bool extension)
       analyse args))))

## "lux try" provides a simple way to interact with the host platform's
## error-handling facilities.
(def: (lux//try extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (case args
      (^ (list opC))
      (do compiler.Monad<Operation>
        [[var-id varT] (typeA.with-env tc.var)
         _ (typeA.infer (type (Either Text varT)))
         opA (typeA.with-type (type (IO varT))
               (analyse opC))]
        (wrap (#///.Extension extension (list opA))))
      
      _
      (lang.throw ///bundle.incorrect-arity [extension +1 (list.size args)]))))

(def: (lux//in-module extension)
  (-> Text ..Handler)
  (function (_ analyse argsC+)
    (case argsC+
      (^ (list [_ (#.Text module-name)] exprC))
      (lang.with-current-module module-name
        (analyse exprC))
      
      _
      (lang.throw ///bundle.invalid-syntax [extension]))))

## (do-template [<name> <type>]
##   [(def: (<name> extension)
##      (-> Text ..Handler)
##      (function (_ analyse args)
##        (case args
##          (^ (list typeC valueC))
##          (do compiler.Monad<Operation>
##            [actualT (eval Type typeC)
##             _ (typeA.infer (:! Type actualT))]
##            (typeA.with-type <type>
##              (analyse valueC)))

##          _
##          (lang.throw ///bundle.incorrect-arity [extension +2 (list.size args)]))))]

##   [lux//check  (:! Type actualT)]
##   [lux//coerce Any]
##   )

(def: (lux//check//type extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (case args
      (^ (list valueC))
      (do compiler.Monad<Operation>
        [_ (typeA.infer Type)
         valueA (typeA.with-type Type
                  (analyse valueC))]
        (wrap valueA))
      
      _
      (lang.throw ///bundle.incorrect-arity [extension +1 (list.size args)]))))

(def: bundle/lux
  ///.Bundle
  (|> ///.fresh
      (///bundle.install "is" lux//is)
      (///bundle.install "try" lux//try)
      (///bundle.install "check" lux//check)
      (///bundle.install "coerce" lux//coerce)
      (///bundle.install "check type" lux//check//type)
      (///bundle.install "in-module" lux//in-module)))

(def: bundle/io
  ///.Bundle
  (<| (///bundle.prefix "io")
      (|> ///.fresh
          (///bundle.install "log" (unary Text Any))
          (///bundle.install "error" (unary Text Nothing))
          (///bundle.install "exit" (unary Int Nothing))
          (///bundle.install "current-time" (nullary Int)))))

(def: bundle/bit
  ///.Bundle
  (<| (///bundle.prefix "bit")
      (|> ///.fresh
          (///bundle.install "and" (binary Nat Nat Nat))
          (///bundle.install "or" (binary Nat Nat Nat))
          (///bundle.install "xor" (binary Nat Nat Nat))
          (///bundle.install "left-shift" (binary Nat Nat Nat))
          (///bundle.install "logical-right-shift" (binary Nat Nat Nat))
          (///bundle.install "arithmetic-right-shift" (binary Int Nat Int))
          )))

(def: bundle/int
  ///.Bundle
  (<| (///bundle.prefix "int")
      (|> ///.fresh
          (///bundle.install "+" (binary Int Int Int))
          (///bundle.install "-" (binary Int Int Int))
          (///bundle.install "*" (binary Int Int Int))
          (///bundle.install "/" (binary Int Int Int))
          (///bundle.install "%" (binary Int Int Int))
          (///bundle.install "=" (binary Int Int Bool))
          (///bundle.install "<" (binary Int Int Bool))
          (///bundle.install "min" (nullary Int))
          (///bundle.install "max" (nullary Int))
          (///bundle.install "to-nat" (unary Int Nat))
          (///bundle.install "to-frac" (unary Int Frac))
          (///bundle.install "char" (unary Int Text)))))

(def: bundle/deg
  ///.Bundle
  (<| (///bundle.prefix "deg")
      (|> ///.fresh
          (///bundle.install "+" (binary Deg Deg Deg))
          (///bundle.install "-" (binary Deg Deg Deg))
          (///bundle.install "*" (binary Deg Deg Deg))
          (///bundle.install "/" (binary Deg Deg Deg))
          (///bundle.install "%" (binary Deg Deg Deg))
          (///bundle.install "=" (binary Deg Deg Bool))
          (///bundle.install "<" (binary Deg Deg Bool))
          (///bundle.install "scale" (binary Deg Nat Deg))
          (///bundle.install "reciprocal" (binary Deg Nat Deg))
          (///bundle.install "min" (nullary Deg))
          (///bundle.install "max" (nullary Deg))
          (///bundle.install "to-frac" (unary Deg Frac)))))

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

(def: bundle/text
  ///.Bundle
  (<| (///bundle.prefix "text")
      (|> ///.fresh
          (///bundle.install "=" (binary Text Text Bool))
          (///bundle.install "<" (binary Text Text Bool))
          (///bundle.install "concat" (binary Text Text Text))
          (///bundle.install "index" (trinary Text Text Nat (type (Maybe Nat))))
          (///bundle.install "size" (unary Text Nat))
          (///bundle.install "hash" (unary Text Nat))
          (///bundle.install "replace-once" (trinary Text Text Text Text))
          (///bundle.install "replace-all" (trinary Text Text Text Text))
          (///bundle.install "char" (binary Text Nat (type (Maybe Nat))))
          (///bundle.install "clip" (trinary Text Nat Nat (type (Maybe Text))))
          )))

(def: (array//get extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[var-id varT] (typeA.with-env tc.var)]
      ((binary (type (Array varT)) Nat (type (Maybe varT)) extension)
       analyse args))))

(def: (array//put extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[var-id varT] (typeA.with-env tc.var)]
      ((trinary (type (Array varT)) Nat varT (type (Array varT)) extension)
       analyse args))))

(def: (array//remove extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[var-id varT] (typeA.with-env tc.var)]
      ((binary (type (Array varT)) Nat (type (Array varT)) extension)
       analyse args))))

(def: bundle/array
  ///.Bundle
  (<| (///bundle.prefix "array")
      (|> ///.fresh
          (///bundle.install "new" (unary Nat Array))
          (///bundle.install "get" array//get)
          (///bundle.install "put" array//put)
          (///bundle.install "remove" array//remove)
          (///bundle.install "size" (unary (type (Ex [a] (Array a))) Nat))
          )))

(def: bundle/math
  ///.Bundle
  (<| (///bundle.prefix "math")
      (|> ///.fresh
          (///bundle.install "cos" (unary Frac Frac))
          (///bundle.install "sin" (unary Frac Frac))
          (///bundle.install "tan" (unary Frac Frac))
          (///bundle.install "acos" (unary Frac Frac))
          (///bundle.install "asin" (unary Frac Frac))
          (///bundle.install "atan" (unary Frac Frac))
          (///bundle.install "cosh" (unary Frac Frac))
          (///bundle.install "sinh" (unary Frac Frac))
          (///bundle.install "tanh" (unary Frac Frac))
          (///bundle.install "exp" (unary Frac Frac))
          (///bundle.install "log" (unary Frac Frac))
          (///bundle.install "ceil" (unary Frac Frac))
          (///bundle.install "floor" (unary Frac Frac))
          (///bundle.install "round" (unary Frac Frac))
          (///bundle.install "atan2" (binary Frac Frac Frac))
          (///bundle.install "pow" (binary Frac Frac Frac))
          )))

(def: (atom-new extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (case args
      (^ (list initC))
      (do compiler.Monad<Operation>
        [[var-id varT] (typeA.with-env tc.var)
         _ (typeA.infer (type (Atom varT)))
         initA (typeA.with-type varT
                 (analyse initC))]
        (wrap (#///.Extension extension (list initA))))
      
      _
      (lang.throw ///bundle.incorrect-arity [extension +1 (list.size args)]))))

(def: (atom-read extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[var-id varT] (typeA.with-env tc.var)]
      ((unary (type (Atom varT)) varT extension)
       analyse args))))

(def: (atom//compare-and-swap extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[var-id varT] (typeA.with-env tc.var)]
      ((trinary (type (Atom varT)) varT varT Bool extension)
       analyse args))))

(def: bundle/atom
  ///.Bundle
  (<| (///bundle.prefix "atom")
      (|> ///.fresh
          (///bundle.install "new" atom-new)
          (///bundle.install "read" atom-read)
          (///bundle.install "compare-and-swap" atom//compare-and-swap)
          )))

(def: (box//new extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (case args
      (^ (list initC))
      (do compiler.Monad<Operation>
        [[var-id varT] (typeA.with-env tc.var)
         _ (typeA.infer (type (All [!] (Box ! varT))))
         initA (typeA.with-type varT
                 (analyse initC))]
        (wrap (#///.Extension extension (list initA))))
      
      _
      (lang.throw ///bundle.incorrect-arity [extension +1 (list.size args)]))))

(def: (box//read extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[thread-id threadT] (typeA.with-env tc.var)
       [var-id varT] (typeA.with-env tc.var)]
      ((unary (type (Box threadT varT)) varT extension)
       analyse args))))

(def: (box//write extension)
  (-> Text ..Handler)
  (function (_ analyse args)
    (do compiler.Monad<Operation>
      [[thread-id threadT] (typeA.with-env tc.var)
       [var-id varT] (typeA.with-env tc.var)]
      ((binary varT (type (Box threadT varT)) Any extension)
       analyse args))))

(def: bundle/box
  ///.Bundle
  (<| (///bundle.prefix "box")
      (|> ///.fresh
          (///bundle.install "new" box//new)
          (///bundle.install "read" box//read)
          (///bundle.install "write" box//write)
          )))

(def: bundle/process
  ///.Bundle
  (<| (///bundle.prefix "process")
      (|> ///.fresh
          (///bundle.install "parallelism" (nullary Nat))
          (///bundle.install "schedule" (binary Nat (type (IO Any)) Any))
          )))

(def: #export bundle
  ///.Bundle
  (<| (///bundle.prefix "lux")
      (|> ///.fresh
          (dict.merge bundle/lux)
          (dict.merge bundle/bit)
          (dict.merge bundle/int)
          (dict.merge bundle/deg)
          (dict.merge bundle/frac)
          (dict.merge bundle/text)
          (dict.merge bundle/array)
          (dict.merge bundle/math)
          (dict.merge bundle/atom)
          (dict.merge bundle/box)
          (dict.merge bundle/process)
          (dict.merge bundle/io))
      ))