aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/synthesis/function.lux
blob: 8d1b71d902f937a87765cb0fb54b15a7bf2664ba (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
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    ["ex" exception (#+ exception:)]]
   [data
    ["." maybe]
    ["." text
     format]
    [collection
     ["." list ("#;." functor monoid fold)]
     ["dict" dictionary (#+ Dictionary)]]]]
  ["." // #_
   ["#." loop (#+ Transform)]
   ["#/" // ("#;." monad)
    ["#/" // #_
     ["#." reference (#+ Register Variable)]
     ["#." analysis (#+ Environment Arity Analysis)]
     ["/" synthesis (#+ Path Synthesis Operation Phase)]]]])

(exception: #export (cannot-find-foreign-variable-in-environment {foreign Register} {environment Environment})
  (ex.report ["Foreign" (%n foreign)]
             ["Environment" (|> environment
                                (list;map ////reference.%variable)
                                (text.join-with " "))]))

(def: arity-arguments
  (-> Arity (List Synthesis))
  (|>> dec
       (list.n/range 1)
       (list;map (|>> /.variable/local))))

(template: #export (self-reference)
  (/.variable/local 0))

(def: (expanded-nested-self-reference arity)
  (-> Arity Synthesis)
  (/.function/apply [(..self-reference) (arity-arguments arity)]))

(def: #export (apply phase)
  (-> Phase Phase)
  (function (_ exprA)
    (let [[funcA argsA] (////analysis.application exprA)]
      (do ///.monad
        [funcS (phase funcA)
         argsS (monad.map @ phase argsA)
         ## locals /.locals
         ]
        (with-expansions [<apply> (as-is (/.function/apply [funcS argsS]))]
          (case funcS
            ## (^ (/.function/abstraction functionS))
            ## (wrap (|> functionS
            ##           (//loop.loop (get@ #/.environment functionS) locals argsS)
            ##           (maybe.default <apply>)))

            (^ (/.function/apply [funcS' argsS']))
            (wrap (/.function/apply [funcS' (list;compose argsS' argsS)]))

            _
            (wrap <apply>)))))))

(def: (find-foreign environment register)
  (-> Environment Register (Operation Variable))
  (case (list.nth register environment)
    (#.Some aliased)
    (///;wrap aliased)

    #.None
    (///.throw cannot-find-foreign-variable-in-environment [register environment])))

(def: (grow-path grow path)
  (-> (-> Synthesis (Operation Synthesis)) Path (Operation Path))
  (case path
    (#/.Bind register)
    (///;wrap (#/.Bind (inc register)))

    (^template [<tag>]
      (<tag> left right)
      (do ///.monad
        [left' (grow-path grow left)
         right' (grow-path grow right)]
        (wrap (<tag> left' right'))))
    ([#/.Alt] [#/.Seq])
    
    (#/.Then thenS)
    (|> thenS
        grow
        (///;map (|>> #/.Then)))

    _
    (///;wrap path)))

(def: (grow-sub-environment super sub)
  (-> Environment Environment (Operation Environment))
  (monad.map ///.monad
             (function (_ variable)
               (case variable
                 (#////reference.Local register)
                 (///;wrap (#////reference.Local (inc register)))
                 
                 (#////reference.Foreign register)
                 (find-foreign super register)))
             sub))

(def: (grow environment expression)
  (-> Environment Synthesis (Operation Synthesis))
  (case expression
    (#/.Structure structure)
    (case structure
      (#////analysis.Variant [lefts right? subS])
      (|> subS
          (grow environment)
          (///;map (|>> [lefts right?] /.variant)))
      
      (#////analysis.Tuple membersS+)
      (|> membersS+
          (monad.map ///.monad (grow environment))
          (///;map (|>> /.tuple))))

    (^ (..self-reference))
    (///;wrap (/.function/apply [expression (list (/.variable/local 1))]))
    
    (#/.Reference reference)
    (case reference
      (#////reference.Variable variable)
      (case variable
        (#////reference.Local register)
        (///;wrap (/.variable/local (inc register)))
        
        (#////reference.Foreign register)
        (|> register
            (find-foreign environment)
            (///;map (|>> /.variable))))
      
      (#////reference.Constant constant)
      (///;wrap expression))
    
    (#/.Control control)
    (case control
      (#/.Branch branch)
      (case branch
        (#/.Let [inputS register bodyS])
        (do ///.monad
          [inputS' (grow environment inputS)
           bodyS' (grow environment bodyS)]
          (wrap (/.branch/let [inputS' (inc register) bodyS'])))
        
        (#/.If [testS thenS elseS])
        (do ///.monad
          [testS' (grow environment testS)
           thenS' (grow environment thenS)
           elseS' (grow environment elseS)]
          (wrap (/.branch/if [testS' thenS' elseS'])))
        
        (#/.Case [inputS pathS])
        (do ///.monad
          [inputS' (grow environment inputS)
           pathS' (grow-path (grow environment) pathS)]
          (wrap (/.branch/case [inputS' pathS']))))
      
      (#/.Loop loop)
      (case loop
        (#/.Scope [start initsS+ iterationS])
        (do ///.monad
          [initsS+' (monad.map @ (grow environment) initsS+)
           iterationS' (grow environment iterationS)]
          (wrap (/.loop/scope [start initsS+' iterationS'])))
        
        (#/.Recur argumentsS+)
        (|> argumentsS+
            (monad.map ///.monad (grow environment))
            (///;map (|>> /.loop/recur))))
      
      (#/.Function function)
      (case function
        (#/.Abstraction [_env _arity _body])
        (do ///.monad
          [_env' (grow-sub-environment environment _env)]
          (wrap (/.function/abstraction [_env' _arity _body])))
        
        (#/.Apply funcS argsS+)
        (case funcS
          (^ (/.function/apply [(..self-reference) pre-argsS+]))
          (///;wrap (/.function/apply [(..self-reference)
                                       (list;compose pre-argsS+ argsS+)]))
          
          _
          (do ///.monad
            [funcS' (grow environment funcS)
             argsS+' (monad.map @ (grow environment) argsS+)]
            (wrap (/.function/apply [funcS' argsS+']))))))
    
    (#/.Extension name argumentsS+)
    (|> argumentsS+
        (monad.map ///.monad (grow environment))
        (///;map (|>> (#/.Extension name))))

    _
    (///;wrap expression)))

(def: #export (abstraction phase environment bodyA)
  (-> Phase Environment Analysis (Operation Synthesis))
  (do ///.monad
    [bodyS (phase bodyA)]
    (case bodyS
      (^ (/.function/abstraction [env' down-arity' bodyS']))
      (|> bodyS'
          (grow env')
          (:: @ map (|>> [environment (inc down-arity')] /.function/abstraction)))
      
      _
      (wrap (/.function/abstraction [environment 1 bodyS])))))