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 : : * Extended theory interface.
11 : : *
12 : : * This implements a generic module, used by theory solvers, for performing
13 : : * "context-dependent simplification", as described in Reynolds et al
14 : : * "Designing Theory Solvers with Extensions", FroCoS 2017.
15 : : *
16 : : * At a high level, this technique implements a generic inference scheme based
17 : : * on the combination of SAT-context-dependent equality reasoning and
18 : : * SAT-context-indepedent rewriting.
19 : : *
20 : : * As a simple example, say
21 : : * (1) TheoryStrings tells us that the following facts hold in the SAT context:
22 : : * x = "A" ^ str.contains( str.++( x, z ), "B" ) = true.
23 : : * (2) The Rewriter tells us that:
24 : : * str.contains( str.++( "A", z ), "B" ) ----> str.contains( z, "B" ).
25 : : * From this, this class may infer that the following lemma is T-valid:
26 : : * x = "A" ^ str.contains( str.++( x, z ), "B" ) => str.contains( z, "B" )
27 : : */
28 : :
29 : : #include "cvc5_private.h"
30 : :
31 : : #ifndef CVC5__THEORY__EXT_THEORY_H
32 : : #define CVC5__THEORY__EXT_THEORY_H
33 : :
34 : : #include <map>
35 : :
36 : : #include "context/cdhashmap.h"
37 : : #include "context/cdhashset.h"
38 : : #include "context/cdo.h"
39 : : #include "context/context.h"
40 : : #include "expr/node.h"
41 : : #include "proof/proof.h"
42 : : #include "smt/env_obj.h"
43 : : #include "theory/theory_inference_manager.h"
44 : :
45 : : namespace cvc5::internal {
46 : : namespace theory {
47 : :
48 : : class OutputChannel;
49 : :
50 : : /** Reasons for why a term was marked reduced */
51 : : enum class ExtReducedId
52 : : {
53 : : // the extended function is not marked reduced
54 : : NONE,
55 : : // the extended function substitutes+rewrites to a constant
56 : : SR_CONST,
57 : : // the extended function was reduced by the callback
58 : : REDUCTION,
59 : : // the extended function substitutes+rewrites to zero
60 : : ARITH_SR_ZERO,
61 : : // the extended function substitutes+rewrites to a linear (non-zero) term
62 : : ARITH_SR_LINEAR,
63 : : // an extended string function substitutes+rewrites to a constant
64 : : STRINGS_SR_CONST,
65 : : // a negative str.contains was reduced to a disequality
66 : : STRINGS_NEG_CTN_DEQ,
67 : : // a str.contains was subsumed by another based on a decomposition
68 : : STRINGS_CTN_DECOMPOSE,
69 : : // reduced via an intersection inference
70 : : STRINGS_REGEXP_INTER,
71 : : // subsumed due to intersection reasoning
72 : : STRINGS_REGEXP_INTER_SUBSUME,
73 : : // subsumed due to RE inclusion reasoning for positive memberships
74 : : STRINGS_REGEXP_INCLUDE,
75 : : // subsumed due to RE inclusion reasoning for negative memberships
76 : : STRINGS_REGEXP_INCLUDE_NEG,
77 : : // satisfied due to normal form substitution into re memberships
78 : : STRINGS_REGEXP_RE_SYM_NF,
79 : : // satisfied due to partial derivative computation
80 : : STRINGS_REGEXP_PDERIVATIVE,
81 : : // reduction for seq.nth over seq.rev
82 : : STRINGS_NTH_REV,
83 : : // the reason for the reduction is unknown
84 : : UNKNOWN,
85 : : };
86 : : /**
87 : : * Converts an ext reduced identifier to a string.
88 : : *
89 : : * @param id The ext reduced identifier
90 : : * @return The name of the ext reduced identifier
91 : : */
92 : : const char* toString(ExtReducedId id);
93 : :
94 : : /**
95 : : * Writes an ext reduced identifier to a stream.
96 : : *
97 : : * @param out The stream to write to
98 : : * @param id The ext reduced identifier to write to the stream
99 : : * @return The stream
100 : : */
101 : : std::ostream& operator<<(std::ostream& out, ExtReducedId id);
102 : :
103 : : /**
104 : : * A callback class for ExtTheory below. This class is responsible for
105 : : * determining how to apply context-dependent simplification.
106 : : */
107 : : class ExtTheoryCallback
108 : : {
109 : : public:
110 : 84367 : virtual ~ExtTheoryCallback() {}
111 : : /*
112 : : * Get current substitution at an effort
113 : : * @param effort The effort identifier
114 : : * @param vars The variables to get a substitution for
115 : : * @param subs The terms to substitute for variables, in order. This vector
116 : : * should be updated to one the same size as vars.
117 : : * @param exp The map containing the explanation for each variable. Together
118 : : * with subs, we have that:
119 : : * ( exp[vars[i]] => vars[i] = subs[i] ) holds for all i
120 : : * @return true if any (non-identity) substitution was added to subs.
121 : : */
122 : : virtual bool getCurrentSubstitution(int effort,
123 : : const std::vector<Node>& vars,
124 : : std::vector<Node>& subs,
125 : : std::map<Node, std::vector<Node> >& exp);
126 : :
127 : : /*
128 : : * Is extended function n reduced? This returns true if n is reduced to a
129 : : * form that requires no further interaction from the theory.
130 : : *
131 : : * @param effort The effort identifier
132 : : * @param n The term to reduce
133 : : * @param on The original form of n, before substitution
134 : : * @param exp The explanation of on = n
135 : : * @param id If this method returns true, then id is updated to the reason
136 : : * why n was reduced.
137 : : * @return true if n is reduced.
138 : : */
139 : : virtual bool isExtfReduced(
140 : : int effort, Node n, Node on, std::vector<Node>& exp, ExtReducedId& id);
141 : :
142 : : /**
143 : : * Get reduction for node n.
144 : : * If return value is true, then n is reduced.
145 : : * If satDep is updated to false, then n is reduced independent of the
146 : : * SAT context (e.g. by a lemma that persists at this
147 : : * user-context level).
148 : : * If nr is non-null, then ( n = nr ) should be added as a lemma by caller,
149 : : * and return value of this method should be true.
150 : : */
151 : : virtual bool getReduction(int effort, Node n, Node& nr, bool& satDep);
152 : : };
153 : :
154 : : /** Extended theory class
155 : : *
156 : : * This class is used for constructing generic extensions to theory solvers.
157 : : * In particular, it provides methods for "context-dependent simplification"
158 : : * and "model-based refinement". For details, see Reynolds et al "Design
159 : : * Theory Solvers with Extensions", FroCoS 2017.
160 : : *
161 : : * It maintains:
162 : : * (1) A set of "extended function" kinds (d_extf_kind),
163 : : * (2) A database of active/inactive extended function terms (d_ext_func_terms)
164 : : * that have been registered to the class.
165 : : *
166 : : * This class has methods doInferences/doReductions, which send out lemmas
167 : : * based on the current set of active extended function terms. The former
168 : : * is based on context-dependent simplification, where this class asks the
169 : : * underlying theory for a "derivable substitution", whereby extended functions
170 : : * may be reducable.
171 : : */
172 : : class ExtTheory : protected EnvObj, public ProofGenerator
173 : : {
174 : : using NodeBoolMap = context::CDHashMap<Node, bool>;
175 : : using NodeExtReducedIdMap = context::CDHashMap<Node, ExtReducedId>;
176 : : using NodeSet = context::CDHashSet<Node>;
177 : :
178 : : public:
179 : : /** constructor
180 : : *
181 : : * If cacheEnabled is false, we do not cache results of getSubstitutedTerm.
182 : : */
183 : : ExtTheory(Env& env, ExtTheoryCallback& p, TheoryInferenceManager& im);
184 : 84367 : virtual ~ExtTheory() {}
185 : : /** Tells this class to treat terms with Kind k as extended functions */
186 : 1259262 : void addFunctionKind(Kind k) { d_extf_kind[k] = true; }
187 : : /** Is kind k treated as an extended function by this class? */
188 : 1012692 : bool hasFunctionKind(Kind k) const
189 : : {
190 : 1012692 : return d_extf_kind.find(k) != d_extf_kind.end();
191 : : }
192 : : /** register term
193 : : *
194 : : * Registers term n with this class. Adds n to the set of extended function
195 : : * terms known by this class (d_ext_func_terms) if n is an extended function,
196 : : * that is, if addFunctionKind( n.getKind() ) was called.
197 : : */
198 : : void registerTerm(Node n);
199 : : /** set n as reduced/inactive
200 : : *
201 : : * If satDep = false, then n remains inactive in the duration of this
202 : : * user-context level
203 : : */
204 : : void markInactive(Node n, ExtReducedId rid, bool satDep = true);
205 : : /** getSubstitutedTerms
206 : : *
207 : : * input : effort, terms
208 : : * output : sterms, exp, where ( exp[i] => terms[i] = sterms[i] ) for all i.
209 : : *
210 : : * For each i, sterms[i] = term[i] * sigma for some "derivable substitution"
211 : : * sigma. We obtain derivable substitutions and their explanations via calls
212 : : * to the underlying theory's Theory::getCurrentSubstitution method.
213 : : */
214 : : void getSubstitutedTerms(int effort,
215 : : const std::vector<Node>& terms,
216 : : std::vector<Node>& sterms,
217 : : std::vector<std::vector<Node> >& exp);
218 : : /** doInferences
219 : : *
220 : : * This function performs "context-dependent simplification". The method takes
221 : : * as input:
222 : : * effort: an identifier used to determine which terms we reduce and the
223 : : * form of the derivable substitution we will use,
224 : : * terms: the set of terms to simplify
225 : : *
226 : : * Sends rewriting lemmas of the form ( exp => t = c ) where t is in terms
227 : : * and c is a constant, c = rewrite( t*sigma ) where exp |= sigma. These
228 : : * lemmas are determined by asking the underlying theory for a derivable
229 : : * substitution (through getCurrentSubstitution with getSubstitutedTerm)
230 : : * above, applying this substitution to terms in terms, rewriting
231 : : * the result and checking with the theory whether the resulting term is
232 : : * in reduced form (isExtfReduced).
233 : : *
234 : : * This method adds the extended terms that are still active to nred, and
235 : : * returns true if and only if a lemma of the above form was sent.
236 : : */
237 : : bool doInferences(int effort,
238 : : const std::vector<Node>& terms,
239 : : std::vector<Node>& nred);
240 : : /**
241 : : * Calls the above function, where terms is getActive(), the set of currently
242 : : * active terms.
243 : : */
244 : : bool doInferences(int effort, std::vector<Node>& nred);
245 : :
246 : : /** get the set of all extended function terms from d_ext_func_terms */
247 : : void getTerms(std::vector<Node>& terms);
248 : : /** is there at least one active extended function term? */
249 : : bool hasActiveTerm() const;
250 : : /** is n an active extended function term? */
251 : : bool isActive(Node n) const;
252 : : /**
253 : : * Same as above, but rid is updated to the reason if this method returns
254 : : * false.
255 : : */
256 : : bool isActive(Node n, ExtReducedId& rid) const;
257 : : /** get the set of active terms from d_ext_func_terms */
258 : : std::vector<Node> getActive() const;
259 : : /** get the set of active terms from d_ext_func_terms of kind k */
260 : : std::vector<Node> getActive(Kind k) const;
261 : :
262 : : /**
263 : : * Get proof for a lemma sent via this class, which is of the form
264 : : * (t1 = s1 ^ ... ^ tn = sn) => (t = s)
265 : : * where the conclusion can be shown via substitution + rewriting.
266 : : */
267 : : std::shared_ptr<ProofNode> getProofFor(Node fact) override;
268 : : /** identify */
269 : : std::string identify() const override;
270 : :
271 : : private:
272 : : /** returns the set of variable subterms of n */
273 : : static std::vector<Node> collectVars(Node n);
274 : : /** is n context dependent inactive? */
275 : : bool isContextIndependentInactive(Node n) const;
276 : : /**
277 : : * Same as above, but rid is updated to the reason if this method returns
278 : : * true.
279 : : */
280 : : bool isContextIndependentInactive(Node n, ExtReducedId& rid) const;
281 : : /** do inferences internal */
282 : : bool doInferencesInternal(int effort,
283 : : const std::vector<Node>& terms,
284 : : std::vector<Node>& nred);
285 : : /** send lemma on the output channel */
286 : : bool sendLemma(TrustNode lem, InferenceId id);
287 : : /** reference to the callback */
288 : : ExtTheoryCallback& d_parent;
289 : : /** inference manager used to send lemmas */
290 : : TheoryInferenceManager& d_im;
291 : : /** the true node */
292 : : Node d_true;
293 : : /** extended function terms, map to whether they are active */
294 : : NodeBoolMap d_ext_func_terms;
295 : : /** mapping to why extended function terms are inactive */
296 : : NodeExtReducedIdMap d_extfExtReducedIdMap;
297 : : /**
298 : : * The set of terms from d_ext_func_terms that are SAT-context-independent
299 : : * inactive. For instance, a term t is SAT-context-independent inactive if
300 : : * a reduction lemma of the form t = t' was added in this user-context level.
301 : : */
302 : : NodeExtReducedIdMap d_ci_inactive;
303 : : /**
304 : : * Watched term for checking if any non-reduced extended functions exist.
305 : : * This is an arbitrary active member of d_ext_func_terms.
306 : : */
307 : : context::CDO<Node> d_has_extf;
308 : : /** the set of kinds we are treated as extended functions */
309 : : std::map<Kind, bool> d_extf_kind;
310 : : /** information for each term in d_ext_func_terms */
311 : : class ExtfInfo
312 : : {
313 : : public:
314 : : /** all variables in this term */
315 : : std::vector<Node> d_vars;
316 : : };
317 : : std::map<Node, ExtfInfo> d_extf_info;
318 : :
319 : : // cache of all lemmas sent
320 : : NodeSet d_lemmas;
321 : : };
322 : :
323 : : } // namespace theory
324 : : } // namespace cvc5::internal
325 : :
326 : : #endif /* CVC5__THEORY__EXT_THEORY_H */
|