aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux
blob: 6694848902bf4af47fe5902e35798bbb07f7e44b (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
(.using
 [library
  [lux (.except Tuple Variant nat int rev case local except)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]
    [monad (.only do)]]
   [control
    ["[0]" function]
    ["[0]" maybe]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]
    [parser
     ["<[0]>" code]]]
   [data
    ["[0]" product]
    ["[0]" bit (.open: "[1]#[0]" equivalence)]
    ["[0]" text (.open: "[1]#[0]" equivalence)
     ["%" format (.only Format)]]
    [collection
     ["[0]" list (.open: "[1]#[0]" functor mix)]]]
   [macro
    [syntax (.only syntax)]]
   [math
    [number
     ["n" nat]
     ["i" int]
     ["r" rev]
     ["f" frac]]]
   [meta
    ["[0]" location]
    ["[0]" configuration (.only Configuration)]]]]
 ["[0]" /
  ["[1][0]" simple (.only Simple)]
  ["[1][0]" complex (.only Tuple Variant Complex)]
  ["[1][0]" pattern (.only Pattern)]
  [//
   [phase
    ["[0]" extension (.only Extension)]]
   [///
    [arity (.only Arity)]
    ["[0]" version (.only Version)]
    ["[0]" phase]
    ["[0]" reference (.only Reference)
     ["[0]" variable (.only Register Variable)]]]]])

(type: .public (Branch' e)
  (Record
   [#when Pattern
    #then e]))

(type: .public (Match' e)
  [(Branch' e) (List (Branch' e))])

(type: .public (Environment a)
  (List a))

(type: .public Analysis
  (Rec Analysis
    (.Variant
     {#Simple Simple}
     {#Structure (Complex Analysis)}
     {#Reference Reference}
     {#Case Analysis (Match' Analysis)}
     {#Function (Environment Analysis) Analysis}
     {#Apply Analysis Analysis}
     {#Extension (Extension Analysis)})))

(type: .public Branch
  (Branch' Analysis))

(type: .public Match
  (Match' Analysis))

(def: (branch_equivalence equivalence)
  (-> (Equivalence Analysis) (Equivalence Branch))
  (implementation
   (def: (= [reference_pattern reference_body] [sample_pattern sample_body])
     (and (at /pattern.equivalence = reference_pattern sample_pattern)
          (at equivalence = reference_body sample_body)))))

(def: .public equivalence
  (Equivalence Analysis)
  (implementation
   (def: (= reference sample)
     (.case [reference sample]
       [{#Simple reference} {#Simple sample}]
       (at /simple.equivalence = reference sample)

       [{#Structure reference} {#Structure sample}]
       (at (/complex.equivalence =) = reference sample)

       [{#Reference reference} {#Reference sample}]
       (at reference.equivalence = reference sample)

       [{#Case [reference_analysis reference_match]}
        {#Case [sample_analysis sample_match]}]
       (and (= reference_analysis sample_analysis)
            (at (list.equivalence (branch_equivalence =)) = {.#Item reference_match} {.#Item sample_match}))

       [{#Function [reference_environment reference_analysis]}
        {#Function [sample_environment sample_analysis]}]
       (and (= reference_analysis sample_analysis)
            (at (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}]
       (at (extension.equivalence =) = reference sample)

       _
       false))))

(with_template [<name> <tag>]
  [(def: .public <name>
     (template (<name> content)
       [{<tag> content}]))]

  [case ..#Case]
  )

(def: .public unit
  (template (unit)
    [{..#Simple {/simple.#Unit}}]))

(with_template [<name> <tag>]
  [(def: .public <name>
     (template (<name> value)
       [{..#Simple {<tag> value}}]))]

  [bit  /simple.#Bit]
  [nat  /simple.#Nat]
  [int  /simple.#Int]
  [rev  /simple.#Rev]
  [frac /simple.#Frac]
  [text /simple.#Text]
  )

(type: .public (Abstraction c)
  [(Environment c) Arity c])

(type: .public (Reification c)
  [c (List c)])

(def: .public no_op
  (template (no_op value)
    [(|> 1
         {variable.#Local}
         {reference.#Variable}
         {..#Reference}
         {..#Function (list)}
         {..#Apply value})]))

(def: .public (reified [abstraction inputs])
  (-> (Reification Analysis) Analysis)
  (list#mix (function (_ input abstraction')
              {#Apply input abstraction'})
            abstraction
            inputs))

(def: .public (reification analysis)
  (-> Analysis (Reification Analysis))
  (loop (again [abstraction analysis
                inputs (is (List Analysis)
                           (list))])
    (.case abstraction
      {#Apply input next}
      (again next {.#Item input inputs})

      _
      [abstraction inputs])))

(with_template [<name> <tag>]
  [(def: .public <name>
     (syntax (_ [content <code>.any])
       (in (list (` (.<| {..#Reference}
                         <tag>
                         (~ content)))))))]

  [variable {reference.#Variable}]
  [constant {reference.#Constant}]

  [local    ((~! reference.local))]
  [foreign  ((~! reference.foreign))]
  )

(with_template [<name> <tag>]
  [(def: .public <name>
     (template (<name> content)
       [(.<| {..#Structure}
             {<tag>}
             content)]))]

  [variant /complex.#Variant]
  [tuple   /complex.#Tuple]
  )

(def: .public (format analysis)
  (Format Analysis)
  (.case analysis
    {#Simple it}
    (/simple.format it)
    
    {#Structure it}
    (/complex.format format it)
    
    {#Reference reference}
    (reference.format reference)
    
    {#Case analysis match}
    (%.format "({"
              (|> {.#Item match}
                  (list#each (function (_ [when then])
                               (%.format (/pattern.format when) " " (format then))))
                  (text.interposed " "))
              "} "
              (format analysis)
              ")")
    
    {#Function environment body}
    (|> (format body)
        (%.format " ")
        (%.format (|> environment
                      (list#each format)
                      (text.interposed " ")
                      (text.enclosed ["[" "]"])))
        (text.enclosed ["(" ")"]))
    
    {#Apply _}
    (|> analysis
        ..reification
        {.#Item}
        (list#each format)
        (text.interposed " ")
        (text.enclosed ["(" ")"]))
    
    {#Extension name parameters}
    (|> parameters
        (list#each format)
        (text.interposed " ")
        (%.format (%.text name) " ")
        (text.enclosed ["(" ")"]))))

(with_template [<special> <general>]
  [(type: .public <special>
     (<general> .Lux Code Analysis))]

  [State+    extension.State]
  [Operation extension.Operation]
  [Phase     extension.Phase]
  [Handler   extension.Handler]
  [Bundle    extension.Bundle]
  )

(def: .public (with_source_code source action)
  (All (_ a) (-> Source (Operation a) (Operation a)))
  (function (_ [bundle state])
    (let [old_source (the .#source state)]
      (.case (action [bundle (has .#source source state)])
        {try.#Success [[bundle' state'] output]}
        {try.#Success [[bundle' (has .#source old_source state')]
                       output]}

        failure
        failure))))

(def: .public (with_current_module name)
  (All (_ a) (-> Text (Operation a) (Operation a)))
  (extension.localized (the .#current_module)
                       (has .#current_module)
                       (function.constant {.#Some name})))

(def: .public (with_location location action)
  (All (_ a) (-> Location (Operation a) (Operation a)))
  (if (text#= "" (product.left location))
    action
    (function (_ [bundle state])
      (let [old_location (the .#location state)]
        (.case (action [bundle (has .#location location state)])
          {try.#Success [[bundle' state'] output]}
          {try.#Success [[bundle' (has .#location old_location state')]
                         output]}

          failure
          failure)))))

(def: (located location error)
  (-> Location Text Text)
  (%.format (%.location location) text.new_line
            error))

(def: .public (failure error)
  (-> Text Operation)
  (function (_ [bundle state])
    {try.#Failure (located (the .#location state) error)}))

(def: .public (of_try it)
  (All (_ a) (-> (Try a) (Operation a)))
  (function (_ [bundle state])
    (.case it
      {try.#Failure error}
      {try.#Failure (located (the .#location state) error)}

      {try.#Success it}
      {try.#Success [[bundle state] it]})))

(def: .public (except exception parameters)
  (All (_ e) (-> (Exception e) e Operation))
  (..failure (exception.error exception parameters)))

(def: .public (assertion exception parameters condition)
  (All (_ e) (-> (Exception e) e Bit (Operation Any)))
  (if condition
    (at phase.monad in [])
    (..except exception parameters)))

(def: .public (with_exception 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.#Failure error}
      (let [[bundle state] bundle,state]
        {try.#Failure (located (the .#location state) error)})

      success
      success)))

(def: .public (set_state state)
  (-> .Lux (Operation Any))
  (function (_ [bundle _])
    {try.#Success [[bundle state]
                   []]}))

(with_template [<name> <type> <field> <value>]
  [(def: .public (<name> value)
     (-> <type> (Operation Any))
     (extension.update (has <field> <value>)))]

  [set_source_code    Source   .#source         value]
  [set_current_module Text     .#current_module {.#Some value}]
  [set_location       Location .#location       value]
  )

(def: .public (location file)
  (-> Text Location)
  [file 1 0])

(def: .public (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: .public (info version host configuration)
  (-> Version Text Configuration Info)
  [.#target host
   .#version (version.format version)
   .#mode {.#Build}
   .#configuration configuration])

(def: .public (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      []
   .#eval            (as (-> Type Code (Meta Any)) [])
   .#host            []])