Represent predicate parameters explicitly

This adds a vector of Parameter structs to PredicateDeclaration. In this
way, the domain of each parameter can be tracked individually.
This commit is contained in:
Patrick Lühne 2018-04-19 16:35:37 +02:00
parent 159717f51c
commit 7bde7c498f
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
6 changed files with 23 additions and 13 deletions

View File

@ -245,9 +245,14 @@ struct PredicateDeclaration
Hidden Hidden
}; };
struct Parameter
{
Domain domain{Domain::Unknown};
};
explicit PredicateDeclaration(std::string &&name, size_t arity) explicit PredicateDeclaration(std::string &&name, size_t arity)
: name{std::move(name)}, : name{std::move(name)},
arity{arity} parameters{std::vector<Parameter>(arity)}
{ {
} }
@ -256,8 +261,13 @@ struct PredicateDeclaration
PredicateDeclaration(PredicateDeclaration &&other) noexcept = default; PredicateDeclaration(PredicateDeclaration &&other) noexcept = default;
PredicateDeclaration &operator=(PredicateDeclaration &&other) noexcept = default; PredicateDeclaration &operator=(PredicateDeclaration &&other) noexcept = default;
size_t arity() const noexcept
{
return parameters.size();
}
std::string name; std::string name;
size_t arity; std::vector<Parameter> parameters;
bool isUsed{false}; bool isUsed{false};
bool isExternal{false}; bool isExternal{false};
Visibility visibility{Visibility::Default}; Visibility visibility{Visibility::Default};

View File

@ -38,7 +38,7 @@ struct Context
const auto matchesExistingPredicateDeclaration = const auto matchesExistingPredicateDeclaration =
[&](const auto &predicateDeclaration) [&](const auto &predicateDeclaration)
{ {
return (predicateDeclaration->arity == arity return (predicateDeclaration->arity() == arity
&& strcmp(predicateDeclaration->name.c_str(), name) == 0); && strcmp(predicateDeclaration->name.c_str(), name) == 0);
}; };

View File

@ -261,7 +261,7 @@ inline output::ColorStream &print(output::ColorStream &stream, const Predicate &
inline output::ColorStream &print(output::ColorStream &stream, const PredicateDeclaration &predicateDeclaration, PrintContext &, bool) inline output::ColorStream &print(output::ColorStream &stream, const PredicateDeclaration &predicateDeclaration, PrintContext &, bool)
{ {
return (stream << predicateDeclaration.name << "/" << predicateDeclaration.arity); return (stream << predicateDeclaration.name << "/" << predicateDeclaration.arity());
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -104,12 +104,12 @@ ast::Formula completePredicate(ast::PredicateDeclaration &predicateDeclaration,
{ {
// Create new set of parameters for the completed definition for the predicate // Create new set of parameters for the completed definition for the predicate
ast::VariableDeclarationPointers parameters; ast::VariableDeclarationPointers parameters;
parameters.reserve(predicateDeclaration.arity); parameters.reserve(predicateDeclaration.arity());
std::vector<ast::Term> arguments; std::vector<ast::Term> arguments;
arguments.reserve(predicateDeclaration.arity); arguments.reserve(predicateDeclaration.arity());
for (size_t i = 0; i < predicateDeclaration.arity; i++) for (size_t i = 0; i < predicateDeclaration.arity(); i++)
{ {
parameters.emplace_back(std::make_unique<ast::VariableDeclaration>(ast::VariableDeclaration::Type::Head)); parameters.emplace_back(std::make_unique<ast::VariableDeclaration>(ast::VariableDeclaration::Type::Head));
arguments.emplace_back(ast::Term::make<ast::Variable>(parameters.back().get())); arguments.emplace_back(ast::Term::make<ast::Variable>(parameters.back().get()));
@ -169,7 +169,7 @@ std::vector<ast::Formula> complete(std::vector<ast::ScopedFormula> &&scopedFormu
if (order != 0) if (order != 0)
return (order < 0); return (order < 0);
return lhs->arity < rhs->arity; return lhs->arity() < rhs->arity();
}); });
std::vector<ast::Formula> completedFormulas; std::vector<ast::Formula> completedFormulas;

View File

@ -181,7 +181,7 @@ PredicateReplacement findReplacement(const ast::PredicateDeclaration &predicateD
else if (completedPredicateDefinition.is<ast::Not>()) else if (completedPredicateDefinition.is<ast::Not>())
return findReplacement(predicateDeclaration, completedPredicateDefinition.get<ast::Not>()); return findReplacement(predicateDeclaration, completedPredicateDefinition.get<ast::Not>());
throw CompletionException("unsupported completed definition for predicate “" + predicateDeclaration.name + "/" + std::to_string(predicateDeclaration.arity) + "” for hiding predicates"); throw CompletionException("unsupported completed definition for predicate “" + predicateDeclaration.name + "/" + std::to_string(predicateDeclaration.arity()) + "” for hiding predicates");
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -217,7 +217,7 @@ void eliminateHiddenPredicates(std::vector<ast::Formula> &completedFormulas, Con
if (isPredicateVisible) if (isPredicateVisible)
continue; continue;
context.logger.log(output::Priority::Debug) << "eliminating “" << predicateDeclaration->name << "/" << predicateDeclaration->arity << ""; context.logger.log(output::Priority::Debug) << "eliminating “" << predicateDeclaration->name << "/" << predicateDeclaration->arity() << "";
const auto &completedPredicateDefinition = completedFormulas[i]; const auto &completedPredicateDefinition = completedFormulas[i];
auto replacement = findReplacement(*predicateDeclaration, completedPredicateDefinition); auto replacement = findReplacement(*predicateDeclaration, completedPredicateDefinition);
@ -227,7 +227,7 @@ void eliminateHiddenPredicates(std::vector<ast::Formula> &completedFormulas, Con
if (hasCircularDependency) if (hasCircularDependency)
{ {
context.logger.log(output::Priority::Warning) << "cannot hide predicate “" << predicateDeclaration->name << "/" << predicateDeclaration->arity << "” due to circular dependency"; context.logger.log(output::Priority::Warning) << "cannot hide predicate “" << predicateDeclaration->name << "/" << predicateDeclaration->arity() << "” due to circular dependency";
continue; continue;
} }

View File

@ -97,7 +97,7 @@ void translate(const char *fileName, std::istream &stream, Context &context)
<< "#show declaration of “" << "#show declaration of “"
<< predicateDeclaration->name << predicateDeclaration->name
<< "/" << "/"
<< predicateDeclaration->arity << predicateDeclaration->arity()
<< "” does not match any declared predicate"; << "” does not match any declared predicate";
// Check for #external statements with undeclared predicates // Check for #external statements with undeclared predicates
@ -106,7 +106,7 @@ void translate(const char *fileName, std::istream &stream, Context &context)
<< "#external declaration of “" << "#external declaration of “"
<< predicateDeclaration->name << predicateDeclaration->name
<< "/" << "/"
<< predicateDeclaration->arity << predicateDeclaration->arity()
<< "” does not match any declared predicate"; << "” does not match any declared predicate";
} }