2017-04-08 18:47:06 +02:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2017-05-30 03:53:51 +02:00
|
|
|
#include <anthem/AST.h>
|
2017-04-08 18:47:06 +02:00
|
|
|
#include <anthem/Context.h>
|
|
|
|
#include <anthem/Translation.h>
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-06-01 02:37:45 +02:00
|
|
|
TEST_CASE("[completion] Rules are completed", "[completion]")
|
2017-04-08 18:47:06 +02:00
|
|
|
{
|
|
|
|
std::stringstream input;
|
|
|
|
std::stringstream output;
|
|
|
|
std::stringstream errors;
|
|
|
|
|
|
|
|
anthem::output::Logger logger(output, errors);
|
2017-05-31 18:03:19 +02:00
|
|
|
anthem::Context context(std::move(logger));
|
2017-06-06 01:44:44 +02:00
|
|
|
context.performSimplification = true;
|
|
|
|
context.performCompletion = true;
|
2017-04-08 18:47:06 +02:00
|
|
|
|
2017-04-10 14:30:35 +02:00
|
|
|
SECTION("predicate in single rule head")
|
2017-04-08 18:47:06 +02:00
|
|
|
{
|
|
|
|
input << "p :- q.";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 18:47:06 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
|
|
|
"(p <-> q)\n"
|
|
|
|
"not q\n");
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("predicate in multiple rule heads")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"p :- q.\n"
|
2017-04-08 18:47:06 +02:00
|
|
|
"p :- r.\n"
|
|
|
|
"p :- s.";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 18:47:06 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
|
|
|
"(p <-> (q or r or s))\n"
|
|
|
|
"not q\n"
|
|
|
|
"not r\n"
|
|
|
|
"not s\n");
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("multiple predicates are correctly separated")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"p :- s.\n"
|
2017-04-08 18:47:06 +02:00
|
|
|
"q :- t.\n"
|
|
|
|
"p :- q.\n"
|
|
|
|
"r :- t.\n"
|
|
|
|
"q :- r.";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"(p <-> (s or q))\n"
|
|
|
|
"(q <-> (t or r))\n"
|
|
|
|
"(r <-> t)\n"
|
|
|
|
"not s\n"
|
|
|
|
"not t\n");
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|
2017-04-08 18:50:42 +02:00
|
|
|
|
|
|
|
SECTION("integrity constraints")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
":- q.\n"
|
|
|
|
":- r(5).\n"
|
|
|
|
":- s(N).\n"
|
2017-04-08 20:02:20 +02:00
|
|
|
"#false :- t.\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"#false :- u(5).";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"not q\n"
|
|
|
|
"forall V1 not r(V1)\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall V2 not s(V2)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not t\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall V3 not u(V3)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not q\n"
|
|
|
|
"not r(5)\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall U1 not s(U1)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not t\n"
|
|
|
|
"not u(5)\n");
|
|
|
|
}
|
2017-04-08 18:50:42 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
SECTION("Booleans")
|
|
|
|
{
|
|
|
|
input <<
|
|
|
|
"#true :- #true.\n"
|
|
|
|
"#true :- #false.\n"
|
|
|
|
"#false :- #true.\n"
|
|
|
|
"#false :- #false.\n";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"not #true\n"
|
|
|
|
"not #false\n");
|
2017-04-08 18:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("facts")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"q.\n"
|
2017-04-08 19:59:59 +02:00
|
|
|
"r.\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"s :- #true.\n"
|
|
|
|
"t :- #true.";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"q\n"
|
|
|
|
"r\n"
|
|
|
|
"s\n"
|
|
|
|
"t\n");
|
2017-04-08 18:50:42 +02:00
|
|
|
}
|
2017-04-08 20:17:01 +02:00
|
|
|
|
2017-06-12 15:32:05 +02:00
|
|
|
SECTION("nested arguments")
|
|
|
|
{
|
|
|
|
input <<
|
|
|
|
"f(f(f(f(f(X))))) :- f(X).\n"
|
|
|
|
"f(1..5).";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"forall V1 (f(V1) <-> (exists U1 (V1 = f(f(f(f(U1)))) and f(U1)) or V1 in 1..5))\n");
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:17:01 +02:00
|
|
|
SECTION("useless implications")
|
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"#true :- p, q(N), t(1, 2).\n"
|
2017-04-08 20:17:01 +02:00
|
|
|
"#true.\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"v :- #false.";
|
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 20:17:01 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
|
|
|
"not p\n"
|
|
|
|
"forall V1 not q(V1)\n"
|
2017-06-01 02:37:45 +02:00
|
|
|
"forall V2, V3 not t(V2, V3)\n"
|
2017-04-10 16:32:12 +02:00
|
|
|
"not v\n");
|
2017-04-08 20:17:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-09 22:10:43 +02:00
|
|
|
SECTION("Schur number example")
|
2017-04-08 20:22:50 +02:00
|
|
|
{
|
2017-04-10 16:32:12 +02:00
|
|
|
input <<
|
|
|
|
"{in(1..n, 1..r)}.\n"
|
2017-04-08 20:22:50 +02:00
|
|
|
"covered(I) :- in(I, S).\n"
|
|
|
|
":- I = 1..n, not covered(I).\n"
|
|
|
|
":- in(I, S), in(J, S), in(I + J, S).";
|
2017-04-10 16:32:12 +02:00
|
|
|
anthem::translate("input", input, context);
|
2017-04-08 20:22:50 +02:00
|
|
|
|
2017-04-10 16:32:12 +02:00
|
|
|
CHECK(output.str() ==
|
2017-06-05 03:58:39 +02:00
|
|
|
"forall V1 (covered(V1) <-> exists U1 in(V1, U1))\n"
|
2018-04-09 23:13:21 +02:00
|
|
|
"forall V2, V3 (in(V2, V3) -> (V2 in 1..n and V3 in 1..r))\n"
|
2018-04-09 23:38:58 +02:00
|
|
|
"forall U2 (not U2 in 1..n or covered(U2))\n"
|
|
|
|
"forall U3, U4, U5 (not in(U3, U4) or not in(U5, U4) or not exists X1 (X1 in (U3 + U5) and in(X1, U4)))\n");
|
2017-04-08 20:22:50 +02:00
|
|
|
}
|
2017-06-09 22:00:00 +02:00
|
|
|
|
|
|
|
SECTION("binary operations with multiple variables")
|
|
|
|
{
|
|
|
|
input << "a(X, Y) :- b(c(X + Y), d(1 + Y)).";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"forall V1, V2 (a(V1, V2) <-> b(c((V1 + V2)), d((1 + V2))))\n"
|
|
|
|
"forall V3, V4 not b(V3, V4)\n");
|
|
|
|
}
|
2017-06-12 18:27:39 +02:00
|
|
|
|
|
|
|
SECTION("predicate with more than one argument")
|
|
|
|
{
|
|
|
|
input << "p(X, Y, Z).";
|
|
|
|
anthem::translate("input", input, context);
|
|
|
|
|
|
|
|
// TODO: simplify further
|
|
|
|
CHECK(output.str() ==
|
|
|
|
"forall V1, V2, V3 (p(V1, V2, V3) <-> #true)\n");
|
|
|
|
}
|
2017-04-08 18:47:06 +02:00
|
|
|
}
|