Overview
TheCVCGenerator class parses first-order logic formulas and generates CVC5-compatible SMT-LIB format files. It handles tokenization, operator precedence, predicate arity checking, and sort unification to ensure valid SMT solver input.
Class Definition
Parameters
First-order logic formula to convert to CVC5 format. Supports standard FOL syntax with:
- Quantifiers:
exists,forall - Operators:
and,or,not,->,<->,=>,<=> - Predicates: Function notation like
P(x, y) - Variables: Single lowercase letters or words
Instance Attributes
The input first-order logic formula
Processed tokens representing the parsed formula structure
Methods
tokenize
list - List of processed tokens
Processing Steps:
- Split formula using regex on operators and punctuation
- Remove empty tokens
- Normalize operators (
->→=>,<->→=) - Process tokens recursively to handle nested structures
process_tokens
tokens(list): Raw tokens to process
list - Processed tokens with Operator and Predicate objects
Handles:
- Quantifier identification and variable binding
- Predicate parsing with argument extraction
- Operator object creation
generatePrefixFormula
tokens(list): Infix tokens to convert
str - Prefix notation formula with full parenthesization
Algorithm:
- Reverse the infix expression
- Convert to postfix using modified Shunting Yard algorithm
- Reverse postfix to get prefix
- Add parentheses for all operators
infixToPostfix
infix(list): Reversed infix tokens
list - Postfix token list
Operator Precedence:
not: 5 (highest)exists,forall: 4and: 3or: 2=>,=: 1 (lowest)
generateCVCScript
Enable finite model finding option in CVC5
str - Complete SMT-LIB script ready for CVC5
Generated Script Structure:
- Logic and options (
set-logic ALL,set-option :produce-models true) - Sort declarations (
BoundSet,UnboundSet) - Variable declarations
- Predicate function declarations with arities
- Assert statement with negated formula
- Check-sat and get-model commands
Helper Classes
Sort
sort(str): Sort name ("BoundSet","UnboundSet", or"Bool")
getSort(): Returns the sort namesetSort(sort): Sets the sort name
Operator
operator(str): Operator stringquantifier(bool): True if operator isexistsorforallarity(int): Number of operands (1 for unary, 2 for binary)priority(int): Operator precedence valuequantified_variable(str): Variable bound by quantifier (if applicable)
getOperatorArity(): Returns operator aritygetPriority(): Returns operator precedencepriority_values(op): Static method returning precedence for operator string
Predicate
name(str): Predicate nameterms(list): List of argument terms (can be nested formulas)prefix_form(list): Prefix notation of each termsort(list[Sort]): Sort of each argument
set_terms(terms): Parse and set predicate termsfind_sort(): Determine sorts for all termsunify_sort(): Ensure consistent sorts across predicate usesunify(sort1, sort2): Unify two sort instances
Global State
The module maintains global state during parsing:Sort Unification
The generator automatically unifies sorts to ensure consistent predicate arities:- Bound Variables: Variables under quantifiers get
BoundSetsort - Free Variables: Assigned
UnboundSetor unified based on usage - Predicate Arguments: Must have consistent sorts across all uses
- Nested Formulas: Arguments that are formulas get
Boolsort
Helper Functions
isOperator
op: Token to check
bool - True if token is an operator
Recognized Operators:
not,and,or->,<->,=>,<=>,=exists <var>,forall <var>
Command-Line Usage
Integration Example
Error Handling
The generator raises exceptions for:The generator automatically normalizes operator syntax:
->becomes=><->and<=>become=ForAllandThereExistscan be used instead offorallandexists
See Also
- NL2FOL - Generate FOL formulas from natural language
- SMTResults - Interpret solver output
- Helper Functions - String processing utilities
