-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathGround.cpp
More file actions
295 lines (252 loc) · 9.44 KB
/
Ground.cpp
File metadata and controls
295 lines (252 loc) · 9.44 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* 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 Ground.cpp
*
* Implements AST Analysis methods to find the grounded arguments in a clause
*
***********************************************************************/
#include "ast/analysis/Ground.h"
#include "RelationTag.h"
#include "ast/Aggregator.h"
#include "ast/Atom.h"
#include "ast/BinaryConstraint.h"
#include "ast/BranchInit.h"
#include "ast/Clause.h"
#include "ast/Constant.h"
#include "ast/Functor.h"
#include "ast/Negation.h"
#include "ast/RecordInit.h"
#include "ast/Relation.h"
#include "ast/TranslationUnit.h"
#include "ast/TypeCast.h"
#include "ast/analysis/Constraint.h"
#include "ast/analysis/ConstraintSystem.h"
#include "souffle/BinaryConstraintOps.h"
#include "souffle/utility/StreamUtil.h"
#include <algorithm>
#include <map>
#include <memory>
#include <ostream>
#include <set>
#include <utility>
#include <vector>
namespace souffle::ast::analysis {
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 = ConstraintAnalysisVar<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);
}
struct GroundednessAnalysis : public ConstraintAnalysis<BoolDisjunctVar> {
Program& program;
std::set<const Atom*> ignore;
UnorderedQualifiedNameMap<std::set<std::size_t>> latticeAttributes;
bool isLatticeTransformerPass;
GroundednessAnalysis(const TranslationUnit& tu, bool isLatticeTransformerPass)
: program(tu.getProgram()), isLatticeTransformerPass(isLatticeTransformerPass) {
if (isLatticeTransformerPass) {
for (const Relation* rel : program.getRelations()) {
const auto attributes = rel->getAttributes();
const auto& name = rel->getQualifiedName();
for (std::size_t i = 0; i < attributes.size(); i++) {
if (attributes[i]->getIsLattice()) {
latticeAttributes[name].insert(i);
}
}
}
}
}
// atoms are producing grounded variables
void visit_(type_identity<Atom>, const Atom& cur) override {
// some atoms need to be skipped (head or negation)
if (ignore.find(&cur) != ignore.end()) {
return;
}
// all arguments are grounded except lattice arguments
const auto& name = cur.getQualifiedName();
const auto& args = cur.getArguments();
for (std::size_t i = 0; i < cur.getArity(); i++) {
if (!isLatticeTransformerPass || !latticeAttributes.count(name) ||
!latticeAttributes[name].count(i)) {
addConstraint(isTrue(getVar(args[i])));
}
}
}
// negations need to be skipped
void visit_(type_identity<Negation>, const Negation& cur) override {
// add nested atom to black-list
ignore.insert(cur.getAtom());
}
// also skip head if we don't have an inline qualifier
void visit_(type_identity<Clause>, const Clause& clause) override {
auto relation = program.getRelation(clause);
// 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(clause.getHead());
}
}
// binary equality relations propagates groundness
void visit_(type_identity<BinaryConstraint>, const BinaryConstraint& cur) override {
// only target equality
if (!isEqConstraint(cur.getBaseOperator())) {
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 visit_(type_identity<RecordInit>, const RecordInit& 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));
}
void visit_(type_identity<BranchInit>, const BranchInit& adt) override {
auto branchVar = getVar(adt);
std::vector<BoolDisjunctVar> argVars;
// If the branch is grounded so are its arguments.
for (const auto* arg : adt.getArguments()) {
auto argVar = getVar(arg);
addConstraint(imply(branchVar, argVar));
argVars.push_back(argVar);
}
// if all arguments are grounded so is the branch.
addConstraint(imply(argVars, branchVar));
}
// Constants are also sources of grounded values
void visit_(type_identity<Constant>, const Constant& constant) override {
addConstraint(isTrue(getVar(constant)));
}
// Aggregators are grounding values
void visit_(type_identity<Aggregator>, const Aggregator& aggregator) override {
addConstraint(isTrue(getVar(aggregator)));
}
// Functors with grounded values are grounded values
void visit_(type_identity<Functor>, const Functor& functor) override {
auto var = getVar(functor);
std::vector<BoolDisjunctVar> varArgs;
for (const auto& arg : functor.getArguments()) {
varArgs.push_back(getVar(arg));
}
addConstraint(imply(varArgs, var));
}
// casts propogate groundedness in and out
void visit_(type_identity<TypeCast>, const ast::TypeCast& cast) override {
addConstraint(imply(getVar(cast.getValue()), getVar(cast)));
addConstraint(imply(getVar(cast), getVar(cast.getValue())));
}
};
} // namespace
/***
* computes for variables in the clause whether they are grounded
*/
std::map<const Argument*, bool> getGroundedTerms(
const TranslationUnit& tu, const Clause& clause, bool isLatticeTransformerPass) {
// run analysis on given clause
return GroundednessAnalysis(tu, isLatticeTransformerPass).analyse(clause);
}
} // namespace souffle::ast::analysis