Lattice Land Core Library
b.hpp
Go to the documentation of this file.
1 // Copyright 2024 Pierre Talbot
2 
3 #ifndef LALA_CORE_B_HPP
4 #define LALA_CORE_B_HPP
5 
6 #include "battery/memory.hpp"
7 
8 namespace lala {
9 
10 template <class Mem> class B;
11 namespace local {
13 }
14 
15 /** This class represents a Boolean lattice \langle \{\mathit{false},\mathit{true}\}, \implies, \lor, \land, \mathit{false}, \mathit{true} \rangle \f$.
16  * The order, join and meet operations are given by the usual Boolean logical connectors.
17  * Note that \f$ \bot = \mathit{false} \f$ and \f$ \top = \mathit{true} \f$.
18  * It cannot represent a logical formula and does not have a concretization function to a concrete domain.
19  * For instance, it is used for extra-logical variables such as the variable `has_changed` indicating whether something changed or not.
20 */
21 template<class Mem>
22 class B
23 {
24 public:
25  using value_type = bool;
26  using memory_type = Mem;
27  using this_type = B<Mem>;
28 
29  template<class M>
30  using this_type2 = B<M>;
31 
32  using local_type = this_type2<battery::local_memory>;
33 
34  constexpr static const bool sequential = Mem::sequential;
35  constexpr static const bool is_totally_ordered = true;
36  constexpr static const bool complemented = true;
37  constexpr static const char* name = "B";
38 
39 private:
40  using atomic_type = memory_type::template atomic_type<value_type>;
41  atomic_type val;
42 
43 public:
44  CUDA static constexpr local_type bot() { return false; }
45  CUDA static constexpr local_type top() { return true; }
46  CUDA constexpr B(): val(false) {}
47  CUDA constexpr B(value_type x): val(x) {}
48  CUDA constexpr B(const this_type& other): B(other.value()) {}
49  constexpr B(this_type&& other) = default;
50 
51  template <class M>
52  CUDA constexpr B(const this_type2<M>& other): B(other.value()) {}
53 
54  /** The assignment operator can only be used in a sequential context.
55  * It is monotone but not extensive.
56  * @sequential @order-preserving */
57  template <class M>
58  CUDA constexpr this_type& operator=(const this_type2<M>& other) {
59  memory_type::store(val, other.value());
60  return *this;
61  }
62 
63  /** @sequential @order-preserving */
64  CUDA constexpr this_type& operator=(const this_type& other) {
65  memory_type::store(val, other.value());
66  return *this;
67  }
68 
69  CUDA constexpr this_type& operator=(bool other) {
70  return operator=(local::B(other));
71  }
72 
73  /** @parallel */
74  CUDA constexpr value_type value() const { return memory_type::load(val); }
75 
76  /** @parallel */
77  CUDA constexpr operator value_type() const { return value(); }
78 
79  /** `true` whenever \f$ a = \top \f$, `false` otherwise.
80  * @parallel @order-preserving @increasing
81  */
82  CUDA constexpr B is_top() const {
83  return value();
84  }
85 
86  /** `true` whenever \f$ a = \bot \f$, `false` otherwise.
87  * @parallel @order-preserving @decreasing
88  */
89  CUDA constexpr B is_bot() const {
90  return !value();
91  }
92 
93  /** @parallel @order-preserving @increasing */
94  CUDA constexpr void join_top() {
95  memory_type::store(val, true);
96  }
97 
98  /** @parallel @order-preserving @increasing
99  * \return `true` if the value has changed, `false` otherwise.
100  */
101  template<class M1>
102  CUDA constexpr bool join(const this_type2<M1>& other) {
103  if(!value() && other.value()) {
104  memory_type::store(val, true);
105  return true;
106  }
107  return false;
108  }
109 
110  /** @order-preserving @increasing */
111  CUDA constexpr void meet_bot() {
112  memory_type::store(val, false);
113  }
114 
115  /** @order-preserving @increasing
116  * \return `true` if the value has changed, `false` otherwise.
117  */
118  template<class M1>
119  CUDA constexpr bool meet(const this_type2<M1>& other) {
120  if(value() && !other.value()) {
121  memory_type::store(val, false);
122  return true;
123  }
124  return false;
125  }
126 
127  CUDA constexpr bool join(value_type other) {
128  return join(local::B(other));
129  }
130 
131  CUDA constexpr bool meet(value_type other) {
132  return meet(local::B(other));
133  }
134 
135  /** Print the current element.
136  * @sequential
137  */
138  CUDA NI void print() const {
139  ::battery::print(value());
140  }
141 
142  CUDA constexpr this_type& operator|= (const this_type& other) {
143  join(other);
144  return *this;
145  }
146 
147  CUDA constexpr this_type& operator&= (const this_type& other) {
148  meet(other);
149  return *this;
150  }
151 
152  CUDA constexpr this_type& operator|= (value_type other) {
153  join(other);
154  return *this;
155  }
156 
157  CUDA constexpr this_type& operator&= (value_type other) {
158  meet(other);
159  return *this;
160  }
161 
162 
163  template<class Mem2>
164  friend class B;
165 };
166 
167 /** @parallel */
168 template<class M1, class M2>
169 CUDA constexpr bool operator<=(const B<M1>& a, const B<M2>& b) {
170  return !a.value() || b.value();
171 }
172 
173 template<class M1, class M2>
174 CUDA constexpr bool operator<(const B<M1>& a, const B<M2>& b) {
175  return !a.value() && b.value();
176 }
177 
178 template<class M1, class M2>
179 CUDA constexpr bool operator>=(const B<M1>& a, const B<M2>& b) {
180  return b <= a;
181 }
182 
183 template<class M1, class M2>
184 CUDA constexpr bool operator>(const B<M1>& a, const B<M2>& b) {
185  return b < a;
186 }
187 
188 template<class M1, class M2>
189 CUDA constexpr bool operator==(const B<M1>& a, const B<M2>& b) {
190  return a.value() == b.value();
191 }
192 
193 template<class M1, class M2>
194 CUDA constexpr bool operator!=(const B<M1>& a, const B<M2>& b) {
195  return a.value() != b.value();
196 }
197 
198 template<class M>
199 std::ostream& operator<<(std::ostream &s, const B<M> &a) {
200  s << a.value();
201  return s;
202 }
203 
204 } // namespace lala
205 
206 #endif
Definition: b.hpp:10
CUDA NI void print(const lala::Sig &sig)
Definition: ast.hpp:225
::lala::B<::battery::local_memory > B
Definition: b.hpp:12
Definition: abstract_deps.hpp:14
constexpr CUDA bool operator==(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition: cartesian_product.hpp:504
std::ostream & operator<<(std::ostream &s, const CartesianProduct< A, As... > &cp)
Definition: cartesian_product.hpp:531
constexpr CUDA bool operator<(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition: cartesian_product.hpp:485
constexpr CUDA bool operator>(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition: cartesian_product.hpp:498