summaryrefslogtreecommitdiff
path: root/src/theory/arith/partial_model.h
blob: 5a0b662f830cf039182859390013239f1d23b298 (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

#include "context/context.h"
#include "context/cdmap.h"
#include "theory/arith/delta_rational.h"
#include "expr/attribute.h"
#include "expr/node.h"

#include <deque>

#ifndef __CVC4__THEORY__ARITH__PARTIAL_MODEL_H
#define __CVC4__THEORY__ARITH__PARTIAL_MODEL_H

namespace CVC4 {
namespace theory {
namespace arith {

namespace partial_model {
struct DeltaRationalCleanupStrategy{
  static void cleanup(DeltaRational* dq){
    Debug("arithgc") << "cleaning up  " << dq << "\n";
    delete dq;
  }
};

struct AssignmentAttrID {};
typedef expr::Attribute<AssignmentAttrID,
                        DeltaRational*,
                        DeltaRationalCleanupStrategy> Assignment;

/**
 * This defines a context dependent delta rational map.
 * This is used to keep track of the current lower and upper bounds on
 * each variable.  This information is conext dependent.
 */
typedef context::CDMap<TNode, DeltaRational, TNodeHashFunction> CDDRationalMap;
/* Side disucssion for why new tables are introduced instead of using the attribute
 * framework.
 * It comes down to that the obvious ways to use the current attribute framework do
 * do not provide a terribly satisfying answer.
 * - Suppose the type of the attribute is CD and maps to type DeltaRational.
 *   Well it can't map to a DeltaRational, and it thus it will be a DeltaRational*
 *   However being context dependent means givening up cleanup functions.
 *   So this leaks memory.
 * - Suppose the type of the attribute is CD and the type mapped to
 *   was a Node wrapping a CONST_DELTA_RATIONAL.
 *   This was rejected for inefficiency, and uglyness.
 *   Inefficiency: Every lookup and comparison will require going through the
 *   massaging in between a node and the constant being wrapped. Every update
 *   requires going through the additional expense of creating at least 1 node.
 *   The Uglyness: If this was chosen it would also suggest using comparisons against
 *   DeltaRationals for the tracking the constraints for conflict analysis.
 *   This seems to invite misuse and introducing Nodes that should probably not escape
 *   arith.
 * - Suppose that the of the attribute was not CD and mapped to a CDO<DeltaRational*>
 *   or similarly a ContextObj that wraps a DeltaRational*.
 *   This is currently rejected just because this is difficult to get right,
 *   and maintain. However with enough effort this the best solution is probably of
 *   this form.
 */


/**
 * This is the literal that was asserted in the current context to the theory
 * that asserted the tightest lower bound on a variable.
 * For example: for a variable x this could be the constraint (>= x 4) or (not (<= x 50))
 * Note the strong correspondence between this and LowerBoundMap.
 * This is used during conflict analysis.
 */
struct LowerConstraintAttrID {};
typedef expr::CDAttribute<LowerConstraintAttrID,TNode> LowerConstraint;

/**
 * See the documentation for LowerConstraint.
 */
struct UpperConstraintAttrID {};
typedef expr::CDAttribute<UpperConstraintAttrID,TNode> UpperConstraint;


}; /*namespace partial_model*/


struct TheoryArithPropagatedID {};
typedef expr::CDAttribute<TheoryArithPropagatedID, bool> TheoryArithPropagated;



class ArithPartialModel {
private:
  partial_model::CDDRationalMap d_LowerBoundMap;
  partial_model::CDDRationalMap d_UpperBoundMap;

  typedef std::pair<TNode, DeltaRational> HistoryPair;
  typedef std::deque< HistoryPair > HistoryStack;

  HistoryStack d_history;

  bool d_savingAssignments;

public:

  ArithPartialModel(context::Context* c):
    d_LowerBoundMap(c),
    d_UpperBoundMap(c),
    d_history(),
    d_savingAssignments(false)
  { }

  void setLowerConstraint(TNode x, TNode constraint);
  void setUpperConstraint(TNode x, TNode constraint);
  TNode getLowerConstraint(TNode x);
  TNode getUpperConstraint(TNode x);


  void beginRecordingAssignments();

  /* */
  void stopRecordingAssignments(bool revert);


  void setUpperBound(TNode x, const DeltaRational& r);
  void setLowerBound(TNode x, const DeltaRational& r);
  void setAssignment(TNode x, const DeltaRational& r);

  /** Must know that the bound exists before calling this! */
  DeltaRational getUpperBound(TNode x) const;
  DeltaRational getLowerBound(TNode x) const;
  DeltaRational getAssignment(TNode x) const;


  /**
   * x <= l
   * ? c < l
   */
  bool belowLowerBound(TNode x, DeltaRational& c, bool strict);

  /**
   * x <= u
   * ? c < u
   */
  bool aboveUpperBound(TNode x, DeltaRational& c, bool strict);

  bool strictlyBelowUpperBound(TNode x);
  bool strictlyAboveLowerBound(TNode x);
  bool assignmentIsConsistent(TNode x);
};




}; /* namesapce arith */
}; /* namespace theory */
}; /* namespace CVC4 */



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