summaryrefslogtreecommitdiff
path: root/src/smt/expand_definitions.cpp
blob: 85a2732c9fd01e51b38fb85791168a7ef0ac0c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*********************                                                        */
/*! \file expand_definitions.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Andrew Reynolds, Morgan Deters, Andres Noetzli
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 ** in the top-level source directory and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Implementation of expand definitions for an SMT engine.
 **/

#include "smt/expand_definitions.h"

#include <stack>
#include <utility>

#include "expr/node_manager_attributes.h"
#include "preprocessing/assertion_pipeline.h"
#include "smt/defined_function.h"
#include "smt/smt_engine.h"
#include "smt/smt_engine_stats.h"
#include "theory/theory_engine.h"

using namespace cvc5::preprocessing;
using namespace cvc5::theory;
using namespace cvc5::kind;

namespace cvc5 {
namespace smt {

ExpandDefs::ExpandDefs(SmtEngine& smt,
                       ResourceManager& rm,
                       SmtEngineStatistics& stats)
    : d_smt(smt), d_resourceManager(rm), d_smtStats(stats), d_tpg(nullptr)
{
}

ExpandDefs::~ExpandDefs() {}

Node ExpandDefs::expandDefinitions(
    TNode n,
    std::unordered_map<Node, Node, NodeHashFunction>& cache,
    bool expandOnly)
{
  TrustNode trn = expandDefinitions(n, cache, expandOnly, nullptr);
  return trn.isNull() ? Node(n) : trn.getNode();
}

TrustNode ExpandDefs::expandDefinitions(
    TNode n,
    std::unordered_map<Node, Node, NodeHashFunction>& cache,
    bool expandOnly,
    TConvProofGenerator* tpg)
{
  const TNode orig = n;
  NodeManager* nm = d_smt.getNodeManager();
  std::stack<std::tuple<Node, Node, bool>> worklist;
  std::stack<Node> result;
  worklist.push(std::make_tuple(Node(n), Node(n), false));
  // The worklist is made of triples, each is input / original node then the
  // output / rewritten node and finally a flag tracking whether the children
  // have been explored (i.e. if this is a downward or upward pass).

  do
  {
    d_resourceManager.spendResource(ResourceManager::Resource::PreprocessStep);

    // n is the input / original
    // node is the output / result
    Node node;
    bool childrenPushed;
    std::tie(n, node, childrenPushed) = worklist.top();
    worklist.pop();

    // Working downwards
    if (!childrenPushed)
    {
      Kind k = n.getKind();

      // we can short circuit (variable) leaves
      if (n.isVar())
      {
        SmtEngine::DefinedFunctionMap* dfuns = d_smt.getDefinedFunctionMap();
        SmtEngine::DefinedFunctionMap::const_iterator i = dfuns->find(n);
        if (i != dfuns->end())
        {
          Node f = (*i).second.getFormula();
          const std::vector<Node>& formals = (*i).second.getFormals();
          // replacement must be closed
          if (!formals.empty())
          {
            f = nm->mkNode(LAMBDA, nm->mkNode(BOUND_VAR_LIST, formals), f);
          }
          // are we are producing proofs for this call?
          if (tpg != nullptr)
          {
            // if this is a variable, we can simply assume it
            // ------- ASSUME
            // n = f
            Node conc = n.eqNode(f);
            tpg->addRewriteStep(n, f, PfRule::ASSUME, {}, {conc}, true);
          }
          // must recursively expand its definition
          TrustNode tfe = expandDefinitions(f, cache, expandOnly, tpg);
          Node fe = tfe.isNull() ? f : tfe.getNode();
          // don't bother putting in the cache
          result.push(fe);
          continue;
        }
        // don't bother putting in the cache
        result.push(n);
        continue;
      }

      // maybe it's in the cache
      std::unordered_map<Node, Node, NodeHashFunction>::iterator cacheHit =
          cache.find(n);
      if (cacheHit != cache.end())
      {
        TNode ret = (*cacheHit).second;
        result.push(ret.isNull() ? n : ret);
        continue;
      }

      // otherwise expand it
      bool doExpand = false;
      if (k == APPLY_UF)
      {
        // Always do beta-reduction here. The reason is that there may be
        // operators such as INTS_MODULUS in the body of the lambda that would
        // otherwise be introduced by beta-reduction via the rewriter, but are
        // not expanded here since the traversal in this function does not
        // traverse the operators of nodes. Hence, we beta-reduce here to
        // ensure terms in the body of the lambda are expanded during this
        // call.
        if (n.getOperator().getKind() == LAMBDA)
        {
          doExpand = true;
        }
        else
        {
          // We always check if this operator corresponds to a defined function.
          doExpand = d_smt.isDefinedFunction(n.getOperator());
        }
      }
      // the premise of the proof of expansion (if we are expanding a definition
      // and proofs are enabled)
      std::vector<Node> pfExpChildren;
      if (doExpand)
      {
        std::vector<Node> formals;
        TNode fm;
        if (n.getOperator().getKind() == LAMBDA)
        {
          TNode op = n.getOperator();
          // lambda
          for (unsigned i = 0; i < op[0].getNumChildren(); i++)
          {
            formals.push_back(op[0][i]);
          }
          fm = op[1];
        }
        else
        {
          // application of a user-defined symbol
          TNode func = n.getOperator();
          SmtEngine::DefinedFunctionMap* dfuns = d_smt.getDefinedFunctionMap();
          SmtEngine::DefinedFunctionMap::const_iterator i = dfuns->find(func);
          if (i == dfuns->end())
          {
            throw TypeCheckingExceptionPrivate(
                n,
                std::string("Undefined function: `") + func.toString() + "'");
          }
          DefinedFunction def = (*i).second;
          formals = def.getFormals();

          if (Debug.isOn("expand"))
          {
            Debug("expand") << "found: " << n << std::endl;
            Debug("expand") << " func: " << func << std::endl;
            std::string name = func.getAttribute(expr::VarNameAttr());
            Debug("expand") << "     : \"" << name << "\"" << std::endl;
          }
          if (Debug.isOn("expand"))
          {
            Debug("expand") << " defn: " << def.getFunction() << std::endl
                            << "       [";
            if (formals.size() > 0)
            {
              copy(formals.begin(),
                   formals.end() - 1,
                   std::ostream_iterator<Node>(Debug("expand"), ", "));
              Debug("expand") << formals.back();
            }
            Debug("expand")
                << "]" << std::endl
                << "       " << def.getFunction().getType() << std::endl
                << "       " << def.getFormula() << std::endl;
          }

          fm = def.getFormula();
          // are we producing proofs for this call?
          if (tpg != nullptr)
          {
            Node pfRhs = fm;
            if (!formals.empty())
            {
              Node bvl = nm->mkNode(BOUND_VAR_LIST, formals);
              pfRhs = nm->mkNode(LAMBDA, bvl, pfRhs);
            }
            Assert(func.getType().isComparableTo(pfRhs.getType()));
            pfExpChildren.push_back(func.eqNode(pfRhs));
          }
        }

        Node instance = fm.substitute(formals.begin(),
                                      formals.end(),
                                      n.begin(),
                                      n.begin() + formals.size());
        Debug("expand") << "made : " << instance << std::endl;
        // are we producing proofs for this call?
        if (tpg != nullptr)
        {
          if (n != instance)
          {
            // This code is run both when we are doing expand definitions and
            // simple beta reduction.
            //
            // f = (lambda ((x T)) t)  [if we are expanding a definition]
            // ---------------------- MACRO_SR_PRED_INTRO
            // n = instance
            Node conc = n.eqNode(instance);
            tpg->addRewriteStep(n,
                                instance,
                                PfRule::MACRO_SR_PRED_INTRO,
                                pfExpChildren,
                                {conc},
                                true);
          }
        }
        // now, call expand definitions again on the result
        TrustNode texp = expandDefinitions(instance, cache, expandOnly, tpg);
        Node expanded = texp.isNull() ? instance : texp.getNode();
        cache[n] = n == expanded ? Node::null() : expanded;
        result.push(expanded);
        continue;
      }
      else if (!expandOnly)
      {
        // do not do any theory stuff if expandOnly is true

        theory::Theory* t = d_smt.getTheoryEngine()->theoryOf(node);

        Assert(t != NULL);
        TrustNode trn = t->expandDefinition(n);
        if (!trn.isNull())
        {
          node = trn.getNode();
          if (tpg != nullptr)
          {
            tpg->addRewriteStep(
                n, node, trn.getGenerator(), true, PfRule::THEORY_EXPAND_DEF);
          }
        }
        else
        {
          node = n;
        }
      }

      // the partial functions can fall through, in which case we still
      // consider their children
      worklist.push(std::make_tuple(
          Node(n), node, true));  // Original and rewritten result

      for (size_t i = 0; i < node.getNumChildren(); ++i)
      {
        worklist.push(
            std::make_tuple(node[i],
                            node[i],
                            false));  // Rewrite the children of the result only
      }
    }
    else
    {
      // Working upwards
      // Reconstruct the node from it's (now rewritten) children on the stack

      Debug("expand") << "cons : " << node << std::endl;
      if (node.getNumChildren() > 0)
      {
        // cout << "cons : " << node << std::endl;
        NodeBuilder<> nb(node.getKind());
        if (node.getMetaKind() == metakind::PARAMETERIZED)
        {
          Debug("expand") << "op   : " << node.getOperator() << std::endl;
          // cout << "op   : " << node.getOperator() << std::endl;
          nb << node.getOperator();
        }
        for (size_t i = 0, nchild = node.getNumChildren(); i < nchild; ++i)
        {
          Assert(!result.empty());
          Node expanded = result.top();
          result.pop();
          // cout << "exchld : " << expanded << std::endl;
          Debug("expand") << "exchld : " << expanded << std::endl;
          nb << expanded;
        }
        node = nb;
      }
      // Only cache once all subterms are expanded
      cache[n] = n == node ? Node::null() : node;
      result.push(node);
    }
  } while (!worklist.empty());

  AlwaysAssert(result.size() == 1);

  Node res = result.top();

  if (res == orig)
  {
    return TrustNode::null();
  }
  return TrustNode::mkTrustRewrite(orig, res, tpg);
}

void ExpandDefs::expandAssertions(AssertionPipeline& assertions,
                                  bool expandOnly)
{
  Chat() << "expanding definitions in assertions..." << std::endl;
  Trace("exp-defs") << "ExpandDefs::simplify(): expanding definitions"
                    << std::endl;
  TimerStat::CodeTimer codeTimer(d_smtStats.d_definitionExpansionTime);
  std::unordered_map<Node, Node, NodeHashFunction> cache;
  for (size_t i = 0, nasserts = assertions.size(); i < nasserts; ++i)
  {
    Node assert = assertions[i];
    // notice we call this method with only one value of expandOnly currently,
    // hence we maintain only a single set of proof steps in d_tpg.
    TrustNode expd = expandDefinitions(assert, cache, expandOnly, d_tpg.get());
    if (!expd.isNull())
    {
      Trace("exp-defs") << "ExpandDefs::expandAssertions: " << assert << " -> "
                        << expd.getNode() << std::endl;
      assertions.replaceTrusted(i, expd);
    }
  }
}

void ExpandDefs::setProofNodeManager(ProofNodeManager* pnm)
{
  if (d_tpg == nullptr)
  {
    d_tpg.reset(new TConvProofGenerator(pnm,
                                        d_smt.getUserContext(),
                                        TConvPolicy::FIXPOINT,
                                        TConvCachePolicy::NEVER,
                                        "ExpandDefs::TConvProofGenerator",
                                        nullptr,
                                        true));
  }
}

}  // namespace smt
}  // namespace cvc5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback