GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/constraint.h Lines: 114 120 95.0 %
Date: 2021-08-14 Branches: 41 236 17.4 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Alex Ozdemir, Haniel Barbosa
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Defines Constraint and ConstraintDatabase which is the internal
14
 * representation of variables in arithmetic
15
 *
16
 * This file defines Constraint and ConstraintDatabase.
17
 * A Constraint is the internal representation of literals in TheoryArithmetic.
18
 * Constraints are fundamentally a triple:
19
 *  - ArithVar associated with the constraint,
20
 *  - a DeltaRational value,
21
 *  - and a ConstraintType.
22
 *
23
 * Literals:
24
 *   The constraint may also keep track of a node corresponding to the
25
 *   Constraint.
26
 *   This can be accessed by getLiteral() in O(1) if it has been set.
27
 *   This node must be in normal form and may be used for communication with
28
 *   the TheoryEngine.
29
 *
30
 * In addition, Constraints keep track of the following:
31
 *  - A Constraint that is the negation of the Constraint.
32
 *  - An iterator into a set of Constraints for the ArithVar sorted by
33
 *    DeltaRational value.
34
 *  - A context dependent internal proof of the node that can be used for
35
 *    explanations.
36
 *  - Whether an equality/disequality has been split in the user context via a
37
 *    lemma.
38
 *  - Whether a constraint, be be used in explanations sent to the context
39
 *
40
 * Looking up constraints:
41
 *  - All of the Constraints with associated nodes in the ConstraintDatabase
42
 *    can be accessed via a single hashtable lookup until the Constraint is
43
 *    removed.
44
 *  - Nodes that have not been associated to a constraints can be
45
 *    inserted/associated to existing nodes in O(log n) time.
46
 *
47
 * Implications:
48
 *  - A Constraint can be used to find unate implications.
49
 *  - A unate implication is an implication based purely on the ArithVar
50
 *    matching  and the DeltaRational value.
51
 *    (implies (<= x c) (<= x d)) given c <= d
52
 *  - This is done using the iterator into the sorted set of constraints.
53
 *  - Given a tight constraint and previous tightest constraint, this will
54
 *    efficiently propagate internally.
55
 *
56
 * Additing and Removing Constraints
57
 *  - Adding Constraints takes O(log n) time where n is the number of
58
 *    constraints associated with the ArithVar.
59
 *  - Removing Constraints takes O(1) time.
60
 *
61
 * Internals:
62
 *  - Constraints are pointers to ConstraintValues.
63
 *  - Undefined Constraints are NullConstraint.
64
 *
65
 * Assumption vs. Assertion:
66
 * - An assertion is anything on the theory d_fact queue.
67
 *   This includes any thing propagated and returned to the fact queue.
68
 *   These can be used in external conflicts and propagations of earlier
69
 *   proofs.
70
 * - An assumption is anything on the theory d_fact queue that has no further
71
 *   explanation i.e. this theory did not propagate it.
72
 * - To set something an assumption, first set it as being as assertion.
73
 * - Internal assumptions have no explanations and must be regressed out of the
74
 *   proof.
75
 */
76
77
#include "cvc5_private.h"
78
79
#ifndef CVC5__THEORY__ARITH__CONSTRAINT_H
80
#define CVC5__THEORY__ARITH__CONSTRAINT_H
81
82
#include <unordered_map>
83
#include <vector>
84
85
#include "base/configuration_private.h"
86
#include "context/cdlist.h"
87
#include "context/cdqueue.h"
88
#include "expr/node.h"
89
#include "proof/trust_node.h"
90
#include "theory/arith/arithvar.h"
91
#include "theory/arith/callbacks.h"
92
#include "theory/arith/constraint_forward.h"
93
#include "theory/arith/delta_rational.h"
94
#include "theory/arith/proof_macros.h"
95
#include "util/statistics_stats.h"
96
97
namespace cvc5 {
98
99
class ProofNodeManager;
100
class EagerProofGenerator;
101
102
namespace context {
103
class Context;
104
}
105
namespace theory {
106
107
namespace arith {
108
109
class Comparison;
110
class ArithCongruenceManager;
111
class ArithVariables;
112
113
/**
114
 * Logs the types of different proofs.
115
 * Current, proof types:
116
 * - NoAP             : This constraint is not known to be true.
117
 * - AssumeAP         : This is an input assertion. There is no proof.
118
 *                    : Something can be both asserted and have a proof.
119
 * - InternalAssumeAP : An internal assumption. This has no guarantee of having an external proof.
120
 *                    : This must be removed by regression.
121
 * - FarkasAP         : A proof with Farka's coefficients, i.e.
122
 *                    :  \sum lambda_i ( asNode(x_i) <= c_i  ) |= 0 < 0
123
 *                    : If proofs are on, coefficients will be logged.
124
 *                    : If proofs are off, coefficients will not be logged.
125
 *                    : A unate implication is a FarkasAP.
126
 * - TrichotomyAP     : This is any entailment using (x<= a and x >=a) => x = a
127
 *                    : Equivalently, (x > a or x < a or x = a)
128
 *                    : There are 3 candidate ways this can propagate:
129
 *                    :   !(x > a) and !(x = a) => x < a
130
 *                    :   !(x < a) and !(x = a) => x > a
131
 *                    :   !(x > a) and !(x < a) => x = a
132
 * - EqualityEngineAP : This is propagated by the equality engine.
133
 *                    : Consult this for the proof.
134
 * - IntTightenAP     : This is indicates that a bound involving integers was tightened.
135
 *                    : e.g. i < 5.5 became i <= 5, when i is an integer.
136
 * - IntHoleAP        : This is currently a catch-all for all integer specific reason.
137
 */
138
enum ArithProofType
139
  { NoAP,
140
    AssumeAP,
141
    InternalAssumeAP,
142
    FarkasAP,
143
    TrichotomyAP,
144
    EqualityEngineAP,
145
    IntTightenAP,
146
    IntHoleAP};
147
148
/**
149
 * The types of constraints.
150
 * The convex constraints are the constraints are LowerBound, Equality,
151
 * and UpperBound.
152
 */
153
enum ConstraintType {LowerBound, Equality, UpperBound, Disequality};
154
155
156
typedef context::CDList<ConstraintCP> CDConstraintList;
157
158
typedef std::unordered_map<Node, ConstraintP> NodetoConstraintMap;
159
160
typedef size_t ConstraintRuleID;
161
static const ConstraintRuleID ConstraintRuleIdSentinel = std::numeric_limits<ConstraintRuleID>::max();
162
163
typedef size_t AntecedentId;
164
static const AntecedentId AntecedentIdSentinel = std::numeric_limits<AntecedentId>::max();
165
166
167
typedef size_t AssertionOrder;
168
static const AssertionOrder AssertionOrderSentinel = std::numeric_limits<AssertionOrder>::max();
169
170
171
/**
172
 * A ValueCollection binds together convex constraints that have the same
173
 * DeltaRational value.
174
 */
175
class ValueCollection {
176
private:
177
178
  ConstraintP d_lowerBound;
179
  ConstraintP d_upperBound;
180
  ConstraintP d_equality;
181
  ConstraintP d_disequality;
182
183
public:
184
  ValueCollection();
185
186
  static ValueCollection mkFromConstraint(ConstraintP c);
187
188
  bool hasLowerBound() const;
189
  bool hasUpperBound() const;
190
  bool hasEquality() const;
191
  bool hasDisequality() const;
192
193
  bool hasConstraintOfType(ConstraintType t) const;
194
195
  ConstraintP getLowerBound() const;
196
  ConstraintP getUpperBound() const;
197
  ConstraintP getEquality() const;
198
  ConstraintP getDisequality() const;
199
200
  ConstraintP getConstraintOfType(ConstraintType t) const;
201
202
  /** Returns true if any of the constraints are non-null. */
203
  bool empty() const;
204
205
  /**
206
   * Remove the constraint of the type t from the collection.
207
   * Returns true if the ValueCollection is now empty.
208
   * If true is returned, d_value is now NULL.
209
   */
210
  void remove(ConstraintType t);
211
212
  /**
213
   * Adds a constraint to the set.
214
   * The collection must not have a constraint of that type already.
215
   */
216
  void add(ConstraintP c);
217
218
  void push_into(std::vector<ConstraintP>& vec) const;
219
220
  ConstraintP nonNull() const;
221
222
  ArithVar getVariable() const;
223
  const DeltaRational& getValue() const;
224
};
225
226
/**
227
 * A Map of ValueCollections sorted by the associated DeltaRational values.
228
 *
229
 * Discussion:
230
 * While it is more natural to consider this a set, this cannot be a set as in
231
 * sets the type of both iterator and const_iterator in sets are
232
 * "constant iterators".  We require iterators that dereference to
233
 * ValueCollection&.
234
 *
235
 * See:
236
 * http://gcc.gnu.org/onlinedocs/libstdc++/ext/lwg-defects.html#103
237
 */
238
typedef std::map<DeltaRational, ValueCollection> SortedConstraintMap;
239
typedef SortedConstraintMap::iterator SortedConstraintMapIterator;
240
typedef SortedConstraintMap::const_iterator SortedConstraintMapConstIterator;
241
242
/** A Pair associating a variables and a Sorted ConstraintSet. */
243
161847
struct PerVariableDatabase{
244
  ArithVar d_var;
245
  SortedConstraintMap d_constraints;
246
247
  // x ? c_1, x ? c_2, x ? c_3, ...
248
  // where ? is a non-empty subset of {lb, ub, eq}
249
  // c_1 < c_2 < c_3 < ...
250
251
161847
  PerVariableDatabase(ArithVar v) : d_var(v), d_constraints() {}
252
253
  bool empty() const {
254
    return d_constraints.empty();
255
  }
256
257
  static bool IsEmpty(const PerVariableDatabase& p){
258
    return p.empty();
259
  }
260
};
261
262
/**
263
 * If proofs are on, there is a vector of rationals for farkas coefficients.
264
 * This is the owner of the memory for the vector, and calls delete upon
265
 * cleanup.
266
 *
267
 */
268
struct ConstraintRule {
269
  ConstraintP d_constraint;
270
  ArithProofType d_proofType;
271
  AntecedentId d_antecedentEnd;
272
273
  /**
274
   * In this comment, we abbreviate ConstraintDatabase::d_antecedents
275
   * and d_farkasCoefficients as ans and fc.
276
   *
277
   * This list is always empty if proofs are not enabled.
278
   *
279
   * If proofs are enabled, the proof of constraint c at p in ans[p] of length n
280
   * is (NullConstraint, ans[p-(n-1)], ... , ans[p-1], ans[p])
281
   *
282
   * Farkas' proofs show a contradiction with the negation of c, c_not =
283
   * c->getNegation().
284
   *
285
   * We treat the position for NullConstraint (p-n) as the position for the
286
   * farkas coefficient for so we pretend c_not is ans[p-n]. So this correlation
287
   * for the constraints we are going to use: (c_not, ans[p-n+(1)], ... ,
288
   * ans[p-n+(n-1)], ans[p-n+(n)]) With the coefficients at positions: (fc[0],
289
   * fc[1)], ... fc[n])
290
   *
291
   * The index of the constraints in the proof are {i | i <= 0 <= n] } (with
292
   * c_not being p-n). Partition the indices into L, U, and E, the lower bounds,
293
   * the upper bounds and equalities.
294
   *
295
   * We standardize the proofs to be upper bound oriented following the
296
   * convention: A x <= b with the proof witness of the form (lambda) Ax <=
297
   * (lambda) b and lambda >= 0.
298
   *
299
   * To accomplish this cleanly, the fc coefficients must be negative for lower
300
   * bounds. The signs of equalities can be either positive or negative.
301
   *
302
   * Thus the proof corresponds to (with multiplication over inequalities):
303
   *    \sum_{u in U} fc[u] ans[p-n+u] + \sum_{e in E} fc[e] ans[p-n+e]
304
   *  + \sum_{l in L} fc[l] ans[p-n+l]
305
   * |= 0 < 0
306
   * where fc[u] > 0, fc[l] < 0, and fc[e] != 0 (i.e. it can be either +/-).
307
   *
308
   * There is no requirement that the proof is minimal.
309
   * We do however use all of the constraints by requiring non-zero
310
   * coefficients.
311
   */
312
  RationalVectorCP d_farkasCoefficients;
313
  ConstraintRule()
314
    : d_constraint(NullConstraint)
315
    , d_proofType(NoAP)
316
    , d_antecedentEnd(AntecedentIdSentinel)
317
  {
318
    d_farkasCoefficients = RationalVectorCPSentinel;
319
  }
320
321
4976123
  ConstraintRule(ConstraintP con, ArithProofType pt)
322
4976123
    : d_constraint(con)
323
    , d_proofType(pt)
324
4976123
    , d_antecedentEnd(AntecedentIdSentinel)
325
  {
326
4976123
    d_farkasCoefficients = RationalVectorCPSentinel;
327
4976123
  }
328
1564376
  ConstraintRule(ConstraintP con, ArithProofType pt, AntecedentId antecedentEnd)
329
1564376
    : d_constraint(con)
330
    , d_proofType(pt)
331
1564376
    , d_antecedentEnd(antecedentEnd)
332
  {
333
1564376
    d_farkasCoefficients = RationalVectorCPSentinel;
334
1564376
  }
335
336
2255054
  ConstraintRule(ConstraintP con, ArithProofType pt, AntecedentId antecedentEnd, RationalVectorCP coeffs)
337
2255054
    : d_constraint(con)
338
    , d_proofType(pt)
339
2255054
    , d_antecedentEnd(antecedentEnd)
340
  {
341
2255054
    Assert(ARITH_PROOF_ON() || coeffs == RationalVectorCPSentinel);
342
2255054
    d_farkasCoefficients = coeffs;
343
2255054
  }
344
345
  void print(std::ostream& out) const;
346
  void debugPrint() const;
347
}; /* class ConstraintRule */
348
349
class Constraint {
350
351
  friend class ConstraintDatabase;
352
353
 public:
354
  /**
355
   * This begins construction of a minimal constraint.
356
   *
357
   * This should only be called by ConstraintDatabase.
358
   *
359
   * Because of circular dependencies a Constraint is not fully valid until
360
   * initialize has been called on it.
361
   */
362
  Constraint(ArithVar x,  ConstraintType t, const DeltaRational& v);
363
364
  /**
365
   * Destructor for a constraint.
366
   * This should only be called if safeToGarbageCollect() is true.
367
   */
368
  ~Constraint();
369
370
  static ConstraintType constraintTypeOfComparison(const Comparison& cmp);
371
372
19779652
  inline ConstraintType getType() const {
373
19779652
    return d_type;
374
  }
375
376
27057713
  inline ArithVar getVariable() const {
377
27057713
    return d_variable;
378
  }
379
380
43264003
  const DeltaRational& getValue() const {
381
43264003
    return d_value;
382
  }
383
384
4162274
  inline ConstraintP getNegation() const {
385
4162274
    return d_negation;
386
  }
387
388
7399407
  bool isEquality() const{
389
7399407
    return d_type == Equality;
390
  }
391
777396
  bool isDisequality() const{
392
777396
    return d_type == Disequality;
393
  }
394
5271039
  bool isLowerBound() const{
395
5271039
    return d_type == LowerBound;
396
  }
397
4916128
  bool isUpperBound() const{
398
4916128
    return d_type == UpperBound;
399
  }
400
1248073
  bool isStrictUpperBound() const{
401
1248073
    Assert(isUpperBound());
402
1248073
    return getValue().infinitesimalSgn() < 0;
403
  }
404
405
1250269
  bool isStrictLowerBound() const{
406
1250269
    Assert(isLowerBound());
407
1250269
    return getValue().infinitesimalSgn() > 0;
408
  }
409
410
1671765
  bool isSplit() const {
411
1671765
    return d_split;
412
  }
413
414
  /**
415
   * Splits the node in the user context.
416
   * Returns a lemma that is assumed to be true for the rest of the user context.
417
   * Constraint must be an equality or disequality.
418
   */
419
  TrustNode split();
420
421
10481321
  bool canBePropagated() const {
422
10481321
    return d_canBePropagated;
423
  }
424
  void setCanBePropagated();
425
426
  /**
427
   * Light wrapper for calling setCanBePropagated(),
428
   * on this and this->d_negation.
429
   */
430
407532
  void setPreregistered(){
431
407532
    setCanBePropagated();
432
407532
    d_negation->setCanBePropagated();
433
407532
  }
434
435
57340458
  bool assertedToTheTheory() const {
436
57340458
    Assert((d_assertionOrder < AssertionOrderSentinel) != d_witness.isNull());
437
57340458
    return d_assertionOrder < AssertionOrderSentinel;
438
  }
439
6201617
  TNode getWitness() const {
440
6201617
    Assert(assertedToTheTheory());
441
6201617
    return d_witness;
442
  }
443
444
5673506
  bool assertedBefore(AssertionOrder time) const {
445
5673506
    return d_assertionOrder < time;
446
  }
447
448
  /**
449
   * Sets the witness literal for a node being on the assertion stack.
450
   *
451
   * If the negation of the node is true, inConflict must be true.
452
   * If the negation of the node is false, inConflict must be false.
453
   * Hence, negationHasProof() == inConflict.
454
   *
455
   * This replaces:
456
   *   void setAssertedToTheTheory(TNode witness);
457
   *   void setAssertedToTheTheoryWithNegationTrue(TNode witness);
458
   */
459
  void setAssertedToTheTheory(TNode witness, bool inConflict);
460
461
14022479
  bool hasLiteral() const {
462
14022479
    return !d_literal.isNull();
463
  }
464
465
  void setLiteral(Node n);
466
467
1582802
  Node getLiteral() const {
468
1582802
    Assert(hasLiteral());
469
1582802
    return d_literal;
470
  }
471
472
  /** Gets a literal in the normal form suitable for proofs.
473
   * That is, (sum of non-const monomials) >< const.
474
   *
475
   * This is a sister method to `getLiteral`, which returns a normal form
476
   * literal, suitable for external solving use.
477
   */
478
  Node getProofLiteral() const;
479
480
  /**
481
   * Set the node as having a proof and being an assumption.
482
   * The node must be assertedToTheTheory().
483
   *
484
   * Precondition: negationHasProof() == inConflict.
485
   *
486
   * Replaces:
487
   *  selfExplaining().
488
   *  selfExplainingWithNegationTrue().
489
   */
490
  void setAssumption(bool inConflict);
491
492
  /** Returns true if the node is an assumption.*/
493
  bool isAssumption() const;
494
495
  /** Set the constraint to have an EqualityEngine proof. */
496
  void setEqualityEngineProof();
497
  bool hasEqualityEngineProof() const;
498
499
  /** Returns true if the node has a Farkas' proof. */
500
  bool hasFarkasProof() const;
501
502
  /**
503
   * @brief Returns whether this constraint is provable using a Farkas
504
   * proof applied to (possibly tightened) input assertions.
505
   *
506
   * An example of a constraint that has a simple Farkas proof:
507
   *    x <= 0 proven from x + y <= 0 and x - y <= 0.
508
   *
509
   * An example of another constraint that has a simple Farkas proof:
510
   *    x <= 0 proven from x + y <= 0 and x - y <= 0.5 for integers x, y
511
   *       (integer bound-tightening is applied first!).
512
   *
513
   * An example of a constraint that might be proven **without** a simple
514
   * Farkas proof:
515
   *    x < 0 proven from not(x == 0) and not(x > 0).
516
   *
517
   * This could be proven internally by the arithmetic theory using
518
   * `TrichotomyAP` as the proof type.
519
   *
520
   */
521
  bool hasSimpleFarkasProof() const;
522
  /**
523
   * Returns whether this constraint is an assumption or a tightened
524
   * assumption.
525
   */
526
  bool isPossiblyTightenedAssumption() const;
527
528
  /** Returns true if the node has a int bound tightening proof. */
529
  bool hasIntTightenProof() const;
530
531
  /** Returns true if the node has a int hole proof. */
532
  bool hasIntHoleProof() const;
533
534
  /** Returns true if the node has a trichotomy proof. */
535
  bool hasTrichotomyProof() const;
536
537
  void printProofTree(std::ostream & out, size_t depth = 0) const;
538
539
  /**
540
   * A sets the constraint to be an internal assumption.
541
   *
542
   * This does not need to have a witness or an associated literal.
543
   * This is always itself in the explanation fringe for both conflicts
544
   * and propagation.
545
   * This cannot be converted back into a Node conflict or explanation.
546
   *
547
   * This cannot have a proof or be asserted to the theory!
548
   *
549
   */
550
  void setInternalAssumption(bool inConflict);
551
  bool isInternalAssumption() const;
552
553
  /**
554
   * Returns a explanation of the constraint that is appropriate for conflicts.
555
   *
556
   * This is not appropriate for propagation!
557
   *
558
   * This is the minimum fringe of the implication tree s.t.
559
   * every constraint is assertedToTheTheory() or hasEqualityEngineProof().
560
   */
561
  TrustNode externalExplainByAssertions() const;
562
563
  /**
564
   * Writes an explanation of a constraint into the node builder.
565
   * Pushes back an explanation that is acceptable to send to the sat solver.
566
   * nb is assumed to be an AND.
567
   *
568
   * This is the minimum fringe of the implication tree s.t.
569
   * every constraint is assertedToTheTheory() or hasEqualityEngineProof().
570
   *
571
   * This is not appropriate for propagation!
572
   * Use explainForPropagation() instead.
573
   */
574
2913464
  std::shared_ptr<ProofNode> externalExplainByAssertions(NodeBuilder& nb) const
575
  {
576
2913464
    return externalExplain(nb, AssertionOrderSentinel);
577
  }
578
579
  /* Equivalent to calling externalExplainByAssertions on all constraints in b */
580
  static Node externalExplainByAssertions(const ConstraintCPVec& b);
581
  static Node externalExplainByAssertions(ConstraintCP a, ConstraintCP b);
582
  static Node externalExplainByAssertions(ConstraintCP a, ConstraintCP b, ConstraintCP c);
583
584
  /**
585
   * This is the minimum fringe of the implication tree s.t. every constraint is
586
   * - assertedToTheTheory(),
587
   * - isInternalDecision() or
588
   * - hasEqualityEngineProof().
589
   */
590
  static void assertionFringe(ConstraintCPVec& v);
591
  static void assertionFringe(ConstraintCPVec& out, const ConstraintCPVec& in);
592
593
  /** The fringe of a farkas' proof. */
594
  bool onFringe() const {
595
    return assertedToTheTheory() || isInternalAssumption() || hasEqualityEngineProof();
596
  }
597
598
  /**
599
   * Returns an explanation of a propagation by the ConstraintDatabase.
600
   * The constraint must have a proof.
601
   * The constraint cannot be an assumption.
602
   *
603
   * This is the minimum fringe of the implication tree (excluding the
604
   * constraint itself) s.t. every constraint is assertedToTheTheory() or
605
   * hasEqualityEngineProof().
606
   *
607
   * All return conjuncts were asserted before this constraint.
608
   *
609
   * Requires the given node to rewrite to the canonical literal for this
610
   * constraint.
611
   *
612
   * @params n the literal to prove
613
   *           n must rewrite to the constraint's canonical literal
614
   *
615
   * @returns a trust node of the form:
616
   *         (=> explanation n)
617
   */
618
  TrustNode externalExplainForPropagation(TNode n) const;
619
620
  /**
621
   * Explain the constraint and its negation in terms of assertions.
622
   * The constraint must be in conflict.
623
   */
624
  TrustNode externalExplainConflict() const;
625
626
  /** The constraint is known to be true. */
627
158259244
  inline bool hasProof() const {
628
158259244
    return d_crid != ConstraintRuleIdSentinel;
629
  }
630
631
  /** The negation of the constraint is known to hold. */
632
51290364
  inline bool negationHasProof() const {
633
51290364
    return d_negation->hasProof();
634
  }
635
636
  /** Neither the contraint has a proof nor the negation has a proof.*/
637
205156
  bool truthIsUnknown() const {
638
205156
    return !hasProof() && !negationHasProof();
639
  }
640
641
  /** This is a synonym for hasProof(). */
642
15231391
  inline bool isTrue() const {
643
15231391
    return hasProof();
644
  }
645
646
  /** Both the constraint and its negation are true. */
647
10034169
  inline bool inConflict() const {
648
10034169
    return hasProof() && negationHasProof();
649
  }
650
651
  /**
652
   * Returns the constraint that corresponds to taking
653
   *    x r ceiling(getValue()) where r is the node's getType().
654
   * Esstentially this is an up branch.
655
   */
656
  ConstraintP getCeiling();
657
658
  /**
659
   * Returns the constraint that corresponds to taking
660
   *    x r floor(getValue()) where r is the node's getType().
661
   * Esstentially this is a down branch.
662
   */
663
  ConstraintP getFloor();
664
665
  static ConstraintP makeNegation(ArithVar v, ConstraintType t, const DeltaRational& r);
666
667
  const ValueCollection& getValueCollection() const;
668
669
670
  ConstraintP getStrictlyWeakerUpperBound(bool hasLiteral, bool mustBeAsserted) const;
671
  ConstraintP getStrictlyWeakerLowerBound(bool hasLiteral, bool mustBeAsserted) const;
672
673
  /**
674
   * Marks a the constraint c as being entailed by a.
675
   * The Farkas proof 1*(a) + -1 (c) |= 0<0
676
   *
677
   * After calling impliedByUnate(), the caller should either raise a conflict
678
   * or try call tryToPropagate().
679
   */
680
  void impliedByUnate(ConstraintCP a, bool inConflict);
681
682
  /**
683
   * Marks a the constraint c as being entailed by a.
684
   * The reason has to do with integer bound tightening.
685
   *
686
   * After calling impliedByIntTighten(), the caller should either raise a conflict
687
   * or try call tryToPropagate().
688
   */
689
  void impliedByIntTighten(ConstraintCP a, bool inConflict);
690
691
  /**
692
   * Marks a the constraint c as being entailed by a.
693
   * The reason has to do with integer reasoning.
694
   *
695
   * After calling impliedByIntHole(), the caller should either raise a conflict
696
   * or try call tryToPropagate().
697
   */
698
  void impliedByIntHole(ConstraintCP a, bool inConflict);
699
700
  /**
701
   * Marks a the constraint c as being entailed by a.
702
   * The reason has to do with integer reasoning.
703
   *
704
   * After calling impliedByIntHole(), the caller should either raise a conflict
705
   * or try call tryToPropagate().
706
   */
707
  void impliedByIntHole(const ConstraintCPVec& b, bool inConflict);
708
709
  /**
710
   * This is a lemma of the form:
711
   *   x < d or x = d or x > d
712
   * The current constraint c is one of the above constraints and {a,b}
713
   * are the negation of the other two constraints.
714
   *
715
   * Preconditions:
716
   * - negationHasProof() == inConflict.
717
   *
718
   * After calling impliedByTrichotomy(), the caller should either raise a conflict
719
   * or try call tryToPropagate().
720
   */
721
  void impliedByTrichotomy(ConstraintCP a, ConstraintCP b, bool inConflict);
722
723
  /**
724
   * Marks the node as having a Farkas proof.
725
   *
726
   * Preconditions:
727
   * - coeffs == NULL if proofs are off.
728
   * - See the comments for ConstraintRule for the form of coeffs when
729
   *   proofs are on.
730
   * - negationHasProof() == inConflict.
731
   *
732
   * After calling impliedByFarkas(), the caller should either raise a conflict
733
   * or try call tryToPropagate().
734
   */
735
  void impliedByFarkas(const ConstraintCPVec& b, RationalVectorCP coeffs, bool inConflict);
736
737
  /**
738
   * Generates an implication node, B => getLiteral(),
739
   * where B is the result of externalExplainByAssertions(b).
740
   * Does not guarantee b is the explanation of the constraint.
741
   */
742
  Node externalImplication(const ConstraintCPVec& b) const;
743
744
  /**
745
   * Returns true if the variable is assigned the value dr,
746
   * the constraint would be satisfied.
747
   */
748
  bool satisfiedBy(const DeltaRational& dr) const;
749
750
  /**
751
   * The node must have a proof already and be eligible for propagation!
752
   * You probably want to call tryToPropagate() instead.
753
   *
754
   * Preconditions:
755
   * - hasProof()
756
   * - canBePropagated()
757
   * - !assertedToTheTheory()
758
   */
759
  void propagate();
760
761
  /**
762
   * If the constraint
763
   *   canBePropagated() and
764
   *   !assertedToTheTheory(),
765
   * the constraint is added to the database's propagation queue.
766
   *
767
   * Precondition:
768
   * - hasProof()
769
   */
770
  void tryToPropagate();
771
772
  /**
773
   * Returns a reference to the containing database.
774
   * Precondition: the constraint must be initialized.
775
   */
776
  const ConstraintDatabase& getDatabase() const;
777
778
  /** Returns the constraint rule at the position. */
779
  const ConstraintRule& getConstraintRule() const;
780
781
 private:
782
  /**  Returns true if the constraint has been initialized. */
783
  bool initialized() const;
784
785
  /**
786
   * This initializes the fields that cannot be set in the constructor due to
787
   * circular dependencies.
788
   */
789
  void initialize(ConstraintDatabase* db,
790
                  SortedConstraintMapIterator v,
791
                  ConstraintP negation);
792
793
  class ConstraintRuleCleanup
794
  {
795
   public:
796
8795553
    inline void operator()(ConstraintRule* crp)
797
    {
798
8795553
      Assert(crp != NULL);
799
8795553
      ConstraintP constraint = crp->d_constraint;
800
8795553
      Assert(constraint->d_crid != ConstraintRuleIdSentinel);
801
8795553
      constraint->d_crid = ConstraintRuleIdSentinel;
802
8795553
      ARITH_PROOF({
803
        if (crp->d_farkasCoefficients != RationalVectorCPSentinel)
804
        {
805
          delete crp->d_farkasCoefficients;
806
        }
807
      });
808
8795553
    }
809
  };
810
811
  class CanBePropagatedCleanup
812
  {
813
   public:
814
815064
    inline void operator()(ConstraintP* p)
815
    {
816
815064
      ConstraintP constraint = *p;
817
815064
      Assert(constraint->d_canBePropagated);
818
815064
      constraint->d_canBePropagated = false;
819
815064
    }
820
  };
821
822
  class AssertionOrderCleanup
823
  {
824
   public:
825
5705763
    inline void operator()(ConstraintP* p)
826
    {
827
5705763
      ConstraintP constraint = *p;
828
5705763
      Assert(constraint->assertedToTheTheory());
829
5705763
      constraint->d_assertionOrder = AssertionOrderSentinel;
830
5705763
      constraint->d_witness = TNode::null();
831
5705763
      Assert(!constraint->assertedToTheTheory());
832
5705763
    }
833
  };
834
835
  class SplitCleanup
836
  {
837
   public:
838
30110
    inline void operator()(ConstraintP* p)
839
    {
840
30110
      ConstraintP constraint = *p;
841
30110
      Assert(constraint->d_split);
842
30110
      constraint->d_split = false;
843
30110
    }
844
  };
845
846
  /**
847
   * Returns true if the node is safe to garbage collect.
848
   * Both it and its negation must have no context dependent data set.
849
   */
850
  bool safeToGarbageCollect() const;
851
852
  /**
853
   * Returns true if the constraint has no context dependent data set.
854
   */
855
  bool contextDependentDataIsSet() const;
856
857
  /**
858
   * Returns true if the node correctly corresponds to the constraint that is
859
   * being set.
860
   */
861
  bool sanityChecking(Node n) const;
862
863
  /** Returns a reference to the map for d_variable. */
864
  SortedConstraintMap& constraintSet() const;
865
866
  /** Returns coefficients for the proofs for farkas cancellation. */
867
  static std::pair<int, int> unateFarkasSigns(ConstraintCP a, ConstraintCP b);
868
869
  Node externalExplain(AssertionOrder order) const;
870
  /**
871
   * Returns an explanation of that was assertedBefore(order).
872
   * The constraint must have a proof.
873
   * The constraint cannot be selfExplaining().
874
   *
875
   * This is the minimum fringe of the implication tree
876
   * s.t. every constraint is assertedBefore(order) or hasEqualityEngineProof().
877
   */
878
  std::shared_ptr<ProofNode> externalExplain(NodeBuilder& nb,
879
                                             AssertionOrder order) const;
880
881
  static Node externalExplain(const ConstraintCPVec& b, AssertionOrder order);
882
883
23190824
  inline ArithProofType getProofType() const {
884
23190824
    return getConstraintRule().d_proofType;
885
  }
886
887
617146
  inline AntecedentId getEndAntecedent() const {
888
617146
    return getConstraintRule().d_antecedentEnd;
889
  }
890
891
12026
  inline RationalVectorCP getFarkasCoefficients() const
892
  {
893
12026
    return ARITH_NULLPROOF(getConstraintRule().d_farkasCoefficients);
894
  }
895
896
  void debugPrint() const;
897
898
  /**
899
   * The proof of the node is empty.
900
   * The proof must be a special proof. Either
901
   *   isSelfExplaining() or
902
   *    hasEqualityEngineProof()
903
   */
904
  bool antecentListIsEmpty() const;
905
906
  bool antecedentListLengthIsOne() const;
907
908
  /** Return true if every element in b has a proof. */
909
  static bool allHaveProof(const ConstraintCPVec& b);
910
911
  /** Precondition: hasFarkasProof()
912
   * Computes the combination implied by the farkas coefficients. Sees if it is
913
   * a contradiction.
914
   */
915
916
  bool wellFormedFarkasProof() const;
917
918
  /** The ArithVar associated with the constraint. */
919
  const ArithVar d_variable;
920
921
  /** The type of the Constraint. */
922
  const ConstraintType d_type;
923
924
  /** The DeltaRational value with the constraint. */
925
  const DeltaRational d_value;
926
927
  /** A pointer to the associated database for the Constraint. */
928
  ConstraintDatabase* d_database;
929
930
  /**
931
   * The node to be communicated with the TheoryEngine.
932
   *
933
   * This is not context dependent, but may be set once.
934
   *
935
   * This must be set if the constraint canBePropagated().
936
   * This must be set if the constraint assertedToTheTheory().
937
   * Otherwise, this may be null().
938
   */
939
  Node d_literal;
940
941
  /** Pointer to the negation of the Constraint. */
942
  ConstraintP d_negation;
943
944
  /**
945
   * This is true if the associated node can be propagated.
946
   *
947
   * This should be enabled if the node has been preregistered.
948
   *
949
   * Sat Context Dependent.
950
   * This is initially false.
951
   */
952
  bool d_canBePropagated;
953
954
  /**
955
   * This is the order the constraint was asserted to the theory.
956
   * If this has been set, the node can be used in conflicts.
957
   * If this is c.d_assertedOrder < d.d_assertedOrder, then c can be used in the
958
   * explanation of d.
959
   *
960
   * This should be set after the literal is dequeued by Theory::get().
961
   *
962
   * Sat Context Dependent.
963
   * This is initially AssertionOrderSentinel.
964
   */
965
  AssertionOrder d_assertionOrder;
966
967
  /**
968
   * This is guaranteed to be on the fact queue.
969
   * For example if x + y = x + 1 is on the fact queue, then use this
970
   */
971
  TNode d_witness;
972
973
  /**
974
   * The position of the constraint in the constraint rule id.
975
   *
976
   * Sat Context Dependent.
977
   * This is initially
978
   */
979
  ConstraintRuleID d_crid;
980
981
  /**
982
   * True if the equality has been split.
983
   * Only meaningful if ConstraintType == Equality.
984
   *
985
   * User Context Dependent.
986
   * This is initially false.
987
   */
988
  bool d_split;
989
990
  /**
991
   * Position in sorted constraint set for the variable.
992
   * Unset if d_type is Disequality.
993
   */
994
  SortedConstraintMapIterator d_variablePosition;
995
996
}; /* class ConstraintValue */
997
998
std::ostream& operator<<(std::ostream& o, const Constraint& c);
999
std::ostream& operator<<(std::ostream& o, const ConstraintP c);
1000
std::ostream& operator<<(std::ostream& o, const ConstraintCP c);
1001
std::ostream& operator<<(std::ostream& o, const ConstraintType t);
1002
std::ostream& operator<<(std::ostream& o, const ValueCollection& c);
1003
std::ostream& operator<<(std::ostream& o, const ConstraintCPVec& v);
1004
std::ostream& operator<<(std::ostream& o, const ArithProofType);
1005
1006
1007
class ConstraintDatabase {
1008
private:
1009
  /**
1010
   * The map from ArithVars to their unique databases.
1011
   * When the vector changes size, we cannot allow the maps to move so this
1012
   * is a vector of pointers.
1013
   */
1014
  std::vector<PerVariableDatabase*> d_varDatabases;
1015
1016
  SortedConstraintMap& getVariableSCM(ArithVar v) const;
1017
1018
  /** Maps literals to constraints.*/
1019
  NodetoConstraintMap d_nodetoConstraintMap;
1020
1021
  /**
1022
   * A queue of propagated constraints.
1023
   * ConstraintCP are pointers.
1024
   * The elements of the queue do not require destruction.
1025
   */
1026
  context::CDQueue<ConstraintCP> d_toPropagate;
1027
1028
  /**
1029
   * Proofs are lists of valid constraints terminated by the first null
1030
   * sentinel value in the proof list.
1031
   * We abbreviate d_antecedents as ans in the comment.
1032
   *
1033
   * The proof at p in ans[p] of length n is
1034
   *  (NullConstraint, ans[p-(n-1)], ... , ans[p-1], ans[p])
1035
   *
1036
   * The proof at p corresponds to the conjunction:
1037
   *  (and x_i)
1038
   *
1039
   * So the proof of a Constraint c corresponds to the horn clause:
1040
   *  (implies (and x_i) c)
1041
   * where (and x_i) is the proof at c.d_crid d_antecedentEnd.
1042
   *
1043
   * Constraints are pointers so this list is designed not to require any destruction.
1044
   */
1045
  CDConstraintList d_antecedents;
1046
1047
  typedef context::CDList<ConstraintRule, Constraint::ConstraintRuleCleanup> ConstraintRuleList;
1048
  typedef context::CDList<ConstraintP, Constraint::CanBePropagatedCleanup> CBPList;
1049
  typedef context::CDList<ConstraintP, Constraint::AssertionOrderCleanup> AOList;
1050
  typedef context::CDList<ConstraintP, Constraint::SplitCleanup> SplitList;
1051
1052
1053
1054
  /**
1055
   * The watch lists are collected together as they need to be garbage collected
1056
   * carefully.
1057
   */
1058
9853
  struct Watches{
1059
1060
    /**
1061
     * Contains the exact list of constraints that have a proof.
1062
     * Upon pop, this unsets d_crid to NoAP.
1063
     *
1064
     * The index in this list is the proper ordering of the proofs.
1065
     */
1066
    ConstraintRuleList d_constraintProofs;
1067
1068
    /**
1069
     * Contains the exact list of constraints that can be used for propagation.
1070
     */
1071
    CBPList d_canBePropagatedWatches;
1072
1073
    /**
1074
     * Contains the exact list of constraints that have been asserted to the theory.
1075
     */
1076
    AOList d_assertionOrderWatches;
1077
1078
1079
    /**
1080
     * Contains the exact list of atoms that have been preregistered.
1081
     * This is a pointer as it must be destroyed before the elements of
1082
     * d_varDatabases.
1083
     */
1084
    SplitList d_splitWatches;
1085
    Watches(context::Context* satContext, context::Context* userContext);
1086
  };
1087
  Watches* d_watches;
1088
1089
  void pushSplitWatch(ConstraintP c);
1090
  void pushCanBePropagatedWatch(ConstraintP c);
1091
  void pushAssertionOrderWatch(ConstraintP c, TNode witness);
1092
1093
  /** Assumes that antecedents have already been pushed. */
1094
  void pushConstraintRule(const ConstraintRule& crp);
1095
1096
  /** Returns true if all of the entries of the vector are empty. */
1097
  static bool emptyDatabase(const std::vector<PerVariableDatabase>& vec);
1098
1099
  /** Map from nodes to arithvars. */
1100
  const ArithVariables& d_avariables;
1101
1102
1716546
  const ArithVariables& getArithVariables() const{
1103
1716546
    return d_avariables;
1104
  }
1105
1106
  ArithCongruenceManager& d_congruenceManager;
1107
1108
  const context::Context * const d_satContext;
1109
  /** Owned by the TheoryArithPrivate, used here. */
1110
  EagerProofGenerator* d_pfGen;
1111
  /** Owned by the TheoryArithPrivate, used here. */
1112
  ProofNodeManager* d_pnm;
1113
1114
  RaiseConflict d_raiseConflict;
1115
1116
1117
  const Rational d_one;
1118
  const Rational d_negOne;
1119
1120
  friend class Constraint;
1121
1122
 public:
1123
  ConstraintDatabase(context::Context* satContext,
1124
                     context::Context* userContext,
1125
                     const ArithVariables& variables,
1126
                     ArithCongruenceManager& dm,
1127
                     RaiseConflict conflictCallBack,
1128
                     EagerProofGenerator* pfGen,
1129
                     ProofNodeManager* pnm);
1130
1131
  ~ConstraintDatabase();
1132
1133
  /** Adds a literal to the database. */
1134
  ConstraintP addLiteral(TNode lit);
1135
1136
  /**
1137
   * If hasLiteral() is true, returns the constraint.
1138
   * Otherwise, returns NullConstraint.
1139
   */
1140
  ConstraintP lookup(TNode literal) const;
1141
1142
  /**
1143
   * Returns true if the literal has been added to the database.
1144
   * This is a hash table lookup.
1145
   * It does not look in the database for an equivalent corresponding constraint.
1146
   */
1147
  bool hasLiteral(TNode literal) const;
1148
1149
4195756
  bool hasMorePropagations() const{
1150
4195756
    return !d_toPropagate.empty();
1151
  }
1152
1153
737146
  ConstraintCP nextPropagation(){
1154
737146
    Assert(hasMorePropagations());
1155
1156
737146
    ConstraintCP p = d_toPropagate.front();
1157
737146
    d_toPropagate.pop();
1158
1159
737146
    return p;
1160
  }
1161
1162
  void addVariable(ArithVar v);
1163
  bool variableDatabaseIsSetup(ArithVar v) const;
1164
  void removeVariable(ArithVar v);
1165
1166
  /** Get an explanation and proof for this constraint from the equality engine
1167
   */
1168
  TrustNode eeExplain(ConstraintCP c) const;
1169
  /** Get an explanation for this constraint from the equality engine */
1170
  void eeExplain(ConstraintCP c, NodeBuilder& nb) const;
1171
1172
  /**
1173
   * Returns a constraint with the variable v, the constraint type t, and a value
1174
   * dominated by r (explained below) if such a constraint exists in the database.
1175
   * If no such constraint exists, NullConstraint is returned.
1176
   *
1177
   * t must be either UpperBound or LowerBound.
1178
   * The returned value v is dominated:
1179
   *  If t is UpperBound, r <= v
1180
   *  If t is LowerBound, r >= v
1181
   *
1182
   * variableDatabaseIsSetup(v) must be true.
1183
   */
1184
  ConstraintP getBestImpliedBound(ArithVar v, ConstraintType t, const DeltaRational& r) const;
1185
1186
  /** Returns the constraint, if it exists */
1187
  ConstraintP lookupConstraint(ArithVar v, ConstraintType t, const DeltaRational& r) const;
1188
1189
  /**
1190
   * Returns a constraint with the variable v, the constraint type t and the value r.
1191
   * If there is such a constraint in the database already, it is returned.
1192
   * If there is no such constraint, this constraint is added to the database.
1193
   *
1194
   */
1195
  ConstraintP getConstraint(ArithVar v, ConstraintType t, const DeltaRational& r);
1196
1197
  /**
1198
   * Returns a constraint of the given type for the value and variable
1199
   * for the given ValueCollection, vc.
1200
   * This is made if there is no such constraint.
1201
   */
1202
  ConstraintP ensureConstraint(ValueCollection& vc, ConstraintType t);
1203
1204
1205
  void deleteConstraintAndNegation(ConstraintP c);
1206
1207
  /** Given constraints `a` and `b` such that `a OR b` by unate reasoning,
1208
   *  adds a TrustNode to `out` which proves `a OR b` as a lemma.
1209
   *
1210
   *  Example: `x <= 5` OR `5 <= x`.
1211
   */
1212
  void proveOr(std::vector<TrustNode>& out,
1213
               ConstraintP a,
1214
               ConstraintP b,
1215
               bool negateSecond) const;
1216
  /** Given constraints `a` and `b` such that `a` implies `b` by unate
1217
   * reasoning, adds a TrustNode to `out` which proves `-a OR b` as a lemma.
1218
   *
1219
   *  Example: `x >= 5` -> `x >= 4`.
1220
   */
1221
  void implies(std::vector<TrustNode>& out, ConstraintP a, ConstraintP b) const;
1222
  /** Given constraints `a` and `b` such that `not(a AND b)` by unate reasoning,
1223
   *  adds a TrustNode to `out` which proves `-a OR -b` as a lemma.
1224
   *
1225
   *  Example: `x >= 4` -> `x <= 3`.
1226
   */
1227
  void mutuallyExclusive(std::vector<TrustNode>& out,
1228
                         ConstraintP a,
1229
                         ConstraintP b) const;
1230
1231
  /**
1232
   * Outputs a minimal set of unate implications onto the vector for the variable.
1233
   * This outputs lemmas of the general forms
1234
   *     (= p c) implies (<= p d) for c < d, or
1235
   *     (= p c) implies (not (= p d)) for c != d.
1236
   */
1237
  void outputUnateEqualityLemmas(std::vector<TrustNode>& lemmas) const;
1238
  void outputUnateEqualityLemmas(std::vector<TrustNode>& lemmas,
1239
                                 ArithVar v) const;
1240
1241
  /**
1242
   * Outputs a minimal set of unate implications onto the vector for the variable.
1243
   *
1244
   * If ineqs is true, this outputs lemmas of the general form
1245
   *     (<= p c) implies (<= p d) for c < d.
1246
   */
1247
  void outputUnateInequalityLemmas(std::vector<TrustNode>& lemmas) const;
1248
  void outputUnateInequalityLemmas(std::vector<TrustNode>& lemmas,
1249
                                   ArithVar v) const;
1250
1251
  void unatePropLowerBound(ConstraintP curr, ConstraintP prev);
1252
  void unatePropUpperBound(ConstraintP curr, ConstraintP prev);
1253
  void unatePropEquality(ConstraintP curr, ConstraintP prevLB, ConstraintP prevUB);
1254
1255
  /** AntecendentID must be in range. */
1256
  ConstraintCP getAntecedent(AntecedentId p) const;
1257
1258
7267560
  bool isProofEnabled() const { return d_pnm != nullptr; }
1259
1260
 private:
1261
  /** returns true if cons is now in conflict. */
1262
  bool handleUnateProp(ConstraintP ant, ConstraintP cons);
1263
1264
  DenseSet d_reclaimable;
1265
1266
  class Statistics {
1267
  public:
1268
    IntStat d_unatePropagateCalls;
1269
    IntStat d_unatePropagateImplications;
1270
1271
    Statistics();
1272
  } d_statistics;
1273
1274
}; /* ConstraintDatabase */
1275
1276
}  // namespace arith
1277
}  // namespace theory
1278
}  // namespace cvc5
1279
1280
#endif /* CVC5__THEORY__ARITH__CONSTRAINT_H */