souffle  2.0.2-371-g6315b36
ClauseNormalisation.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, 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 ClauseNormalisation.h
12  *
13  * Defines a clause-normalisation analysis, providing a normal form for
14  * each clause in the program.
15  *
16  ***********************************************************************/
17 
18 #pragma once
19 
20 #include "ast/Argument.h"
21 #include "ast/Atom.h"
22 #include "ast/Clause.h"
23 #include "ast/Literal.h"
24 #include "ast/QualifiedName.h"
25 #include "ast/analysis/Analysis.h"
26 #include <cstddef>
27 #include <iosfwd>
28 #include <map>
29 #include <set>
30 #include <string>
31 #include <vector>
32 
33 namespace souffle::ast {
34 
35 class TranslationUnit;
36 
37 namespace analysis {
38 
39 class NormalisedClause {
40 public:
41  struct NormalisedClauseElement {
42  QualifiedName name;
43  std::vector<std::string> params;
44  };
45 
46  NormalisedClause() = default;
47 
48  NormalisedClause(const Clause* clause);
49 
50  bool isFullyNormalised() const {
51  return fullyNormalised;
52  }
53 
54  const std::set<std::string>& getVariables() const {
55  return variables;
56  }
57 
58  const std::set<std::string>& getConstants() const {
59  return constants;
60  }
61 
62  const std::vector<NormalisedClauseElement>& getElements() const {
63  return clauseElements;
64  }
65 
66 private:
67  bool fullyNormalised{true};
68  size_t aggrScopeCount{0};
69  std::set<std::string> variables{};
70  std::set<std::string> constants{};
71  std::vector<NormalisedClauseElement> clauseElements{};
72 
73  /**
74  * Parse an atom with a preset name qualifier into the element list.
75  */
76  void addClauseAtom(const std::string& qualifier, const std::string& scopeID, const Atom* atom);
77 
78  /**
79  * Parse a body literal into the element list.
80  */
81  void addClauseBodyLiteral(const std::string& scopeID, const Literal* lit);
82 
83  /**
84  * Return a normalised string repr of an argument.
85  */
86  std::string normaliseArgument(const Argument* arg);
87 };
88 
90 public:
91  static constexpr const char* name = "clause-normalisation";
92 
94 
95  void run(const TranslationUnit& translationUnit) override;
96 
97  void print(std::ostream& os) const override;
98 
99  const NormalisedClause& getNormalisation(const Clause* clause) const;
100 
101 private:
102  std::map<const Clause*, NormalisedClause> normalisations;
103 };
104 
105 } // namespace analysis
106 } // namespace souffle::ast
souffle::ast::analysis::NormalisedClause::fullyNormalised
bool fullyNormalised
Definition: ClauseNormalisation.h:74
souffle::ast::analysis::NormalisedClause::getVariables
const std::set< std::string > & getVariables() const
Definition: ClauseNormalisation.h:61
souffle::ast::analysis::NormalisedClause
Definition: ClauseNormalisation.h:46
souffle::ast::analysis::NormalisedClause::isFullyNormalised
bool isFullyNormalised() const
Definition: ClauseNormalisation.h:57
souffle::ast::analysis::NormalisedClause::getElements
const std::vector< NormalisedClauseElement > & getElements() const
Definition: ClauseNormalisation.h:69
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::analysis::ClauseNormalisationAnalysis::print
void print(std::ostream &os) const override
print the analysis result in HTML format
Definition: ClauseNormalisation.cpp:174
souffle::ast::Atom
An atom class.
Definition: Atom.h:51
souffle::ast::analysis::ClauseNormalisationAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: ClauseNormalisation.cpp:166
souffle::ast::analysis::NormalisedClause::NormalisedClause
NormalisedClause()=default
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
Argument.h
souffle::ast::analysis::NormalisedClause::getConstants
const std::set< std::string > & getConstants() const
Definition: ClauseNormalisation.h:65
souffle::ast::analysis::NormalisedClause::NormalisedClauseElement::name
QualifiedName name
Definition: ClauseNormalisation.h:49
souffle::ast::analysis::NormalisedClause::variables
std::set< std::string > variables
Definition: ClauseNormalisation.h:76
souffle::ast::analysis::NormalisedClause::NormalisedClauseElement::params
std::vector< std::string > params
Definition: ClauseNormalisation.h:50
Atom.h
souffle::ast::analysis::ClauseNormalisationAnalysis::normalisations
std::map< const Clause *, NormalisedClause > normalisations
Definition: ClauseNormalisation.h:109
Literal.h
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::analysis::ClauseNormalisationAnalysis::name
static constexpr const char * name
Definition: ClauseNormalisation.h:98
souffle::ast::analysis::ClauseNormalisationAnalysis::ClauseNormalisationAnalysis
ClauseNormalisationAnalysis()
Definition: ClauseNormalisation.h:100
souffle::ast::Literal
Defines an abstract class for literals in a horn clause.
Definition: Literal.h:36
souffle::ast::analysis::NormalisedClause::clauseElements
std::vector< NormalisedClauseElement > clauseElements
Definition: ClauseNormalisation.h:78
souffle::ast::analysis::ClauseNormalisationAnalysis::getNormalisation
const NormalisedClause & getNormalisation(const Clause *clause) const
Definition: ClauseNormalisation.cpp:188
souffle::ast::analysis::Analysis
Abstract class for a AST Analysis.
Definition: Analysis.h:38
souffle::ast::analysis::ClauseNormalisationAnalysis
Definition: ClauseNormalisation.h:96
QualifiedName.h
souffle::ast::analysis::NormalisedClause::constants
std::set< std::string > constants
Definition: ClauseNormalisation.h:77
souffle::ast::analysis::NormalisedClause::aggrScopeCount
size_t aggrScopeCount
Definition: ClauseNormalisation.h:75
Clause.h
souffle::ast
Definition: Aggregator.h:35
Analysis.h
souffle::ast::analysis::NormalisedClause::addClauseBodyLiteral
void addClauseBodyLiteral(const std::string &scopeID, const Literal *lit)
Parse a body literal into the element list.
Definition: ClauseNormalisation.cpp:79
souffle::ast::analysis::NormalisedClause::normaliseArgument
std::string normaliseArgument(const Argument *arg)
Return a normalised string repr of an argument.
Definition: ClauseNormalisation.cpp:103
souffle::ast::analysis::NormalisedClause::addClauseAtom
void addClauseAtom(const std::string &qualifier, const std::string &scopeID, const Atom *atom)
Parse an atom with a preset name qualifier into the element list.
Definition: ClauseNormalisation.cpp:66