Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * Regular expression solver for the theory of strings. 11 : : */ 12 : : 13 : : #include "cvc5_private.h" 14 : : 15 : : #ifndef CVC5__THEORY__STRINGS__REGEXP_SOLVER_H 16 : : #define CVC5__THEORY__STRINGS__REGEXP_SOLVER_H 17 : : 18 : : #include <map> 19 : : 20 : : #include "context/cdhashset.h" 21 : : #include "context/cdlist.h" 22 : : #include "context/context.h" 23 : : #include "expr/node.h" 24 : : #include "smt/env_obj.h" 25 : : #include "theory/strings/extf_solver.h" 26 : : #include "theory/strings/inference_manager.h" 27 : : #include "theory/strings/regexp_operation.h" 28 : : #include "theory/strings/sequences_stats.h" 29 : : #include "theory/strings/skolem_cache.h" 30 : : #include "theory/strings/solver_state.h" 31 : : #include "theory/strings/term_registry.h" 32 : : #include "util/string.h" 33 : : 34 : : namespace cvc5::internal { 35 : : namespace theory { 36 : : namespace strings { 37 : : 38 : : class RegExpSolver : protected EnvObj 39 : : { 40 : : typedef context::CDList<Node> NodeList; 41 : : typedef context::CDHashMap<Node, bool> NodeBoolMap; 42 : : typedef context::CDHashMap<Node, int> NodeIntMap; 43 : : typedef context::CDHashMap<Node, unsigned> NodeUIntMap; 44 : : typedef context::CDHashMap<Node, Node> NodeNodeMap; 45 : : typedef context::CDHashSet<Node> NodeSet; 46 : : 47 : : public: 48 : : RegExpSolver(Env& env, 49 : : SolverState& s, 50 : : InferenceManager& im, 51 : : TermRegistry& tr, 52 : : CoreSolver& cs, 53 : : ExtfSolver& es, 54 : : SequencesStatistics& stats); 55 : 50707 : ~RegExpSolver() {} 56 : : 57 : : /** check regular expression memberships 58 : : * 59 : : * This checks the satisfiability of all regular expression memberships 60 : : * of the form (not) s in R. We use various heuristic techniques based on 61 : : * unrolling, combined with techniques from Liang et al, "A Decision Procedure 62 : : * for Regular Membership and Length Constraints over Unbounded Strings", 63 : : * FroCoS 2015. 64 : : */ 65 : : void checkMemberships(Theory::Effort e); 66 : : /** 67 : : * Check regular expression memberships eagerly, before running the CAV 14 68 : : * procedure for word equations. Adds lemmas based on our strategy involving 69 : : * reductions or simplifications. 70 : : */ 71 : : void checkMembershipsEager(); 72 : : 73 : : private: 74 : : /** compute asserted memberships, store in d_assertedMems */ 75 : : void computeAssertedMemberships(); 76 : : /** Compute active extended terms of kind k, grouped by representative. */ 77 : : std::map<Node, std::vector<Node>> computeAssertions(Kind k) const; 78 : : /** 79 : : * Check inclusions, 80 : : * Assumes d_assertedMems has been computed. 81 : : */ 82 : : void checkInclusions(); 83 : : /** 84 : : * Check evaluations, which applies substitutions for normal forms to 85 : : * regular expression memberships and evaluates them, and also calls 86 : : * other methods (e.g. partial derivative computations) for the purposes 87 : : * of discovering conflictx. 88 : : * Assumes d_assertedMems has been computed. 89 : : */ 90 : : void checkEvaluations(); 91 : : /** 92 : : * Check unfold, which unfolds regular expression memberships based on the 93 : : * effort level. 94 : : * Assumes d_assertedMems has been computed. 95 : : */ 96 : : void checkUnfold(Theory::Effort effort); 97 : : /** 98 : : * Check memberships in equivalence class for regular expression 99 : : * inclusion. 100 : : * 101 : : * This method returns false if it discovered a conflict for this set of 102 : : * assertions, and true otherwise. It discovers a conflict e.g. if mems 103 : : * contains str.in.re(xi, Ri) and ~str.in.re(xj, Rj) and Rj includes Ri. 104 : : * 105 : : * @param mems Vector of memberships of the form: (~)str.in.re(x1, R1) 106 : : * ... (~)str.in.re(xn, Rn) where x1 = ... = xn in the 107 : : * current context. The function removes elements from this 108 : : * vector that were marked as reduced. 109 : : * @return False if a conflict was detected, true otherwise 110 : : */ 111 : : bool checkEqcInclusion(std::vector<Node>& mems); 112 : : 113 : : /** 114 : : * Check memberships for equivalence class. 115 : : * The vector mems is a vector of memberships of the form: 116 : : * (~) (x1 in R1 ) ... (~) (xn in Rn) 117 : : * where x1 = ... = xn in the current context. 118 : : * 119 : : * This method may add lemmas or conflicts via the inference manager. 120 : : * 121 : : * This method returns false if it discovered a conflict for this set of 122 : : * assertions, and true otherwise. It discovers a conflict e.g. if mems 123 : : * contains (xi in Ri) and (xj in Rj) and intersect(xi,xj) is empty. 124 : : */ 125 : : bool checkEqcIntersect(const std::vector<Node>& mems); 126 : : /** 127 : : * Return true if we should process regular expression unfoldings with 128 : : * the given polarity at the given effort. 129 : : */ 130 : : bool shouldUnfold(Theory::Effort e, bool pol) const; 131 : : /** 132 : : * Add the unfolding lemma for asserted regular expression membership 133 : : * assertion. Return true if a lemma was successfully sent to the inference 134 : : * manager. 135 : : */ 136 : : bool doUnfold(const Node& assertion); 137 : : // Constants 138 : : Node d_emptyString; 139 : : Node d_emptyRegexp; 140 : : Node d_true; 141 : : Node d_false; 142 : : /** The solver state of the parent of this object */ 143 : : SolverState& d_state; 144 : : /** the output channel of the parent of this object */ 145 : : InferenceManager& d_im; 146 : : /** reference to the core solver, used for certain queries */ 147 : : CoreSolver& d_csolver; 148 : : /** reference to the extended function solver of the parent */ 149 : : ExtfSolver& d_esolver; 150 : : /** Reference to the statistics for the theory of strings/sequences. */ 151 : : SequencesStatistics& d_statistics; 152 : : /** 153 : : * Check partial derivative 154 : : * 155 : : * Returns false if a lemma pertaining to checking the partial derivative 156 : : * of x in r was added. In this case, addedLemma is updated to true. 157 : : * 158 : : * The argument atom is the assertion that explains x in r, which is the 159 : : * normalized form of atom that may be modified using a substitution whose 160 : : * explanation is nf_exp. 161 : : */ 162 : : bool checkPDerivative(Node x, Node r, Node atom, std::vector<Node>& nf_exp); 163 : : cvc5::internal::String getHeadConst(Node x); 164 : : bool deriveRegExp(Node x, Node r, Node atom, std::vector<Node>& ant); 165 : : Node getNormalSymRegExp(Node r, std::vector<Node>& nf_exp); 166 : : /** regular expression operation module */ 167 : : RegExpOpr d_regexp_opr; 168 : : /** Asserted memberships, cached during a full effort check */ 169 : : std::map<Node, std::vector<Node>> d_assertedMems; 170 : : }; /* class TheoryStrings */ 171 : : 172 : : } // namespace strings 173 : : } // namespace theory 174 : : } // namespace cvc5::internal 175 : : 176 : : #endif /* CVC5__THEORY__STRINGS__THEORY_STRINGS_H */