summaryrefslogtreecommitdiff
path: root/src/theory/booleans/circuit_propagator.h
blob: 9593f7735c2d9946ced3f05750a25cd3a47fb2a9 (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
/*********************                                                        */
/*! \file circuit_propagator.h
 ** \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 A non-clausal circuit propagator for Boolean simplification
 **
 ** A non-clausal circuit propagator for Boolean simplification.
 **/

#include "cvc4_private.h"

#ifndef __CVC4__THEORY__BOOLEANS__CIRCUIT_PROPAGATOR_H
#define __CVC4__THEORY__BOOLEANS__CIRCUIT_PROPAGATOR_H

#include <vector>
#include <functional>

#include "theory/theory.h"
#include "context/context.h"
#include "util/hash.h"
#include "expr/node.h"

namespace CVC4 {
namespace theory {
namespace booleans {


/**
 * The main purpose of the CircuitPropagator class is to maintain the
 * state of the circuit for subsequent calls to propagate(), so that
 * the same fact is not output twice, so that the same edge in the
 * circuit isn't propagated twice, etc.
 */
class CircuitPropagator {

public:

  /**
   * Value of a particular node
   */
  enum AssignmentStatus {
    /** Node is currently unassigned */
    UNASSIGNED = 0,
    /** Node is assigned to true */
    ASSIGNED_TO_TRUE,
    /** Node is assigned to false */
    ASSIGNED_TO_FALSE,
  };

  /** Invert a set value */
  static inline AssignmentStatus neg(AssignmentStatus value) {
    Assert(value != UNASSIGNED);
    if (value == ASSIGNED_TO_TRUE) return ASSIGNED_TO_FALSE;
    else return ASSIGNED_TO_TRUE;
  }

private:

  /** Back edges from nodes to where they are used */
  typedef std::hash_map<TNode, std::vector<TNode>, TNodeHashFunction> BackEdgesMap;
  BackEdgesMap d_backEdges;

  /** The propagation queue */
  std::vector<TNode> d_propagationQueue;

  /** Are we in conflict */
  bool d_conflict;

  /** Map of substitutions */
  std::vector<Node>& d_learnedLiterals;

  /** Nodes that have been attached already (computed forward edges for) */
  // All the nodes we've visited so far
  std::hash_set<TNode, TNodeHashFunction> d_seen;

  /**
   * Assignment status of each node.
   */
  typedef std::hash_map<TNode, AssignmentStatus, TNodeHashFunction> AssignmentMap;
  AssignmentMap d_state;

  /**
   * Assign Node in circuit with the value and add it to the queue; note conflicts.
   */
  void assignAndEnqueue(TNode n, bool value) {

    Debug("circuit-prop") << "CircuitPropagator::assign(" << n << ", " << (value ? "true" : "false") << ")" << std::endl;

    if (n.getKind() == kind::CONST_BOOLEAN) {
      // Assigning a constant to the opposite value is dumb
      if (value != n.getConst<bool>()) {
        d_conflict = true;
        return;
      }
    }

    // Get the current assignement
    AssignmentStatus state = d_state[n];

    if(state != UNASSIGNED) {
      // If the node is already assigned we might have a conflict
      if(value != (state == ASSIGNED_TO_TRUE)) {
        d_conflict = true;
      }
    } else {
      // If unassigned, mark it as assigned
      d_state[n] = value ? ASSIGNED_TO_TRUE : ASSIGNED_TO_FALSE;
      // Add for further propagation
      d_propagationQueue.push_back(n);
    }
  }

  /** True iff Node is assigned in circuit (either true or false). */
  bool isAssigned(TNode n) const {
    AssignmentMap::const_iterator i = d_state.find(n);
    return i != d_state.end() && ((*i).second != UNASSIGNED);
  }

  /** True iff Node is assigned to the value. */
  bool isAssignedTo(TNode n, bool value) const {
    AssignmentMap::const_iterator i = d_state.find(n);
    if (i == d_state.end()) return false;
    if (value && ((*i).second == ASSIGNED_TO_TRUE)) return true;
    if (!value && ((*i).second == ASSIGNED_TO_FALSE)) return true;
    return false;
  }

  /** Get Node assignment in circuit.  Assert-fails if Node is unassigned. */
  bool getAssignment(TNode n) const {
    Assert(d_state.find(n) != d_state.end() && d_state.find(n)->second != UNASSIGNED);
    return d_state.find(n)->second == ASSIGNED_TO_TRUE;
  }

  /** Predicate for use in STL functions. */
  class IsAssigned : public std::unary_function<TNode, bool> {
    CircuitPropagator& d_circuit;
  public:
    IsAssigned(CircuitPropagator& circuit) :
      d_circuit(circuit) {
    }

    bool operator()(TNode in) const {
      return d_circuit.isAssigned(in);
    }
  };/* class IsAssigned */

  /** Predicate for use in STL functions. */
  class IsAssignedTo : public std::unary_function<TNode, bool> {
    CircuitPropagator& d_circuit;
    bool d_value;
  public:
    IsAssignedTo(CircuitPropagator& circuit, bool value) :
      d_circuit(circuit),
      d_value(value) {
    }

    bool operator()(TNode in) const {
      return d_circuit.isAssignedTo(in, d_value);
    }
  };/* class IsAssignedTo */

  /**
   * Compute the map from nodes to the nodes that use it.
   */
  void computeBackEdges(TNode node);

  /**
   * Propagate new information forward in circuit to
   * the parents of "in".
   */
  void propagateForward(TNode child, bool assignment);

  /**
   * Propagate new information backward in circuit to
   * the children of "in".
   */
  void propagateBackward(TNode parent, bool assignment);

  /** Whether to perform forward propagation */
  bool d_forwardPropagation;
  /** Whether to perform backward propagation */
  bool d_backwardPropagation;

public:
  /**
   * Construct a new CircuitPropagator with the given atoms and backEdges.
   */
  CircuitPropagator(std::vector<Node>& outLearnedLiterals, bool enableForward = true, bool enableBackward = true) :
    d_conflict(false),
    d_learnedLiterals(outLearnedLiterals),
    d_forwardPropagation(enableForward),
    d_backwardPropagation(enableBackward) {
  }

  /** Assert for propagation */
  void assert(TNode assertion);

  /**
   * Propagate through the asserted circuit propagator. New information discovered by the propagator
   * are put in the subsitutions vector used in construction.
   *
   * @return true iff conflict found
   */
  bool propagate() CVC4_WARN_UNUSED_RESULT;

};/* class CircuitPropagator */

}/* CVC4::theory::booleans namespace */
}/* CVC4::theory namespace */
}/* CVC4 namespace */

#endif /* __CVC4__THEORY__BOOLEANS__CIRCUIT_PROPAGATOR_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback