summaryrefslogtreecommitdiff
path: root/src/theory/idl/idl_assertion.cpp
blob: 717122195ba17d4f49e997bd8588a85a793fdacc (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
/*********************                                                        */
/*! \file idl_assertion.cpp
 ** \verbatim
 ** Original author: Dejan Jovanovic
 ** Major contributors: none
 ** Minor contributors (to current version): Morgan Deters
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2013  New York University and The University of Iowa
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief [[ Add one-line brief description here ]]
 **
 ** [[ Add lengthier description here ]]
 ** \todo document this file
 **/

#include "theory/idl/idl_assertion.h"

using namespace CVC4;
using namespace theory;
using namespace idl;

IDLAssertion::IDLAssertion()
: d_op(kind::LAST_KIND)
{}

IDLAssertion::IDLAssertion(TNode node) {
  bool ok = parse(node, 1, false);
  if (!ok) {
    d_x = d_y = TNode::null();
  } else {
    if (d_op == kind::GT) {
      // Turn GT into LT x - y > c is the same as y - x < -c
      std::swap(d_x, d_y);
      d_c = -d_c;
      d_op = kind::LT;
    }
    if (d_op == kind::GEQ) {
      // Turn GT into LT x - y >= c is the same as y - x <= -c
      std::swap(d_x, d_y);
      d_c = -d_c;
      d_op = kind::LEQ;
    }
    if (d_op == kind::LT) {
      // Turn strict into non-strict x - y < c is the same as x - y <= c-1
      d_c = d_c - 1;
      d_op = kind::LEQ;
    }
  }
  d_original = node;
}

IDLAssertion::IDLAssertion(const IDLAssertion& other)
: d_x(other.d_x)
, d_y(other.d_y)
, d_op(other.d_op)
, d_c(other.d_c)
, d_original(other.d_original)
{}

bool IDLAssertion::propagate(IDLModel& model) const {
  Debug("theory::idl::model") << model << std::endl;
  Assert(ok());
  // Should be d_x - d_y <= d_c, or d_x - d_c <= d_y
  Integer x_value = model.getValue(d_x);
  Integer y_value = model.getValue(d_y);
  if (x_value - y_value > d_c) {
    model.setValue(d_y, x_value - d_c, IDLReason(d_x, d_original));
    Debug("theory::idl::model") << model << std::endl;
    return true;
  } else {
    return false;
  }
}

void IDLAssertion::toStream(std::ostream& out) const {
  out << "IDL[" << d_x << " - " << d_y << " " << d_op << " " << d_c << "]";
}

/** Negates the given arithmetic kind */
static Kind negateOp(Kind op) {
  switch (op) {
  case kind::LT:
    // not (a < b) = (a >= b)
    return kind::GEQ;
  case kind::LEQ:
    // not (a <= b) = (a > b)
    return kind::GT;
  case kind::GT:
    // not (a > b) = (a <= b)
    return kind::LEQ;
  case kind::GEQ:
    // not (a >= b) = (a < b)
    return kind::LT;
  case kind::EQUAL:
    // not (a = b) = (a != b)
    return kind::DISTINCT;
  case kind::DISTINCT:
    // not (a != b) = (a = b)
    return kind::EQUAL;
  default:
    Unreachable();
    break;
  }
  return kind::LAST_KIND;
}

bool IDLAssertion::parse(TNode node, int c, bool negated) {

  // Only unit coefficients allowed
  if (c != 1 && c != -1) {
    return false;
  }

  // Assume we're ok
  bool ok = true;

  // The kind of the node
  switch(node.getKind()) {

  case kind::NOT:
    // We parse the negation
    ok = parse(node[0], c, true);
    // Setup the kind
    if (ok) {
      d_op = negateOp(d_op);
    }
    break;

  case kind::EQUAL:
  case kind::LT:
  case kind::LEQ:
  case kind::GT:
  case kind::GEQ: {
    // All relation operators are parsed on both sides
    d_op = node.getKind();
    ok = parse(node[0], c, negated);
    if (ok) {
      ok = parse(node[1],-c, negated);
    }
    break;
  }

  case kind::CONST_RATIONAL: {
    // Constants
    Rational m = node.getConst<Rational>();
    if (m.isIntegral()) {
      d_c +=  m.getNumerator() * (-c);
    } else {
      ok = false;
    }
    break;
  }
  case kind::MULT: {
    // Only unit multiplication of variables
    if (node.getNumChildren() == 2 && node[0].isConst()) {
      Rational a = node[0].getConst<Rational>();
      if (a == 1 || a == -1) {
        ok = parse(node[1], c * a.sgn(), negated);
      } else {
        ok = false;
      }
    } else {
      ok = false;
    }
    break;
  }

  case kind::PLUS: {
    for(unsigned i = 0; i < node.getNumChildren(); ++i) {
      ok = parse(node[i], c, negated);
      if(!ok) {
        break;
      }
    }
    break;
  }

  case kind::MINUS: {
    ok = parse(node[0], c, negated);
    if (ok) {
      ok = parse(node[1], -c, negated);
    }
    break;
  }

  case kind::UMINUS: {
    ok = parse(node[0], -c, negated);
    break;
  }

  default: {
    if (c > 0) {
      if (d_x.isNull()) {
        d_x = node;
      } else {
        ok = false;
      }
    } else {
      if (d_y.isNull()) {
        d_y = node;
      } else {
        ok = false;
      }
    }
    break;
  }
  } // End case

  // Difference logic OK
  return ok;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback