How to get all the variables that were added to any constraint in a Solver? - z3

I am using Z3py and trying to get the set of all the variables in any constraint in a Solver. I can call Solver.assertions() to get an ASTVector, then loop over this vector and get objects of type BoolRef, for example, but then I'm stuck. How can I recursively iterate over an assertion, a BoolRef instance for example, to get the individual variables?

Thanks Taylor for your answer. I think the second link addresses the question.
In more detail, the python script that Leo added in the previous answer walks an AST, the AstMap ensures that shared sub-expressions are only walked once.

Related

How to test if an equation is in its simplest (most compact) form

I'm building a learning tool where students are required to solve/enter mathematical equations. I'm using maxima to compare the answer of the student with the solution entered by the teacher to determine whether or not the student has solved it correctly. I would like to allow equivalent answers but only if they are in their simplest form. So if the answer is a+(b+1)/2, then a+(1+b)/2, (b+1)/2+a and (1+b)/2+a are also correct, but a+(2b+2)/4 is not.
Is there a way to do this in maxima?
I tried using ratsimp to simplify the expression and then check if it has remained the same, if so it would mean it was already in its simplest form. Unfortunately, if I use this on a+b for instance, it changes the order of the variables to b+a. Is there some way to check if two expressions are identical except for the order of the operations?
For future reference, I found that you can use the following:
args(expression), simp: false
This will return an array of all the arguments, without simplifying, which we then can use to check if this array is the same for two expressions except for the order.

How to recover a valuation from a satifsiable formula, a question about model

I'm using Z3 with the ml interface. I had created a formula
f(x_i)
that is satisfiable, according to the solver
Solver.mk_simple_solver ctxr.
The problem is: I can get a model, but he find me values only for some variables of the formula, and not all (some of my Model.get_const_interp_er end with a type None)
How can it be possible that the model can give me only a part of the x_ir? In my understanding, if the model work for one of the values, it means that the formula was satisfiable (in my case, it is) and so all the values can be given...
I don't understand something..
Thanks for reading me!
You should always post full examples so people can help with actual coding issues; without seeing your actual code, it's impossible to know what might be the actual reason.
Having said that, this sounds very much like the following question: Why Z3Py does not provide all possible solutions So, perhaps the answer given there will help you.
Long story short: Z3 models will only contain values for variables that matter for the model. For anything that is not explicitly assigned, any value will do. There are ways to get "full" models as explained in that answer of course; which I'm sure is also possible from the ML interface.

Are partial valued functions closures?

My understanding is that closures are basically functions (and by that I mean piece of code) using variables that are bound to some values.
The partial valuation of a function on the other and is nothing but a new function obtained by another one binding some of its variables/arguments.
It seems o me that two concepts are basically the same: indeed one could regard (i.e. implement) closures as partial valuations of functions, that use additional arguments for the variables to be bound in the closure, and on the other hand a partial valuation seems to be just the closure of a function in which some of its variables-arguments are bound to values.
Is this line of thought correct? Are these two concepts really the same? And if no, what are the differences between these concepts?
Thanks in advance for any answer.
I wouldn't say they are the same thing. They are two concepts that have the same power, but that does not mean they are the same thing.

Maxima: How to add superscript to a symbolic variable?

How can I add a superscript to a variable, when I try to type it in to the Maxima Computer Algebra System?
So for example, I would like to have variables named U^(AC), U^(DC) where my intention is not to raise the variable to the power of something, but to have it as part of its name.
UPDATE, NEW ANSWER: Code to implement presuperscripts, presubscripts, postsuperscripts, and postsubscripts has been merged into Maxima. It is available now in the current version from Git, and it will be included in the next release of Maxima, which will be Maxima 5.44. See declare_index_properties in the online documentation (via ?).
OLD ANSWER: There isn't a built-in way to achieve that. That said, to some extent you can use A^B as a symbolic variable in some ways, depending on what you are trying to do. For example, given e:X*A^B + Y you can say solve(e, A^B) and it will return [A^B = -Y/X]. If you say more about exactly what you are trying to achieve, I might be able to give more specific advice.
A while ago I wrote some code to enable Maxima to treat indices of variables as subscripts as well as subscripts (as put the indices before as well as after the variable). I will dust off that code and write more about it here.
You can name it like that :
U^"AC"
U^"AC"*2=456; solve(%, U^"AC");
But it is a good idea to 'define' it before with something like :
UAC : U^"AC"; UAC *2=456; solve(%, UAC );

How can I find all uses of a ValueDecl?

I'd like to take clang AST, analyze how a certain variable is used and do some
source-to-source transformation if a specific usage pattern is recognized.
Particularly, I'm looking for patterns like this:
void *h;
h = create_handler(...);
use_handler(h);
destroy_handler(h);
So far, I am able to detect ValueDecl corresponding to void *h. Next step
would be to find all uses of h and see if they are safe and if
create_handler/destroy_handler properly dominate/post-dominate one another.
Unfortunately, I have no idea how to iterate over h's uses, it seems that
there is no such interface in ValueDecl class.
I'd appreciate it if you could you either suggest how I could find all uses of a
variable in AST, or point me to some clang-based tool dealing with a similar problem.
Thank you!
One can match declRefExprs referencing the variable (using AST matchers). After that, ParentMap could be used to traverse AST backward and find recursively AST nodes which use those declRefExprs. Keep in mind that typically ParentMap is constructed not for the whole AST but for a subtree only (passed as a parameter into the constructor).

Resources