forked from rishig34/DPTrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
davis_putnam.h
145 lines (116 loc) · 4.32 KB
/
davis_putnam.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef davis_putnam_h_
#define davis_putnam_h_
#include <string>
#include <vector>
#include <list>
#include <map>
typedef unsigned int uint; //Hopefully this fixes the compilation errors
// Objects for setting and keeping track of truth values for atomic statements.
class Atomic {
public:
Atomic();
Atomic(const std::string& n) : name(n) {}
// Accessors
bool getValue() const;
int getQuantity() const { return quantity; }
const std::string& getName() const { return name; }
// Modifiers
void setValue(bool v);
void unsetValue() { set_val = false; }
void resetQuantity() { quantity = 0; }
void operator++() { ++quantity; } // Increases for every occurance of atomic.
private:
// Representation
std::string name;
int quantity = 0; // Number of occurances in all input statements.
bool val, set_val=false; // Truth value and whether truth value has been set.
};
class FullStatement;
class ClauseSet;
// Node objects for storing binary and negation operators as well as relevant literals.
class Statement {
public:
friend class FullStatement;
friend class ClauseSet;
private:
Statement() {}
Statement(const std::string& raw_stat, std::map<std::string,Atomic*>& atomics, std::string& orig);
bool containsAtomic(const std::string& a) const;
// Construction/destruction helper functions.
Statement* copy() const;
void destroy();
// Helper functions for solving.
void mergeAtomics(const std::map<std::string, Atomic*>& atoms2);
Statement* evaluate();
void reassign(Statement& s2);
void simplify(Statement* keep);
// Helper functions for CNF conversion.
void elimConditional();
Statement* elimBiconditional();
void DeMorgan();
void DistribDisjunct(bool nested_left);
// Representation
char op_sym = ' '; // Binary operator symbol, space is used for atomic statements.
bool negated = false; // Presence of negation operator
bool val, set_val = false; // Truth value and whether truth value has been set.
Statement* parent_ = NULL;
Statement* left_ = NULL;
Statement* right_ = NULL;
std::map<std::string, Atomic*> s_atomics; // All literals beneath operator.
};
/* Top-level object for holding contained Statement objects. Uses tree structure to
represent logical statements with multiple binary operators. */
class FullStatement {
public:
FullStatement(const std::string& raw_stat, std::map<std::string,Atomic*>& atomics);
FullStatement(const FullStatement& fs);
~FullStatement() { root_->destroy(); }
// Accessors
const std::string& getOrig() const { return orig; }
Statement* getRoot() const { return root_; }
bool containsAtomic(const std::string& a) const;
bool getValSet() const { return set_val; }
bool getVal() const { return val; }
// Main solving functions
void evaluate(const std::string& curr_a);
void rewrite();
void convertCNF();
private:
std::string rewrite(Statement* s) const;
void convertCNF(Statement* s);
// Representation
Statement* root_ = NULL;
bool val, set_val = false; // Truth value and whether truth value has been set.
std::string orig; // Written logical expression.
std::map<std::string, Atomic*>* atomics_; // All literals used in full statment.
};
typedef std::pair<std::string, bool> Literal;
typedef std::map<Literal, Atomic*> Clause;
// Alternate method for storing and solving logical arguments using CNF and clause conversion.
class ClauseSet {
public:
typedef std::list<Clause>::iterator iterator;
typedef std::list<Clause>::const_iterator const_iterator;
ClauseSet(std::list<FullStatement>& premises);
bool evaluate(const Literal& lit, uint index);
// Accessors
iterator getSmallest();
const std::vector<std::string>& getOutput() const { return output_tree; }
std::pair<bool,bool> emptyClause() const;
private:
// Shortcut modifiers
bool elimTaut();
bool elimSub();
bool elimPure();
// Output writing functions
void write(const std::string& curr_atom, uint index);
void writeElim(std::string& elim) const;
// Representation
std::list<Clause> clauses;
std::map<std::string, Atomic*>* atomics_; // All literals used in clauses.
std::vector<std::string> output_tree; // Text for tree graphic encoding.
};
void redundancy(std::string& stat);
Literal negate(const Literal& lit);
bool sortClause(const Clause& c1, const Clause& c2);
#endif