aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/loop.lux
blob: d9890dbc9c26c6087a801379f6b8678b1f11e987 (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
(.module:
  [library
   [lux #*
    [abstract
     ["." monad (#+ do)]]
    [control
     ["." maybe ("#\." monad)]]
    [data
     [collection
      ["." list]]]
    [math
     [number
      ["n" nat]]]]]
  [////
   ["." analysis (#+ Environment)]
   ["/" synthesis (#+ Path Abstraction Synthesis)]
   [///
    [arity (#+ Arity)]
    ["." reference
     ["." variable (#+ Register Variable)]]]])

(type: .public (Transform a)
  (-> a (Maybe a)))

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

(def: (path_optimization body_optimization offset)
  (-> (Transform Synthesis) Register (Transform Path))
  (function (recur path)
    (case path
      (#/.Bind register)
      (#.Some (#/.Bind (register_optimization offset register)))

      (^template [<tag>]
        [(<tag> left right)
         (do maybe.monad
           [left' (recur left)
            right' (recur right)]
           (in (<tag> left' right')))])
      ([#/.Alt] [#/.Seq])

      (#/.Bit_Fork when then else)
      (do {! maybe.monad}
        [then (recur then)
         else (case else
                (#.Some else)
                (\ ! map (|>> #.Some) (recur else))

                #.None
                (in #.None))]
        (in (#/.Bit_Fork when then else)))
      
      (^template [<tag>]
        [(<tag> [[test then] elses])
         (do {! maybe.monad}
           [then (recur then)
            elses (monad.map ! (function (_ [else_test else_then])
                                 (do !
                                   [else_then (recur 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\map (|>> #/.Then)))

      _
      (#.Some path))))

(def: (body_optimization true_loop? offset scope_environment arity expr)
  (-> Bit Register (Environment Synthesis) Arity (Transform Synthesis))
  (loop [return? true
         expr expr]
    (case expr
      (#/.Primitive _)
      (#.Some expr)

      (#/.Structure structure)
      (case structure
        (#analysis.Variant variant)
        (do maybe.monad
          [value' (|> variant (get@ #analysis.value) (recur false))]
          (in (|> variant
                  (set@ #analysis.value value')
                  /.variant)))
        
        (#analysis.Tuple tuple)
        (|> tuple
            (monad.map maybe.monad (recur false))
            (maybe\map (|>> /.tuple))))

      (#/.Reference reference)
      (case 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/case [input path]))
      (do maybe.monad
        [input' (recur false input)
         path' (path_optimization (recur return?) offset path)]
        (in (|> path' [input'] /.branch/case)))

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

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

      (^ (/.branch/get [path record]))
      (do maybe.monad
        [record (recur false record)]
        (in (/.branch/get [path record])))

      (^ (/.loop/scope scope))
      (do {! maybe.monad}
        [inits' (|> scope
                    (get@ #/.inits)
                    (monad.map ! (recur false)))
         iteration' (recur return? (get@ #/.iteration scope))]
        (in (/.loop/scope {#/.start (|> scope (get@ #/.start) (register_optimization offset))
                           #/.inits inits'
                           #/.iteration iteration'})))

      (^ (/.loop/recur args))
      (|> args
          (monad.map maybe.monad (recur false))
          (maybe\map (|>> /.loop/recur)))

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

      (#/.Extension [name args])
      (|> args
          (monad.map maybe.monad (recur false))
          (maybe\map (|>> [name] #/.Extension))))))

(def: .public (optimization true_loop? offset inits functionS)
  (-> Bit Register (List Synthesis) Abstraction (Maybe [Register (List Synthesis) Synthesis]))
  (|> (get@ #/.body functionS)
      (body_optimization true_loop? offset (get@ #/.environment functionS) (get@ #/.arity functionS))
      (maybe\map (|>> [offset inits]))))