aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/compiler/default/phase/extension/analysis/common.lux
blob: 437a54bbc8075f2d414f0f3aa49de0210e830614 (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
(.module:
  [lux #*
   [control
    ["." monad (#+ do)]
    ["ex" exception (#+ exception:)]
    [thread (#+ Box)]]
   [concurrency
    [atom (#+ Atom)]]
   [data
    ["." text
     format]
    [collection
     ["." list ("list/." Functor<List>)]
     ["." array]
     ["dict" dictionary (#+ Dictionary)]]]
   [type
    ["." check]]
   [io (#+ IO)]]
  ["." ////
   ["." analysis (#+ Analysis Handler Bundle)
    [".A" type]
    [".A" case]
    [".A" function]]]
  ["." ///
   ["." bundle]])

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

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

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

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

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

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

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

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

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

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

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

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

(def: bundle::lux
  Bundle
  (|> bundle.empty
      (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")
      (|> bundle.empty
          (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")
      (|> bundle.empty
          (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")
      (|> bundle.empty
          (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 Bit))
          (bundle.install "<" (binary Int Int Bit))
          (bundle.install "to-frac" (unary Int Frac))
          (bundle.install "char" (unary Int Text)))))

(def: bundle::frac
  Bundle
  (<| (bundle.prefix "frac")
      (|> bundle.empty
          (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 Bit))
          (bundle.install "<" (binary Frac Frac Bit))
          (bundle.install "smallest" (nullary Frac))
          (bundle.install "min" (nullary Frac))
          (bundle.install "max" (nullary Frac))
          (bundle.install "to-rev" (unary Frac Rev))
          (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")
      (|> bundle.empty
          (bundle.install "=" (binary Text Text Bit))
          (bundle.install "<" (binary Text Text Bit))
          (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 "char" (binary Text Nat (type (Maybe Nat))))
          (bundle.install "clip" (trinary Text Nat Nat (type (Maybe Text))))
          )))

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

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

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

(def: bundle::array
  Bundle
  (<| (bundle.prefix "array")
      (|> bundle.empty
          (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: atom::new
  Handler
  (function (_ extension-name analyse args)
    (case args
      (^ (list initC))
      (do ////.Monad<Operation>
        [[var-id varT] (typeA.with-env check.var)
         _ (typeA.infer (type (Atom varT)))
         initA (typeA.with-type varT
                 (analyse initC))]
        (wrap (#analysis.Extension extension-name (list initA))))
      
      _
      (////.throw bundle.incorrect-arity [extension-name 1 (list.size args)]))))

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

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

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

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

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

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

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

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

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