CBMC
cnf.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: CNF Generation, via Tseitin
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_SOLVERS_SAT_CNF_H
13 #define CPROVER_SOLVERS_SAT_CNF_H
14 
15 #include <solvers/prop/prop.h>
16 
17 class cnft:public propt
18 {
19 public:
20  // For CNF, we don't use index 0 as a matter of principle,
21  // so we'll start counting variables at 1.
22  explicit cnft(message_handlert &message_handler)
23  : propt(message_handler), _no_variables(1)
24  {
25  }
26  virtual ~cnft() { }
27 
28  virtual literalt land(literalt a, literalt b) override;
29  virtual literalt lor(literalt a, literalt b) override;
30  virtual literalt land(const bvt &bv) override;
31  virtual literalt lor(const bvt &bv) override;
32  virtual literalt lxor(const bvt &bv) override;
33  virtual literalt lxor(literalt a, literalt b) override;
34  virtual literalt lnand(literalt a, literalt b) override;
35  virtual literalt lnor(literalt a, literalt b) override;
36  virtual literalt lequal(literalt a, literalt b) override;
37  virtual literalt limplies(literalt a, literalt b) override;
38  // a?b:c
39  virtual literalt lselect(literalt a, literalt b, literalt c) override;
40  virtual literalt new_variable() override;
41  bvt new_variables(std::size_t width) override;
42  virtual size_t no_variables() const override { return _no_variables; }
43  virtual void set_no_variables(size_t no) { _no_variables=no; }
44  virtual size_t no_clauses() const=0;
45 
46 protected:
47  void gate_and(literalt a, literalt b, literalt o);
48  void gate_or(literalt a, literalt b, literalt o);
49  void gate_xor(literalt a, literalt b, literalt o);
50  void gate_nand(literalt a, literalt b, literalt o);
51  void gate_nor(literalt a, literalt b, literalt o);
52  void gate_equal(literalt a, literalt b, literalt o);
53  void gate_implies(literalt a, literalt b, literalt o);
54 
55  static bvt eliminate_duplicates(const bvt &);
56 
57  size_t _no_variables;
58 
59  bool process_clause(const bvt &bv, bvt &dest) const;
60 
61  static bool is_all(const bvt &bv, literalt l)
62  {
63  for(const auto &literal : bv)
64  {
65  if(literal != l)
66  return false;
67  }
68  return true;
69  }
70 };
71 
72 class cnf_solvert:public cnft
73 {
74 public:
75  explicit cnf_solvert(message_handlert &message_handler)
76  : cnft(message_handler), status(statust::INIT), clause_counter(0)
77  {
78  }
79 
80  virtual size_t no_clauses() const override
81  {
82  return clause_counter;
83  }
84 
85 protected:
86  enum class statust { INIT, SAT, UNSAT, ERROR };
89 };
90 
91 #endif // CPROVER_SOLVERS_SAT_CNF_H
statust status
Definition: cnf.h:87
cnf_solvert(message_handlert &message_handler)
Definition: cnf.h:75
virtual size_t no_clauses() const override
Definition: cnf.h:80
statust
Definition: cnf.h:86
size_t clause_counter
Definition: cnf.h:88
Definition: cnf.h:18
static bool is_all(const bvt &bv, literalt l)
Definition: cnf.h:61
void gate_equal(literalt a, literalt b, literalt o)
Tseitin encoding of equality between two literals.
Definition: cnf.cpp:143
bool process_clause(const bvt &bv, bvt &dest) const
filter 'true' from clause, eliminate duplicates, recognise trivially satisfied clauses
Definition: cnf.cpp:424
bvt new_variables(std::size_t width) override
Generate a vector of new variables.
Definition: cnf.cpp:396
void gate_or(literalt a, literalt b, literalt o)
Tseitin encoding of disjunction of two literals.
Definition: cnf.cpp:46
virtual ~cnft()
Definition: cnf.h:26
virtual literalt limplies(literalt a, literalt b) override
Definition: cnf.cpp:334
virtual literalt lselect(literalt a, literalt b, literalt c) override
Definition: cnf.cpp:344
virtual void set_no_variables(size_t no)
Definition: cnf.h:43
static bvt eliminate_duplicates(const bvt &)
eliminate duplicates from given vector of literals
Definition: cnf.cpp:412
void gate_nand(literalt a, literalt b, literalt o)
Tseitin encoding of NAND of two literals.
Definition: cnf.cpp:99
virtual literalt new_variable() override
Generate a new variable and return it as a literal.
Definition: cnf.cpp:385
virtual size_t no_variables() const override
Definition: cnf.h:42
void gate_xor(literalt a, literalt b, literalt o)
Tseitin encoding of XOR of two literals.
Definition: cnf.cpp:68
void gate_nor(literalt a, literalt b, literalt o)
Tseitin encoding of NOR of two literals.
Definition: cnf.cpp:121
size_t _no_variables
Definition: cnf.h:57
cnft(message_handlert &message_handler)
Definition: cnf.h:22
virtual literalt lequal(literalt a, literalt b) override
Definition: cnf.cpp:329
virtual literalt land(literalt a, literalt b) override
Definition: cnf.cpp:263
void gate_implies(literalt a, literalt b, literalt o)
Tseitin encoding of implication between two literals.
Definition: cnf.cpp:150
virtual literalt lxor(const bvt &bv) override
Tseitin encoding of XOR between multiple literals.
Definition: cnf.cpp:244
void gate_and(literalt a, literalt b, literalt o)
Tseitin encoding of conjunction of two literals.
Definition: cnf.cpp:23
virtual literalt lnand(literalt a, literalt b) override
Definition: cnf.cpp:317
virtual size_t no_clauses() const =0
virtual literalt lnor(literalt a, literalt b) override
Definition: cnf.cpp:324
virtual literalt lor(literalt a, literalt b) override
Definition: cnf.cpp:279
TO_BE_DOCUMENTED.
Definition: prop.h:25
std::vector< literalt > bvt
Definition: literal.h:201