Skip to content

Commit 9c1ff0c

Browse files
committed
iff
1 parent 273e74e commit 9c1ff0c

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/kdrag/parsers/microlean.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import kdrag.smt as smt
1414
from lark import Tree
1515
import kdrag as kd
16-
from typing import NamedTuple
16+
from typing import NamedTuple, Optional
1717

1818

1919
# TODO: let syntax
@@ -26,8 +26,9 @@
2626
?match: "match" expr "with" match_case+ -> match_
2727
match_case: "|" pattern ("=>" | "↦") expr -> match_case
2828
?ite: quantifier | "if" expr "then" expr "else" expr -> if
29-
?quantifier: implication | ("forall" | "∀") binders "," expr -> forall_ | \
29+
?quantifier: iff | ("forall" | "∀") binders "," expr -> forall_ | \
3030
("exists" | "∃") binders "," expr -> exists_ | ("fun" | "λ") binders ("=>" | "↦") expr -> fun_ | set
31+
?iff: implication | implication ("<->" | "↔") iff -> iff_
3132
?implication: disjunction | disjunction ("->" | "→") implication -> implies
3233
?disjunction: conjunction | disjunction ("\\/" | "∨" | "||" | "∪" ) conjunction -> or_
3334
?conjunction: comparison | conjunction ("/\\" | "∧" | "&&" | "∩") comparison -> and_
@@ -42,7 +43,7 @@
4243
| additive "+" multiplicative -> add | additive "-" multiplicative -> sub
4344
?multiplicative: unary
4445
| multiplicative "*" unary -> mul | multiplicative "/" unary -> div
45-
?unary : application | "-" unary -> neg
46+
?unary : application | "-" unary -> neg | "!" unary -> not_ | "~~~" unary -> bvnot
4647
?application: atom atom* -> app
4748
?atom: const | num | bool_ | "(" expr ")" | seq | char | string
4849
@@ -254,7 +255,7 @@ def pattern(tree, env: Env, expected_sort: smt.SortRef | None) -> smt.ExprRef:
254255
raise ValueError("Unknown pattern tree", tree)
255256

256257

257-
def expr(tree, env: Env) -> smt.ExprRef:
258+
def expr(tree, env: Env, expected_sort: Optional[smt.SortRef] = None) -> smt.ExprRef:
258259
match tree:
259260
# TODO: obviously this is not well typed.
260261
case Tree("num", [n]):
@@ -289,6 +290,10 @@ def expr(tree, env: Env) -> smt.ExprRef:
289290
return smt.And(expr(left, env), expr(right, env))
290291
case Tree("or_", [left, right]):
291292
return smt.Or(expr(left, env), expr(right, env))
293+
case Tree("iff_", [left, right]):
294+
l = expr(left, env)
295+
r = expr(right, env)
296+
return smt.Eq(l, r)
292297
case Tree("add", [left, right]):
293298
return expr(left, env) + expr(right, env)
294299
case Tree("sub", [left, right]):
@@ -299,6 +304,8 @@ def expr(tree, env: Env) -> smt.ExprRef:
299304
return expr(left, env) / expr(right, env)
300305
case Tree("neg", [e]):
301306
return -expr(e, env)
307+
case Tree("not_", [e]):
308+
return smt.Not(expr(e, env))
302309
case Tree("eq", [left, right]):
303310
return smt.Eq(expr(left, env), expr(right, env))
304311
case Tree("neq", [left, right]):
@@ -416,10 +423,10 @@ def parse(s: str, locals=None, globals=None) -> smt.ExprRef:
416423
Lambda(x, x > 0)
417424
>>> parse("if true && false then 1 + 1 else 0")
418425
If(And(True, False), 2, 0)
419-
>>> parse("'a'").eq(smt.CharVal('a'))
420-
True
421-
>>> parse("\\"hello world\\"").eq(smt.StringVal("hello world"))
422-
True
426+
>>> assert parse("'a'").eq(smt.CharVal('a'))
427+
>>> assert parse("\\"hello world\\"").eq(smt.StringVal("hello world"))
428+
>>> assert parse("!true").eq(smt.Not(smt.BoolVal(True)))
429+
>>> assert parse("true <-> false").eq(smt.Eq(smt.BoolVal(True), smt.BoolVal(False)))
423430
"""
424431
env = Env(locals=locals or {}, globals=globals or {})
425432
return start(parser.parse(s), env)

src/kdrag/tactics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ def exists(self, *ts) -> "ProofState":
11921192
goalctx = self.top_goal()
11931193
ctx, goal = goalctx.ctx, goalctx.goal
11941194
assert isinstance(goal, smt.QuantifierRef) and goal.is_exists()
1195+
# ts = []
11951196
lemma = kd.kernel.forget(ts, goal)
11961197
self.add_lemma(lemma)
11971198
self.goals[-1] = goalctx._replace(ctx=ctx, goal=lemma.thm.arg(0))
@@ -1848,7 +1849,7 @@ def Theorem(
18481849
"""
18491850
if isinstance(goal, str):
18501851
l, g = kd.utils.calling_globals_locals()
1851-
goal1 = microlean.parse(goal, g)
1852+
goal1 = microlean.parse(goal, l, g)
18521853
else:
18531854
goal1 = goal
18541855
assert isinstance(goal1, smt.BoolRef)

0 commit comments

Comments
 (0)