From a93d59c2a4cda233d38347d65b7d9707d1d6a05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 28 Mar 2017 18:07:06 +0200 Subject: [PATCH] Cleaned up clingo rule head and body visitors. --- include/anthem/Body.h | 78 ++++-------------------------- include/anthem/Head.h | 107 +++++++----------------------------------- 2 files changed, 28 insertions(+), 157 deletions(-) diff --git a/include/anthem/Body.h b/include/anthem/Body.h index b7e774d..60c6969 100644 --- a/include/anthem/Body.h +++ b/include/anthem/Body.h @@ -51,36 +51,6 @@ ast::Variable makeAuxiliaryBodyVariable(int i) struct BodyTermTranslateVisitor { - std::experimental::optional visit(const Clingo::Symbol &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“symbol” terms currently unsupported in this context", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::Variable &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“variable” terms currently unsupported in this context", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported in this context", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported in this context", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::Interval &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“interval” terms currently unsupported in this context", context); - return std::experimental::nullopt; - } - // TODO: refactor std::experimental::optional visit(const Clingo::AST::Function &function, const Clingo::AST::Literal &literal, const Clingo::AST::Term &, Context &context) { @@ -120,9 +90,10 @@ struct BodyTermTranslateVisitor return ast::Formula::make(std::move(variables), std::move(conjunction)); } - std::experimental::optional visit(const Clingo::AST::Pool &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) + template + std::experimental::optional visit(const T &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“pool” terms currently unsupported", context); + throwErrorAtLocation(term.location, "term currently unsupported in this context, expected function", context); return std::experimental::nullopt; } }; @@ -131,16 +102,9 @@ struct BodyTermTranslateVisitor struct BodyLiteralTranslateVisitor { - std::experimental::optional visit(const Clingo::AST::Boolean &, const Clingo::AST::Literal &literal, Context &context) - { - throwErrorAtLocation(literal.location, "“boolean” literals currently unsupported in this context", context); - return std::experimental::nullopt; - } - std::experimental::optional visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &literal, Context &context) { return term.data.accept(BodyTermTranslateVisitor(), literal, term, context); - return std::experimental::nullopt; } // TODO: refactor @@ -148,7 +112,7 @@ struct BodyLiteralTranslateVisitor { // Comparisons should never have a sign, because these are converted to positive comparisons by clingo if (literal.sign != Clingo::AST::Sign::None) - throwErrorAtLocation(literal.location, "negated comparisons in body currently unsupported", context); + throwErrorAtLocation(literal.location, "negated comparisons currently unsupported", context); const auto operator_ = translate(comparison.comparison); @@ -168,9 +132,10 @@ struct BodyLiteralTranslateVisitor return ast::Formula::make(std::move(variables), std::move(conjunction)); } - std::experimental::optional visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, Context &context) + template + std::experimental::optional visit(const T &, const Clingo::AST::Literal &literal, Context &context) { - throwErrorAtLocation(literal.location, "CSP literals currently unsupported", context); + throwErrorAtLocation(literal.location, "literal currently unsupported in this context, expected function or term", context); return std::experimental::nullopt; } }; @@ -184,33 +149,10 @@ struct BodyBodyLiteralTranslateVisitor return literal.data.accept(BodyLiteralTranslateVisitor(), literal, context); } - std::experimental::optional visit(const Clingo::AST::ConditionalLiteral &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) + template + std::experimental::optional visit(const T &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) { - throwErrorAtLocation(bodyLiteral.location, "“conditional literal” body literals currently unsupported", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::Aggregate &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) - { - throwErrorAtLocation(bodyLiteral.location, "“aggregate” body literals currently unsupported", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::BodyAggregate &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) - { - throwErrorAtLocation(bodyLiteral.location, "“body aggregate” body literals currently unsupported", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) - { - throwErrorAtLocation(bodyLiteral.location, "“theory atom” body literals currently unsupported", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::Disjoint &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) - { - throwErrorAtLocation(bodyLiteral.location, "“disjoint” body literals currently unsupported", context); + throwErrorAtLocation(bodyLiteral.location, "body literal currently unsupported in this context, expected literal", context); return std::experimental::nullopt; } }; diff --git a/include/anthem/Head.h b/include/anthem/Head.h index 4fa76f2..4f53ed6 100644 --- a/include/anthem/Head.h +++ b/include/anthem/Head.h @@ -18,31 +18,6 @@ namespace anthem struct TermCollectFunctionTermsVisitor { - void visit(const Clingo::Symbol &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“symbol” terms not allowed, function expected", context); - } - - void visit(const Clingo::AST::Variable &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“variable” terms currently unsupported, function expected", context); - } - - void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported, function expected", context); - } - - void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported, function expected", context); - } - - void visit(const Clingo::AST::Interval &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“interval” terms currently unsupported, function expected", context); - } - void visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term, Context &context) { if (function.external) @@ -54,9 +29,10 @@ struct TermCollectFunctionTermsVisitor context.headTerms.emplace_back(&argument); } - void visit(const Clingo::AST::Pool &, const Clingo::AST::Term &term, Context &context) + template + void visit(const T &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“pool” terms currently unsupported, function expected", context); + throwErrorAtLocation(term.location, "term currently unsupported in this context, function expected", context); } }; @@ -73,12 +49,8 @@ struct LiteralCollectFunctionTermsVisitor term.data.accept(TermCollectFunctionTermsVisitor(), term, context); } - void visit(const Clingo::AST::Comparison &, const Clingo::AST::Literal &literal, Context &context) - { - throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); - } - - void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, Context &context) + template + void visit(const T &, const Clingo::AST::Literal &literal, Context &context) { throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); } @@ -115,7 +87,7 @@ struct HeadLiteralCollectFunctionTermsVisitor context.numberOfHeadLiterals = aggregate.elements.size(); if (aggregate.left_guard || aggregate.right_guard) - throwErrorAtLocation(headLiteral.location, "aggregates with left or right guards not allowed", context); + throwErrorAtLocation(headLiteral.location, "aggregates with left or right guards currently unsupported", context); for (const auto &conditionalLiteral : aggregate.elements) { @@ -126,14 +98,10 @@ struct HeadLiteralCollectFunctionTermsVisitor } } - void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) + template + void visit(const T &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“head aggregate” head literals currently unsupported", context); - } - - void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) - { - throwErrorAtLocation(headLiteral.location, "“theory” head literals currently unsupported", context); + throwErrorAtLocation(headLiteral.location, "head literal currently unsupported in this context, expected literal, disjunction, or aggregate", context); } }; @@ -141,36 +109,6 @@ struct HeadLiteralCollectFunctionTermsVisitor struct FunctionTermTranslateVisitor { - std::experimental::optional visit(const Clingo::Symbol &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“symbol” terms not allowed, function expected", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::Variable &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“variable” terms currently unsupported, function expected", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported, function expected", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported, function expected", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::Interval &, const Clingo::AST::Term &term, Context &context) - { - throwErrorAtLocation(term.location, "“interval” terms currently unsupported, function expected", context); - return std::experimental::nullopt; - } - // TODO: check correctness std::experimental::optional visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term, Context &context) { @@ -194,9 +132,10 @@ struct FunctionTermTranslateVisitor return ast::Formula::make(function.name, std::move(arguments)); } - std::experimental::optional visit(const Clingo::AST::Pool &, const Clingo::AST::Term &term, Context &context) + template + std::experimental::optional visit(const T &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“pool” terms currently unsupported, function expected", context); + throwErrorAtLocation(term.location, "term currently unsupported in this context, function expected", context); return std::experimental::nullopt; } }; @@ -215,13 +154,8 @@ struct LiteralTranslateVisitor return term.data.accept(FunctionTermTranslateVisitor(), term, context); } - std::experimental::optional visit(const Clingo::AST::Comparison &, const Clingo::AST::Literal &literal, Context &context) - { - throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, Context &context) + template + std::experimental::optional visit(const T &, const Clingo::AST::Literal &literal, Context &context) { throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); return std::experimental::nullopt; @@ -272,7 +206,7 @@ struct HeadLiteralTranslateToConsequentVisitor std::experimental::optional visit(const Clingo::AST::Aggregate &aggregate, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { if (aggregate.left_guard || aggregate.right_guard) - throwErrorAtLocation(headLiteral.location, "aggregates with left or right guards not allowed", context); + throwErrorAtLocation(headLiteral.location, "aggregates with left or right guards currently unsupported", context); const auto translateConditionalLiteral = [&](const auto &conditionalLiteral) @@ -302,15 +236,10 @@ struct HeadLiteralTranslateToConsequentVisitor return ast::Formula::make(std::move(arguments)); } - std::experimental::optional visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) + template + std::experimental::optional visit(const T &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“head aggregate” head literals currently unsupported", context); - return std::experimental::nullopt; - } - - std::experimental::optional visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) - { - throwErrorAtLocation(headLiteral.location, "“theory” head literals currently unsupported", context); + throwErrorAtLocation(headLiteral.location, "head literal currently unsupported in this context, expected literal, disjunction, or aggregate", context); return std::experimental::nullopt; } };