CBMC
qdimacs_cnf.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "qdimacs_cnf.h"
10 
11 #include <iostream>
12 
13 #include <util/invariant.h>
14 
15 void qdimacs_cnft::write_qdimacs_cnf(std::ostream &out)
16 {
17  write_problem_line(out);
18  write_prefix(out);
19  write_clauses(out);
20 }
21 
22 void qdimacs_cnft::write_prefix(std::ostream &out) const
23 {
24  std::vector<bool> quantified;
25 
26  quantified.resize(no_variables(), false);
27 
28  for(quantifierst::const_iterator
29  it=quantifiers.begin();
30  it!=quantifiers.end();
31  it++)
32  {
33  const quantifiert &quantifier=*it;
34 
36  quantifier.var_no < no_variables(),
37  "unknown variable: ",
38  std::to_string(quantifier.var_no));
39  // double quantification?
40  INVARIANT(!quantified[quantifier.var_no], "no double quantification");
41  quantified[quantifier.var_no]=true;
42 
43  switch(quantifier.type)
44  {
46  out << "a";
47  break;
48 
50  out << "e";
51  break;
52 
55  }
56 
57  out << " " << quantifier.var_no << " 0\n";
58  }
59 
60  // variables that are not quantified
61  // will be quantified existentially in the innermost scope
62 
63  for(std::size_t i=1; i<no_variables(); i++)
64  if(!quantified[i])
65  out << "e " << i << " 0\n";
66 }
67 
68 bool qdimacs_cnft::operator==(const qdimacs_cnft &other) const
69 {
70  return quantifiers==other.quantifiers && clauses==other.clauses;
71 }
72 
74  const quantifiert::typet type,
75  const literalt l)
76 {
77  for(quantifierst::iterator it=quantifiers.begin();
78  it!=quantifiers.end();
79  it++)
80  if(it->var_no==l.var_no())
81  {
82  it->type=type;
83  return;
84  }
85 
86  // variable not found - let's add a new quantifier.
87  add_quantifier(type, l);
88 }
89 
91 {
93 
94  for(quantifierst::const_iterator
95  it=quantifiers.begin();
96  it!=quantifiers.end();
97  it++)
98  cnf.add_quantifier(*it);
99 
100  for(clausest::const_iterator
101  it=clauses.begin();
102  it!=clauses.end();
103  it++)
104  cnf.lcnf(*it);
105 }
106 
107 size_t qdimacs_cnft::hash() const
108 {
109  size_t result=0;
110 
111  for(quantifierst::const_iterator it=quantifiers.begin();
112  it!=quantifiers.end();
113  it++)
114  result=((result<<1)^it->hash())-result;
115 
116  return result^cnf_clause_listt::hash();
117 }
118 
120 {
121  for(quantifierst::const_iterator it=quantifiers.begin();
122  it!=quantifiers.end();
123  it++)
124  if(it->var_no==l.var_no())
125  return true;
126 
127  return false;
128 }
129 
131 {
132  for(quantifierst::const_iterator it=quantifiers.begin();
133  it!=quantifiers.end();
134  it++)
135  if(it->var_no==l.var_no())
136  {
137  q=*it;
138  return true;
139  }
140 
141  return false;
142 }
size_t hash() const
void lcnf(const bvt &bv) override
virtual void set_no_variables(size_t no)
Definition: cnf.h:43
virtual size_t no_variables() const override
Definition: cnf.h:42
size_t _no_variables
Definition: cnf.h:57
void write_clauses(std::ostream &out) const
Definition: dimacs_cnf.cpp:79
void write_problem_line(std::ostream &out) const
Definition: dimacs_cnf.cpp:46
var_not var_no() const
Definition: literal.h:83
virtual void add_quantifier(const quantifiert &quantifier)
Definition: qdimacs_cnf.h:64
bool find_quantifier(const literalt l, quantifiert &q) const
void write_prefix(std::ostream &out) const
Definition: qdimacs_cnf.cpp:22
virtual void write_qdimacs_cnf(std::ostream &out)
Definition: qdimacs_cnf.cpp:15
size_t hash() const
virtual void set_quantifier(const quantifiert::typet type, const literalt l)
Definition: qdimacs_cnf.cpp:73
bool operator==(const qdimacs_cnft &other) const
Definition: qdimacs_cnf.cpp:68
quantifierst quantifiers
Definition: qdimacs_cnf.h:62
void copy_to(qdimacs_cnft &cnf) const
Definition: qdimacs_cnf.cpp:90
bool is_quantified(const literalt l) const
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
#define INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Same as invariant, with one or more diagnostics attached Diagnostics can be of any type that has a sp...
Definition: invariant.h:437
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.