3#ifndef LALA_CORE_CARTESIAN_PRODUCT_HPP
4#define LALA_CORE_CARTESIAN_PRODUCT_HPP
6#include "battery/utility.hpp"
7#include "battery/vector.hpp"
8#include "battery/tuple.hpp"
9#include "battery/variant.hpp"
16class CartesianProduct;
20 CUDA
constexpr auto index_sequence_of(
const CartesianProduct<As...>&) {
21 return std::index_sequence_for<As...>{};
25 CUDA
constexpr auto index_sequence_of(
const battery::tuple<As...>&) {
26 return std::index_sequence_for<As...>{};
29 template <
class A,
class B>
30 CUDA
constexpr auto index_sequence_of(
const A& a,
const B& b) {
31 static_assert(
decltype(impl::index_sequence_of(a))::size() ==
decltype(impl::index_sequence_of(b))::size());
32 return impl::index_sequence_of(a);
36 CUDA
constexpr typename CartesianProduct<Os...>::local_type make_cp(Os... os) {
42 static constexpr bool value =
false;
46 struct is_product<CartesianProduct<As...>> {
47 static constexpr bool value =
true;
51 struct is_product<
battery::tuple<As...>> {
52 static constexpr bool value =
true;
56 inline static constexpr bool is_product_v = is_product<A>::value;
65 using type_of =
typename battery::tuple_element<i, battery::tuple<As...>>::type;
66 constexpr static size_t n = battery::tuple_size<battery::tuple<As...>>{};
67 static_assert(
n > 0,
"CartesianProduct must not be empty.");
74 using value_type = battery::tuple<
typename As::value_type...>;
77 constexpr static const bool sequential = (... && As::sequential);
79 constexpr static const bool preserve_bot = (... && As::preserve_bot);
80 constexpr static const bool preserve_top = (... && As::preserve_top);
85 constexpr static const char*
name =
"CartesianProduct";
88 battery::tuple<As...> val;
100 template<
class... Bs>
105 template <
class... Bs>
128 template<
IKind kind,
bool diagnose,
size_t i,
class F,
class Env,
class... Bs>
133 template<
IKind kind,
bool diagnose,
size_t i = 0,
class F,
class Env,
class... Bs>
134 CUDA NI
static bool interpret_all(
const F& f,
CartesianProduct<Bs...>& k,
bool empty,
const Env& env, IDiagnostics& diagnostics) {
135 if constexpr(i ==
n) {
139 bool res = interpret_one<kind, diagnose, i>(f, env, k, diagnostics);
140 return interpret_all<kind, diagnose, i+1>(f, k, empty && !res, env, diagnostics);
145 template<
size_t i,
bool diagnose =
false,
class F,
class Env,
class... Bs>
147 return interpret_one<IKind::TELL, diagnose, i>(f, env, k, diagnostics);
150 template<
size_t i,
bool diagnose =
false,
class F,
class Env,
class... Bs>
152 return interpret_one<IKind::ASK, diagnose, i>(f, env, k, diagnostics);
155 template<
IKind kind,
bool diagnose =
false,
class F,
class Env,
class... Bs>
158 "No component of this Cartesian product can interpret this formula.",
159 (interpret_all<kind, diagnose>(f, k,
true, env, diagnostics))
164 template<
bool diagnose,
class F,
class Env,
class... Bs>
169 template<
bool diagnose,
class F,
class Env,
class... Bs>
175 template<
size_t... I>
176 CUDA
constexpr value_type value_(std::index_sequence<I...>)
const {
180 template<
size_t... I>
181 CUDA
constexpr local::B is_top_(std::index_sequence<I...>)
const {
185 template<
size_t... I>
186 CUDA
constexpr local::B is_bot_(std::index_sequence<I...>)
const {
194 return battery::get<i>(val);
199 return battery::get<i>(val);
203 return value_(std::index_sequence_for<As...>{});
208 return is_top_(std::index_sequence_for<As...>{});
215 return is_bot_(std::index_sequence_for<As...>{});
219 template<
size_t i = 0,
class... Bs>
221 if constexpr (i <
n) {
223 has_changed |= join_<i+1>(other);
231 template<
size_t i = 0,
class... Bs>
233 if constexpr (i <
n) {
235 has_changed |= meet_<i+1>(other);
243 template<
size_t i = 0>
244 CUDA
constexpr void meet_bot_() {
245 if constexpr (i <
n) {
251 template<
size_t i = 0,
class... Bs>
253 if constexpr (i <
n) {
255 return is_under && extract_<i+1>(ua);
262 template <
size_t i = 0>
263 CUDA
constexpr void join_top_() {
264 if constexpr(i <
n) {
275 template <
class... Bs>
280 template<
size_t i,
class Ai>
281 CUDA
constexpr bool join(
const Ai& a) {
289 template <
class... Bs>
294 template<
size_t i,
class Ai>
295 CUDA
constexpr bool meet(
const Ai& a) {
300 template <
class... Bs>
308 template<
class A,
size_t... I>
309 CUDA
constexpr void project(
Sig fun,
const A& a, std::index_sequence<I...>)
314 template<
class A,
class B,
size_t... I>
315 CUDA
constexpr void project(
Sig fun,
const A& a,
const B& b, std::index_sequence<I...>)
320 template<
class A,
class B,
size_t... I>
321 CUDA
constexpr void project_left(
Sig fun,
const A& a,
const B& b, std::index_sequence<I...>)
326 template<
class A,
class B,
size_t... I>
327 CUDA
constexpr void project_right(
Sig fun,
const A& a,
const B& b, std::index_sequence<I...>)
334 return (... && As::is_trivial_fun(fun));
338 template<
class... Bs>
340 project(fun, a, impl::index_sequence_of(a));
345 template<
class... As2,
class... Bs>
347 project(fun, a, b, impl::index_sequence_of(a, b));
350 template<
class... As2,
class B>
352 project_left(fun, a, b, impl::index_sequence_of(a));
355 template<
class A,
class... Bs>
357 project_right(fun, a, b, impl::index_sequence_of(b));
361 template<
size_t i,
class Env,
class Allocator =
typename Env::allocator_type>
365 if constexpr(i <
n) {
366 auto f =
project<i>().deinterpret(x, env, allocator);
370 return deinterpret_<i+1, Env>(x, seq, env, allocator);
373 if(seq.size() == 1) {
374 return std::move(seq[0]);
385 template<
class Env,
class Allocator =
typename Env::allocator_type>
388 return deinterpret_<0, Env>(x, seq, env, allocator);
392 template<
size_t i = 0>
393 CUDA NI
void print_()
const {
394 if constexpr(i <
n) {
396 if constexpr(i <
n - 1) {
410template<
size_t i,
class... As>
411CUDA
constexpr const typename CartesianProduct<As...>::template type_of<i>&
416template<
size_t i,
class... As>
417CUDA
constexpr typename CartesianProduct<As...>::template type_of<i>&
424 template<
class A,
class B,
size_t... I>
425 CUDA
constexpr auto fjoin_(
const A& a,
const B& b, std::index_sequence<I...>)
430 template<
class A,
class B,
size_t... I>
431 CUDA
constexpr auto fmeet_(
const A& a,
const B& b, std::index_sequence<I...>)
436 template<
class A,
class B,
size_t... I>
437 CUDA
constexpr bool eq_(
const A& a,
const B& b, std::index_sequence<I...>)
442 template<
class A,
class B,
size_t... I>
443 CUDA
constexpr bool neq_(
const A& a,
const B& b, std::index_sequence<I...>)
448 template<
class A,
class B,
size_t... I>
449 CUDA
constexpr bool leq_(
const A& a,
const B& b, std::index_sequence<I...>)
454 template<
class A,
class B,
size_t... I>
455 CUDA
constexpr bool lt_(
const A& a,
const B& b, std::index_sequence<I...>)
457 return leq_(a, b, std::index_sequence<I...>{}) && neq_(a, b, std::index_sequence<I...>{});
462template<
class... As,
class... Bs>
465 return impl::fjoin_(a, b, impl::index_sequence_of(a, b));
469template<
class... As,
class... Bs>
472 return impl::fmeet_(a, b, impl::index_sequence_of(a, b));
476template<
class... As,
class... Bs>
477CUDA
constexpr bool operator<=(
const CartesianProduct<As...>& a,
const CartesianProduct<Bs...>& b)
479 if(a.is_bot()) {
return true; }
480 return impl::leq_(a, b, impl::index_sequence_of(a, b));
483template<
class... As,
class... Bs>
487 return impl::lt_(a, b, impl::index_sequence_of(a, b));
490template<
class... As,
class... Bs>
491CUDA
constexpr bool operator>=(
const CartesianProduct<As...>& a,
const CartesianProduct<Bs...>& b)
496template<
class... As,
class... Bs>
502template<
class... As,
class... Bs>
506 return impl::eq_(a, b, impl::index_sequence_of(a, b));
509template<
class... As,
class... Bs>
510CUDA
constexpr bool operator!=(
const CartesianProduct<As...>& a,
const CartesianProduct<Bs...>& b)
512 if(a.is_bot() != b.is_bot()) {
return true; }
513 return impl::neq_(a, b, impl::index_sequence_of(a, b));
517 template<
size_t i = 0,
class... As>
518 void std_print(std::ostream &s,
const CartesianProduct<As...> &cp) {
519 if constexpr(i < CartesianProduct<As...>::n) {
521 if constexpr(i < CartesianProduct<As...>::n - 1) {
523 std_print<i+1>(s, cp);
529template<
class A,
class... As>
534 impl::std_print<0>(s, cp);
Definition cartesian_product.hpp:62
CUDA constexpr this_type & operator=(const CartesianProduct< Bs... > &other)
Definition cartesian_product.hpp:106
CUDA constexpr bool join(const CartesianProduct< Bs... > &other)
Definition cartesian_product.hpp:276
CUDA constexpr void project(Sig fun, const CartesianProduct< As2... > &a, const B &b)
Definition cartesian_product.hpp:351
constexpr CartesianProduct()=default
CUDA constexpr void project(Sig fun, const CartesianProduct< As2... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:346
static CUDA bool interpret_ask(const F &f, const Env &env, CartesianProduct< Bs... > &k, IDiagnostics &diagnostics)
Definition cartesian_product.hpp:170
friend class CartesianProduct
Definition cartesian_product.hpp:72
static CUDA constexpr bool is_trivial_fun(Sig fun)
Definition cartesian_product.hpp:333
CUDA constexpr bool join(const Ai &a)
Definition cartesian_product.hpp:281
CUDA TFormula< Allocator > deinterpret(AVar x, const Env &env, const Allocator &allocator=Allocator()) const
Definition cartesian_product.hpp:386
CUDA constexpr bool extract(CartesianProduct< Bs... > &ua) const
Definition cartesian_product.hpp:301
CUDA constexpr local::B is_bot() const
Definition cartesian_product.hpp:214
CUDA constexpr local::B is_top() const
Definition cartesian_product.hpp:207
static CUDA bool interpret_one_ask(const F &f, const Env &env, CartesianProduct< Bs... > &k, IDiagnostics &diagnostics)
Definition cartesian_product.hpp:151
CUDA constexpr type_of< i > & project()
Definition cartesian_product.hpp:193
static CUDA bool interpret_one_tell(const F &f, const Env &env, CartesianProduct< Bs... > &k, IDiagnostics &diagnostics)
Definition cartesian_product.hpp:146
CUDA constexpr value_type value() const
Definition cartesian_product.hpp:202
static constexpr const bool is_totally_ordered
Definition cartesian_product.hpp:78
static constexpr const bool injective_concretization
Definition cartesian_product.hpp:83
static CUDA bool interpret_tell(const F &f, const Env &env, CartesianProduct< Bs... > &k, IDiagnostics &diagnostics)
Definition cartesian_product.hpp:165
static constexpr const bool preserve_join
Definition cartesian_product.hpp:81
CUDA void print() const
Definition cartesian_product.hpp:404
CUDA constexpr bool meet(const CartesianProduct< Bs... > &other)
Definition cartesian_product.hpp:290
CUDA constexpr void meet_bot()
Definition cartesian_product.hpp:285
static constexpr const bool sequential
Definition cartesian_product.hpp:77
typename battery::tuple_element< i, battery::tuple< As... > >::type type_of
Definition cartesian_product.hpp:65
static constexpr const char * name
Definition cartesian_product.hpp:85
CUDA constexpr void project(Sig fun, const CartesianProduct< Bs... > &a)
Definition cartesian_product.hpp:339
CUDA constexpr this_type & operator=(const this_type &other)
Definition cartesian_product.hpp:111
static constexpr size_t n
Definition cartesian_product.hpp:66
CUDA constexpr void join_top()
Definition cartesian_product.hpp:271
static constexpr const bool preserve_bot
Definition cartesian_product.hpp:79
static constexpr const bool preserve_meet
Definition cartesian_product.hpp:82
battery::tuple< typename As::value_type... > value_type
Definition cartesian_product.hpp:74
CUDA constexpr const type_of< i > & project() const
Definition cartesian_product.hpp:198
static CUDA constexpr local_type top()
Definition cartesian_product.hpp:122
typename type_of< 0 >::memory_type memory_type
Definition cartesian_product.hpp:70
CUDA constexpr CartesianProduct(const As &... as)
Definition cartesian_product.hpp:93
CUDA constexpr CartesianProduct(typename As::value_type... vs)
Definition cartesian_product.hpp:95
CartesianProduct< typename As::local_type... > local_type
Definition cartesian_product.hpp:69
CUDA constexpr bool meet(const Ai &a)
Definition cartesian_product.hpp:295
static constexpr const bool preserve_concrete_covers
Definition cartesian_product.hpp:84
CUDA static NI bool interpret(const F &f, const Env &env, CartesianProduct< Bs... > &k, IDiagnostics &diagnostics)
Definition cartesian_product.hpp:156
static constexpr const bool preserve_top
Definition cartesian_product.hpp:80
CUDA constexpr CartesianProduct(CartesianProduct< Bs... > &&other)
Definition cartesian_product.hpp:101
static CUDA constexpr local_type bot()
Definition cartesian_product.hpp:117
CUDA constexpr auto project(Sig fun, const A &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:356
static constexpr const bool is_abstract_universe
Definition cartesian_product.hpp:76
CUDA constexpr CartesianProduct(const CartesianProduct< Bs... > &other)
Definition cartesian_product.hpp:98
CUDA constexpr CartesianProduct(As &&... as)
Definition cartesian_product.hpp:94
Definition diagnostics.hpp:19
#define CALL_WITH_ERROR_CONTEXT(MSG, CALL)
Definition diagnostics.hpp:180
::lala::B<::battery::local_memory > B
Definition b.hpp:12
Definition abstract_deps.hpp:14
std::ostream & operator<<(std::ostream &s, const CartesianProduct< A, As... > &cp)
Definition cartesian_product.hpp:530
CUDA constexpr auto fjoin(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:463
CUDA constexpr bool operator==(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:503
@ AND
Unary arithmetic function symbols.
Definition ast.hpp:114
CUDA constexpr bool operator<(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:484
CUDA constexpr auto fmeet(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:470
CUDA constexpr bool operator>(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition cartesian_product.hpp:497
IKind
Definition ast.hpp:28
CUDA constexpr const CartesianProduct< As... >::template type_of< i > & project(const CartesianProduct< As... > &cp)
Similar to cp.template project<i>(), just to avoid the ".template" syntax.
Definition cartesian_product.hpp:412