aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/generation.lux
blob: f32b128655000e8b2dc472f7852fe4b654fa2ad4 (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
(.module:
  [library
   [lux (#- Module)
    [abstract
     [monad (#+ do)]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]
     ["." function]]
    [data
     [binary (#+ Binary)]
     ["." product]
     ["." name]
     ["." text ("#\." equivalence)
      ["%" format (#+ format)]]
     [collection
      ["." row (#+ Row)]
      ["." list ("#\." functor)]]]
    [math
     [number
      ["n" nat]]]]]
  [//
   [synthesis (#+ Synthesis)]
   [phase
    ["." extension]]
   [///
    ["." phase]
    [meta
     ["." archive (#+ Archive)
      ["." descriptor (#+ Module)]
      ["." artifact]]]]])

(type: .public Context
  [archive.ID artifact.ID])

(type: .public (Buffer directive)
  (Row [artifact.ID (Maybe Text) directive]))

(exception: .public (cannot_interpret {error Text})
  (exception.report
   ["Error" error]))

(template [<name>]
  [(exception: .public (<name> {artifact_id artifact.ID})
     (exception.report
      ["Artifact ID" (%.nat artifact_id)]))]

  [cannot_overwrite_output]
  [no_buffer_for_saving_code]
  )

(interface: .public (Host expression directive)
  (: (-> Context expression (Try Any))
     evaluate!)
  (: (-> directive (Try Any))
     execute!)
  (: (-> Context (Maybe Text) expression (Try [Text Any directive]))
     define!)

  (: (-> Context Binary directive)
     ingest)
  (: (-> Context (Maybe Text) directive (Try Any))
     re_learn)
  (: (-> Context (Maybe Text) directive (Try Any))
     re_load))

(type: .public (State anchor expression directive)
  {#module Module
   #anchor (Maybe anchor)
   #host (Host expression directive)
   #buffer (Maybe (Buffer directive))
   #registry artifact.Registry
   #counter Nat
   #context (Maybe artifact.ID)
   #log (Row Text)})

(template [<special> <general>]
  [(type: .public (<special> anchor expression directive)
     (<general> (State anchor expression directive) Synthesis expression))]

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

(def: .public (state host module)
  (All [anchor expression directive]
    (-> (Host expression directive)
        Module
        (..State anchor expression directive)))
  {#module module
   #anchor #.None
   #host host
   #buffer #.None
   #registry artifact.empty
   #counter 0
   #context #.None
   #log row.empty})

(def: .public empty_buffer Buffer row.empty)

(template [<tag>
           <with_declaration> <with_type> <with_value>
           <set> <get> <get_type> <exception>]
  [(exception: .public <exception>)

   (def: .public <with_declaration>
     (All [anchor expression directive output] <with_type>)
     (function (_ body)
       (function (_ [bundle state])
         (case (body [bundle (set@ <tag> (#.Some <with_value>) state)])
           (#try.Success [[bundle' state'] output])
           (#try.Success [[bundle' (set@ <tag> (get@ <tag> state) state')]
                          output])

           (#try.Failure error)
           (#try.Failure error)))))

   (def: .public <get>
     (All [anchor expression directive]
       (Operation anchor expression directive <get_type>))
     (function (_ (^@ stateE [bundle state]))
       (case (get@ <tag> state)
         (#.Some output)
         (#try.Success [stateE output])

         #.None
         (exception.except <exception> []))))

   (def: .public (<set> value)
     (All [anchor expression directive]
       (-> <get_type> (Operation anchor expression directive Any)))
     (function (_ [bundle state])
       (#try.Success [[bundle (set@ <tag> (#.Some value) state)]
                      []])))]

  [#anchor
   (with_anchor anchor)
   (-> anchor (Operation anchor expression directive output)
       (Operation anchor expression directive output))
   anchor
   set_anchor anchor anchor no_anchor]

  [#buffer
   with_buffer
   (-> (Operation anchor expression directive output)
       (Operation anchor expression directive output))
   ..empty_buffer
   set_buffer buffer (Buffer directive) no_active_buffer]
  )

(def: .public get_registry
  (All [anchor expression directive]
    (Operation anchor expression directive artifact.Registry))
  (function (_ (^@ stateE [bundle state]))
    (#try.Success [stateE (get@ #registry state)])))

(def: .public (set_registry value)
  (All [anchor expression directive]
    (-> artifact.Registry (Operation anchor expression directive Any)))
  (function (_ [bundle state])
    (#try.Success [[bundle (set@ #registry value state)]
                   []])))

(def: .public next
  (All [anchor expression directive]
    (Operation anchor expression directive Nat))
  (do phase.monad
    [count (extension.read (get@ #counter))
     _ (extension.update (update@ #counter inc))]
    (in count)))

(def: .public (gensym prefix)
  (All [anchor expression directive]
    (-> Text (Operation anchor expression directive Text)))
  (\ phase.monad map (|>> %.nat (format prefix)) ..next))

(def: .public (enter_module module)
  (All [anchor expression directive]
    (-> Module (Operation anchor expression directive Any)))
  (extension.update (set@ #module module)))

(def: .public module
  (All [anchor expression directive]
    (Operation anchor expression directive Module))
  (extension.read (get@ #module)))

(def: .public (evaluate! label code)
  (All [anchor expression directive]
    (-> Context expression (Operation anchor expression directive Any)))
  (function (_ (^@ state+ [bundle state]))
    (case (\ (get@ #host state) evaluate! label code)
      (#try.Success output)
      (#try.Success [state+ output])

      (#try.Failure error)
      (exception.except ..cannot_interpret error))))

(def: .public (execute! code)
  (All [anchor expression directive]
    (-> directive (Operation anchor expression directive Any)))
  (function (_ (^@ state+ [bundle state]))
    (case (\ (get@ #host state) execute! code)
      (#try.Success output)
      (#try.Success [state+ output])

      (#try.Failure error)
      (exception.except ..cannot_interpret error))))

(def: .public (define! context custom code)
  (All [anchor expression directive]
    (-> Context (Maybe Text) expression (Operation anchor expression directive [Text Any directive])))
  (function (_ (^@ stateE [bundle state]))
    (case (\ (get@ #host state) define! context custom code)
      (#try.Success output)
      (#try.Success [stateE output])

      (#try.Failure error)
      (exception.except ..cannot_interpret error))))

(def: .public (save! artifact_id custom code)
  (All [anchor expression directive]
    (-> artifact.ID (Maybe Text) directive (Operation anchor expression directive Any)))
  (do {! phase.monad}
    [?buffer (extension.read (get@ #buffer))]
    (case ?buffer
      (#.Some buffer)
      ## TODO: Optimize by no longer checking for overwrites...
      (if (row.any? (|>> product.left (n.= artifact_id)) buffer)
        (phase.except ..cannot_overwrite_output [artifact_id])
        (extension.update (set@ #buffer (#.Some (row.add [artifact_id custom code] buffer)))))
      
      #.None
      (phase.except ..no_buffer_for_saving_code [artifact_id]))))

(template [<name> <artifact>]
  [(def: .public (<name> name)
     (All [anchor expression directive]
       (-> Text (Operation anchor expression directive artifact.ID)))
     (function (_ (^@ stateE [bundle state]))
       (let [[id registry'] (<artifact> name (get@ #registry state))]
         (#try.Success [[bundle (set@ #registry registry' state)]
                        id]))))]

  [learn artifact.definition]
  [learn_custom artifact.custom]
  [learn_analyser artifact.analyser]
  [learn_synthesizer artifact.synthesizer]
  [learn_generator artifact.generator]
  [learn_directive artifact.directive]
  )

(exception: .public (unknown_definition {name Name}
                                        {known_definitions (List Text)})
  (exception.report
   ["Definition" (name.short name)]
   ["Module" (name.module name)]
   ["Known Definitions" (exception.listing function.identity known_definitions)]))

(def: .public (remember archive name)
  (All [anchor expression directive]
    (-> Archive Name (Operation anchor expression directive Context)))
  (function (_ (^@ stateE [bundle state]))
    (let [[_module _name] name]
      (do try.monad
        [module_id (archive.id _module archive)
         registry (if (text\= (get@ #module state) _module)
                    (#try.Success (get@ #registry state))
                    (do try.monad
                      [[descriptor document] (archive.find _module archive)]
                      (#try.Success (get@ #descriptor.registry descriptor))))]
        (case (artifact.remember _name registry)
          #.None
          (exception.except ..unknown_definition [name (artifact.definitions registry)])
          
          (#.Some id)
          (#try.Success [stateE [module_id id]]))))))

(exception: .public no_context)

(def: .public (module_id module archive)
  (All [anchor expression directive]
    (-> Module Archive (Operation anchor expression directive archive.ID)))
  (function (_ (^@ stateE [bundle state]))
    (do try.monad
      [module_id (archive.id module archive)]
      (in [stateE module_id]))))

(def: .public (context archive)
  (All [anchor expression directive]
    (-> Archive (Operation anchor expression directive Context)))
  (function (_ (^@ stateE [bundle state]))
    (case (get@ #context state)
      #.None
      (exception.except ..no_context [])
      
      (#.Some id)
      (do try.monad
        [module_id (archive.id (get@ #module state) archive)]
        (in [stateE [module_id id]])))))

(def: .public (with_context id body)
  (All [anchor expression directive a]
    (-> artifact.ID
        (Operation anchor expression directive a)
        (Operation anchor expression directive a)))
  (function (_ [bundle state])
    (do try.monad
      [[[bundle' state'] output] (body [bundle (set@ #context (#.Some id) state)])]
      (in [[bundle' (set@ #context (get@ #context state) state')]
           output]))))

(def: .public (with_new_context archive body)
  (All [anchor expression directive a]
    (-> Archive (Operation anchor expression directive a)
        (Operation anchor expression directive [Context a])))
  (function (_ (^@ stateE [bundle state]))
    (let [[id registry'] (artifact.resource (get@ #registry state))]
      (do try.monad
        [[[bundle' state'] output] (body [bundle (|> state
                                                     (set@ #registry registry')
                                                     (set@ #context (#.Some id)))])
         module_id (archive.id (get@ #module state) archive)]
        (in [[bundle' (set@ #context (get@ #context state) state')]
             [[module_id id]
              output]])))))

(def: .public (log! message)
  (All [anchor expression directive a]
    (-> Text (Operation anchor expression directive Any)))
  (function (_ [bundle state])
    (#try.Success [[bundle
                    (update@ #log (row.add message) state)]
                   []])))