aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/synthesis/function.lux
blob: 0c9b3b3ab8949590a714bffd175ea8fb76ae7a87 (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
... 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)]
    ["[0]" enum]]
   [control
    ["[0]" pipe]
    ["[0]" maybe (.use "[1]#[0]" functor)]
    ["[0]" exception (.only Exception)]]
   [data
    [text
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor monoid)]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [macro
     ["^" pattern]]]]]
 ["[0]" //
  ["[1][0]" loop (.only Transform)]
  ["//[1]" ///
   ["/" synthesis (.only Path Abstraction Operation Phase)]
   ["[0]" phase (.use "[1]#[0]" monad)]
   ["[0]" analysis (.only Environment)
    ["[1]/[0]" complex]]
   [///
    [arity (.only Arity)]
    ["[1][0]" reference (.only)
     ["[1]/[0]" variable (.only Register Variable)]]]]])

(exception.def .public (cannot_find_foreign_variable_in_environment [foreign environment])
  (Exception [Register (Environment /.Term)])
  (exception.report
   (list ["Foreign" (%.nat foreign)]
         ["Environment" (exception.listing /.%synthesis environment)])))

(def .public self_reference
  (template (self_reference @)
    [(/.variable/local @ 0)]))

(def .public (apply @ phase)
  (-> Location Phase
      Phase)
  (function (_ archive exprA)
    (let [[funcA argsA] (analysis.reification exprA)]
      (do [! phase.monad]
        [funcS (phase archive funcA)
         argsS (monad.each ! (phase archive) argsA)]
        (with_expansions [<apply> (these (/.function/apply @ [funcS argsS]))]
          (when funcS
            (/.function/abstraction @ functionS)
            (if (n.= (the /.#arity functionS)
                     (list.size argsS))
              (do !
                [locals /.locals]
                (in (|> functionS
                        (//loop.optimization true locals argsS)
                        (maybe#each (is (-> [Nat (List /.Term) /.Term]
                                            /.Term)
                                        (function (_ [start inits iteration])
                                          (when iteration
                                            (/.loop/scope @ [start' inits' output])
                                            (if (and (n.= start start')
                                                     (list.empty? inits'))
                                              (/.loop/scope @ [start inits output])
                                              (/.loop/scope @ [start inits iteration]))

                                            _
                                            (/.loop/scope @ [start inits iteration])))))
                        (maybe.else <apply>))))
              (in <apply>))

            (/.function/apply @ [funcS' argsS'])
            (in (/.function/apply @ [funcS' (list#composite argsS' argsS)]))

            _
            (in <apply>)))))))

(def (find_foreign environment register)
  (-> (Environment /.Term) Register
      (Operation /.Term))
  (when (list.item register environment)
    {.#Some aliased}
    (phase#in aliased)

    {.#None}
    (phase.except ..cannot_find_foreign_variable_in_environment [register environment])))

(def (grow_path grow path)
  (-> (-> /.Term (Operation /.Term)) Path
      (Operation Path))
  (when path
    {/.#Bind register}
    (phase#in {/.#Bind (++ register)})

    (^.with_template [<tag>]
      [{<tag> left right}
       (do phase.monad
         [left' (grow_path grow left)
          right' (grow_path grow right)]
         (in {<tag> left' right'}))])
    ([/.#Alt] [/.#Seq])

    {/.#Bit_Fork test then else}
    (do [! phase.monad]
      [then (grow_path grow then)
       else (when else
              {.#Some else}
              (of ! each (|>> {.#Some}) (grow_path grow else))

              {.#None}
              (in {.#None}))]
      (in {/.#Bit_Fork test then else}))
    
    (^.with_template [<tag>]
      [{<tag> [[test then] elses]}
       (do [! phase.monad]
         [then (grow_path grow then)
          elses (monad.each ! (function (_ [else_test else_then])
                                (do !
                                  [else_then (grow_path grow else_then)]
                                  (in [else_test else_then])))
                            elses)]
         (in {<tag> [[test then] elses]}))])
    ([/.#I64_Fork]
     [/.#F64_Fork]
     [/.#Text_Fork])
    
    {/.#Then thenS}
    (|> thenS
        grow
        (phase#each (|>> {/.#Then})))

    _
    (phase#in path)))

(def (grow environment expression)
  (-> (Environment /.Term) /.Term
      (Operation /.Term))
  (when expression
    [@ {/.#Structure structure}]
    (when structure
      {analysis/complex.#Variant [lefts right? subS]}
      (|> subS
          (grow environment)
          (phase#each (|>> [lefts right?] (/.variant @))))
      
      {analysis/complex.#Tuple membersS+}
      (|> membersS+
          (monad.each phase.monad (grow environment))
          (phase#each (|>> (/.tuple @)))))

    (..self_reference @)
    (phase#in (/.function/apply @ [expression (list (/.variable/local @ 1))]))
    
    [@ {/.#Reference reference}]
    (when reference
      {////reference.#Variable variable}
      (when variable
        {////reference/variable.#Local register}
        (phase#in (/.variable/local @ (++ register)))
        
        {////reference/variable.#Foreign register}
        (..find_foreign environment register))
      
      {////reference.#Constant constant}
      (phase#in expression))
    
    [@ {/.#Control control}]
    (when control
      {/.#Branch branch}
      (when branch
        {/.#Exec [this that]}
        (do phase.monad
          [this (grow environment this)
           that (grow environment that)]
          (in (/.branch/exec @ [this that])))
        
        {/.#Let [inputS register bodyS]}
        (do phase.monad
          [inputS' (grow environment inputS)
           bodyS' (grow environment bodyS)]
          (in (/.branch/let @ [inputS' (++ register) bodyS'])))
        
        {/.#If [testS thenS elseS]}
        (do phase.monad
          [testS' (grow environment testS)
           thenS' (grow environment thenS)
           elseS' (grow environment elseS)]
          (in (/.branch/if @ [testS' thenS' elseS'])))

        {/.#Get members inputS}
        (do phase.monad
          [inputS' (grow environment inputS)]
          (in (/.branch/get @ [members inputS'])))
        
        {/.#When [inputS pathS]}
        (do phase.monad
          [inputS' (grow environment inputS)
           pathS' (grow_path (grow environment) pathS)]
          (in (/.branch/when @ [inputS' pathS']))))
      
      {/.#Loop loop}
      (when loop
        {/.#Scope [start initsS+ iterationS]}
        (do [! phase.monad]
          [initsS+' (monad.each ! (grow environment) initsS+)
           iterationS' (grow environment iterationS)]
          (in (/.loop/scope @ [(++ start) initsS+' iterationS'])))
        
        {/.#Again argumentsS+}
        (|> argumentsS+
            (monad.each phase.monad (grow environment))
            (phase#each (|>> (/.loop/again @)))))
      
      {/.#Function function}
      (when function
        {/.#Abstraction [_env _arity _body]}
        (do [! phase.monad]
          [_env' (monad.each !
                             (|>> (pipe.when
                                    [@ {/.#Reference {////reference.#Variable {////reference/variable.#Foreign register}}}]
                                    (..find_foreign environment register)

                                    captured
                                    (grow environment captured)))
                             _env)]
          (in (/.function/abstraction @ [_env' _arity _body])))
        
        {/.#Apply funcS argsS+}
        (do [! phase.monad]
          [funcS (grow environment funcS)
           argsS+ (monad.each ! (grow environment) argsS+)]
          (in (/.function/apply @ (when funcS
                                    (/.function/apply @ [(..self_reference @) pre_argsS+])
                                    [(..self_reference @)
                                     (list#composite pre_argsS+ argsS+)]

                                    _
                                    [funcS
                                     argsS+]))))))
    
    [@ {/.#Extension name argumentsS+}]
    (|> argumentsS+
        (monad.each phase.monad (grow environment))
        (phase#each (|>> {/.#Extension name} [@])))

    [@ {/.#Simple _}]
    (phase#in expression)))

(def .public (abstraction @ phase environment archive bodyA)
  (-> Location Phase (Environment analysis.Term)
      Phase)
  (do [! phase.monad]
    [environment (monad.each ! (phase archive) environment)
     bodyS (/.with_currying? true
             (/.with_locals 2
               (phase archive bodyA)))
     abstraction (is (Operation Abstraction)
                     (when bodyS
                       (/.function/abstraction @ [env' down_arity' bodyS'])
                       (|> bodyS'
                           (grow env')
                           (of ! each (function (_ body)
                                        [/.#environment environment
                                         /.#arity (++ down_arity')
                                         /.#body body])))
                       
                       _
                       (in [/.#environment environment
                            /.#arity 1
                            /.#body bodyS])))
     currying? /.currying?]
    (in (<| (/.function/abstraction @)
            (if currying?
              abstraction
              (when (//loop.optimization false 1 (list) abstraction)
                {.#Some [startL initsL bodyL]}
                [/.#environment environment
                 /.#arity (the /.#arity abstraction)
                 /.#body (/.loop/scope @ [startL initsL bodyL])]
                
                {.#None}
                abstraction))))))