Lattice Land Core Library
Loading...
Searching...
No Matches
pre_binc.hpp
Go to the documentation of this file.
1// Copyright 2022 Pierre Talbot
2
3#ifndef LALA_CORE_PRE_BINC_HPP
4#define LALA_CORE_PRE_BINC_HPP
5
6#include "../logic/logic.hpp"
7
8namespace lala {
9
10struct PreBDec;
11
12/** `PreBInc` is a pre-abstract universe \f$ \langle \{\mathit{true}, \mathit{false}\}, \leq \rangle \f$ such that \f$ \mathit{false} \leq \mathit{true} \f$.
13 It is used to represent Boolean variables which truth's value progresses from \f$ \mathit{false} \f$ to \f$ \mathit{true} \f$.
14 Note that this type is unable to represent Boolean domain which requires four states: unknown (bot), true, false and failed (top).
15 To obtain such a domain, you should use `Interval<BInc>`.
16*/
17struct PreBInc {
20 using value_type = bool;
22
23 constexpr static const bool is_totally_ordered = true;
24 constexpr static const bool preserve_bot = true;
25 constexpr static const bool preserve_top = false;
26 constexpr static const bool preserve_join = true;
27 constexpr static const bool preserve_meet = true;
28 constexpr static const bool injective_concretization = true;
29 constexpr static const bool preserve_concrete_covers = true;
30 constexpr static const bool complemented = false;
31 constexpr static const bool increasing = true;
32 constexpr static const char* name = "BInc";
33 constexpr static const bool is_arithmetic = true;
34 CUDA constexpr static value_type zero() { return false; }
35 CUDA constexpr static value_type one() { return true; }
36
37 /** Interpret a formula into an upset Boolean lattice.
38 * \return The result of the interpretation when the formula `f` is a constant of type `Bool`. Otherwise it returns an explanation of the error. */
39 template<bool diagnose, class F>
40 CUDA static bool interpret_tell(const F& f, value_type& tell, IDiagnostics& diagnostics) {
41 if(f.is(F::B)) {
42 tell = f.b();
43 return true;
44 }
45 RETURN_INTERPRETATION_ERROR("Only constant of types `Bool` can be interpreted in a Boolean domain.");
46 }
47
48 /** In this domain, the ask version of any constraint is the same as the tell version.
49 * This is because this domain can exactly represent Boolean values.
50 */
51 template<bool diagnose, class F>
52 CUDA static bool interpret_ask(const F& f, value_type& ask, IDiagnostics& diagnostics) {
53 return interpret_tell<diagnose>(f, ask, diagnostics);
54 }
55
56 /** Verify if the type of a variable, introduced by an existential quantifier, is compatible with the current abstract universe.
57 * \return `bot()` if the type of the existentially quantified variable is `Bool`. Otherwise it returns an explanation of the error.
58 */
59 template<bool diagnose, class F>
60 CUDA NI static bool interpret_type(const F& f, value_type& k, IDiagnostics& diagnostics) {
61 assert(f.is(F::E));
62 const auto& cty = battery::get<1>(f.exists());
63 if(cty.is_bool()) {
64 k = bot();
65 return true;
66 }
67 else {
68 const auto& vname = battery::get<0>(f.exists());
69 RETURN_INTERPRETATION_ERROR("The type of `" + vname + "` can only be `Bool`.");
70 }
71 }
72
73 /** Given a Boolean value, create a logical constant representing that value.
74 * Note that the lattice order has no influence here.
75 */
76 template<class F>
77 CUDA static F deinterpret(const value_type& v) {
78 return F::make_bool(v);
79 }
80
81 /** The logical predicate symbol corresponding to the order of this pre-universe.
82 We have \f$ a \leq_\mathit{BInc} b \Leftrightarrow a \leq b \f$.
83 \return The logical symbol `LEQ`. */
84 CUDA static constexpr Sig sig_order() { return LEQ; }
85 CUDA static constexpr Sig sig_strict_order() { return LT; }
86
87 /** \f$ \bot \f$ is represented by `false`. */
88 CUDA static constexpr value_type bot() { return false; }
89
90 /** \f$ \top \f$ is represented by `true`. */
91 CUDA static constexpr value_type top() { return true; }
92
93 /** \return \f$ x \sqcup y \f$ defined as \f$ x \lor y \f$. */
94 CUDA static constexpr value_type join(value_type x, value_type y) { return x || y; }
95
96 /** \return \f$ x \sqcap y \f$ defined as \f$ x \land y \f$. */
97 CUDA static constexpr value_type meet(value_type x, value_type y) { return x && y; }
98
99 /** \return \f$ \mathit{true} \f$ if \f$ x \leq_\mathit{BInc} y \f$ where the order \f$ \mathit{false} \leq_\mathit{BInc} \mathit{true} \f$, otherwise returns \f$ \mathit{false} \f$.
100 Note that the order is the Boolean implication, \f$ x \leq y \Rightleftarrow x \Rightarrow y \f$. */
101 CUDA static constexpr bool order(value_type x, value_type y) { return !x || y; }
102
103/** \return \f$ \mathit{true} \f$ if \f$ x \leq_\mathit{BInc} y \f$ where the order \f$ \mathit{false} \leq_\mathit{BInc} \mathit{true} \f$, otherwise returns \f$ \mathit{false} \f$.
104 Note that the strict order is the Boolean converse nonimplication, \f$ x \leq y \Rightleftarrow x \not\Leftarrow y \f$. */
105 CUDA static constexpr bool strict_order(value_type x, value_type y) { return !x && y; }
106
107 /** From a lattice perspective, this function returns an element \f$ y \f$ such that \f$ y \f$ is a cover of \f$ x \f$.
108
109 \return `true`. */
110 CUDA static constexpr value_type next(value_type x) { return true; }
111
112 /** From a lattice perspective, this function returns an element \f$ y \f$ such that \f$ x \f$ is a cover of \f$ y \f$.
113
114 \return `false`. */
115 CUDA static constexpr value_type prev(value_type x) { return false; }
116
117 CUDA NI static constexpr bool is_supported_fun(Sig sig) {
118 switch(sig) {
119 case AND:
120 case OR:
121 case IMPLY:
122 case EQUIV:
123 case XOR:
124 case NOT:
125 case EQ:
126 case NEQ:
127 return true;
128 default:
129 return false;
130 }
131 }
132
133 template<Sig sig>
134 CUDA static constexpr value_type fun(value_type x) {
135 static_assert(sig == NOT, "Unsupported unary function.");
136 switch(sig) {
137 case NOT: return !x;
138 default: assert(0); return x;
139 }
140 }
141
142 template<Sig sig>
143 CUDA NI static constexpr value_type fun(value_type x, value_type y) {
144 static_assert(sig == AND || sig == OR || sig == IMPLY || sig == EQUIV || sig == XOR,
145 "Unsupported binary function.");
146 switch(sig) {
147 case AND: return x && y;
148 case OR: return x || y;
149 case IMPLY: return !x || y;
150 case EQUIV:
151 case EQ: return x == y;
152 case XOR:
153 case NEQ: return x != y;
154 default: assert(0); return x;
155 }
156 }
157};
158
159} // namespace lala
160
161#endif
Definition diagnostics.hpp:19
#define RETURN_INTERPRETATION_ERROR(MSG)
Definition diagnostics.hpp:155
Definition abstract_deps.hpp:14
Sig
Definition ast.hpp:94
@ XOR
Logical connector.
Definition ast.hpp:114
@ NEQ
Equality relations.
Definition ast.hpp:112
@ LEQ
Unary arithmetic function symbols.
Definition ast.hpp:113
@ NOT
Unary arithmetic function symbols.
Definition ast.hpp:114
@ IMPLY
Unary arithmetic function symbols.
Definition ast.hpp:114
@ OR
Unary arithmetic function symbols.
Definition ast.hpp:114
@ EQ
Unary arithmetic function symbols.
Definition ast.hpp:112
@ LT
Unary arithmetic function symbols.
Definition ast.hpp:113
@ AND
Unary arithmetic function symbols.
Definition ast.hpp:114
@ EQUIV
Unary arithmetic function symbols.
Definition ast.hpp:114
Definition pre_bdec.hpp:16
Definition pre_binc.hpp:17
CUDA static NI constexpr bool is_supported_fun(Sig sig)
Definition pre_binc.hpp:117
static constexpr const bool preserve_top
Definition pre_binc.hpp:25
static constexpr const bool is_arithmetic
Definition pre_binc.hpp:33
static constexpr const bool increasing
Definition pre_binc.hpp:31
CUDA static NI constexpr value_type fun(value_type x, value_type y)
Definition pre_binc.hpp:143
static CUDA constexpr value_type prev(value_type x)
Definition pre_binc.hpp:115
static CUDA bool interpret_tell(const F &f, value_type &tell, IDiagnostics &diagnostics)
Definition pre_binc.hpp:40
static CUDA constexpr value_type fun(value_type x)
Definition pre_binc.hpp:134
static constexpr const bool injective_concretization
Definition pre_binc.hpp:28
CUDA static NI bool interpret_type(const F &f, value_type &k, IDiagnostics &diagnostics)
Definition pre_binc.hpp:60
CUDA static constexpr value_type zero()
Definition pre_binc.hpp:34
static CUDA constexpr bool strict_order(value_type x, value_type y)
Definition pre_binc.hpp:105
CUDA static constexpr value_type one()
Definition pre_binc.hpp:35
static CUDA constexpr value_type bot()
Definition pre_binc.hpp:88
static constexpr const bool preserve_bot
Definition pre_binc.hpp:24
static CUDA constexpr value_type meet(value_type x, value_type y)
Definition pre_binc.hpp:97
static CUDA constexpr value_type join(value_type x, value_type y)
Definition pre_binc.hpp:94
static CUDA F deinterpret(const value_type &v)
Definition pre_binc.hpp:77
static constexpr const bool complemented
Definition pre_binc.hpp:30
static CUDA bool interpret_ask(const F &f, value_type &ask, IDiagnostics &diagnostics)
Definition pre_binc.hpp:52
static CUDA constexpr value_type top()
Definition pre_binc.hpp:91
static constexpr const bool preserve_join
Definition pre_binc.hpp:26
static constexpr const char * name
Definition pre_binc.hpp:32
static CUDA constexpr bool order(value_type x, value_type y)
Definition pre_binc.hpp:101
static constexpr const bool preserve_concrete_covers
Definition pre_binc.hpp:29
static CUDA constexpr Sig sig_order()
Definition pre_binc.hpp:84
bool value_type
Definition pre_binc.hpp:20
static CUDA constexpr Sig sig_strict_order()
Definition pre_binc.hpp:85
static constexpr const bool is_totally_ordered
Definition pre_binc.hpp:23
static CUDA constexpr value_type next(value_type x)
Definition pre_binc.hpp:110
static constexpr const bool preserve_meet
Definition pre_binc.hpp:27