Lattice Land Core Library
sort.hpp
Go to the documentation of this file.
1 // Copyright 2022 Pierre Talbot
2 
3 #ifndef LALA_CORE_TYPES_HPP
4 #define LALA_CORE_TYPES_HPP
5 
6 #include "battery/utility.hpp"
7 #include "battery/vector.hpp"
8 #include "battery/string.hpp"
9 #include "battery/tuple.hpp"
10 #include "battery/variant.hpp"
11 #include "battery/unique_ptr.hpp"
12 
13 namespace lala {
14 
15 /** Each abstract domain is uniquely identified by an UID.
16  We call it an _abstract type_.
17  Each formula (and recursively, its subformulas) is assigned to an abstract type indicating in what abstract domain this formula should be interpreted. */
18 using AType = int;
19 
20 /** This value means a formula is not typed in a particular abstract domain and its type should be inferred. */
21 #define UNTYPED (-1)
22 
23 /** The concrete type of variables, called `sort`, introduced by existential quantification.
24  More concrete types could be added later. */
25 template <class Allocator>
26 struct Sort {
27  enum Tag {
29  Int,
31  Set
32  };
33 
34  using allocator_type = Allocator;
36 
38  battery::unique_ptr<this_type, allocator_type> sub;
39 
40  CUDA Sort(Tag tag): tag(tag) {
41  assert(tag != Set);
42  }
43 
44  CUDA NI Sort(Tag tag, Sort&& sub_ty, const allocator_type& alloc = allocator_type())
45  : tag(tag), sub(battery::allocate_unique<this_type>(alloc, std::move(sub_ty)))
46  {
47  assert(tag == Set);
48  }
49 
50  template<class Alloc2>
51  CUDA NI Sort(const Sort<Alloc2>& other, const allocator_type& alloc = allocator_type())
52  : tag(static_cast<Tag>(other.tag))
53  {
54  if(other.sub) {
55  this_type s = this_type(*other.sub, alloc);
56  sub = battery::allocate_unique<this_type>(alloc, std::move(s));
57  }
58  }
59 
60  CUDA Sort(const this_type& other): Sort(other, other.sub.get_allocator()) {}
61 
62  Sort& operator=(const this_type& other) = default;
63  Sort(Sort&&) = default;
64  Sort& operator=(Sort&&) = default;
65 
66  CUDA bool is_bool() const { return tag == Bool; }
67  CUDA bool is_int() const { return tag == Int; }
68  CUDA bool is_real() const { return tag == Real; }
69  CUDA bool is_set() const { return tag == Set; }
70 
71  CUDA NI void print() const {
72  switch(tag) {
73  case Bool: printf("B"); break;
74  case Int: printf("Z"); break;
75  case Real: printf("R"); break;
76  case Set: printf("S("); sub->print(); printf(")"); break;
77  default: assert(false); // "Sort: Unknown type".
78  }
79  }
80 
81  template <class U>
82  CUDA NI void print_value(const U& u) const {
83  if(is_bool()) {
84  if(u <= U::eq_zero()) {
85  printf("false");
86  }
87  else {
88  printf("true");
89  }
90  }
91  else {
92  u.lb().print();
93  }
94  }
95 };
96 
97 template <class Alloc1, class Alloc2>
98 CUDA NI inline bool operator==(const Sort<Alloc1>& lhs, const Sort<Alloc2>& rhs) {
99  if(static_cast<int>(lhs.tag) == static_cast<int>(rhs.tag)) {
100  if(lhs.tag == Sort<Alloc1>::Set) {
101  return *(lhs.sub) == *(rhs.sub);
102  }
103  return true;
104  }
105  return false;
106 }
107 
108 /** The type of Boolean used in logic formulas. */
109 using logic_bool = bool;
110 
111 /** The type of integers used in logic formulas.
112  Integers are represented by the set \f$ \{-\infty, \infty\} \cup Z (\text{ with} Z \subset \mathbb{Z}) \f$.
113  The minimal and maximal values of `logic_int` represents \f$ -\infty \f$ and \f$ \infty \f$ respectively. */
114 using logic_int = long long int;
115 
116 /** The type of real numbers used in logic formulas.
117  Real numbers are approximated by the set \f$ \mathbb{F} \times \mathbb{F} \f$.
118  When a real number \f$ r \in \mathbb{R} \f$ is also a floating-point number, then it is represented by \f$ (r, r) \f$, otherwise it is represented by \f$ (\lfloor r \rfloor, \lceil r \rceil) \f$ such that \f$ \lfloor r \rfloor < r < \lceil r \rceil \f$ and there is no floating-point number \f$ f \f$ such that \f$ \lfloor r \rfloor < f < \lceil r \rceil \f$. */
119 using logic_real = battery::tuple<double, double>;
120 
121 /** A set is parametric in a universe of discourse.
122  For instance, `logic_set<F>`, with F representing an integer constant, is a set of integers.
123  Sets are defined in extension: we explicitly list the values belonging to the set.
124  To avoid using too much memory with large sets, we use an interval representation, e.g., \f$ \{1..3, 5..5, 10..12\} = \{1, 2, 3, 5, 10, 11, 12\} \f$.
125  When sets occur in intervals, they are ordered by set inclusion, e.g., \f$ \{\{1..2\}..\{1..4\}\} = \{\{1,2\}, \{1,2,3\}, \{1,2,4\}, \{1,2,3,4\}\} \f$. */
126 template<class F>
127 using logic_set = battery::vector<battery::tuple<F, F>, typename F::allocator_type>;
128 
129 }
130 
131 #endif
Definition: ast.hpp:223
Definition: abstract_deps.hpp:14
battery::tuple< double, double > logic_real
Definition: sort.hpp:119
bool logic_bool
Definition: sort.hpp:109
constexpr CUDA bool operator==(const CartesianProduct< As... > &a, const CartesianProduct< Bs... > &b)
Definition: cartesian_product.hpp:504
long long int logic_int
Definition: sort.hpp:114
battery::vector< battery::tuple< F, F >, typename F::allocator_type > logic_set
Definition: sort.hpp:127
int AType
Definition: sort.hpp:18
Definition: sort.hpp:26
Sort(Sort &&)=default
Tag
Definition: sort.hpp:27
@ Bool
Definition: sort.hpp:28
@ Int
Definition: sort.hpp:29
@ Set
Definition: sort.hpp:31
@ Real
Definition: sort.hpp:30
CUDA NI Sort(const Sort< Alloc2 > &other, const allocator_type &alloc=allocator_type())
Definition: sort.hpp:51
CUDA bool is_real() const
Definition: sort.hpp:68
Sort & operator=(Sort &&)=default
CUDA bool is_bool() const
Definition: sort.hpp:66
battery::unique_ptr< this_type, allocator_type > sub
Definition: sort.hpp:38
CUDA NI Sort(Tag tag, Sort &&sub_ty, const allocator_type &alloc=allocator_type())
Definition: sort.hpp:44
CUDA bool is_int() const
Definition: sort.hpp:67
Sort< allocator_type > this_type
Definition: sort.hpp:35
CUDA NI void print_value(const U &u) const
Definition: sort.hpp:82
Sort & operator=(const this_type &other)=default
Tag tag
Definition: sort.hpp:37
CUDA NI void print() const
Definition: sort.hpp:71
CUDA bool is_set() const
Definition: sort.hpp:69
CUDA Sort(Tag tag)
Definition: sort.hpp:40
Allocator allocator_type
Definition: sort.hpp:34
CUDA Sort(const this_type &other)
Definition: sort.hpp:60