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:
62
tests/TestPlaceholders.cpp
Normal file
62
tests/TestPlaceholders.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <anthem/AST.h>
|
||||
#include <anthem/Context.h>
|
||||
#include <anthem/Translation.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_CASE("[placeholders] Programs with placeholders are correctly completed", "[placeholders]")
|
||||
{
|
||||
std::stringstream input;
|
||||
std::stringstream output;
|
||||
std::stringstream errors;
|
||||
|
||||
anthem::output::Logger logger(output, errors);
|
||||
anthem::Context context(std::move(logger));
|
||||
context.performSimplification = true;
|
||||
context.performCompletion = true;
|
||||
|
||||
SECTION("no placeholders")
|
||||
{
|
||||
input <<
|
||||
"colored(V, red) :- vertex(V), not colored(V, green), not colored(V, blue).";
|
||||
anthem::translate("input", input, context);
|
||||
|
||||
CHECK(output.str() ==
|
||||
"forall V1, V2 (colored(V1, V2) <-> (V2 = red and vertex(V1) and not colored(V1, green) and not colored(V1, blue)))\n"
|
||||
"forall V3 not vertex(V3)\n");
|
||||
}
|
||||
|
||||
SECTION("single placeholder")
|
||||
{
|
||||
input <<
|
||||
"#external vertex(1).\n"
|
||||
"colored(V, red) :- vertex(V), not colored(V, green), not colored(V, blue).";
|
||||
anthem::translate("input", input, context);
|
||||
|
||||
CHECK(output.str() ==
|
||||
"forall V1, V2 (colored(V1, V2) <-> (V2 = red and vertex(V1) and not colored(V1, green) and not colored(V1, blue)))\n");
|
||||
}
|
||||
|
||||
SECTION("complex example: graph coloring")
|
||||
{
|
||||
input <<
|
||||
"#external color(1).\n"
|
||||
"#external edge(2).\n"
|
||||
"#external vertex(1).\n"
|
||||
"#show color/2.\n"
|
||||
"{color(V, C)} :- vertex(V), color(C).\n"
|
||||
"covered(V) :- color(V, _).\n"
|
||||
":- vertex(V), not covered(V).\n"
|
||||
":- color(V1, C), color(V2, C), edge(V1, V2).";
|
||||
anthem::translate("input", input, context);
|
||||
|
||||
CHECK(output.str() ==
|
||||
"forall V1, V2 (color(V1, V2) <-> (vertex(V1) and color(V2) and color(V1, V2)))\n"
|
||||
"forall U1 not (vertex(U1) and not exists U2 color(U1, U2))\n"
|
||||
"forall U3, U4, U5 not (color(U3, U4) and color(U5, U4) and edge(U3, U5))\n");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user