blob: 62bfda31c9d044230cc37d8ca74b1267a1981d58 (
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
|
(.module:
[lux (#- loop)
[abstract
["." monad (#+ do)]]
[control
["p" parser]]
[data
["." maybe ("#;." monad)]
[number
["n" nat]]
[collection
["." list ("#;." functor)]]]
[macro
["." code]
["." syntax]]]
["." /// #_
## TODO: Remove the 'extension' import ASAP.
["#." extension]
["#/" //
["#." reference (#+ Register Variable)]
["#." analysis (#+ Environment)]
["/" synthesis (#+ Path Abstraction Synthesis)]]])
(type: #export (Transform a)
(-> a (Maybe a)))
(def: (some? maybe)
(All [a] (-> (Maybe a) Bit))
(case maybe
(#.Some _) #1
#.None #0))
(template: #export (self)
(#/.Reference (////reference.local 0)))
(template: (recursive-apply args)
(#/.Apply (self) args))
(def: improper #0)
(def: proper #1)
(def: (proper? exprS)
(-> Synthesis Bit)
(case exprS
(^ (self))
improper
(#/.Structure structure)
(case structure
(#////analysis.Variant variantS)
(proper? (get@ #////analysis.value variantS))
(#////analysis.Tuple membersS+)
(list.every? proper? membersS+))
(#/.Control controlS)
(case controlS
(#/.Branch branchS)
(case branchS
(#/.Case inputS pathS)
(and (proper? inputS)
(.loop [pathS pathS]
(case pathS
(^or (#/.Alt leftS rightS) (#/.Seq leftS rightS))
(and (recur leftS) (recur rightS))
(#/.Then bodyS)
(proper? bodyS)
_
proper)))
(#/.Let inputS register bodyS)
(and (proper? inputS)
(proper? bodyS))
(#/.If inputS thenS elseS)
(and (proper? inputS)
(proper? thenS)
(proper? elseS)))
(#/.Loop loopS)
(case loopS
(#/.Scope scopeS)
(and (list.every? proper? (get@ #/.inits scopeS))
(proper? (get@ #/.iteration scopeS)))
(#/.Recur argsS)
(list.every? proper? argsS))
(#/.Function functionS)
(case functionS
(#/.Abstraction environment arity bodyS)
(list.every? ////reference.self? environment)
(#/.Apply funcS argsS)
(and (proper? funcS)
(list.every? proper? argsS))))
(#/.Extension [name argsS])
(list.every? proper? argsS)
_
proper))
(def: (path-recursion synthesis-recursion)
(-> (Transform Synthesis) (Transform Path))
(function (recur pathS)
(case pathS
(#/.Alt leftS rightS)
(let [leftS' (recur leftS)
rightS' (recur rightS)]
(if (or (some? leftS')
(some? rightS'))
(#.Some (#/.Alt (maybe.default leftS leftS')
(maybe.default rightS rightS')))
#.None))
(#/.Seq leftS rightS)
(maybe;map (|>> (#/.Seq leftS)) (recur rightS))
(#/.Then bodyS)
(maybe;map (|>> #/.Then) (synthesis-recursion bodyS))
_
#.None)))
(def: #export (recursion arity)
(-> Nat (Transform Synthesis))
(function (recur exprS)
(case exprS
(#/.Control controlS)
(case controlS
(#/.Branch branchS)
(case branchS
(#/.Case inputS pathS)
(|> pathS
(path-recursion recur)
(maybe;map (|>> (#/.Case inputS) #/.Branch #/.Control)))
(#/.Let inputS register bodyS)
(maybe;map (|>> (#/.Let inputS register) #/.Branch #/.Control)
(recur bodyS))
(#/.If inputS thenS elseS)
(let [thenS' (recur thenS)
elseS' (recur elseS)]
(if (or (some? thenS')
(some? elseS'))
(#.Some (|> (#/.If inputS
(maybe.default thenS thenS')
(maybe.default elseS elseS'))
#/.Branch #/.Control))
#.None)))
(^ (#/.Function (recursive-apply argsS)))
(if (n.= arity (list.size argsS))
(#.Some (|> argsS #/.Recur #/.Loop #/.Control))
#.None)
_
#.None)
_
#.None)))
(def: (resolve environment)
(-> Environment (Transform Variable))
(function (_ variable)
(case variable
(#////reference.Foreign register)
(list.nth register environment)
_
(#.Some variable))))
(def: (adjust-path adjust-synthesis offset)
(-> (Transform Synthesis) Register (Transform Path))
(function (recur pathS)
(case pathS
(#/.Bind register)
(#.Some (#/.Bind (n.+ offset register)))
(^template [<tag>]
(<tag> leftS rightS)
(do maybe.monad
[leftS' (recur leftS)
rightS' (recur rightS)]
(wrap (<tag> leftS' rightS'))))
([#/.Alt] [#/.Seq])
(#/.Then bodyS)
(|> bodyS adjust-synthesis (maybe;map (|>> #/.Then)))
_
(#.Some pathS))))
(def: (adjust scope-environment offset)
(-> Environment Register (Transform Synthesis))
(function (recur exprS)
(case exprS
(#/.Structure structureS)
(case structureS
(#////analysis.Variant variantS)
(do maybe.monad
[valueS' (|> variantS (get@ #////analysis.value) recur)]
(wrap (|> variantS
(set@ #////analysis.value valueS')
#////analysis.Variant
#/.Structure)))
(#////analysis.Tuple membersS+)
(|> membersS+
(monad.map maybe.monad recur)
(maybe;map (|>> #////analysis.Tuple #/.Structure))))
(#/.Reference reference)
(case reference
(^ (////reference.constant constant))
(#.Some exprS)
(^ (////reference.local register))
(#.Some (#/.Reference (////reference.local (n.+ offset register))))
(^ (////reference.foreign register))
(|> scope-environment
(list.nth register)
(maybe;map (|>> #////reference.Variable #/.Reference))))
(^ (/.branch/case [inputS pathS]))
(do maybe.monad
[inputS' (recur inputS)
pathS' (adjust-path recur offset pathS)]
(wrap (|> pathS' [inputS'] /.branch/case)))
(^ (/.branch/let [inputS register bodyS]))
(do maybe.monad
[inputS' (recur inputS)
bodyS' (recur bodyS)]
(wrap (/.branch/let [inputS' register bodyS'])))
(^ (/.branch/if [inputS thenS elseS]))
(do maybe.monad
[inputS' (recur inputS)
thenS' (recur thenS)
elseS' (recur elseS)]
(wrap (/.branch/if [inputS' thenS' elseS'])))
(^ (/.loop/scope scopeS))
(do maybe.monad
[inits' (|> scopeS
(get@ #/.inits)
(monad.map maybe.monad recur))
iteration' (recur (get@ #/.iteration scopeS))]
(wrap (/.loop/scope {#/.start (|> scopeS (get@ #/.start) (n.+ offset))
#/.inits inits'
#/.iteration iteration'})))
(^ (/.loop/recur argsS))
(|> argsS
(monad.map maybe.monad recur)
(maybe;map (|>> /.loop/recur)))
(^ (/.function/abstraction [environment arity bodyS]))
(do maybe.monad
[environment' (monad.map maybe.monad
(resolve scope-environment)
environment)]
(wrap (/.function/abstraction [environment' arity bodyS])))
(^ (/.function/apply [function arguments]))
(do maybe.monad
[function' (recur function)
arguments' (monad.map maybe.monad recur arguments)]
(wrap (/.function/apply [function' arguments'])))
(#/.Extension [name argsS])
(|> argsS
(monad.map maybe.monad recur)
(maybe;map (|>> [name] #/.Extension)))
_
(#.Some exprS))))
(def: #export (loop environment num-locals inits functionS)
(-> Environment Nat (List Synthesis) Abstraction (Maybe Synthesis))
(let [bodyS (get@ #/.body functionS)]
(if (and (n.= (list.size inits)
(get@ #/.arity functionS))
(proper? bodyS))
(|> bodyS
(adjust environment num-locals)
(maybe;map (|>> [(inc num-locals) inits] /.loop/scope)))
#.None)))
|