summaryrefslogtreecommitdiff
path: root/src/theory/booleans/theory_bool.cpp
blob: 3b3cde33d8dd20ee35dc1b4dd36cace09af3526b (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
/*********************                                                        */
/*! \file theory_bool.cpp
 ** \verbatim
 ** Original author: mdeters
 ** Major contributors: dejan
 ** 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
    Node v = d_valuation.getValue(n[0]);
    return v.isNull() ? Node::null() : nodeManager->mkConst(! v.getConst<bool>());
  }

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

  case kind::IFF: { // 2 args
    Node v0 = d_valuation.getValue(n[0]);
    Node v1 = d_valuation.getValue(n[1]);
    if(v0.isNull() || v1.isNull()) {
      return Node::null();
    }
    return nodeManager->mkConst( v0.getConst<bool>() == v1.getConst<bool>() );
  }

  case kind::IMPLIES: { // 2 args
    Node v0 = d_valuation.getValue(n[0]);
    Node v1 = d_valuation.getValue(n[1]);
    if(v0.isNull() && v1.isNull()) {
      return Node::null();
    }
    bool value = false;
    if(! v0.isNull()) {
      value = value || (! v0.getConst<bool>());
    }
    if(! v1.isNull()) {
      value = value || v1.getConst<bool>();
    }
    return nodeManager->mkConst(value);
  }

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

  case kind::XOR: { // 2 args
    Node v0 = d_valuation.getValue(n[0]);
    Node v1 = d_valuation.getValue(n[1]);
    if(v0.isNull() || v1.isNull()) {
      return Node::null();
    }
    return nodeManager->mkConst( v0.getConst<bool>() != v1.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() );
    Node v0 = d_valuation.getValue(n[0]);
    Node v1 = d_valuation.getValue(n[1]);
    Node v2 = d_valuation.getValue(n[2]);
    if(v0.isNull()) {
      return v1 == v2 ? v1 : Node::null();
    }
    return nodeManager->mkConst( v0.getConst<bool>() ? v1.getConst<bool>() : v2.getConst<bool>() );
  }

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

Theory::PPAssertStatus TheoryBool::ppAsert(TNode in, SubstitutionMap& outSubstitutions) {

  if (in.getKind() == kind::CONST_BOOLEAN && !in.getConst<bool>()) {
    // If we get a false literal, we're in conflict
    return PP_ASSERT_STATUS_CONFLICT;
  }

  // Add the substitution from the variable to it's value
  if (in.getKind() == kind::NOT) {
    if (in[0].getKind() == kind::VARIABLE) {
      outSubstitutions.addSubstitution(in[0], NodeManager::currentNM()->mkConst<bool>(false));
    } else {
      return PP_ASSERT_STATUS_UNSOLVED;
    }
  } else {
    if (in.getKind() == kind::VARIABLE) {
      outSubstitutions.addSubstitution(in, NodeManager::currentNM()->mkConst<bool>(true));
    } else {
      return PP_ASSERT_STATUS_UNSOLVED;
    }
  }

  return PP_ASSERT_STATUS_SOLVED;
}


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