Skip to content

fix(normalize): Add Xor simplification to simplify_boolean#906

Closed
aryanjsingh wants to merge 1 commit intoCPMpy:masterfrom
aryanjsingh:fix/issue-747-decompose-before-simplifyboolean-for-xor
Closed

fix(normalize): Add Xor simplification to simplify_boolean#906
aryanjsingh wants to merge 1 commit intoCPMpy:masterfrom
aryanjsingh:fix/issue-747-decompose-before-simplifyboolean-for-xor

Conversation

@aryanjsingh
Copy link
Copy Markdown

Summary

This PR adds support for simplifying Xor expressions within the simplify_boolean transformation. This prevents premature decomposition of Xor with constant values, which could lead to errors in later transformations like linearize as reported in the issue.

Changes

  • cpmpy/transformations/normalize.py: Added simplification logic for the 'xor' operator in simplify_boolean transformation. This is done by adding a new case in the visit_operator method of the SimplifyVisitor class. The logic correctly handles constant BoolVal arguments, simplifying the expression before it can be decomposed incorrectly by later transformations.
  • tests/test_trans_simplify.py: Added a new test method test_xor_simplify to verify the new simplification logic for the Xor operator. This test includes the specific case from the issue, as well as other edge cases to ensure correctness.

Related Issue

Closes #747

@aryanjsingh aryanjsingh closed this Apr 4, 2026
@tias
Copy link
Copy Markdown
Collaborator

tias commented Apr 4, 2026

Hi aryan,

Thanks for submitting a PR.

I'm not sure why you closed it?

It does look like you are introducing a whole lot of code. I've looked a bit closer into the bug, and there are 2 things:

  1. Xor decomp can create a reified Const + Const == 1 constraint
  2. linearize does not handle Const + Const == 1 well

The 2nd one is the less obvious one that needs attention, but even if it would be fixed, the 1st would still be a needless waste of effort.

So instead of adding xor to simplification, perhaps we should add simplification before the xor decomposition...

@tias tias mentioned this pull request Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decompose before simplify_boolean for Xor

2 participants