Skip to content

Commit 649a72f

Browse files
committed
cegis
1 parent 7d6a85d commit 649a72f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Explorations in Synthesis
3+
"""
4+
5+
import kdrag.smt as smt
6+
import kdrag as kd
7+
from typing import Optional
8+
9+
10+
def cegis_simple(spec: smt.ExprRef) -> Optional[list[smt.ExprRef]]:
11+
"""
12+
Given formula of the form exists ys, forall xs, P(xs,ys), attempt to find witness for ys.
13+
14+
>>> x = smt.Const("x", smt.IntSort())
15+
>>> y = smt.Const("y", smt.IntSort())
16+
>>> assert cegis_simple(smt.Exists([y], smt.ForAll([x], x + y > x)))[0].as_long() > 0
17+
>>> z = smt.Const("z", smt.IntSort())
18+
>>> assert cegis_simple(smt.Exists([y], smt.ForAll([x], smt.And(y <= 0, x + y > x)))) is None
19+
"""
20+
# smt.Exists([y], smt.ForAll([x], x + y > 0)) This won't solve. There isn't a solution.
21+
# receive formula of the form exists y, forall x, P(x,y)
22+
assert isinstance(spec, kd.smt.QuantifierRef) and spec.is_exists()
23+
evs, body = kd.utils.open_binder(spec)
24+
assert isinstance(body, kd.smt.QuantifierRef) and body.is_forall()
25+
avs, body = kd.utils.open_binder(body)
26+
synth = smt.Solver()
27+
synth.add(
28+
smt.FreshConst(e.sort()) == e for e in evs
29+
) # seed solver to give e values
30+
while True:
31+
res = synth.check() # find candidate e values
32+
if res == smt.unsat:
33+
return None
34+
elif res == smt.sat:
35+
m = synth.model()
36+
# verify
37+
verif = smt.Solver()
38+
verif.add([smt.Eq(e, m.eval(e)) for e in evs]) # or smt.substitute
39+
verif.add(smt.Not(body)) # seek counterexample
40+
res2 = verif.check()
41+
if res2 == smt.unsat: # success!
42+
return [m.eval(e) for e in evs]
43+
elif res2 == smt.sat:
44+
m2 = verif.model()
45+
# add counterexample to synthesizer
46+
synth.add(smt.substitute(body, [(a, m2.eval(a)) for a in avs]))
47+
else:
48+
raise Exception("Unknown result from solver", res)
49+
50+
51+
"""
52+
Top down
53+
Bottom up / Contextual compression
54+
Use hypothesis
55+
Term enumeration
56+
Houdini
57+
Spacer / CHC
58+
Fitting
59+
LLM
60+
Hierasynth
61+
Portfolio
62+
"""

0 commit comments

Comments
 (0)