GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/quant_bound_inference.cpp Lines: 52 52 100.0 %
Date: 2021-05-22 Branches: 81 144 56.3 %

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
21
using namespace cvc5::kind;
22
23
namespace cvc5 {
24
namespace theory {
25
namespace quantifiers {
26
27
9460
QuantifiersBoundInference::QuantifiersBoundInference(unsigned cardMax,
28
9460
                                                     bool isFmf)
29
9460
    : d_cardMax(cardMax), d_isFmf(isFmf), d_bint(nullptr)
30
{
31
9460
}
32
33
6414
void QuantifiersBoundInference::finishInit(BoundedIntegers* b) { d_bint = b; }
34
35
8493
bool QuantifiersBoundInference::mayComplete(TypeNode tn)
36
{
37
8493
  std::unordered_map<TypeNode, bool>::iterator it = d_may_complete.find(tn);
38
8493
  if (it == d_may_complete.end())
39
  {
40
    // cache
41
455
    bool mc = mayComplete(tn, d_cardMax);
42
455
    d_may_complete[tn] = mc;
43
455
    return mc;
44
  }
45
8038
  return it->second;
46
}
47
48
455
bool QuantifiersBoundInference::mayComplete(TypeNode tn, unsigned maxCard)
49
{
50
455
  if (!tn.isClosedEnumerable())
51
  {
52
192
    return false;
53
  }
54
263
  bool mc = false;
55
  // we cannot use FMF to complete interpreted types, thus we pass
56
  // false for fmfEnabled here
57
263
  if (isCardinalityClassFinite(tn.getCardinalityClass(), false))
58
  {
59
60
    Cardinality c = tn.getCardinality();
60
30
    if (!c.isLargeFinite())
61
    {
62
26
      NodeManager* nm = NodeManager::currentNM();
63
52
      Node card = nm->mkConst(Rational(c.getFiniteCardinality()));
64
      // check if less than fixed upper bound
65
52
      Node oth = nm->mkConst(Rational(maxCard));
66
52
      Node eq = nm->mkNode(LEQ, card, oth);
67
26
      eq = Rewriter::rewrite(eq);
68
26
      mc = eq.isConst() && eq.getConst<bool>();
69
    }
70
  }
71
263
  return mc;
72
}
73
74
5893
bool QuantifiersBoundInference::isFiniteBound(Node q, Node v)
75
{
76
5893
  if (d_bint && d_bint->isBound(q, v))
77
  {
78
68
    return true;
79
  }
80
11650
  TypeNode tn = v.getType();
81
5825
  if (tn.isSort() && d_isFmf)
82
  {
83
1424
    return true;
84
  }
85
4401
  else if (mayComplete(tn))
86
  {
87
71
    return true;
88
  }
89
4330
  return false;
90
}
91
92
5754
BoundVarType QuantifiersBoundInference::getBoundVarType(Node q, Node v)
93
{
94
5754
  if (d_bint)
95
  {
96
4278
    return d_bint->getBoundVarType(q, v);
97
  }
98
1476
  return isFiniteBound(q, v) ? BOUND_FINITE : BOUND_NONE;
99
}
100
101
4266
void QuantifiersBoundInference::getBoundVarIndices(
102
    Node q, std::vector<unsigned>& indices) const
103
{
104
4266
  Assert(indices.empty());
105
  // we take the bounded variables first
106
4266
  if (d_bint)
107
  {
108
3200
    d_bint->getBoundVarIndices(q, indices);
109
  }
110
  // then get the remaining ones
111
10296
  for (size_t i = 0, nvars = q[0].getNumChildren(); i < nvars; i++)
112
  {
113
6030
    if (std::find(indices.begin(), indices.end(), i) == indices.end())
114
    {
115
1766
      indices.push_back(i);
116
    }
117
  }
118
4266
}
119
120
2590
bool QuantifiersBoundInference::getBoundElements(
121
    RepSetIterator* rsi,
122
    bool initial,
123
    Node q,
124
    Node v,
125
    std::vector<Node>& elements) const
126
{
127
2590
  if (d_bint)
128
  {
129
2578
    return d_bint->getBoundElements(rsi, initial, q, v, elements);
130
  }
131
12
  return false;
132
}
133
134
}  // namespace quantifiers
135
}  // namespace theory
136
28194
}  // namespace cvc5