souffle  2.0.2-371-g6315b36
Public Member Functions | Private Member Functions
souffle::ast::transform::ReplaceSingletonVariablesTransformer Class Reference

Transformation pass to replace singleton variables with unnamed variables. More...

#include <ReplaceSingletonVariables.h>

Inheritance diagram for souffle::ast::transform::ReplaceSingletonVariablesTransformer:
Inheritance graph
Collaboration diagram for souffle::ast::transform::ReplaceSingletonVariablesTransformer:
Collaboration graph

Public Member Functions

ReplaceSingletonVariablesTransformerclone () const override
 
std::string getName () const override
 
- Public Member Functions inherited from souffle::ast::transform::Transformer
bool apply (TranslationUnit &translationUnit)
 
virtual ~Transformer ()=default
 

Private Member Functions

bool transform (TranslationUnit &translationUnit) override
 

Detailed Description

Transformation pass to replace singleton variables with unnamed variables.

E.g.: a() :- b(x). -> a() :- b(_).

Definition at line 32 of file ReplaceSingletonVariables.h.

Member Function Documentation

◆ clone()

ReplaceSingletonVariablesTransformer* souffle::ast::transform::ReplaceSingletonVariablesTransformer::clone ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 42 of file ReplaceSingletonVariables.h.

◆ getName()

std::string souffle::ast::transform::ReplaceSingletonVariablesTransformer::getName ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 38 of file ReplaceSingletonVariables.h.

38  :
39  bool transform(TranslationUnit& translationUnit) override;
40 };

◆ transform()

bool souffle::ast::transform::ReplaceSingletonVariablesTransformer::transform ( TranslationUnit translationUnit)
overrideprivatevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 38 of file ReplaceSingletonVariables.cpp.

40  : public NodeMapper {
41  std::set<std::string>& singletons;
42 
43  replaceSingletons(std::set<std::string>& singletons) : singletons(singletons) {}
44 
45  Own<Node> operator()(Own<Node> node) const override {
46  if (auto* var = dynamic_cast<ast::Variable*>(node.get())) {
47  if (singletons.find(var->getName()) != singletons.end()) {
48  return mk<UnnamedVariable>();
49  }
50  }
51  node->apply(*this);
52  return node;
53  }
54  };
55 
56  for (Relation* rel : program.getRelations()) {
57  for (Clause* clause : getClauses(program, *rel)) {
58  std::set<std::string> nonsingletons;
59  std::set<std::string> vars;
60 
61  visitDepthFirst(*clause, [&](const ast::Variable& var) {
62  const std::string& name = var.getName();
63  if (vars.find(name) != vars.end()) {
64  // Variable seen before, so not a singleton variable
65  nonsingletons.insert(name);
66  } else {
67  vars.insert(name);
68  }
69  });
70 
71  std::set<std::string> ignoredVars;
72 
73  // Don't unname singleton variables occurring in records.
74  // TODO (azreika): remove this check once issue #420 is fixed
75  std::set<std::string> recordVars;
76  visitDepthFirst(*clause, [&](const RecordInit& rec) {
77  visitDepthFirst(rec, [&](const ast::Variable& var) { ignoredVars.insert(var.getName()); });
78  });
79 
80  // Don't unname singleton variables occuring in constraints.
81  std::set<std::string> constraintVars;
82  visitDepthFirst(*clause, [&](const Constraint& cons) {
83  visitDepthFirst(cons, [&](const ast::Variable& var) { ignoredVars.insert(var.getName()); });
84  });
85 
86  std::set<std::string> singletons;
87  for (auto& var : vars) {
88  if ((nonsingletons.find(var) == nonsingletons.end()) &&
89  (ignoredVars.find(var) == ignoredVars.end())) {
90  changed = true;
91  singletons.insert(var);
92  }
93  }
94 
95  // Replace the singletons found with underscores
96  replaceSingletons update(singletons);
97  clause->apply(update);
98  }
99  }
100 
101  return changed;
102 }
103 
104 } // namespace souffle::ast::transform

The documentation for this class was generated from the following files:
souffle::ast::transform::ReplaceSingletonVariablesTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Definition: ReplaceSingletonVariables.cpp:38
souffle::ast::getClauses
std::vector< Clause * > getClauses(const Program &program, const QualifiedName &relationName)
Returns a vector of clauses in the program describing the relation with the given name.
Definition: Utils.cpp:77
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
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086