summaryrefslogtreecommitdiff
path: root/src/theory/booleans/theory_bool.cpp
blob: b06972a2e3df1071828920afcca0dadb1f2cf275 (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
/*********************                                                        */
/*! \file theory_bool.cpp
 ** \verbatim
 ** Original author: mdeters
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010, 2011  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief The theory of booleans.
 **
 ** The theory of booleans.
 **/

#include "theory/theory.h"
#include "theory/booleans/theory_bool.h"
#include "theory/booleans/circuit_propagator.h"
#include "theory/valuation.h"
#include "util/boolean_simplification.h"

#include <vector>
#include <stack>
#include "util/hash.h"

using namespace std;

namespace CVC4 {
namespace theory {
namespace booleans {

Node TheoryBool::getValue(TNode n) {
  NodeManager* nodeManager = NodeManager::currentNM();

  switch(n.getKind()) {
  case kind::VARIABLE:
    // case for Boolean vars is implemented in TheoryEngine (since it
    // appeals to the PropEngine to get the value)
    Unreachable();

  case kind::EQUAL: // 2 args
    // should be handled by IFF
    Unreachable();

  case kind::NOT: // 1 arg
    return nodeManager->mkConst(! d_valuation.getValue(n[0]).getConst<bool>());

  case kind::AND: { // 2+ args
    for(TNode::iterator i = n.begin(),
            iend = n.end();
          i != iend;
          ++i) {
      if(! d_valuation.getValue(*i).getConst<bool>()) {
        return nodeManager->mkConst(false);
      }
    }
    return nodeManager->mkConst(true);
  }

  case kind::IFF: // 2 args
    return nodeManager->mkConst( d_valuation.getValue(n[0]).getConst<bool>() ==
                                 d_valuation.getValue(n[1]).getConst<bool>() );

  case kind::IMPLIES: // 2 args
    return nodeManager->mkConst( (! d_valuation.getValue(n[0]).getConst<bool>()) ||
                                 d_valuation.getValue(n[1]).getConst<bool>() );

  case kind::OR: { // 2+ args
    for(TNode::iterator i = n.begin(),
            iend = n.end();
          i != iend;
          ++i) {
      if(d_valuation.getValue(*i).getConst<bool>()) {
        return nodeManager->mkConst(true);
      }
    }
    return nodeManager->mkConst(false);
  }

  case kind::XOR: // 2 args
    return nodeManager->mkConst( d_valuation.getValue(n[0]).getConst<bool>() !=
                                 d_valuation.getValue(n[1]).getConst<bool>() );

  case kind::ITE: // 3 args
    // all ITEs should be gone except (bool,bool,bool) ones
    Assert( n[1].getType() == nodeManager->booleanType() &&
            n[2].getType() == nodeManager->booleanType() );
    return nodeManager->mkConst( d_valuation.getValue(n[0]).getConst<bool>() ?
                                 d_valuation.getValue(n[1]).getConst<bool>() :
                                 d_valuation.getValue(n[2]).getConst<bool>() );

  default:
    Unhandled(n.getKind());
  }
}

static void
findAtoms(TNode in, vector<TNode>& atoms,
          hash_map<TNode, vector<TNode>, TNodeHashFunction>& backEdges) {
  Kind k = in.getKind();
  Assert(kindToTheoryId(k) == THEORY_BOOL);

  stack< pair<TNode, TNode::iterator> > trail;
  hash_set<TNode, TNodeHashFunction> seen;
  trail.push(make_pair(in, in.begin()));

  while(!trail.empty()) {
    pair<TNode, TNode::iterator>& pr = trail.top();
    Debug("simplify") << "looking at: " << pr.first << endl;
    if(pr.second == pr.first.end()) {
      trail.pop();
    } else {
      if(pr.second == pr.first.begin()) {
        Debug("simplify") << "first visit: " << pr.first << endl;
        // first time we've visited this node?
        if(seen.find(pr.first) == seen.end()) {
          Debug("simplify") << "+ haven't seen it yet." << endl;
          seen.insert(pr.first);
        } else {
          Debug("simplify") << "+ saw it before." << endl;
          trail.pop();
          continue;
        }
      }
      TNode n = *pr.second++;
      if(seen.find(n) == seen.end()) {
        Debug("simplify") << "back edge " << n << " to " << pr.first << endl;
        backEdges[n].push_back(pr.first);
        Kind nk = n.getKind();
        if(kindToTheoryId(nk) == THEORY_BOOL) {
          trail.push(make_pair(n, n.begin()));
        } else {
          Debug("simplify") << "atom: " << n << endl;
          atoms.push_back(n);
        }
      }
    }
  }
}

Node TheoryBool::simplify(TNode in, Substitutions& outSubstitutions) {
  const unsigned prev = outSubstitutions.size();

  if(kindToTheoryId(in.getKind()) != THEORY_BOOL) {
    Assert(in.getMetaKind() == kind::metakind::VARIABLE &&
           in.getType().isBoolean());
    return in;
  }

  // form back edges and atoms
  vector<TNode> atoms;
  hash_map<TNode, vector<TNode>, TNodeHashFunction> backEdges;
  Debug("simplify") << "finding atoms..." << endl << push;
  findAtoms(in, atoms, backEdges);
  Debug("simplify") << pop << "done finding atoms..." << endl;

  hash_map<TNode, Node, TNodeHashFunction> simplified;

  vector<Node> newFacts;
  CircuitPropagator circuit(atoms, backEdges);
  Debug("simplify") << "propagate..." << endl;
  if(circuit.propagate(in, true, newFacts)) {
    Notice() << "Found a conflict in nonclausal Boolean reasoning" << endl;
    return NodeManager::currentNM()->mkConst(false);
  }
  Debug("simplify") << "done w/ propagate..." << endl;

  for(vector<Node>::iterator i = newFacts.begin(), i_end = newFacts.end(); i != i_end; ++i) {
    TNode a = *i;
    if(a.getKind() == kind::NOT) {
      if(a[0].getMetaKind() == kind::metakind::VARIABLE ) {
        Debug("simplify") << "found bool unit ~" << a[0] << "..." << endl;
        outSubstitutions.push_back(make_pair(a[0], NodeManager::currentNM()->mkConst(false)));
      } else if(kindToTheoryId(a[0].getKind()) != THEORY_BOOL) {
        Debug("simplify") << "simplifying: " << a << endl;
        simplified[a] = d_valuation.simplify(a, outSubstitutions);
        Debug("simplify") << "done simplifying, got: " << simplified[a] << endl;
      }
    } else {
      if(a.getMetaKind() == kind::metakind::VARIABLE) {
        Debug("simplify") << "found bool unit " << a << "..." << endl;
        outSubstitutions.push_back(make_pair(a, NodeManager::currentNM()->mkConst(true)));
      } else if(kindToTheoryId(a.getKind()) != THEORY_BOOL) {
        Debug("simplify") << "simplifying: " << a << endl;
        simplified[a] = d_valuation.simplify(a, outSubstitutions);
        Debug("simplify") << "done simplifying, got: " << simplified[a] << endl;
      }
    }
  }

  Debug("simplify") << "substituting in root..." << endl;
  Node n = in.substitute(outSubstitutions.begin(), outSubstitutions.end());
  Debug("simplify") << "got: " << n << endl;
  if(Debug.isOn("simplify")) {
    Debug("simplify") << "new substitutions:" << endl;
    copy(outSubstitutions.begin() + prev, outSubstitutions.end(),
         ostream_iterator< pair<TNode, TNode> >(Debug("simplify"), "\n"));
    Debug("simplify") << "done." << endl;
  }
  if(outSubstitutions.size() > prev) {
    NodeBuilder<> b(kind::AND);
    b << n;
    for(Substitutions::iterator i = outSubstitutions.begin() + prev,
          i_end = outSubstitutions.end();
        i != i_end;
        ++i) {
      if((*i).first.getType().isBoolean()) {
        if((*i).second.getMetaKind() == kind::metakind::CONSTANT) {
          if((*i).second.getConst<bool>()) {
            b << (*i).first;
          } else {
            b << BooleanSimplification::negate((*i).first);
          }
        } else {
          b << (*i).first.iffNode((*i).second);
        }
      } else {
        b << (*i).first.eqNode((*i).second);
      }
    }
    n = b;
  }
  Debug("simplify") << "final boolean simplification returned: " << n << endl;
  return n;
}


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