aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/extension/declaration/lux.lux
blob: 1768bdd076e48eb973544f607bebe840d59d1504 (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
... 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 https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    [io (.only IO)]
    ["<>" parser]
    ["[0]" maybe (.use "[1]#[0]" functor)]
    ["[0]" try]
    ["[0]" exception (.only Exception)]]
   [data
    ["[0]" binary]
    ["[0]" product]
    ["[0]" text
     ["%" \\format]]
    [collection
     ["[0]" dictionary]
     ["[0]" array]
     ["[0]" list (.use "[1]#[0]" functor mix)]
     ["[0]" set (.only Set)]]]
   [math
    [number
     ["n" nat]]]
   ["[0]" meta (.only)
    ["[0]" code
     ["<[1]>" \\parser (.only Parser)]]
    [macro
     ["^" pattern]]
    ["[0]" type (.only sharing) (.use "[1]#[0]" equivalence)
     ["[0]" check]]]]]
 ["[0]" /// (.only)
  ["[1][0]" analysis]
  ["/[1]" //
   ["/[1]" //
    ["[0]" analysis (.only)
     [macro (.only Expander)]
     ["[1]/[0]" evaluation]
     ["[0]A" type]
     ["[0]A" module]
     ["[0]" scope]]
    ["[0]" synthesis]
    ["[1][0]" translation]
    ["[1][0]" declaration (.only Import Requirements Phase Operation Handler Extender Bundle)]
    ["[0]" phase (.only)
     [extension
      ["[0]E" analysis
       ["[1]" lux]]]]
    ["[1][0]" program (.only Program)]
    [///
     [meta
      ["[0]" archive (.only Archive)
       ["[0]" artifact]
       ["[0]" module]
       ["[0]" unit]]
      ["[0]" cache
       [dependency
        ["[1]/[0]" artifact]]]]]]]])

(def .public (custom [syntax handler])
  (All (_ anchor expression declaration of)
    (-> [(Parser of)
         (-> (Phase anchor expression declaration) Archive of
             (Operation anchor expression declaration Requirements))]
        (Handler anchor expression declaration)))
  (function (_ phase archive inputs)
    (when (<code>.result syntax inputs)
      {try.#Success inputs}
      (handler phase archive inputs)

      {try.#Failure error}
      (phase.failure error))))

... TODO: Inline "evaluate!'" into "evaluate!" ASAP
(def (evaluate!' archive translation code//type codeS)
  (All (_ anchor expression declaration)
    (-> Archive
        (/////translation.Phase anchor expression declaration)
        Type
        synthesis.Term
        (Operation anchor expression declaration [Type expression Any])))
  (/////declaration.of_translation
   (do phase.monad
     [module /////translation.module
      id /////translation.next
      codeG (translation archive codeS)
      @module (/////translation.module_id module archive)
      codeV (/////translation.evaluate! [{.#None} codeG])]
     (in [code//type codeG codeV]))))

(def .public (evaluate! archive type codeC)
  (All (_ anchor expression declaration)
    (-> Archive Type Code
        (Operation anchor expression declaration [Type expression Any])))
  (do phase.monad
    [state phase.state
     .let [analysis_state (the [/////declaration.#analysis /////declaration.#state] state)
           analysis (the [/////declaration.#analysis /////declaration.#phase] state)
           synthesis ((the [/////declaration.#synthesis /////declaration.#phase] state) analysis_state)
           translation ((the [/////declaration.#translation /////declaration.#phase] state) analysis_state)]
     [_ codeA] (<| /////declaration.of_analysis
                   scope.with
                   typeA.fresh
                   (typeA.expecting type)
                   (analysis archive codeC))
     codeS (/////declaration.of_synthesis
            (synthesis archive codeA))]
    (evaluate!' archive translation type codeS)))

... TODO: Inline "definition'" into "definition" ASAP
(def (definition' archive translation [module name] code//type codeS)
  (All (_ anchor expression declaration)
    (-> Archive
        (/////translation.Phase anchor expression declaration)
        Symbol
        Type
        synthesis.Term
        (Operation anchor expression declaration [Type expression Any])))
  (/////declaration.of_translation
   (do phase.monad
     [dependencies (cache/artifact.dependencies archive codeS)
      [interim_artifacts codeG] (/////translation.with_interim_artifacts archive
                                  (translation archive codeS))
      .let [@abstraction (when codeS
                           (synthesis.function/abstraction @ [env arity body])
                           (|> interim_artifacts
                               list.last
                               (maybe#each (|>> [arity])))

                           _
                           {.#None})]
      @module (phase.of_try (archive.id module archive))
      @self (/////translation.learn [name @abstraction] false (list#mix set.has dependencies interim_artifacts))
      [target_name value declaration] (/////translation.define! [@module @self] {.#None} [(maybe#each product.right @abstraction) codeG])
      _ (/////translation.save! @self {.#None} declaration)]
     (in [code//type codeG value]))))

(def (definition archive name expected codeC)
  (All (_ anchor expression declaration)
    (-> Archive Symbol (Maybe Type) Code
        (Operation anchor expression declaration [Type expression Any])))
  (do [! phase.monad]
    [state phase.state
     .let [analysis_state (the [/////declaration.#analysis /////declaration.#state] state)
           analysis (the [/////declaration.#analysis /////declaration.#phase] state)
           synthesis ((the [/////declaration.#synthesis /////declaration.#phase] state) analysis_state)
           translation ((the [/////declaration.#translation /////declaration.#phase] state) analysis_state)]
     [_ code//type codeA] (/////declaration.of_analysis
                           (scope.with
                             (typeA.fresh
                              (when expected
                                {.#None}
                                (do !
                                  [[code//type codeA] (typeA.inferring
                                                       (analysis archive codeC))
                                   code//type (typeA.check (check.clean (list) code//type))]
                                  (in [code//type codeA]))

                                {.#Some expected}
                                (do !
                                  [codeA (<| (typeA.expecting expected)
                                             (analysis archive codeC))]
                                  (in [expected codeA]))))))
     codeS (/////declaration.of_synthesis
            (synthesis archive codeA))]
    (definition' archive translation name code//type codeS)))

... TODO: Get rid of this function ASAP.
(def refresh
  (All (_ anchor expression declaration)
    (Operation anchor expression declaration Any))
  (do [! phase.monad]
    [state phase.state
     .let [eval (analysis/evaluation.evaluator (the [/////declaration.#analysis /////declaration.#phase] state)
                                               [(the [/////declaration.#synthesis /////declaration.#state] state)
                                                (the [/////declaration.#synthesis /////declaration.#phase] state)]
                                               [(the [/////declaration.#translation /////declaration.#state] state)
                                                (the [/////declaration.#translation /////declaration.#phase] state)])]
     _ (/////declaration.of_analysis
        (do !
          [_ (moduleA.override_definition [.prelude "is#"] [true {.#Default [.Analysis (analysisE.is#_extension eval "is#")]}])
           _ (moduleA.override_definition [.prelude "as#"] [true {.#Default [.Analysis (analysisE.as#_extension eval "as#")]}])]
          (in [])))]
    (in [])))

(def (announce_definition! module short type)
  (All (_ anchor expression declaration)
    (-> Text Text Type
        (Operation anchor expression declaration Any)))
  (/////declaration.of_translation
   (/////translation.log! (%.format short " : " (type.relative_format module type)))))

(def lux::def
  Handler
  (..custom
   [(all <>.and <code>.local <code>.any <code>.any)
    (function (_ phase archive [short_name valueC exported?C])
      (when valueC
        [_ {.#Symbol original}]
        (do phase.monad
          [_ ..refresh
           state phase.state
           .let [analysis (the [/////declaration.#analysis /////declaration.#phase] state)]
           [code//type codeA] (<| /////declaration.of_analysis
                                  typeA.fresh
                                  typeA.inferring
                                  (analysis archive valueC))
           [_ _ exported?] (evaluate! archive Bit exported?C)
           .let [original (when codeA
                            (analysis.constant @ original)
                            original

                            _
                            (undefined))]
           _ (/////declaration.of_analysis
              (moduleA.define short_name [(as Bit exported?) {.#Alias original}]))]
          (in /////declaration.no_requirements))

        _
        (do phase.monad
          [_ ..refresh
           current_module (/////declaration.of_analysis meta.current_module_name)
           .let [full_name [current_module short_name]]
           [type valueT value] (..definition archive full_name {.#None} valueC)
           [_ _ exported?] (evaluate! archive Bit exported?C)
           _ (/////declaration.of_analysis
              (moduleA.define short_name [(as Bit exported?) {.#Definition [type value]}]))
           _ (..announce_definition! current_module short_name type)]
          (in /////declaration.no_requirements))))]))

(def imports
  (Parser (List Import))
  (|> (<code>.tuple (<>.and <code>.text <code>.text))
      <>.some
      <code>.tuple))

(def def_module
  Handler
  (..custom
   [..imports
    (function (_ phase archive imports)
      (do [! phase.monad]
        [_ (/////declaration.of_analysis
            (monad.each ! (function (_ [module alias])
                            (do !
                              [_ (moduleA.import module)]
                              (when alias
                                "" (in [])
                                _ (moduleA.alias alias module))))
                        imports))]
        (in [/////declaration.#imports imports
             /////declaration.#referrals (list)])))]))

... TODO: Stop requiring these types and the "swapped" function below to make types line-up.
(with_template [<name> <anonymous>]
  [(def <name>
     Type
     (with_expansions [<original> binary.Binary]
       (let [_ <original>]
         {.#Named (symbol <original>)
                  <anonymous>})))]

  [Binary|Python (Nominal "bytearray")]
  [Binary|DEFAULT (type_literal (array.Array (I64 Any)))]
  )

(def (swapped original replacement)
  (-> Type Type Type
      Type)
  (function (again type)
    (if (type#= original type)
      replacement
      (when type
        {.#Nominal name parameters}
        {.#Nominal name (list#each again parameters)}

        (^.with_template [<tag>]
          [{<tag> left right}
           {<tag> (again left) (again right)}])
        ([.#Sum]
         [.#Product]
         [.#Function]
         [.#Apply])
        
        (^.or {.#Parameter _}
              {.#Var _}
              {.#Ex _})
        type

        (^.with_template [<tag>]
          [{<tag> closure body}
           {<tag> closure (again body)}])
        ([.#UnivQ]
         [.#ExQ])
        
        {.#Named name anonymous}
        {.#Named name (again anonymous)}))))

(def .public bundle
  Bundle
  (|> ///.empty
      (dictionary.has "def#" lux::def)
      (dictionary.has "module#" def_module)))