-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframework.py
228 lines (210 loc) · 5.99 KB
/
framework.py
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
from grammar import *
from substitution import *
VarMapInternal = dict()
InverseVarMap = dict()
def VarMap(scope, valList):
global InverseVarMap
global VarMapInternal
key = (tuple(scope), tuple(valList))
if key in VarMapInternal:
return VarMapInternal[key]
newX = Val(VAR, "X" + str(len(InverseVarMap)))
VarMapInternal[key] = newX
InverseVarMap[newX] = key
return newX
# Use this one with some modification if the same loop requests an iterator more than once
IterMapInternal = dict()
def IterMap(scope, numList):
global IterMapInternal
key = (tuple(scope), tuple(numList))
if key in IterMapInternal:
return IterMapInternal[key]
itersAssigned = len(IterMapInternal)
newI = Val(ITER, "I" + str(itersAssigned))
IterMapInternal[key] = newI
return newI
def setOfPairsToLists(s):
a = []
b = []
for x in s:
a.append(x[0])
b.append(x[1])
return (b, a)
def flatten(l):
return [val for sublist in l for val in sublist]
def InferIdentity(ios):
inputs, outputs = setOfPairsToLists(ios)
inputs = map(lambda x: x.v, inputs)
outputs = map(lambda x: x.v, outputs)
if inputs == outputs:
return lambda x: x
return None
def InferConst(ios):
_, outputs = setOfPairsToLists(ios)
outputs = map(lambda x: x.v, outputs)
if len(set(outputs)) == 1:
return lambda x: outputs[0]
def InferDict(ios):
inputs, outputs = setOfPairsToLists(ios)
inputs = map(lambda x: x.v, inputs)
outputs = map(lambda x: x.v, outputs)
d = dict()
for i in range(len(inputs)):
if inputs[i] in d:
if d[inputs[i]] != outputs[i]:
return None
d[inputs[i]] = outputs[i]
return lambda x : d[x]
def Scope(X):
if not X in InverseVarMap:
raise Exception('Scope', 'Variable not mapped to scope')
return InverseVarMap[X][0]
def InferProgram(inputList, outputList):
tau1 = asAtomic(InferTreeExp(frozenset(), inputList))
tau2 = asAtomic(InferTreeExp(frozenset(), outputList))
#tau1.printTree()
#tau2.printTree()
for x in Var(tau2) - Var(tau1):
found = False
i = 0
for t in [InferIdentity, InferConst, InferDict]:
i = i + 1
if found:
break
for y in [y for y in Var(tau1) if Scope(y) == Scope(x)]:
f = t(GetLiterals(x, y))
if f is not None:
found = True
tau2 = tau2.replace(x, Val(FEXP, y.v, f))
#print x.v, "is", i, y.v
break
if not found:
raise Exception('InferProgram', 'Could not infer literal functions for ' + x.v)
subsetCond = not Var(tau2).issubset(Var(tau1))
otherCond = not Iter(tau2).issubset(Iter(tau1))
if subsetCond or otherCond:
raise Exception('InferProgram', 'Not subset')
return Program(tau1, tau2)
def InferTreeExp(s, treeList):
allEmpty = True
for x in treeList:
if asAtomic(x)._type != EMPTY:
allEmpty = False
break
if allEmpty:
return EmptyTree()
candidates = flatten([list(Root(x)) for x in treeList])
candidates = list(frozenset(candidates))
filtered = []
for x in candidates:
works = False
for t in treeList:
if FirstRoot(t, x):
works = True
break
if works:
filtered.append(x)
e = None
for x in filtered:
works = True
for t in treeList:
if x in Root(t) and not FirstRoot(t, x):
works = False
break
if works:
e = x
break
if e is None:
raise Exception('InferTreeExp', 'No e')
rList = []
tPrimeList = []
for t in treeList:
tempList = asList(t)
tempRList = []
while tempList.list:
r = tempList.list[0]
if Root(r) == frozenset([e]):
tempRList.append(r)
tempList.list.pop(0)
else:
break
tempTPrime = ListTree(tempList.list)
if e in Root(tempTPrime):
raise Exception('InferTreeExp', 'Weird thing')
rList.append(tempRList)
tPrimeList.append(tempTPrime)
tau = InferTreeExp(s, tPrimeList)
if 0 in [len(r) for r in rList]:
raise Exception('InferTreeExp', '0 length iteration somewhere')
M = len(rList[0])
if [M]*len(rList) == [len(x) for x in rList]:
rhoList = []
for j in range(M):
rhoj = InferRootExp(s, [x[j] for x in rList])
rhoList.append(rhoj)
rhoList.append(tau)
return removeEmpties(ListTree(rhoList))
I = IterMap(tuple(s), tuple([len(x) for x in rList]))
rho = InferRootExp(s | frozenset([I]), flatten(rList))
loopTree = LoopTree(I, rho)
return removeEmpties(ListTree(loopTree, tau))
def InferRootExp(s, rList):
rList = [asAtomic(x) for x in rList]
mList = []
tList = []
e = rList[0].tag
for r in rList:
if r._type != ROOT:
raise Exception('InferRootExp', 'NonRoot in arguments')
if r.tag != e:
raise Exception('InferRootExp', 'Root with different tag')
mList.append(r.map)
tList.append(r.children)
phi = InferAttMap(s, mList)
tau = InferTreeExp(s, tList)
return RootTree(e, phi, tau)
def InferAttMap(s, mlist):
domain = mlist[0].keys()
for x in mlist:
if x.keys() != domain:
raise Exception('InferAttMap', 'Domains mismatch')
phi = dict()
for a in domain:
v = mlist[0][a]
allSame = True
for x in mlist:
if x[a].v != v.v:
allSame = False
break
if allSame:
phi[a] = v
else:
phi[a] = VarMap(s, [y[a] for y in mlist])
return phi
def GetLiterals(x1, x2):
if x1 not in InverseVarMap or x2 not in InverseVarMap:
raise Exception('Get Literals', 'Unknown variables')
preimage1 = InverseVarMap[x1]
preimage2 = InverseVarMap[x2]
if len(preimage1[1]) != len(preimage2[1]):
raise Exception('Get Literals', 'Lengths not same')
if preimage1[0] != preimage2[0]:
raise Exception('Get Literals', 'Scope different')
R = frozenset()
for i in range(len(preimage1[1])):
if preimage1[1][i]._type == VAR and preimage2[1][i]._type == VAR:
R = R | GetLiterals(preimage1[1][i], preimage2[1][i])
elif preimage1[1][i]._type == LIT and preimage2[1][i]._type == LIT:
R = R | frozenset([(preimage1[1][i], preimage2[1][i])])
else:
raise Exception('Get Literals', 'Apparent mismatch of literal and variable')
return R
def RunProgram(P, t):
sigma = MatchTree(P.input, t)
tPrime = ApplyTree(P.output, sigma)
if len(Var(tPrime)) == 0 and len(Iter(tPrime)) == 0:
return tPrime
raise Exception('RunProgram', 'Output not concrete')
def main(inputList, outputList, newInput):
P = InferProgram(inputList, outputList)
return RunProgram(P, newInput)