GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/prop/skolem_def_manager.cpp Lines: 90 91 98.9 %
Date: 2021-11-06 Branches: 131 282 46.5 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds
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
 * Skolem definition manager.
14
 */
15
16
#include "prop/skolem_def_manager.h"
17
18
#include "expr/attribute.h"
19
20
namespace cvc5 {
21
namespace prop {
22
23
15339
SkolemDefManager::SkolemDefManager(context::Context* context,
24
15339
                                   context::UserContext* userContext)
25
15339
    : d_skDefs(userContext), d_skActive(context)
26
{
27
15339
}
28
29
15334
SkolemDefManager::~SkolemDefManager() {}
30
31
29760
void SkolemDefManager::notifySkolemDefinition(TNode skolem, Node def)
32
{
33
  // Notice that skolem may have kind SKOLEM or BOOLEAN_TERM_VARIABLE
34
59520
  Trace("sk-defs") << "notifySkolemDefinition: " << def << " for " << skolem
35
29760
                   << std::endl;
36
  // in very rare cases, a skolem may be generated twice for terms that are
37
  // equivalent up to purification
38
29760
  if (d_skDefs.find(skolem) == d_skDefs.end())
39
  {
40
29695
    d_skDefs.insert(skolem, def);
41
  }
42
29760
}
43
44
2523
TNode SkolemDefManager::getDefinitionForSkolem(TNode skolem) const
45
{
46
2523
  NodeNodeMap::const_iterator it = d_skDefs.find(skolem);
47
2523
  Assert(it != d_skDefs.end()) << "No skolem def for " << skolem;
48
2523
  return it->second;
49
}
50
51
7662981
void SkolemDefManager::notifyAsserted(TNode literal,
52
                                      std::vector<TNode>& activatedSkolems,
53
                                      bool useDefs)
54
{
55
15325962
  std::unordered_set<Node> skolems;
56
7662981
  getSkolems(literal, skolems);
57
11551707
  for (const Node& k : skolems)
58
  {
59
3888726
    if (d_skActive.find(k) != d_skActive.end())
60
    {
61
      // already active
62
3515899
      continue;
63
    }
64
372827
    d_skActive.insert(k);
65
372827
    if (useDefs)
66
    {
67
      // add its definition to the activated list
68
372827
      NodeNodeMap::const_iterator it = d_skDefs.find(k);
69
372827
      Assert(it != d_skDefs.end());
70
372827
      activatedSkolems.push_back(it->second);
71
    }
72
    else
73
    {
74
      // add to the activated list
75
      activatedSkolems.push_back(k);
76
    }
77
  }
78
7662981
}
79
80
struct HasSkolemTag
81
{
82
};
83
struct HasSkolemComputedTag
84
{
85
};
86
/** Attribute true for nodes with skolems in them */
87
typedef expr::Attribute<HasSkolemTag, bool> HasSkolemAttr;
88
/** Attribute true for nodes where we have computed the above attribute */
89
typedef expr::Attribute<HasSkolemComputedTag, bool> HasSkolemComputedAttr;
90
91
23511389
bool SkolemDefManager::hasSkolems(TNode n) const
92
{
93
47022778
  std::unordered_set<TNode> visited;
94
23511389
  std::unordered_set<TNode>::iterator it;
95
47022778
  std::vector<TNode> visit;
96
47022778
  TNode cur;
97
23511389
  visit.push_back(n);
98
3219034
  do
99
  {
100
26730423
    cur = visit.back();
101
51127723
    if (cur.getAttribute(HasSkolemComputedAttr()))
102
    {
103
24397300
      visit.pop_back();
104
      // already computed
105
24397300
      continue;
106
    }
107
2333123
    it = visited.find(cur);
108
2333123
    if (it == visited.end())
109
    {
110
1267320
      visited.insert(cur);
111
1267320
      if (cur.getNumChildren() == 0)
112
      {
113
201517
        visit.pop_back();
114
201517
        bool hasSkolem = false;
115
201517
        if (cur.isVar())
116
        {
117
145598
          hasSkolem = (d_skDefs.find(cur) != d_skDefs.end());
118
        }
119
201517
        cur.setAttribute(HasSkolemAttr(), hasSkolem);
120
201517
        cur.setAttribute(HasSkolemComputedAttr(), true);
121
      }
122
      else
123
      {
124
1065803
        if (cur.getMetaKind() == kind::metakind::PARAMETERIZED)
125
        {
126
194407
          visit.push_back(cur.getOperator());
127
        }
128
1065803
        visit.insert(visit.end(), cur.begin(), cur.end());
129
      }
130
    }
131
    else
132
    {
133
1065803
      visit.pop_back();
134
      bool hasSkolem;
135
2131606
      if (cur.getMetaKind() == kind::metakind::PARAMETERIZED
136
2131606
          && cur.getOperator().getAttribute(HasSkolemAttr()))
137
      {
138
42
        hasSkolem = true;
139
      }
140
      else
141
      {
142
1065761
        hasSkolem = false;
143
2795346
        for (TNode i : cur)
144
        {
145
1875304
          Assert(i.getAttribute(HasSkolemComputedAttr()));
146
1875304
          if (i.getAttribute(HasSkolemAttr()))
147
          {
148
145719
            hasSkolem = true;
149
145719
            break;
150
          }
151
        }
152
      }
153
1065803
      cur.setAttribute(HasSkolemAttr(), hasSkolem);
154
1065803
      cur.setAttribute(HasSkolemComputedAttr(), true);
155
    }
156
26730423
  } while (!visit.empty());
157
23511389
  Assert(n.getAttribute(HasSkolemComputedAttr()));
158
47022778
  return n.getAttribute(HasSkolemAttr());
159
}
160
161
7666658
void SkolemDefManager::getSkolems(TNode n,
162
                                  std::unordered_set<Node>& skolems) const
163
{
164
15333316
  std::unordered_set<TNode> visited;
165
7666658
  std::unordered_set<TNode>::iterator it;
166
15333316
  std::vector<TNode> visit;
167
15333316
  TNode cur;
168
7666658
  visit.push_back(n);
169
15844731
  do
170
  {
171
23511389
    cur = visit.back();
172
23511389
    visit.pop_back();
173
23511389
    if (!hasSkolems(cur))
174
    {
175
      // does not have skolems, continue
176
10569994
      continue;
177
    }
178
12941395
    it = visited.find(cur);
179
12941395
    if (it == visited.end())
180
    {
181
12337594
      visited.insert(cur);
182
16228843
      if (cur.isVar())
183
      {
184
3891249
        if (d_skDefs.find(cur) != d_skDefs.end())
185
        {
186
3891249
          skolems.insert(cur);
187
        }
188
3891249
        continue;
189
      }
190
8446345
      if (cur.getMetaKind() == kind::metakind::PARAMETERIZED)
191
      {
192
1748710
        visit.push_back(cur.getOperator());
193
      }
194
8446345
      visit.insert(visit.end(), cur.begin(), cur.end());
195
    }
196
23511389
  } while (!visit.empty());
197
7666658
}
198
199
}  // namespace prop
200
31137
}  // namespace cvc5