Skip to content

Commit 1303f38

Browse files
committed
Add RecAddDefinition and AddDefinition
This commit adds support for defining functions (recursive or not) through defineFun and defineFunRec from the base API.
1 parent eb7ae9d commit 1303f38

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

cvc5_pythonic_api/cvc5_pythonic.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
* FiniteDomainSort
6565
* Fixedpoint API
6666
* SMT2 file support
67-
* recursive functions
6867
* Not missing, but different
6968
* Options
7069
* as expected
@@ -938,6 +937,44 @@ def FreshFunction(*sig):
938937
return Function(name, *sig)
939938

940939

940+
def RecAddDefinition(func, args, body):
941+
"""Define a new SMT recursive function with the given function declaration.
942+
Replaces constants in `args` with bound variables.
943+
944+
>>> fact = Function('fact', IntSort(), IntSort())
945+
>>> n = Int('n')
946+
>>> RecAddDefinition(fact, n, If(n == 0, 1, n * fact(n - 1)))
947+
>>> solve(Not(fact(5) == 120))
948+
unsat
949+
"""
950+
if is_app(args):
951+
args = [args]
952+
ctx = func.ctx
953+
consts = [a.ast for a in args]
954+
vars_ = [ctx.solver.mkVar(a.sort().ast, str(a)) for a in args]
955+
subbed_body = body.ast.substitute(consts, vars_)
956+
ctx.solver.defineFunRec(func.ast, vars_, subbed_body)
957+
958+
959+
def AddDefinition(name, args, body):
960+
"""Define a new SMT function with the given function declaration.
961+
Replaces constants in `args` with bound variables.
962+
963+
>>> x, y = Ints('x y')
964+
>>> minus = AddDefinition(minus, [x, y], x - y)
965+
>>> solve(Not(minus(10, 5) == 5))
966+
unsat
967+
"""
968+
if is_app(args):
969+
args = [args]
970+
ctx = body.ctx
971+
consts = [a.ast for a in args]
972+
vars_ = [ctx.solver.mkVar(a.sort().ast, str(a)) for a in args]
973+
subbed_body = body.ast.substitute(consts, vars_)
974+
func = ctx.solver.defineFun(name, vars_, subbed_body.getSort(), subbed_body)
975+
return FuncDeclRef(func, ctx)
976+
977+
941978
#########################################
942979
#
943980
# Expressions

0 commit comments

Comments
 (0)