souffle  2.0.2-371-g6315b36
Constraint.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, 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 Constraint.h
12  *
13  * Contains AST Constraint Analysis Infrastructure for doing constraint analysis on AST objects
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ConstraintSystem.h"
20 #include "ast/Argument.h"
21 #include "ast/Clause.h"
22 #include "ast/Node.h"
23 #include "ast/Variable.h"
24 #include "ast/utility/Visitor.h"
25 #include <map>
26 #include <memory>
27 #include <ostream>
28 #include <string>
29 
30 namespace souffle::ast::analysis {
31 
32 /**
33  * A variable type to be utilized by AST constraint analysis. Each such variable is
34  * associated with an Argument which's property it is describing.
35  *
36  * @tparam PropertySpace the property space associated to the analysis
37  */
38 template <typename PropertySpace>
39 struct ConstraintAnalysisVar : public Variable<const Argument*, PropertySpace> {
40  explicit ConstraintAnalysisVar(const Argument* arg) : Variable<const Argument*, PropertySpace>(arg) {}
41  explicit ConstraintAnalysisVar(const Argument& arg) : Variable<const Argument*, PropertySpace>(&arg) {}
42 
43  /** adds print support */
44  void print(std::ostream& out) const override {
45  out << "var(" << *(this->id) << ")";
46  }
47 };
48 
49 /**
50  * A base class for ConstraintAnalysis collecting constraints for an analysis
51  * by visiting every node of a given AST. The collected constraints are
52  * then utilized to obtain the desired analysis result.
53  *
54  * @tparam AnalysisVar the type of variable (and included property space)
55  * to be utilized by this analysis.
56  */
57 template <typename AnalysisVar>
58 class ConstraintAnalysis : public Visitor<void> {
59 public:
60  using value_type = typename AnalysisVar::property_space::value_type;
61  using constraint_type = std::shared_ptr<Constraint<AnalysisVar>>;
62  using solution_type = std::map<const Argument*, value_type>;
63 
64  virtual void collectConstraints(const Clause& clause) {
65  visitDepthFirstPreOrder(clause, *this);
66  }
67 
68  /**
69  * Runs this constraint analysis on the given clause.
70  *
71  * @param clause the close to be analysed
72  * @param debug a flag enabling the printing of debug information
73  * @return an assignment mapping a property to each argument in the given clause
74  */
75  solution_type analyse(const Clause& clause, std::ostream* debugOutput = nullptr) {
76  collectConstraints(clause);
77 
79 
80  // print debug information if desired
81  if (debugOutput != nullptr) {
82  *debugOutput << "Clause: " << clause << "\n";
83  *debugOutput << "Problem:\n" << constraints << "\n";
84  *debugOutput << "Solution:\n" << assignment << "\n";
85  }
86 
87  // convert assignment to result
88  solution_type solution;
89  visitDepthFirst(clause, [&](const Argument& arg) { solution[&arg] = assignment[getVar(arg)]; });
90  return solution;
91  }
92 
93 protected:
94  /**
95  * A utility function mapping an Argument to its associated analysis variable.
96  *
97  * @param arg the AST argument to be mapped
98  * @return the analysis variable representing its associated value
99  */
100  AnalysisVar getVar(const Argument& arg) {
101  const auto* var = dynamic_cast<const ast::Variable*>(&arg);
102  if (var == nullptr) {
103  // no mapping required
104  return AnalysisVar(arg);
105  }
106 
107  // filter through map => always take the same variable
108  auto res = variables.insert({var->getName(), AnalysisVar(var)}).first;
109  return res->second;
110  }
111 
112  /**
113  * A utility function mapping an Argument to its associated analysis variable.
114  *
115  * @param arg the AST argument to be mapped
116  * @return the analysis variable representing its associated value
117  */
118  AnalysisVar getVar(const Argument* arg) {
119  return getVar(*arg);
120  }
121 
122  /** Adds another constraint to the internally maintained list of constraints */
123  void addConstraint(const constraint_type& constraint) {
124  constraints.add(constraint);
125  }
126 
128 
129  /** The list of constraints making underlying this analysis */
131 
132  /** A map mapping variables to unique instances to facilitate the unification of variables */
133  std::map<std::string, AnalysisVar> variables;
134 };
135 
136 } // namespace souffle::ast::analysis
souffle::ast::analysis::ConstraintAnalysis
A base class for ConstraintAnalysis collecting constraints for an analysis by visiting every node of ...
Definition: Constraint.h:64
souffle::ast::analysis::ConstraintAnalysisVar::ConstraintAnalysisVar
ConstraintAnalysisVar(const Argument *arg)
Definition: Constraint.h:52
souffle::ast::Visitor
The generic base type of all AstVisitors realizing the dispatching of visitor calls.
Definition: Visitor.h:87
souffle::ast::analysis::ConstraintAnalysis::getVar
AnalysisVar getVar(const Argument &arg)
A utility function mapping an Argument to its associated analysis variable.
Definition: Constraint.h:106
souffle::ast::analysis::ConstraintAnalysis::assignment
Assignment< AnalysisVar > assignment
Definition: Constraint.h:133
souffle::ast::analysis::Problem< AnalysisVar >
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::analysis::Variable
A variable to be utilized within constraints to be handled by the constraint solver.
Definition: ConstraintSystem.h:41
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
ConstraintSystem.h
Argument.h
souffle::ast::analysis::Variable< const Argument *, PropertySpace >::id
const Argument * id
the underlying value giving this variable its identity
Definition: ConstraintSystem.h:178
souffle::ast::analysis::ConstraintAnalysis::variables
std::map< std::string, AnalysisVar > variables
A map mapping variables to unique instances to facilitate the unification of variables.
Definition: Constraint.h:139
souffle::ast::analysis::ConstraintAnalysis::addConstraint
void addConstraint(const constraint_type &constraint)
Adds another constraint to the internally maintained list of constraints.
Definition: Constraint.h:129
souffle::ast::analysis::ConstraintAnalysis::solution_type
std::map< const Argument *, value_type > solution_type
Definition: Constraint.h:68
souffle::ast::analysis::Assignment< AnalysisVar >
souffle::ast::analysis::Variable< const Argument *, PropertySpace >::Variable
Variable(const Argument * id)
Definition: ConstraintSystem.h:141
souffle::ast::visitDepthFirstPreOrder
void visitDepthFirstPreOrder(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:235
souffle::ast::analysis::ConstraintAnalysis::collectConstraints
virtual void collectConstraints(const Clause &clause)
Definition: Constraint.h:70
Node.h
souffle::ast::analysis::ConstraintAnalysis::analyse
solution_type analyse(const Clause &clause, std::ostream *debugOutput=nullptr)
Runs this constraint analysis on the given clause.
Definition: Constraint.h:81
souffle::ast::analysis::ConstraintAnalysis::value_type
typename AnalysisVar::property_space::value_type value_type
Definition: Constraint.h:66
Visitor.h
Clause.h
souffle::ast::analysis
Definition: Aggregate.cpp:39
souffle::ast::analysis::ConstraintAnalysisVar::print
void print(std::ostream &out) const override
adds print support
Definition: Constraint.h:56
souffle::ast::analysis::ConstraintAnalysis::constraint_type
std::shared_ptr< Constraint< AnalysisVar > > constraint_type
Definition: Constraint.h:67
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
souffle::ast::analysis::ConstraintAnalysis::constraints
Problem< AnalysisVar > constraints
The list of constraints making underlying this analysis.
Definition: Constraint.h:136
souffle::ast::analysis::Problem::add
void add(const constraint_ptr &constraint)
Adds another constraint to the internally maintained list of constraints.
Definition: ConstraintSystem.h:384
souffle::ast::analysis::Problem::solve
Assignment< Var > solve() const
Computes a solution (minimum fixpoint) for the contained list of constraints.
Definition: ConstraintSystem.h:394
Variable.h