carl  24.04
Computer ARithmetic Library
NNF.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Negations.h"
4 #include "aux.h"
5 
6 namespace carl {
7 
8 template<typename Poly>
10  if(formula.type() == carl::FormulaType::TRUE || formula.type() == carl::FormulaType::FALSE){
11  return formula;
12  }
13  if(formula.is_atom()){
14  return formula;
15  }
16  if(formula.is_literal()){
17  return resolve_negation(formula, false, true);
18  }
19 
20  switch(formula.type()){
22  return to_nnf(resolve_negation(formula, false));
24  auto premise = to_nnf(formula.premise().negated());
25  auto conclusion = to_nnf(formula.conclusion());
26  return Formula<Poly>(carl::FormulaType::OR, Formulas<Poly>{ premise, conclusion});
27  }
29  Formulas<Poly> poss;
30  Formulas<Poly> negs;
31  for (const auto& f : formula.subformulas()) {
32  poss.emplace_back(to_nnf(f));
33  negs.emplace_back(to_nnf(f.negated()));
34  }
35  Formula<Poly> pos(carl::FormulaType::AND, std::move(poss));
36  Formula<Poly> neg(carl::FormulaType::AND, std::move(negs));
37  return Formula<Poly>(carl::FormulaType::OR, Formulas<Poly>({ pos, neg }));
38  }
41  const auto& rhs = formula.subformulas().back();
42  Formula<Poly> pos(FormulaType::OR, { to_nnf(lhs), to_nnf(rhs) });
43  Formula<Poly> neg(FormulaType::OR, { to_nnf(!lhs), to_nnf(!rhs) });
45  }
47  Formula<Poly> first(FormulaType::OR, {to_nnf(!formula.condition()), to_nnf(formula.first_case())});
48  Formula<Poly> second(FormulaType::OR, {to_nnf(formula.condition()), to_nnf(formula.second_case())});
49  return Formula<Poly>(carl::FormulaType::AND, Formulas<Poly>({ first, second }));
50  }
52  Formulas<Poly> disjunctions;
53  for(const auto& subformula : formula.subformulas()){
54  disjunctions.push_back(to_nnf(subformula));
55  }
56  return Formula<Poly>(carl::FormulaType::OR, std::move(disjunctions));
57  }
59  Formulas<Poly> conjunctions;
60  for(const auto& subformula : formula.subformulas()){
61  conjunctions.push_back(to_nnf(subformula));
62  }
63  return Formula<Poly>(carl::FormulaType::AND, std::move(conjunctions));
64  }
65  default:
66  assert(false);
68  }
69 
70 }
71 
72 }
carl is the main namespace for the library.
Formula< Poly > to_nnf(const Formula< Poly > &formula)
Definition: NNF.h:9
std::vector< Formula< Poly > > Formulas
Formula< Pol > resolve_negation(const Formula< Pol > &f, bool _keepConstraint=true, bool resolve_varcomp=false)
Resolves the outermost negation of this formula.
Definition: Negations.h:12
Formula< Pol > connectPrecedingSubformulas(const Formula< Pol > &f)
[Auxiliary method]
Definition: aux.h:13
const Formula & premise() const
Definition: Formula.h:336
const Formula & conclusion() const
Definition: Formula.h:345
bool is_atom() const
Definition: Formula.h:541
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
bool is_literal() const
Definition: Formula.h:549
FormulaType type() const
Definition: Formula.h:254
Formula negated() const
Definition: Formula.h:308
const Formula & condition() const
Definition: Formula.h:354