|
64 | 64 | * FiniteDomainSort |
65 | 65 | * Fixedpoint API |
66 | 66 | * SMT2 file support |
67 | | - * recursive functions |
68 | 67 | * Not missing, but different |
69 | 68 | * Options |
70 | 69 | * as expected |
@@ -938,6 +937,44 @@ def FreshFunction(*sig): |
938 | 937 | return Function(name, *sig) |
939 | 938 |
|
940 | 939 |
|
| 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 | + |
941 | 978 | ######################################### |
942 | 979 | # |
943 | 980 | # Expressions |
|
0 commit comments