LCOV - code coverage report
Current view: top level - buildbot/coverage/build/src/theory/strings - extf_solver.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2 2 100.0 %
Date: 2026-05-02 10:46:03 Functions: 2 2 100.0 %
Branches: 0 0 -

           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                 :            :  * Solver for extended functions of theory of strings.
      11                 :            :  */
      12                 :            : 
      13                 :            : #include "cvc5_private.h"
      14                 :            : 
      15                 :            : #ifndef CVC5__THEORY__STRINGS__EXTF_SOLVER_H
      16                 :            : #define CVC5__THEORY__STRINGS__EXTF_SOLVER_H
      17                 :            : 
      18                 :            : #include <map>
      19                 :            : #include <vector>
      20                 :            : 
      21                 :            : #include "context/cdo.h"
      22                 :            : #include "expr/node.h"
      23                 :            : #include "smt/env_obj.h"
      24                 :            : #include "theory/ext_theory.h"
      25                 :            : #include "theory/strings/base_solver.h"
      26                 :            : #include "theory/strings/core_solver.h"
      27                 :            : #include "theory/strings/inference_manager.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/strings_rewriter.h"
      32                 :            : #include "theory/strings/theory_strings_preprocess.h"
      33                 :            : 
      34                 :            : namespace cvc5::internal {
      35                 :            : namespace theory {
      36                 :            : namespace strings {
      37                 :            : 
      38                 :            : /**
      39                 :            :  * Non-static information about an extended function t. This information is
      40                 :            :  * constructed and used during the check extended function evaluation
      41                 :            :  * inference schema.
      42                 :            :  *
      43                 :            :  * In the following, we refer to the "context-dependent simplified form"
      44                 :            :  * of a term t to be the result of rewriting t * sigma, where sigma is a
      45                 :            :  * derivable substitution in the current context. For example, the
      46                 :            :  * context-depdendent simplified form of contains( x++y, "a" ) given
      47                 :            :  * sigma = { x -> "" } is contains(y,"a").
      48                 :            :  */
      49                 :            : class ExtfInfoTmp
      50                 :            : {
      51                 :            :  public:
      52                 :     907322 :   ExtfInfoTmp() : d_modelActive(true) {}
      53                 :            :   /**
      54                 :            :    * If s is in d_ctn[true] (resp. d_ctn[false]), then contains( t, s )
      55                 :            :    * (resp. ~contains( t, s )) holds in the current context. The vector
      56                 :            :    * d_ctnFrom is the explanation for why this holds. For example,
      57                 :            :    * if d_ctn[false][i] is "A", then d_ctnFrom[false][i] might be
      58                 :            :    * t = x ++ y AND x = "" AND y = "B".
      59                 :            :    */
      60                 :            :   std::map<bool, std::vector<Node> > d_ctn;
      61                 :            :   std::map<bool, std::vector<Node> > d_ctnFrom;
      62                 :            :   /**
      63                 :            :    * The constant that t is entailed to be equal to, or null if none exist.
      64                 :            :    */
      65                 :            :   Node d_const;
      66                 :            :   /**
      67                 :            :    * The explanation for why t is equal to its context-dependent simplified
      68                 :            :    * form.
      69                 :            :    */
      70                 :            :   std::vector<Node> d_initExp;
      71                 :            :   std::vector<Node> d_exp;
      72                 :            :   /** This flag is false if t is reduced in the model. */
      73                 :            :   bool d_modelActive;
      74                 :            : };
      75                 :            : 
      76                 :            : /**
      77                 :            :  * Extended function solver for the theory of strings. This manages extended
      78                 :            :  * functions for the theory of strings using a combination of context-dependent
      79                 :            :  * simplification (Reynolds et al CAV 2017) and lazy reductions.
      80                 :            :  */
      81                 :            : class ExtfSolver : public InferSideEffectProcess, protected EnvObj
      82                 :            : {
      83                 :            :   typedef context::CDHashSet<Node> NodeSet;
      84                 :            : 
      85                 :            :  public:
      86                 :            :   ExtfSolver(Env& env,
      87                 :            :              SolverState& s,
      88                 :            :              InferenceManager& im,
      89                 :            :              TermRegistry& tr,
      90                 :            :              StringsRewriter& rewriter,
      91                 :            :              BaseSolver& bs,
      92                 :            :              CoreSolver& cs,
      93                 :            :              ExtTheory& et,
      94                 :            :              SequencesStatistics& statistics);
      95                 :            :   ~ExtfSolver();
      96                 :            :   /** check extended functions evaluation
      97                 :            :    *
      98                 :            :    * This applies "context-dependent simplification" for all active extended
      99                 :            :    * function terms in this SAT context. This infers facts of the form:
     100                 :            :    *   x = c => f( t1 ... tn ) = c'
     101                 :            :    * where the rewritten form of f( t1...tn ) { x |-> c } is c', and x = c
     102                 :            :    * is a (tuple of) equalities that are asserted in this SAT context, and
     103                 :            :    * f( t1 ... tn ) is a term from this SAT context.
     104                 :            :    *
     105                 :            :    * For more details, this is steps 4 when effort=0 and step 6 when
     106                 :            :    * effort=1 from Strategy 1 in Reynolds et al, "Scaling up DPLL(T) String
     107                 :            :    * Solvers using Context-Dependent Simplification", CAV 2017. When called with
     108                 :            :    * effort=3, we apply context-dependent simplification based on model values.
     109                 :            :    */
     110                 :            :   void checkExtfEval(int effort);
     111                 :            :   /** check extended function reductions
     112                 :            :    *
     113                 :            :    * This adds "reduction" lemmas for each active extended function in this SAT
     114                 :            :    * context. These are generally lemmas of the form:
     115                 :            :    *   F[t1...tn,k] ^ f( t1 ... tn ) = k
     116                 :            :    * where f( t1 ... tn ) is an active extended function, k is a fresh constant
     117                 :            :    * and F is a formula that constrains k based on the definition of f.
     118                 :            :    *
     119                 :            :    * For more details, this is step 7 from Strategy 1 in Reynolds et al,
     120                 :            :    * CAV 2017. We stratify this in practice based on the effort level,
     121                 :            :    * where for instance, we reduce negatively asserted str.contains only
     122                 :            :    * at LAST_CALL effort. For more information see discussion of "model-based
     123                 :            :    * reductions" in Reynolds et al CAV 2022.
     124                 :            :    */
     125                 :            :   void checkExtfReductions(Theory::Effort e);
     126                 :            :   /**
     127                 :            :    * Check for extended functions that should be applied eagerly. This is
     128                 :            :    * called earlier in the search strategy of strings, in particular before
     129                 :            :    * the core equality reasoning is done.
     130                 :            :    */
     131                 :            :   void checkExtfReductionsEager();
     132                 :            :   /** get preprocess module */
     133                 :            :   StringsPreprocess* getPreprocess() { return &d_preproc; }
     134                 :            : 
     135                 :            :   /**
     136                 :            :    * Get the current substitution for term n.
     137                 :            :    *
     138                 :            :    * This method returns a term that n is currently equal to in the current
     139                 :            :    * context. It updates exp to contain an explanation of why it is currently
     140                 :            :    * equal to that term.
     141                 :            :    *
     142                 :            :    * The argument effort determines what kind of term to return, either
     143                 :            :    * a constant in the equivalence class of n (effort=0), the normal form of
     144                 :            :    * n (effort=1,2) or the model value of n (effort>=3). The latter is only
     145                 :            :    * valid at LAST_CALL effort. If a term of the above form cannot be returned,
     146                 :            :    * then n itself is returned.
     147                 :            :    */
     148                 :            :   Node getCurrentSubstitutionFor(int effort, Node n, std::vector<Node>& exp);
     149                 :            :   /** get mapping from extended functions to their information
     150                 :            :    *
     151                 :            :    * This information is valid during a full effort check after a call to
     152                 :            :    * checkExtfEval.
     153                 :            :    */
     154                 :            :   const std::map<Node, ExtfInfoTmp>& getInfo() const;
     155                 :            :   //---------------------------------- information about ExtTheory
     156                 :            :   /** Are there any active extended functions? */
     157                 :            :   bool hasExtendedFunctions() const;
     158                 :            :   /**
     159                 :            :    * Get the function applications of kind k that are active in the current
     160                 :            :    * context (see ExtTheory::getActive).
     161                 :            :    */
     162                 :            :   std::vector<Node> getActive(Kind k) const;
     163                 :            :   /**
     164                 :            :    * Return true if n is active in the model. If this method returns false,
     165                 :            :    * then n is already satisfied in the model (its value in the model is
     166                 :            :    * the same as its representative in the equality engine).
     167                 :            :    */
     168                 :            :   bool isActiveInModel(Node n) const;
     169                 :            :   /**
     170                 :            :    * @return The relevant active terms. This method retrieves the relevant
     171                 :            :    * terms from the term registry and filters out inactive terms.
     172                 :            :    *
     173                 :            :    * Note that the set of active terms is not a subset of the relevant terms
     174                 :            :    * since active terms may include preregistered terms that don't appear
     175                 :            :    * in any current assertions.
     176                 :            :    */
     177                 :            :   std::vector<Node> getRelevantActive() const;
     178                 :            :   //---------------------------------- end information about ExtTheory
     179                 :            :   /**
     180                 :            :    * Print the relevant information regarding why we have a model, return as a
     181                 :            :    * string.
     182                 :            :    */
     183                 :            :   std::string debugPrintModel();
     184                 :            : 
     185                 :            :   /**
     186                 :            :    * Is extended function (or regular expression membership) reduced? Note that
     187                 :            :    * if n has Boolean type, our reductions are dependent upon the polarity of n,
     188                 :            :    * in which case n may be the negation of an extended function. For
     189                 :            :    * example, (not (str.in_re x R)) indicates that we have reduced
     190                 :            :    * (str.in_re x R) based on its negative unfolding.
     191                 :            :    */
     192                 :            :   bool isReduced(const Node& n) const;
     193                 :            :   /**
     194                 :            :    * Mark that extended function (or regular expression membership) n has been
     195                 :            :    * reduced. Like above, n could be a negation of an extended function of
     196                 :            :    * Boolean type.
     197                 :            :    */
     198                 :            :   void markReduced(const Node& n);
     199                 :            : 
     200                 :            :   /** Called when ii is ready to be processed as a fact */
     201                 :            :   void processFact(InferInfo& ii, ProofGenerator*& pg) override;
     202                 :            :   /** Called when ii is ready to be processed as a lemma */
     203                 :            :   TrustNode processLemma(InferInfo& ii, LemmaProperty& p) override;
     204                 :            : 
     205                 :            :  private:
     206                 :            :   /**
     207                 :            :    * Helper method for checkExtfReductions / maybeHasCandidateModel, returns
     208                 :            :    * true if a reduction lemma was sent
     209                 :            :    */
     210                 :            :   bool checkExtfReductionsInternal(int effort);
     211                 :            :   /**
     212                 :            :    * Determines if n should be reduced based on the effort level.
     213                 :            :    *
     214                 :            :    * @param effort the effort level
     215                 :            :    * @param n the term to reduce
     216                 :            :    * @param pol polarity of n, where 1 true, -1 false, 0 neither
     217                 :            :    */
     218                 :            :   bool shouldDoReduction(int effort, Node n, int pol);
     219                 :            :   /** do reduction
     220                 :            :    *
     221                 :            :    * This is called when an extended function application n is not able to be
     222                 :            :    * simplified by context-depdendent simplification, and we are resorting to
     223                 :            :    * expanding n to its full semantics via a reduction. This method returns
     224                 :            :    * true if it successfully reduced n by some reduction. If so, it either
     225                 :            :    * caches that the reduction lemma was sent, or marks n as reduced in this
     226                 :            :    * SAT-context. The argument effort has the same meaning as in
     227                 :            :    * checkExtfReductions.
     228                 :            :    *
     229                 :            :    * @param n the term to reduce
     230                 :            :    * @param pol polarity of n, where 1 true, -1 false, 0 neither
     231                 :            :    */
     232                 :            :   void doReduction(Node n, int pol);
     233                 :            :   /** check extended function inferences
     234                 :            :    *
     235                 :            :    * This function makes additional inferences for n that do not contribute
     236                 :            :    * to its reduction, but may help show a refutation.
     237                 :            :    *
     238                 :            :    * This function is called when the context-depdendent simplified form of
     239                 :            :    * n is nr. The argument "in" is the information object for n.
     240                 :            :    */
     241                 :            :   void checkExtfInference(Node n, Node nr, ExtfInfoTmp& in);
     242                 :            :   /** The solver state object */
     243                 :            :   SolverState& d_state;
     244                 :            :   /** The (custom) output channel of the theory of strings */
     245                 :            :   InferenceManager& d_im;
     246                 :            :   /** Reference to the term registry of theory of strings */
     247                 :            :   TermRegistry& d_termReg;
     248                 :            :   /** The theory rewriter for this theory. */
     249                 :            :   StringsRewriter& d_rewriter;
     250                 :            :   /** reference to the base solver, used for certain queries */
     251                 :            :   BaseSolver& d_bsolver;
     252                 :            :   /** reference to the core solver, used for certain queries */
     253                 :            :   CoreSolver& d_csolver;
     254                 :            :   /** the extended theory object for the theory of strings */
     255                 :            :   ExtTheory& d_extt;
     256                 :            :   /** Reference to the statistics for the theory of strings/sequences. */
     257                 :            :   SequencesStatistics& d_statistics;
     258                 :            :   /** preprocessing utility, for performing strings reductions */
     259                 :            :   StringsPreprocess d_preproc;
     260                 :            :   /** Common constants */
     261                 :            :   Node d_true;
     262                 :            :   Node d_false;
     263                 :            :   /** Empty vector */
     264                 :            :   std::vector<Node> d_emptyVec;
     265                 :            :   /** map extended functions to the above information */
     266                 :            :   std::map<Node, ExtfInfoTmp> d_extfInfoTmp;
     267                 :            :   /** map from reduced extended functions to their original */
     268                 :            :   std::map<Node, Node> d_extfToOrig;
     269                 :            :   /** any non-reduced extended functions exist? */
     270                 :            :   context::CDO<bool> d_hasExtf;
     271                 :            :   /** extended functions inferences cache */
     272                 :            :   NodeSet d_extfInferCache;
     273                 :            :   /** The set of extended functions we have sent reduction lemmas for */
     274                 :            :   NodeSet d_reduced;
     275                 :            :   /** Map from lemmas to the terms they justify the reduction of */
     276                 :            :   std::map<Node, Node> d_reductionWaitingMap;
     277                 :            : };
     278                 :            : 
     279                 :            : /** An extended theory callback */
     280                 :            : class StringsExtfCallback : public ExtTheoryCallback
     281                 :            : {
     282                 :            :  public:
     283                 :      51016 :   StringsExtfCallback() : d_esolver(nullptr) {}
     284                 :            :   /**
     285                 :            :    * Get current substitution based on the underlying extended function
     286                 :            :    * solver.
     287                 :            :    */
     288                 :            :   bool getCurrentSubstitution(int effort,
     289                 :            :                               const std::vector<Node>& vars,
     290                 :            :                               std::vector<Node>& subs,
     291                 :            :                               std::map<Node, std::vector<Node> >& exp) override;
     292                 :            :   /** The extended function solver */
     293                 :            :   ExtfSolver* d_esolver;
     294                 :            : };
     295                 :            : 
     296                 :            : }  // namespace strings
     297                 :            : }  // namespace theory
     298                 :            : }  // namespace cvc5::internal
     299                 :            : 
     300                 :            : #endif /* CVC5__THEORY__STRINGS__EXTF_SOLVER_H */

Generated by: LCOV version 1.14