carl  24.04
Computer ARithmetic Library
Substitution.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Formula.h"
4 #include "Visit.h"
5 
6 namespace carl {
7 
8 namespace helper {
9  template<typename Pol>
10  struct Substitutor {
11  const std::map<Formula<Pol>,Formula<Pol>>& replacements;
12  explicit Substitutor(const std::map<Formula<Pol>,Formula<Pol>>& repl): replacements(repl) {}
14  auto it = replacements.find(formula);
15  if (it == replacements.end()) return formula;
16  return it->second;
17  }
18  };
19 
20  template<typename Pol>
22  const std::map<Variable,typename Formula<Pol>::PolynomialType>& replacements;
23  explicit PolynomialSubstitutor(const std::map<Variable,typename Formula<Pol>::PolynomialType>& repl): replacements(repl) {}
25  if (formula.type() != FormulaType::CONSTRAINT) return formula;
26  return Formula<Pol>(carl::substitute(formula.constraint().lhs(), replacements), formula.constraint().relation());
27  }
28  };
29 
30  template<typename Pol>
32  const std::map<BVVariable,BVTerm>& replacements;
33  explicit BitvectorSubstitutor(const std::map<BVVariable,BVTerm>& repl): replacements(repl) {}
35  if (formula.type() != FormulaType::BITVECTOR) return formula;
36  BVTerm lhs = formula.bv_constraint().lhs().substitute(replacements);
37  BVTerm rhs = formula.bv_constraint().rhs().substitute(replacements);
38  return Formula<Pol>(BVConstraint::create(formula.bv_constraint().relation(), lhs, rhs));
39  }
40  };
41 
42  template<typename Pol>
44  const std::map<UVariable,UFInstance>& replacements;
45  explicit UninterpretedSubstitutor(const std::map<UVariable,UFInstance>& repl): replacements(repl) {}
47  if (formula.type() != FormulaType::UEQ) return formula;
48 
49  }
50  };
51 }
52 
53 template<typename Pol, typename Source, typename Target>
54 Formula<Pol> substitute(const Formula<Pol>& formula, const Source& source, const Target& target) {
55  std::map<Source,Target> tmp;
56  tmp.emplace(source, target);
57  return substitute(formula, tmp);
58 }
59 template<typename Pol>
60 Formula<Pol> substitute(const Formula<Pol>& formula, const std::map<Formula<Pol>,Formula<Pol>>& replacements) {
61  helper::Substitutor<Pol> subs(replacements);
62  return visit_result(formula, std::function<Formula<Pol>(Formula<Pol>)>(subs));
63 }
64 template<typename Pol>
65 Formula<Pol> substitute(const Formula<Pol>& formula, const std::map<Variable,typename Formula<Pol>::PolynomialType>& replacements) {
66  helper::PolynomialSubstitutor<Pol> subs(replacements);
67  return visit_result(formula, std::function<Formula<Pol>(Formula<Pol>)>(subs));
68 }
69 template<typename Pol>
70 Formula<Pol> substitute(const Formula<Pol>& formula, const std::map<BVVariable,BVTerm>& replacements) {
71  helper::BitvectorSubstitutor<Pol> subs(replacements);
72  return visit_result(formula, std::function<Formula<Pol>(Formula<Pol>)>(subs));
73 }
74 template<typename Pol>
75 Formula<Pol> substitute(const Formula<Pol>& formula, const std::map<UVariable,UFInstance>& replacements) {
76  helper::UninterpretedSubstitutor<Pol> subs(replacements);
77  return visit_result(formula, std::function<Formula<Pol>(Formula<Pol>)>(subs));
78 }
79 
80 }
carl is the main namespace for the library.
@ CONSTRAINT
@ BITVECTOR
Coeff substitute(const Monomial &m, const std::map< Variable, Coeff > &substitutions)
Applies the given substitutions to a monomial.
Definition: Substitution.h:19
Formula< Pol > visit_result(const Formula< Pol > &formula, Visitor func)
Recursively calls func on every subformula and return a new formula.
Definition: Visit.h:49
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
const BVTerm & rhs() const
Definition: BVConstraint.h:62
const BVTerm & lhs() const
Definition: BVConstraint.h:55
BVCompareRelation relation() const
Definition: BVConstraint.h:69
static BVConstraint create(bool _consistent=true)
BVTerm substitute(const std::map< BVVariable, BVTerm > &) const
Definition: BVTerm.cpp:99
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
Definition: Formula.h:47
Pol PolynomialType
A typedef for the template argument.
Definition: Formula.h:59
const Constraint< Pol > & constraint() const
Definition: Formula.h:410
const BVConstraint & bv_constraint() const
Definition: Formula.h:426
FormulaType type() const
Definition: Formula.h:254
const std::map< Formula< Pol >, Formula< Pol > > & replacements
Definition: Substitution.h:11
Formula< Pol > operator()(const Formula< Pol > &formula)
Definition: Substitution.h:13
Substitutor(const std::map< Formula< Pol >, Formula< Pol >> &repl)
Definition: Substitution.h:12
Formula< Pol > operator()(const Formula< Pol > &formula)
Definition: Substitution.h:24
PolynomialSubstitutor(const std::map< Variable, typename Formula< Pol >::PolynomialType > &repl)
Definition: Substitution.h:23
const std::map< Variable, typename Formula< Pol >::PolynomialType > & replacements
Definition: Substitution.h:22
Formula< Pol > operator()(const Formula< Pol > &formula)
Definition: Substitution.h:34
BitvectorSubstitutor(const std::map< BVVariable, BVTerm > &repl)
Definition: Substitution.h:33
const std::map< BVVariable, BVTerm > & replacements
Definition: Substitution.h:32
const std::map< UVariable, UFInstance > & replacements
Definition: Substitution.h:44
UninterpretedSubstitutor(const std::map< UVariable, UFInstance > &repl)
Definition: Substitution.h:45
Formula< Pol > operator()(const Formula< Pol > &formula)
Definition: Substitution.h:46