GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/rewriter.h Lines: 1 1 100.0 %
Date: 2021-09-15 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
/**
33
 * The main rewriter class.
34
 */
35
10576
class Rewriter {
36
37
 public:
38
  Rewriter();
39
40
  /**
41
   * !!! Temporary until static access to rewriter is eliminated.
42
   *
43
   * Rewrites the node using theoryOf() to determine which rewriter to
44
   * use on the node.
45
   */
46
  static Node rewrite(TNode node);
47
  /**
48
   * !!! Temporary until static access to rewriter is eliminated.
49
   */
50
  static Node callExtendedRewrite(TNode node, bool aggr = true);
51
52
  /**
53
   * Rewrites the equality node using theoryOf() to determine which rewriter to
54
   * use on the node corresponding to an equality s = t.
55
   *
56
   * Specifically, this method performs rewrites whose conclusion is not
57
   * necessarily one of { s = t, t = s, true, false }, which is an invariant
58
   * guaranted by the above method. This invariant is motivated by theory
59
   * combination, which needs to guarantee that equalities between terms
60
   * can be communicated for all pairs of terms.
61
   */
62
  Node rewriteEqualityExt(TNode node);
63
64
  /**
65
   * Extended rewrite of the given node. This method is implemented by a
66
   * custom ExtendRewriter class that wraps this class to perform custom
67
   * rewrites (usually those that are not useful for solving, but e.g. useful
68
   * for SyGuS symmetry breaking).
69
   * @param node The node to rewrite
70
   * @param aggr Whether to perform aggressive rewrites.
71
   */
72
  Node extendedRewrite(TNode node, bool aggr = true);
73
74
  /**
75
   * Rewrite with proof production, which is managed by the term conversion
76
   * proof generator managed by this class (d_tpg). This method requires a call
77
   * to setProofNodeManager prior to this call.
78
   *
79
   * @param node The node to rewrite.
80
   * @param isExtEq Whether node is an equality which we are applying
81
   * rewriteEqualityExt on.
82
   * @return The trust node of kind TrustNodeKind::REWRITE that contains the
83
   * rewritten form of node.
84
   */
85
  TrustNode rewriteWithProof(TNode node,
86
                             bool isExtEq = false);
87
88
  /** Set proof node manager */
89
  void setProofNodeManager(ProofNodeManager* pnm);
90
91
  /** Garbage collects the rewrite caches. */
92
  void clearCaches();
93
94
  /**
95
   * Registers a theory rewriter with this rewriter. The rewriter does not own
96
   * the theory rewriters.
97
   *
98
   * @param tid The theory that the theory rewriter should be associated with.
99
   * @param trew The theory rewriter to register.
100
   */
101
  void registerTheoryRewriter(theory::TheoryId tid, TheoryRewriter* trew);
102
103
  /** Get the theory rewriter for the given id */
104
  TheoryRewriter* getTheoryRewriter(theory::TheoryId theoryId);
105
106
  /**
107
   * Apply rewrite on n via the rewrite method identifier idr (see method_id.h).
108
   * This encapsulates the exact behavior of a REWRITE step in a proof.
109
   *
110
   * @param n The node to rewrite,
111
   * @param idr The method identifier of the rewriter, by default RW_REWRITE
112
   * specifying a call to rewrite.
113
   * @return The rewritten form of n.
114
   */
115
  Node rewriteViaMethod(TNode n, MethodId idr = MethodId::RW_REWRITE);
116
117
 private:
118
  /**
119
   * Get the rewriter associated with the SmtEngine in scope.
120
   *
121
   * TODO(#3468): Get rid of this function (it relies on there being an
122
   * singleton with the current SmtEngine in scope)
123
   */
124
  static Rewriter* getInstance();
125
126
  /** Returns the appropriate cache for a node */
127
  Node getPreRewriteCache(theory::TheoryId theoryId, TNode node);
128
129
  /** Returns the appropriate cache for a node */
130
  Node getPostRewriteCache(theory::TheoryId theoryId, TNode node);
131
132
  /** Sets the appropriate cache for a node */
133
  void setPreRewriteCache(theory::TheoryId theoryId, TNode node, TNode cache);
134
135
  /** Sets the appropriate cache for a node */
136
  void setPostRewriteCache(theory::TheoryId theoryId, TNode node, TNode cache);
137
138
  /**
139
   * Rewrites the node using the given theory rewriter.
140
   */
141
  Node rewriteTo(theory::TheoryId theoryId,
142
                 Node node,
143
                 TConvProofGenerator* tcpg = nullptr);
144
145
  /** Calls the pre-rewriter for the given theory */
146
  RewriteResponse preRewrite(theory::TheoryId theoryId,
147
                             TNode n,
148
                             TConvProofGenerator* tcpg = nullptr);
149
150
  /** Calls the post-rewriter for the given theory */
151
  RewriteResponse postRewrite(theory::TheoryId theoryId,
152
                              TNode n,
153
                              TConvProofGenerator* tcpg = nullptr);
154
  /** processes a trust rewrite response */
155
  RewriteResponse processTrustRewriteResponse(
156
      theory::TheoryId theoryId,
157
      const TrustRewriteResponse& tresponse,
158
      bool isPre,
159
      TConvProofGenerator* tcpg);
160
161
  /**
162
   * Calls the equality-rewriter for the given theory.
163
   */
164
  Node callRewriteEquality(theory::TheoryId theoryId, TNode equality);
165
166
  void clearCachesInternal();
167
168
  /** Theory rewriters used by this rewriter instance */
169
  TheoryRewriter* d_theoryRewriters[theory::THEORY_LAST];
170
171
  /** The proof generator */
172
  std::unique_ptr<TConvProofGenerator> d_tpg;
173
#ifdef CVC5_ASSERTIONS
174
  std::unique_ptr<std::unordered_set<Node>> d_rewriteStack = nullptr;
175
#endif /* CVC5_ASSERTIONS */
176
};/* class Rewriter */
177
178
}  // namespace theory
179
}  // namespace cvc5