blob: d2382537ac1579e26feb2f9b9f03236efbd8b82b (
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
(.module:
[lux (#- nat int rev)
[abstract
[equivalence (#+ Equivalence)]
[hash (#+ Hash)]
[monad (#+ do)]]
[control
["." function]
["." try]
["." exception (#+ Exception)]]
[data
["." product]
["." maybe]
["." bit ("#\." equivalence)]
["." text ("#\." equivalence)
["%" format (#+ Format format)]]
[collection
["." list ("#\." functor fold)]]]
[math
[number
["n" nat]
["i" int]
["r" rev]
["f" frac]]]
[meta
["." location]]]
[//
[phase
["." extension (#+ Extension)]]
[///
[arity (#+ Arity)]
[version (#+ Version)]
["." phase]
["." reference (#+ Reference)
["." variable (#+ Register Variable)]]]])
(type: #export #rec Primitive
#Unit
(#Bit Bit)
(#Nat Nat)
(#Int Int)
(#Rev Rev)
(#Frac Frac)
(#Text Text))
(type: #export Tag
Nat)
(type: #export (Variant a)
{#lefts Nat
#right? Bit
#value a})
(def: #export (tag lefts right?)
(-> Nat Bit Nat)
(if right?
(inc lefts)
lefts))
(def: (lefts tag right?)
(-> Nat Bit Nat)
(if right?
(dec tag)
tag))
(def: #export (choice options pick)
(-> Nat Nat [Nat Bit])
(let [right? (n.= (dec options) pick)]
[(..lefts pick right?)
right?]))
(type: #export (Tuple a)
(List a))
(type: #export (Composite a)
(#Variant (Variant a))
(#Tuple (Tuple a)))
(type: #export #rec Pattern
(#Simple Primitive)
(#Complex (Composite Pattern))
(#Bind Register))
(type: #export (Branch' e)
{#when Pattern
#then e})
(type: #export (Match' e)
[(Branch' e) (List (Branch' e))])
(type: #export (Environment a)
(List a))
(type: #export #rec Analysis
(#Primitive Primitive)
(#Structure (Composite Analysis))
(#Reference Reference)
(#Case Analysis (Match' Analysis))
(#Function (Environment Analysis) Analysis)
(#Apply Analysis Analysis)
(#Extension (Extension Analysis)))
(type: #export Branch
(Branch' Analysis))
(type: #export Match
(Match' Analysis))
(structure: primitive_equivalence
(Equivalence Primitive)
(def: (= reference sample)
(case [reference sample]
[#Unit #Unit]
true
(^template [<tag> <=>]
[[(<tag> reference) (<tag> sample)]
(<=> reference sample)])
([#Bit bit\=]
[#Nat n.=]
[#Int i.=]
[#Rev r.=]
[#Frac f.=]
[#Text text\=])
_
false)))
(structure: #export (composite_equivalence (^open "/\."))
(All [a] (-> (Equivalence a) (Equivalence (Composite a))))
(def: (= reference sample)
(case [reference sample]
[(#Variant [reference_lefts reference_right? reference_value])
(#Variant [sample_lefts sample_right? sample_value])]
(and (n.= reference_lefts sample_lefts)
(bit\= reference_right? sample_right?)
(/\= reference_value sample_value))
[(#Tuple reference) (#Tuple sample)]
(\ (list.equivalence /\=) = reference sample)
_
false)))
(structure: #export (composite_hash super)
(All [a] (-> (Hash a) (Hash (Composite a))))
(def: &equivalence
(..composite_equivalence (\ super &equivalence)))
(def: (hash value)
(case value
(#Variant [lefts right? value])
($_ n.* 2
(\ n.hash hash lefts)
(\ bit.hash hash right?)
(\ super hash value))
(#Tuple members)
($_ n.* 3
(\ (list.hash super) hash members))
)))
(structure: pattern_equivalence
(Equivalence Pattern)
(def: (= reference sample)
(case [reference sample]
[(#Simple reference) (#Simple sample)]
(\ primitive_equivalence = reference sample)
[(#Complex reference) (#Complex sample)]
(\ (composite_equivalence =) = reference sample)
[(#Bind reference) (#Bind sample)]
(n.= reference sample)
_
false)))
(structure: (branch_equivalence equivalence)
(-> (Equivalence Analysis) (Equivalence Branch))
(def: (= [reference_pattern reference_body] [sample_pattern sample_body])
(and (\ pattern_equivalence = reference_pattern sample_pattern)
(\ equivalence = reference_body sample_body))))
(structure: #export equivalence
(Equivalence Analysis)
(def: (= reference sample)
(case [reference sample]
[(#Primitive reference) (#Primitive sample)]
(\ primitive_equivalence = reference sample)
[(#Structure reference) (#Structure sample)]
(\ (composite_equivalence =) = reference sample)
[(#Reference reference) (#Reference sample)]
(\ reference.equivalence = reference sample)
[(#Case [reference_analysis reference_match])
(#Case [sample_analysis sample_match])]
(and (= reference_analysis sample_analysis)
(\ (list.equivalence (branch_equivalence =)) = (#.Cons reference_match) (#.Cons sample_match)))
[(#Function [reference_environment reference_analysis])
(#Function [sample_environment sample_analysis])]
(and (= reference_analysis sample_analysis)
(\ (list.equivalence =) = reference_environment sample_environment))
[(#Apply [reference_input reference_abstraction])
(#Apply [sample_input sample_abstraction])]
(and (= reference_input sample_input)
(= reference_abstraction sample_abstraction))
[(#Extension reference) (#Extension sample)]
(\ (extension.equivalence =) = reference sample)
_
false)))
(template [<name> <tag>]
[(template: #export (<name> content)
(<tag> content))]
[control/case #..Case]
)
(template: #export (unit)
(#..Primitive #..Unit))
(template [<name> <tag>]
[(template: #export (<name> value)
(#..Primitive (<tag> value)))]
[bit #..Bit]
[nat #..Nat]
[int #..Int]
[rev #..Rev]
[frac #..Frac]
[text #..Text]
)
(type: #export (Abstraction c)
[(Environment c) Arity c])
(type: #export (Application c)
[c (List c)])
(def: (last? size tag)
(-> Nat Tag Bit)
(n.= (dec size) tag))
(template: #export (no_op value)
(|> 1 #variable.Local #reference.Variable #..Reference
(#..Function (list))
(#..Apply value)))
(def: #export (apply [abstraction inputs])
(-> (Application Analysis) Analysis)
(list\fold (function (_ input abstraction')
(#Apply input abstraction'))
abstraction
inputs))
(def: #export (application analysis)
(-> Analysis (Application Analysis))
(loop [abstraction analysis
inputs (list)]
(case abstraction
(#Apply input next)
(recur next (#.Cons input inputs))
_
[abstraction inputs])))
(template [<name> <tag>]
[(template: #export (<name> content)
(.<| #..Reference
<tag>
content))]
[variable #reference.Variable]
[constant #reference.Constant]
[variable/local reference.local]
[variable/foreign reference.foreign]
)
(template [<name> <tag>]
[(template: #export (<name> content)
(.<| #..Complex
<tag>
content))]
[pattern/variant #..Variant]
[pattern/tuple #..Tuple]
)
(template [<name> <tag>]
[(template: #export (<name> content)
(.<| #..Structure
<tag>
content))]
[variant #..Variant]
[tuple #..Tuple]
)
(template: #export (pattern/unit)
(#..Simple #..Unit))
(template [<name> <tag>]
[(template: #export (<name> content)
(#..Simple (<tag> content)))]
[pattern/bit #..Bit]
[pattern/nat #..Nat]
[pattern/int #..Int]
[pattern/rev #..Rev]
[pattern/frac #..Frac]
[pattern/text #..Text]
)
(template: #export (pattern/bind register)
(#..Bind register))
(def: #export (%analysis analysis)
(Format Analysis)
(case analysis
(#Primitive primitive)
(case primitive
#Unit
"[]"
(^template [<tag> <format>]
[(<tag> value)
(<format> value)])
([#Bit %.bit]
[#Nat %.nat]
[#Int %.int]
[#Rev %.rev]
[#Frac %.frac]
[#Text %.text]))
(#Structure structure)
(case structure
(#Variant [lefts right? value])
(format "(" (%.nat lefts) " " (%.bit right?) " " (%analysis value) ")")
(#Tuple members)
(|> members
(list\map %analysis)
(text.join_with " ")
(text.enclose ["[" "]"])))
(#Reference reference)
(reference.format reference)
(#Case analysis match)
"{?}"
(#Function environment body)
(|> (%analysis body)
(format " ")
(format (|> environment
(list\map %analysis)
(text.join_with " ")
(text.enclose ["[" "]"])))
(text.enclose ["(" ")"]))
(#Apply _)
(|> analysis
..application
#.Cons
(list\map %analysis)
(text.join_with " ")
(text.enclose ["(" ")"]))
(#Extension name parameters)
(|> parameters
(list\map %analysis)
(text.join_with " ")
(format (%.text name) " ")
(text.enclose ["(" ")"]))))
(template [<special> <general>]
[(type: #export <special>
(<general> .Lux Code Analysis))]
[State+ extension.State]
[Operation extension.Operation]
[Phase extension.Phase]
[Handler extension.Handler]
[Bundle extension.Bundle]
)
(def: #export (with_source_code source action)
(All [a] (-> Source (Operation a) (Operation a)))
(function (_ [bundle state])
(let [old_source (get@ #.source state)]
(case (action [bundle (set@ #.source source state)])
(#try.Success [[bundle' state'] output])
(#try.Success [[bundle' (set@ #.source old_source state')]
output])
(#try.Failure error)
(#try.Failure error)))))
(def: fresh_bindings
(All [k v] (Bindings k v))
{#.counter 0
#.mappings (list)})
(def: fresh_scope
Scope
{#.name (list)
#.inner 0
#.locals fresh_bindings
#.captured fresh_bindings})
(def: #export (with_scope action)
(All [a] (-> (Operation a) (Operation [Scope a])))
(function (_ [bundle state])
(case (action [bundle (update@ #.scopes (|>> (#.Cons fresh_scope)) state)])
(#try.Success [[bundle' state'] output])
(case (get@ #.scopes state')
(#.Cons head tail)
(#try.Success [[bundle' (set@ #.scopes tail state')]
[head output]])
#.Nil
(#try.Failure "Impossible error: Drained scopes!"))
(#try.Failure error)
(#try.Failure error))))
(def: #export (with_current_module name)
(All [a] (-> Text (Operation a) (Operation a)))
(extension.localized (get@ #.current_module)
(set@ #.current_module)
(function.constant (#.Some name))))
(def: #export (with_location location action)
(All [a] (-> Location (Operation a) (Operation a)))
(if (text\= "" (product.left location))
action
(function (_ [bundle state])
(let [old_location (get@ #.location state)]
(case (action [bundle (set@ #.location location state)])
(#try.Success [[bundle' state'] output])
(#try.Success [[bundle' (set@ #.location old_location state')]
output])
(#try.Failure error)
(#try.Failure error))))))
(def: (locate_error location error)
(-> Location Text Text)
(format (%.location location) text.new_line
error))
(def: #export (fail error)
(-> Text Operation)
(function (_ [bundle state])
(#try.Failure (locate_error (get@ #.location state) error))))
(def: #export (throw exception parameters)
(All [e] (-> (Exception e) e Operation))
(..fail (exception.construct exception parameters)))
(def: #export (assert exception parameters condition)
(All [e] (-> (Exception e) e Bit (Operation Any)))
(if condition
(\ phase.monad wrap [])
(..throw exception parameters)))
(def: #export (fail' error)
(-> Text (phase.Operation Lux))
(function (_ state)
(#try.Failure (locate_error (get@ #.location state) error))))
(def: #export (throw' exception parameters)
(All [e] (-> (Exception e) e (phase.Operation Lux)))
(..fail' (exception.construct exception parameters)))
(def: #export (with_stack exception message action)
(All [e o] (-> (Exception e) e (Operation o) (Operation o)))
(function (_ bundle,state)
(case (exception.with exception message
(action bundle,state))
(#try.Success output)
(#try.Success output)
(#try.Failure error)
(let [[bundle state] bundle,state]
(#try.Failure (locate_error (get@ #.location state) error))))))
(def: #export (install state)
(-> .Lux (Operation Any))
(function (_ [bundle _])
(#try.Success [[bundle state]
[]])))
(template [<name> <type> <field> <value>]
[(def: #export (<name> value)
(-> <type> (Operation Any))
(extension.update (set@ <field> <value>)))]
[set_source_code Source #.source value]
[set_current_module Text #.current_module (#.Some value)]
[set_location Location #.location value]
)
(def: #export (location file)
(-> Text Location)
[file 1 0])
(def: #export (source file code)
(-> Text Text Source)
[(location file) 0 code])
(def: dummy_source
Source
[location.dummy 0 ""])
(def: type_context
Type_Context
{#.ex_counter 0
#.var_counter 0
#.var_bindings (list)})
(def: #export (info version host)
(-> Version Text Info)
{#.target host
#.version (%.nat version)
#.mode #.Build})
(def: #export (state info)
(-> Info Lux)
{#.info info
#.source ..dummy_source
#.location location.dummy
#.current_module #.None
#.modules (list)
#.scopes (list)
#.type_context ..type_context
#.expected #.None
#.seed 0
#.scope_type_vars (list)
#.extensions []
#.host []})
|