GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/constraint.h Lines: 101 107 94.4 %
Date: 2021-11-07 Branches: 36 216 16.7 %

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 "smt/env_obj.h"
91
#include "theory/arith/arithvar.h"
92
#include "theory/arith/callbacks.h"
93
#include "theory/arith/constraint_forward.h"
94
#include "theory/arith/delta_rational.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
181453
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
181453
  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
314
  ConstraintRule();
315
  ConstraintRule(ConstraintP con, ArithProofType pt);
316
  ConstraintRule(ConstraintP con,
317
                 ArithProofType pt,
318
                 AntecedentId antecedentEnd);
319
  ConstraintRule(ConstraintP con,
320
                 ArithProofType pt,
321
                 AntecedentId antecedentEnd,
322
                 RationalVectorCP coeffs);
323
324
  void print(std::ostream& out, bool produceProofs) const;
325
}; /* class ConstraintRule */
326
327
class Constraint {
328
329
  friend class ConstraintDatabase;
330
331
 public:
332
  /**
333
   * This begins construction of a minimal constraint.
334
   *
335
   * This should only be called by ConstraintDatabase.
336
   *
337
   * Because of circular dependencies a Constraint is not fully valid until
338
   * initialize has been called on it.
339
   */
340
  Constraint(ArithVar x,
341
             ConstraintType t,
342
             const DeltaRational& v,
343
             bool produceProofs);
344
345
  /**
346
   * Destructor for a constraint.
347
   * This should only be called if safeToGarbageCollect() is true.
348
   */
349
  ~Constraint();
350
351
  static ConstraintType constraintTypeOfComparison(const Comparison& cmp);
352
353
23193994
  inline ConstraintType getType() const {
354
23193994
    return d_type;
355
  }
356
357
32331843
  inline ArithVar getVariable() const {
358
32331843
    return d_variable;
359
  }
360
361
60878191
  const DeltaRational& getValue() const {
362
60878191
    return d_value;
363
  }
364
365
4211917
  inline ConstraintP getNegation() const {
366
4211917
    return d_negation;
367
  }
368
369
8843376
  bool isEquality() const{
370
8843376
    return d_type == Equality;
371
  }
372
1201258
  bool isDisequality() const{
373
1201258
    return d_type == Disequality;
374
  }
375
5948280
  bool isLowerBound() const{
376
5948280
    return d_type == LowerBound;
377
  }
378
6296916
  bool isUpperBound() const{
379
6296916
    return d_type == UpperBound;
380
  }
381
1839962
  bool isStrictUpperBound() const{
382
1839962
    Assert(isUpperBound());
383
1839962
    return getValue().infinitesimalSgn() < 0;
384
  }
385
386
1537233
  bool isStrictLowerBound() const{
387
1537233
    Assert(isLowerBound());
388
1537233
    return getValue().infinitesimalSgn() > 0;
389
  }
390
391
2255927
  bool isSplit() const {
392
2255927
    return d_split;
393
  }
394
395
  /**
396
   * Splits the node in the user context.
397
   * Returns a lemma that is assumed to be true for the rest of the user context.
398
   * Constraint must be an equality or disequality.
399
   */
400
  TrustNode split();
401
402
14224995
  bool canBePropagated() const {
403
14224995
    return d_canBePropagated;
404
  }
405
  void setCanBePropagated();
406
407
  /**
408
   * Light wrapper for calling setCanBePropagated(),
409
   * on this and this->d_negation.
410
   */
411
469806
  void setPreregistered(){
412
469806
    setCanBePropagated();
413
469806
    d_negation->setCanBePropagated();
414
469806
  }
415
416
72085605
  bool assertedToTheTheory() const {
417
72085605
    Assert((d_assertionOrder < AssertionOrderSentinel) != d_witness.isNull());
418
72085605
    return d_assertionOrder < AssertionOrderSentinel;
419
  }
420
7578602
  TNode getWitness() const {
421
7578602
    Assert(assertedToTheTheory());
422
7578602
    return d_witness;
423
  }
424
425
6899593
  bool assertedBefore(AssertionOrder time) const {
426
6899593
    return d_assertionOrder < time;
427
  }
428
429
  /**
430
   * Sets the witness literal for a node being on the assertion stack.
431
   *
432
   * If the negation of the node is true, inConflict must be true.
433
   * If the negation of the node is false, inConflict must be false.
434
   * Hence, negationHasProof() == inConflict.
435
   *
436
   * This replaces:
437
   *   void setAssertedToTheTheory(TNode witness);
438
   *   void setAssertedToTheTheoryWithNegationTrue(TNode witness);
439
   */
440
  void setAssertedToTheTheory(TNode witness, bool inConflict);
441
442
17374202
  bool hasLiteral() const {
443
17374202
    return !d_literal.isNull();
444
  }
445
446
  void setLiteral(Node n);
447
448
2063185
  Node getLiteral() const {
449
2063185
    Assert(hasLiteral());
450
2063185
    return d_literal;
451
  }
452
453
  /** Gets a literal in the normal form suitable for proofs.
454
   * That is, (sum of non-const monomials) >< const.
455
   *
456
   * This is a sister method to `getLiteral`, which returns a normal form
457
   * literal, suitable for external solving use.
458
   */
459
  Node getProofLiteral() const;
460
461
  /**
462
   * Set the node as having a proof and being an assumption.
463
   * The node must be assertedToTheTheory().
464
   *
465
   * Precondition: negationHasProof() == inConflict.
466
   *
467
   * Replaces:
468
   *  selfExplaining().
469
   *  selfExplainingWithNegationTrue().
470
   */
471
  void setAssumption(bool inConflict);
472
473
  /** Returns true if the node is an assumption.*/
474
  bool isAssumption() const;
475
476
  /** Whether we produce proofs */
477
14765156
  bool isProofProducing() const { return d_produceProofs; }
478
479
  /** Set the constraint to have an EqualityEngine proof. */
480
  void setEqualityEngineProof();
481
  bool hasEqualityEngineProof() const;
482
483
  /** Returns true if the node has a Farkas' proof. */
484
  bool hasFarkasProof() const;
485
486
  /**
487
   * @brief Returns whether this constraint is provable using a Farkas
488
   * proof applied to (possibly tightened) input assertions.
489
   *
490
   * An example of a constraint that has a simple Farkas proof:
491
   *    x <= 0 proven from x + y <= 0 and x - y <= 0.
492
   *
493
   * An example of another constraint that has a simple Farkas proof:
494
   *    x <= 0 proven from x + y <= 0 and x - y <= 0.5 for integers x, y
495
   *       (integer bound-tightening is applied first!).
496
   *
497
   * An example of a constraint that might be proven **without** a simple
498
   * Farkas proof:
499
   *    x < 0 proven from not(x == 0) and not(x > 0).
500
   *
501
   * This could be proven internally by the arithmetic theory using
502
   * `TrichotomyAP` as the proof type.
503
   *
504
   */
505
  bool hasSimpleFarkasProof() const;
506
  /**
507
   * Returns whether this constraint is an assumption or a tightened
508
   * assumption.
509
   */
510
  bool isPossiblyTightenedAssumption() const;
511
512
  /** Returns true if the node has a int bound tightening proof. */
513
  bool hasIntTightenProof() const;
514
515
  /** Returns true if the node has a int hole proof. */
516
  bool hasIntHoleProof() const;
517
518
  /** Returns true if the node has a trichotomy proof. */
519
  bool hasTrichotomyProof() const;
520
521
  void printProofTree(std::ostream & out, size_t depth = 0) const;
522
523
  /**
524
   * A sets the constraint to be an internal assumption.
525
   *
526
   * This does not need to have a witness or an associated literal.
527
   * This is always itself in the explanation fringe for both conflicts
528
   * and propagation.
529
   * This cannot be converted back into a Node conflict or explanation.
530
   *
531
   * This cannot have a proof or be asserted to the theory!
532
   *
533
   */
534
  void setInternalAssumption(bool inConflict);
535
  bool isInternalAssumption() const;
536
537
  /**
538
   * Returns a explanation of the constraint that is appropriate for conflicts.
539
   *
540
   * This is not appropriate for propagation!
541
   *
542
   * This is the minimum fringe of the implication tree s.t.
543
   * every constraint is assertedToTheTheory() or hasEqualityEngineProof().
544
   */
545
  TrustNode externalExplainByAssertions() const;
546
547
  /**
548
   * Writes an explanation of a constraint into the node builder.
549
   * Pushes back an explanation that is acceptable to send to the sat solver.
550
   * nb is assumed to be an AND.
551
   *
552
   * This is the minimum fringe of the implication tree s.t.
553
   * every constraint is assertedToTheTheory() or hasEqualityEngineProof().
554
   *
555
   * This is not appropriate for propagation!
556
   * Use explainForPropagation() instead.
557
   */
558
3258810
  std::shared_ptr<ProofNode> externalExplainByAssertions(NodeBuilder& nb) const
559
  {
560
3258810
    return externalExplain(nb, AssertionOrderSentinel);
561
  }
562
563
  /* Equivalent to calling externalExplainByAssertions on all constraints in b */
564
  static Node externalExplainByAssertions(const ConstraintCPVec& b);
565
  static Node externalExplainByAssertions(ConstraintCP a, ConstraintCP b);
566
  static Node externalExplainByAssertions(ConstraintCP a, ConstraintCP b, ConstraintCP c);
567
568
  /**
569
   * This is the minimum fringe of the implication tree s.t. every constraint is
570
   * - assertedToTheTheory(),
571
   * - isInternalDecision() or
572
   * - hasEqualityEngineProof().
573
   */
574
  static void assertionFringe(ConstraintCPVec& v);
575
  static void assertionFringe(ConstraintCPVec& out, const ConstraintCPVec& in);
576
577
  /** The fringe of a farkas' proof. */
578
  bool onFringe() const {
579
    return assertedToTheTheory() || isInternalAssumption() || hasEqualityEngineProof();
580
  }
581
582
  /**
583
   * Returns an explanation of a propagation by the ConstraintDatabase.
584
   * The constraint must have a proof.
585
   * The constraint cannot be an assumption.
586
   *
587
   * This is the minimum fringe of the implication tree (excluding the
588
   * constraint itself) s.t. every constraint is assertedToTheTheory() or
589
   * hasEqualityEngineProof().
590
   *
591
   * All return conjuncts were asserted before this constraint.
592
   *
593
   * Requires the given node to rewrite to the canonical literal for this
594
   * constraint.
595
   *
596
   * @params n the literal to prove
597
   *           n must rewrite to the constraint's canonical literal
598
   *
599
   * @returns a trust node of the form:
600
   *         (=> explanation n)
601
   */
602
  TrustNode externalExplainForPropagation(TNode n) const;
603
604
  /**
605
   * Explain the constraint and its negation in terms of assertions.
606
   * The constraint must be in conflict.
607
   */
608
  TrustNode externalExplainConflict() const;
609
610
  /** The constraint is known to be true. */
611
208116746
  inline bool hasProof() const {
612
208116746
    return d_crid != ConstraintRuleIdSentinel;
613
  }
614
615
  /** The negation of the constraint is known to hold. */
616
66841716
  inline bool negationHasProof() const {
617
66841716
    return d_negation->hasProof();
618
  }
619
620
  /** Neither the contraint has a proof nor the negation has a proof.*/
621
258333
  bool truthIsUnknown() const {
622
258333
    return !hasProof() && !negationHasProof();
623
  }
624
625
  /** This is a synonym for hasProof(). */
626
20337832
  inline bool isTrue() const {
627
20337832
    return hasProof();
628
  }
629
630
  /** Both the constraint and its negation are true. */
631
13214534
  inline bool inConflict() const {
632
13214534
    return hasProof() && negationHasProof();
633
  }
634
635
  /**
636
   * Returns the constraint that corresponds to taking
637
   *    x r ceiling(getValue()) where r is the node's getType().
638
   * Esstentially this is an up branch.
639
   */
640
  ConstraintP getCeiling();
641
642
  /**
643
   * Returns the constraint that corresponds to taking
644
   *    x r floor(getValue()) where r is the node's getType().
645
   * Esstentially this is a down branch.
646
   */
647
  ConstraintP getFloor();
648
649
  static ConstraintP makeNegation(ArithVar v,
650
                                  ConstraintType t,
651
                                  const DeltaRational& r,
652
                                  bool produceProofs);
653
654
  const ValueCollection& getValueCollection() const;
655
656
657
  ConstraintP getStrictlyWeakerUpperBound(bool hasLiteral, bool mustBeAsserted) const;
658
  ConstraintP getStrictlyWeakerLowerBound(bool hasLiteral, bool mustBeAsserted) const;
659
660
  /**
661
   * Marks a the constraint c as being entailed by a.
662
   * The Farkas proof 1*(a) + -1 (c) |= 0<0
663
   *
664
   * After calling impliedByUnate(), the caller should either raise a conflict
665
   * or try call tryToPropagate().
666
   */
667
  void impliedByUnate(ConstraintCP a, bool inConflict);
668
669
  /**
670
   * Marks a the constraint c as being entailed by a.
671
   * The reason has to do with integer bound tightening.
672
   *
673
   * After calling impliedByIntTighten(), the caller should either raise a conflict
674
   * or try call tryToPropagate().
675
   */
676
  void impliedByIntTighten(ConstraintCP a, bool inConflict);
677
678
  /**
679
   * Marks a the constraint c as being entailed by a.
680
   * The reason has to do with integer reasoning.
681
   *
682
   * After calling impliedByIntHole(), the caller should either raise a conflict
683
   * or try call tryToPropagate().
684
   */
685
  void impliedByIntHole(ConstraintCP a, bool inConflict);
686
687
  /**
688
   * Marks a the constraint c as being entailed by a.
689
   * The reason has to do with integer reasoning.
690
   *
691
   * After calling impliedByIntHole(), the caller should either raise a conflict
692
   * or try call tryToPropagate().
693
   */
694
  void impliedByIntHole(const ConstraintCPVec& b, bool inConflict);
695
696
  /**
697
   * This is a lemma of the form:
698
   *   x < d or x = d or x > d
699
   * The current constraint c is one of the above constraints and {a,b}
700
   * are the negation of the other two constraints.
701
   *
702
   * Preconditions:
703
   * - negationHasProof() == inConflict.
704
   *
705
   * After calling impliedByTrichotomy(), the caller should either raise a conflict
706
   * or try call tryToPropagate().
707
   */
708
  void impliedByTrichotomy(ConstraintCP a, ConstraintCP b, bool inConflict);
709
710
  /**
711
   * Marks the node as having a Farkas proof.
712
   *
713
   * Preconditions:
714
   * - coeffs == NULL if proofs are off.
715
   * - See the comments for ConstraintRule for the form of coeffs when
716
   *   proofs are on.
717
   * - negationHasProof() == inConflict.
718
   *
719
   * After calling impliedByFarkas(), the caller should either raise a conflict
720
   * or try call tryToPropagate().
721
   */
722
  void impliedByFarkas(const ConstraintCPVec& b, RationalVectorCP coeffs, bool inConflict);
723
724
  /**
725
   * Generates an implication node, B => getLiteral(),
726
   * where B is the result of externalExplainByAssertions(b).
727
   * Does not guarantee b is the explanation of the constraint.
728
   */
729
  Node externalImplication(const ConstraintCPVec& b) const;
730
731
  /**
732
   * Returns true if the variable is assigned the value dr,
733
   * the constraint would be satisfied.
734
   */
735
  bool satisfiedBy(const DeltaRational& dr) const;
736
737
  /**
738
   * The node must have a proof already and be eligible for propagation!
739
   * You probably want to call tryToPropagate() instead.
740
   *
741
   * Preconditions:
742
   * - hasProof()
743
   * - canBePropagated()
744
   * - !assertedToTheTheory()
745
   */
746
  void propagate();
747
748
  /**
749
   * If the constraint
750
   *   canBePropagated() and
751
   *   !assertedToTheTheory(),
752
   * the constraint is added to the database's propagation queue.
753
   *
754
   * Precondition:
755
   * - hasProof()
756
   */
757
  void tryToPropagate();
758
759
  /**
760
   * Returns a reference to the containing database.
761
   * Precondition: the constraint must be initialized.
762
   */
763
  const ConstraintDatabase& getDatabase() const;
764
765
  /** Returns the constraint rule at the position. */
766
  const ConstraintRule& getConstraintRule() const;
767
768
 private:
769
  /**  Returns true if the constraint has been initialized. */
770
  bool initialized() const;
771
772
  /**
773
   * This initializes the fields that cannot be set in the constructor due to
774
   * circular dependencies.
775
   */
776
  void initialize(ConstraintDatabase* db,
777
                  SortedConstraintMapIterator v,
778
                  ConstraintP negation);
779
780
  class ConstraintRuleCleanup
781
  {
782
   public:
783
11438556
    inline void operator()(ConstraintRule* crp)
784
    {
785
11438556
      Assert(crp != NULL);
786
11438556
      ConstraintP constraint = crp->d_constraint;
787
11438556
      Assert(constraint->d_crid != ConstraintRuleIdSentinel);
788
11438556
      constraint->d_crid = ConstraintRuleIdSentinel;
789
11438556
      if (constraint->isProofProducing())
790
      {
791
4109314
        if (crp->d_farkasCoefficients != RationalVectorCPSentinel)
792
        {
793
1116385
          delete crp->d_farkasCoefficients;
794
        }
795
      }
796
11438556
    }
797
  };
798
799
  class CanBePropagatedCleanup
800
  {
801
   public:
802
939612
    inline void operator()(ConstraintP* p)
803
    {
804
939612
      ConstraintP constraint = *p;
805
939612
      Assert(constraint->d_canBePropagated);
806
939612
      constraint->d_canBePropagated = false;
807
939612
    }
808
  };
809
810
  class AssertionOrderCleanup
811
  {
812
   public:
813
7173019
    inline void operator()(ConstraintP* p)
814
    {
815
7173019
      ConstraintP constraint = *p;
816
7173019
      Assert(constraint->assertedToTheTheory());
817
7173019
      constraint->d_assertionOrder = AssertionOrderSentinel;
818
7173019
      constraint->d_witness = TNode::null();
819
7173019
      Assert(!constraint->assertedToTheTheory());
820
7173019
    }
821
  };
822
823
  class SplitCleanup
824
  {
825
   public:
826
37366
    inline void operator()(ConstraintP* p)
827
    {
828
37366
      ConstraintP constraint = *p;
829
37366
      Assert(constraint->d_split);
830
37366
      constraint->d_split = false;
831
37366
    }
832
  };
833
834
  /**
835
   * Returns true if the node is safe to garbage collect.
836
   * Both it and its negation must have no context dependent data set.
837
   */
838
  bool safeToGarbageCollect() const;
839
840
  /**
841
   * Returns true if the constraint has no context dependent data set.
842
   */
843
  bool contextDependentDataIsSet() const;
844
845
  /**
846
   * Returns true if the node correctly corresponds to the constraint that is
847
   * being set.
848
   */
849
  bool sanityChecking(Node n) const;
850
851
  /** Returns a reference to the map for d_variable. */
852
  SortedConstraintMap& constraintSet() const;
853
854
  /** Returns coefficients for the proofs for farkas cancellation. */
855
  static std::pair<int, int> unateFarkasSigns(ConstraintCP a, ConstraintCP b);
856
857
  Node externalExplain(AssertionOrder order) const;
858
  /**
859
   * Returns an explanation of that was assertedBefore(order).
860
   * The constraint must have a proof.
861
   * The constraint cannot be selfExplaining().
862
   *
863
   * This is the minimum fringe of the implication tree
864
   * s.t. every constraint is assertedBefore(order) or hasEqualityEngineProof().
865
   */
866
  std::shared_ptr<ProofNode> externalExplain(NodeBuilder& nb,
867
                                             AssertionOrder order) const;
868
869
  static Node externalExplain(const ConstraintCPVec& b, AssertionOrder order);
870
871
30599168
  inline ArithProofType getProofType() const {
872
30599168
    return getConstraintRule().d_proofType;
873
  }
874
875
718759
  inline AntecedentId getEndAntecedent() const {
876
718759
    return getConstraintRule().d_antecedentEnd;
877
  }
878
879
13861
  inline RationalVectorCP getFarkasCoefficients() const
880
  {
881
13861
    return d_produceProofs ? getConstraintRule().d_farkasCoefficients : nullptr;
882
  }
883
884
  /**
885
   * The proof of the node is empty.
886
   * The proof must be a special proof. Either
887
   *   isSelfExplaining() or
888
   *    hasEqualityEngineProof()
889
   */
890
  bool antecentListIsEmpty() const;
891
892
  bool antecedentListLengthIsOne() const;
893
894
  /** Return true if every element in b has a proof. */
895
  static bool allHaveProof(const ConstraintCPVec& b);
896
897
  /** Precondition: hasFarkasProof()
898
   * Computes the combination implied by the farkas coefficients. Sees if it is
899
   * a contradiction.
900
   */
901
902
  bool wellFormedFarkasProof() const;
903
904
  /** The ArithVar associated with the constraint. */
905
  const ArithVar d_variable;
906
907
  /** The type of the Constraint. */
908
  const ConstraintType d_type;
909
910
  /** The DeltaRational value with the constraint. */
911
  const DeltaRational d_value;
912
913
  /** A pointer to the associated database for the Constraint. */
914
  ConstraintDatabase* d_database;
915
916
  /**
917
   * The node to be communicated with the TheoryEngine.
918
   *
919
   * This is not context dependent, but may be set once.
920
   *
921
   * This must be set if the constraint canBePropagated().
922
   * This must be set if the constraint assertedToTheTheory().
923
   * Otherwise, this may be null().
924
   */
925
  Node d_literal;
926
927
  /** Pointer to the negation of the Constraint. */
928
  ConstraintP d_negation;
929
930
  /**
931
   * This is true if the associated node can be propagated.
932
   *
933
   * This should be enabled if the node has been preregistered.
934
   *
935
   * Sat Context Dependent.
936
   * This is initially false.
937
   */
938
  bool d_canBePropagated;
939
940
  /**
941
   * This is the order the constraint was asserted to the theory.
942
   * If this has been set, the node can be used in conflicts.
943
   * If this is c.d_assertedOrder < d.d_assertedOrder, then c can be used in the
944
   * explanation of d.
945
   *
946
   * This should be set after the literal is dequeued by Theory::get().
947
   *
948
   * Sat Context Dependent.
949
   * This is initially AssertionOrderSentinel.
950
   */
951
  AssertionOrder d_assertionOrder;
952
953
  /**
954
   * This is guaranteed to be on the fact queue.
955
   * For example if x + y = x + 1 is on the fact queue, then use this
956
   */
957
  TNode d_witness;
958
959
  /**
960
   * The position of the constraint in the constraint rule id.
961
   *
962
   * Sat Context Dependent.
963
   * This is initially
964
   */
965
  ConstraintRuleID d_crid;
966
967
  /**
968
   * True if the equality has been split.
969
   * Only meaningful if ConstraintType == Equality.
970
   *
971
   * User Context Dependent.
972
   * This is initially false.
973
   */
974
  bool d_split;
975
976
  /**
977
   * Position in sorted constraint set for the variable.
978
   * Unset if d_type is Disequality.
979
   */
980
  SortedConstraintMapIterator d_variablePosition;
981
982
  /** Whether to produce proofs, */
983
  bool d_produceProofs;
984
985
}; /* class ConstraintValue */
986
987
std::ostream& operator<<(std::ostream& o, const Constraint& c);
988
std::ostream& operator<<(std::ostream& o, const ConstraintP c);
989
std::ostream& operator<<(std::ostream& o, const ConstraintCP c);
990
std::ostream& operator<<(std::ostream& o, const ConstraintType t);
991
std::ostream& operator<<(std::ostream& o, const ValueCollection& c);
992
std::ostream& operator<<(std::ostream& o, const ConstraintCPVec& v);
993
std::ostream& operator<<(std::ostream& o, const ArithProofType);
994
995
class ConstraintDatabase : protected EnvObj
996
{
997
 private:
998
  /**
999
   * The map from ArithVars to their unique databases.
1000
   * When the vector changes size, we cannot allow the maps to move so this
1001
   * is a vector of pointers.
1002
   */
1003
  std::vector<PerVariableDatabase*> d_varDatabases;
1004
1005
  SortedConstraintMap& getVariableSCM(ArithVar v) const;
1006
1007
  /** Maps literals to constraints.*/
1008
  NodetoConstraintMap d_nodetoConstraintMap;
1009
1010
  /**
1011
   * A queue of propagated constraints.
1012
   * ConstraintCP are pointers.
1013
   * The elements of the queue do not require destruction.
1014
   */
1015
  context::CDQueue<ConstraintCP> d_toPropagate;
1016
1017
  /**
1018
   * Proofs are lists of valid constraints terminated by the first null
1019
   * sentinel value in the proof list.
1020
   * We abbreviate d_antecedents as ans in the comment.
1021
   *
1022
   * The proof at p in ans[p] of length n is
1023
   *  (NullConstraint, ans[p-(n-1)], ... , ans[p-1], ans[p])
1024
   *
1025
   * The proof at p corresponds to the conjunction:
1026
   *  (and x_i)
1027
   *
1028
   * So the proof of a Constraint c corresponds to the horn clause:
1029
   *  (implies (and x_i) c)
1030
   * where (and x_i) is the proof at c.d_crid d_antecedentEnd.
1031
   *
1032
   * Constraints are pointers so this list is designed not to require any destruction.
1033
   */
1034
  CDConstraintList d_antecedents;
1035
1036
  typedef context::CDList<ConstraintRule, Constraint::ConstraintRuleCleanup> ConstraintRuleList;
1037
  typedef context::CDList<ConstraintP, Constraint::CanBePropagatedCleanup> CBPList;
1038
  typedef context::CDList<ConstraintP, Constraint::AssertionOrderCleanup> AOList;
1039
  typedef context::CDList<ConstraintP, Constraint::SplitCleanup> SplitList;
1040
1041
1042
1043
  /**
1044
   * The watch lists are collected together as they need to be garbage collected
1045
   * carefully.
1046
   */
1047
15268
  struct Watches{
1048
1049
    /**
1050
     * Contains the exact list of constraints that have a proof.
1051
     * Upon pop, this unsets d_crid to NoAP.
1052
     *
1053
     * The index in this list is the proper ordering of the proofs.
1054
     */
1055
    ConstraintRuleList d_constraintProofs;
1056
1057
    /**
1058
     * Contains the exact list of constraints that can be used for propagation.
1059
     */
1060
    CBPList d_canBePropagatedWatches;
1061
1062
    /**
1063
     * Contains the exact list of constraints that have been asserted to the theory.
1064
     */
1065
    AOList d_assertionOrderWatches;
1066
1067
1068
    /**
1069
     * Contains the exact list of atoms that have been preregistered.
1070
     * This is a pointer as it must be destroyed before the elements of
1071
     * d_varDatabases.
1072
     */
1073
    SplitList d_splitWatches;
1074
    Watches(context::Context* satContext, context::Context* userContext);
1075
  };
1076
  Watches* d_watches;
1077
1078
  void pushSplitWatch(ConstraintP c);
1079
  void pushCanBePropagatedWatch(ConstraintP c);
1080
  void pushAssertionOrderWatch(ConstraintP c, TNode witness);
1081
1082
  /** Assumes that antecedents have already been pushed. */
1083
  void pushConstraintRule(const ConstraintRule& crp);
1084
1085
  /** Returns true if all of the entries of the vector are empty. */
1086
  static bool emptyDatabase(const std::vector<PerVariableDatabase>& vec);
1087
1088
  /** Map from nodes to arithvars. */
1089
  const ArithVariables& d_avariables;
1090
1091
1718875
  const ArithVariables& getArithVariables() const{
1092
1718875
    return d_avariables;
1093
  }
1094
1095
  ArithCongruenceManager& d_congruenceManager;
1096
1097
  /** Owned by the TheoryArithPrivate, used here. */
1098
  EagerProofGenerator* d_pfGen;
1099
  /** Owned by the TheoryArithPrivate, used here. */
1100
  ProofNodeManager* d_pnm;
1101
1102
  RaiseConflict d_raiseConflict;
1103
1104
1105
  const Rational d_one;
1106
  const Rational d_negOne;
1107
1108
  friend class Constraint;
1109
1110
 public:
1111
  ConstraintDatabase(Env& env,
1112
                     const ArithVariables& variables,
1113
                     ArithCongruenceManager& dm,
1114
                     RaiseConflict conflictCallBack,
1115
                     EagerProofGenerator* pfGen);
1116
1117
  ~ConstraintDatabase();
1118
1119
  /** Adds a literal to the database. */
1120
  ConstraintP addLiteral(TNode lit);
1121
1122
  /**
1123
   * If hasLiteral() is true, returns the constraint.
1124
   * Otherwise, returns NullConstraint.
1125
   */
1126
  ConstraintP lookup(TNode literal) const;
1127
1128
  /**
1129
   * Returns true if the literal has been added to the database.
1130
   * This is a hash table lookup.
1131
   * It does not look in the database for an equivalent corresponding constraint.
1132
   */
1133
  bool hasLiteral(TNode literal) const;
1134
1135
6169213
  bool hasMorePropagations() const{
1136
6169213
    return !d_toPropagate.empty();
1137
  }
1138
1139
1116509
  ConstraintCP nextPropagation(){
1140
1116509
    Assert(hasMorePropagations());
1141
1142
1116509
    ConstraintCP p = d_toPropagate.front();
1143
1116509
    d_toPropagate.pop();
1144
1145
1116509
    return p;
1146
  }
1147
1148
  void addVariable(ArithVar v);
1149
  bool variableDatabaseIsSetup(ArithVar v) const;
1150
  void removeVariable(ArithVar v);
1151
1152
  /** Get an explanation and proof for this constraint from the equality engine
1153
   */
1154
  TrustNode eeExplain(ConstraintCP c) const;
1155
  /** Get an explanation for this constraint from the equality engine */
1156
  void eeExplain(ConstraintCP c, NodeBuilder& nb) const;
1157
1158
  /**
1159
   * Returns a constraint with the variable v, the constraint type t, and a value
1160
   * dominated by r (explained below) if such a constraint exists in the database.
1161
   * If no such constraint exists, NullConstraint is returned.
1162
   *
1163
   * t must be either UpperBound or LowerBound.
1164
   * The returned value v is dominated:
1165
   *  If t is UpperBound, r <= v
1166
   *  If t is LowerBound, r >= v
1167
   *
1168
   * variableDatabaseIsSetup(v) must be true.
1169
   */
1170
  ConstraintP getBestImpliedBound(ArithVar v, ConstraintType t, const DeltaRational& r) const;
1171
1172
  /** Returns the constraint, if it exists */
1173
  ConstraintP lookupConstraint(ArithVar v, ConstraintType t, const DeltaRational& r) const;
1174
1175
  /**
1176
   * Returns a constraint with the variable v, the constraint type t and the value r.
1177
   * If there is such a constraint in the database already, it is returned.
1178
   * If there is no such constraint, this constraint is added to the database.
1179
   *
1180
   */
1181
  ConstraintP getConstraint(ArithVar v, ConstraintType t, const DeltaRational& r);
1182
1183
  /**
1184
   * Returns a constraint of the given type for the value and variable
1185
   * for the given ValueCollection, vc.
1186
   * This is made if there is no such constraint.
1187
   */
1188
  ConstraintP ensureConstraint(ValueCollection& vc, ConstraintType t);
1189
1190
1191
  void deleteConstraintAndNegation(ConstraintP c);
1192
1193
  /** Given constraints `a` and `b` such that `a OR b` by unate reasoning,
1194
   *  adds a TrustNode to `out` which proves `a OR b` as a lemma.
1195
   *
1196
   *  Example: `x <= 5` OR `5 <= x`.
1197
   */
1198
  void proveOr(std::vector<TrustNode>& out,
1199
               ConstraintP a,
1200
               ConstraintP b,
1201
               bool negateSecond) const;
1202
  /** Given constraints `a` and `b` such that `a` implies `b` by unate
1203
   * reasoning, adds a TrustNode to `out` which proves `-a OR b` as a lemma.
1204
   *
1205
   *  Example: `x >= 5` -> `x >= 4`.
1206
   */
1207
  void implies(std::vector<TrustNode>& out, ConstraintP a, ConstraintP b) const;
1208
  /** Given constraints `a` and `b` such that `not(a AND b)` by unate reasoning,
1209
   *  adds a TrustNode to `out` which proves `-a OR -b` as a lemma.
1210
   *
1211
   *  Example: `x >= 4` -> `x <= 3`.
1212
   */
1213
  void mutuallyExclusive(std::vector<TrustNode>& out,
1214
                         ConstraintP a,
1215
                         ConstraintP b) const;
1216
1217
  /**
1218
   * Outputs a minimal set of unate implications onto the vector for the variable.
1219
   * This outputs lemmas of the general forms
1220
   *     (= p c) implies (<= p d) for c < d, or
1221
   *     (= p c) implies (not (= p d)) for c != d.
1222
   */
1223
  void outputUnateEqualityLemmas(std::vector<TrustNode>& lemmas) const;
1224
  void outputUnateEqualityLemmas(std::vector<TrustNode>& lemmas,
1225
                                 ArithVar v) const;
1226
1227
  /**
1228
   * Outputs a minimal set of unate implications onto the vector for the variable.
1229
   *
1230
   * If ineqs is true, this outputs lemmas of the general form
1231
   *     (<= p c) implies (<= p d) for c < d.
1232
   */
1233
  void outputUnateInequalityLemmas(std::vector<TrustNode>& lemmas) const;
1234
  void outputUnateInequalityLemmas(std::vector<TrustNode>& lemmas,
1235
                                   ArithVar v) const;
1236
1237
  void unatePropLowerBound(ConstraintP curr, ConstraintP prev);
1238
  void unatePropUpperBound(ConstraintP curr, ConstraintP prev);
1239
  void unatePropEquality(ConstraintP curr, ConstraintP prevLB, ConstraintP prevUB);
1240
1241
  /** AntecendentID must be in range. */
1242
  ConstraintCP getAntecedent(AntecedentId p) const;
1243
1244
8859886
  bool isProofEnabled() const { return d_pnm != nullptr; }
1245
1246
 private:
1247
  /** returns true if cons is now in conflict. */
1248
  bool handleUnateProp(ConstraintP ant, ConstraintP cons);
1249
1250
  DenseSet d_reclaimable;
1251
1252
  class Statistics {
1253
  public:
1254
    IntStat d_unatePropagateCalls;
1255
    IntStat d_unatePropagateImplications;
1256
1257
    Statistics();
1258
  } d_statistics;
1259
1260
}; /* ConstraintDatabase */
1261
1262
}  // namespace arith
1263
}  // namespace theory
1264
}  // namespace cvc5
1265
1266
#endif /* CVC5__THEORY__ARITH__CONSTRAINT_H */