GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/rewriter.h Lines: 1 1 100.0 %
Date: 2021-09-07 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andres Noetzli, Andrew Reynolds, Dejan Jovanovic
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * The Rewriter class.
14
 */
15
16
#include "cvc5_private.h"
17
18
#pragma once
19
20
#include "expr/node.h"
21
#include "proof/method_id.h"
22
#include "theory/theory_rewriter.h"
23
24
namespace cvc5 {
25
26
class TConvProofGenerator;
27
class ProofNodeManager;
28
class TrustNode;
29
30
namespace theory {
31
32
namespace builtin {
33
class BuiltinProofRuleChecker;
34
}
35
36
/**
37
 * The rewrite environment holds everything that the individual rewrites have
38
 * access to.
39
 */
40
class RewriteEnvironment
41
{
42
};
43
44
/**
45
 * The identity rewrite just returns the original node.
46
 *
47
 * @param re The rewrite environment
48
 * @param n The node to rewrite
49
 * @return The original node
50
 */
51
RewriteResponse identityRewrite(RewriteEnvironment* re, TNode n);
52
53
/**
54
 * The main rewriter class.
55
 */
56
10554
class Rewriter {
57
  friend builtin::BuiltinProofRuleChecker;
58
59
 public:
60
  Rewriter();
61
62
  /**
63
   * Rewrites the node using theoryOf() to determine which rewriter to
64
   * use on the node.
65
   */
66
  static Node rewrite(TNode node);
67
68
  /**
69
   * Rewrites the equality node using theoryOf() to determine which rewriter to
70
   * use on the node corresponding to an equality s = t.
71
   *
72
   * Specifically, this method performs rewrites whose conclusion is not
73
   * necessarily one of { s = t, t = s, true, false }, which is an invariant
74
   * guaranted by the above method. This invariant is motivated by theory
75
   * combination, which needs to guarantee that equalities between terms
76
   * can be communicated for all pairs of terms.
77
   */
78
  Node rewriteEqualityExt(TNode node);
79
80
  /**
81
   * Rewrite with proof production, which is managed by the term conversion
82
   * proof generator managed by this class (d_tpg). This method requires a call
83
   * to setProofNodeManager prior to this call.
84
   *
85
   * @param node The node to rewrite.
86
   * @param isExtEq Whether node is an equality which we are applying
87
   * rewriteEqualityExt on.
88
   * @return The trust node of kind TrustNodeKind::REWRITE that contains the
89
   * rewritten form of node.
90
   */
91
  TrustNode rewriteWithProof(TNode node,
92
                             bool isExtEq = false);
93
94
  /** Set proof node manager */
95
  void setProofNodeManager(ProofNodeManager* pnm);
96
97
  /** Garbage collects the rewrite caches. */
98
  void clearCaches();
99
100
  /**
101
   * Registers a theory rewriter with this rewriter. The rewriter does not own
102
   * the theory rewriters.
103
   *
104
   * @param tid The theory that the theory rewriter should be associated with.
105
   * @param trew The theory rewriter to register.
106
   */
107
  void registerTheoryRewriter(theory::TheoryId tid, TheoryRewriter* trew);
108
109
  /** Get the theory rewriter for the given id */
110
  TheoryRewriter* getTheoryRewriter(theory::TheoryId theoryId);
111
112
  /**
113
   * Apply rewrite on n via the rewrite method identifier idr (see method_id.h).
114
   * This encapsulates the exact behavior of a REWRITE step in a proof.
115
   *
116
   * @param n The node to rewrite,
117
   * @param idr The method identifier of the rewriter, by default RW_REWRITE
118
   * specifying a call to rewrite.
119
   * @return The rewritten form of n.
120
   */
121
  Node rewriteViaMethod(TNode n, MethodId idr = MethodId::RW_REWRITE);
122
123
 private:
124
  /**
125
   * Get the rewriter associated with the SmtEngine in scope.
126
   *
127
   * TODO(#3468): Get rid of this function (it relies on there being an
128
   * singleton with the current SmtEngine in scope)
129
   */
130
  static Rewriter* getInstance();
131
132
  /** Returns the appropriate cache for a node */
133
  Node getPreRewriteCache(theory::TheoryId theoryId, TNode node);
134
135
  /** Returns the appropriate cache for a node */
136
  Node getPostRewriteCache(theory::TheoryId theoryId, TNode node);
137
138
  /** Sets the appropriate cache for a node */
139
  void setPreRewriteCache(theory::TheoryId theoryId, TNode node, TNode cache);
140
141
  /** Sets the appropriate cache for a node */
142
  void setPostRewriteCache(theory::TheoryId theoryId, TNode node, TNode cache);
143
144
  /**
145
   * Rewrites the node using the given theory rewriter.
146
   */
147
  Node rewriteTo(theory::TheoryId theoryId,
148
                 Node node,
149
                 TConvProofGenerator* tcpg = nullptr);
150
151
  /** Calls the pre-rewriter for the given theory */
152
  RewriteResponse preRewrite(theory::TheoryId theoryId,
153
                             TNode n,
154
                             TConvProofGenerator* tcpg = nullptr);
155
156
  /** Calls the post-rewriter for the given theory */
157
  RewriteResponse postRewrite(theory::TheoryId theoryId,
158
                              TNode n,
159
                              TConvProofGenerator* tcpg = nullptr);
160
  /** processes a trust rewrite response */
161
  RewriteResponse processTrustRewriteResponse(
162
      theory::TheoryId theoryId,
163
      const TrustRewriteResponse& tresponse,
164
      bool isPre,
165
      TConvProofGenerator* tcpg);
166
167
  /**
168
   * Calls the equality-rewriter for the given theory.
169
   */
170
  Node callRewriteEquality(theory::TheoryId theoryId, TNode equality);
171
172
  void clearCachesInternal();
173
174
  /** Theory rewriters used by this rewriter instance */
175
  TheoryRewriter* d_theoryRewriters[theory::THEORY_LAST];
176
177
  RewriteEnvironment d_re;
178
179
  /** The proof generator */
180
  std::unique_ptr<TConvProofGenerator> d_tpg;
181
#ifdef CVC5_ASSERTIONS
182
  std::unique_ptr<std::unordered_set<Node>> d_rewriteStack = nullptr;
183
#endif /* CVC5_ASSERTIONS */
184
};/* class Rewriter */
185
186
}  // namespace theory
187
}  // namespace cvc5