summaryrefslogtreecommitdiff
path: root/src/theory/bags/solver_state.cpp
blob: 9bcb6ae3c54ca29a0e0f8773665d358aceff68b0 (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
/*********************                                                        */
/*! \file solver_state.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Mudathir Mohamed, Morgan Deters, Dejan Jovanovic
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2020 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 bags state object
 **/

#include "theory/bags/solver_state.h"

#include "expr/attribute.h"
#include "expr/bound_var_manager.h"
#include "expr/skolem_manager.h"
#include "theory/uf/equality_engine.h"

using namespace std;
using namespace CVC4::kind;

namespace CVC4 {
namespace theory {
namespace bags {

SolverState::SolverState(context::Context* c,
                         context::UserContext* u,
                         Valuation val)
    : TheoryState(c, u, val)
{
  d_true = NodeManager::currentNM()->mkConst(true);
  d_false = NodeManager::currentNM()->mkConst(false);
  d_nm = NodeManager::currentNM();
}

void SolverState::registerBag(TNode n)
{
  Assert(n.getType().isBag());
  d_bags.insert(n);
}

void SolverState::registerCountTerm(TNode n)
{
  Assert(n.getKind() == BAG_COUNT);
  Node element = getRepresentative(n[0]);
  Node bag = getRepresentative(n[1]);
  d_bagElements[bag].insert(element);
}

const std::set<Node>& SolverState::getBags() { return d_bags; }

const std::set<Node>& SolverState::getElements(Node B)
{
  Node bag = getRepresentative(B);
  return d_bagElements[B];
}

void SolverState::reset()
{
  d_bagElements.clear();
  d_bags.clear();
}

void SolverState::initialize()
{
  reset();
  collectBagsAndCountTerms();
}

void SolverState::collectBagsAndCountTerms()
{
  Trace("SolverState::collectBagsAndCountTerms")
      << "SolverState::collectBagsAndCountTerms start" << endl;
  eq::EqClassesIterator repIt = eq::EqClassesIterator(d_ee);
  while (!repIt.isFinished())
  {
    Node eqc = (*repIt);
    Trace("SolverState::collectBagsAndCountTerms")
        << "[" << eqc << "]: " << endl;

    if (eqc.getType().isBag())
    {
      registerBag(eqc);
    }

    eq::EqClassIterator it = eq::EqClassIterator(eqc, d_ee);
    while (!it.isFinished())
    {
      Node n = (*it);
      Kind k = n.getKind();
      if (k == MK_BAG)
      {
        // for terms (bag x c) we need to store x by registering the count term
        // (bag.count x (bag x c))
        Node count = d_nm->mkNode(BAG_COUNT, n[0], n);
        registerCountTerm(count);
        Trace("SolverState::collectBagsAndCountTerms")
            << "registered " << count << endl;
      }
      if (k == BAG_COUNT)
      {
        // this takes care of all count terms in each equivalent class
        registerCountTerm(n);
        Trace("SolverState::collectBagsAndCountTerms")
            << "registered " << n << endl;
      }
      ++it;
    }

    ++repIt;
  }

  Trace("SolverState::collectBagsAndCountTerms")
      << "SolverState::collectBagsAndCountTerms end" << endl;
}

}  // namespace bags
}  // namespace theory
}  // namespace CVC4
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback