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 : : * Arithmetic theory. 11 : : */ 12 : : 13 : : #include "cvc5_private.h" 14 : : 15 : : #pragma once 16 : : 17 : : #include "expr/node.h" 18 : : #include "theory/arith/arith_preprocess.h" 19 : : #include "theory/arith/arith_rewriter.h" 20 : : #include "theory/arith/arith_subs.h" 21 : : #include "theory/arith/branch_and_bound.h" 22 : : #include "theory/arith/inference_manager.h" 23 : : #include "theory/arith/linear/linear_solver.h" 24 : : #include "theory/arith/pp_rewrite_eq.h" 25 : : #include "theory/arith/proof_checker.h" 26 : : #include "theory/theory.h" 27 : : #include "theory/theory_state.h" 28 : : 29 : : namespace cvc5::internal { 30 : : namespace theory { 31 : : namespace arith { 32 : : namespace nl { 33 : : class NonlinearExtension; 34 : : } 35 : : 36 : : class EqualitySolver; 37 : : 38 : : /** 39 : : * Implementation of linear and non-linear integer and real arithmetic. 40 : : * The linear arithmetic solver is based upon: 41 : : * http://research.microsoft.com/en-us/um/people/leonardo/cav06.pdf 42 : : */ 43 : : class TheoryArith : public Theory 44 : : { 45 : : public: 46 : : TheoryArith(Env& env, OutputChannel& out, Valuation valuation); 47 : : virtual ~TheoryArith(); 48 : : 49 : : //--------------------------------- initialization 50 : : /** get the official theory rewriter of this theory */ 51 : : TheoryRewriter* getTheoryRewriter() override; 52 : : /** get the proof checker of this theory */ 53 : : ProofRuleChecker* getProofChecker() override; 54 : : /** 55 : : * Returns true if this theory needs an equality engine, which is assigned 56 : : * to it (d_equalityEngine) by the equality engine manager during 57 : : * TheoryEngine::finishInit, prior to calling finishInit for this theory. 58 : : * If this method returns true, it stores instructions for the notifications 59 : : * this Theory wishes to receive from its equality engine. 60 : : */ 61 : : bool needsEqualityEngine(EeSetupInfo& esi) override; 62 : : /** finish initialization */ 63 : : void finishInit() override; 64 : : //--------------------------------- end initialization 65 : : /** 66 : : * Does non-context dependent setup for a node connected to a theory. 67 : : */ 68 : : void preRegisterTerm(TNode n) override; 69 : : 70 : : //--------------------------------- standard check 71 : : /** Pre-check, called before the fact queue of the theory is processed. */ 72 : : bool preCheck(Effort level) override; 73 : : /** Post-check, called after the fact queue of the theory is processed. */ 74 : : void postCheck(Effort level) override; 75 : : /** Pre-notify fact, return true if processed. */ 76 : : bool preNotifyFact(TNode atom, 77 : : bool pol, 78 : : TNode fact, 79 : : bool isPrereg, 80 : : bool isInternal) override; 81 : : //--------------------------------- end standard check 82 : : bool needsCheckLastEffort() override; 83 : : void propagate(Effort e) override; 84 : : TrustNode explain(TNode n) override; 85 : : 86 : : bool collectModelInfo(TheoryModel* m, const std::set<Node>& termSet) override; 87 : : /** 88 : : * Collect model values in m based on the relevant terms given by termSet. 89 : : */ 90 : : bool collectModelValues(TheoryModel* m, 91 : : const std::set<Node>& termSet) override; 92 : : 93 : : void presolve() override; 94 : : void notifyRestart() override; 95 : : bool ppAssert(TrustNode tin, TrustSubstitutionMap& outSubstitutions) override; 96 : : /** 97 : : * Preprocess rewrite terms, return the trust node encapsulating the 98 : : * preprocessed form of n, and the proof generator that can provide the 99 : : * proof for the equivalence of n and this term. 100 : : * 101 : : * This calls the operator elimination utility to eliminate extended 102 : : * symbols. 103 : : */ 104 : : TrustNode ppRewrite(TNode atom, std::vector<SkolemLemma>& lems) override; 105 : : TrustNode ppStaticRewrite(TNode atom) override; 106 : : void ppStaticLearn(TNode in, std::vector<TrustNode>& learned) override; 107 : : 108 : 0 : std::string identify() const override { return std::string("TheoryArith"); } 109 : : 110 : : EqualityStatus getEqualityStatus(TNode a, TNode b) override; 111 : : 112 : : void notifySharedTerm(TNode n) override; 113 : : 114 : : Node getCandidateModelValue(TNode var) override; 115 : : 116 : : std::pair<bool, Node> entailmentCheck(TNode lit) override; 117 : : 118 : : /** Return a reference to the arith::InferenceManager. */ 119 : 14971 : InferenceManager& getInferenceManager() { return d_im; } 120 : : 121 : : private: 122 : : /** 123 : : * Update d_arithModelCache (if it is empty right now) and compute the termSet 124 : : * by calling collectAssertedTerms. 125 : : */ 126 : : void updateModelCache(std::set<Node>& termSet); 127 : : /** 128 : : * Update d_arithModelCache (if it is empty right now) using the given 129 : : * termSet. 130 : : */ 131 : : void updateModelCacheInternal(const std::set<Node>& termSet); 132 : : /** 133 : : * Finalized model cache. Called after d_arithModelCache is finalized during 134 : : * a full effort check. It computes d_arithModelCacheSubs/Vars, which are 135 : : * used during theory combination, for getEqualityStatus. 136 : : */ 137 : : void finalizeModelCache(); 138 : : /** 139 : : * Perform a sanity check on the model that all integer variables are assigned 140 : : * to integer values. If an integer variables is assigned to a non-integer 141 : : * value, but the respective lemma can not be added (i.e. it has already been 142 : : * added) an assertion triggers. Otherwise teturns true if a lemma was added, 143 : : * false otherwise. 144 : : */ 145 : : bool sanityCheckIntegerModel(); 146 : : 147 : : /** Get the proof equality engine */ 148 : : eq::ProofEqEngine* getProofEqEngine(); 149 : : /** Timer for ppRewrite */ 150 : : TimerStat d_ppRewriteTimer; 151 : : /** The state object */ 152 : : TheoryState d_astate; 153 : : /** The arith::InferenceManager. */ 154 : : InferenceManager d_im; 155 : : /** The preprocess rewriter for equality */ 156 : : PreprocessRewriteEq d_ppre; 157 : : /** The branch and bound utility */ 158 : : BranchAndBound d_bab; 159 : : /** The equality solver */ 160 : : std::unique_ptr<EqualitySolver> d_eqSolver; 161 : : /** The (old) linear arithmetic solver */ 162 : : linear::LinearSolver d_internal; 163 : : 164 : : /** 165 : : * The non-linear extension, responsible for all approaches for non-linear 166 : : * arithmetic. 167 : : */ 168 : : std::unique_ptr<nl::NonlinearExtension> d_nonlinearExtension; 169 : : /** The operator elimination utility */ 170 : : OperatorElim d_opElim; 171 : : /** The preprocess utility */ 172 : : ArithPreprocess d_arithPreproc; 173 : : /** The theory rewriter for this theory. */ 174 : : ArithRewriter d_rewriter; 175 : : 176 : : /** 177 : : * Caches the current arithmetic model with the following life cycle: 178 : : * postCheck retrieves the model from arith_private and puts it into the 179 : : * cache. If nonlinear reasoning is enabled, the cache is used for (and 180 : : * possibly updated by) model-based refinement in postCheck. 181 : : * In collectModelValues, the cache is filtered for the termSet and then 182 : : * used to augment the TheoryModel. 183 : : */ 184 : : std::map<Node, Node> d_arithModelCache; 185 : : /** Component of the above that was ill-typed */ 186 : : std::map<Node, Node> d_arithModelCacheIllTyped; 187 : : /** The above model cache, in substitution form. */ 188 : : ArithSubs d_arithModelCacheSubs; 189 : : /** Is the above map computed? */ 190 : : bool d_arithModelCacheSet; 191 : : /** Checks the proof rules of this theory. */ 192 : : ArithProofRuleChecker d_checker; 193 : : 194 : : }; /* class TheoryArith */ 195 : : 196 : : } // namespace arith 197 : : } // namespace theory 198 : : } // namespace cvc5::internal