carl  24.04
Computer ARithmetic Library
Visit.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace carl {
4 
5 
6 /**
7  * Recursively calls func on every subformula.
8  * @param formula Formula to visit.
9  * @param func Function to call.
10  */
11 template<typename Pol, typename Visitor>
12 void visit(const Formula<Pol>& formula, /*std::function<void(const Formula<Pol>&)>&*/ Visitor func) {
13  switch (formula.type()) {
14  case AND:
15  case OR:
16  case IFF:
17  case XOR:
18  case IMPLIES:
19  case ITE:
20  for (const auto& cur: formula.subformulas()) visit(cur, func);
21  break;
22  case NOT:
23  visit(formula.subformula(), func);
24  break;
25  case BOOL:
26  case CONSTRAINT:
27  case VARCOMPARE:
28  case VARASSIGN:
29  case BITVECTOR:
30  case TRUE:
31  case FALSE:
32  case UEQ:
33  break;
34  case EXISTS:
35  case FORALL:
36  visit(formula.quantified_formula(), func);
37  break;
38  }
39  func(formula);
40 }
41 /**
42  * Recursively calls func on every subformula and return a new formula.
43  * On every call of func, the passed formula is replaced by the result.
44  * @param formula Formula to visit.
45  * @param func Function to call.
46  * @return New formula.
47  */
48 template<typename Pol, typename Visitor>
49 Formula<Pol> visit_result(const Formula<Pol>& formula, /*std::function<Formula<Pol>(const Formula<Pol>&)>&*/ Visitor func) {
50  Formula<Pol> newFormula = formula;
51  switch (formula.type()) {
52  case AND:
53  case OR:
54  case IFF:
55  case XOR: {
57  bool changed = false;
58  for (const auto& cur: formula.subformulas()) {
59  Formula newCur = visit_result(cur, func);
60  if (newCur != cur) changed = true;
61  newSubformulas.push_back(newCur);
62  }
63  if (changed) {
64  newFormula = Formula(formula.type(), std::move(newSubformulas));
65  }
66  break;
67  }
68  case NOT: {
69  Formula<Pol> cur = visit_result(formula.subformula(), func);
70  if (cur != formula.subformula()) {
71  newFormula = !cur;
72  }
73  break;
74  }
75  case IMPLIES: {
76  Formula<Pol> prem = visit_result(formula.premise(), func);
77  Formula<Pol> conc = visit_result(formula.conclusion(), func);
78  if ((prem != formula.premise()) || (conc != formula.conclusion())) {
79  newFormula = Formula(IMPLIES, {prem, conc});
80  }
81  break;
82  }
83  case ITE: {
84  Formula<Pol> cond = visit_result(formula.condition(), func);
85  Formula<Pol> fCase = visit_result(formula.first_case(), func);
86  Formula<Pol> sCase = visit_result(formula.second_case(), func);
87  if ((cond != formula.condition()) || (fCase != formula.first_case()) || (sCase != formula.second_case())) {
88  newFormula = Formula(ITE, {cond, fCase, sCase});
89  }
90  break;
91  }
92  case BOOL:
93  case CONSTRAINT:
94  case VARCOMPARE:
95  case VARASSIGN:
96  case BITVECTOR:
97  case TRUE:
98  case FALSE:
99  case UEQ:
100  break;
101  case EXISTS:
102  case FORALL: {
103  Formula<Pol> sub = visit_result(formula.quantified_formula(), func);
104  if (sub != formula.quantified_formula()) {
105  newFormula = Formula<Pol>(formula.type(), formula.quantified_variables(), sub);
106  }
107  break;
108  }
109  }
110  return func(newFormula);
111 }
112 
113 
114 }
carl is the main namespace for the library.
@ CONSTRAINT
@ BITVECTOR
@ VARCOMPARE
@ VARASSIGN
std::vector< Formula< Poly > > Formulas
void visit(const Formula< Pol > &formula, Visitor func)
Recursively calls func on every subformula.
Definition: Visit.h:12
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
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
Definition: Formula.h:47
const Formula & subformula() const
Definition: Formula.h:327
const Formula & premise() const
Definition: Formula.h:336
const Formula & conclusion() const
Definition: Formula.h:345
const Formula & quantified_formula() const
Definition: Formula.h:390
const std::vector< carl::Variable > & quantified_variables() const
Definition: Formula.h:381
const Formulas< Pol > & subformulas() const
Definition: Formula.h:400
const Formula & first_case() const
Definition: Formula.h:363
const Formula & second_case() const
Definition: Formula.h:372
FormulaType type() const
Definition: Formula.h:254
const Formula & condition() const
Definition: Formula.h:354