blob: d2b8cb1e5cb716158e53c2d0718b3c837357b39f (
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
|
## Copyright (c) Eduardo Julian. All rights reserved.
## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
## If a copy of the MPL was not distributed with this file,
## You can obtain one at http://mozilla.org/MPL/2.0/.
(;module:
lux
(lux (control monad)
(data [text "Text/" Eq<Text>]
text/format
[number]
(struct [list "List/" Monad<List> Fold<List>]
[dict])
[bool]
[product])
[compiler #+ Monad<Lux>]
(macro [ast]
["s" syntax #+ syntax: Syntax])
[type]
(type ["tc" check #+ Check Monad<Check>])
))
(def: (find-type-var id env)
(-> Nat (Bindings Nat (Maybe Type)) (Lux Type))
(case (list;find (|>. product;left (n.= id))
(get@ #;mappings env))
(#;Some [_ (#;Some type)])
(case type
(#;VarT id')
(find-type-var id' env)
_
(:: Monad<Lux> wrap type))
(#;Some [_ #;None])
(compiler;fail (format "Unbound type-var " (%n id)))
#;None
(compiler;fail (format "Unknown type-var " (%n id)))
))
(def: (resolve-type var-name)
(-> Ident (Lux Type))
(do Monad<Lux>
[raw-type (compiler;find-type var-name)
compiler compiler;get-compiler]
(case raw-type
(#;VarT id)
(find-type-var id (get@ #;type-vars compiler))
_
(wrap raw-type))))
(def: (find-member-type idx sig-type)
(-> Nat Type (Check Type))
(case sig-type
(#;NamedT _ sig-type')
(find-member-type idx sig-type')
(#;AppT func arg)
(case (type;apply-type func arg)
#;None
(tc;fail (format "Can't apply type " (%type func) " to type " (%type arg)))
(#;Some sig-type')
(find-member-type idx sig-type'))
(#;ProdT left right)
(if (n.= +0 idx)
(:: Monad<Check> wrap left)
(find-member-type (n.dec idx) right))
_
(if (n.= +0 idx)
(:: Monad<Check> wrap sig-type)
(tc;fail (format "Can't find member type " (%n idx) " for " (%type sig-type))))))
(def: (find-member-name member)
(-> Ident (Lux Ident))
(case member
["" simple-name]
(compiler;either (do Monad<Lux>
[member (compiler;normalize member)
_ (compiler;resolve-tag member)]
(wrap member))
(do Monad<Lux>
[this-module-name compiler;current-module-name
imp-mods (compiler;imported-modules this-module-name)
tag-lists (mapM @ compiler;tag-lists imp-mods)
#let [tag-lists (|> tag-lists List/join (List/map product;left) List/join)
candidates (list;filter (. (Text/= simple-name) product;right)
tag-lists)]]
(case candidates
#;Nil
(compiler;fail (format "Unknown tag: " (%ident member)))
(#;Cons winner #;Nil)
(wrap winner)
_
(compiler;fail (format "Too many candidate tags: " (%list %ident candidates))))))
_
(:: Monad<Lux> wrap member)))
(def: (resolve-member member)
(-> Ident (Lux [Nat Type]))
(do Monad<Lux>
[member (find-member-name member)
[idx tag-list sig-type] (compiler;resolve-tag member)]
(wrap [idx sig-type])))
(def: (prepare-defs this-module-name defs)
(-> Text (List [Text Def]) (List [Ident Type]))
(|> defs
(list;filter (lambda [[name [def-type def-anns def-value]]]
(compiler;struct? def-anns)))
(List/map (lambda [[name [def-type def-anns def-value]]]
[[this-module-name name] def-type]))))
(def: local-env
(Lux (List [Ident Type]))
(do Monad<Lux>
[local-batches compiler;locals
#let [total-locals (List/fold (lambda [[name type] table]
(dict;put~ name type table))
(: (dict;Dict Text Type)
(dict;new text;Hash<Text>))
(List/join local-batches))]]
(wrap (|> total-locals
dict;entries
(List/map (lambda [[name type]] [["" name] type]))))))
(def: local-structs
(Lux (List [Ident Type]))
(do Monad<Lux>
[this-module-name compiler;current-module-name
defs (compiler;defs this-module-name)]
(wrap (prepare-defs this-module-name defs))))
(def: import-structs
(Lux (List [Ident Type]))
(do Monad<Lux>
[this-module-name compiler;current-module-name
imp-mods (compiler;imported-modules this-module-name)
export-batches (mapM @ (lambda [imp-mod]
(do @
[exports (compiler;exports imp-mod)]
(wrap (prepare-defs imp-mod exports))))
imp-mods)]
(wrap (List/join export-batches))))
(def: (apply-function-type func arg)
(-> Type Type (Check Type))
(case func
(#;NamedT _ func')
(apply-function-type func' arg)
(#;UnivQ _)
(do Monad<Check>
[[id var] tc;create-var]
(apply-function-type (default (undefined)
(type;apply-type func var))
arg))
(#;LambdaT input output)
(do Monad<Check>
[_ (tc;check input arg)]
(wrap output))
_
(tc;fail (format "Invalid function type: " (%type func)))))
(def: (concrete-type type)
(-> Type (Check [(List Nat) Type]))
(case type
(#;UnivQ _)
(do Monad<Check>
[[id var] tc;create-var
[ids final-output] (concrete-type (default (undefined)
(type;apply-type type var)))]
(wrap [(#;Cons id ids)
final-output]))
_
(:: Monad<Check> wrap [(list) type])))
(def: (check-apply member-type input-types output-type)
(-> Type (List Type) Type (Check []))
(do Monad<Check>
[member-type' (foldM Monad<Check>
(lambda [input member]
(apply-function-type member input))
member-type
input-types)]
(tc;check output-type member-type')))
(def: compiler-type-context
(Lux tc;Context)
(lambda [compiler]
(let [type-vars (get@ #;type-vars compiler)
context (|> tc;fresh-context
(set@ #tc;var-counter (get@ #;counter type-vars))
(set@ #tc;bindings (dict;from-list number;Hash<Nat> (get@ #;mappings type-vars))))]
(#;Right [compiler context]))))
(def: (test-provision context dep alts)
(-> tc;Context Type (List [Ident Type]) (Lux (List Ident)))
(do Monad<Lux>
[compiler compiler;get-compiler]
(case (|> alts
(list;filter (lambda [[alt-name alt-type]]
(case (tc;run context
(do Monad<Check>
[_ (tc;check dep alt-type)]
(wrap [])))
(#;Left error)
false
(#;Right _)
true)))
(List/map product;left))
#;Nil
(compiler;fail (format "No candidates for provisioning: " (%type dep)))
found
(wrap found))))
(def: (provision compiler context dep)
(-> Compiler tc;Context Type (Check Ident))
(case (compiler;run compiler
($_ compiler;either
(do Monad<Lux> [alts local-env] (test-provision context dep alts))
(do Monad<Lux> [alts local-structs] (test-provision context dep alts))
(do Monad<Lux> [alts import-structs] (test-provision context dep alts))))
(#;Left error)
(tc;fail error)
(#;Right candidates)
(case candidates
#;Nil
(tc;fail (format "No candidates for provisioning: " (%type dep)))
(#;Cons winner #;Nil)
(:: Monad<Check> wrap winner)
_
(tc;fail (format "Too many candidates for provisioning: " (%type dep) " --- " (%list %ident candidates))))
))
(def: (test-alternatives sig-type member-idx input-types output-type alts)
(-> Type Nat (List Type) Type (List [Ident Type]) (Lux (List [Ident (List Ident)])))
(do Monad<Lux>
[compiler compiler;get-compiler
context compiler-type-context]
(case (|> alts
(List/map (lambda [[alt-name alt-type]]
(case (tc;run context
(do Monad<Check>
[[tvars alt-type] (concrete-type alt-type)
#let [[deps alt-type] (type;flatten-function alt-type)]
_ (tc;check alt-type sig-type)
member-type (find-member-type member-idx alt-type)
_ (check-apply member-type input-types output-type)
=tvars (mapM @ tc;deref tvars)
context' tc;get-context
=deps (mapM @ (provision compiler context') deps)]
(wrap =deps)))
(#;Left error)
(list)
(#;Right =deps)
(list [alt-name =deps]))))
List/join)
#;Nil
(compiler;fail (format "No alternatives."
"\n"
(|> alts
(List/map product;left)
(%list %ident))))
found
(wrap found))))
(def: (find-alternatives sig-type member-idx input-types output-type)
(-> Type Nat (List Type) Type (Lux (List [Ident (List Ident)])))
(let [test (test-alternatives sig-type member-idx input-types output-type)]
($_ compiler;either
(do Monad<Lux> [alts local-env] (test alts))
(do Monad<Lux> [alts local-structs] (test alts))
(do Monad<Lux> [alts import-structs] (test alts)))))
(def: (var? input)
(-> AST Bool)
(case input
[_ (#;SymbolS _)]
true
_
false))
(def: (join-pair [l r])
(All [a] (-> [a a] (List a)))
(list l r))
(syntax: #export (::: [member s;symbol]
[args (s;alt (s;seq (s;some s;symbol) s;end)
(s;seq (s;some s;any) s;end))])
(case args
(#;Left [args _])
(do @
[[member-idx sig-type] (resolve-member member)
input-types (mapM @ resolve-type args)
output-type compiler;expected-type
chosen-ones (find-alternatives sig-type member-idx input-types output-type)]
(case chosen-ones
#;Nil
(compiler;fail (format "No structure option could be found for member: " (%ident member)))
(#;Cons [chosen deps] #;Nil)
(let [chosen-inst (case deps
#;Nil
(ast;symbol chosen)
_
(` ((~ (ast;symbol chosen)) (~@ (List/map ast;symbol deps)))))]
(wrap (list (` (:: (~ chosen-inst)
(~ (ast;local-symbol (product;right member)))
(~@ (List/map ast;symbol args)))))))
_
(compiler;fail (format "Too many options available: "
(|> chosen-ones
(List/map (. %ident product;left))
(text;join-with ", ")
)))))
(#;Right [args _])
(do @
[labels (seqM @ (list;repeat (list;size args)
(compiler;gensym "")))
#let [retry (` (let [(~@ (|> (list;zip2 labels args) (List/map join-pair) List/join))]
(;;::: (~ (ast;symbol member)) (~@ labels))))]]
(wrap (list retry)))
))
|