Skip to content

Commit 50a3f2b

Browse files
committed
moved superset check
1 parent a12ce53 commit 50a3f2b

2 files changed

Lines changed: 48 additions & 66 deletions

File tree

grape/automaton_generator.py

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -233,76 +233,10 @@ def grammar_from_memory(
233233
added.add((old, ()))
234234
relevant_dfta.refresh_reversed_rules()
235235
n = relevant_dfta.trees_until_size(max_size)
236-
from_enum = __check_is_superset__(memory, relevant_dfta, max_size)
237-
total_programs = sum(
238-
sum(len(memory[state][s]) for state in memory) for s in range(1, max_size + 1)
239-
)
240-
print(
241-
f"obs. equivalence: {total_programs:.3e} pruned: {from_enum:.3e} ({from_enum / total_programs:.2%})"
242-
)
243236
# Delete them now that they have been used
244237
for x in added:
245238
del relevant_dfta.rules[x]
246239
relevant_dfta.refresh_reversed_rules()
247240
# ==================================
248241

249242
return relevant_dfta, n
250-
251-
252-
def __check_is_superset__(
253-
memory: dict[Any, dict[int, list[Program]]], dfta: DFTA[Any, Program], max_size: int
254-
) -> int:
255-
enum = Enumerator(dfta)
256-
gen = enum.enumerate_until_size(max_size + 1)
257-
pbar = tqdm(total=dfta.trees_until_size(max_size), desc="checking")
258-
next(gen)
259-
size = 0
260-
count = 0
261-
while True:
262-
try:
263-
gen.send(True)
264-
count += 1
265-
if count & 15 == 0:
266-
pbar.update(16)
267-
count = 0
268-
except StopIteration:
269-
break
270-
pbar.update(count)
271-
pbar.close()
272-
new_memory_to_size = {}
273-
old_memory_to_size = {}
274-
total = 0
275-
for value in enum.memory.values():
276-
for size, programs in value.items():
277-
if size not in new_memory_to_size:
278-
new_memory_to_size[size] = []
279-
new_memory_to_size[size] += programs
280-
total += len(programs)
281-
for value in memory.values():
282-
for size, programs in value.items():
283-
if size not in old_memory_to_size:
284-
old_memory_to_size[size] = []
285-
old_memory_to_size[size] += programs
286-
287-
sizes = set(new_memory_to_size.keys()) | set(old_memory_to_size.keys())
288-
for size in sizes:
289-
if size not in new_memory_to_size:
290-
print(f"[warning] missing expressions of size: {size}", file=sys.stderr)
291-
print("[warning] stopped check here to avoid blow up", file=sys.stderr)
292-
break
293-
elif size not in old_memory_to_size:
294-
pass
295-
# print("+", new_memory_to_size[size])
296-
else:
297-
# more = set(new_memory_to_size[size]) - set(old_memory_to_size[size])
298-
less = set(old_memory_to_size[size]) - set(new_memory_to_size[size])
299-
# if more:
300-
# print("+", more)
301-
if less:
302-
print(
303-
f"[warning] missing the following of size {size}: {less}",
304-
file=sys.stderr,
305-
)
306-
print("[warning] stopped check here to avoid blow up", file=sys.stderr)
307-
break
308-
return total

tests/cli/test_prune.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,51 @@ def test_incremental_next_size():
142142
direct, tr = prune(dsl, evaluator, manager, max_size=max_size + 1, rtype="int")
143143
assert direct.rules == incremental.rules
144144
assert direct.finals == incremental.finals
145+
146+
147+
def test_is_superset():
148+
evaluator = Evaluator(dsl, inputs, {}, set())
149+
150+
manager = EquivalenceClassManager()
151+
out, tr = prune(dsl, evaluator, manager, max_size=max_size, rtype="int")
152+
tr = "int->int"
153+
base = grammar_by_saturation(dsl, tr)
154+
evaluator = Evaluator(dsl, inputs, {}, set())
155+
ebase = Enumerator(base)
156+
gen = ebase.enumerate_until_size(max_size)
157+
p = next(gen)
158+
should_keep = evaluator.eval(p, tr) is None
159+
try:
160+
while True:
161+
p = gen.send(should_keep)
162+
should_keep = evaluator.eval(p, tr) is None
163+
except StopIteration:
164+
pass
165+
e = Enumerator(specialize(out, tr, dsl))
166+
gen = e.enumerate_until_size(max_size)
167+
p = next(gen)
168+
try:
169+
while True:
170+
gen.send(True)
171+
except StopIteration:
172+
pass
173+
174+
new_memory_to_size = {}
175+
old_memory_to_size = {}
176+
for value in e.memory.values():
177+
for size, programs in value.items():
178+
if size not in new_memory_to_size:
179+
new_memory_to_size[size] = []
180+
new_memory_to_size[size] += programs
181+
for value in ebase.memory.values():
182+
for size, programs in value.items():
183+
if size not in old_memory_to_size:
184+
old_memory_to_size[size] = []
185+
old_memory_to_size[size] += programs
186+
187+
sizes = set(new_memory_to_size.keys()) | set(old_memory_to_size.keys())
188+
for size in sizes:
189+
assert size in new_memory_to_size
190+
assert set(old_memory_to_size.get(size, [])).issubset(
191+
set(new_memory_to_size[size])
192+
)

0 commit comments

Comments
 (0)