-
Notifications
You must be signed in to change notification settings - Fork 1
/
synthesizer.py
94 lines (75 loc) · 2.99 KB
/
synthesizer.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
import itertools
import os
from collections import namedtuple
from state import State
from linetuple import LineTuple
beamState = namedtuple("beamState", "lineInst, backptr, score")
def scoreInstruction(state, target):
# Linear combination of
return []
def enumerateInstruction(state, instructions, lastLine, target):
lineInsts = []
if state.mem is not None:
validMemIndex = []
for i in range(len(state.mem)):
if state.mem[i] is not None:
validMemIndex.append(i)
lineInsts.append(LineTuple("outbox", state, loc=i))
rawLineInsts = list(itertools.product(instructions, validMemIndex, validMemIndex, range(len(state.mem))))
for line in rawLineInsts:
lineInsts.append(LineTuple(line[0], state, line[1], line[2], line[3]))
stateTuples = {"outbox": [], "arith": []}
for inst in lineInsts:
if inst.operator == "outbox":
stateTuples["outbox"].append(beamState(inst, lastLine, scoreInstruction(inst, target)))
else:
stateTuples["arith"].append(beamState(inst, lastLine, scoreInstruction(inst, target)))
return stateTuples
def enumerateNextInstruction(beamstate, instructions, target):
line = beamstate.lineInst
state = line.state
return enumerateInstruction(state, instructions, beamstate, target)
def pruneStack(stack, target):
# outbox
for statetuple in stack["outbox"][:]:
line = statetuple.lineInst
state = line.state
output = state.output
commonPrefix = os.path.commonprefix([line.state.output, target])
if len(commonPrefix) < len(output) or len(output) > len(target):
stack["outbox"].remove(statetuple)
# arithmetic
def printStack(stack):
for state in stack["outbox"]:
print('-', state)
for state in stack["arith"]:
print('-', state)
print('\n')
def beam(beginst, instSet, target, lineLimit, pruneLimit):
# Key: current
stacks = [{"outbox": [], "arith": []} for _ in range(lineLimit + 1)]
enumInst = enumerateInstruction(beginst, instSet, None, target)
stacks[0]["outbox"] = enumInst["outbox"]
stacks[0]["arith"] = enumInst["arith"]
print("stack 0:")
pruneStack(stacks[0], target)
printStack(stacks[0])
for i in range(lineLimit):
i = i + 1
print("stack " + str(i) + " :")
for state in stacks[i - 1]["outbox"] + stacks[i - 1]["arith"]:
enumInst = enumerateNextInstruction(state, instSet, target)
stacks[i]["outbox"] += enumInst["outbox"]
stacks[i]["arith"] += enumInst["arith"]
pruneStack(stacks[i], target)
printStack(stacks[i])
return stacks
if __name__ == '__main__':
st = State([2, None, 3], [])
# instSet = [instructions.add, instructions.sub, instructions.outbox]
instSet = ["add", "sub"]
result = beam(st, instSet, [2, 5], 2, 2)
# print result
# for entry in result:
# print entry
# print enumerateInsts(st, instSet, 2)