CBMC
literal.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_SOLVERS_PROP_LITERAL_H
11 #define CPROVER_SOLVERS_PROP_LITERAL_H
12 
13 #include <iosfwd>
14 #include <util/narrow.h>
15 #include <vector>
16 
17 // a pair of a variable number and a sign, encoded as follows:
18 //
19 // sign='false' means positive
20 // sign='true' means negative
21 //
22 // The top bit is used to indicate that the literal is constant
23 // true or false.
24 
25 class literalt
26 {
27 public:
28  // We deliberately don't use size_t here to save some memory
29  // on 64-bit machines; i.e., in practice, we restrict ourselves
30  // to SAT instances with no more than 2^31 variables.
31  typedef unsigned var_not;
32 
33  // constructors
35  {
36  set(unused_var_no(), false);
37  }
38 
40  {
41  set(v, sign);
42  }
43 
44  bool operator==(const literalt other) const
45  {
46  return l==other.l;
47  }
48 
49  bool operator!=(const literalt other) const
50  {
51  return l!=other.l;
52  }
53 
54  // for sets
55  bool operator<(const literalt other) const
56  {
57  return l<other.l;
58  }
59 
60  // negates if 'b' is true
61  literalt operator^(const bool b) const
62  {
63  literalt result(*this);
64  result.l^=(var_not)b;
65  return result;
66  }
67 
68  // negates the literal
70  {
71  literalt result(*this);
72  result.invert();
73  return result;
74  }
75 
76  literalt operator^=(const bool a)
77  {
78  // we use the least significant bit to store the sign
79  l^=(var_not)a;
80  return *this;
81  }
82 
83  var_not var_no() const
84  {
85  return l>>1;
86  }
87 
88  bool sign() const
89  {
90  return l&1;
91  }
92 
93  void set(var_not _l)
94  {
95  l=_l;
96  }
97 
98  void set(var_not v, bool sign)
99  {
100  l=(v<<1)|((var_not)sign);
101  }
102 
103  var_not get() const
104  {
105  return l;
106  }
107 
108  void invert()
109  {
110  l^=(var_not)1;
111  }
112 
113  //
114  // Returns a literal in the dimacs CNF encoding.
115  // A negative integer denotes a negated literal.
116  //
117  int dimacs() const
118  {
119  int result = narrow_cast<int>(var_no());
120 
121  if(sign())
122  result=-result;
123 
124  return result;
125  }
126 
127  void from_dimacs(int d)
128  {
129  bool sign=d<0;
130  if(sign)
131  d=-d;
132  set(narrow_cast<unsigned>(d), sign);
133  }
134 
135  void clear()
136  {
137  l=0;
138  }
139 
140  void swap(literalt &x)
141  {
142  std::swap(x.l, l);
143  }
144 
145  // constants
146  void make_true()
147  {
148  set(const_var_no(), true);
149  }
150 
151  void make_false()
152  {
153  set(const_var_no(), false);
154  }
155 
156  bool is_true() const
157  {
158  return is_constant() && sign();
159  }
160 
161  bool is_false() const
162  {
163  return is_constant() && !sign();
164  }
165 
166  bool is_constant() const
167  {
168  return var_no()==const_var_no();
169  }
170 
171  static inline var_not const_var_no()
172  {
173  return (var_not(-1)<<1)>>1;
174  }
175 
176  static inline var_not unused_var_no()
177  {
178  return (var_not(-2)<<1)>>1;
179  }
180 
181 protected:
183 };
184 
185 std::ostream &operator << (std::ostream &out, literalt l);
186 
187 // constants
188 inline literalt const_literal(bool value)
189 {
190  return literalt(literalt::const_var_no(), value);
191 }
192 
193 inline literalt neg(literalt a) { return !a; }
194 inline literalt pos(literalt a) { return a; }
195 
196 
197 inline bool is_false (const literalt &l) { return (l.is_false()); }
198 inline bool is_true (const literalt &l) { return (l.is_true()); }
199 
200 // bit-vectors
201 typedef std::vector<literalt> bvt;
202 
203 std::ostream &operator<<(std::ostream &out, const bvt &bv);
204 
205 #endif // CPROVER_SOLVERS_PROP_LITERAL_H
bool is_true() const
Definition: literal.h:156
unsigned var_not
Definition: literal.h:31
bool sign() const
Definition: literal.h:88
bool is_constant() const
Definition: literal.h:166
int dimacs() const
Definition: literal.h:117
literalt()
Definition: literal.h:34
bool operator==(const literalt other) const
Definition: literal.h:44
static var_not const_var_no()
Definition: literal.h:171
var_not get() const
Definition: literal.h:103
bool operator!=(const literalt other) const
Definition: literal.h:49
void from_dimacs(int d)
Definition: literal.h:127
var_not var_no() const
Definition: literal.h:83
void set(var_not v, bool sign)
Definition: literal.h:98
literalt operator^(const bool b) const
Definition: literal.h:61
bool operator<(const literalt other) const
Definition: literal.h:55
void set(var_not _l)
Definition: literal.h:93
literalt(var_not v, bool sign)
Definition: literal.h:39
void swap(literalt &x)
Definition: literal.h:140
literalt operator^=(const bool a)
Definition: literal.h:76
void invert()
Definition: literal.h:108
literalt operator!() const
Definition: literal.h:69
var_not l
Definition: literal.h:182
void make_false()
Definition: literal.h:151
bool is_false() const
Definition: literal.h:161
void clear()
Definition: literal.h:135
static var_not unused_var_no()
Definition: literal.h:176
void make_true()
Definition: literal.h:146
bool is_false(const literalt &l)
Definition: literal.h:197
literalt neg(literalt a)
Definition: literal.h:193
bool is_true(const literalt &l)
Definition: literal.h:198
std::ostream & operator<<(std::ostream &out, literalt l)
Definition: literal.cpp:16
std::vector< literalt > bvt
Definition: literal.h:201
literalt const_literal(bool value)
Definition: literal.h:188
literalt pos(literalt a)
Definition: literal.h:194