CBMC
Loading...
Searching...
No Matches
solver_types.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Solver Types
4
5Author: Daniel Kroening, dkr@amazon.com
6
7\*******************************************************************/
8
11
12#include "solver_types.h"
13
14#include <util/format_expr.h>
15
16#include "free_symbols.h"
17
18#include <iostream>
19#include <set>
20
22{
23 if(invariant.id() == ID_and)
24 {
25 for(const auto &conjunct : to_and_expr(invariant).operands())
27 }
28 else
29 {
30 auxiliaries_set.insert(invariant);
31 auxiliaries.push_back(std::move(invariant));
32 }
33}
34
36{
37 if(invariant.id() == ID_and)
38 {
39 for(const auto &conjunct : to_and_expr(invariant).operands())
41 }
42 else
43 {
44 invariants_set.insert(invariant);
45 invariants.push_back(std::move(invariant));
46 }
47}
48
50{
51 if(obligation.id() == ID_and)
52 {
53 for(const auto &conjunct : to_and_expr(obligation).operands())
55 }
56 else
57 {
59 obligations.push_back(std::move(obligation));
60 }
61}
62
63frame_mapt build_frame_map(const std::vector<framet> &frames)
64{
66
67 for(std::size_t i = 0; i < frames.size(); i++)
68 frame_map[frames[i].symbol] = frame_reft(i);
69
70 return frame_map;
71}
72
73std::vector<framet> setup_frames(const std::vector<exprt> &constraints)
74{
75 std::set<symbol_exprt> states_set;
76 std::vector<framet> frames;
77
78 for(auto &c : constraints)
79 {
80 auto &location = c.source_location();
81 free_symbols(c, [&states_set, &location, &frames](const symbol_exprt &s) {
82 auto insert_result = states_set.insert(s);
83 if(insert_result.second)
84 frames.emplace_back(s, location, frame_reft(frames.size()));
85 });
86 }
87
88 return frames;
89}
90
92 const std::vector<exprt> &constraints,
93 std::vector<framet> &frames)
94{
95 const auto frame_map = build_frame_map(frames);
96
97 for(const auto &c : constraints)
98 {
99 // look for ∀ ς : state . Sxx(ς) ⇒ Syy(...)
100 // and ∀ ς : state . Sxx(ς) ⇒ ...
101 if(c.id() == ID_forall && to_forall_expr(c).where().id() == ID_implies)
102 {
104
105 if(
106 implication.rhs().id() == ID_function_application &&
107 to_function_application_expr(implication.rhs()).function().id() ==
108 ID_symbol)
109 {
111 to_function_application_expr(implication.rhs()).function());
112 auto s_it = frame_map.find(rhs_symbol);
113 if(s_it != frame_map.end())
114 {
115 frames[s_it->second.index].implications.emplace_back(
117 }
118 }
119 }
120 }
121}
122
125{
126 auto entry = frame_map.find(frame_symbol);
127
128 if(entry == frame_map.end())
129 PRECONDITION(false);
130
131 return entry->second;
132}
133
134std::vector<propertyt> find_properties(
135 const std::vector<exprt> &constraints,
136 const std::vector<framet> &frames)
137{
138 const auto frame_map = build_frame_map(frames);
139 std::vector<propertyt> properties;
140
141 for(const auto &c : constraints)
142 {
143 // look for ∀ ς : state . Sxx(ς) ⇒ ...
144 if(c.id() == ID_forall && to_forall_expr(c).where().id() == ID_implies)
145 {
147
148 if(
149 implication.rhs().id() != ID_function_application &&
150 implication.lhs().id() == ID_function_application &&
151 to_function_application_expr(implication.lhs()).function().id() ==
152 ID_symbol)
153 {
155 to_function_application_expr(implication.lhs()).function());
157 properties.emplace_back(
158 c.source_location(), lhs_frame, implication.rhs());
159 }
160 }
161 }
162
163 return properties;
164}
165
167{
168 // Sxx(ς) ⇒ p(ς)
169 return src.rhs();
170}
171
172void dump(
173 const std::vector<framet> &frames,
174 const propertyt &property,
175 bool values,
176 bool implications)
177{
178 for(const auto &f : frames)
179 {
180 std::cout << "FRAME: " << format(f.symbol) << '\n';
181
182 if(implications)
183 {
184 for(auto &c : f.implications)
185 {
186 std::cout << " implication: ";
187 std::cout << format(c.lhs) << " -> " << format(c.rhs);
188 std::cout << '\n';
189 }
190 }
191
192 if(values)
193 {
194 for(auto &i : f.invariants)
195 std::cout << " invariant: " << format(i) << '\n';
196
197 for(auto &i : f.obligations)
198 std::cout << " obligation: " << format(i) << '\n';
199
200 for(auto &i : f.auxiliaries)
201 std::cout << " auxiliary: " << format(i) << '\n';
202 }
203
204 if(property.frame == f.ref)
205 std::cout << " property: " << format(property.condition) << '\n';
206 }
207}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
exprt & rhs()
Definition std_expr.h:678
Base class for all expressions.
Definition expr.h:56
std::unordered_set< exprt, irep_hash > invariants_set
std::vector< exprt > obligations
std::unordered_set< exprt, irep_hash > auxiliaries_set
void add_invariant(exprt)
void add_auxiliary(exprt)
std::unordered_set< exprt, irep_hash > obligations_set
std::vector< exprt > auxiliaries
void add_obligation(exprt)
std::vector< exprt > invariants
Boolean implication.
Definition std_expr.h:2230
const irep_idt & id() const
Definition irep.h:388
Expression to hold a symbol (variable)
Definition std_expr.h:131
static format_containert< T > format(const T &o)
Definition format.h:37
void free_symbols(const exprt &expr, const std::function< void(const symbol_exprt &)> &f)
Free Symbols.
static exprt implication(exprt lhs, exprt rhs)
const forall_exprt & to_forall_expr(const exprt &expr)
const function_application_exprt & to_function_application_expr(const exprt &expr)
Cast an exprt to a function_application_exprt.
frame_reft find_frame(const frame_mapt &frame_map, const symbol_exprt &frame_symbol)
frame_mapt build_frame_map(const std::vector< framet > &frames)
std::vector< framet > setup_frames(const std::vector< exprt > &constraints)
void find_implications(const std::vector< exprt > &constraints, std::vector< framet > &frames)
std::vector< propertyt > find_properties(const std::vector< exprt > &constraints, const std::vector< framet > &frames)
exprt property_predicate(const implies_exprt &src)
void dump(const std::vector< framet > &frames, const propertyt &property, bool values, bool implications)
std::unordered_map< symbol_exprt, frame_reft, irep_hash > frame_mapt
#define PRECONDITION(CONDITION)
Definition invariant.h:463
const and_exprt & to_and_expr(const exprt &expr)
Cast an exprt to a and_exprt.
Definition std_expr.h:2177
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:272
const implies_exprt & to_implies_expr(const exprt &expr)
Cast an exprt to a implies_exprt.
Definition std_expr.h:2255