9
9
#include < util/substitute_symbols.h>
10
10
#include < util/format_expr.h>
11
11
12
- class ResolutionVisitor : public wto_element_visitort
12
+ /*
13
+ * Traverses the clauses in order and resolving all predicates (symbols)
14
+ * that are not a head (e.g. a head of a loop).
15
+ * This is similar to variable elimination in SAT.
16
+ */
17
+ class resolution_visitort : public wto_element_visitort
13
18
{
14
19
private:
15
- chc_db & m_db;
16
- chc_db m_new_db;
17
- std::unordered_map<std::size_t , std::vector<horn_clause>> m_def;
18
- std::set<std::size_t > m_heads;
20
+ chc_dbt & m_db;
21
+ chc_dbt m_new_db;
22
+ std::unordered_map<std::size_t , std::vector<horn_clauset>> m_def;
23
+ std::unordered_set<std::size_t > m_heads;
24
+ bool m_verbose;
19
25
20
26
public:
21
- ResolutionVisitor (chc_db & db) : m_db(db) {}
27
+ resolution_visitort (chc_dbt & db) : m_db(db), m_verbose( false ) {}
22
28
23
- chc_db &giveme_new_db () { return m_new_db; }
29
+ chc_dbt &giveme_new_db () { return m_new_db; }
24
30
25
31
virtual void visit (const wto_singletont & s)
26
32
{
27
33
const symbol_exprt* symb = s.get ();
28
- resolve (symb);
34
+ eliminate (symb);
29
35
}
30
36
31
37
virtual void visit (const wto_componentt & comp)
@@ -38,7 +44,7 @@ class ResolutionVisitor : public wto_element_visitort
38
44
{
39
45
it->get ()->accept (this );
40
46
}
41
- resolve (head);
47
+ eliminate (head);
42
48
}
43
49
44
50
void populate_new_db ()
@@ -75,43 +81,41 @@ class ResolutionVisitor : public wto_element_visitort
75
81
}
76
82
77
83
private:
78
- void resolve (const symbol_exprt *symb)
84
+ void eliminate (const symbol_exprt *symb)
79
85
{
80
- for (auto clause : m_db.def (*symb))
86
+ for (auto idx : m_db.def (*symb))
81
87
{
88
+ auto & clause = m_db.get_clause (idx);
82
89
std::vector<symbol_exprt> use;
83
- if (false )
84
- {
85
- std::cout << " Clause: " << format (clause->get_chc ()) << " \n " ;
86
- }
87
- clause->used_relations (m_db,std::back_inserter (use));
90
+
91
+ clause.used_relations (m_db,std::back_inserter (use));
88
92
if (use.size () > 1 ) {
89
93
throw incorrect_goto_program_exceptiont (" Non-linear CHCs not supported yet" );
90
94
}
91
95
92
- if (clause-> is_fact ())
96
+ if (clause. is_fact ())
93
97
{
94
98
m_heads.insert (symb->hash ());
95
- std::vector<horn_clause > def_chcs;
96
- def_chcs.push_back (clause-> get_chc ());
99
+ std::vector<horn_clauset > def_chcs;
100
+ def_chcs.push_back (clause. get_chc ());
97
101
m_def.insert (std::make_pair (symb->hash (), def_chcs));
98
102
}
99
103
100
104
for (auto & p : use)
101
105
{
102
106
auto it = m_def.find (p.hash ());
103
- std::vector<horn_clause > def_chcs;
107
+ std::vector<horn_clauset > def_chcs;
104
108
if (it == m_def.end () || m_heads.find (p.hash ()) != m_heads.end ())
105
109
{
106
- def_chcs.push_back (* clause);
110
+ def_chcs.push_back (clause);
107
111
}
108
112
else
109
113
{
110
114
for (auto cls_it = it->second .begin (); cls_it != it->second .end (); cls_it++)
111
115
{
112
- forall_exprt resolvent = resolve_cls2 ((*cls_it), * clause);
113
- if (false )
114
- std::cout << " Result where :\n " << format (resolvent) << " \n " ;
116
+ forall_exprt resolvent = resolve_clauses ((*cls_it), clause);
117
+ if (m_verbose )
118
+ std::cout << " Result:\n " << format (resolvent) << " \n " ;
115
119
def_chcs.push_back (resolvent);
116
120
}
117
121
}
@@ -127,7 +131,7 @@ class ResolutionVisitor : public wto_element_visitort
127
131
}
128
132
}
129
133
130
- forall_exprt resolve_cls2 (const horn_clause & c1, const horn_clause & c2)
134
+ forall_exprt resolve_clauses (const horn_clauset & c1, const horn_clauset & c2)
131
135
{
132
136
const exprt &body1 = *c1.body ();
133
137
const exprt &head1 = *c1.head ();
@@ -151,7 +155,7 @@ class ResolutionVisitor : public wto_element_visitort
151
155
if (head1_pred == nullptr )
152
156
throw analysis_exceptiont (" Resolution not possible" );
153
157
154
- if (false )
158
+ if (m_verbose )
155
159
std::cout << " Resolving: \n " << format (c1.get_chc ()) << " \n And: \n "
156
160
<< format (c2.get_chc ()) << " \n " ;
157
161
@@ -179,9 +183,6 @@ class ResolutionVisitor : public wto_element_visitort
179
183
if ((head_arg.id () != ID_symbol) ||
180
184
(to_symbol_expr (head_arg).get_identifier () != body_arg.get_identifier ()))
181
185
{
182
- // std::cout << "body arg: " << format(body_arg)
183
- // << " head arg: " << format(head_arg) << "\n";
184
-
185
186
std::map<irep_idt, exprt> subs;
186
187
subs.insert (std::make_pair (body_arg.get_identifier (), head_arg));
187
188
0 commit comments