Implemented prenex normalization.

This commit is contained in:
2016-09-07 00:34:26 +02:00
parent 2e52357dd2
commit e0ed145716
10 changed files with 203 additions and 9 deletions

View File

@@ -120,11 +120,15 @@ class Expression
ExpressionPointer normalized();
virtual ExpressionPointer reduced();
virtual ExpressionPointer negationNormalized();
virtual ExpressionPointer prenex();
virtual ExpressionPointer simplified();
ExpressionPointer negated();
virtual void print(std::ostream &ostream) const = 0;
protected:
static ExpressionPointer prenex(ExpressionPointer parent, ExpressionPointer &child);
private:
friend void intrusive_ptr_add_ref(Expression *expression);
friend void intrusive_ptr_release(Expression *expression);

View File

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

View File

@@ -4,6 +4,9 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/expressions/Exists.h>
#include <plasp/pddl/expressions/ForAll.h>
namespace plasp
{
namespace pddl
@@ -31,6 +34,7 @@ class Binary: public ExpressionCRTP<Derived>
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
void print(std::ostream &ostream) const override;
@@ -117,6 +121,24 @@ inline ExpressionPointer Binary<Derived>::negationNormalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::prenex()
{
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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline void Binary<Derived>::print(std::ostream &ostream) const
{

View File

@@ -4,6 +4,9 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/expressions/Exists.h>
#include <plasp/pddl/expressions/ForAll.h>
namespace plasp
{
namespace pddl
@@ -33,6 +36,7 @@ class NAry: public ExpressionCRTP<Derived>
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override;
@@ -148,6 +152,26 @@ inline ExpressionPointer NAry<Derived>::negationNormalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::prenex()
{
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];
BOOST_ASSERT(argument);
result = Expression::prenex(result, argument);
}
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::simplified()
{

View File

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

View File

@@ -36,6 +36,7 @@ class Quantified: public ExpressionCRTP<Derived>
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
ExpressionPointer prenex() override;
ExpressionPointer simplified() override;
void print(std::ostream &ostream) const override;
@@ -121,7 +122,9 @@ const Variables &Quantified<Derived>::variables() const
template<class Derived>
inline ExpressionPointer Quantified<Derived>::reduced()
{
m_argument = m_argument->reduced();
BOOST_ASSERT(m_argument);
m_argument = m_argument->prenex();
return this;
}
@@ -131,7 +134,22 @@ inline ExpressionPointer Quantified<Derived>::reduced()
template<class Derived>
inline ExpressionPointer Quantified<Derived>::negationNormalized()
{
m_argument = m_argument->negationNormalized();
BOOST_ASSERT(m_argument);
m_argument = m_argument->prenex();
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::prenex()
{
BOOST_ASSERT(m_argument);
// Quantifiers may not move before other quantifiers, their order matters
m_argument = m_argument->prenex();
return this;
}
@@ -141,6 +159,8 @@ inline ExpressionPointer Quantified<Derived>::negationNormalized()
template<class Derived>
inline ExpressionPointer Quantified<Derived>::simplified()
{
BOOST_ASSERT(m_argument);
m_argument = m_argument->simplified();
// Associate same-type children, such as (forall (?x) (forall (?y) (...)))