From eaabeb0c55e524f9b3d874d5df625906248fa03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Mon, 9 Apr 2018 00:07:47 +0200 Subject: [PATCH] Support exponentiation operator Because of a bug in the Clingo API, the exponentation operator was not properly exposed to anthem. This updates Clingo to a version with a fixed API and adds proper support for exponentation within anthem along with a matching unit test. --- include/anthem/AST.h | 3 ++- include/anthem/Term.h | 12 +++++++++--- include/anthem/output/AST.h | 2 ++ lib/clingo | 2 +- tests/TestTranslation.cpp | 8 ++++++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/anthem/AST.h b/include/anthem/AST.h index 94e5af3..f5c70fe 100644 --- a/include/anthem/AST.h +++ b/include/anthem/AST.h @@ -32,7 +32,8 @@ struct BinaryOperation Minus, Multiplication, Division, - Modulo + Modulo, + Power }; explicit BinaryOperation(Operator operator_, Term &&left, Term &&right) diff --git a/include/anthem/Term.h b/include/anthem/Term.h index 3addeb9..01373f8 100644 --- a/include/anthem/Term.h +++ b/include/anthem/Term.h @@ -23,6 +23,12 @@ ast::BinaryOperation::Operator translate(Clingo::AST::BinaryOperator binaryOpera { switch (binaryOperator) { + case Clingo::AST::BinaryOperator::XOr: + throw TranslationException(term.location, "binary operation “xor” currently unsupported"); + case Clingo::AST::BinaryOperator::Or: + throw TranslationException(term.location, "binary operation “or” currently unsupported"); + case Clingo::AST::BinaryOperator::And: + throw TranslationException(term.location, "binary operation “and” currently unsupported"); case Clingo::AST::BinaryOperator::Plus: return ast::BinaryOperation::Operator::Plus; case Clingo::AST::BinaryOperator::Minus: @@ -33,11 +39,11 @@ ast::BinaryOperation::Operator translate(Clingo::AST::BinaryOperator binaryOpera return ast::BinaryOperation::Operator::Division; case Clingo::AST::BinaryOperator::Modulo: return ast::BinaryOperation::Operator::Modulo; - default: - throw TranslationException(term.location, "“binary operation” terms currently unsupported"); + case Clingo::AST::BinaryOperator::Power: + return ast::BinaryOperation::Operator::Power; } - return ast::BinaryOperation::Operator::Plus; + throw TranslationException(term.location, "unknown binary operation"); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/anthem/output/AST.h b/include/anthem/output/AST.h index a6796af..8f7ea04 100644 --- a/include/anthem/output/AST.h +++ b/include/anthem/output/AST.h @@ -85,6 +85,8 @@ inline output::ColorStream &print(output::ColorStream &stream, BinaryOperation:: return (stream << output::Operator("/")); case BinaryOperation::Operator::Modulo: return (stream << output::Operator("%")); + case BinaryOperation::Operator::Power: + return (stream << output::Operator("**")); } return stream; diff --git a/lib/clingo b/lib/clingo index e2187b6..969ce8f 160000 --- a/lib/clingo +++ b/lib/clingo @@ -1 +1 @@ -Subproject commit e2187b697f738f236828f7f780b5481c9a9284e6 +Subproject commit 969ce8f618c4cdf78d5c430ba827217075b88460 diff --git a/tests/TestTranslation.cpp b/tests/TestTranslation.cpp index e8d38a7..c55f543 100644 --- a/tests/TestTranslation.cpp +++ b/tests/TestTranslation.cpp @@ -296,4 +296,12 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]") CHECK(output.str() == "((V1 in U1 and V2 in U2 and exists X1, X2 (X1 in U3 and X2 in U4 and q(X1, X2))) -> p(V1, V2))\n"); } + + SECTION("exponentiation operator") + { + input << "p(N, N ** N) :- N = 1..n."; + anthem::translate("input", input, context); + + CHECK(output.str() == "((V1 in U1 and V2 in (U1 ** U1) and exists X1, X2 (X1 in U1 and X2 in 1..n and X1 = X2)) -> p(V1, V2))\n"); + } }