Skip to content

Work around Z3 not producing models for some quantified expressions#8703

Open
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:trying-to-fix-z3-with-quantifiers
Open

Work around Z3 not producing models for some quantified expressions#8703
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:trying-to-fix-z3-with-quantifiers

Conversation

@tautschnig
Copy link
Copy Markdown
Collaborator

Until the Z3 bug-fix (see Z3Prover/z3#7743) is available widely work around the problem that Z3's preprocessing introduces by adding extra symbols. This may cause solving to take longer, to be determined empirically.

Fixes: #8679

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@tautschnig tautschnig self-assigned this Jul 31, 2025
@tautschnig tautschnig force-pushed the trying-to-fix-z3-with-quantifiers branch from b77cf1c to e92bda3 Compare August 5, 2025 17:26
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.41%. Comparing base (59d2211) to head (bfa6d2d).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8703      +/-   ##
===========================================
- Coverage    80.41%   80.41%   -0.01%     
===========================================
  Files         1703     1703              
  Lines       188398   188403       +5     
  Branches        73       73              
===========================================
- Hits        151502   151500       -2     
- Misses       36896    36903       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Z3's solve_eqs preprocessor eliminates symbols defined via
(assert (= symbol <quantified-expr>)) by substituting the quantified
expression everywhere. This causes get-value to return quantified
expressions instead of simple boolean values, which CBMC cannot parse.

Using bidirectional implication (assert (=> symbol <expr>)) and
(assert (=> <expr> symbol)) instead of equality prevents solve_eqs
from recognising this as an elimination opportunity, while preserving
the logical equivalence.

See Z3Prover/z3#7743 for the upstream bug report.

Fixes: diffblue#8679

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the trying-to-fix-z3-with-quantifiers branch from e92bda3 to bfa6d2d Compare March 26, 2026 18:35
@tautschnig tautschnig marked this pull request as ready for review March 27, 2026 14:01
Copilot AI review requested due to automatic review settings March 27, 2026 14:01
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Works around a Z3 issue where preprocessing can eliminate Boolean symbols whose definitions contain quantifiers, leading to problematic get-value/model extraction behavior (e.g., quantified expressions appearing in counterexamples).

Changes:

  • Switch quantifier-containing Boolean “definitions” from a single equality assertion to two implication assertions (bidirectional implication) to avoid Z3’s solve_eqs substitution eliminating the symbol.
  • Add explanatory comments documenting why equality assertions are insufficient in this scenario.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1026 to +1035
out << "(assert (=> ";
convert_literal(l);
out << ' ';
convert_expr(prepared_expr);
out << "))\n";
out << "(assert (=> ";
convert_expr(prepared_expr);
out << ' ';
convert_literal(l);
out << "))\n";
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quantified expression is emitted twice (once in each implication), which can significantly increase SMT2 output size and slow down both writing and solving for large quantified terms. Consider using a single assert that binds the quantified term once (e.g., via an SMT-LIB let) and then asserts both implications against that binding to avoid duplicating prepared_expr.

Copilot uses AI. Check for mistakes.
Comment on lines 1017 to 1021
// Z3 refuses get-value when a defined symbol contains a quantifier.
// Using (assert (= ...)) doesn't help because Z3's solve_eqs
// preprocessor eliminates the symbol by substituting the quantified
// expression. Using bidirectional implication instead prevents this.
if(has_quantifier(prepared_expr))
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is intended to address a Z3 model/get-value regression involving quantified terms, but the PR description indicates no regression test is included. Please add or update a regression test (e.g., in regression/cbmc/z3/) that reproduces #8679 on affected Z3 versions and demonstrates the workaround prevents quantified terms from appearing in get-value responses / keeps traces readable.

Copilot uses AI. Check for mistakes.
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.

CBMC cannot understand Z3 generating a quantified expression in a counter-example

2 participants