3#ifndef LALA_CORE_SIMPLIFIER_HPP
4#define LALA_CORE_SIMPLIFIER_HPP
9#include "battery/dynamic_bitset.hpp"
23template<
class A,
class Allocator>
34 constexpr static const bool sequential = universe_type::sequential;
43 constexpr static const char*
name =
"Simplifier";
45 template<
class A2,
class Alloc2>
61 battery::dynamic_bitset<memory_type, allocator_type> eliminated_variables;
63 battery::dynamic_bitset<memory_type, allocator_type> eliminated_formulas;
65 battery::vector<ZUB<int, memory_type>,
allocator_type> equivalence_classes;
67 battery::vector<universe_type, allocator_type> constants;
73 : atype(atype), sub(sub), env(alloc)
74 , formulas(alloc), simplified_formulas(alloc)
75 , eliminated_variables(alloc), eliminated_formulas(alloc)
76 , equivalence_classes(alloc), constants(alloc)
80 : atype(other.atype), sub(std::move(other.sub)), env(other.env)
81 , formulas(std::move(other.formulas)), simplified_formulas(std::move(other.simplified_formulas))
82 , eliminated_variables(std::move(other.eliminated_variables)), eliminated_formulas(std::move(other.eliminated_formulas))
83 , equivalence_classes(std::move(other.equivalence_classes)), constants(std::move(other.constants))
89 template<
class A2,
class Alloc2>
93 , env(other.env, alloc)
94 , equivalence_classes(other.equivalence_classes, alloc)
95 , constants(other.constants, alloc)
99 return formulas.get_allocator();
108 return sub->is_bot();
113 return equivalence_classes.size();
116 template <
class Alloc>
127 template <
bool diagnose = false,
class F,
class Env,
class Alloc2>
131 if(env.interpret(f.map_atype(
aty()), avar, diagnostics)) {
145 template <IKind kind,
bool diagnose = false,
class F,
class Env,
class Alloc2>
151 template <
class Alloc2>
153 if(t.env !=
nullptr) {
155 eliminated_variables.resize(t.num_vars);
156 eliminated_formulas.resize(t.formulas.size());
157 constants.resize(t.num_vars);
158 equivalence_classes.resize(t.num_vars);
159 for(
int i = 0; i < equivalence_classes.size(); ++i) {
162 formulas = std::move(t.formulas);
163 simplified_formulas.resize(formulas.size());
172 CUDA
AVar to_sub_var(
AVar x)
const {
173 assert(env[x].avars.size() > 0);
174 return env[x].avars[0];
177 CUDA AVar to_sub_var(
size_t vid)
const {
178 return to_sub_var(AVar{
aty(), vid});
182 CUDA AVar var_of(
const TFormula<allocator_type>& f)
const {
183 using F = TFormula<allocator_type>;
186 assert(env.
variable_of(f.lv())->get().avar_of(
aty()).has_value());
191 assert(env[f.v()].avar_of(
aty()).has_value());
192 return env[f.v()].avar_of(
aty()).value();
199 template <
class Alloc,
class Abs,
class Env>
201 const auto& local_var = env.
variable_of(vname)->get();
202 int rep = equivalence_classes[local_var.avar_of(
aty())->vid()];
204 auto benv_variable = benv.variable_of(rep_name);
205 if(benv_variable.has_value()) {
206 benv_variable->get().sort.print_value(b.project(benv_variable->get().avars[0]));
209 local_var.sort.print_value(constants[rep]);
215 CUDA
local::B eliminate(battery::dynamic_bitset<memory_type, allocator_type>& mask,
size_t i) {
225 const auto& u = sub->project(to_sub_var(i));
226 size_t j = equivalence_classes[i];
227 local::B has_changed = constants[j].meet(u);
229 has_changed |= eliminate(eliminated_variables, j);
234 CUDA
local::B cons_deduce(
size_t i) {
235 using F = TFormula<allocator_type>;
238 AVar x = var_of(formulas[i].seq(0));
239 AVar y = var_of(formulas[i].seq(1));
240 local::B has_changed = equivalence_classes[x.vid()].meet(
local::ZUB(equivalence_classes[y.vid()]));
241 has_changed |= equivalence_classes[y.vid()].meet(
local::ZUB(equivalence_classes[x.vid()]));
242 has_changed |= eliminate(eliminated_formulas, i);
247 IDiagnostics diagnostics;
248 typename sub_type::template ask_type<allocator_type> ask;
250 if(sub->interpret_ask(formulas[i], env, ask, diagnostics)) {
252 if(sub->template interpret_ask(formulas[i], env, ask, diagnostics)) {
255 return eliminate(eliminated_formulas, i);
261 auto f = formulas[i].map([&](
const F& f,
const F& parent) {
262 if(f.is_variable()) {
264 if(eliminated_variables.test(x.vid())) {
266 if(env[x].sort.is_bool() && k.is(F::Z) && parent.is_logical()) {
267 return k.z() == 0 ? F::make_false() : F::make_true();
271 else if(equivalence_classes[x.vid()] != x.vid()) {
272 return F::make_lvar(
UNTYPED, env.
name_of(AVar{aty(), equivalence_classes[x.vid()]}));
280 return eliminate(eliminated_formulas, i);
282 if(f != simplified_formulas[i]) {
283 simplified_formulas[i] = f;
293 return constants.size() + formulas.size();
298 if(i < constants.size()) {
302 return cons_deduce(i - constants.size());
308 for(
int i = 0; i < equivalence_classes.size(); ++i) {
309 if(equivalence_classes[i] == i && !eliminated_variables.test(i)) {
313 return equivalence_classes.size() - keep;
317 return eliminated_formulas.count();
325 return F::make_false();
329 for(
int i = 0; i < equivalence_classes.size(); ++i) {
330 eliminated_variables.set(equivalence_classes[i], eliminated_variables.test(equivalence_classes[i]) && eliminated_variables.test(i));
334 for(
int i = 0; i < equivalence_classes.size(); ++i) {
335 if(equivalence_classes[i] == i && !eliminated_variables.test(i)) {
336 const auto& x = env[
AVar{
aty(), i}];
337 seq.push_back(F::make_exists(
UNTYPED, x.name, x.sort));
340 seq.push_back(domain_constraint);
345 for(
int i = 0; i < simplified_formulas.size(); ++i) {
346 if(!eliminated_formulas.test(i)) {
347 seq.push_back(simplified_formulas[i]);
350 if(seq.size() == 0) {
351 return F::make_true();
354 return F::make_nary(
AND, std::move(seq));
Definition arith_bound.hpp:118
Definition diagnostics.hpp:19
Definition simplifier.hpp:24
CUDA allocator_type get_allocator() const
Definition simplifier.hpp:98
static constexpr const bool preserve_bot
Definition simplifier.hpp:37
CUDA local::B deduce(size_t i)
Definition simplifier.hpp:296
CUDA Simplifier(this_type &&other)
Definition simplifier.hpp:79
CUDA bool deduce(tell_type< Alloc2 > &&t)
Definition simplifier.hpp:152
Allocator allocator_type
Definition simplifier.hpp:26
A sub_type
Definition simplifier.hpp:27
CUDA size_t vars() const
Definition simplifier.hpp:112
static constexpr const bool preserve_join
Definition simplifier.hpp:39
CUDA local::B is_bot() const
Definition simplifier.hpp:107
static constexpr const bool injective_concretization
Definition simplifier.hpp:41
CUDA NI bool interpret_tell(const F &f, Env &env, tell_type< Alloc2 > &tell, IDiagnostics &diagnostics) const
Definition simplifier.hpp:128
CUDA size_t num_eliminated_variables() const
Definition simplifier.hpp:306
battery::vector< TFormula< allocator_type >, allocator_type > formula_sequence
Definition simplifier.hpp:48
CUDA bool interpret(const F &f, Env &env, tell_type< Alloc2 > &tell, IDiagnostics &diagnostics) const
Definition simplifier.hpp:146
CUDA AType aty() const
Definition simplifier.hpp:102
static constexpr const bool preserve_top
Definition simplifier.hpp:38
static constexpr const char * name
Definition simplifier.hpp:43
static constexpr const bool is_abstract_universe
Definition simplifier.hpp:33
static constexpr const bool is_totally_ordered
Definition simplifier.hpp:35
CUDA size_t num_deductions() const
Definition simplifier.hpp:292
typename sub_type::allocator_type sub_allocator_type
Definition simplifier.hpp:28
typename universe_type::memory_type memory_type
Definition simplifier.hpp:30
CUDA NI TFormula< allocator_type > deinterpret()
Definition simplifier.hpp:320
CUDA Simplifier(AType atype, abstract_ptr< sub_type > sub, const allocator_type &alloc=allocator_type())
Definition simplifier.hpp:70
CUDA size_t num_eliminated_formulas() const
Definition simplifier.hpp:316
CUDA void print_variable(const LVar< Alloc > &vname, const Env &benv, const Abs &b) const
Definition simplifier.hpp:200
CUDA Simplifier(const Simplifier< A2, Alloc2 > &other, light_copy_tag tag, abstract_ptr< sub_type > sub, const allocator_type &alloc=allocator_type())
Definition simplifier.hpp:90
static constexpr const bool sequential
Definition simplifier.hpp:34
typename sub_type::universe_type universe_type
Definition simplifier.hpp:29
static constexpr const bool preserve_concrete_covers
Definition simplifier.hpp:42
static constexpr const bool preserve_meet
Definition simplifier.hpp:40
CUDA NI std::optional< std::reference_wrapper< const variable_type > > variable_of(const char *lv) const
Definition env.hpp:457
CUDA const bstring & name_of(AVar av) const
Definition env.hpp:496
::lala::ZUB< int, battery::local_memory > ZUB
Definition arith_bound.hpp:69
::lala::B<::battery::local_memory > B
Definition b.hpp:12
Definition abstract_deps.hpp:14
CUDA NI void map_avar_to_lvar(F &f, const Env &env, bool erase_type=false)
Definition algorithm.hpp:398
CUDA constexpr LDual dual(const L &x)
Definition arith_bound.hpp:110
int AType
Definition sort.hpp:18
@ AND
Unary arithmetic function symbols.
Definition ast.hpp:114
CUDA NI bool is_var_equality(const TFormula< Allocator, ExtendedSig > &f)
Definition algorithm.hpp:32
battery::string< Allocator > LVar
Definition ast.hpp:25
battery::shared_ptr< A, typename A::allocator_type > abstract_ptr
Definition abstract_deps.hpp:17
CUDA NI F eval(const F &f)
Definition algorithm.hpp:774
#define UNTYPED
Definition sort.hpp:21
Definition simplifier.hpp:86
Definition simplifier.hpp:117
tell_type(const Alloc &alloc=Alloc())
Definition simplifier.hpp:121
int num_vars
Definition simplifier.hpp:118
VarEnv< Alloc > * env
Definition simplifier.hpp:120
formula_sequence formulas
Definition simplifier.hpp:119