parser, minisat, other things..
[cvc5.git] / src / sat / minisat / core / SolverTypes.h
1 /***********************************************************************************[SolverTypes.h]
2 MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 associated documentation files (the "Software"), to deal in the Software without restriction,
6 including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all copies or
11 substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 **************************************************************************************************/
19
20 #ifndef CVC4_MiniSat_SolverTypes_h
21 #define CVC4_MiniSat_SolverTypes_h
22
23 #include <cassert>
24 #include <stdint.h>
25
26 namespace CVC4 {
27 namespace MiniSat {
28
29 //=================================================================================================
30 // Variables, literals, lifted booleans, clauses:
31
32
33 // NOTE! Variables are just integers. No abstraction here. They should be chosen from 0..N,
34 // so that they can be used as array indices.
35
36 typedef int Var;
37 #define var_Undef (-1)
38
39
40 class Lit {
41 int x;
42 public:
43 Lit() : x(2*var_Undef) { } // (lit_Undef)
44 explicit Lit(Var var, bool sign = false) : x((var+var) + (int)sign) { }
45
46 // Don't use these for constructing/deconstructing literals. Use the normal constructors instead.
47 friend int toInt (Lit p); // Guarantees small, positive integers suitable for array indexing.
48 friend Lit toLit (int i); // Inverse of 'toInt()'
49 friend Lit operator ~(Lit p);
50 friend bool sign (Lit p);
51 friend int var (Lit p);
52 friend Lit unsign (Lit p);
53 friend Lit id (Lit p, bool sgn);
54
55 bool operator == (Lit p) const { return x == p.x; }
56 bool operator != (Lit p) const { return x != p.x; }
57 bool operator < (Lit p) const { return x < p.x; } // '<' guarantees that p, ~p are adjacent in the ordering.
58 };
59
60 inline int toInt (Lit p) { return p.x; }
61 inline Lit toLit (int i) { Lit p; p.x = i; return p; }
62 inline Lit operator ~(Lit p) { Lit q; q.x = p.x ^ 1; return q; }
63 inline bool sign (Lit p) { return p.x & 1; }
64 inline int var (Lit p) { return p.x >> 1; }
65 inline Lit unsign (Lit p) { Lit q; q.x = p.x & ~1; return q; }
66 inline Lit id (Lit p, bool sgn) { Lit q; q.x = p.x ^ (int)sgn; return q; }
67
68 const Lit lit_Undef(var_Undef, false); // }- Useful special constants.
69 const Lit lit_Error(var_Undef, true ); // }
70
71
72 //=================================================================================================
73 // Lifted booleans:
74
75
76 class lbool {
77 char value;
78 explicit lbool(int v) : value(v) { }
79
80 public:
81 lbool() : value(0) { }
82 lbool(bool x) : value((int)x*2-1) { }
83 int toInt(void) const { return value; }
84
85 bool operator == (lbool b) const { return value == b.value; }
86 bool operator != (lbool b) const { return value != b.value; }
87 lbool operator ^ (bool b) const { return b ? lbool(-value) : lbool(value); }
88
89 friend int toInt (lbool l);
90 friend lbool toLbool(int v);
91 };
92 inline int toInt (lbool l) { return l.toInt(); }
93 inline lbool toLbool(int v) { return lbool(v); }
94
95 const lbool l_True = toLbool( 1);
96 const lbool l_False = toLbool(-1);
97 const lbool l_Undef = toLbool( 0);
98
99 //=================================================================================================
100 // Clause -- a simple class for representing a clause:
101
102
103 class Clause {
104 uint32_t size_etc;
105 union { float act; uint32_t abst; } extra;
106 Lit data[0];
107
108 public:
109 void calcAbstraction() {
110 uint32_t abstraction = 0;
111 for (int i = 0; i < size(); i++)
112 abstraction |= 1 << (var(data[i]) & 31);
113 extra.abst = abstraction; }
114
115 // NOTE: This constructor cannot be used directly (doesn't allocate enough memory).
116 template<class V>
117 Clause(const V& ps, bool learnt) {
118 size_etc = (ps.size() << 3) | (uint32_t)learnt;
119 for (int i = 0; i < ps.size(); i++) data[i] = ps[i];
120 if (learnt) extra.act = 0; else calcAbstraction(); }
121
122 // -- use this function instead:
123 template<class V>
124 friend Clause* Clause_new(const V& ps, bool learnt = false) {
125 assert(sizeof(Lit) == sizeof(uint32_t));
126 assert(sizeof(float) == sizeof(uint32_t));
127 void* mem = malloc(sizeof(Clause) + sizeof(uint32_t)*(ps.size()));
128 return new (mem) Clause(ps, learnt); }
129
130 int size () const { return size_etc >> 3; }
131 void shrink (int i) { assert(i <= size()); size_etc = (((size_etc >> 3) - i) << 3) | (size_etc & 7); }
132 void pop () { shrink(1); }
133 bool learnt () const { return size_etc & 1; }
134 uint32_t mark () const { return (size_etc >> 1) & 3; }
135 void mark (uint32_t m) { size_etc = (size_etc & ~6) | ((m & 3) << 1); }
136 const Lit& last () const { return data[size()-1]; }
137
138 // NOTE: somewhat unsafe to change the clause in-place! Must manually call 'calcAbstraction' afterwards for
139 // subsumption operations to behave correctly.
140 Lit& operator [] (int i) { return data[i]; }
141 Lit operator [] (int i) const { return data[i]; }
142 operator const Lit* (void) const { return data; }
143
144 float& activity () { return extra.act; }
145 uint32_t abstraction () const { return extra.abst; }
146
147 Lit subsumes (const Clause& other) const;
148 void strengthen (Lit p);
149 };
150
151
152 /*_________________________________________________________________________________________________
153 |
154 | subsumes : (other : const Clause&) -> Lit
155 |
156 | Description:
157 | Checks if clause subsumes 'other', and at the same time, if it can be used to simplify 'other'
158 | by subsumption resolution.
159 |
160 | Result:
161 | lit_Error - No subsumption or simplification
162 | lit_Undef - Clause subsumes 'other'
163 | p - The literal p can be deleted from 'other'
164 |________________________________________________________________________________________________@*/
165 inline Lit Clause::subsumes(const Clause& other) const
166 {
167 if (other.size() < size() || (extra.abst & ~other.extra.abst) != 0)
168 return lit_Error;
169
170 Lit ret = lit_Undef;
171 const Lit* c = (const Lit*)(*this);
172 const Lit* d = (const Lit*)other;
173
174 for (int i = 0; i < size(); i++) {
175 // search for c[i] or ~c[i]
176 for (int j = 0; j < other.size(); j++)
177 if (c[i] == d[j])
178 goto ok;
179 else if (ret == lit_Undef && c[i] == ~d[j]){
180 ret = c[i];
181 goto ok;
182 }
183
184 // did not find it
185 return lit_Error;
186 ok:;
187 }
188
189 return ret;
190 }
191
192
193 inline void Clause::strengthen(Lit p)
194 {
195 remove(*this, p);
196 calcAbstraction();
197 }
198
199 }/* CVC4::MiniSat namespace */
200 }/* CVC4 namespace */
201
202 #endif /* CVC4_MiniSat_SolverTypes_h */