45 Program& program = translationUnit.getProgram();
48 auto isExistentialAtom = [&](
const Atom& atom) {
49 for (
Argument* arg : atom.getArguments()) {
50 if (!isA<UnnamedVariable>(arg)) {
70 std::set<QualifiedName> minimalIrreducibleRelations;
77 minimalIrreducibleRelations.insert(
relation->getQualifiedName());
82 if (atom.getQualifiedName() == clause->getHead()->getQualifiedName()) {
86 if (!isExistentialAtom(atom)) {
89 relationGraph.
insert(clause->getHead()->getQualifiedName(), atom.getQualifiedName());
93 minimalIrreducibleRelations.insert(atom.getQualifiedName());
104 aggr, [&](
const Atom& atom) { minimalIrreducibleRelations.insert(atom.getQualifiedName()); });
110 std::set<QualifiedName> irreducibleRelations;
111 for (QualifiedName relationName : minimalIrreducibleRelations) {
113 relationName, [&](
const QualifiedName& subRel) { irreducibleRelations.insert(subRel); });
117 std::set<QualifiedName> existentialRelations;
118 for (Relation*
relation : program.getRelations()) {
120 irreducibleRelations.find(
relation->getQualifiedName()) == irreducibleRelations.end()) {
121 existentialRelations.insert(
relation->getQualifiedName());
126 for (QualifiedName relationName : existentialRelations) {
127 Relation* originalRelation =
getRelation(program, relationName);
129 std::stringstream newRelationName;
130 newRelationName <<
"+?exists_" << relationName;
132 auto newRelation = mk<Relation>();
133 newRelation->setQualifiedName(newRelationName.str());
134 newRelation->setSrcLoc(originalRelation->getSrcLoc());
142 for (Clause* clause :
getClauses(program, *originalRelation)) {
144 auto newClause = mk<Clause>();
146 newClause->setSrcLoc(clause->getSrcLoc());
147 if (
const ExecutionPlan* plan = clause->getExecutionPlan()) {
150 newClause->setHead(mk<Atom>(newRelationName.str()));
151 for (Literal* lit : clause->getBodyLiterals()) {
155 program.addClause(std::move(newClause));
159 program.addRelation(std::move(newRelation));
164 struct renameExistentials :
public NodeMapper {
165 const std::set<QualifiedName>&
relations;
169 Own<Node> operator()(Own<Node> node)
const override {
170 if (
auto* clause =
dynamic_cast<Clause*
>(node.get())) {
175 }
else if (
auto* atom =
dynamic_cast<Atom*
>(node.get())) {
178 std::stringstream newName;
179 newName <<
"+?exists_" << atom->getQualifiedName();
180 return mk<Atom>(newName.str());
188 renameExistentials update(existentialRelations);
189 program.apply(update);
191 bool changed = !existentialRelations.empty();