mirror of
https://github.com/Z3Prover/z3
synced 2025-10-07 00:11:55 +00:00
5.8 KiB
5.8 KiB
Summary: UserPropagator Quantifier Instantiation Callback Documentation
Implementation Complete
This implementation addresses the documentation request for the UserPropagator callback for quantifier instantiations feature added in Z3 version 4.15.3.
What Was Delivered
1. Complete Working Examples
Python Example (examples/python/quantifier_instantiation_callback.py
)
- ✅ Basic instantiation control: Demonstrates limiting the number of quantifier instantiations
- ✅ Advanced pattern filtering: Shows filtering based on instantiation patterns
- ✅ Instantiation logging: Provides comprehensive logging and analysis capabilities
- ✅ Error handling: Includes proper exception handling
- ✅ Fully tested: All examples run successfully and produce expected output
C++ Example (examples/c++/quantifier_instantiation_callback.cpp
)
- ✅ Complete C++ API usage: Shows how to use the
Z3_solver_propagate_on_binding
function - ✅ Multiple callback strategies: Demonstrates different filtering approaches
- ✅ Context management: Proper handling of user context data
- ✅ Memory management: Safe handling of callback contexts
C Example (examples/c/quantifier_instantiation_callback.c
)
- ✅ Low-level C API: Direct usage of Z3 C API functions
- ✅ Pattern tracking: Dynamic tracking of instantiation patterns
- ✅ Memory safety: Proper allocation and deallocation
2. Comprehensive Documentation
API Documentation (doc/quantifier_instantiation_callback.md
)
- ✅ Complete API reference: Covers Python, C++, and C APIs
- ✅ Usage patterns: Best practices and common use cases
- ✅ Performance considerations: Guidance on efficiency
- ✅ Troubleshooting: Common issues and solutions
- ✅ Technical details: Callback semantics and behavior
Usage Guide (examples/README_quantifier_callbacks.md
)
- ✅ Step-by-step instructions: How to compile and run examples
- ✅ Prerequisites: Required dependencies and setup
- ✅ Expected output: What users should see when running examples
- ✅ Troubleshooting: Common compilation and runtime issues
3. Validation and Testing
Unit Tests (src/test/test_quantifier_instantiation_callback.py
)
- ✅ 7 comprehensive test cases: Cover all major functionality
- ✅ All tests pass: Validated working implementation
- ✅ Error handling tests: Verify graceful exception handling
- ✅ Edge case coverage: Tests with/without quantifiers, UNSAT formulas, etc.
Key Features Demonstrated
1. Instantiation Control
def on_quantifier_instantiation(self, quantifier, instantiation):
# Allow only first 3 instantiations
return self.count <= 3
2. Pattern-Based Filtering
def filter_by_pattern(self, quantifier, instantiation):
pattern = str(instantiation)
# Allow max 2 instantiations per pattern
return self.pattern_counts[pattern] <= 2
3. Comprehensive Logging
def log_instantiation(self, quantifier, instantiation):
self.log.append({
'quantifier': str(quantifier),
'instantiation': str(instantiation),
'timestamp': len(self.log)
})
return True # Allow all for analysis
Verified Working Examples
Python Output
============================================================
BASIC QUANTIFIER INSTANTIATION CONTROL EXAMPLE
============================================================
Checking satisfiability with instantiation control...
Instantiation #1:
Quantifier: ForAll(x, f(x) >= 0)
Instantiation: f(1) >= 0
-> ALLOWED (#1)
Instantiation #2:
Quantifier: ForAll(x, f(x) >= 0)
Instantiation: f(2) >= 0
-> ALLOWED (#2)
Instantiation #3:
Quantifier: ForAll(x, f(x) >= 0)
Instantiation: f(3) >= 0
-> ALLOWED (#3)
Result: sat
Model: [c = 3, b = 2, a = 1, f = [else -> 0]]
Unit Test Results
Ran 7 tests in 0.024s
OK
Use Cases Addressed
- Performance Optimization: Examples show how to limit expensive instantiations
- Custom Instantiation Strategies: Demonstrate domain-specific filtering
- Debugging and Analysis: Comprehensive logging for understanding solver behavior
- Interactive Solving: Fine-grained control over quantifier instantiation process
API Coverage
- ✅ Python API: Complete
UserPropagateBase
integration withadd_on_binding()
- ✅ C API: Direct usage of
Z3_solver_propagate_on_binding()
- ✅ C++ API: Wrapper around C API with proper type safety
- ✅ Error handling: Graceful handling of callback exceptions
- ✅ Context management: Proper user context passing and handling
File Structure Created
z3/
├── doc/
│ └── quantifier_instantiation_callback.md # Complete API documentation
├── examples/
│ ├── README_quantifier_callbacks.md # Usage guide
│ ├── python/
│ │ └── quantifier_instantiation_callback.py # Python examples
│ ├── c++/
│ │ └── quantifier_instantiation_callback.cpp # C++ examples
│ └── c/
│ └── quantifier_instantiation_callback.c # C examples
└── src/test/
└── test_quantifier_instantiation_callback.py # Unit tests
Impact and Benefits
- Complete Documentation: Users now have comprehensive documentation for this advanced feature
- Working Examples: Copy-paste ready code for immediate use
- Multiple Languages: Coverage of Python, C++, and C APIs
- Validated Implementation: All examples tested and working
- Best Practices: Guidance on performance, error handling, and proper usage
This implementation fully addresses the original request and provides users with everything they need to understand and use the UserPropagator quantifier instantiation callback feature effectively.