Skip to content

Commit 76953cc

Browse files
committed
Fixed TermNames to also consider mapping of internal terms to original user terms
1 parent d566a97 commit 76953cc

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

src/api/MainSolver.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "MainSolver.h"
1010

1111
#include <common/ApiException.h>
12+
#include <common/InternalToUserTermMap.h>
1213
#include <itehandler/IteHandler.h>
1314
#include <logics/ArrayTheory.h>
1415
#include <logics/LATheory.h>
@@ -30,7 +31,7 @@ MainSolver::MainSolver(Logic & logic, SMTConfig & conf, std::string name)
3031
term_mapper(new TermMapper(logic)),
3132
thandler(new THandler(getTheory(), *term_mapper)),
3233
smt_solver(createInnerSolver(conf, *thandler)),
33-
termNames(conf),
34+
termNames(conf, internalToUserTermMap),
3435
logic(thandler->getLogic()),
3536
pmanager(logic),
3637
config(conf),
@@ -46,7 +47,7 @@ MainSolver::MainSolver(std::unique_ptr<Theory> th, std::unique_ptr<TermMapper> t
4647
term_mapper(std::move(tm)),
4748
thandler(std::move(thd)),
4849
smt_solver(std::move(ss)),
49-
termNames(conf),
50+
termNames(conf, internalToUserTermMap),
5051
logic(thandler->getLogic()),
5152
pmanager(logic),
5253
config(conf),
@@ -108,7 +109,11 @@ void MainSolver::insertFormula(PTRef fla) {
108109
throw ApiException("Top-level assertion sort must be Bool, got " + logic.printSort(logic.getSortRef(fla)));
109110
}
110111
// TODO: Move this to preprocessing of the formulas
111-
fla = IteHandler(logic, getPartitionManager().getNofPartitions()).rewrite(fla);
112+
{
113+
PTRef newFla = IteHandler(logic, getPartitionManager().getNofPartitions()).rewrite(fla);
114+
internalToUserTermMap.maybeAddMapping(newFla, fla);
115+
fla = newFla;
116+
}
112117

113118
if (trackPartitions()) {
114119
// MB: Important for HiFrog! partition index is the index of the formula in an virtual array of inserted

src/api/MainSolver.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace opensmt {
2525
class Logic;
26+
class InternalToUserTermMap;
2627

2728
class sstat {
2829
public:
@@ -139,7 +140,7 @@ class MainSolver {
139140
// Returns interpolation context for the last query (must be in UNSAT state)
140141
std::unique_ptr<InterpolationContext> getInterpolationContext();
141142

142-
[[deprecated("Use tryAddNamedAssertion or tryAddTermName")]]
143+
[[deprecated("Use tryAddNamedAssertion or tryAddTermNameFor")]]
143144
TermNames & getTermNames() {
144145
return termNames;
145146
}
@@ -310,6 +311,7 @@ class MainSolver {
310311
std::unique_ptr<TermMapper> term_mapper;
311312
std::unique_ptr<THandler> thandler;
312313
std::unique_ptr<SimpSMTSolver> smt_solver;
314+
InternalToUserTermMap internalToUserTermMap;
313315
TermNames termNames;
314316
Logic & logic;
315317
PartitionManager pmanager;

src/common/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ PUBLIC
77
"${CMAKE_CURRENT_LIST_DIR}/Partitions.h"
88
"${CMAKE_CURRENT_LIST_DIR}/TermNames.h"
99
PRIVATE
10-
"${CMAKE_CURRENT_LIST_DIR}/SystemQueries.h"
1110
"${CMAKE_CURRENT_LIST_DIR}/inttypes.h"
11+
"${CMAKE_CURRENT_LIST_DIR}/InternalToUserTermMap.h"
12+
"${CMAKE_CURRENT_LIST_DIR}/NatSet.h"
13+
"${CMAKE_CURRENT_LIST_DIR}/PartitionInfo.cc"
14+
"${CMAKE_CURRENT_LIST_DIR}/ReportUtils.h"
1215
"${CMAKE_CURRENT_LIST_DIR}/StringMap.h"
1316
"${CMAKE_CURRENT_LIST_DIR}/StringConv.h"
17+
"${CMAKE_CURRENT_LIST_DIR}/SystemQueries.h"
1418
"${CMAKE_CURRENT_LIST_DIR}/Timer.h"
1519
"${CMAKE_CURRENT_LIST_DIR}/TreeOps.h"
16-
"${CMAKE_CURRENT_LIST_DIR}/NatSet.h"
17-
"${CMAKE_CURRENT_LIST_DIR}/PartitionInfo.cc"
1820
"${CMAKE_CURRENT_LIST_DIR}/VerificationUtils.cc"
19-
"${CMAKE_CURRENT_LIST_DIR}/ReportUtils.h"
2021
"${CMAKE_CURRENT_LIST_DIR}/polynomials/Translations.cc"
2122
)
2223

@@ -25,5 +26,5 @@ include(numbers/CMakeLists.txt)
2526
install(FILES
2627
StringMap.h Timer.h inttypes.h IColor.h
2728
TreeOps.h FlaPartitionMap.h PartitionInfo.h Partitions.h ApiException.h TypeUtils.h
28-
NatSet.h ScopedVector.h TermNames.h
29+
NatSet.h ScopedVector.h TermNames.h InternalToUserTermMap.h
2930
DESTINATION ${INSTALL_HEADERS_DIR}/common)

src/common/InternalToUserTermMap.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef OPENSMT_USERTERMMAP_H
2+
#define OPENSMT_USERTERMMAP_H
3+
4+
#include <pterms/PTRef.h>
5+
6+
#include <unordered_map>
7+
8+
namespace opensmt {
9+
class InternalToUserTermMap {
10+
public:
11+
bool maybeAddMapping(PTRef newTerm, PTRef userTerm) {
12+
if (newTerm == userTerm) { return false; }
13+
14+
// overwrite any previous mappings
15+
map.insert_or_assign(newTerm, userTerm);
16+
return true;
17+
}
18+
19+
PTRef getUserTerm(PTRef term) const {
20+
auto it = map.find(term);
21+
if (it == map.end()) { return term; }
22+
23+
assert(it->first.x == term.x);
24+
return it->second;
25+
}
26+
27+
private:
28+
std::unordered_map<PTRef, PTRef, PTRefHash> map;
29+
};
30+
} // namespace opensmt
31+
32+
#endif // OPENSMT_USERTERMMAP_H

src/common/TermNames.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef OPENSMT_TERMNAMES_H
22
#define OPENSMT_TERMNAMES_H
33

4+
#include "InternalToUserTermMap.h"
45
#include "ScopedVector.h"
56
#include "TypeUtils.h"
67

@@ -19,12 +20,15 @@ using TermName = std::string;
1920

2021
class TermNames {
2122
public:
22-
TermNames(SMTConfig const & conf) : config{conf} {}
23+
TermNames(SMTConfig const & conf, InternalToUserTermMap const & map) : config{conf}, internalToUserTermMap{map} {}
2324

2425
bool isGlobal() const { return config.declarations_are_global(); }
2526

2627
bool contains(TermName const & name) const { return nameToTerm.contains(name); }
27-
bool contains(PTRef term) const { return termToNames.contains(term); }
28+
bool contains(PTRef term) const {
29+
term = internalToUserTermMap.getUserTerm(term);
30+
return termToNames.contains(term);
31+
}
2832

2933
[[deprecated("Use tryInsert")]]
3034
void insert(TermName const & name, PTRef term) {
@@ -48,6 +52,8 @@ class TermNames {
4852
}
4953

5054
std::vector<TermName> const & namesForTerm(PTRef term) const {
55+
assert(contains(term));
56+
term = internalToUserTermMap.getUserTerm(term);
5157
assert(contains(term));
5258
return termToNames.at(term);
5359
}
@@ -72,6 +78,7 @@ class TermNames {
7278

7379
// std::optional does not work with references so we must use pointers
7480
std::vector<TermName> const * tryGetNamesForTerm(PTRef term) const {
81+
term = internalToUserTermMap.getUserTerm(term);
7582
if (auto it = termToNames.find(term); it != termToNames.end()) { return &it->second; }
7683

7784
return nullptr;
@@ -121,16 +128,26 @@ class TermNames {
121128
if (isGlobal()) { return; }
122129
scopedNamesAndTerms.popScope([this](auto const & p) {
123130
auto const & [name, term] = p;
124-
auto it = nameToTerm.find(name);
125-
if (it == nameToTerm.end()) { return; }
126-
auto & names_ = _namesForTerm(term);
127-
names_.erase(std::find(names_.begin(), names_.end(), name));
128-
nameToTerm.erase(it);
131+
assert(not contains(term) or nameToTerm.find(name)->second.x == term.x);
132+
eraseTermName(name);
129133
});
130134
}
131135

136+
bool eraseTermName(TermName const & name) {
137+
auto termIt = nameToTerm.find(name);
138+
if (termIt == nameToTerm.end()) { return false; }
139+
140+
auto const & term = termIt->second;
141+
auto & names_ = _namesForTerm(term);
142+
names_.erase(std::find(names_.begin(), names_.end(), name));
143+
nameToTerm.erase(termIt);
144+
return true;
145+
}
146+
132147
SMTConfig const & config;
133148

149+
InternalToUserTermMap const & internalToUserTermMap;
150+
134151
ScopedNamesAndTerms scopedNamesAndTerms;
135152
NameToTermMap nameToTerm;
136153
TermToNamesMap termToNames;

0 commit comments

Comments
 (0)