summaryrefslogtreecommitdiff
path: root/src/theory/arith/operator_elim.cpp
blob: 593fbd584b778edecd88ad82ae13ad673e370517 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*********************                                                        */
/*! \file operator_elim.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Tim King, Andrew Reynolds, Morgan Deters
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Implementation of operator elimination for arithmetic
 **/

#include "theory/arith/operator_elim.h"

#include "expr/skolem_manager.h"
#include "options/arith_options.h"
#include "smt/logic_exception.h"
#include "theory/arith/arith_utilities.h"
#include "theory/rewriter.h"
#include "theory/theory.h"

using namespace CVC4::kind;

namespace CVC4 {
namespace theory {
namespace arith {

OperatorElim::OperatorElim(const LogicInfo& info) : d_info(info) {}

void OperatorElim::checkNonLinearLogic(Node term)
{
  if (d_info.isLinear())
  {
    Trace("arith-logic") << "ERROR: Non-linear term in linear logic: " << term
                         << std::endl;
    std::stringstream serr;
    serr << "A non-linear fact was asserted to arithmetic in a linear logic."
         << std::endl;
    serr << "The fact in question: " << term << std::endl;
    throw LogicException(serr.str());
  }
}

Node OperatorElim::eliminateOperatorsRec(Node n)
{
  Trace("arith-elim") << "Begin elim: " << n << std::endl;
  NodeManager* nm = NodeManager::currentNM();
  std::unordered_map<Node, Node, TNodeHashFunction> visited;
  std::unordered_map<Node, Node, TNodeHashFunction>::iterator it;
  std::vector<Node> visit;
  Node cur;
  visit.push_back(n);
  do
  {
    cur = visit.back();
    visit.pop_back();
    it = visited.find(cur);
    if (Theory::theoryOf(cur) != THEORY_ARITH)
    {
      visited[cur] = cur;
    }
    else if (it == visited.end())
    {
      visited[cur] = Node::null();
      visit.push_back(cur);
      for (const Node& cn : cur)
      {
        visit.push_back(cn);
      }
    }
    else if (it->second.isNull())
    {
      Node ret = cur;
      bool childChanged = false;
      std::vector<Node> children;
      if (cur.getMetaKind() == metakind::PARAMETERIZED)
      {
        children.push_back(cur.getOperator());
      }
      for (const Node& cn : cur)
      {
        it = visited.find(cn);
        Assert(it != visited.end());
        Assert(!it->second.isNull());
        childChanged = childChanged || cn != it->second;
        children.push_back(it->second);
      }
      if (childChanged)
      {
        ret = nm->mkNode(cur.getKind(), children);
      }
      Node retElim = eliminateOperators(ret);
      if (retElim != ret)
      {
        // recursively eliminate operators in result, since some eliminations
        // are defined in terms of other non-standard operators.
        ret = eliminateOperatorsRec(retElim);
      }
      visited[cur] = ret;
    }
  } while (!visit.empty());
  Assert(visited.find(n) != visited.end());
  Assert(!visited.find(n)->second.isNull());
  return visited[n];
}

Node OperatorElim::eliminateOperators(Node node)
{
  NodeManager* nm = NodeManager::currentNM();
  SkolemManager* sm = nm->getSkolemManager();

  Kind k = node.getKind();
  switch (k)
  {
    case TANGENT:
    case COSECANT:
    case SECANT:
    case COTANGENT:
    {
      // these are eliminated by rewriting
      return Rewriter::rewrite(node);
      break;
    }
    case TO_INTEGER:
    case IS_INTEGER:
    {
      Node toIntSkolem;
      std::map<Node, Node>::const_iterator it = d_to_int_skolem.find(node[0]);
      if (it == d_to_int_skolem.end())
      {
        // node[0] - 1 < toIntSkolem <= node[0]
        // -1 < toIntSkolem - node[0] <= 0
        // 0 <= node[0] - toIntSkolem < 1
        Node v = nm->mkBoundVar(nm->integerType());
        Node one = mkRationalNode(1);
        Node zero = mkRationalNode(0);
        Node diff = nm->mkNode(MINUS, node[0], v);
        Node lem = mkInRange(diff, zero, one);
        toIntSkolem = sm->mkSkolem(
            v, lem, "toInt", "a conversion of a Real term to its Integer part");
        toIntSkolem = SkolemManager::getWitnessForm(toIntSkolem);
        d_to_int_skolem[node[0]] = toIntSkolem;
      }
      else
      {
        toIntSkolem = (*it).second;
      }
      if (k == IS_INTEGER)
      {
        return nm->mkNode(EQUAL, node[0], toIntSkolem);
      }
      Assert(k == TO_INTEGER);
      return toIntSkolem;
    }

    case INTS_DIVISION_TOTAL:
    case INTS_MODULUS_TOTAL:
    {
      Node den = Rewriter::rewrite(node[1]);
      Node num = Rewriter::rewrite(node[0]);
      Node intVar;
      Node rw = nm->mkNode(k, num, den);
      std::map<Node, Node>::const_iterator it = d_int_div_skolem.find(rw);
      if (it == d_int_div_skolem.end())
      {
        Node v = nm->mkBoundVar(nm->integerType());
        Node lem;
        Node leqNum = nm->mkNode(LEQ, nm->mkNode(MULT, den, v), num);
        if (den.isConst())
        {
          const Rational& rat = den.getConst<Rational>();
          if (num.isConst() || rat == 0)
          {
            // just rewrite
            return Rewriter::rewrite(node);
          }
          if (rat > 0)
          {
            lem = nm->mkNode(
                AND,
                leqNum,
                nm->mkNode(
                    LT,
                    num,
                    nm->mkNode(MULT,
                               den,
                               nm->mkNode(PLUS, v, nm->mkConst(Rational(1))))));
          }
          else
          {
            lem = nm->mkNode(
                AND,
                leqNum,
                nm->mkNode(
                    LT,
                    num,
                    nm->mkNode(
                        MULT,
                        den,
                        nm->mkNode(PLUS, v, nm->mkConst(Rational(-1))))));
          }
        }
        else
        {
          checkNonLinearLogic(node);
          lem = nm->mkNode(
              AND,
              nm->mkNode(
                  IMPLIES,
                  nm->mkNode(GT, den, nm->mkConst(Rational(0))),
                  nm->mkNode(
                      AND,
                      leqNum,
                      nm->mkNode(
                          LT,
                          num,
                          nm->mkNode(
                              MULT,
                              den,
                              nm->mkNode(PLUS, v, nm->mkConst(Rational(1))))))),
              nm->mkNode(
                  IMPLIES,
                  nm->mkNode(LT, den, nm->mkConst(Rational(0))),
                  nm->mkNode(
                      AND,
                      leqNum,
                      nm->mkNode(
                          LT,
                          num,
                          nm->mkNode(
                              MULT,
                              den,
                              nm->mkNode(
                                  PLUS, v, nm->mkConst(Rational(-1))))))));
        }
        intVar = sm->mkSkolem(
            v, lem, "linearIntDiv", "the result of an intdiv-by-k term");
        intVar = SkolemManager::getWitnessForm(intVar);
        d_int_div_skolem[rw] = intVar;
      }
      else
      {
        intVar = (*it).second;
      }
      if (k == INTS_MODULUS_TOTAL)
      {
        Node nn = nm->mkNode(MINUS, num, nm->mkNode(MULT, den, intVar));
        return nn;
      }
      else
      {
        return intVar;
      }
      break;
    }
    case DIVISION_TOTAL:
    {
      Node num = Rewriter::rewrite(node[0]);
      Node den = Rewriter::rewrite(node[1]);
      if (den.isConst())
      {
        // No need to eliminate here, can eliminate via rewriting later.
        // Moreover, rewriting may change the type of this node from real to
        // int, which impacts certain issues with subtyping.
        return node;
      }
      checkNonLinearLogic(node);
      Node var;
      Node rw = nm->mkNode(k, num, den);
      std::map<Node, Node>::const_iterator it = d_div_skolem.find(rw);
      if (it == d_div_skolem.end())
      {
        Node v = nm->mkBoundVar(nm->realType());
        Node lem = nm->mkNode(IMPLIES,
                              den.eqNode(nm->mkConst(Rational(0))).negate(),
                              nm->mkNode(MULT, den, v).eqNode(num));
        var = sm->mkSkolem(
            v, lem, "nonlinearDiv", "the result of a non-linear div term");
        var = SkolemManager::getWitnessForm(var);
        d_div_skolem[rw] = var;
      }
      else
      {
        var = (*it).second;
      }
      return var;
      break;
    }
    case DIVISION:
    {
      Node num = Rewriter::rewrite(node[0]);
      Node den = Rewriter::rewrite(node[1]);
      Node ret = nm->mkNode(DIVISION_TOTAL, num, den);
      if (!den.isConst() || den.getConst<Rational>().sgn() == 0)
      {
        checkNonLinearLogic(node);
        Node divByZeroNum = getArithSkolemApp(num, ArithSkolemId::DIV_BY_ZERO);
        Node denEq0 = nm->mkNode(EQUAL, den, nm->mkConst(Rational(0)));
        ret = nm->mkNode(ITE, denEq0, divByZeroNum, ret);
      }
      return ret;
      break;
    }

    case INTS_DIVISION:
    {
      // partial function: integer div
      Node num = Rewriter::rewrite(node[0]);
      Node den = Rewriter::rewrite(node[1]);
      Node ret = nm->mkNode(INTS_DIVISION_TOTAL, num, den);
      if (!den.isConst() || den.getConst<Rational>().sgn() == 0)
      {
        checkNonLinearLogic(node);
        Node intDivByZeroNum =
            getArithSkolemApp(num, ArithSkolemId::INT_DIV_BY_ZERO);
        Node denEq0 = nm->mkNode(EQUAL, den, nm->mkConst(Rational(0)));
        ret = nm->mkNode(ITE, denEq0, intDivByZeroNum, ret);
      }
      return ret;
      break;
    }

    case INTS_MODULUS:
    {
      // partial function: mod
      Node num = Rewriter::rewrite(node[0]);
      Node den = Rewriter::rewrite(node[1]);
      Node ret = nm->mkNode(INTS_MODULUS_TOTAL, num, den);
      if (!den.isConst() || den.getConst<Rational>().sgn() == 0)
      {
        checkNonLinearLogic(node);
        Node modZeroNum = getArithSkolemApp(num, ArithSkolemId::MOD_BY_ZERO);
        Node denEq0 = nm->mkNode(EQUAL, den, nm->mkConst(Rational(0)));
        ret = nm->mkNode(ITE, denEq0, modZeroNum, ret);
      }
      return ret;
      break;
    }

    case ABS:
    {
      return nm->mkNode(ITE,
                        nm->mkNode(LT, node[0], nm->mkConst(Rational(0))),
                        nm->mkNode(UMINUS, node[0]),
                        node[0]);
      break;
    }
    case SQRT:
    case ARCSINE:
    case ARCCOSINE:
    case ARCTANGENT:
    case ARCCOSECANT:
    case ARCSECANT:
    case ARCCOTANGENT:
    {
      checkNonLinearLogic(node);
      // eliminate inverse functions here
      std::map<Node, Node>::const_iterator it =
          d_nlin_inverse_skolem.find(node);
      if (it == d_nlin_inverse_skolem.end())
      {
        Node var = nm->mkBoundVar(nm->realType());
        Node lem;
        if (k == SQRT)
        {
          Node skolemApp = getArithSkolemApp(node[0], ArithSkolemId::SQRT);
          Node uf = skolemApp.eqNode(var);
          Node nonNeg =
              nm->mkNode(AND, nm->mkNode(MULT, var, var).eqNode(node[0]), uf);

          // sqrt(x) reduces to:
          // witness y. ite(x >= 0.0, y * y = x ^ y = Uf(x), y = Uf(x))
          //
          // Uf(x) makes sure that the reduction still behaves like a function,
          // otherwise the reduction of (x = 1) ^ (sqrt(x) != sqrt(1)) would be
          // satisfiable. On the original formula, this would require that we
          // simultaneously interpret sqrt(1) as 1 and -1, which is not a valid
          // model.
          lem = nm->mkNode(ITE,
                           nm->mkNode(GEQ, node[0], nm->mkConst(Rational(0))),
                           nonNeg,
                           uf);
        }
        else
        {
          Node pi = mkPi();

          // range of the skolem
          Node rlem;
          if (k == ARCSINE || k == ARCTANGENT || k == ARCCOSECANT)
          {
            Node half = nm->mkConst(Rational(1) / Rational(2));
            Node pi2 = nm->mkNode(MULT, half, pi);
            Node npi2 = nm->mkNode(MULT, nm->mkConst(Rational(-1)), pi2);
            // -pi/2 < var <= pi/2
            rlem = nm->mkNode(
                AND, nm->mkNode(LT, npi2, var), nm->mkNode(LEQ, var, pi2));
          }
          else
          {
            // 0 <= var < pi
            rlem = nm->mkNode(AND,
                              nm->mkNode(LEQ, nm->mkConst(Rational(0)), var),
                              nm->mkNode(LT, var, pi));
          }

          Kind rk = k == ARCSINE
                        ? SINE
                        : (k == ARCCOSINE
                               ? COSINE
                               : (k == ARCTANGENT
                                      ? TANGENT
                                      : (k == ARCCOSECANT
                                             ? COSECANT
                                             : (k == ARCSECANT ? SECANT
                                                               : COTANGENT))));
          Node invTerm = nm->mkNode(rk, var);
          lem = nm->mkNode(AND, rlem, invTerm.eqNode(node[0]));
        }
        Assert(!lem.isNull());
        Node ret = sm->mkSkolem(
            var,
            lem,
            "tfk",
            "Skolem to eliminate a non-standard transcendental function");
        ret = SkolemManager::getWitnessForm(ret);
        d_nlin_inverse_skolem[node] = ret;
        return ret;
      }
      return (*it).second;
      break;
    }

    default: break;
  }
  return node;
}

Node OperatorElim::getArithSkolem(ArithSkolemId asi)
{
  std::map<ArithSkolemId, Node>::iterator it = d_arith_skolem.find(asi);
  if (it == d_arith_skolem.end())
  {
    NodeManager* nm = NodeManager::currentNM();

    TypeNode tn;
    std::string name;
    std::string desc;
    switch (asi)
    {
      case ArithSkolemId::DIV_BY_ZERO:
        tn = nm->realType();
        name = std::string("divByZero");
        desc = std::string("partial real division");
        break;
      case ArithSkolemId::INT_DIV_BY_ZERO:
        tn = nm->integerType();
        name = std::string("intDivByZero");
        desc = std::string("partial int division");
        break;
      case ArithSkolemId::MOD_BY_ZERO:
        tn = nm->integerType();
        name = std::string("modZero");
        desc = std::string("partial modulus");
        break;
      case ArithSkolemId::SQRT:
        tn = nm->realType();
        name = std::string("sqrtUf");
        desc = std::string("partial sqrt");
        break;
      default: Unhandled();
    }

    Node skolem;
    if (options::arithNoPartialFun())
    {
      // partial function: division
      skolem = nm->mkSkolem(name, tn, desc, NodeManager::SKOLEM_EXACT_NAME);
    }
    else
    {
      // partial function: division
      skolem = nm->mkSkolem(name,
                            nm->mkFunctionType(tn, tn),
                            desc,
                            NodeManager::SKOLEM_EXACT_NAME);
    }
    d_arith_skolem[asi] = skolem;
    return skolem;
  }
  return it->second;
}

Node OperatorElim::getArithSkolemApp(Node n, ArithSkolemId asi)
{
  Node skolem = getArithSkolem(asi);
  if (!options::arithNoPartialFun())
  {
    skolem = NodeManager::currentNM()->mkNode(APPLY_UF, skolem, n);
  }
  return skolem;
}

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