GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quant_bound_inference.cpp Lines: 52 52 100.0 %
Date: 2021-11-07 Branches: 82 142 57.7 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz
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
 * Implementation of quantifiers bound inference.
14
 */
15
16
#include "theory/quantifiers/quant_bound_inference.h"
17
18
#include "theory/quantifiers/fmf/bounded_integers.h"
19
#include "theory/rewriter.h"
20
#include "util/rational.h"
21
22
using namespace cvc5::kind;
23
24
namespace cvc5 {
25
namespace theory {
26
namespace quantifiers {
27
28
15273
QuantifiersBoundInference::QuantifiersBoundInference(unsigned cardMax,
29
15273
                                                     bool isFmf)
30
15273
    : d_cardMax(cardMax), d_isFmf(isFmf), d_bint(nullptr)
31
{
32
15273
}
33
34
12150
void QuantifiersBoundInference::finishInit(BoundedIntegers* b) { d_bint = b; }
35
36
10524
bool QuantifiersBoundInference::mayComplete(TypeNode tn)
37
{
38
10524
  std::unordered_map<TypeNode, bool>::iterator it = d_may_complete.find(tn);
39
10524
  if (it == d_may_complete.end())
40
  {
41
    // cache
42
552
    bool mc = mayComplete(tn, d_cardMax);
43
552
    d_may_complete[tn] = mc;
44
552
    return mc;
45
  }
46
9972
  return it->second;
47
}
48
49
552
bool QuantifiersBoundInference::mayComplete(TypeNode tn, unsigned maxCard)
50
{
51
552
  if (!tn.isClosedEnumerable())
52
  {
53
206
    return false;
54
  }
55
346
  bool mc = false;
56
  // we cannot use FMF to complete interpreted types, thus we pass
57
  // false for fmfEnabled here
58
346
  if (isCardinalityClassFinite(tn.getCardinalityClass(), false))
59
  {
60
104
    Cardinality c = tn.getCardinality();
61
52
    if (!c.isLargeFinite())
62
    {
63
48
      NodeManager* nm = NodeManager::currentNM();
64
96
      Node card = nm->mkConst(Rational(c.getFiniteCardinality()));
65
      // check if less than fixed upper bound
66
96
      Node oth = nm->mkConst(Rational(maxCard));
67
96
      Node eq = nm->mkNode(LEQ, card, oth);
68
48
      eq = Rewriter::rewrite(eq);
69
48
      mc = eq.isConst() && eq.getConst<bool>();
70
    }
71
  }
72
346
  return mc;
73
}
74
75
6074
bool QuantifiersBoundInference::isFiniteBound(Node q, Node v)
76
{
77
6074
  if (d_bint && d_bint->isBound(q, v))
78
  {
79
88
    return true;
80
  }
81
11972
  TypeNode tn = v.getType();
82
5986
  if (tn.isSort() && d_isFmf)
83
  {
84
1306
    return true;
85
  }
86
4680
  else if (mayComplete(tn))
87
  {
88
85
    return true;
89
  }
90
4595
  return false;
91
}
92
93
5758
BoundVarType QuantifiersBoundInference::getBoundVarType(Node q, Node v)
94
{
95
5758
  if (d_bint)
96
  {
97
4398
    return d_bint->getBoundVarType(q, v);
98
  }
99
1360
  return isFiniteBound(q, v) ? BOUND_FINITE : BOUND_NONE;
100
}
101
102
5475
void QuantifiersBoundInference::getBoundVarIndices(
103
    Node q, std::vector<unsigned>& indices) const
104
{
105
5475
  Assert(indices.empty());
106
  // we take the bounded variables first
107
5475
  if (d_bint)
108
  {
109
4513
    d_bint->getBoundVarIndices(q, indices);
110
  }
111
  // then get the remaining ones
112
13001
  for (size_t i = 0, nvars = q[0].getNumChildren(); i < nvars; i++)
113
  {
114
7526
    if (std::find(indices.begin(), indices.end(), i) == indices.end())
115
    {
116
1676
      indices.push_back(i);
117
    }
118
  }
119
5475
}
120
121
2877
bool QuantifiersBoundInference::getBoundElements(
122
    RepSetIterator* rsi,
123
    bool initial,
124
    Node q,
125
    Node v,
126
    std::vector<Node>& elements) const
127
{
128
2877
  if (d_bint)
129
  {
130
2865
    return d_bint->getBoundElements(rsi, initial, q, v, elements);
131
  }
132
12
  return false;
133
}
134
135
}  // namespace quantifiers
136
}  // namespace theory
137
31137
}  // namespace cvc5