Skip to content
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

ask returns True instead of raising inconsistent assumptions exception #25349

Open
TiloRC opened this issue Jul 10, 2023 · 3 comments · May be fixed by #25350
Open

ask returns True instead of raising inconsistent assumptions exception #25349

TiloRC opened this issue Jul 10, 2023 · 3 comments · May be fixed by #25350
Labels
assumptions Wrong Result The output produced by SymPy is mathematically incorrect.

Comments

@TiloRC
Copy link
Contributor

TiloRC commented Jul 10, 2023

from sympy import ask, Q
from sympy.abc import x, y
ask(Q.positive(x), Q.positive(x) & Q.positive(y) & ~Q.positive(y)) # returns True instead of raising exception

The problem happens at lines 471 , 472 (of ask.py):

 # extract the relevant facts from assumptions with respect to args
 local_facts = _extract_all_facts(assump_cnf, args)

here we lose all of the predicate that don't have x as their argument.

Originally, I had thought this error had originated from satask, but it correctly rasies an exception here:

from sympy.assumptions.satask import satask
satask(Q.positive(x), Q.positive(x) & Q.positive(y) & ~Q.positive(y)) # correctly raises inconsistent assumptions exception

However, with a little modification satask has the same bug:

satask(Q.positive(x), Q.positive(x) & Q.positive(y) & Q.zero(y)) # returns True instead of raising exception

The problem is due the get_all_relevant_facts function which only thinks facts relating to the variables in the proposition are relevant. Facts about expressions like y which don't appear in the predicate are ignored.

Edit: here's a modified case where satask correctly raises the exception:

satask(Q.positive(x) & Q.positive(y), Q.positive(x) & Q.positive(y) & Q.zero(y)) # correctly raises exception
@TiloRC TiloRC changed the title ask returns True instead raising inconsistent assumptions exception ask returns True instead of raising inconsistent assumptions exception Jul 10, 2023
@asmeurer asmeurer added the Wrong Result The output produced by SymPy is mathematically incorrect. label Jul 10, 2023
TiloRC added a commit to TiloRC/sympy that referenced this issue Jul 10, 2023
Before this fix, extract_predargs was not extracting arguements
that were only present in the assumptions in some cases.
As a result, satask would fail to raise an inconsistent assumptions
exception in some cases. This is now fixed.

An unfortunate consequence of this fix is that it makes satask
slower. Previously running the tests for the new assumptions
took 21.7 +/- .2 seconds for me. Now they take 26.5 +/- .2
seconds.
@TiloRC
Copy link
Contributor Author

TiloRC commented Jul 11, 2023

@ShubhamKJha, @JSS95, @asmeurer can you shed some light on the purpose of this code from the extract_predargs function? Getting rid of it fixes this bug, but slows down satask a lot. For the purposes of this function, why should arguments from the proposition be treated differently than arguments from the assumptions?

while tmp_keys != set():
tmp = set()
for l in lkeys:
syms = find_symbols(l)
if (syms & req_keys) != set():
tmp |= syms
tmp_keys = tmp - req_keys
req_keys |= tmp_keys
keys |= {l for l in lkeys if find_symbols(l) & req_keys != set()}

@TiloRC
Copy link
Contributor Author

TiloRC commented Jul 11, 2023

My impression of it is that it's a way to reduce the number of clauses given to the sat solver to speed things up? However, as this bug demonstrates that's problematic for some edge cases.

@asmeurer
Copy link
Member

It looks like it was a performance optimization #17144. But we need to make sure that whatever optimizations we make are correct. It's better to have correct but slow code than to have code that is fast but gives wrong answers sometimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assumptions Wrong Result The output produced by SymPy is mathematically incorrect.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants