Added separate simplification step to normalize in a single recursion.

This commit is contained in:
2016-09-04 23:30:08 +02:00
parent 97ad4268d7
commit 4d72c20d9b
11 changed files with 128 additions and 16 deletions

View File

@@ -109,7 +109,9 @@ class Expression
virtual Type expressionType() const = 0;
virtual ExpressionPointer normalized();
ExpressionPointer normalized();
virtual ExpressionPointer simplified();
virtual ExpressionPointer negationNormalized();
ExpressionPointer negated();
virtual void print(std::ostream &ostream) const = 0;

View File

@@ -37,7 +37,8 @@ class At: public ExpressionCRTP<At>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;

View File

@@ -31,7 +31,8 @@ class Binary: public ExpressionCRTP<Derived>
void setArgument(size_t i, ExpressionPointer argument);
const std::array<ExpressionPointer, 2> &arguments() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;
@@ -90,7 +91,22 @@ const std::array<ExpressionPointer, 2> &Binary<Derived>::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::normalized()
inline ExpressionPointer Binary<Derived>::simplified()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
m_arguments[i] = m_arguments[i]->simplified();
}
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::negationNormalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{

View File

@@ -24,7 +24,7 @@ class Imply: public Binary<Imply>
static const std::string Identifier;
public:
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -33,7 +33,8 @@ class NAry: public ExpressionCRTP<Derived>
Expressions &arguments();
const Expressions &arguments() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;
@@ -120,13 +121,13 @@ Expressions &NAry<Derived>::arguments()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::normalized()
inline ExpressionPointer NAry<Derived>::simplified()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
m_arguments[i] = m_arguments[i]->normalized();
m_arguments[i] = m_arguments[i]->simplified();
}
return this;
@@ -134,6 +135,35 @@ inline ExpressionPointer NAry<Derived>::normalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::negationNormalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
m_arguments[i] = m_arguments[i]->negationNormalized();
}
/*// Unify same-type children
for (size_t i = 0; i < m_arguments.size(); i++)
{
if (m_arguments[i]->expressionType() != Derived::ExpressionType)
continue;
auto &nAryExpression = dynamic_cast<Derived &>(*m_arguments[i]);
m_arguments.reserve(m_arguments.size() + nAryExpression.arguments().size());
m_arguments.emplace_back(nAryExpression.arguments());
m_arguments.erase(const_iterator __position)
}*/
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline void NAry<Derived>::print(std::ostream &ostream) const
{

View File

@@ -32,7 +32,8 @@ class Not: public ExpressionCRTP<Not>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;