Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary Variables in forall scope #5084

Closed
SunandanAdhikary opened this issue Mar 8, 2021 · 4 comments
Closed

Temporary Variables in forall scope #5084

SunandanAdhikary opened this issue Mar 8, 2021 · 4 comments

Comments

@SunandanAdhikary
Copy link

SunandanAdhikary commented Mar 8, 2021

I am stuck with a forall statement. Found this answer useful. So just wanted to know, is there any way to define a temporary variable to use in the forall scope like we do in z3 using let keyword? Because if there are lots of variables with long expressions, it is difficult to parse for z3py it seems!

Yeah, I don't think there's anything like ((y Int)) in the Python API (when creating your forall); this is because Python (not Z3!) needs to resolve the symbols you want to use inside of your quantification. This is 100% a parse-time thing and not a Z3 limitation.

Your example is something like:

from z3 import *

s = Solver()

z = Int("z")
y = Int("y")

s.add(ForAll([y], Implies(And(y > -5, y < 5), y + z > 10)))

print(s.sexpr())

Originally posted by @andrewvaughanj in #5044 (comment)

@SunandanAdhikary SunandanAdhikary changed the title So just wanted to know, is there any way to define a temporary variable to use in the forall scope like we do in z3 using let keyword? Because if there are lots of variables with long expressions, it is difficult to parse for z3py it seems! Temporary Variables in forall scope Mar 8, 2021
@aytey
Copy link
Contributor

aytey commented Mar 8, 2021

You don't have to write the constraint for ForAll "inline"; you can totally do something like:

from z3 import *


def my_fun(y, z):
    return Implies(And(y > -5, y < 5), y + z > 10)


s = Solver()

z = Int("z")
y = Int("y")

s.add(ForAll([y], my_fun(y, z)))

print(s.sexpr())

I think in Z3 parlance, my_fun is called a "generator expression" -- that is, it a function that generates further Z3 expressions.

@SunandanAdhikary
Copy link
Author

So I can write anything in place of Implies... in the function definition? Like we normally do in python? And reuse that?

@aytey
Copy link
Contributor

aytey commented Mar 8, 2021

Yes, as long as "normally do" returns a (self-contained) Z3 expression.

@SunandanAdhikary
Copy link
Author

Understood. Thank you.

@Z3Prover Z3Prover locked and limited conversation to collaborators Mar 8, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants