-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorise.lua
More file actions
362 lines (318 loc) · 10.4 KB
/
vectorise.lua
File metadata and controls
362 lines (318 loc) · 10.4 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require 'unicode'
require 'exp'
local vlib = lib()
function numargs(functype)
local argtype = arg0(functype)
if fn(functype) == '→' and isobj(argtype) and obj(argtype) == ',' then
if #argtype <= 4 then
return #argtype
end
end
return 1
end
local simpel = set('getal', 'int', 'bit', 'functie')
local cmp = set('>', '<', '=g', '≠', '≥', '≤')
local function issimpel(type)
return not not simpel[atom(type)]
end
local function isfunctie(exp)
return atom(exp) == 'functie' or fn(exp) == '→'
end
-- past types toe om operators te preoverloaden
function vectorise(asb, types, debug)
local primair = {}
local function rec(exp)
primair[exp] = true
if fn(exp) == '⇒' then
rec(arg1(exp))
if arg2(exp) then
rec(arg2(exp))
end
end
if fn(exp) == '⋀' then
for i, sub in ipairs(arg(exp)) do
rec(sub)
end
end
end
rec(asb)
for exp in treepairsdfs(asb) do
-- cmp
if cmp[fn(exp)] then
local atype = types[hash(arg0(exp))]
local btype = types[hash(arg1(exp))]
if isfunctie(atype) then
if isfunctie(btype) then
exp.f = X(fn(exp)..'f')
else
exp.f = X(fn(exp)..'f1')
end
end
end
-- filter2,3,4
if fname(exp) == 'filter' or fname(exp) == 'map' then
local type = types[hash(exp)]
local atype = types[hash(arg0(exp))]
local functype = types[hash(arg1(exp)[2])]
local num = numargs(functype)
if num > 1 then
exp.a[1] = X(atom(exp.a[1])..'4')
types[hash(exp)] = type
types[hash(arg0(exp))] = atype
end
end
-- cart tuple
-- ((a,b)[] × c) -> ([(a,b)[] ×t c)
-- ×l
if fn(exp) == '×' then
local atype = types[hash(arg0(exp))]
local itemtype = arg1(atype)
if fn(atype) == '→' and obj(itemtype) == ',' then
assign(exp.f, X'×t')
end
end
-- call1,2,3,4
if fn(exp) == '_' then
local type = types[hash(arg0(exp))]
--local argtype = arg0(type)
local argtype = types[hash(arg1(exp))]
if argtype then
local args = arg1(exp)
if false and issimpel(argtype) then
local type = types[hash(exp)]
--local nexp = substitute(arg0(exp), X('_arg'), X('_arg0'))
assign(exp, X('call1', arg0(exp), args))
types[hash(exp)] = type
end
if obj(args) == ',' then
local type = types[hash(exp)]
if #args == 2 then
assign(exp, X('call2', arg0(exp), args[1], args[2]))
elseif #args == 3 then
assign(exp, X('call3', arg0(exp), args[1], args[2], args[3]))
elseif #args == 4 then
assign(exp, X('call4', arg0(exp), args[1], args[2], args[3], args[4]))
end
types[hash(exp)] = type
end
end
end
-- rtti?
if fn(exp) == '_' and atom(arg0(exp)) == 'type' then
local type = deparse(types[hash(arg1(exp))])
local text = {o=X'"'}
for i,u in utf8pairs(type) do
text[i] = X(tostring(u))
end
--error(unlisp(text))
assign(exp, text)
end
-- L_i → (_i)(L, i)
if fn(exp) == '_' then
local fntype = types[hash(arg0(exp))]
local islijst = arg0(fntype) and atom(arg0(fntype)) == 'nat' or obj(fntype) == ','
local istext = fn(fntype) == '→' and atom(arg0(fntype)) == 'nat' and atom(arg1(fntype)) == 'letter'
local isfunc = fn(fntype) == '→' and atom(arg0(fntype)) ~= 'nat'
if islijst then
exp.f = X'index'
elseif istext then
exp.f = X'_t'
elseif isfunc or true then
local nargs = #arg0(fntype)
if nargs > 4 then
nargs = 1
end
--exp.f = X('call'..nargs)
--exp.f = X('call')
local args = exp.a[2]
--print(hash(exp.a[2]))
--error(deparse(exp))
if false and nargs > 1 then
-- clear
for i,v in ipairs(exp.a) do
exp.a[i] = nil
end
-- vul
for i=1,nargs do
exp.a[i] = X(',', X('index', args, tostring(i)))
end
end
if atom(arg0(exp)) == 'icode' then
local oud = types[hash(exp)]
assign(exp, X('icode', arg1(exp)))
types[hash(exp)] = oud
elseif nargs > 1 then
exp.f = X'call'
else
exp.f = X'call'
end
else
error'NEE'
--print('Waarschuwing: vectortype van '..unlisp(exp)..' kon niet eenduidig worden bepaald')
end
end
-- (X map [1,2,3]) → (X lmap [1,2,3])
if fname(exp) == 'map' then
local maptype = types[hash(arg2(exp))]
if fn(maptype) == '→' and atom(arg0(maptype)) == 'nat' then
exp.a[1] = X'lmap'
end
end
-- (F^i) → (^)(F, i)
if fn(exp) == '^' then
local basetype = types[hash(arg0(exp))]
local islijst = atom(arg0(basetype)) == 'nat'-- or obj(basetype) == ','
local isfunc = fn(basetype) == '→' or atom(basetype) == 'functie'
if islijst then
exp.f = X'^l'
elseif isfunc then
exp.f = X'^f'
elseif atom(basetype) == 'getal' or atom(basetype) == 'int' then
exp.f = X'^'
else
-- niets
end
end
-- (1 = 2) ⇒ (1 =g 2)
if not primair[exp] and fn(exp) == '=' or fn(exp) == '≠' then
local type = types[hash(exp)]
local atype = types[hash(arg0(exp))]
local btype = types[hash(arg1(exp))]
local agetal = atom(atype) == 'getal' or atom(atype) == 'int'
local bgetal = atom(btype) == 'getal' or atom(btype) == 'int'
if hash(type) ~= 'ok' then
if agetal or bgetal then
exp.f = X(fn(exp)..'g')
elseif atom(atype) == 'bit' then
--exp.f = X(fn(exp)..'g')
end
end
end
-- TODO set -
-- (+) → +v | +v1 | +m | +m1 | +f
if fn(exp) == '+' then
local atype = types[hash(arg0(exp))]
local btype = types[hash(arg1(exp))]
local isnumA = atom(atype) == 'int' or atom(atype) == 'getal'
local isnumB = atom(btype) == 'int' or atom(btype) == 'getal'
local isfuncA = fn(atype) == '→' or atom(atype) == 'functie'
local isfuncB = fn(btype) == '→' or atom(btype) == 'functie'
local islijstA = atom(arg0(atype)) == 'nat' or obj(atype) == ','
local islijstB = atom(arg0(btype)) == 'nat' or obj(btype) == ','
local ismatA = atom(arg0(atype)) == 'nat' and atom(arg0(arg1(atype))) == 'nat'
local ismatB = atom(arg0(btype)) == 'nat' and atom(arg0(arg1(btype))) == 'nat'
-- vector
if islijstA and islijstB then exp.f = X'+v'
elseif islijstA and isnumB then exp.f = X'+v1'
elseif islijstB and isnumA then
exp.f = X'+v1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
-- functie
elseif isfuncA and isfuncB then exp.f = X'+f'
elseif isfuncA and isnumB then exp.f = X'+f1'
elseif isfuncB and isnumA then
exp.f = X'+f1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
-- matrix
elseif ismatA and ismatB then exp.f = X'+m'
elseif ismatA and isnumB then exp.f = X'+m1'
elseif isnumA and ismatB then
exp.f = X'+m1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
end
end
if fn(exp) == '-' then
local type = types[hash(arg(exp))]
local isnum = atom(type) == 'int' or atom(type) == 'getal'
local isfunc = fn(type) == '→' or atom(type) == 'functie'
local islijst = atom(arg0(type)) == 'nat' or obj(type) == ','
local ismat = atom(arg0(type)) == 'nat' and atom(arg0(arg1(type))) == 'nat'
--local type = types[hash(arg(exp))]
--local isfunc = fn(type) == '→' or atom(type) == 'functie'
--local islijst = atom(arg0(type)) == 'nat' or obj(type) == ','
--local ismat = atom(arg0(type)) == 'nat' and atom(arg0(arg1(type))) == 'nat'
if islijst then
exp.f = X'-v'
elseif isfunc then
exp.f = X'-f'
else
exp.f = X'-'
end
end
-- (/) → /v | /v1 | /m | /m1 | /f
if fn(exp) == '/' then
local atype = types[hash(arg0(exp))]
local btype = types[hash(arg1(exp))]
local isnumA = atom(atype) == 'int' or atom(atype) == 'getal'
local isnumB = atom(btype) == 'int' or atom(btype) == 'getal'
local isfuncA = fn(atype) == '→' or atom(atype) == 'functie'
local isfuncB = fn(btype) == '→' or atom(btype) == 'functie'
local islijstA = atom(arg0(atype)) == 'nat' or obj(atype) == ','
local islijstB = atom(arg0(btype)) == 'nat' or obj(btype) == ','
local ismatA = atom(arg0(atype)) == 'nat' and atom(arg0(arg1(atype))) == 'nat'
local ismatB = atom(arg0(btype)) == 'nat' and atom(arg0(arg1(btype))) == 'nat'
--print(deparse(atype), deparse(btype))
-- matrix
if ismatA and ismatB then exp.f = X'/m' -- matrix multiplication!!
elseif ismatA and islijstB then exp.f = X'/mv'
elseif islijstA and ismatB then
exp.f = X'/mv'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
elseif ismatA and isnumB then exp.f = X'/m1'
elseif isnumA and ismatB then
exp.f = X'/m1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
-- vector
elseif islijstA and islijstB then exp.f = X'/v' ;
elseif islijstA and isnumB then exp.f = X'/v1'
elseif islijstB and isnumA then
exp.f = X'/v1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
-- functie
elseif isfuncA and isfuncB then exp.f = X'/f'
elseif isfuncA and isnumB then exp.f = X'/f1'
elseif isfuncB and isnumA then
exp.f = X'/f1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
end
end
-- (·) → ·v | ·v1 | ·m | ·mv | ·m1 | ·f
if fn(exp) == '·' then
local atype = types[hash(arg0(exp))]
local btype = types[hash(arg1(exp))]
local isnumA = atom(atype) == 'int' or atom(atype) == 'getal'
local isnumB = atom(btype) == 'int' or atom(btype) == 'getal'
local isfuncA = fn(atype) == '→' or atom(atype) == 'functie'
local isfuncB = fn(btype) == '→' or atom(btype) == 'functie'
local islijstA = atom(arg0(atype)) == 'nat' or obj(atype) == ','
local islijstB = atom(arg0(btype)) == 'nat' or obj(btype) == ','
local ismatA = atom(arg0(atype)) == 'nat' and atom(arg0(arg1(atype))) == 'nat'
local ismatB = atom(arg0(btype)) == 'nat' and atom(arg0(arg1(btype))) == 'nat'
--print(deparse(atype), deparse(btype))
-- matrix
if ismatA and ismatB then exp.f = X'·m' -- matrix multiplication!!
elseif ismatA and islijstB then exp.f = X'·mv'
elseif islijstA and ismatB then
exp.f = X'·mv'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
elseif ismatA and isnumB then exp.f = X'·m1'
elseif isnumA and ismatB then
exp.f = X'·m1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
-- vector
elseif islijstA and islijstB then exp.f = X'·v'
elseif islijstA and isnumB then exp.f = X'·v1'
elseif islijstB and isnumA then
exp.f = X'·v1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
-- functie
elseif isfuncA and isfuncB then exp.f = X'·f'
elseif isfuncA and isnumB then exp.f = X'·f1'
elseif isfuncB and isnumA then
exp.f = X'·f1'
arg(exp)[1], arg(exp)[2] = arg(exp)[2], arg(exp)[1]
end
end
end
return asb
end