aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/synthesis/loop.lux
blob: 27ce06b2538074c7860e4f053a7384007f184e45 (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
... 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
    ["[0]" maybe (.use "[1]#[0]" monad)]]
   [data
    [collection
     ["[0]" list]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [macro
     ["^" pattern]]]]]
 [////
  ["/" synthesis (.only Path Abstraction)]
  ["[0]" analysis (.only Environment)
   ["[1]/[0]" complex]]
  [///
   [arity (.only Arity)]
   ["[0]" reference (.only)
    ["[0]" variable (.only Register Variable)]]]])

(type .public (Transform of)
  (-> of
      (Maybe of)))

(def .public (register_optimization offset)
  (-> Register
      (-> Register
          Register))
  (|>> -- (n.+ offset)))

(def (path_optimization body_optimization offset)
  (-> (Transform /.Term) Register
      (Transform Path))
  (function (again path)
    (when path
      {/.#Bind register}
      {.#Some {/.#Bind (register_optimization offset register)}}

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

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

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

      _
      {.#Some path})))

(def (body_optimization true_loop? offset scope_environment arity expr)
  (-> Bit Register (Environment /.Term) Arity
      (Transform /.Term))
  (loop (again [return? true
                expr expr])
    (when expr
      [@ {/.#Simple _}]
      {.#Some expr}

      [@ {/.#Structure structure}]
      (when structure
        {analysis/complex.#Variant variant}
        (do maybe.monad
          [value' (|> variant (the analysis/complex.#value) (again false))]
          (in (|> variant
                  (has analysis/complex.#value value')
                  (/.variant @))))
        
        {analysis/complex.#Tuple tuple}
        (|> tuple
            (monad.each maybe.monad (again false))
            (maybe#each (|>> (/.tuple @)))))

      [@ {/.#Reference reference}]
      (when reference
        {reference.#Variable (variable.self)}
        (if true_loop?
          {.#None}
          {.#Some expr})
        
        (reference.constant constant)
        {.#Some expr}

        (reference.local register)
        {.#Some [@ {/.#Reference (reference.local (register_optimization offset register))}]}

        (reference.foreign register)
        (if true_loop?
          (list.item register scope_environment)
          {.#Some expr}))

      (/.branch/when @ [input path])
      (do maybe.monad
        [input' (again false input)
         path' (path_optimization (again return?) offset path)]
        (in (|> path' [input'] (/.branch/when @))))

      (/.branch/exec @ [this that])
      (do maybe.monad
        [this (again false this)
         that (again return? that)]
        (in (/.branch/exec @ [this that])))

      (/.branch/let @ [input register body])
      (do maybe.monad
        [input' (again false input)
         body' (again return? body)]
        (in (/.branch/let @ [input' (register_optimization offset register) body'])))

      (/.branch/if @ [input then else])
      (do maybe.monad
        [input' (again false input)
         then' (again return? then)
         else' (again return? else)]
        (in (/.branch/if @ [input' then' else'])))

      (/.branch/get @ [path record])
      (do maybe.monad
        [record (again false record)]
        (in (/.branch/get @ [path record])))

      (/.loop/scope @ scope)
      (do [! maybe.monad]
        [inits' (|> scope
                    (the /.#inits)
                    (monad.each ! (again false)))
         iteration' (again return? (the /.#iteration scope))]
        (in (/.loop/scope @ [/.#start (|> scope (the /.#start) (register_optimization offset))
                             /.#inits inits'
                             /.#iteration iteration'])))

      (/.loop/again @ args)
      (|> args
          (monad.each maybe.monad (again false))
          (maybe#each (|>> (/.loop/again @))))

      (/.function/abstraction @ [environment arity body])
      (do [! maybe.monad]
        [environment' (monad.each ! (again false) environment)]
        (in (/.function/abstraction @ [environment' arity body])))
      
      (/.function/apply @ [abstraction arguments])
      (do [! maybe.monad]
        [arguments' (monad.each ! (again false) arguments)]
        (with_expansions [<application> (these (do !
                                                 [abstraction' (again false abstraction)]
                                                 (in (/.function/apply @ [abstraction' arguments']))))]
          (when abstraction
            [@ {/.#Reference {reference.#Variable (variable.self)}}]
            (if (and return?
                     (n.= arity (list.size arguments)))
              (in (/.loop/again @ arguments'))
              (if true_loop?
                {.#None}
                <application>))
            
            _
            <application>)))

      ... TODO: Stop relying on this custom code.
      [@ {/.#Extension [[.prelude "when_char#|translation"] (list.partial input else matches)]}]
      (if return?
        (do [! maybe.monad]
          [input (again false input)
           matches (monad.each !
                               (is (-> /.Term
                                       (Maybe /.Term))
                                   (function (_ match)
                                     (when match
                                       [@ {/.#Structure {analysis/complex.#Tuple (list when then)}}]
                                       (do !
                                         [when (again false when)
                                          then (again return? then)]
                                         (in [@ {/.#Structure {analysis/complex.#Tuple (list when then)}}]))

                                       _
                                       (again false match))))
                               matches)
           else (again return? else)]
          (in [@ {/.#Extension [[.prelude "when_char#|translation"] (list.partial input else matches)]}]))
        {.#None})

      [@ {/.#Extension [name args]}]
      (|> args
          (monad.each maybe.monad (again false))
          (maybe#each (|>> [name] {/.#Extension} [@]))))))

(def .public (optimization true_loop? offset inits functionS)
  (-> Bit Register (List /.Term) Abstraction
      (Maybe [Register (List /.Term) /.Term]))
  (|> (the /.#body functionS)
      (body_optimization true_loop? offset (the /.#environment functionS) (the /.#arity functionS))
      (maybe#each (|>> [offset inits]))))