3#ifndef LALA_CORE_VSTORE_HPP
4#define LALA_CORE_VSTORE_HPP
14 static constexpr bool atoms =
false;
18 static constexpr bool atoms =
true;
37template<
class U,
class Allocator>
45 template <
class Alloc>
53 template <
class VarDom>
57 template <
class Alloc>
58 using tell_type = battery::vector<var_dom<Alloc>, Alloc>;
60 template <
class Alloc>
63 template <
class Alloc = allocator_type>
67 constexpr static const bool sequential = universe_type::sequential;
71 constexpr static const bool preserve_join = universe_type::preserve_join;
72 constexpr static const bool preserve_meet = universe_type::preserve_meet;
75 constexpr static const char*
name =
"VStore";
77 template<
class U2,
class Alloc2>
81 using store_type = battery::vector<universe_type, allocator_type>;
82 using memory_type =
typename universe_type::memory_type;
90 : atype(other.atype), data(other.data), is_at_top(other.is_at_top)
95 : atype(atype), data(alloc), is_at_top(false)
99 : atype(atype), data(size, alloc), is_at_top(false)
104 : atype(other.atype), data(other.data), is_at_top(other.is_at_top)
107 template<
class R,
class Alloc2>
109 : atype(other.atype), data(other.data, alloc), is_at_top(other.is_at_top)
114 template<
class R,
class Alloc2,
class... Allocators>
119 atype(other.atype), data(std::move(other.data)), is_at_top(other.is_at_top) {}
122 return data.get_allocator();
137 return VStore(atype, alloc);
151 return bot(env.extends_abstract_dom(), alloc);
158 return top(env.extends_abstract_dom(), alloc);
169 if(is_at_top) {
return false; }
170 for(
int i = 0; i <
vars(); ++i) {
179 template <
class Alloc = allocator_type>
184 template <
class Alloc>
186 while(snap.size() < data.size()) {
190 for(
int i = 0; i < snap.size(); ++i) {
191 data[i].
dtell(snap[i]);
198 template <
bool diagnose,
class F,
class Env,
class Alloc2>
199 CUDA NI
bool interpret_existential(
const F& f, Env& env, tell_type<Alloc2>&
tell,
IDiagnostics& diagnostics)
const {
202 if(local_universe::template interpret_tell<diagnose>(f, env, k.dom, diagnostics)) {
203 if(env.interpret(f.map_atype(atype), k.avar, diagnostics)) {
212 template <
bool diagnose,
class F,
class Env,
class Alloc2>
213 CUDA NI
bool interpret_zero_predicate(
const F& f,
const Env& env, tell_type<Alloc2>&
tell, IDiagnostics& diagnostics)
const {
217 else if(f.is_false()) {
218 tell.push_back(var_dom<Alloc2>(AVar{}, U::top()));
227 template <IKind kind,
bool diagnose,
class F,
class Env,
class Alloc2>
228 CUDA NI
bool interpret_unary_predicate(
const F& f,
const Env& env, tell_type<Alloc2>&
tell, IDiagnostics& diagnostics)
const {
230 bool res = local_universe::template interpret<kind, diagnose>(f, env, u, diagnostics);
232 const auto& varf =
var_in(f);
236 tell.push_back(var_dom<Alloc2>(varf.v(), u));
239 auto var =
var_in(f, env);
240 if(!var.has_value()) {
243 auto avar = var->avar_of(atype);
244 if(!avar.has_value()) {
245 RETURN_INTERPRETATION_ERROR(
"The variable was not declared in the current abstract element (but exists in other abstract elements).");
247 tell.push_back(var_dom<Alloc2>(*avar, u));
256 template <IKind kind,
bool diagnose,
class F,
class Env,
class Alloc2>
257 CUDA NI
bool interpret_predicate(
const F& f, Env& env, tell_type<Alloc2>&
tell, IDiagnostics& diagnostics)
const {
258 if(f.type() !=
UNTYPED && f.type() !=
aty()) {
263 return interpret_existential<diagnose>(f, env,
tell, diagnostics);
267 case 0:
return interpret_zero_predicate<diagnose>(f, env,
tell, diagnostics);
268 case 1:
return interpret_unary_predicate<kind, diagnose>(f, env,
tell, diagnostics);
274 template <IKind kind,
bool diagnose = false,
class F,
class Env,
class I>
276 if(f.is_untyped() || f.type() ==
aty()) {
277 return interpret_predicate<kind, diagnose>(f, env, intermediate, diagnostics);
295 template <
bool diagnose = false,
class F,
class Env,
class Alloc2>
297 return interpret<IKind::TELL, diagnose>(f, env,
tell, diagnostics);
301 template <
bool diagnose = false,
class F,
class Env,
class Alloc2>
303 return const_cast<this_type*
>(
this)->interpret<IKind::ASK, diagnose>(f,
const_cast<Env&
>(env),
ask, diagnostics);
309 assert(x.
vid() < data.size());
310 return data[x.
vid()];
327 assert(x < data.size());
336 assert(x < data.size());
337 data[x].tell(dom, has_changed);
352 return tell(x.
vid(), dom, has_changed);
358 template <
class Alloc2,
class Mem>
363 if(t[0].avar ==
AVar{}) {
367 if(t.back().avar.vid() >= data.size()) {
368 data.resize(t.back().avar.vid()+1);
370 for(
int i = 0; i < t.size(); ++i) {
371 tell(t[i].avar, t[i].dom, has_changed);
379 template <
class Alloc2>
382 return tell(t, has_changed);
386 template <
class U2,
class Alloc2,
class Mem>
388 is_at_top.
tell(other.is_at_top, has_changed);
389 int min_size = battery::min(
vars(), other.
vars());
390 for(
int i = 0; i < min_size; ++i) {
391 data[i].tell(other[i], has_changed);
393 for(
int i = min_size; i < other.
vars(); ++i) {
394 assert(other[i].
is_bot());
400 template <
class U2,
class Alloc2>
403 return tell(other, has_changed);
408 for(
int i = 0; i < data.size(); ++i) {
415 template <
class U2,
class Alloc2,
class Mem>
420 int min_size = battery::min(
vars(), other.
vars());
421 is_at_top.
dtell(other.is_at_top, has_changed);
422 for(
int i = 0; i < min_size; ++i) {
423 data[i].
dtell(other[i], has_changed);
425 for(
int i = min_size; i <
vars(); ++i) {
426 data[i].
dtell(U::bot(), has_changed);
428 for(
int i = min_size; i < other.
vars(); ++i) {
429 assert(other[i].
is_bot());
435 template <
class U2,
class Alloc2,
class Mem>
438 return dtell(other, has_changed);
443 template <
class Alloc2>
445 for(
int i = 0; i < t.size(); ++i) {
446 if(!(data[t[i].avar.vid()] >= t[i].dom)) {
460 template<
class ExtractionStrategy = NonAtomicExtraction>
461 CUDA
bool is_extractable(
const ExtractionStrategy& strategy = ExtractionStrategy())
const {
465 if constexpr(ExtractionStrategy::atoms) {
466 for(
int i = 0; i < data.size(); ++i) {
467 if(data[i].lb() < dual<typename universe_type::LB>(data[i].ub())) {
478 template<
class U2,
class Alloc2>
480 if((
void*)&ua != (
void*)
this) {
482 ua.is_at_top.dtell_bot();
489 typename F::Sequence seq{env.get_allocator()};
490 for(
int i = 0; i < data.size(); ++i) {
492 seq.push_back(F::make_exists(
aty(), env.name_of(v), env.sort_of(v)));
493 auto f = data[i].deinterpret(v, env);
496 seq.push_back(std::move(f));
498 return F::make_nary(
AND, std::move(seq),
aty());
506 for(
int i = 0; i <
vars(); ++i) {
508 printf(
"%s", (i+1 ==
vars() ?
"" :
", "));
518template<
class L,
class K,
class Alloc>
521 using U =
decltype(
join(a[0], b[0]));
525 int max_size = battery::max(a.
vars(), b.
vars());
526 int min_size = battery::min(a.
vars(), b.
vars());
528 for(
int i = 0; i < min_size; ++i) {
531 for(
int i = min_size; i < a.
vars(); ++i) {
534 for(
int i = min_size; i < b.
vars(); ++i) {
540template<
class L,
class K,
class Alloc>
543 using U =
decltype(
meet(a[0], b[0]));
556 int min_size = battery::min(a.
vars(), b.
vars());
558 for(
int i = 0; i < min_size; ++i) {
565template<
class L,
class K,
class Alloc1,
class Alloc2>
566CUDA
bool operator<=(
const VStore<L, Alloc1>& a,
const VStore<K, Alloc2>& b)
572 int min_size = battery::min(a.vars(), b.vars());
573 for(
int i = 0; i < min_size; ++i) {
578 for(
int i = min_size; i < a.vars(); ++i) {
587template<
class L,
class K,
class Alloc1,
class Alloc2>
594 int min_size = battery::min(a.
vars(), b.
vars());
596 for(
int i = 0; i < b.
vars(); ++i) {
601 else if(a[i] > b[i]) {
605 else if(!b[i].is_bot()) {
609 for(
int i = min_size; i < a.
vars(); ++i) {
618template<
class L,
class K,
class Alloc1,
class Alloc2>
619CUDA
bool operator>=(
const VStore<L, Alloc1>& a,
const VStore<K, Alloc2>& b)
624template<
class L,
class K,
class Alloc1,
class Alloc2>
630template<
class L,
class K,
class Alloc1,
class Alloc2>
640 int min_size = battery::min(a.
vars(), b.
vars());
641 for(
int i = 0; i < min_size; ++i) {
646 for(
int i = min_size; i < a.
vars(); ++i) {
651 for(
int i = min_size; i < b.
vars(); ++i) {
660template<
class L,
class K,
class Alloc1,
class Alloc2>
661CUDA
bool operator!=(
const VStore<L, Alloc1>& a,
const VStore<K, Alloc2>& b)
666template<
class L,
class Alloc>
673 for(
int i = 0; i < vstore.
vars(); ++i) {
674 s << vstore[i] << (i+1 == vstore.
vars() ?
"" :
", ");
CUDA constexpr int vid() const
Definition ast.hpp:69
CUDA constexpr int aty() const
Definition ast.hpp:65
Definition abstract_deps.hpp:28
Definition diagnostics.hpp:19
Definition primitive_upset.hpp:118
CUDA constexpr this_type & dtell(const this_type2< M1 > &other, BInc< M2 > &has_changed)
Definition primitive_upset.hpp:265
CUDA constexpr this_type & dtell_bot()
Definition primitive_upset.hpp:259
CUDA constexpr this_type & tell_top()
Definition primitive_upset.hpp:233
CUDA constexpr this_type & tell(const this_type2< M1 > &other, BInc< M2 > &has_changed)
Definition primitive_upset.hpp:239
battery::vector< var_dom< Alloc >, Alloc > tell_type
Definition vstore.hpp:58
static CUDA this_type top(AType atype=UNTYPED, const allocator_type &alloc=allocator_type{})
Definition vstore.hpp:141
static constexpr const bool is_abstract_universe
Definition vstore.hpp:66
static constexpr const bool preserve_top
Definition vstore.hpp:70
static constexpr const bool preserve_bot
Definition vstore.hpp:69
CUDA this_type & tell(const VStore< U2, Alloc2 > &other)
Definition vstore.hpp:401
CUDA local::BInc is_top() const
Definition vstore.hpp:162
static CUDA this_type bot(AType atype=UNTYPED, const allocator_type &alloc=allocator_type{})
Definition vstore.hpp:134
CUDA const universe_type & project(AVar x) const
Definition vstore.hpp:307
CUDA AType aty() const
Definition vstore.hpp:125
CUDA VStore(AType atype, const allocator_type &alloc=allocator_type())
Definition vstore.hpp:94
static constexpr const bool sequential
Definition vstore.hpp:67
tell_type< Alloc > ask_type
Definition vstore.hpp:61
CUDA this_type & dtell(const VStore< U2, Alloc2 > &other, BInc< Mem > &has_changed)
Definition vstore.hpp:416
CUDA bool is_extractable(const ExtractionStrategy &strategy=ExtractionStrategy()) const
Definition vstore.hpp:461
CUDA void print() const
Definition vstore.hpp:501
static CUDA this_type top(Env &env, const allocator_type &alloc=allocator_type{})
Definition vstore.hpp:155
CUDA allocator_type get_allocator() const
Definition vstore.hpp:121
CUDA size_t num_refinements() const
Definition vstore.hpp:453
CUDA this_type & tell(const tell_type< Alloc2 > &t, BInc< Mem > &has_changed)
Definition vstore.hpp:359
battery::vector< local_universe, Alloc > snapshot_type
Definition vstore.hpp:64
CUDA this_type & dtell(const VStore< U2, Alloc2 > &other)
Definition vstore.hpp:436
CUDA this_type & tell_top()
Definition vstore.hpp:318
U universe_type
Definition vstore.hpp:40
static constexpr const bool preserve_join
Definition vstore.hpp:71
CUDA VStore(const VStore< R, allocator_type > &other)
Definition vstore.hpp:103
static constexpr const char * name
Definition vstore.hpp:75
CUDA snapshot_type< Alloc > snapshot(const Alloc &alloc=Alloc()) const
Definition vstore.hpp:180
typename universe_type::local_type local_universe
Definition vstore.hpp:41
friend class VStore
Definition vstore.hpp:78
CUDA local::BDec is_bot() const
Definition vstore.hpp:168
CUDA NI bool interpret(const F &f, Env &env, I &intermediate, IDiagnostics &diagnostics) const
Definition vstore.hpp:275
CUDA VStore(AType atype, size_t size, const allocator_type &alloc=allocator_type())
Definition vstore.hpp:98
CUDA this_type & restore(const snapshot_type< Alloc > &snap)
Definition vstore.hpp:185
CUDA const universe_type & operator[](int x) const
Definition vstore.hpp:314
static constexpr const bool injective_concretization
Definition vstore.hpp:73
static constexpr const bool preserve_concrete_covers
Definition vstore.hpp:74
CUDA this_type & dtell_bot()
Definition vstore.hpp:406
CUDA NI bool interpret_ask(const F &f, const Env &env, ask_type< Alloc2 > &ask, IDiagnostics &diagnostics) const
Definition vstore.hpp:302
CUDA VStore(this_type &&other)
Definition vstore.hpp:118
CUDA VStore(const this_type &other)
Definition vstore.hpp:89
CUDA this_type & tell(int x, const universe_type &dom)
Definition vstore.hpp:326
static constexpr const bool is_totally_ordered
Definition vstore.hpp:68
CUDA this_type & tell(AVar x, const universe_type &dom)
Definition vstore.hpp:343
CUDA void refine(size_t, BInc< M > &) const
Definition vstore.hpp:455
CUDA this_type & tell(int x, const universe_type &dom, BInc< Mem > &has_changed)
Definition vstore.hpp:335
CUDA this_type & tell(AVar x, const universe_type &dom, BInc< Mem > &has_changed)
Definition vstore.hpp:350
CUDA local::BInc ask(const ask_type< Alloc2 > &t) const
Definition vstore.hpp:444
static constexpr const bool preserve_meet
Definition vstore.hpp:72
CUDA void extract(VStore< U2, Alloc2 > &ua) const
Definition vstore.hpp:479
CUDA VStore(const VStore< R, Alloc2 > &other, const allocator_type &alloc=allocator_type())
Definition vstore.hpp:108
CUDA size_t vars() const
Definition vstore.hpp:130
CUDA NI TFormula< typename Env::allocator_type > deinterpret(const Env &env) const
Definition vstore.hpp:487
CUDA VStore(const VStore< R, Alloc2 > &other, const AbstractDeps< Allocators... > &deps)
Definition vstore.hpp:115
CUDA this_type & tell(const tell_type< Alloc2 > &t)
Definition vstore.hpp:380
Allocator allocator_type
Definition vstore.hpp:42
CUDA this_type & tell(const VStore< U2, Alloc2 > &other, BInc< Mem > &has_changed)
Definition vstore.hpp:387
CUDA NI bool interpret_tell(const F &f, Env &env, tell_type< Alloc2 > &tell, IDiagnostics &diagnostics) const
Definition vstore.hpp:296
static CUDA this_type bot(Env &env, const allocator_type &alloc=allocator_type{})
Definition vstore.hpp:148
#define RETURN_INTERPRETATION_ERROR(MSG)
Definition diagnostics.hpp:155
Definition abstract_deps.hpp:14
std::ostream & operator<<(std::ostream &s, const CartesianProduct< A, As... > &cp)
Definition cartesian_product.hpp:597
CUDA NI void map_avar_to_lvar(F &f, const Env &env, bool erase_type=false)
Definition algorithm.hpp:418
int AType
Definition sort.hpp:18
CUDA constexpr bool operator==(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:572
@ AND
Unary arithmetic function symbols.
Definition ast.hpp:114
CUDA constexpr auto join(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:534
CUDA constexpr auto meet(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:541
CUDA constexpr bool operator<(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:554
CUDA constexpr bool operator>(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:566
CUDA NI int num_vars(const F &f)
Definition algorithm.hpp:153
CUDA const TFormula< Allocator, ExtendedSig > & var_in(const TFormula< Allocator, ExtendedSig > &f)
Definition algorithm.hpp:145
#define UNTYPED
Definition sort.hpp:21
CUDA var_dom(const Alloc &)
Definition vstore.hpp:51
var_dom(const var_dom< Alloc > &)=default
CUDA var_dom(AVar avar, const local_universe &dom)
Definition vstore.hpp:52
AVar avar
Definition vstore.hpp:47
local_universe dom
Definition vstore.hpp:48
CUDA var_dom(const VarDom &other)
Definition vstore.hpp:54