GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/rewriter.cpp Lines: 166 187 88.8 %
Date: 2021-09-13 Branches: 306 736 41.6 %

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
159764996
static TheoryId theoryOf(TNode node) {
46
159764996
  if (node.getKind() == kind::EQUAL)
47
  {
48
    // Equality is owned by the theory that owns the domain
49
24863354
    return Theory::theoryOf(node[0].getType());
50
  }
51
  // Regular nodes are owned by the kind
52
134901642
  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
133616356
struct RewriteStackElement {
61
  /**
62
   * Construct a fresh stack element.
63
   */
64
39774148
  RewriteStackElement(TNode node, TheoryId theoryId)
65
39774148
      : d_node(node),
66
        d_original(node),
67
        d_theoryId(theoryId),
68
        d_originalTheoryId(theoryId),
69
39774148
        d_nextChild(0)
70
  {
71
39774148
  }
72
73
235723194
  TheoryId getTheoryId() { return static_cast<TheoryId>(d_theoryId); }
74
75
23489136
  TheoryId getOriginalTheoryId()
76
  {
77
23489136
    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
43928507
Node Rewriter::rewrite(TNode node) {
97
43928507
  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
9150335
    return node;
102
  }
103
34778172
  return getInstance()->rewriteTo(theoryOf(node), node);
104
}
105
106
63381
Node Rewriter::callExtendedRewrite(TNode node, bool aggr)
107
{
108
63381
  return getInstance()->extendedRewrite(node, aggr);
109
}
110
111
244013
Node Rewriter::extendedRewrite(TNode node, bool aggr)
112
{
113
488026
  quantifiers::ExtendedRewriter er(*this, aggr);
114
488026
  return er.extendedRewrite(node);
115
}
116
117
61183
TrustNode Rewriter::rewriteWithProof(TNode node,
118
                                     bool isExtEq)
119
{
120
  // must set the proof checker before calling this
121
61183
  Assert(d_tpg != nullptr);
122
61183
  if (isExtEq)
123
  {
124
    // theory rewriter is responsible for rewriting the equality
125
37
    TheoryRewriter* tr = getInstance()->d_theoryRewriters[theoryOf(node)];
126
37
    Assert(tr != nullptr);
127
37
    return tr->rewriteEqualityExtWithProof(node);
128
  }
129
122292
  Node ret = getInstance()->rewriteTo(theoryOf(node), node, d_tpg.get());
130
61146
  return TrustNode::mkTrustRewrite(node, ret, d_tpg.get());
131
}
132
133
3783
void Rewriter::setProofNodeManager(ProofNodeManager* pnm)
134
{
135
  // if not already initialized with proof support
136
3783
  if (d_tpg == nullptr)
137
  {
138
3783
    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
7566
    d_tpg.reset(new TConvProofGenerator(pnm,
142
                                        nullptr,
143
                                        TConvPolicy::FIXPOINT,
144
                                        TConvCachePolicy::STATIC,
145
3783
                                        "Rewriter::TConvProofGenerator"));
146
  }
147
3783
}
148
149
140
Node Rewriter::rewriteEqualityExt(TNode node)
150
{
151
140
  Assert(node.getKind() == kind::EQUAL);
152
  // note we don't force caching of this method currently
153
140
  return d_theoryRewriters[theoryOf(node)]->rewriteEqualityExt(node);
154
}
155
156
128957
void Rewriter::registerTheoryRewriter(theory::TheoryId tid,
157
                                      TheoryRewriter* trew)
158
{
159
128957
  d_theoryRewriters[tid] = trew;
160
128957
}
161
162
14095
TheoryRewriter* Rewriter::getTheoryRewriter(theory::TheoryId theoryId)
163
{
164
14095
  return d_theoryRewriters[theoryId];
165
}
166
167
34902736
Rewriter* Rewriter::getInstance()
168
{
169
34902736
  return smt::currentSmtEngine()->getRewriter();
170
}
171
172
36086097
Node Rewriter::rewriteTo(theory::TheoryId theoryId,
173
                         Node node,
174
                         TConvProofGenerator* tcpg)
175
{
176
  RewriteWithProofsAttribute rpfa;
177
#ifdef CVC5_ASSERTIONS
178
36086097
  bool isEquality = node.getKind() == kind::EQUAL && (!node[0].getType().isBoolean());
179
180
36086097
  if (d_rewriteStack == nullptr)
181
  {
182
9524
    d_rewriteStack.reset(new std::unordered_set<Node>());
183
  }
184
#endif
185
186
36086097
  Trace("rewriter") << "Rewriter::rewriteTo(" << theoryId << "," << node << ")"<< std::endl;
187
188
  // Check if it's been cached already
189
72172194
  Node cached = getPostRewriteCache(theoryId, node);
190
36086097
  if (!cached.isNull() && (tcpg == nullptr || node.getAttribute(rpfa)))
191
  {
192
29517760
    return cached;
193
  }
194
195
  // Put the node on the stack in order to start the "recursive" rewrite
196
13136674
  vector<RewriteStackElement> rewriteStack;
197
6568337
  rewriteStack.push_back(RewriteStackElement(node, theoryId));
198
199
6568337
  ResourceManager* rm = NULL;
200
6568337
  bool hasSmtEngine = smt::smtEngineInScope();
201
6568337
  if (hasSmtEngine) {
202
6568337
    rm = smt::currentResourceManager();
203
  }
204
  // Rewrite until the stack is empty
205
  for (;;){
206
72979957
    if (hasSmtEngine)
207
    {
208
72979957
      rm->spendResource(Resource::RewriteStep);
209
    }
210
211
    // Get the top of the recursion stack
212
72979957
    RewriteStackElement& rewriteStackTop = rewriteStack.back();
213
214
145959914
    Trace("rewriter") << "Rewriter::rewriting: "
215
145959914
                      << rewriteStackTop.getTheoryId() << ","
216
72979957
                      << rewriteStackTop.d_node << std::endl;
217
218
    // Before rewriting children we need to do a pre-rewrite of the node
219
72979957
    if (rewriteStackTop.d_nextChild == 0)
220
    {
221
      // Check if the pre-rewrite has already been done (it's in the cache)
222
39774148
      cached = getPreRewriteCache(rewriteStackTop.getTheoryId(),
223
                                  rewriteStackTop.d_node);
224
79548296
      if (cached.isNull()
225
39774148
          || (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
14347719
              rewriteStackTop.getTheoryId(), rewriteStackTop.d_node, tcpg);
232
233
          // Put the rewritten node to the top of the stack
234
13209067
          rewriteStackTop.d_node = response.d_node;
235
13209067
          TheoryId newTheory = theoryOf(rewriteStackTop.d_node);
236
          // In the pre-rewrite, if changing theories, we just call the other theories pre-rewrite
237
26418134
          if (newTheory == rewriteStackTop.getTheoryId()
238
13209067
              && response.d_status == REWRITE_DONE)
239
          {
240
12070415
            break;
241
          }
242
1138652
          rewriteStackTop.d_theoryId = newTheory;
243
1138652
        }
244
245
        // Cache the rewrite
246
12070415
        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
27703733
        rewriteStackTop.d_node = cached;
254
27703733
        rewriteStackTop.d_theoryId = theoryOf(cached);
255
      }
256
    }
257
258
72979957
    rewriteStackTop.d_original = rewriteStackTop.d_node;
259
    // Now it's time to rewrite the children, check if this has already been done
260
72979957
    cached = getPostRewriteCache(rewriteStackTop.getTheoryId(),
261
                                 rewriteStackTop.d_node);
262
    // If not, go through the children
263
145959914
    if (cached.isNull()
264
72979957
        || (tcpg != nullptr && !rewriteStackTop.d_node.getAttribute(rpfa)))
265
    {
266
      // The child we need to rewrite
267
44624534
      unsigned child = rewriteStackTop.d_nextChild++;
268
269
      // To build the rewritten expression we set up the builder
270
44624534
      if(child == 0) {
271
11418725
        if (rewriteStackTop.d_node.getNumChildren() > 0)
272
        {
273
          // The children will add themselves to the builder once they're done
274
10788230
          rewriteStackTop.d_builder << rewriteStackTop.d_node.getKind();
275
10788230
          kind::MetaKind metaKind = rewriteStackTop.d_node.getMetaKind();
276
10788230
          if (metaKind == kind::metakind::PARAMETERIZED) {
277
690019
            rewriteStackTop.d_builder << rewriteStackTop.d_node.getOperator();
278
          }
279
        }
280
      }
281
282
      // Process the next child
283
44624534
      if (child < rewriteStackTop.d_node.getNumChildren())
284
      {
285
        // The child node
286
66411622
        Node childNode = rewriteStackTop.d_node[child];
287
        // Push the rewrite request to the stack (NOTE: rewriteStackTop might be a bad reference now)
288
33205811
        rewriteStack.push_back(RewriteStackElement(childNode, theoryOf(childNode)));
289
        // Go on with the rewriting
290
33205811
        continue;
291
      }
292
293
      // Incorporate the children if necessary
294
11418723
      if (rewriteStackTop.d_node.getNumChildren() > 0)
295
      {
296
21576456
        Node rewritten = rewriteStackTop.d_builder;
297
10788228
        rewriteStackTop.d_node = rewritten;
298
10788228
        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
11907761
            rewriteStackTop.getTheoryId(), rewriteStackTop.d_node, tcpg);
306
307
        // We continue with the response we got
308
11663239
        TheoryId newTheoryId = theoryOf(response.d_node);
309
23326478
        if (newTheoryId != rewriteStackTop.getTheoryId()
310
11663239
            || response.d_status == REWRITE_AGAIN_FULL)
311
        {
312
          // In the post rewrite if we've changed theories, we must do a full rewrite
313
1246779
          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
1246779
          Assert(d_rewriteStack->find(response.d_node)
317
                 == d_rewriteStack->end());
318
1246779
          d_rewriteStack->insert(response.d_node);
319
#endif
320
2493558
          Node rewritten = rewriteTo(newTheoryId, response.d_node, tcpg);
321
1246779
          rewriteStackTop.d_node = rewritten;
322
#ifdef CVC5_ASSERTIONS
323
1246779
          d_rewriteStack->erase(response.d_node);
324
#endif
325
1246779
          break;
326
        }
327
10416460
        else if (response.d_status == REWRITE_DONE)
328
        {
329
#ifdef CVC5_ASSERTIONS
330
          RewriteResponse r2 =
331
20343884
              d_theoryRewriters[newTheoryId]->postRewrite(response.d_node);
332
20343884
          Assert(r2.d_node == response.d_node)
333
10171942
              << r2.d_node << " != " << response.d_node;
334
#endif
335
10171942
          rewriteStackTop.d_node = response.d_node;
336
10171942
          break;
337
        }
338
        // Check for trivial rewrite loops of size 1 or 2
339
244518
        Assert(response.d_node != rewriteStackTop.d_node);
340
244518
        Assert(d_theoryRewriters[rewriteStackTop.getTheoryId()]
341
                   ->postRewrite(response.d_node)
342
                   .d_node
343
               != rewriteStackTop.d_node);
344
244518
        rewriteStackTop.d_node = response.d_node;
345
244518
      }
346
347
      // We're done with the post rewrite, so we add to the cache
348
11418721
      if (tcpg != nullptr)
349
      {
350
        // if proofs are enabled, mark that we've rewritten with proofs
351
334615
        rewriteStackTop.d_original.setAttribute(rpfa, true);
352
334615
        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
320614
          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
11418721
      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
28355423
      rewriteStackTop.d_node = cached;
390
28355423
      rewriteStackTop.d_theoryId = theoryOf(cached);
391
    }
392
393
    // If this is the last node, just return
394
39774144
    if (rewriteStack.size() == 1) {
395
6568335
      Assert(!isEquality || rewriteStackTop.d_node.getKind() == kind::EQUAL
396
             || rewriteStackTop.d_node.isConst());
397
6568335
      return rewriteStackTop.d_node;
398
    }
399
400
    // We're done with this node, append it to the parent
401
33205809
    rewriteStack[rewriteStack.size() - 2].d_builder << rewriteStackTop.d_node;
402
33205809
    rewriteStack.pop_back();
403
66411620
  }
404
405
  Unreachable();
406
} /* Rewriter::rewriteTo() */
407
408
13209067
RewriteResponse Rewriter::preRewrite(theory::TheoryId theoryId,
409
                                     TNode n,
410
                                     TConvProofGenerator* tcpg)
411
{
412
13209067
  if (tcpg != nullptr)
413
  {
414
    // call the trust rewrite response interface
415
    TrustRewriteResponse tresponse =
416
925932
        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
462966
    return processTrustRewriteResponse(theoryId, tresponse, true, tcpg);
420
  }
421
12746101
  return d_theoryRewriters[theoryId]->preRewrite(n);
422
}
423
424
11663241
RewriteResponse Rewriter::postRewrite(theory::TheoryId theoryId,
425
                                      TNode n,
426
                                      TConvProofGenerator* tcpg)
427
{
428
11663241
  if (tcpg != nullptr)
429
  {
430
    // same as above, for post-rewrite
431
    TrustRewriteResponse tresponse =
432
673626
        d_theoryRewriters[theoryId]->postRewriteWithProof(n);
433
336813
    return processTrustRewriteResponse(theoryId, tresponse, false, tcpg);
434
  }
435
11326430
  return d_theoryRewriters[theoryId]->postRewrite(n);
436
}
437
438
799779
RewriteResponse Rewriter::processTrustRewriteResponse(
439
    theory::TheoryId theoryId,
440
    const TrustRewriteResponse& tresponse,
441
    bool isPre,
442
    TConvProofGenerator* tcpg)
443
{
444
799779
  Assert(tcpg != nullptr);
445
1599558
  TrustNode trn = tresponse.d_node;
446
799779
  Assert(trn.getKind() == TrustNodeKind::REWRITE);
447
1599558
  Node proven = trn.getProven();
448
799779
  if (proven[0] != proven[1])
449
  {
450
194937
    ProofGenerator* pg = trn.getGenerator();
451
194937
    if (pg == nullptr)
452
    {
453
389874
      Node tidn = builtin::BuiltinProofRuleChecker::mkTheoryIdNode(theoryId);
454
      // add small step trusted rewrite
455
      Node rid = mkMethodId(isPre ? MethodId::RW_REWRITE_THEORY_PRE
456
389874
                                  : MethodId::RW_REWRITE_THEORY_POST);
457
779748
      tcpg->addRewriteStep(proven[0],
458
                           proven[1],
459
                           PfRule::THEORY_REWRITE,
460
                           {},
461
                           {proven, tidn, rid},
462
584811
                           isPre);
463
    }
464
    else
465
    {
466
      // store proven rewrite step
467
      tcpg->addRewriteStep(proven[0], proven[1], pg, isPre);
468
    }
469
  }
470
1599558
  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
1673139
Node Rewriter::rewriteViaMethod(TNode n, MethodId idr)
483
{
484
1673139
  if (idr == MethodId::RW_REWRITE)
485
  {
486
1644639
    return rewrite(n);
487
  }
488
28500
  if (idr == MethodId::RW_EXT_REWRITE)
489
  {
490
14
    return extendedRewrite(n);
491
  }
492
28486
  if (idr == MethodId::RW_REWRITE_EQ_EXT)
493
  {
494
140
    return rewriteEqualityExt(n);
495
  }
496
28346
  if (idr == MethodId::RW_EVALUATE)
497
  {
498
    Evaluator eval;
499
28346
    return eval.eval(n, {}, {}, false);
500
  }
501
  if (idr == MethodId::RW_IDENTITY)
502
  {
503
    // does nothing
504
    return n;
505
  }
506
  // unknown rewriter
507
  Unhandled() << "Rewriter::rewriteViaMethod: no rewriter for " << idr
508
              << std::endl;
509
  return n;
510
}
511
512
}  // namespace theory
513
29514
}  // namespace cvc5