GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/rewriter.cpp Lines: 151 174 86.8 %
Date: 2021-09-29 Branches: 281 706 39.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, 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 "theory/rewriter.h"
17
18
#include "options/theory_options.h"
19
#include "proof/conv_proof_generator.h"
20
#include "smt/smt_engine.h"
21
#include "smt/smt_engine_scope.h"
22
#include "smt/smt_statistics_registry.h"
23
#include "theory/builtin/proof_checker.h"
24
#include "theory/evaluator.h"
25
#include "theory/quantifiers/extended_rewrite.h"
26
#include "theory/rewriter_tables.h"
27
#include "theory/theory.h"
28
#include "util/resource_manager.h"
29
30
using namespace std;
31
32
namespace cvc5 {
33
namespace theory {
34
35
/** Attribute true for nodes that have been rewritten with proofs enabled */
36
struct RewriteWithProofsAttributeId
37
{
38
};
39
typedef expr::Attribute<RewriteWithProofsAttributeId, bool>
40
    RewriteWithProofsAttribute;
41
42
// Note that this function is a simplified version of Theory::theoryOf for
43
// (type-based) theoryOfMode. We expand and simplify it here for the sake of
44
// efficiency.
45
108640997
static TheoryId theoryOf(TNode node) {
46
108640997
  if (node.getKind() == kind::EQUAL)
47
  {
48
    // Equality is owned by the theory that owns the domain
49
14446222
    return Theory::theoryOf(node[0].getType());
50
  }
51
  // Regular nodes are owned by the kind
52
94194775
  return kindToTheoryId(node.getKind());
53
}
54
55
/**
56
 * TheoryEngine::rewrite() keeps a stack of things that are being pre-
57
 * and post-rewritten.  Each element of the stack is a
58
 * RewriteStackElement.
59
 */
60
92261934
struct RewriteStackElement {
61
  /**
62
   * Construct a fresh stack element.
63
   */
64
27819922
  RewriteStackElement(TNode node, TheoryId theoryId)
65
27819922
      : d_node(node),
66
        d_original(node),
67
        d_theoryId(theoryId),
68
        d_originalTheoryId(theoryId),
69
27819922
        d_nextChild(0)
70
  {
71
27819922
  }
72
73
163205071
  TheoryId getTheoryId() { return static_cast<TheoryId>(d_theoryId); }
74
75
15184620
  TheoryId getOriginalTheoryId()
76
  {
77
15184620
    return static_cast<TheoryId>(d_originalTheoryId);
78
  }
79
80
  /** The node we're currently rewriting */
81
  Node d_node;
82
  /** Original node (either the unrewritten node or the node after prerewriting)
83
   */
84
  Node d_original;
85
  /** Id of the theory that's currently rewriting this node */
86
  unsigned d_theoryId : 8;
87
  /** Id of the original theory that started the rewrite */
88
  unsigned d_originalTheoryId : 8;
89
  /** Index of the child this node is done rewriting */
90
  unsigned d_nextChild : 32;
91
  /** Builder for this node */
92
  NodeBuilder d_builder;
93
};
94
95
96
29133633
Node Rewriter::rewrite(TNode node) {
97
29133633
  if (node.getNumChildren() == 0)
98
  {
99
    // Nodes with zero children should never change via rewriting. We return
100
    // eagerly for the sake of efficiency here.
101
7699369
    return node;
102
  }
103
21434264
  return getInstance()->rewriteTo(theoryOf(node), node);
104
}
105
106
62673
Node Rewriter::callExtendedRewrite(TNode node, bool aggr)
107
{
108
62673
  return getInstance()->extendedRewrite(node, aggr);
109
}
110
111
243145
Node Rewriter::extendedRewrite(TNode node, bool aggr)
112
{
113
486290
  quantifiers::ExtendedRewriter er(*this, aggr);
114
486290
  return er.extendedRewrite(node);
115
}
116
117
43
TrustNode Rewriter::rewriteWithProof(TNode node,
118
                                     bool isExtEq)
119
{
120
  // must set the proof checker before calling this
121
43
  Assert(d_tpg != nullptr);
122
43
  if (isExtEq)
123
  {
124
    // theory rewriter is responsible for rewriting the equality
125
    TheoryRewriter* tr = getInstance()->d_theoryRewriters[theoryOf(node)];
126
    Assert(tr != nullptr);
127
    return tr->rewriteEqualityExtWithProof(node);
128
  }
129
86
  Node ret = getInstance()->rewriteTo(theoryOf(node), node, d_tpg.get());
130
43
  return TrustNode::mkTrustRewrite(node, ret, d_tpg.get());
131
}
132
133
143
void Rewriter::setProofNodeManager(ProofNodeManager* pnm)
134
{
135
  // if not already initialized with proof support
136
143
  if (d_tpg == nullptr)
137
  {
138
143
    Trace("rewriter") << "Rewriter::setProofNodeManager" << std::endl;
139
    // the rewriter is staticly determinstic, thus use static cache policy
140
    // for the term conversion proof generator
141
286
    d_tpg.reset(new TConvProofGenerator(pnm,
142
                                        nullptr,
143
                                        TConvPolicy::FIXPOINT,
144
                                        TConvCachePolicy::STATIC,
145
143
                                        "Rewriter::TConvProofGenerator"));
146
  }
147
143
}
148
149
Node Rewriter::rewriteEqualityExt(TNode node)
150
{
151
  Assert(node.getKind() == kind::EQUAL);
152
  // note we don't force caching of this method currently
153
  return d_theoryRewriters[theoryOf(node)]->rewriteEqualityExt(node);
154
}
155
156
81559
void Rewriter::registerTheoryRewriter(theory::TheoryId tid,
157
                                      TheoryRewriter* trew)
158
{
159
81559
  d_theoryRewriters[tid] = trew;
160
81559
}
161
162
14160
TheoryRewriter* Rewriter::getTheoryRewriter(theory::TheoryId theoryId)
163
{
164
14160
  return d_theoryRewriters[theoryId];
165
}
166
167
21496980
Rewriter* Rewriter::getInstance()
168
{
169
21496980
  return smt::currentSmtEngine()->getRewriter();
170
}
171
172
22394548
Node Rewriter::rewriteTo(theory::TheoryId theoryId,
173
                         Node node,
174
                         TConvProofGenerator* tcpg)
175
{
176
  RewriteWithProofsAttribute rpfa;
177
#ifdef CVC5_ASSERTIONS
178
22394548
  bool isEquality = node.getKind() == kind::EQUAL && (!node[0].getType().isBoolean());
179
180
22394548
  if (d_rewriteStack == nullptr)
181
  {
182
5894
    d_rewriteStack.reset(new std::unordered_set<Node>());
183
  }
184
#endif
185
186
22394548
  Trace("rewriter") << "Rewriter::rewriteTo(" << theoryId << "," << node << ")"<< std::endl;
187
188
  // Check if it's been cached already
189
44789096
  Node cached = getPostRewriteCache(theoryId, node);
190
22394548
  if (!cached.isNull() && (tcpg == nullptr || node.getAttribute(rpfa)))
191
  {
192
18197251
    return cached;
193
  }
194
195
  // Put the node on the stack in order to start the "recursive" rewrite
196
8394594
  vector<RewriteStackElement> rewriteStack;
197
4197297
  rewriteStack.push_back(RewriteStackElement(node, theoryId));
198
199
4197297
  ResourceManager* rm = NULL;
200
4197297
  bool hasSmtEngine = smt::smtEngineInScope();
201
4197297
  if (hasSmtEngine) {
202
4197297
    rm = smt::currentResourceManager();
203
  }
204
  // Rewrite until the stack is empty
205
  for (;;){
206
51442545
    if (hasSmtEngine)
207
    {
208
51442545
      rm->spendResource(Resource::RewriteStep);
209
    }
210
211
    // Get the top of the recursion stack
212
51442545
    RewriteStackElement& rewriteStackTop = rewriteStack.back();
213
214
102885090
    Trace("rewriter") << "Rewriter::rewriting: "
215
102885090
                      << rewriteStackTop.getTheoryId() << ","
216
51442545
                      << rewriteStackTop.d_node << std::endl;
217
218
    // Before rewriting children we need to do a pre-rewrite of the node
219
51442545
    if (rewriteStackTop.d_nextChild == 0)
220
    {
221
      // Check if the pre-rewrite has already been done (it's in the cache)
222
27819922
      cached = getPreRewriteCache(rewriteStackTop.getTheoryId(),
223
                                  rewriteStackTop.d_node);
224
55639844
      if (cached.isNull()
225
27819922
          || (tcpg != nullptr && !rewriteStackTop.d_node.getAttribute(rpfa)))
226
      {
227
        // Rewrite until fix-point is reached
228
        for(;;) {
229
          // Perform the pre-rewrite
230
          RewriteResponse response = preRewrite(
231
9317444
              rewriteStackTop.getTheoryId(), rewriteStackTop.d_node, tcpg);
232
233
          // Put the rewritten node to the top of the stack
234
8558439
          rewriteStackTop.d_node = response.d_node;
235
8558439
          TheoryId newTheory = theoryOf(rewriteStackTop.d_node);
236
          // In the pre-rewrite, if changing theories, we just call the other theories pre-rewrite
237
17116878
          if (newTheory == rewriteStackTop.getTheoryId()
238
8558439
              && response.d_status == REWRITE_DONE)
239
          {
240
7799434
            break;
241
          }
242
759005
          rewriteStackTop.d_theoryId = newTheory;
243
759005
        }
244
245
        // Cache the rewrite
246
7799434
        setPreRewriteCache(rewriteStackTop.getOriginalTheoryId(),
247
                           rewriteStackTop.d_original,
248
                           rewriteStackTop.d_node);
249
      }
250
      // Otherwise we're have already been pre-rewritten (in pre-rewrite cache)
251
      else {
252
        // Continue with the cached version
253
20020488
        rewriteStackTop.d_node = cached;
254
20020488
        rewriteStackTop.d_theoryId = theoryOf(cached);
255
      }
256
    }
257
258
51442545
    rewriteStackTop.d_original = rewriteStackTop.d_node;
259
    // Now it's time to rewrite the children, check if this has already been done
260
51442545
    cached = getPostRewriteCache(rewriteStackTop.getTheoryId(),
261
                                 rewriteStackTop.d_node);
262
    // If not, go through the children
263
102885090
    if (cached.isNull()
264
51442545
        || (tcpg != nullptr && !rewriteStackTop.d_node.getAttribute(rpfa)))
265
    {
266
      // The child we need to rewrite
267
31007813
      unsigned child = rewriteStackTop.d_nextChild++;
268
269
      // To build the rewritten expression we set up the builder
270
31007813
      if(child == 0) {
271
7385190
        if (rewriteStackTop.d_node.getNumChildren() > 0)
272
        {
273
          // The children will add themselves to the builder once they're done
274
6980953
          rewriteStackTop.d_builder << rewriteStackTop.d_node.getKind();
275
6980953
          kind::MetaKind metaKind = rewriteStackTop.d_node.getMetaKind();
276
6980953
          if (metaKind == kind::metakind::PARAMETERIZED) {
277
417273
            rewriteStackTop.d_builder << rewriteStackTop.d_node.getOperator();
278
          }
279
        }
280
      }
281
282
      // Process the next child
283
31007813
      if (child < rewriteStackTop.d_node.getNumChildren())
284
      {
285
        // The child node
286
47245250
        Node childNode = rewriteStackTop.d_node[child];
287
        // Push the rewrite request to the stack (NOTE: rewriteStackTop might be a bad reference now)
288
23622625
        rewriteStack.push_back(RewriteStackElement(childNode, theoryOf(childNode)));
289
        // Go on with the rewriting
290
23622625
        continue;
291
      }
292
293
      // Incorporate the children if necessary
294
7385188
      if (rewriteStackTop.d_node.getNumChildren() > 0)
295
      {
296
13961902
        Node rewritten = rewriteStackTop.d_builder;
297
6980951
        rewriteStackTop.d_node = rewritten;
298
6980951
        rewriteStackTop.d_theoryId = theoryOf(rewriteStackTop.d_node);
299
      }
300
301
      // Done with all pre-rewriting, so let's do the post rewrite
302
      for(;;) {
303
        // Do the post-rewrite
304
        RewriteResponse response = postRewrite(
305
7793728
            rewriteStackTop.getTheoryId(), rewriteStackTop.d_node, tcpg);
306
307
        // We continue with the response we got
308
7589455
        TheoryId newTheoryId = theoryOf(response.d_node);
309
15178910
        if (newTheoryId != rewriteStackTop.getTheoryId()
310
7589455
            || response.d_status == REWRITE_AGAIN_FULL)
311
        {
312
          // In the post rewrite if we've changed theories, we must do a full rewrite
313
960241
          Assert(response.d_node != rewriteStackTop.d_node);
314
          //TODO: this is not thread-safe - should make this assertion dependent on sequential build
315
#ifdef CVC5_ASSERTIONS
316
960241
          Assert(d_rewriteStack->find(response.d_node)
317
                 == d_rewriteStack->end());
318
960241
          d_rewriteStack->insert(response.d_node);
319
#endif
320
1920482
          Node rewritten = rewriteTo(newTheoryId, response.d_node, tcpg);
321
960241
          rewriteStackTop.d_node = rewritten;
322
#ifdef CVC5_ASSERTIONS
323
960241
          d_rewriteStack->erase(response.d_node);
324
#endif
325
960241
          break;
326
        }
327
6629214
        else if (response.d_status == REWRITE_DONE)
328
        {
329
#ifdef CVC5_ASSERTIONS
330
          RewriteResponse r2 =
331
12849890
              d_theoryRewriters[newTheoryId]->postRewrite(response.d_node);
332
12849890
          Assert(r2.d_node == response.d_node)
333
6424945
              << r2.d_node << " != " << response.d_node;
334
#endif
335
6424945
          rewriteStackTop.d_node = response.d_node;
336
6424945
          break;
337
        }
338
        // Check for trivial rewrite loops of size 1 or 2
339
204269
        Assert(response.d_node != rewriteStackTop.d_node);
340
204269
        Assert(d_theoryRewriters[rewriteStackTop.getTheoryId()]
341
                   ->postRewrite(response.d_node)
342
                   .d_node
343
               != rewriteStackTop.d_node);
344
204269
        rewriteStackTop.d_node = response.d_node;
345
204269
      }
346
347
      // We're done with the post rewrite, so we add to the cache
348
7385186
      if (tcpg != nullptr)
349
      {
350
        // if proofs are enabled, mark that we've rewritten with proofs
351
91
        rewriteStackTop.d_original.setAttribute(rpfa, true);
352
91
        if (!cached.isNull())
353
        {
354
          // We may have gotten a different node, due to non-determinism in
355
          // theory rewriters (e.g. quantifiers rewriter which introduces
356
          // fresh BOUND_VARIABLE). This can happen if we wrote once without
357
          // proofs and then rewrote again with proofs.
358
75
          if (rewriteStackTop.d_node != cached)
359
          {
360
            Trace("rewriter-proof") << "WARNING: Rewritten forms with and "
361
                                       "without proofs were not equivalent"
362
                                    << std::endl;
363
            Trace("rewriter-proof")
364
                << "   original: " << rewriteStackTop.d_original << std::endl;
365
            Trace("rewriter-proof")
366
                << "with proofs: " << rewriteStackTop.d_node << std::endl;
367
            Trace("rewriter-proof") << " w/o proofs: " << cached << std::endl;
368
            Node eq = rewriteStackTop.d_node.eqNode(cached);
369
            // we make this a post-rewrite, since we are processing a node that
370
            // has finished post-rewriting above
371
            tcpg->addRewriteStep(rewriteStackTop.d_node,
372
                                 cached,
373
                                 PfRule::TRUST_REWRITE,
374
                                 {},
375
                                 {eq},
376
                                 false);
377
            // don't overwrite the cache, should be the same
378
            rewriteStackTop.d_node = cached;
379
          }
380
        }
381
      }
382
7385186
      setPostRewriteCache(rewriteStackTop.getOriginalTheoryId(),
383
                          rewriteStackTop.d_original,
384
                          rewriteStackTop.d_node);
385
    }
386
    else
387
    {
388
      // We were already in cache, so just remember it
389
20434732
      rewriteStackTop.d_node = cached;
390
20434732
      rewriteStackTop.d_theoryId = theoryOf(cached);
391
    }
392
393
    // If this is the last node, just return
394
27819918
    if (rewriteStack.size() == 1) {
395
4197295
      Assert(!isEquality || rewriteStackTop.d_node.getKind() == kind::EQUAL
396
             || rewriteStackTop.d_node.isConst());
397
4197295
      return rewriteStackTop.d_node;
398
    }
399
400
    // We're done with this node, append it to the parent
401
23622623
    rewriteStack[rewriteStack.size() - 2].d_builder << rewriteStackTop.d_node;
402
23622623
    rewriteStack.pop_back();
403
47245248
  }
404
405
  Unreachable();
406
} /* Rewriter::rewriteTo() */
407
408
8558439
RewriteResponse Rewriter::preRewrite(theory::TheoryId theoryId,
409
                                     TNode n,
410
                                     TConvProofGenerator* tcpg)
411
{
412
8558439
  if (tcpg != nullptr)
413
  {
414
    // call the trust rewrite response interface
415
    TrustRewriteResponse tresponse =
416
344
        d_theoryRewriters[theoryId]->preRewriteWithProof(n);
417
    // process the trust rewrite response: store the proof step into
418
    // tcpg if necessary and then convert to rewrite response.
419
172
    return processTrustRewriteResponse(theoryId, tresponse, true, tcpg);
420
  }
421
8558267
  return d_theoryRewriters[theoryId]->preRewrite(n);
422
}
423
424
7589457
RewriteResponse Rewriter::postRewrite(theory::TheoryId theoryId,
425
                                      TNode n,
426
                                      TConvProofGenerator* tcpg)
427
{
428
7589457
  if (tcpg != nullptr)
429
  {
430
    // same as above, for post-rewrite
431
    TrustRewriteResponse tresponse =
432
182
        d_theoryRewriters[theoryId]->postRewriteWithProof(n);
433
91
    return processTrustRewriteResponse(theoryId, tresponse, false, tcpg);
434
  }
435
7589368
  return d_theoryRewriters[theoryId]->postRewrite(n);
436
}
437
438
263
RewriteResponse Rewriter::processTrustRewriteResponse(
439
    theory::TheoryId theoryId,
440
    const TrustRewriteResponse& tresponse,
441
    bool isPre,
442
    TConvProofGenerator* tcpg)
443
{
444
263
  Assert(tcpg != nullptr);
445
526
  TrustNode trn = tresponse.d_node;
446
263
  Assert(trn.getKind() == TrustNodeKind::REWRITE);
447
526
  Node proven = trn.getProven();
448
263
  if (proven[0] != proven[1])
449
  {
450
85
    ProofGenerator* pg = trn.getGenerator();
451
85
    if (pg == nullptr)
452
    {
453
170
      Node tidn = builtin::BuiltinProofRuleChecker::mkTheoryIdNode(theoryId);
454
      // add small step trusted rewrite
455
      Node rid = mkMethodId(isPre ? MethodId::RW_REWRITE_THEORY_PRE
456
170
                                  : MethodId::RW_REWRITE_THEORY_POST);
457
340
      tcpg->addRewriteStep(proven[0],
458
                           proven[1],
459
                           PfRule::THEORY_REWRITE,
460
                           {},
461
                           {proven, tidn, rid},
462
255
                           isPre);
463
    }
464
    else
465
    {
466
      // store proven rewrite step
467
      tcpg->addRewriteStep(proven[0], proven[1], pg, isPre);
468
    }
469
  }
470
526
  return RewriteResponse(tresponse.d_status, trn.getNode());
471
}
472
473
void Rewriter::clearCaches()
474
{
475
#ifdef CVC5_ASSERTIONS
476
  d_rewriteStack.reset(nullptr);
477
#endif
478
479
  clearCachesInternal();
480
}
481
482
}  // namespace theory
483
22746
}  // namespace cvc5