souffle  2.0.2-371-g6315b36
Public Member Functions | Private Types | Private Member Functions | Private Attributes
souffle::ast::BindingStore Class Reference

#include <BindingStore.h>

Collaboration diagram for souffle::ast::BindingStore:
Collaboration graph

Public Member Functions

 BindingStore (const Clause *clause)
 
void bindVariableStrongly (std::string varName)
 Mark the given variable as strongly bound. More...
 
void bindVariableWeakly (std::string varName)
 Mark the given variable as weakly bound. More...
 
bool isBound (const Argument *arg) const
 Check if an argument is bound. More...
 
bool isBound (std::string varName) const
 Check if a variable is bound. More...
 
size_t numBoundArguments (const Atom *atom) const
 Counts the number of bound arguments in the given atom. More...
 

Private Types

using ConjBindingSet = std::set< std::string >
 
using DisjBindingSet = std::set< ConjBindingSet >
 

Private Member Functions

void addBindingDependency (std::string variable, ConjBindingSet dependency)
 Add a new conjunction of variables as a potential binder for a given variable. More...
 
void generateBindingDependencies (const Clause *clause)
 Generate all binding dependencies implied by the constraints within a given clause. More...
 
void processEqualityBindings (const Argument *lhs, const Argument *rhs)
 Add binding dependencies formed on lhs by a <lhs> = <rhs> equality constraint. More...
 
bool reduceDependencies ()
 Reduce the full set of dependencies for all tracked variables, binding whatever needs to be bound. More...
 
ConjBindingSet reduceDependency (const ConjBindingSet &origDependency)
 Reduce a conjunctive set of dependencies based on the current bound variable set. More...
 
DisjBindingSet reduceDependency (const DisjBindingSet &origDependency)
 Reduce a disjunctive set of variable dependencies based on the current bound variable set. More...
 

Private Attributes

std::set< std::string > stronglyBoundVariables {}
 
std::map< std::string, DisjBindingSetvariableDependencies {}
 
std::set< std::string > weaklyBoundVariables {}
 

Detailed Description

Definition at line 38 of file BindingStore.h.

Member Typedef Documentation

◆ ConjBindingSet

using souffle::ast::BindingStore::ConjBindingSet = std::set<std::string>
private

Definition at line 83 of file BindingStore.h.

◆ DisjBindingSet

Definition at line 84 of file BindingStore.h.

Constructor & Destructor Documentation

◆ BindingStore()

souffle::ast::BindingStore::BindingStore ( const Clause clause)

Definition at line 42 of file BindingStore.cpp.

43  {
44  bool containsAggregators = false;
45  visitDepthFirst(bc, [&](const Aggregator& /* aggr */) { containsAggregators = true; });

References souffle::ast::BinaryConstraint::getBaseOperator(), souffle::isEqConstraint(), and souffle::ast::visitDepthFirst().

Here is the call graph for this function:

Member Function Documentation

◆ addBindingDependency()

void souffle::ast::BindingStore::addBindingDependency ( std::string  variable,
ConjBindingSet  dependency 
)
inlineprivate

Add a new conjunction of variables as a potential binder for a given variable.

The variable is considered bound if all variables in the conjunction are bound.

Definition at line 94 of file BindingStore.h.

◆ bindVariableStrongly()

void souffle::ast::BindingStore::bindVariableStrongly ( std::string  varName)
inline

Mark the given variable as strongly bound.

Strongly bound variables can be used to bind functor arguments. This is the usual case, e.g. body atom appearances

Definition at line 54 of file BindingStore.h.

57  {
58  return contains(stronglyBoundVariables, varName) || contains(weaklyBoundVariables, varName);
59  }

◆ bindVariableWeakly()

void souffle::ast::BindingStore::bindVariableWeakly ( std::string  varName)
inline

Mark the given variable as weakly bound.

Weakly bound variables cannot be used to bind functor arguments. E.g. bound head arguments in MST adorned relations

Definition at line 66 of file BindingStore.h.

67  :
68  // Helper types to represent a disjunction of several dependency sets

Referenced by souffle::ast::transform::MagicSetTransformer::AdornDatabaseTransformer::adornClause().

◆ generateBindingDependencies()

void souffle::ast::BindingStore::generateBindingDependencies ( const Clause clause)
private

Generate all binding dependencies implied by the constraints within a given clause.

Definition at line 47 of file BindingStore.cpp.

52  : relevantEqConstraints) {
53  processEqualityBindings(eqConstraint->getLHS(), eqConstraint->getRHS());
54  processEqualityBindings(eqConstraint->getRHS(), eqConstraint->getLHS());
55  }
56 }
57 
58 void BindingStore::processEqualityBindings(const Argument* lhs, const Argument* rhs) {
59  // Only care about equalities affecting the bound status of variables
60  const auto* var = dynamic_cast<const Variable*>(lhs);
61  if (var == nullptr) return;
62 
63  // If all variables on the rhs are bound, then lhs is also bound

◆ isBound() [1/2]

bool souffle::ast::BindingStore::isBound ( const Argument arg) const

Check if an argument is bound.

Definition at line 149 of file BindingStore.cpp.

152  {
153  return true;
154  } else {
155  return false;
156  }
157 }
158 
159 size_t BindingStore::numBoundArguments(const Atom* atom) const {
160  size_t count = 0;
161  for (const auto* arg : atom->getArguments()) {
162  if (isBound(arg)) {
163  count++;
164  }

◆ isBound() [2/2]

bool souffle::ast::BindingStore::isBound ( std::string  varName) const
inline

Check if a variable is bound.

Definition at line 71 of file BindingStore.h.

72  {};
73  std::set<std::string> weaklyBoundVariables{};

◆ numBoundArguments()

size_t souffle::ast::BindingStore::numBoundArguments ( const Atom atom) const

Counts the number of bound arguments in the given atom.

Definition at line 166 of file BindingStore.cpp.

◆ processEqualityBindings()

void souffle::ast::BindingStore::processEqualityBindings ( const Argument lhs,
const Argument rhs 
)
private

Add binding dependencies formed on lhs by a <lhs> = <rhs> equality constraint.

Definition at line 65 of file BindingStore.cpp.

65  { depSet.insert(subVar.getName()); });
66  addBindingDependency(var->getName(), depSet);
67 
68  // If the lhs is bound, then all args in the rec on the rhs are also bound
69  if (const auto* rec = dynamic_cast<const RecordInit*>(rhs)) {
70  for (const auto* arg : rec->getArguments()) {
71  const auto* subVar = dynamic_cast<const Variable*>(arg);
72  assert(subVar != nullptr && "expected args to be variables");
73  addBindingDependency(subVar->getName(), BindingStore::ConjBindingSet({var->getName()}));
74  }
75  }
76 }
77 
79  const BindingStore::ConjBindingSet& origDependency) {
80  BindingStore::ConjBindingSet newDependency;
81  for (const auto& var : origDependency) {
82  // Only keep unbound variables in the dependency
83  if (!contains(stronglyBoundVariables, var)) {

◆ reduceDependencies()

bool souffle::ast::BindingStore::reduceDependencies ( )
private

Reduce the full set of dependencies for all tracked variables, binding whatever needs to be bound.

Definition at line 109 of file BindingStore.cpp.

110  {
111  changed = true;
112  continue;
113  }
114 
115  // Reduce the dependency set based on bound variables
116  auto newDependencies = reduceDependency(dependencies);
117  if (newDependencies.empty() || newDependencies.size() < dependencies.size()) {
118  // At least one dependency has been satisfied, so variable is now bound
119  changed = true;
120  variablesToBind.insert(headVar);
121  continue;
122  }
123  newVariableDependencies[headVar] = newDependencies;
124  changed |= (newDependencies != dependencies);
125  }
126 
127  // Bind variables that need to be bound
128  for (auto var : variablesToBind) {
129  stronglyBoundVariables.insert(var);
130  }
131 
132  // Repeat it recursively if any changes happened, until we reach a fixpoint
133  if (changed) {
134  variableDependencies = newVariableDependencies;
136  return true;
137  }
138  assert(variableDependencies == newVariableDependencies && "unexpected change");
139  return false;
140 }
141 
142 bool BindingStore::isBound(const Argument* arg) const {
143  if (const auto* var = dynamic_cast<const Variable*>(arg)) {
144  return isBound(var->getName());
145  } else if (const auto* term = dynamic_cast<const Term*>(arg)) {
146  for (const auto* subArg : term->getArguments()) {
147  if (!isBound(subArg)) {

◆ reduceDependency() [1/2]

BindingStore::ConjBindingSet souffle::ast::BindingStore::reduceDependency ( const ConjBindingSet origDependency)
private

Reduce a conjunctive set of dependencies based on the current bound variable set.

Definition at line 85 of file BindingStore.cpp.

91  {
92  BindingStore::DisjBindingSet newDependencies;
93  for (const auto& dep : origDependency) {
94  auto newDep = reduceDependency(dep);
95  if (!newDep.empty()) {

◆ reduceDependency() [2/2]

BindingStore::DisjBindingSet souffle::ast::BindingStore::reduceDependency ( const DisjBindingSet origDependency)
private

Reduce a disjunctive set of variable dependencies based on the current bound variable set.

Definition at line 97 of file BindingStore.cpp.

102  {
103  bool changed = false;
104  std::map<std::string, BindingStore::DisjBindingSet> newVariableDependencies;
105  std::set<std::string> variablesToBind;
106 
107  // Reduce each variable's set of dependencies one by one

Field Documentation

◆ stronglyBoundVariables

std::set<std::string> souffle::ast::BindingStore::stronglyBoundVariables {}
private

Definition at line 86 of file BindingStore.h.

◆ variableDependencies

std::map<std::string, DisjBindingSet> souffle::ast::BindingStore::variableDependencies {}
private

Definition at line 88 of file BindingStore.h.

◆ weaklyBoundVariables

std::set<std::string> souffle::ast::BindingStore::weaklyBoundVariables {}
private

Definition at line 87 of file BindingStore.h.


The documentation for this class was generated from the following files:
souffle::contains
bool contains(const C &container, const typename C::value_type &element)
A utility to check generically whether a given element is contained in a given container.
Definition: ContainerUtil.h:75
souffle::ast::BindingStore::reduceDependency
ConjBindingSet reduceDependency(const ConjBindingSet &origDependency)
Reduce a conjunctive set of dependencies based on the current bound variable set.
Definition: BindingStore.cpp:85
souffle::ast::BindingStore::DisjBindingSet
std::set< ConjBindingSet > DisjBindingSet
Definition: BindingStore.h:84
souffle::ast::BindingStore::weaklyBoundVariables
std::set< std::string > weaklyBoundVariables
Definition: BindingStore.h:87
rhs
Own< Argument > rhs
Definition: ResolveAliases.cpp:185
lhs
Own< Argument > lhs
Definition: ResolveAliases.cpp:184
souffle::ast::BindingStore::stronglyBoundVariables
std::set< std::string > stronglyBoundVariables
Definition: BindingStore.h:86
souffle::ast::BindingStore::ConjBindingSet
std::set< std::string > ConjBindingSet
Definition: BindingStore.h:83
souffle::ast::BindingStore::reduceDependencies
bool reduceDependencies()
Reduce the full set of dependencies for all tracked variables, binding whatever needs to be bound.
Definition: BindingStore.cpp:109
souffle::test::count
int count(const C &c)
Definition: table_test.cpp:40
souffle::ast::BindingStore::isBound
bool isBound(std::string varName) const
Check if a variable is bound.
Definition: BindingStore.h:71
souffle::ast::BindingStore::processEqualityBindings
void processEqualityBindings(const Argument *lhs, const Argument *rhs)
Add binding dependencies formed on lhs by a <lhs> = <rhs> equality constraint.
Definition: BindingStore.cpp:65
souffle::ast::BindingStore::addBindingDependency
void addBindingDependency(std::string variable, ConjBindingSet dependency)
Add a new conjunction of variables as a potential binder for a given variable.
Definition: BindingStore.h:94
souffle::ast::BindingStore::numBoundArguments
size_t numBoundArguments(const Atom *atom) const
Counts the number of bound arguments in the given atom.
Definition: BindingStore.cpp:166
souffle::ast::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the ast rooted by the given node recursively in a depth-...
Definition: Visitor.h:273
souffle::ast::BindingStore::variableDependencies
std::map< std::string, DisjBindingSet > variableDependencies
Definition: BindingStore.h:88