blob: 3b6dceb27811e8573acf55dfc1add2cd1253a76a (
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
;; 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/.
(ns lux.analyser.case
(:require clojure.core.match
clojure.core.match.array
(lux [base :as & :refer [deftags |do return fail |let |case]]
[parser :as &parser]
[type :as &type])
(lux.analyser [base :as &&]
[env :as &env]
[module :as &module]
[record :as &&record])))
;; [Tags]
(deftags
["DefaultTotal"
"BoolTotal"
"IntTotal"
"RealTotal"
"CharTotal"
"TextTotal"
"TupleTotal"
"VariantTotal"]
)
(deftags
["StoreTestAC"
"BoolTestAC"
"IntTestAC"
"RealTestAC"
"CharTestAC"
"TextTestAC"
"TupleTestAC"
"VariantTestAC"]
)
;; [Utils]
(def ^:private unit
(&/T (&/T "" -1 -1) (&/V &/$TupleS &/Nil$)))
(defn ^:private resolve-type [type]
(|case type
(&/$VarT ?id)
(|do [type* (&/try-all% (&/|list (&type/deref ?id)
(fail "##1##")))]
(resolve-type type*))
(&/$UnivQ _)
(|do [$var &type/existential
=type (&type/apply-type type $var)]
(&type/actual-type =type))
_
(&type/actual-type type)))
(defn update-up-frame [frame]
(|let [[_env _idx _var] frame]
(&/T _env (+ 2 _idx) _var)))
(defn adjust-type* [up type]
"(-> (List (, (Maybe (List Type)) Int Type)) Type (Lux Type))"
(|case type
(&/$UnivQ _aenv _abody)
(&type/with-var
(fn [$var]
(|do [=type (&type/apply-type type $var)]
(adjust-type* (&/Cons$ (&/T _aenv 1 $var) (&/|map update-up-frame up)) =type))))
(&/$ExQ _aenv _abody)
(|do [$var &type/existential
=type (&type/apply-type type $var)]
(adjust-type* up =type))
(&/$TupleT ?members)
(|do [(&/$TupleT ?members*) (&/fold% (fn [_abody ena]
(|let [[_aenv _aidx (&/$VarT _avar)] ena]
(|do [_ (&type/set-var _avar (&/V &/$BoundT _aidx))]
(&type/clean* _avar _abody))))
type
up)]
(return (&type/Tuple$ (&/|map (fn [v]
(&/fold (fn [_abody ena]
(|let [[_aenv _aidx _avar] ena]
(&/V &/$UnivQ (&/T _aenv _abody))))
v
up))
?members*))))
(&/$SumT ?left ?right)
(|do [(&/$SumT =left =right) (&/fold% (fn [_abody ena]
(|let [[_aenv _aidx (&/$VarT _avar)] ena]
(|do [_ (&type/set-var _avar (&/V &/$BoundT _aidx))]
(&type/clean* _avar _abody))))
type
up)
:let [distributor (fn [v]
(&/fold (fn [_abody ena]
(|let [[_aenv _aidx _avar] ena]
(&/V &/$UnivQ (&/T _aenv _abody))))
v
up))]]
(return (&type/Sum$ (distributor =left) (distributor =right))))
(&/$AppT ?tfun ?targ)
(|do [=type (&type/apply-type ?tfun ?targ)]
(adjust-type* up =type))
(&/$VarT ?id)
(|do [type* (&/try-all% (&/|list (&type/deref ?id)
(fail "##2##")))]
(adjust-type* up type*))
(&/$NamedT ?name ?type)
(adjust-type* up ?type)
(&/$UnitT)
(return type)
_
(fail (str "[Pattern-matching Error] Can't adjust type: " (&type/show-type type)))
))
(defn adjust-type [type]
"(-> Type (Lux Type))"
(adjust-type* &/Nil$ type))
(defn ^:private analyse-pattern [var?? value-type pattern kont]
(|let [[meta pattern*] pattern]
(|case pattern*
(&/$SymbolS "" name)
(|case var??
(&/$Some var-analysis)
(|do [=kont (&env/with-alias name var-analysis
kont)]
(return (&/T (&/V $StoreTestAC -1) =kont)))
_
(|do [=kont (&env/with-local name value-type
kont)
idx &env/next-local-idx]
(return (&/T (&/V $StoreTestAC idx) =kont))))
(&/$SymbolS ident)
(fail (str "[Pattern-matching Error] Symbols must be unqualified: " (&/ident->text ident)))
(&/$BoolS ?value)
(|do [_ (&type/check value-type &type/Bool)
=kont kont]
(return (&/T (&/V $BoolTestAC ?value) =kont)))
(&/$IntS ?value)
(|do [_ (&type/check value-type &type/Int)
=kont kont]
(return (&/T (&/V $IntTestAC ?value) =kont)))
(&/$RealS ?value)
(|do [_ (&type/check value-type &type/Real)
=kont kont]
(return (&/T (&/V $RealTestAC ?value) =kont)))
(&/$CharS ?value)
(|do [_ (&type/check value-type &type/Char)
=kont kont]
(return (&/T (&/V $CharTestAC ?value) =kont)))
(&/$TextS ?value)
(|do [_ (&type/check value-type &type/Text)
=kont kont]
(return (&/T (&/V $TextTestAC ?value) =kont)))
(&/$TupleS ?members)
(|case ?members
(&/$Nil)
(|do [_ (&type/check value-type &type/Unit)
=kont kont]
(return (&/T (&/V $TupleTestAC (&/|list)) =kont)))
_
(|do [value-type* (adjust-type value-type)]
(|case value-type*
(&/$TupleT ?member-types)
(if (not (.equals ^Object (&/|length ?member-types) (&/|length ?members)))
(fail (str "[Pattern-matching Error] Pattern-matching mismatch. Require tuple[" (&/|length ?member-types) "]. Given tuple [" (&/|length ?members) "]" " -- " (&/show-ast pattern)))
(|do [[=tests =kont] (&/fold (fn [kont* vm]
(|let [[v m] vm]
(|do [[=test [=tests =kont]] (analyse-pattern &/None$ v m kont*)]
(return (&/T (&/Cons$ =test =tests) =kont)))))
(|do [=kont kont]
(return (&/T &/Nil$ =kont)))
(&/|reverse (&/zip2 ?member-types ?members)))]
(return (&/T (&/V $TupleTestAC =tests) =kont))))
_
(fail (str "[Pattern-matching Error] Tuples require tuple-types: " (&type/show-type value-type))))))
(&/$RecordS pairs)
(|do [[rec-members rec-type] (&&record/order-record pairs)]
(analyse-pattern &/None$ value-type (&/T meta (&/V &/$TupleS rec-members)) kont))
(&/$TagS ?ident)
(|do [[=module =name] (&&/resolved-ident ?ident)
value-type* (adjust-type value-type)
idx (&module/tag-index =module =name)
group (&module/tag-group =module =name)
case-type (&type/sum-at idx value-type*)
[=test =kont] (analyse-pattern &/None$ case-type unit kont)]
(return (&/T (&/V $VariantTestAC (&/T idx (&/|length group) =test)) =kont)))
(&/$FormS (&/$Cons [_ (&/$TagS ?ident)]
?values))
(|do [[=module =name] (&&/resolved-ident ?ident)
value-type* (adjust-type value-type)
idx (&module/tag-index =module =name)
group (&module/tag-group =module =name)
case-type (&type/sum-at idx value-type*)
[=test =kont] (case (int (&/|length ?values))
0 (analyse-pattern &/None$ case-type unit kont)
1 (analyse-pattern &/None$ case-type (&/|head ?values) kont)
;; 1+
(analyse-pattern &/None$ case-type (&/T (&/T "" -1 -1) (&/V &/$TupleS ?values)) kont))]
(return (&/T (&/V $VariantTestAC (&/T idx (&/|length group) =test)) =kont)))
_
(fail (str "[Pattern-matching Error] Unrecognized pattern syntax: " (&/show-ast pattern)))
)))
(defn ^:private analyse-branch [analyse exo-type var?? value-type pattern body patterns]
(|do [pattern+body (analyse-pattern var?? value-type pattern
(&&/analyse-1 analyse exo-type body))]
(return (&/Cons$ pattern+body patterns))))
(let [compare-kv #(.compareTo ^String (aget ^objects %1 0) ^String (aget ^objects %2 0))]
(defn ^:private merge-total [struct test+body]
(|let [[test ?body] test+body]
(|case [struct test]
[($DefaultTotal total?) ($StoreTestAC ?idx)]
(return (&/V $DefaultTotal true))
[($BoolTotal total? ?values) ($StoreTestAC ?idx)]
(return (&/V $BoolTotal (&/T true ?values)))
[($IntTotal total? ?values) ($StoreTestAC ?idx)]
(return (&/V $IntTotal (&/T true ?values)))
[($RealTotal total? ?values) ($StoreTestAC ?idx)]
(return (&/V $RealTotal (&/T true ?values)))
[($CharTotal total? ?values) ($StoreTestAC ?idx)]
(return (&/V $CharTotal (&/T true ?values)))
[($TextTotal total? ?values) ($StoreTestAC ?idx)]
(return (&/V $TextTotal (&/T true ?values)))
[($TupleTotal total? ?values) ($StoreTestAC ?idx)]
(return (&/V $TupleTotal (&/T true ?values)))
[($VariantTotal total? ?values) ($StoreTestAC ?idx)]
(return (&/V $VariantTotal (&/T true ?values)))
[($DefaultTotal total?) ($BoolTestAC ?value)]
(return (&/V $BoolTotal (&/T total? (&/|list ?value))))
[($BoolTotal total? ?values) ($BoolTestAC ?value)]
(return (&/V $BoolTotal (&/T total? (&/Cons$ ?value ?values))))
[($DefaultTotal total?) ($IntTestAC ?value)]
(return (&/V $IntTotal (&/T total? (&/|list ?value))))
[($IntTotal total? ?values) ($IntTestAC ?value)]
(return (&/V $IntTotal (&/T total? (&/Cons$ ?value ?values))))
[($DefaultTotal total?) ($RealTestAC ?value)]
(return (&/V $RealTotal (&/T total? (&/|list ?value))))
[($RealTotal total? ?values) ($RealTestAC ?value)]
(return (&/V $RealTotal (&/T total? (&/Cons$ ?value ?values))))
[($DefaultTotal total?) ($CharTestAC ?value)]
(return (&/V $CharTotal (&/T total? (&/|list ?value))))
[($CharTotal total? ?values) ($CharTestAC ?value)]
(return (&/V $CharTotal (&/T total? (&/Cons$ ?value ?values))))
[($DefaultTotal total?) ($TextTestAC ?value)]
(return (&/V $TextTotal (&/T total? (&/|list ?value))))
[($TextTotal total? ?values) ($TextTestAC ?value)]
(return (&/V $TextTotal (&/T total? (&/Cons$ ?value ?values))))
[($DefaultTotal total?) ($TupleTestAC ?tests)]
(|do [structs (&/map% (fn [t]
(merge-total (&/V $DefaultTotal total?) (&/T t ?body)))
?tests)]
(return (&/V $TupleTotal (&/T total? structs))))
[($TupleTotal total? ?values) ($TupleTestAC ?tests)]
(if (.equals ^Object (&/|length ?values) (&/|length ?tests))
(|do [structs (&/map2% (fn [v t]
(merge-total v (&/T t ?body)))
?values ?tests)]
(return (&/V $TupleTotal (&/T total? structs))))
(fail "[Pattern-matching Error] Inconsistent tuple-size."))
[($DefaultTotal total?) ($VariantTestAC ?tag ?count ?test)]
(|do [sub-struct (merge-total (&/V $DefaultTotal total?)
(&/T ?test ?body))
structs (|case (&/|list-put ?tag sub-struct (&/|repeat ?count (&/V $DefaultTotal total?)))
(&/$Some list)
(return list)
(&/$None)
(fail "[Pattern-matching Error] YOLO"))]
(return (&/V $VariantTotal (&/T total? structs))))
[($VariantTotal total? ?branches) ($VariantTestAC ?tag ?count ?test)]
(|do [sub-struct (merge-total (|case (&/|at ?tag ?branches)
(&/$Some sub)
sub
(&/$None)
(&/V $DefaultTotal total?))
(&/T ?test ?body))
structs (|case (&/|list-put ?tag sub-struct ?branches)
(&/$Some list)
(return list)
(&/$None)
(fail "[Pattern-matching Error] YOLO"))]
(return (&/V $VariantTotal (&/T total? structs))))
))))
(defn check-totality+ [check-totality]
(fn [?token]
(&type/with-var
(fn [$var]
(|do [=output (check-totality $var ?token)
?type (&type/deref+ $var)
=type (&type/clean $var ?type)]
(return (&/T =output =type)))))))
(defn ^:private check-totality [value-type struct]
(|case struct
($DefaultTotal ?total)
(return ?total)
($BoolTotal ?total ?values)
(|do [_ (&type/check value-type &type/Bool)]
(return (or ?total
(= #{true false} (set (&/->seq ?values))))))
($IntTotal ?total _)
(|do [_ (&type/check value-type &type/Int)]
(return ?total))
($RealTotal ?total _)
(|do [_ (&type/check value-type &type/Real)]
(return ?total))
($CharTotal ?total _)
(|do [_ (&type/check value-type &type/Char)]
(return ?total))
($TextTotal ?total _)
(|do [_ (&type/check value-type &type/Text)]
(return ?total))
($TupleTotal ?total ?structs)
(|case ?structs
(&/$Nil)
(|do [value-type* (resolve-type value-type)]
(|case value-type*
(&/$UnitT)
(return true)
_
(fail "[Pattern-maching Error] Unit is not total.")))
_
(|do [unknown? (&type/unknown? value-type)]
(if unknown?
(|do [=structs (&/map% (check-totality+ check-totality) ?structs)
_ (&type/check value-type (&/V &/$TupleT (&/|map &/|second =structs)))]
(return (or ?total
(&/fold #(and %1 %2) true (&/|map &/|first =structs)))))
(if ?total
(return true)
(|do [value-type* (resolve-type value-type)]
(|case value-type*
(&/$TupleT ?members)
(|do [totals (&/map2% (fn [sub-struct ?member]
(check-totality ?member sub-struct))
?structs ?members)]
(return (&/fold #(and %1 %2) true totals)))
_
(fail "[Pattern-maching Error] Tuple is not total.")))))))
($VariantTotal ?total ?structs)
(if ?total
(return true)
(|do [value-type* (resolve-type value-type)]
(|case value-type*
(&/$SumT _)
(|do [:let [?members (&type/flatten-sum value-type*)]
totals (&/map2% check-totality ?members ?structs)]
(return (&/fold #(and %1 %2) true totals)))
_
(fail "[Pattern-maching Error] Variant is not total."))))
))
;; [Exports]
(defn analyse-branches [analyse exo-type var?? value-type branches]
(|do [patterns (&/fold% (fn [patterns branch]
(|let [[pattern body] branch]
(analyse-branch analyse exo-type var?? value-type pattern body patterns)))
&/Nil$
branches)
struct (&/fold% merge-total (&/V $DefaultTotal false) patterns)
? (check-totality value-type struct)]
(if ?
(return patterns)
(fail "[Pattern-maching Error] Pattern-matching is non-total."))))
|