Skip to content

Commit f5448de

Browse files
committed
added Eq workaround to __eq__ flipping
1 parent 410b9b2 commit f5448de

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

kdrag/kernel.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,16 @@ def define(name: str, args: list[smt.ExprRef], body: smt.ExprRef) -> smt.FuncDec
148148
def_ax = axiom(
149149
smt.ForAll(
150150
args + vs,
151-
f(*args)[tuple(vs)] == smt.substitute_vars(body.body(), *reversed(vs)),
151+
smt.Eq(
152+
f(*args)[tuple(vs)], smt.substitute_vars(body.body(), *reversed(vs))
153+
),
152154
),
153155
by="definition",
154156
)
155157
elif len(args) == 0:
156-
def_ax = axiom(f() == body, by="definition")
158+
def_ax = axiom(smt.Eq(f(), body), by="definition")
157159
else:
158-
def_ax = axiom(smt.ForAll(args, f(*args) == body), by="definition")
160+
def_ax = axiom(smt.ForAll(args, smt.Eq(f(*args), body)), by="definition")
159161
# assert f not in __sig or __sig[f].eq( def_ax.thm) # Check for redefinitions. This is kind of painful. Hmm.
160162
# Soft warning is more pleasant.
161163
defn = Defn(name, args, body, def_ax)
@@ -196,7 +198,7 @@ def consider(x: smt.ExprRef) -> Proof:
196198
Axiom schema. We may give a fresh name to any constant. An "anonymous" form of define.
197199
Pointing out the interesting terms is sometimes the essence of a proof.
198200
"""
199-
return axiom(smt.FreshConst(x.sort(), prefix="consider") == x)
201+
return axiom(smt.Eq(smt.FreshConst(x.sort(), prefix="consider"), x))
200202

201203

202204
def instan(ts: list[smt.ExprRef], pf: Proof) -> Proof:
@@ -297,4 +299,4 @@ def beta_conv(lam: smt.QuantifierRef, *args) -> Proof:
297299
"""
298300
assert len(args) == lam.num_vars()
299301
assert smt.is_quantifier(lam) and lam.is_lambda()
300-
return axiom(lam[args] == smt.substitute_vars(lam.body(), *reversed(args)))
302+
return axiom(smt.Eq(lam[args], smt.substitute_vars(lam.body(), *reversed(args))))

kdrag/smt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ def _qsort(self):
7272
)
7373

7474
config.solver = Solver
75+
76+
77+
def Eq(x, y):
78+
"""Python __eq__ resolution rules flips the order if y is a subclass of x.
79+
This function corrects that."""
80+
e = x == y
81+
if type(x) is not type(y) and issubclass(type(y), type(x)):
82+
return e.decl()(e.arg(1), e.arg(0))
83+
else:
84+
return e

tests/test_kernel.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ def test_Lemma():
209209
even = kd.define("even", [x], smt.Exists([y], x == 2 * y))
210210
odd = kd.define("odd", [x], smt.Exists([y], x == 2 * y + 1))
211211

212-
evdef2 = kd.lemma(
212+
evdef2 = kd.kernel.lemma(
213213
smt.ForAll([x], even(x) == smt.Exists([y], x == 2 * y)), by=[even.defn]
214214
)
215215
l = kd.Lemma(kd.QForAll([x], even(x), even(x + 2)))
216216
x1 = l.intros()
217217
l.intros()
218-
l.apply(even.defn, rev=True)
219-
l.rewrite(even.defn, at=0, rev=True)
218+
l.apply(even.defn)
219+
l.rewrite(even.defn, at=0)
220220
y1 = l.einstan(0)
221221
l.exists(y1 + 1)
222222
l.auto()
@@ -229,8 +229,8 @@ def test_Lemma():
229229
[
230230
x1 := l.intros(),
231231
l.intros(),
232-
l.apply(even.defn, rev=True),
233-
l.rewrite(even.defn, at=0, rev=True),
232+
l.apply(even.defn),
233+
l.rewrite(even.defn, at=0),
234234
y1 := l.einstan(0),
235235
l.exists(y1 + 1),
236236
l.auto(),
@@ -264,6 +264,13 @@ def test_Lemma():
264264
l.qed()
265265

266266

267+
def test_eq():
268+
x, y = smt.Ints("x y")
269+
assert smt.Eq(smt.IntVal(2) + smt.IntVal(3), smt.IntVal(5)).arg(1).eq(smt.IntVal(5))
270+
assert smt.Eq((x >= 14), smt.Exists([y], x == 2 * y)).arg(0).eq(x >= 14)
271+
assert smt.Eq((x >= 14), x >= 13).arg(0).eq(x >= 14)
272+
273+
267274
def test_pred():
268275
Even = kd.Record(
269276
"Even", ("val", kd.Z), ("div2", kd.Z), pred=lambda x: 2 * x.div2 == x.val
@@ -292,7 +299,7 @@ def test_beta():
292299
)
293300
t = smt.Lambda([x], x)
294301
assert kd.kernel.beta_conv(t, smt.IntVal(42)).thm.eq(
295-
t[smt.IntVal(42)] == smt.IntVal(42)
302+
smt.Eq(t[smt.IntVal(42)], smt.IntVal(42))
296303
)
297304

298305

tests/test_solver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ def test_tptp():
292292
)
293293

294294

295+
"""
296+
TODO: Not installing aprove in CI?
295297
def test_aprove():
296298
plus = smt.Function("plus", smt.IntSort(), smt.IntSort(), smt.IntSort())
297299
x, y, z = smt.Ints("x y z")
@@ -300,3 +302,4 @@ def test_aprove():
300302
kd.solvers.aprove.run_aprove(
301303
[x, y], [plus(x, zero) == x, plus(x, succ(y)) == succ(plus(x, y))]
302304
)
305+
"""

0 commit comments

Comments
 (0)