-
Notifications
You must be signed in to change notification settings - Fork 192
/
Ground.cpp
253 lines (214 loc) · 7.91 KB
/
Ground.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2020, The Souffle Developers. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/
/************************************************************************
*
* @file GroundAnalysis.cpp
*
* Implements AST Analysis methods to find the grounded arguments in a clause
*
***********************************************************************/
#include "ast/analysis/Ground.h"
#include "BinaryConstraintOps.h"
#include "Constraints.h"
#include "RelationTag.h"
#include "ast/Argument.h"
#include "ast/Clause.h"
#include "ast/Literal.h"
#include "ast/Relation.h"
#include "ast/TranslationUnit.h"
#include "ast/analysis/Constraint.h"
#include "ast/analysis/RelationDetailCache.h"
#include "utility/StreamUtil.h"
#include <algorithm>
#include <map>
#include <memory>
#include <ostream>
#include <set>
#include <utility>
#include <vector>
namespace souffle {
namespace {
// -----------------------------------------------------------------------------
// Boolean Disjunct Lattice
// -----------------------------------------------------------------------------
/**
* The disjunct meet operator, aka boolean or.
*/
struct bool_or {
bool operator()(bool& a, bool b) const {
bool t = a;
a = a || b;
return t != a;
}
};
/**
* A factory producing the value false.
*/
struct false_factory {
bool operator()() const {
return false;
}
};
/**
* The definition of a lattice utilizing the boolean values {true} and {false} as
* its value set and the || operation as its meet operation. Correspondingly,
* the bottom value is {false} and the top value {true}.
*/
struct bool_disjunct_lattic : public property_space<bool, bool_or, false_factory> {};
/** A base type for analysis based on the boolean disjunct lattice */
using BoolDisjunctVar = AstConstraintAnalysisVar<bool_disjunct_lattic>;
/** A base type for constraints on the boolean disjunct lattice */
using BoolDisjunctConstraint = std::shared_ptr<Constraint<BoolDisjunctVar>>;
/**
* A constraint factory for a constraint ensuring that the value assigned to the
* given variable is (at least) {true}
*/
BoolDisjunctConstraint isTrue(const BoolDisjunctVar& var) {
struct C : public Constraint<BoolDisjunctVar> {
BoolDisjunctVar var;
C(BoolDisjunctVar var) : var(std::move(var)) {}
bool update(Assignment<BoolDisjunctVar>& ass) const override {
auto res = !ass[var];
ass[var] = true;
return res;
}
void print(std::ostream& out) const override {
out << var << " is true";
}
};
return std::make_shared<C>(var);
}
/**
* A constraint factory for a constraint ensuring the constraint
*
* a ⇒ b
*
* Hence, whenever a is mapped to {true}, so is b.
*/
BoolDisjunctConstraint imply(const BoolDisjunctVar& a, const BoolDisjunctVar& b) {
return sub(a, b, "⇒");
}
/**
* A constraint factory for a constraint ensuring the constraint
*
* vars[0] ∧ vars[1] ∧ ... ∧ vars[n] ⇒ res
*
* Hence, whenever all variables vars[i] are mapped to true, so is res.
*/
BoolDisjunctConstraint imply(const std::vector<BoolDisjunctVar>& vars, const BoolDisjunctVar& res) {
struct C : public Constraint<BoolDisjunctVar> {
BoolDisjunctVar res;
std::vector<BoolDisjunctVar> vars;
C(BoolDisjunctVar res, std::vector<BoolDisjunctVar> vars)
: res(std::move(res)), vars(std::move(vars)) {}
bool update(Assignment<BoolDisjunctVar>& ass) const override {
bool r = ass[res];
if (r) {
return false;
}
for (const auto& cur : vars) {
if (!ass[cur]) {
return false;
}
}
ass[res] = true;
return true;
}
void print(std::ostream& out) const override {
out << join(vars, " ∧ ") << " ⇒ " << res;
}
};
return std::make_shared<C>(res, vars);
}
} // namespace
/***
* computes for variables in the clause whether they are grounded
*/
std::map<const AstArgument*, bool> getGroundedTerms(const AstTranslationUnit& tu, const AstClause& clause) {
struct Analysis : public AstConstraintAnalysis<BoolDisjunctVar> {
const RelationDetailCacheAnalysis& relCache;
std::set<const AstAtom*> ignore;
Analysis(const AstTranslationUnit& tu) : relCache(*tu.getAnalysis<RelationDetailCacheAnalysis>()) {}
// atoms are producing grounded variables
void visitAtom(const AstAtom& cur) override {
// some atoms need to be skipped (head or negation)
if (ignore.find(&cur) != ignore.end()) {
return;
}
// all arguments are grounded
for (const auto& arg : cur.getArguments()) {
addConstraint(isTrue(getVar(arg)));
}
}
// negations need to be skipped
void visitNegation(const AstNegation& cur) override {
// add nested atom to black-list
ignore.insert(cur.getAtom());
}
// also skip head if we don't have an inline qualifier
void visitClause(const AstClause& clause) override {
if (auto clauseHead = clause.getHead()) {
auto relation = relCache.getRelation(clauseHead->getQualifiedName());
// Only skip the head if the relation ISN'T inline. Keeping the head will ground
// any mentioned variables, allowing us to pretend they're grounded.
if (!(relation && relation->hasQualifier(RelationQualifier::INLINE))) {
ignore.insert(clauseHead);
}
}
}
// binary equality relations propagates groundness
void visitBinaryConstraint(const AstBinaryConstraint& cur) override {
// only target equality
if (!isEqConstraint(cur.getOperator())) {
return;
}
// if equal, link right and left side
auto lhs = getVar(cur.getLHS());
auto rhs = getVar(cur.getRHS());
addConstraint(imply(lhs, rhs));
addConstraint(imply(rhs, lhs));
}
// record init nodes
void visitRecordInit(const AstRecordInit& init) override {
auto cur = getVar(init);
std::vector<BoolDisjunctVar> vars;
// if record is grounded, so are all its arguments
for (const auto& arg : init.getArguments()) {
auto arg_var = getVar(arg);
addConstraint(imply(cur, arg_var));
vars.push_back(arg_var);
}
// if all arguments are grounded, so is the record
addConstraint(imply(vars, cur));
}
// constants are also sources of grounded values
void visitConstant(const AstConstant& c) override {
addConstraint(isTrue(getVar(c)));
}
// aggregators are grounding values
void visitAggregator(const AstAggregator& c) override {
addConstraint(isTrue(getVar(c)));
}
// functors with grounded values are grounded values
void visitFunctor(const AstFunctor& cur) override {
auto fun = getVar(cur);
std::vector<BoolDisjunctVar> varArgs;
for (const auto& arg : cur.getArguments()) {
varArgs.push_back(getVar(arg));
}
addConstraint(imply(varArgs, fun));
}
// casts propogate groundedness in and out
void visitTypeCast(const AstTypeCast& cast) override {
addConstraint(imply(getVar(cast.getValue()), getVar(cast)));
}
};
// run analysis on given clause
return Analysis(tu).analyse(clause);
}
} // end of namespace souffle