forked from JamesPane-Joyce/Talos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputParser.py
321 lines (306 loc) · 12 KB
/
outputParser.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
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
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), "../", "DCEC_Library"))
import cleaning
import string
from DCECContainer import DCECContainer
def nextQuant(prev):
this = string.ascii_lowercase
prevquant = prev[0]
prevmult = int(prev[1:])
if prevquant == 'z':
newquant = 'a'
newmult = prevmult+1
else:
newquant = this[this.find(prevquant)+1]
newmult = prevmult
return newquant+str(newmult)
def overwriteQuant(quants,binding,sort,quantMap):
for thing in enumerate(quants):
if thing[1] == quantMap[binding]:
quants[thing[0]-1] = sort
def storeQuant(quants,binding,sort,transformDict,quantMap):
if not "TEMP" in quantMap.keys():
quantMap["TEMP"] = 'a0'
quantifier = nextQuant(quantMap["TEMP"])
while(quantifier in transformDict.keys() or quantifier in transformDict.values()):
quantifier = nextQuant(quantifier)
quantMap[binding] = quantifier
quantMap[quantifier] = binding
quants.append(sort)
quants.append(quantMap[binding])
quantMap["TEMP"] = quantifier
def cleanArg(arg,sorts):
newArg = arg
for sort in sorts:
newArg = newArg.replace(sort,"")
if newArg == arg:
return arg
else:
return newArg[:-1]
def findSubTokens(statement,transformDict,sorts,quants,quantMap):
#Find the sub-level tokens at this level of parsing
temp = statement[1:-1].strip(",")
level = 0
highlevel = ""
sublevel = []
for index in range(0,len(temp)):
if temp[index] == "(":
level += 1
if(level == 1):
sublevel.append([index,cleaning.getMatchingCloseParen(temp,index)+1])
continue
if temp[index] == ')':
level -= 1
continue
if level == 0:
highlevel = highlevel + temp[index]
#These are the function components. One is the function name, the others are its args.
args = highlevel.split(",")
#Find quantifiers
#If there is a singular thing
if args[0] in sorts:
if not args[1] == "":
if not args[1] in quantMap.keys() and not args[1] in transformDict.keys():
storeQuant(quants,args[1],args[0],transformDict,quantMap)
elif not args[1] in transformDict.keys():
overwriteQuant(quants,args[1],args[0],quantMap)
#If there is a funciton
elif args[0] in transformDict.keys():
argSorts = transformDict[args[0]][2]
for sort in range(0,len(argSorts)):
if not args[sort+1] == "" and not args[sort+1] in quantMap.keys() and not args[sort+1] in transformDict.keys():
storeQuant(quants,args[sort+1],argSorts[sort],transformDict,quantMap)
place = 0
#Recurse
for index in range(0,len(args)):
if args[index] == "":
args[index] = findSubTokens(temp[sublevel[place][0]:sublevel[place][1]],transformDict,sorts,quants,quantMap)
place += 1
return args
def makeFuncs(quants,statementlist,sorts,transformDict,quantMap):
#Return atomics
if isinstance(statementlist,str):
returnlist = []
if statementlist in transformDict.keys():
returnlist.append(transformDict[statementlist][0])
elif not statementlist in quantMap.keys():
storeQuant(quants,statementlist,"UNBOUND",transformDict,quantMap)
returnlist.append(quantMap[statementlist])
else:
returnlist.append(quantMap[statementlist])
return returnlist
#If there is a sort, strip it
if statementlist[0] in sorts:
return makeFuncs(quants,statementlist[1],sorts,transformDict,quantMap)
#Otherwise there is a function to strip
newlist = []
for arg in enumerate(statementlist):
if isinstance(arg[1],list):
newlist.append(makeFuncs(quants,statementlist[arg[0]],sorts,transformDict,quantMap))
else:
newlist.append(arg[1])
#Clean the arguments so that they are of the right form
for arg in enumerate(newlist):
if isinstance(arg[1],list):
pass
elif arg[1] in transformDict.keys():
newlist[arg[0]] = transformDict[arg[1]][0]
elif arg[1] in quantMap.keys():
newlist[arg[0]] = quantMap[arg[1]]
else:
storeQuant(quants,arg[1],"UNBOUND",transformDict,quantMap)
newlist[arg[0]] = quantMap[arg[1]]
#return the cleaned list
return newlist
def tokenizeSPASS(partialStatement,sorts,transformDict,quantMap):
#If there are no constraints
if(partialStatement == ""):
return [],[]
#Strip all the unneccesary information
statements = partialStatement.strip(" ").strip("->").strip(" ").split(" ")
for statement in enumerate(statements):
if(statement[1] == "->"):
statements[statement[0]] = "TEMP"
continue
temp = statement[1]
while True:
nextTemp = temp.strip(".").strip(" ").strip("*").strip("+")
if(temp ==nextTemp):
statements[statement[0]] = nextTemp
break
else:
temp = nextTemp
#Conclusions are weird, stupid SPASS
if statements[-1] == "":
statements = statements[:-2]
#Initialize the return lists
quants = []
funcs = []
#Find all the neccesary information
for statement in statements:
#Implies is weird
if(statement == "TEMP"):
funcs.append(["TEMP"])
continue
#Find the functions derived in the proof
temp = cleaning.tuckFunctions(statement)
tokens = findSubTokens(temp,transformDict,sorts,quants,quantMap)
#Funcs have isValid in front
if tokens[0] == "isValid":
if isinstance(tokens[1],str):
if not tokens[1] in transformDict.keys() and not tokens[1] in quantMap.keys():
storeQuant(quants,tokens[1],"Boolean",transformDict,quantMap)
funcs.append(makeFuncs(quants,tokens[1],sorts,transformDict,quantMap))
#Implies is weird, collapse all the implies
returner = []
index = len(funcs)-1
while index >= 0:
if funcs[index-1][0] == "TEMP":
returner.append(["implies",funcs[index-2],funcs[index]])
index -= 3
else:
returner.append(funcs[index])
index -= 1
funcs = returner
return quants,funcs
def tokenToString(token):
returnString = ""
for arg in token:
if isinstance(arg,list):
if len(arg) > 1:
returnString += tokenToString(arg)+" "
else:
returnString += arg[0]+" "
else:
returnString += arg+" "
returnString = returnString.strip(" ")
if(len(token) > 1):
returnString = "("+returnString+")"
return returnString
def remake(tokens,newMap):
for token in range(0,len(tokens)):
if isinstance(tokens[token],str):
if tokens[token] in newMap.keys():
tokens[token] = newMap[tokens[token]]
else:
tokens[token] = remake(tokens[token],newMap)
return tokens
def sortQuants(quants,sorts,transformDict,newMap):
orderedquants = []
newBase = nextQuant("a0")
while newBase in transformDict.keys() or newBase in transformDict.values():
newBase = nextQuant(newBase)
for sort in sorts:
if sort in quants:
for quant in range(0,len(quants)/2):
index = quant*2
if quants[index] == sort:
orderedquants.append(quants[index])
orderedquants.append(quants[index+1])
newMap[quants[index+1]] = newBase
newBase = nextQuant(newBase)
while newBase in transformDict.keys() or newBase in transformDict.values():
newBase = nextQuant(newBase)
return orderedquants
def tokensToString(quants,sorts,constraints,results):
returnString = ""
numEndParens = 0
#If there is nothing, just return nothing
if len(constraints) == 0 and len(results) == 0:
return returnString
#Translate to DCEC-quantifiers, sort the quantifiers
for quant in range(0,len(quants)/2):
returnString += "(forAll ("
numEndParens += 1
returnString += quants[quant*2]+" "
returnString += quants[quant*2+1]+") "
#Translate to DCEC-constraints
place = 0
constraintString = ""
if len(constraints) == 0:
pass
else:
while place < len(constraints)-1:
constraintString += "(and "
adder = tokenToString(constraints[place])
constraintString += adder+" "
place += 1
adder = tokenToString(constraints[-1])
constraintString += adder
for x in range(0,place):
constraintString += ")"
#Translate to DCEC-results
place = 0
resultString = ""
if len(results) == 0:
return resultString
else:
while place < len(results)-1:
resultString += "(and "
adder = tokenToString(results[place])
resultString += adder+","
place += 1
adder = tokenToString(results[-1])
resultString += adder
for x in range(0,place):
resultString += ")"
#Make the implication
if constraintString == "":
returnString += resultString
else:
returnString += "(implies "+constraintString+" "+resultString+")"
#Add end parens
for whatevs in range(0,numEndParens):
returnString += ")"
#Return the completed DCEC statement
return returnString
def toSNotation(SPASSstatement,sorts,transformDict):
#The quantifiers and conclusion are seperated by ||
templist = SPASSstatement.split("||")
if not len(templist) == 2:
return ""
#Create lists of tokens
quants = []
quantMap = {}
quants1,constraints = tokenizeSPASS(templist[0],sorts,transformDict,quantMap)
quants2,results = tokenizeSPASS(templist[1],sorts,transformDict,quantMap)
quants = quants1
#Add quantifiers in a setwise fashion
for quant in range(0,len(quants2)/2):
if not quants2[quant*2+1] in quants:
quants = quants + quants2[quant*2:quant*2+2]
#Sort and remake the quantifiers
newMap = {}
quants = sortQuants(quants,sorts,transformDict,newMap)
quants = remake(quants,newMap)
constraints = remake(constraints,newMap)
results = remake(results,newMap)
#stringify the tokens
returnString = tokensToString(quants,sorts,constraints,results)
return returnString
def parseOutput(outputTree):
outputContainer = DCECContainer()
currentStatement = toSNotation(outputTree.conclusion)
if __name__ == "__main__":
transformDict = {}
transformDict["Test2BooleanAgent"] = ["Test",2,["Boolean","Agent"]]
transformDict["Test1Boolean"] = ["Test",1,["Boolean"]]
transformDict["Action2AgentActionType"] = ["Action",2,["Agent","ActionType"]]
transformDict["a0"] = ["a"]
transformDict["James0"] = ["james"]
outputContainer = DCECContainer()
outputContainer.namespace.addCodeSort("Boolean")
outputContainer.namespace.addCodeSort("Agent")
outputContainer.namespace.addCodeSort("Action")
outputContainer.namespace.addCodeSort("ActionType")
quant = "a0"
for x in range(0,1000):
print quant
quant = nextQuant(quant)
print toSNotation("Agent(skc0) Boolean(U) Boolean(Test2BooleanAgent(Boolean(Test1Boolean(a0)),skc0)) isValid(Test2BooleanAgent(Boolean(Test1Boolean(a0)),skc0)) isValid(U)|| -> isValid(U)* -> isValid(U)*.",outputContainer.namespace.sorts,transformDict)
print toSNotation("|| -> isValid(Agent(James0))*.",outputContainer.namespace.sorts,transformDict)
print toSNotation("Boolean(U) isValid(V) || isValid(Test1Boolean(U)) -> isValid(U)",outputContainer.namespace.sorts,transformDict)
print toSNotation("Boolean(V) isValid(U) Boolean(U) || isValid(Test1Boolean(V)) -> isValid(V)",outputContainer.namespace.sorts,transformDict)
print toSNotation("|| -> .",outputContainer.namespace.sorts,transformDict)
print toSNotation("|| -> Action(Action2AgentActionType(U,V))*.",outputContainer.namespace.sorts,transformDict)
print toSNotation("|| -> Action(Action2AgentActionType(U,V))*.",outputContainer.namespace.sorts,transformDict)