-
Notifications
You must be signed in to change notification settings - Fork 0
/
formula.hpp
126 lines (107 loc) · 3.63 KB
/
formula.hpp
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
#ifndef __FORMULA_HPP__
#define __FORMULA_HPP__ 1
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class BaseFormula; // abstract class for representation of formulas
typedef shared_ptr<BaseFormula> Formula; // data type for representation of formulas
// Formula types
enum FormulaType { T_TRUE, T_FALSE, T_ATOM, T_NOT, T_AND, T_OR, T_IMP, T_IFF };
// Variable data type
typedef string Variable;
class BaseFormula : public enable_shared_from_this<BaseFormula> {
public:
virtual FormulaType getType() const = 0; // get formula type
virtual void printFormula(ostream & ostr) const = 0; // print formula to an output stream
virtual bool equalTo(const Formula & f) const = 0; // check syntax equality of two formulas
virtual Formula simplify(); // simplifying for equivalence
virtual ~BaseFormula() {}
};
ostream & operator << (ostream & ostr, const Formula & f);
// Class for representation of logic constants (True, False)
class LogicConstant : public BaseFormula {
public:
virtual bool equalTo(const Formula & f) const;
};
// Class for representation of True logic constant
class True : public LogicConstant {
public:
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
};
// Class for representation of False logic constant
class False : public LogicConstant {
public:
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
};
// Class for representation of atoms in propositional logic
class Atom : public BaseFormula {
private:
Variable _v;
public:
Atom(const Variable & v);
const Variable & getVariable() const;
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
virtual bool equalTo(const Formula & f) const;
};
// Class for representation of unary connectives (includes Negation)
class UnaryConnective : public BaseFormula {
protected:
Formula _op;
public:
UnaryConnective(const Formula & op);
const Formula & getOperand() const;
virtual bool equalTo(const Formula & f) const;
};
// Class for representation of Negation formulas
class Not : public UnaryConnective {
public:
using UnaryConnective::UnaryConnective;
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
};
// Class for representation of binary connectives
// (includes Conjunction, Disjunction, Implication, Equivalence)
class BinaryConnective : public BaseFormula {
protected:
Formula _op1, _op2;
public:
BinaryConnective(const Formula & op1, const Formula & op2);
const Formula & getOperand1() const;
const Formula & getOperand2() const;
virtual bool equalTo(const Formula & f) const;
};
// Class for representation of Conjunction formulas
class And : public BinaryConnective {
public:
using BinaryConnective::BinaryConnective;
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
};
// Class for representation of Disjunction formulas
class Or : public BinaryConnective {
public:
using BinaryConnective::BinaryConnective;
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
};
// Class for representation of Implication formulas
class Imp : public BinaryConnective {
public:
using BinaryConnective::BinaryConnective;
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
};
// Class for representation of Equivalence formulas
class Iff : public BinaryConnective {
public:
using BinaryConnective::BinaryConnective;
virtual FormulaType getType() const;
virtual void printFormula(ostream & ostr) const;
// (A => B) /\ (B => A) instead of A <=> B
virtual Formula simplify();
};
#endif