Finished implementing the prenex normal form with maximal groups of same-type quantifiers.

This commit is contained in:
2016-09-08 02:40:51 +02:00
parent e0ed145716
commit 34496a7158
12 changed files with 207 additions and 86 deletions

View File

@@ -73,6 +73,9 @@ class PrimitiveType;
using PrimitiveTypePointer = boost::intrusive_ptr<PrimitiveType>;
using PrimitiveTypes = std::vector<PrimitiveTypePointer>;
class Quantified;
using QuantifiedPointer = boost::intrusive_ptr<Quantified>;
template<class Type>
class Reference;
template<class Type>
@@ -120,14 +123,14 @@ class Expression
ExpressionPointer normalized();
virtual ExpressionPointer reduced();
virtual ExpressionPointer negationNormalized();
virtual ExpressionPointer prenex();
virtual ExpressionPointer prenex(Expression::Type lastQuantifierType = Expression::Type::Exists);
virtual ExpressionPointer simplified();
ExpressionPointer negated();
virtual void print(std::ostream &ostream) const = 0;
protected:
static ExpressionPointer prenex(ExpressionPointer parent, ExpressionPointer &child);
static ExpressionPointer moveUpQuantifiers(ExpressionPointer parent, ExpressionPointer &child);
private:
friend void intrusive_ptr_add_ref(Expression *expression);

View File

@@ -39,7 +39,7 @@ class At: public ExpressionCRTP<At>
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
ExpressionPointer prenex(Expression::Type lastExpressionType) override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override;

View File

@@ -34,7 +34,7 @@ class Binary: public ExpressionCRTP<Derived>
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
ExpressionPointer prenex(Expression::Type lastExpressionType) override;
void print(std::ostream &ostream) const override;
@@ -122,19 +122,10 @@ inline ExpressionPointer Binary<Derived>::negationNormalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::prenex()
inline ExpressionPointer Binary<Derived>::prenex(Expression::Type)
{
ExpressionPointer result = this;
for (size_t i = 0; i < m_arguments.size(); i++)
{
// Iterate in backward order to wrap quantifiers in forward order
auto &argument = m_arguments[m_arguments.size() - i - 1];
result = Expression::prenex(result, argument);
}
return result;
// TODO: implement by refactoring binary expressions
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -16,7 +16,7 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Exists: public Quantified<Exists>
class Exists: public QuantifiedCRTP<Exists>
{
public:
static const Expression::Type ExpressionType = Expression::Type::Exists;

View File

@@ -16,7 +16,7 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class ForAll: public Quantified<ForAll>
class ForAll: public QuantifiedCRTP<ForAll>
{
public:
static const Expression::Type ExpressionType = Expression::Type::ForAll;

View File

@@ -36,7 +36,7 @@ class NAry: public ExpressionCRTP<Derived>
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
ExpressionPointer prenex(Expression::Type lastExpressionType) override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override;
@@ -153,21 +153,85 @@ inline ExpressionPointer NAry<Derived>::negationNormalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::prenex()
inline ExpressionPointer NAry<Derived>::prenex(Expression::Type lastExpressionType)
{
ExpressionPointer result = this;
// First, move all childrens quantifiers to the front
for (size_t i = 0; i < m_arguments.size(); i++)
{
// Iterate in backward order to wrap quantifiers in forward order
auto &argument = m_arguments[m_arguments.size() - i - 1];
BOOST_ASSERT(m_arguments[i]);
BOOST_ASSERT(argument);
result = Expression::prenex(result, argument);
m_arguments[i] = m_arguments[i]->prenex(lastExpressionType);
}
return result;
// Next, move all childrens quantifiers up
const auto isQuantifier =
[](const auto &expression)
{
return expression->expressionType() == Expression::Type::Exists
|| expression->expressionType() == Expression::Type::ForAll;
};
QuantifiedPointer front = nullptr;
QuantifiedPointer back = nullptr;
const auto moveUpQuantifiers =
[&](auto &child, const auto expressionType)
{
BOOST_ASSERT(child);
bool changed = false;
while (isQuantifier(child)
&& child->expressionType() == expressionType)
{
// Decouple quantifier from tree and replace it with its child
auto expression = Expression::moveUpQuantifiers(nullptr, child);
auto quantifier = QuantifiedPointer(dynamic_cast<Quantified *>(expression.get()));
if (!front)
front = quantifier;
else
back->setArgument(quantifier);
back = quantifier;
changed = true;
}
return changed;
};
bool changed = true;
const auto otherExpressionType = (lastExpressionType == Expression::Type::Exists)
? Expression::Type::ForAll : Expression::Type::Exists;
// Group quantifiers of the same type when moving them up, starting with the parent quantifiers type
while (changed)
{
changed = false;
// Group all quantifiers of the same type as the parent quantifier
for (size_t i = 0; i < m_arguments.size(); i++)
changed = moveUpQuantifiers(m_arguments[i], lastExpressionType) || changed;
// Group all other quantifiers
for (size_t i = 0; i < m_arguments.size(); i++)
changed = moveUpQuantifiers(m_arguments[i], otherExpressionType) || changed;
}
// If quantifiers were moved up, put this node back into the node hierarchy
if (front)
{
BOOST_ASSERT(back);
back->setArgument(this);
return front;
}
// If no quantifiers were moved up, simply return this node
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -34,7 +34,7 @@ class Not: public ExpressionCRTP<Not>
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
ExpressionPointer prenex(Expression::Type lastExpressionType) override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override;

View File

@@ -19,14 +19,8 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class Quantified: public ExpressionCRTP<Derived>
class Quantified: public Expression
{
public:
template<typename ExpressionParser>
static boost::intrusive_ptr<Derived> parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
@@ -34,13 +28,6 @@ class Quantified: public ExpressionCRTP<Derived>
Variables &variables();
const Variables &variables() const;
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override;
protected:
Variables m_variables;
ExpressionPointer m_argument;
@@ -48,9 +35,33 @@ class Quantified: public ExpressionCRTP<Derived>
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class QuantifiedCRTP: public Quantified
{
public:
template<typename ExpressionParser>
static boost::intrusive_ptr<Derived> parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
Type expressionType() const override final
{
return Derived::ExpressionType;
}
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex(Expression::Type lastExpressionType) override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
template<typename ExpressionParser>
boost::intrusive_ptr<Derived> Quantified<Derived>::parse(Context &context,
boost::intrusive_ptr<Derived> QuantifiedCRTP<Derived>::parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression)
{
auto &parser = context.parser;
@@ -75,7 +86,7 @@ boost::intrusive_ptr<Derived> Quantified<Derived>::parse(Context &context,
expressionContext.variables.push(&expression->m_variables);
// Parse argument of the expression
expression->Quantified<Derived>::setArgument(parseExpression(context, expressionContext));
expression->Quantified::setArgument(parseExpression(context, expressionContext));
// Clean up variable stack
expressionContext.variables.pop();
@@ -87,24 +98,28 @@ boost::intrusive_ptr<Derived> Quantified<Derived>::parse(Context &context,
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void Quantified<Derived>::setArgument(ExpressionPointer expression)
inline void Quantified::setArgument(ExpressionPointer expression)
{
m_argument = expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
ExpressionPointer Quantified<Derived>::argument() const
inline ExpressionPointer Quantified::argument() const
{
return m_argument;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
Variables &Quantified<Derived>::variables()
inline Variables &Quantified::variables()
{
return m_variables;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline const Variables &Quantified::variables() const
{
return m_variables;
}
@@ -112,19 +127,24 @@ Variables &Quantified<Derived>::variables()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
const Variables &Quantified<Derived>::variables() const
inline ExpressionPointer QuantifiedCRTP<Derived>::reduced()
{
return m_variables;
BOOST_ASSERT(m_argument);
m_argument = m_argument->reduced();
// Child quantifiers may not move before this quantifier, the order matters
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::reduced()
inline ExpressionPointer QuantifiedCRTP<Derived>::negationNormalized()
{
BOOST_ASSERT(m_argument);
m_argument = m_argument->prenex();
m_argument = m_argument->negationNormalized();
return this;
}
@@ -132,32 +152,20 @@ inline ExpressionPointer Quantified<Derived>::reduced()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::negationNormalized()
inline ExpressionPointer QuantifiedCRTP<Derived>::prenex(Expression::Type)
{
BOOST_ASSERT(m_argument);
m_argument = m_argument->prenex();
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::prenex()
{
BOOST_ASSERT(m_argument);
m_argument = m_argument->prenex(Derived::ExpressionType);
// Quantifiers may not move before other quantifiers, their order matters
m_argument = m_argument->prenex();
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::simplified()
inline ExpressionPointer QuantifiedCRTP<Derived>::simplified()
{
BOOST_ASSERT(m_argument);
@@ -183,7 +191,7 @@ inline ExpressionPointer Quantified<Derived>::simplified()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline void Quantified<Derived>::print(std::ostream &ostream) const
inline void QuantifiedCRTP<Derived>::print(std::ostream &ostream) const
{
ostream << "(" << Derived::Identifier << " (";