GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/expr/subs.cpp Lines: 33 81 40.7 %
Date: 2021-03-23 Branches: 36 214 16.8 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file subs.cpp
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Andrew Reynolds
6
 ** This file is part of the CVC4 project.
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.\endverbatim
11
 **
12
 ** \brief Simple substitution utility
13
 **/
14
15
#include "expr/subs.h"
16
17
#include <sstream>
18
19
#include "theory/rewriter.h"
20
21
namespace CVC4 {
22
23
99
bool Subs::empty() const { return d_vars.empty(); }
24
25
size_t Subs::size() const { return d_vars.size(); }
26
27
930
bool Subs::contains(Node v) const
28
{
29
930
  return std::find(d_vars.begin(), d_vars.end(), v) != d_vars.end();
30
}
31
32
Node Subs::getSubs(Node v) const
33
{
34
  std::vector<Node>::const_iterator it =
35
      std::find(d_vars.begin(), d_vars.end(), v);
36
  if (it == d_vars.end())
37
  {
38
    return Node::null();
39
  }
40
  size_t i = std::distance(d_vars.begin(), it);
41
  Assert(i < d_subs.size());
42
  return d_subs[i];
43
}
44
45
29
void Subs::add(Node v)
46
{
47
  // default, use a fresh skolem of the same type
48
58
  Node s = NodeManager::currentNM()->mkSkolem("sk", v.getType());
49
29
  add(v, s);
50
29
}
51
52
19
void Subs::add(const std::vector<Node>& vs)
53
{
54
48
  for (const Node& v : vs)
55
  {
56
29
    add(v);
57
  }
58
19
}
59
60
317
void Subs::add(Node v, Node s)
61
{
62
317
  Assert(v.getType().isComparableTo(s.getType()));
63
317
  d_vars.push_back(v);
64
317
  d_subs.push_back(s);
65
317
}
66
67
20
void Subs::add(const std::vector<Node>& vs, const std::vector<Node>& ss)
68
{
69
20
  Assert(vs.size() == ss.size());
70
46
  for (size_t i = 0, nvs = vs.size(); i < nvs; i++)
71
  {
72
26
    add(vs[i], ss[i]);
73
  }
74
20
}
75
76
void Subs::addEquality(Node eq)
77
{
78
  Assert(eq.getKind() == kind::EQUAL);
79
  add(eq[0], eq[1]);
80
}
81
82
void Subs::append(Subs& s)
83
{
84
  // add the substitution list
85
  add(s.d_vars, s.d_subs);
86
}
87
88
77
Node Subs::apply(Node n, bool doRewrite) const
89
{
90
77
  if (d_vars.empty())
91
  {
92
    return n;
93
  }
94
  Node ns =
95
154
      n.substitute(d_vars.begin(), d_vars.end(), d_subs.begin(), d_subs.end());
96
77
  if (doRewrite)
97
  {
98
    ns = theory::Rewriter::rewrite(ns);
99
  }
100
77
  return ns;
101
}
102
19
Node Subs::rapply(Node n, bool doRewrite) const
103
{
104
19
  if (d_vars.empty())
105
  {
106
    return n;
107
  }
108
  Node ns =
109
38
      n.substitute(d_subs.begin(), d_subs.end(), d_vars.begin(), d_vars.end());
110
19
  if (doRewrite)
111
  {
112
19
    ns = theory::Rewriter::rewrite(ns);
113
  }
114
19
  return ns;
115
}
116
117
void Subs::applyToRange(Subs& s, bool doRewrite) const
118
{
119
  if (d_vars.empty())
120
  {
121
    return;
122
  }
123
  for (size_t i = 0, ns = s.d_subs.size(); i < ns; i++)
124
  {
125
    s.d_subs[i] = apply(s.d_subs[i], doRewrite);
126
  }
127
}
128
129
void Subs::rapplyToRange(Subs& s, bool doRewrite) const
130
{
131
  if (d_vars.empty())
132
  {
133
    return;
134
  }
135
  for (size_t i = 0, ns = s.d_subs.size(); i < ns; i++)
136
  {
137
    s.d_subs[i] = rapply(s.d_subs[i], doRewrite);
138
  }
139
}
140
141
Node Subs::getEquality(size_t i) const
142
{
143
  Assert(i < d_vars.size());
144
  return d_vars[i].eqNode(d_subs[i]);
145
}
146
147
std::map<Node, Node> Subs::toMap() const
148
{
149
  std::map<Node, Node> ret;
150
  for (size_t i = 0, nvs = d_vars.size(); i < nvs; i++)
151
  {
152
    ret[d_vars[i]] = d_subs[i];
153
  }
154
  return ret;
155
}
156
157
std::string Subs::toString() const
158
{
159
  std::stringstream ss;
160
  ss << "[";
161
  for (size_t i = 0, nvs = d_vars.size(); i < nvs; i++)
162
  {
163
    if (i > 0)
164
    {
165
      ss << " ";
166
    }
167
    ss << d_vars[i] << " -> " << d_subs[i];
168
  }
169
  ss << "]";
170
  return ss.str();
171
}
172
173
std::ostream& operator<<(std::ostream& out, const Subs& s)
174
{
175
  out << s.toString();
176
  return out;
177
}
178
179
26685
}  // namespace CVC4