souffle  2.0.2-371-g6315b36
BindingStore.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file BindingStore.h
12  *
13  * Defines the BindingStore class, which can be used to dynamically
14  * determine the set of bound variables within a given clause.
15  *
16  ***********************************************************************/
17 
18 #pragma once
19 
20 #include "ast/Argument.h"
21 #include "ast/Atom.h"
22 #include "ast/Clause.h"
24 #include <cstddef>
25 #include <map>
26 #include <set>
27 #include <string>
28 
29 namespace souffle::ast {
30 
31 class BindingStore {
32 public:
33  BindingStore(const Clause* clause);
34 
35  /**
36  * Mark the given variable as strongly bound.
37  * Strongly bound variables can be used to bind functor arguments.
38  * This is the usual case, e.g. body atom appearances
39  */
40  void bindVariableStrongly(std::string varName) {
41  stronglyBoundVariables.insert(varName);
42 
43  // Some functor dependencies may be reduced
45  }
46 
47  /**
48  * Mark the given variable as weakly bound.
49  * Weakly bound variables cannot be used to bind functor arguments.
50  * E.g. bound head arguments in MST adorned relations
51  */
52  void bindVariableWeakly(std::string varName) {
53  weaklyBoundVariables.insert(varName);
54  }
55 
56  /** Check if a variable is bound */
57  bool isBound(std::string varName) const {
58  return contains(stronglyBoundVariables, varName) || contains(weaklyBoundVariables, varName);
59  }
60 
61  /** Check if an argument is bound */
62  bool isBound(const Argument* arg) const;
63 
64  /** Counts the number of bound arguments in the given atom */
65  size_t numBoundArguments(const Atom* atom) const;
66 
67 private:
68  // Helper types to represent a disjunction of several dependency sets
69  using ConjBindingSet = std::set<std::string>;
70  using DisjBindingSet = std::set<ConjBindingSet>;
71 
72  std::set<std::string> stronglyBoundVariables{};
73  std::set<std::string> weaklyBoundVariables{};
74  std::map<std::string, DisjBindingSet> variableDependencies{};
75 
76  /**
77  * Add a new conjunction of variables as a potential binder for a given variable.
78  * The variable is considered bound if all variables in the conjunction are bound.
79  */
80  void addBindingDependency(std::string variable, ConjBindingSet dependency) {
81  if (!contains(variableDependencies, variable)) {
83  }
84  variableDependencies[variable].insert(dependency);
85  }
86 
87  /** Add binding dependencies formed on lhs by a <lhs> = <rhs> equality constraint. */
89 
90  /** Generate all binding dependencies implied by the constraints within a given clause. */
91  void generateBindingDependencies(const Clause* clause);
92 
93  /** Reduce a conjunctive set of dependencies based on the current bound variable set. */
95 
96  /** Reduce a disjunctive set of variable dependencies based on the current bound variable set. */
97  DisjBindingSet reduceDependency(const DisjBindingSet& origDependency);
98 
99  /** Reduce the full set of dependencies for all tracked variables, binding whatever needs to be bound. */
100  bool reduceDependencies();
101 };
102 
103 } // namespace souffle::ast
souffle::ast::BindingStore::BindingStore
BindingStore(const Clause *clause)
Definition: BindingStore.cpp:42
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::bindVariableStrongly
void bindVariableStrongly(std::string varName)
Mark the given variable as strongly bound.
Definition: BindingStore.h:54
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::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::BindingStore::weaklyBoundVariables
std::set< std::string > weaklyBoundVariables
Definition: BindingStore.h:87
rhs
Own< Argument > rhs
Definition: ResolveAliases.cpp:185
souffle::ast::BindingStore::generateBindingDependencies
void generateBindingDependencies(const Clause *clause)
Generate all binding dependencies implied by the constraints within a given clause.
Definition: BindingStore.cpp:47
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
lhs
Own< Argument > lhs
Definition: ResolveAliases.cpp:184
Argument.h
souffle::ast::BindingStore::stronglyBoundVariables
std::set< std::string > stronglyBoundVariables
Definition: BindingStore.h:86
ContainerUtil.h
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
Atom.h
souffle::ast::BindingStore::bindVariableWeakly
void bindVariableWeakly(std::string varName)
Mark the given variable as weakly bound.
Definition: BindingStore.h:66
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
Clause.h
souffle::ast
Definition: Aggregator.h:35
souffle::ast::BindingStore::variableDependencies
std::map< std::string, DisjBindingSet > variableDependencies
Definition: BindingStore.h:88