Support placeholders with #external declarations
This adds support for declaring predicates as placeholders through the “#external” directive in the input language of clingo. Placeholders are not subject to completion. This prevents predicates that represent instance-specific facts from being assumed as universally false by default negation when translating an encoding. This stretches clingo’s usual syntax a bit to make the implementation lightweight. In order to declare a predicate with a specific arity as a placeholder, the following statement needs to be added to the program: #external <predicate name>(<arity>). Multiple unit tests cover cases where placeholders are used or not as well as a more complex graph coloring example.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <anthem/AST.h>
|
||||
#include <anthem/ASTVisitors.h>
|
||||
#include <anthem/Context.h>
|
||||
|
||||
namespace anthem
|
||||
{
|
||||
@@ -40,7 +41,7 @@ class VariableStack
|
||||
bool matches(const Predicate &lhs, const Predicate &rhs);
|
||||
bool matches(const Predicate &predicate, const PredicateSignature &signature);
|
||||
bool matches(const PredicateSignature &lhs, const PredicateSignature &rhs);
|
||||
void collectPredicateSignatures(const Formula &formula, std::vector<PredicateSignature> &predicateSignatures);
|
||||
void collectPredicateSignatures(const Formula &formula, std::vector<PredicateSignature> &predicateSignatures, Context &context);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Replacing Variables
|
||||
|
@@ -16,6 +16,14 @@ namespace anthem
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct PredicateSignatureMeta
|
||||
{
|
||||
ast::PredicateSignature predicateSignature;
|
||||
bool used{false};
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Context
|
||||
{
|
||||
Context() = default;
|
||||
@@ -30,7 +38,8 @@ struct Context
|
||||
bool performSimplification = false;
|
||||
bool performCompletion = false;
|
||||
|
||||
std::optional<std::vector<ast::PredicateSignature>> visiblePredicateSignatures;
|
||||
std::optional<std::vector<PredicateSignatureMeta>> visiblePredicateSignatures;
|
||||
std::optional<std::vector<PredicateSignatureMeta>> externalPredicateSignatures;
|
||||
|
||||
ast::ParenthesisStyle parenthesisStyle = ast::ParenthesisStyle::Normal;
|
||||
};
|
||||
|
@@ -176,7 +176,8 @@ struct StatementVisitor
|
||||
|
||||
context.logger.log(output::Priority::Debug, statement.location) << "showing “" << signature.name() << "/" << signature.arity() << "”";
|
||||
|
||||
context.visiblePredicateSignatures.value().emplace_back(std::string(signature.name()), signature.arity());
|
||||
auto predicateSignature = ast::PredicateSignature{std::string(signature.name()), signature.arity()};
|
||||
context.visiblePredicateSignatures.value().emplace_back(PredicateSignatureMeta{std::move(predicateSignature)});
|
||||
}
|
||||
|
||||
void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement, std::vector<ast::ScopedFormula> &, Context &)
|
||||
@@ -184,6 +185,44 @@ struct StatementVisitor
|
||||
throw LogicException(statement.location, "only #show statements for atoms (not terms) are supported currently");
|
||||
}
|
||||
|
||||
void visit(const Clingo::AST::External &external, const Clingo::AST::Statement &statement, std::vector<ast::ScopedFormula> &, Context &context)
|
||||
{
|
||||
const auto fail =
|
||||
[&]()
|
||||
{
|
||||
throw LogicException(statement.location, "only #external declarations of the form “#external <predicate name>(<arity>).” supported");
|
||||
};
|
||||
|
||||
if (!external.body.empty())
|
||||
fail();
|
||||
|
||||
if (!external.atom.data.is<Clingo::AST::Function>())
|
||||
fail();
|
||||
|
||||
const auto &predicate = external.atom.data.get<Clingo::AST::Function>();
|
||||
|
||||
if (predicate.arguments.size() != 1)
|
||||
fail();
|
||||
|
||||
const auto &arityArgument = predicate.arguments.front();
|
||||
|
||||
if (!arityArgument.data.is<Clingo::Symbol>())
|
||||
fail();
|
||||
|
||||
const auto &aritySymbol = arityArgument.data.get<Clingo::Symbol>();
|
||||
|
||||
if (aritySymbol.type() != Clingo::SymbolType::Number)
|
||||
fail();
|
||||
|
||||
const size_t arity = arityArgument.data.get<Clingo::Symbol>().number();
|
||||
|
||||
if (!context.externalPredicateSignatures)
|
||||
context.externalPredicateSignatures.emplace();
|
||||
|
||||
auto predicateSignature = ast::PredicateSignature{std::string(predicate.name), arity};
|
||||
context.externalPredicateSignatures->emplace_back(PredicateSignatureMeta{std::move(predicateSignature)});
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void visit(const T &, const Clingo::AST::Statement &statement, std::vector<ast::ScopedFormula> &, Context &)
|
||||
{
|
||||
|
Reference in New Issue
Block a user