souffle  2.0.2-371-g6315b36
FoldAnonymousRecords.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 FoldAnonymousRecords.h
12  *
13  * Defines AST transformation passes.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/BinaryConstraint.h"
20 #include "ast/Clause.h"
21 #include "ast/Literal.h"
22 #include "ast/TranslationUnit.h"
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 namespace souffle::ast::transform {
30 
31 /**
32  * Transformation pass that removes (binary) constraints on the anonymous records.
33  * After resolving aliases this is equivalent to completely removing anonymous records.
34  *
35  * e.g.
36  * [a, b, c] = [x, y, z] → a = x, b = y, c = z.
37  * [a, b, c] != [x, y, z] → a != x b != y c != z (expanded to three new clauses)
38  *
39  * In a single pass, in case of equalities a transformation expands a single level
40  * of records in every clause. (e.g. [[a]] = [[1]] => [a] = [1])
41  * In case of inequalities, it expands at most a single inequality in every clause
42  *
43  *
44  * This transformation does not resolve aliases.
45  * E.g. A = [a, b], A = [c, d]
46  * Thus it should be called in conjunction with ResolveAnonymousRecordAliases.
47  */
48 class FoldAnonymousRecords : public Transformer {
49 public:
50  std::string getName() const override {
51  return "FoldAnonymousRecords";
52  }
53 
54  FoldAnonymousRecords* clone() const override {
55  return new FoldAnonymousRecords();
56  }
57 
58 private:
59  bool transform(TranslationUnit& translationUnit) override;
60 
61  /**
62  * Process a single clause.
63  *
64  * @parem clause Clause to be processed.
65  * @param newClauses a destination for the newly produced clauses.
66  */
67  void transformClause(const Clause& clause, VecOwn<Clause>& newClauses);
68 
69  /**
70  * Expand constraint on records position-wise.
71  *
72  * eg.
73  * [1, 2, 3] = [a, b, c] => vector(1 = a, 2 = b, 3 = c)
74  * [x, y, z] != [a, b, c] => vector(x != a, x != b, z != c)
75  *
76  * Procedure assumes that argument has a valid operation,
77  * that children are of type RecordInit and that the size
78  * of both sides is the same
79  */
81 
82  /**
83  * Determine if the clause contains at least one binary constraint which can be expanded.
84  */
86 
87  /**
88  * Determine if binary constraint can be expanded.
89  */
90  bool isValidRecordConstraint(const Literal* literal);
91 };
92 
93 } // namespace souffle::ast::transform
TranslationUnit.h
souffle::ast::transform::FoldAnonymousRecords::getName
std::string getName() const override
Definition: FoldAnonymousRecords.h:62
Transformer.h
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::transform::FoldAnonymousRecords::isValidRecordConstraint
bool isValidRecordConstraint(const Literal *literal)
Determine if binary constraint can be expanded.
Definition: FoldAnonymousRecords.cpp:41
souffle::ast::transform::FoldAnonymousRecords::containsValidRecordConstraint
bool containsValidRecordConstraint(const Clause &)
Determine if the clause contains at least one binary constraint which can be expanded.
Definition: FoldAnonymousRecords.cpp:70
souffle::ast::transform::FoldAnonymousRecords::clone
FoldAnonymousRecords * clone() const override
Definition: FoldAnonymousRecords.h:66
ContainerUtil.h
souffle::ast::transform::FoldAnonymousRecords
Transformation pass that removes (binary) constraints on the anonymous records.
Definition: FoldAnonymousRecords.h:54
Literal.h
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::Literal
Defines an abstract class for literals in a horn clause.
Definition: Literal.h:36
souffle::ast::transform
Definition: Program.h:45
souffle::ast::transform::FoldAnonymousRecords::transform
bool transform(TranslationUnit &translationUnit) override
Definition: FoldAnonymousRecords.cpp:164
souffle::ast::transform::FoldAnonymousRecords::transformClause
void transformClause(const Clause &clause, VecOwn< Clause > &newClauses)
Process a single clause.
Definition: FoldAnonymousRecords.cpp:110
souffle::ast::BinaryConstraint
Binary constraint class.
Definition: BinaryConstraint.h:53
souffle::ast::transform::FoldAnonymousRecords::expandRecordBinaryConstraint
VecOwn< Literal > expandRecordBinaryConstraint(const BinaryConstraint &)
Expand constraint on records position-wise.
Definition: FoldAnonymousRecords.cpp:78
Clause.h
BinaryConstraint.h
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45