summaryrefslogtreecommitdiff
path: root/src/theory/bags/solver_state.cpp
blob: ad817062fb512a6999d3c8a354e20f4e54ed3175 (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
/******************************************************************************
 * Top contributors (to current version):
 *   Mudathir Mohamed, Aina Niemetz
 *
 * This file is part of the cvc5 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.
 * ****************************************************************************
 *
 * 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 cvc5::kind;

namespace cvc5 {
namespace theory {
namespace bags {

SolverState::SolverState(Env& env, Valuation val) : TheoryState(env, 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 = 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[bag];
}

const std::set<Node>& SolverState::getDisequalBagTerms() { return d_deq; }

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

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

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

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

    eq::EqClassIterator it = eq::EqClassIterator(eqc, d_ee);
    while (!it.isFinished())
    {
      Node n = (*it);
      Trace("bags-eqc") << (*it) << " ";
      Kind k = n.getKind();
      if (k == BAG_MAKE)
      {
        // 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;
    }
    Trace("bags-eqc") << " } " << std::endl;
    ++repIt;
  }

  Trace("bags-eqc") << "bag representatives: " << d_bags << endl;
  Trace("bags-eqc") << "bag elements: " << d_bagElements << endl;
}

void SolverState::collectDisequalBagTerms()
{
  eq::EqClassIterator it = eq::EqClassIterator(d_false, d_ee);
  while (!it.isFinished())
  {
    Node n = (*it);
    if (n.getKind() == EQUAL && n[0].getType().isBag())
    {
      Trace("bags-eqc") << "Disequal terms: " << n << std::endl;
      d_deq.insert(n);
    }
    ++it;
  }
}

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