Compare commits
32 Commits
v0.1.9-rc.
...
develop
Author | SHA1 | Date | |
---|---|---|---|
e0aa3497ee | |||
64bab69a36 | |||
9566629237 | |||
c199f609bd | |||
1570432ee0 | |||
0ce4e54d1a | |||
fa3ed31eca | |||
e85807accb | |||
3393f84a4a | |||
7013b9ea54 | |||
c84ae51ff2 | |||
d60e2a736b | |||
bb9013e7c5 | |||
69688d1d39 | |||
a70b1fb726 | |||
a2c4d87852 | |||
b60c65a810 | |||
19e1e16e45 | |||
63f39e5162 | |||
811523e580 | |||
09ef64a0e1 | |||
43d2c153c7 | |||
541cb3fb47 | |||
921d5ed4f0 | |||
e0509f725a | |||
48cf8ee3e0 | |||
f7d99c82fa | |||
618189368c | |||
d0debc6ad1 | |||
3ba80e8c9d | |||
d66d3557c1 | |||
e15a6b2e88 |
19
CHANGELOG.md
19
CHANGELOG.md
@ -1,13 +1,24 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
## 0.1.9 RC 2 (2018-04-21)
|
## (unreleased)
|
||||||
|
|
||||||
|
## 0.1.9 (2018-05-04)
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
* turns on completion and simplification by default, which can now be switched off with `--no-complete` and `--no-simplify`
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
* optional detection of integer variables and integer predicate parameters
|
* detection of integer variables and integer predicate parameters
|
||||||
* command-line option `--detect-integers` to enable integer variable detection
|
* command-line option `--no-detect-integers` to disable integer variable detection
|
||||||
* support for declaring functions integer with the `#external` directive
|
|
||||||
* new simplification rule applying to integer variables
|
* new simplification rule applying to integer variables
|
||||||
|
* support for declaring functions integer with the `#external` directive
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fixes incorrect translation of unsupported choice rules with multiple elements by returning an error instead
|
||||||
|
* fixes precedence of intervals by enclosing them in parentheses
|
||||||
|
|
||||||
## 0.1.8 (2018-04-20)
|
## 0.1.8 (2018-04-20)
|
||||||
|
|
||||||
|
@ -9,11 +9,12 @@
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ anthem [--complete] [--simplify] file...
|
$ anthem [--no-complete] [--no-simplify] [--no-detect-integers] file...
|
||||||
```
|
```
|
||||||
|
|
||||||
`--complete` instructs `anthem` to perform Clark’s completion on the translated formulas.
|
By default, `anthem` performs Clark’s completion on the translated formulas, detects which variables are integer, and simplifies the output by applying several basic transformation rules.
|
||||||
With the option `--simplify`, the output formulas are simplified by applying several basic transformation rules.
|
|
||||||
|
These processing steps can be turned off with the options `--no-complete`, `--no-simplify`, and `--no-detect-integers`.
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
|
14
app/main.cpp
14
app/main.cpp
@ -16,9 +16,9 @@ int main(int argc, char **argv)
|
|||||||
("h,help", "Display this help message")
|
("h,help", "Display this help message")
|
||||||
("v,version", "Display version information")
|
("v,version", "Display version information")
|
||||||
("i,input", "Input files", cxxopts::value<std::vector<std::string>>())
|
("i,input", "Input files", cxxopts::value<std::vector<std::string>>())
|
||||||
("s,simplify", "Simplify the output")
|
("no-simplify", "Do not simplify the output")
|
||||||
("c,complete", "Perform completion")
|
("no-complete", "Do not perform completion")
|
||||||
("d,detect-integers", "Detect integer variables")
|
("no-detect-integers", "Do not detect integer variables")
|
||||||
("color", "Colorize output (always, never, auto)", cxxopts::value<std::string>()->default_value("auto"))
|
("color", "Colorize output (always, never, auto)", cxxopts::value<std::string>()->default_value("auto"))
|
||||||
("parentheses", "Parenthesis style (normal, full)", cxxopts::value<std::string>()->default_value("normal"))
|
("parentheses", "Parenthesis style (normal, full)", cxxopts::value<std::string>()->default_value("normal"))
|
||||||
("p,log-priority", "Log messages starting from this priority (debug, info, warning, error)", cxxopts::value<std::string>()->default_value("info"));
|
("p,log-priority", "Log messages starting from this priority (debug, info, warning, error)", cxxopts::value<std::string>()->default_value("info"));
|
||||||
@ -49,9 +49,9 @@ int main(int argc, char **argv)
|
|||||||
if (parseResult.count("input") > 0)
|
if (parseResult.count("input") > 0)
|
||||||
inputFiles = parseResult["input"].as<std::vector<std::string>>();
|
inputFiles = parseResult["input"].as<std::vector<std::string>>();
|
||||||
|
|
||||||
context.performSimplification = (parseResult.count("simplify") > 0);
|
context.performSimplification = (parseResult.count("no-simplify") == 0);
|
||||||
context.performCompletion = (parseResult.count("complete") > 0);
|
context.performCompletion = (parseResult.count("no-complete") == 0);
|
||||||
context.performIntegerDetection = (parseResult.count("detect-integers") > 0);
|
context.performIntegerDetection = (parseResult.count("no-detect-integers") == 0);
|
||||||
colorPolicyString = parseResult["color"].as<std::string>();
|
colorPolicyString = parseResult["color"].as<std::string>();
|
||||||
parenthesisStyleString = parseResult["parentheses"].as<std::string>();
|
parenthesisStyleString = parseResult["parentheses"].as<std::string>();
|
||||||
logPriorityString = parseResult["log-priority"].as<std::string>();
|
logPriorityString = parseResult["log-priority"].as<std::string>();
|
||||||
@ -72,7 +72,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
if (version)
|
if (version)
|
||||||
{
|
{
|
||||||
std::cout << "anthem version 0.1.9-rc.2" << std::endl;
|
std::cout << "anthem version 0.1.9+git" << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
examples/choice-rules.lp
Normal file
2
examples/choice-rules.lp
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
p(a).
|
||||||
|
{q(a)}.
|
@ -1,10 +1,18 @@
|
|||||||
#external color(1).
|
% assign a set of colors to each vertex
|
||||||
#external edge(2).
|
|
||||||
#external vertex(1).
|
|
||||||
#show color/2.
|
|
||||||
|
|
||||||
{color(V, C)} :- vertex(V), color(C).
|
{color(V, C)} :- vertex(V), color(C).
|
||||||
|
|
||||||
|
% at most one color per vertex
|
||||||
|
:- color(V, C1), color(V, C2), C1 != C2.
|
||||||
|
|
||||||
|
% at least one color per vertex
|
||||||
covered(V) :- color(V, _).
|
covered(V) :- color(V, _).
|
||||||
:- vertex(V), not covered(V).
|
:- vertex(V), not covered(V).
|
||||||
|
|
||||||
|
% adjacent vertices don’t share the same color
|
||||||
:- color(V1, C), color(V2, C), edge(V1, V2).
|
:- color(V1, C), color(V2, C), edge(V1, V2).
|
||||||
:- color(V, C1), color(V, C2), C1 != C2.
|
|
||||||
|
#show color/2.
|
||||||
|
|
||||||
|
#external vertex(1).
|
||||||
|
#external edge(2).
|
||||||
|
#external color(1).
|
||||||
|
11
examples/letters.lp
Normal file
11
examples/letters.lp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
letter(a).
|
||||||
|
letter(b).
|
||||||
|
letter(c).
|
||||||
|
|
||||||
|
{p(1..3, Y)} :- letter(Y).
|
||||||
|
:- p(X1, Y), p(X2, Y), X1 != X2.
|
||||||
|
|
||||||
|
q(X) :- p(X, _).
|
||||||
|
:- X = 1..3, not q(X).
|
||||||
|
|
||||||
|
#show p/2.
|
@ -1,5 +1,4 @@
|
|||||||
#show p/2.
|
#show p/2.
|
||||||
#external integer(n(0)).
|
|
||||||
|
|
||||||
{p(1..n, 1..n)}.
|
{p(1..n, 1..n)}.
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#show prime/1.
|
#show prime/1.
|
||||||
#external integer(n(0)).
|
|
||||||
|
|
||||||
composite(I * J) :- I = 2..n, J = 2..n.
|
composite(I * J) :- I = 2..n, J = 2..n.
|
||||||
prime(N) :- N = 2..n, not composite(N).
|
prime(N) :- N = 2..n, not composite(N).
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
#show in/2.
|
#show in/2.
|
||||||
#external integer(n(0)).
|
|
||||||
#external integer(r(0)).
|
|
||||||
|
|
||||||
{in(1..n, 1..r)}.
|
{in(1..n, 1..r)}.
|
||||||
covered(I) :- in(I, S).
|
covered(I) :- in(I, S).
|
||||||
|
9
examples/simple-external-show.lp
Normal file
9
examples/simple-external-show.lp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
s(X) :- p(X).
|
||||||
|
s(X) :- q(X).
|
||||||
|
u(X) :- r(X), not s(X).
|
||||||
|
|
||||||
|
#show u/1.
|
||||||
|
|
||||||
|
#external p(1).
|
||||||
|
#external q(1).
|
||||||
|
#external r(1).
|
5
examples/simple-external.lp
Normal file
5
examples/simple-external.lp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
s(X) :- p(X).
|
||||||
|
s(X) :- q(X).
|
||||||
|
|
||||||
|
#external p(1).
|
||||||
|
#external q(1).
|
@ -2,7 +2,7 @@
|
|||||||
#define __ANTHEM__AST_H
|
#define __ANTHEM__AST_H
|
||||||
|
|
||||||
#include <anthem/ASTForward.h>
|
#include <anthem/ASTForward.h>
|
||||||
#include <anthem/Domain.h>
|
#include <anthem/Utils.h>
|
||||||
|
|
||||||
namespace anthem
|
namespace anthem
|
||||||
{
|
{
|
||||||
@ -148,7 +148,7 @@ struct FunctionDeclaration
|
|||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
size_t arity;
|
size_t arity;
|
||||||
Domain domain{Domain::General};
|
Domain domain{Domain::Noninteger};
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -90,6 +90,21 @@ struct ReplaceVariableInFormulaVisitor : public RecursiveFormulaVisitor<ReplaceV
|
|||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Accessing Variable Domains
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct DefaultVariableDomainAccessor
|
||||||
|
{
|
||||||
|
Domain operator()(const ast::Variable &variable)
|
||||||
|
{
|
||||||
|
return variable.declaration->domain;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,262 +0,0 @@
|
|||||||
#ifndef __ANTHEM__ARITHMETICS_H
|
|
||||||
#define __ANTHEM__ARITHMETICS_H
|
|
||||||
|
|
||||||
#include <anthem/Utils.h>
|
|
||||||
|
|
||||||
namespace anthem
|
|
||||||
{
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// Arithmetics
|
|
||||||
//
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct DefaultVariableDomainAccessor
|
|
||||||
{
|
|
||||||
ast::Domain operator()(const ast::Variable &variable)
|
|
||||||
{
|
|
||||||
return variable.declaration->domain;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
template <class VariableDomainAccessor = DefaultVariableDomainAccessor, class... Arguments>
|
|
||||||
EvaluationResult isArithmetic(const ast::Term &term, Arguments &&...);
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
template <class VariableDomainAccessor = DefaultVariableDomainAccessor>
|
|
||||||
struct IsTermArithmeticVisitor
|
|
||||||
{
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::BinaryOperation &binaryOperation, Arguments &&... arguments)
|
|
||||||
{
|
|
||||||
const auto isLeftArithemtic = isArithmetic<VariableDomainAccessor>(binaryOperation.left, std::forward<Arguments>(arguments)...);
|
|
||||||
const auto isRightArithmetic = isArithmetic<VariableDomainAccessor>(binaryOperation.right, std::forward<Arguments>(arguments)...);
|
|
||||||
|
|
||||||
if (isLeftArithemtic == EvaluationResult::Error || isRightArithmetic == EvaluationResult::Error)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (isLeftArithemtic == EvaluationResult::False || isRightArithmetic == EvaluationResult::False)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (isLeftArithemtic == EvaluationResult::Unknown || isRightArithmetic == EvaluationResult::Unknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
return EvaluationResult::True;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::Boolean &, Arguments &&...)
|
|
||||||
{
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::Function &function, Arguments &&...)
|
|
||||||
{
|
|
||||||
switch (function.declaration->domain)
|
|
||||||
{
|
|
||||||
case ast::Domain::General:
|
|
||||||
return EvaluationResult::False;
|
|
||||||
case ast::Domain::Integer:
|
|
||||||
return EvaluationResult::True;
|
|
||||||
case ast::Domain::Unknown:
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::Integer &, Arguments &&...)
|
|
||||||
{
|
|
||||||
return EvaluationResult::True;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::Interval &interval, Arguments &&... arguments)
|
|
||||||
{
|
|
||||||
const auto isFromArithmetic = isArithmetic<VariableDomainAccessor>(interval.from, std::forward<Arguments>(arguments)...);
|
|
||||||
const auto isToArithmetic = isArithmetic<VariableDomainAccessor>(interval.to, std::forward<Arguments>(arguments)...);
|
|
||||||
|
|
||||||
if (isFromArithmetic == EvaluationResult::Error || isToArithmetic == EvaluationResult::Error)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (isFromArithmetic == EvaluationResult::False || isToArithmetic == EvaluationResult::False)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (isFromArithmetic == EvaluationResult::Unknown || isToArithmetic == EvaluationResult::Unknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
return EvaluationResult::True;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::SpecialInteger &, Arguments &&...)
|
|
||||||
{
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::String &, Arguments &&...)
|
|
||||||
{
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::UnaryOperation &unaryOperation, Arguments &&... arguments)
|
|
||||||
{
|
|
||||||
const auto isArgumentArithmetic = isArithmetic<VariableDomainAccessor>(unaryOperation.argument, std::forward<Arguments>(arguments)...);
|
|
||||||
|
|
||||||
switch (unaryOperation.operator_)
|
|
||||||
{
|
|
||||||
case ast::UnaryOperation::Operator::Absolute:
|
|
||||||
return (isArgumentArithmetic == EvaluationResult::False ? EvaluationResult::Error : isArgumentArithmetic);
|
|
||||||
}
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::Variable &variable, Arguments &&... arguments)
|
|
||||||
{
|
|
||||||
const auto domain = VariableDomainAccessor()(variable, std::forward<Arguments>(arguments)...);
|
|
||||||
|
|
||||||
switch (domain)
|
|
||||||
{
|
|
||||||
case ast::Domain::General:
|
|
||||||
return EvaluationResult::False;
|
|
||||||
case ast::Domain::Integer:
|
|
||||||
return EvaluationResult::True;
|
|
||||||
case ast::Domain::Unknown:
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
template <class VariableDomainAccessor, class... Arguments>
|
|
||||||
EvaluationResult isArithmetic(const ast::Term &term, Arguments &&... arguments)
|
|
||||||
{
|
|
||||||
return term.accept(IsTermArithmeticVisitor<VariableDomainAccessor>(), std::forward<Arguments>(arguments)...);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
EvaluationResult isInteger(const ast::Term &term);
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct IsTermIntegerVisitor
|
|
||||||
{
|
|
||||||
static EvaluationResult visit(const ast::BinaryOperation &binaryOperation)
|
|
||||||
{
|
|
||||||
const auto isLeftArithemtic = isArithmetic(binaryOperation.left);
|
|
||||||
const auto isRightArithmetic = isArithmetic(binaryOperation.right);
|
|
||||||
|
|
||||||
if (isLeftArithemtic == EvaluationResult::Error || isRightArithmetic == EvaluationResult::Error)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (isLeftArithemtic == EvaluationResult::False || isRightArithmetic == EvaluationResult::False)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (binaryOperation.operator_ == ast::BinaryOperation::Operator::Division)
|
|
||||||
return EvaluationResult::False;
|
|
||||||
|
|
||||||
if (isLeftArithemtic == EvaluationResult::Unknown || isRightArithmetic == EvaluationResult::Unknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
return EvaluationResult::True;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Boolean &)
|
|
||||||
{
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Function &function)
|
|
||||||
{
|
|
||||||
switch (function.declaration->domain)
|
|
||||||
{
|
|
||||||
case ast::Domain::General:
|
|
||||||
return EvaluationResult::False;
|
|
||||||
case ast::Domain::Integer:
|
|
||||||
return EvaluationResult::True;
|
|
||||||
case ast::Domain::Unknown:
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Integer &)
|
|
||||||
{
|
|
||||||
return EvaluationResult::True;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::Interval &)
|
|
||||||
{
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::SpecialInteger &)
|
|
||||||
{
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::String &)
|
|
||||||
{
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class... Arguments>
|
|
||||||
static EvaluationResult visit(const ast::UnaryOperation &unaryOperation)
|
|
||||||
{
|
|
||||||
const auto isArgumentArithmetic = isArithmetic(unaryOperation.argument);
|
|
||||||
|
|
||||||
switch (unaryOperation.operator_)
|
|
||||||
{
|
|
||||||
case ast::UnaryOperation::Operator::Absolute:
|
|
||||||
return (isArgumentArithmetic == EvaluationResult::False ? EvaluationResult::Error : isArgumentArithmetic);
|
|
||||||
}
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Variable &variable)
|
|
||||||
{
|
|
||||||
switch (variable.declaration->domain)
|
|
||||||
{
|
|
||||||
case ast::Domain::General:
|
|
||||||
return EvaluationResult::False;
|
|
||||||
case ast::Domain::Integer:
|
|
||||||
return EvaluationResult::True;
|
|
||||||
case ast::Domain::Unknown:
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
inline EvaluationResult isInteger(const ast::Term &term)
|
|
||||||
{
|
|
||||||
return term.accept(IsTermIntegerVisitor());
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,27 +0,0 @@
|
|||||||
#ifndef __ANTHEM__DOMAIN_H
|
|
||||||
#define __ANTHEM__DOMAIN_H
|
|
||||||
|
|
||||||
namespace anthem
|
|
||||||
{
|
|
||||||
namespace ast
|
|
||||||
{
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// Domain
|
|
||||||
//
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
enum class Domain
|
|
||||||
{
|
|
||||||
General,
|
|
||||||
Integer,
|
|
||||||
Unknown,
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -268,8 +268,8 @@ struct TermEqualityVisitor
|
|||||||
return Tristate::Unknown;
|
return Tristate::Unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (equal(binaryOperation.left, binaryOperation.right) == Tristate::True
|
if (equal(binaryOperation.left, otherBinaryOperation.right) == Tristate::True
|
||||||
&& equal(binaryOperation.right, binaryOperation.left) == Tristate::True)
|
&& equal(binaryOperation.right, otherBinaryOperation.left) == Tristate::True)
|
||||||
{
|
{
|
||||||
return Tristate::True;
|
return Tristate::True;
|
||||||
}
|
}
|
||||||
|
244
include/anthem/Evaluation.h
Normal file
244
include/anthem/Evaluation.h
Normal file
@ -0,0 +1,244 @@
|
|||||||
|
#ifndef __ANTHEM__EVALUATION_H
|
||||||
|
#define __ANTHEM__EVALUATION_H
|
||||||
|
|
||||||
|
#include <anthem/AST.h>
|
||||||
|
#include <anthem/ASTUtils.h>
|
||||||
|
#include <anthem/Utils.h>
|
||||||
|
|
||||||
|
namespace anthem
|
||||||
|
{
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Evaluation
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template <class VariableDomainAccessor = DefaultVariableDomainAccessor>
|
||||||
|
struct EvaluateFormulaVisitor
|
||||||
|
{
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::And &and_, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
bool someFalse = false;
|
||||||
|
bool someUnknown = false;
|
||||||
|
|
||||||
|
for (const auto &argument : and_.arguments)
|
||||||
|
{
|
||||||
|
const auto result = evaluate(argument, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
switch (result)
|
||||||
|
{
|
||||||
|
case EvaluationResult::Error:
|
||||||
|
return EvaluationResult::Error;
|
||||||
|
case EvaluationResult::True:
|
||||||
|
break;
|
||||||
|
case EvaluationResult::False:
|
||||||
|
someFalse = true;
|
||||||
|
break;
|
||||||
|
case EvaluationResult::Unknown:
|
||||||
|
someUnknown = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (someFalse)
|
||||||
|
return EvaluationResult::False;
|
||||||
|
|
||||||
|
if (someUnknown)
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
|
||||||
|
return EvaluationResult::True;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Biconditional &biconditional, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto leftResult = evaluate(biconditional.left, std::forward<Arguments>(arguments)...);
|
||||||
|
const auto rightResult = evaluate(biconditional.right, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
if (leftResult == EvaluationResult::Error || rightResult == EvaluationResult::Error)
|
||||||
|
return EvaluationResult::Error;
|
||||||
|
|
||||||
|
if (leftResult == EvaluationResult::Unknown || rightResult == EvaluationResult::Unknown)
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
|
||||||
|
return (leftResult == rightResult ? EvaluationResult::True : EvaluationResult::False);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Boolean &boolean, Arguments &&...)
|
||||||
|
{
|
||||||
|
return (boolean.value == true ? EvaluationResult::True : EvaluationResult::False);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Comparison &comparison, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto leftType = type(comparison.left, std::forward<Arguments>(arguments)...);
|
||||||
|
const auto rightType = type(comparison.right, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
// Comparisons with empty sets always return false
|
||||||
|
if (leftType.setSize == SetSize::Empty || rightType.setSize == SetSize::Empty)
|
||||||
|
return EvaluationResult::False;
|
||||||
|
|
||||||
|
// If either side has an unknown domain, the result is unknown
|
||||||
|
if (leftType.domain == Domain::Unknown || rightType.domain == Domain::Unknown)
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
|
||||||
|
// If both sides have the same domain, the result is unknown
|
||||||
|
if (leftType.domain == rightType.domain)
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
|
||||||
|
// If one side is integer, but the other one isn’t, they are not equal
|
||||||
|
switch (comparison.operator_)
|
||||||
|
{
|
||||||
|
case ast::Comparison::Operator::Equal:
|
||||||
|
return EvaluationResult::False;
|
||||||
|
case ast::Comparison::Operator::NotEqual:
|
||||||
|
return EvaluationResult::True;
|
||||||
|
default:
|
||||||
|
// TODO: implement more cases
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Exists &exists, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
return evaluate(exists.argument, std::forward<Arguments>(arguments)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::ForAll &forAll, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
return evaluate(forAll.argument, std::forward<Arguments>(arguments)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Implies &implies, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto antecedentResult = evaluate(implies.antecedent, std::forward<Arguments>(arguments)...);
|
||||||
|
const auto consequentResult = evaluate(implies.consequent, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
if (antecedentResult == EvaluationResult::Error || consequentResult == EvaluationResult::Error)
|
||||||
|
return EvaluationResult::Error;
|
||||||
|
|
||||||
|
if (antecedentResult == EvaluationResult::False)
|
||||||
|
return EvaluationResult::True;
|
||||||
|
|
||||||
|
if (consequentResult == EvaluationResult::True)
|
||||||
|
return EvaluationResult::True;
|
||||||
|
|
||||||
|
if (antecedentResult == EvaluationResult::True && consequentResult == EvaluationResult::False)
|
||||||
|
return EvaluationResult::False;
|
||||||
|
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::In &in, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto elementType = type(in.element, std::forward<Arguments>(arguments)...);
|
||||||
|
const auto setType = type(in.set, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
// The element to test shouldn’t be empty or a proper set by itself
|
||||||
|
assert(elementType.setSize != SetSize::Empty && elementType.setSize != SetSize::Multi);
|
||||||
|
|
||||||
|
// If the set is empty, no element can be selected
|
||||||
|
if (setType.setSize == SetSize::Empty)
|
||||||
|
return EvaluationResult::False;
|
||||||
|
|
||||||
|
// If one of the sides has an unknown type, the result is unknown
|
||||||
|
if (elementType.domain == Domain::Unknown || setType.domain == Domain::Unknown)
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
|
||||||
|
// If both sides have the same domain, the result is unknown
|
||||||
|
if (elementType.domain == setType.domain)
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
|
||||||
|
// If one side is integer, but the other one isn’t, set inclusion is never satisfied
|
||||||
|
return EvaluationResult::False;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Not ¬_, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto result = evaluate(not_.argument, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
if (result == EvaluationResult::Error || result == EvaluationResult::Unknown)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return (result == EvaluationResult::True ? EvaluationResult::False : EvaluationResult::True);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Or &or_, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
bool someTrue = false;
|
||||||
|
bool someUnknown = false;
|
||||||
|
|
||||||
|
for (const auto &argument : or_.arguments)
|
||||||
|
{
|
||||||
|
const auto result = evaluate(argument, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
switch (result)
|
||||||
|
{
|
||||||
|
case EvaluationResult::Error:
|
||||||
|
return EvaluationResult::Error;
|
||||||
|
case EvaluationResult::True:
|
||||||
|
someTrue = true;
|
||||||
|
break;
|
||||||
|
case EvaluationResult::False:
|
||||||
|
break;
|
||||||
|
case EvaluationResult::Unknown:
|
||||||
|
someUnknown = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (someTrue)
|
||||||
|
return EvaluationResult::True;
|
||||||
|
|
||||||
|
if (someUnknown)
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
|
||||||
|
return EvaluationResult::False;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static EvaluationResult visit(const ast::Predicate &predicate, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
assert(predicate.arguments.size() == predicate.declaration->arity());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < predicate.arguments.size(); i++)
|
||||||
|
{
|
||||||
|
const auto &argument = predicate.arguments[i];
|
||||||
|
const auto ¶meter = predicate.declaration->parameters[i];
|
||||||
|
|
||||||
|
if (parameter.domain != Domain::Integer)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const auto argumentType = type(argument, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
if (argumentType.domain == Domain::Noninteger || argumentType.setSize == SetSize::Empty)
|
||||||
|
return EvaluationResult::Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EvaluationResult::Unknown;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template <class VariableDomainAccessor = DefaultVariableDomainAccessor, class... Arguments>
|
||||||
|
EvaluationResult evaluate(const ast::Formula &formula, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
return formula.accept(EvaluateFormulaVisitor<VariableDomainAccessor>(), std::forward<Arguments>(arguments)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -237,7 +237,7 @@ struct StatementVisitor
|
|||||||
const size_t arity = aritySymbol.number();
|
const size_t arity = aritySymbol.number();
|
||||||
|
|
||||||
auto functionDeclaration = context.findOrCreateFunctionDeclaration(function.name, arity);
|
auto functionDeclaration = context.findOrCreateFunctionDeclaration(function.name, arity);
|
||||||
functionDeclaration->domain = ast::Domain::Integer;
|
functionDeclaration->domain = Domain::Integer;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
157
include/anthem/Type.h
Normal file
157
include/anthem/Type.h
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
#ifndef __ANTHEM__TYPE_H
|
||||||
|
#define __ANTHEM__TYPE_H
|
||||||
|
|
||||||
|
#include <anthem/AST.h>
|
||||||
|
#include <anthem/ASTUtils.h>
|
||||||
|
#include <anthem/Utils.h>
|
||||||
|
|
||||||
|
namespace anthem
|
||||||
|
{
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
//
|
||||||
|
// Type
|
||||||
|
//
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template <class VariableDomainAccessor = DefaultVariableDomainAccessor, class... Arguments>
|
||||||
|
Type type(const ast::Term &term, Arguments &&... arguments);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template <class VariableDomainAccessor = DefaultVariableDomainAccessor>
|
||||||
|
struct TermTypeVisitor
|
||||||
|
{
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::BinaryOperation &binaryOperation, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto leftType = type<VariableDomainAccessor>(binaryOperation.left, std::forward<Arguments>(arguments)...);
|
||||||
|
const auto rightType = type<VariableDomainAccessor>(binaryOperation.right, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
// Binary operations on empty sets return an empty set (also with division)
|
||||||
|
if (leftType.setSize == SetSize::Empty || rightType.setSize == SetSize::Empty)
|
||||||
|
return {Domain::Unknown, SetSize::Empty};
|
||||||
|
|
||||||
|
// Binary operations on nonintegers return an empty set (also with division)
|
||||||
|
if (leftType.domain == Domain::Noninteger || rightType.domain == Domain::Noninteger)
|
||||||
|
return {Domain::Unknown, SetSize::Empty};
|
||||||
|
|
||||||
|
// Binary operations on unknown types return an unknown set
|
||||||
|
if (leftType.domain == Domain::Unknown || rightType.domain == Domain::Unknown)
|
||||||
|
return {Domain::Unknown, SetSize::Unknown};
|
||||||
|
|
||||||
|
// Divisions return an unknown set
|
||||||
|
if (binaryOperation.operator_ == ast::BinaryOperation::Operator::Division)
|
||||||
|
return {Domain::Integer, SetSize::Unknown};
|
||||||
|
|
||||||
|
// Binary operations on integer sets of unknown size return an integer set of unknown size
|
||||||
|
if (leftType.setSize == SetSize::Unknown || rightType.setSize == SetSize::Unknown)
|
||||||
|
return {Domain::Integer, SetSize::Unknown};
|
||||||
|
|
||||||
|
// Binary operations on integer sets with multiple elements return an integer set with multiple elements
|
||||||
|
if (leftType.setSize == SetSize::Multi || rightType.setSize == SetSize::Multi)
|
||||||
|
return {Domain::Integer, SetSize::Multi};
|
||||||
|
|
||||||
|
// Binary operations on plain integers return a plain integer
|
||||||
|
return {Domain::Integer, SetSize::Unit};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::Boolean &, Arguments &&...)
|
||||||
|
{
|
||||||
|
return {Domain::Noninteger, SetSize::Unit};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::Function &function, Arguments &&...)
|
||||||
|
{
|
||||||
|
// TODO: check that functions cannot return sets
|
||||||
|
|
||||||
|
return {function.declaration->domain, SetSize::Unit};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::Integer &, Arguments &&...)
|
||||||
|
{
|
||||||
|
return {Domain::Integer, SetSize::Unit};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::Interval &interval, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto fromType = type<VariableDomainAccessor>(interval.from, std::forward<Arguments>(arguments)...);
|
||||||
|
const auto toType = type<VariableDomainAccessor>(interval.to, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
// Intervals with empty sets return an empty set
|
||||||
|
if (fromType.setSize == SetSize::Empty || toType.setSize == SetSize::Empty)
|
||||||
|
return {Domain::Unknown, SetSize::Empty};
|
||||||
|
|
||||||
|
// Intervals with nonintegers return an empty set
|
||||||
|
if (fromType.domain == Domain::Noninteger || toType.domain == Domain::Noninteger)
|
||||||
|
return {Domain::Unknown, SetSize::Empty};
|
||||||
|
|
||||||
|
// Intervals with unknown types return an unknown set
|
||||||
|
if (fromType.domain == Domain::Unknown || toType.domain == Domain::Unknown)
|
||||||
|
return {Domain::Unknown, SetSize::Unknown};
|
||||||
|
|
||||||
|
// Intervals with integers generally return integer sets
|
||||||
|
// TODO: handle 1-element intervals such as 1..1 and empty intervals such as 2..1
|
||||||
|
return {Domain::Integer, SetSize::Unknown};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::SpecialInteger &, Arguments &&...)
|
||||||
|
{
|
||||||
|
return {Domain::Noninteger, SetSize::Unit};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::String &, Arguments &&...)
|
||||||
|
{
|
||||||
|
return {Domain::Noninteger, SetSize::Unit};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::UnaryOperation &unaryOperation, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
assert(unaryOperation.operator_ == ast::UnaryOperation::Operator::Absolute);
|
||||||
|
|
||||||
|
const auto argumentType = type<VariableDomainAccessor>(unaryOperation.argument, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
// Absolute value of an empty set returns an empty set
|
||||||
|
if (argumentType.setSize == SetSize::Empty)
|
||||||
|
return {Domain::Unknown, SetSize::Empty};
|
||||||
|
|
||||||
|
// Absolute value of nonintegers returns an empty set
|
||||||
|
if (argumentType.domain == Domain::Noninteger)
|
||||||
|
return {Domain::Unknown, SetSize::Empty};
|
||||||
|
|
||||||
|
// Absolute value of integers returns the same type
|
||||||
|
if (argumentType.domain == Domain::Integer)
|
||||||
|
return argumentType;
|
||||||
|
|
||||||
|
return {Domain::Unknown, SetSize::Unknown};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class... Arguments>
|
||||||
|
static Type visit(const ast::Variable &variable, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
const auto domain = VariableDomainAccessor()(variable, std::forward<Arguments>(arguments)...);
|
||||||
|
|
||||||
|
return {domain, SetSize::Unit};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
template <class VariableDomainAccessor, class... Arguments>
|
||||||
|
Type type(const ast::Term &term, Arguments &&... arguments)
|
||||||
|
{
|
||||||
|
return term.accept(TermTypeVisitor<VariableDomainAccessor>(), std::forward<Arguments>(arguments)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,13 +1,6 @@
|
|||||||
#ifndef __ANTHEM__UTILS_H
|
#ifndef __ANTHEM__UTILS_H
|
||||||
#define __ANTHEM__UTILS_H
|
#define __ANTHEM__UTILS_H
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <clingo.hh>
|
|
||||||
|
|
||||||
#include <anthem/Context.h>
|
|
||||||
#include <anthem/Location.h>
|
|
||||||
|
|
||||||
namespace anthem
|
namespace anthem
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -20,6 +13,7 @@ namespace anthem
|
|||||||
constexpr const auto HeadVariablePrefix = "V";
|
constexpr const auto HeadVariablePrefix = "V";
|
||||||
constexpr const auto BodyVariablePrefix = "X";
|
constexpr const auto BodyVariablePrefix = "X";
|
||||||
constexpr const auto UserVariablePrefix = "U";
|
constexpr const auto UserVariablePrefix = "U";
|
||||||
|
constexpr const auto IntegerVariablePrefix = "N";
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@ -32,6 +26,14 @@ enum class Tristate
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
enum class OperationResult
|
||||||
|
{
|
||||||
|
Unchanged,
|
||||||
|
Changed,
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
enum class EvaluationResult
|
enum class EvaluationResult
|
||||||
{
|
{
|
||||||
True,
|
True,
|
||||||
@ -42,10 +44,29 @@ enum class EvaluationResult
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
enum class OperationResult
|
enum class Domain
|
||||||
{
|
{
|
||||||
Unchanged,
|
Noninteger,
|
||||||
Changed,
|
Integer,
|
||||||
|
Unknown,
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
enum class SetSize
|
||||||
|
{
|
||||||
|
Empty,
|
||||||
|
Unit,
|
||||||
|
Multi,
|
||||||
|
Unknown,
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct Type
|
||||||
|
{
|
||||||
|
Domain domain{Domain::Unknown};
|
||||||
|
SetSize setSize{SetSize::Unknown};
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -35,6 +35,7 @@ struct PrintContext
|
|||||||
std::map<const VariableDeclaration *, size_t> userVariableIDs;
|
std::map<const VariableDeclaration *, size_t> userVariableIDs;
|
||||||
std::map<const VariableDeclaration *, size_t> headVariableIDs;
|
std::map<const VariableDeclaration *, size_t> headVariableIDs;
|
||||||
std::map<const VariableDeclaration *, size_t> bodyVariableIDs;
|
std::map<const VariableDeclaration *, size_t> bodyVariableIDs;
|
||||||
|
std::map<const VariableDeclaration *, size_t> integerVariableIDs;
|
||||||
|
|
||||||
const Context &context;
|
const Context &context;
|
||||||
};
|
};
|
||||||
@ -218,16 +219,16 @@ inline output::ColorStream &print(output::ColorStream &stream, const Integer &in
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
inline output::ColorStream &print(output::ColorStream &stream, const Interval &interval, PrintContext &printContext, bool)
|
inline output::ColorStream &print(output::ColorStream &stream, const Interval &interval, PrintContext &printContext, bool omitParentheses)
|
||||||
{
|
{
|
||||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||||
stream << "(";
|
stream << "(";
|
||||||
|
|
||||||
print(stream, interval.from, printContext);
|
print(stream, interval.from, printContext);
|
||||||
stream << "..";
|
stream << "..";
|
||||||
print(stream, interval.to, printContext);
|
print(stream, interval.to, printContext);
|
||||||
|
|
||||||
if (printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
if (!omitParentheses || printContext.context.parenthesisStyle == ParenthesisStyle::Full)
|
||||||
stream << ")";
|
stream << ")";
|
||||||
|
|
||||||
return stream;
|
return stream;
|
||||||
@ -322,24 +323,8 @@ inline output::ColorStream &print(output::ColorStream &stream, const Variable &v
|
|||||||
|
|
||||||
inline output::ColorStream &print(output::ColorStream &stream, const VariableDeclaration &variableDeclaration, PrintContext &printContext, bool)
|
inline output::ColorStream &print(output::ColorStream &stream, const VariableDeclaration &variableDeclaration, PrintContext &printContext, bool)
|
||||||
{
|
{
|
||||||
const auto domainSuffix =
|
|
||||||
[&variableDeclaration]()
|
|
||||||
{
|
|
||||||
switch (variableDeclaration.domain)
|
|
||||||
{
|
|
||||||
case Domain::Unknown:
|
|
||||||
return "";
|
|
||||||
case Domain::General:
|
|
||||||
return "g";
|
|
||||||
case Domain::Integer:
|
|
||||||
return "i";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
};
|
|
||||||
|
|
||||||
const auto printVariableDeclaration =
|
const auto printVariableDeclaration =
|
||||||
[&](const auto *prefix, auto &variableIDs) -> output::ColorStream &
|
[&stream, &variableDeclaration](const auto *prefix, auto &variableIDs) -> output::ColorStream &
|
||||||
{
|
{
|
||||||
auto matchingVariableID = variableIDs.find(&variableDeclaration);
|
auto matchingVariableID = variableIDs.find(&variableDeclaration);
|
||||||
|
|
||||||
@ -350,11 +335,14 @@ inline output::ColorStream &print(output::ColorStream &stream, const VariableDec
|
|||||||
matchingVariableID = emplaceResult.first;
|
matchingVariableID = emplaceResult.first;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto variableName = std::string(prefix) + std::to_string(matchingVariableID->second) + domainSuffix();
|
const auto variableName = std::string(prefix) + std::to_string(matchingVariableID->second);
|
||||||
|
|
||||||
return (stream << output::Variable(variableName.c_str()));
|
return (stream << output::Variable(variableName.c_str()));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (variableDeclaration.domain == Domain::Integer)
|
||||||
|
return printVariableDeclaration(IntegerVariablePrefix, printContext.integerVariableIDs);
|
||||||
|
|
||||||
switch (variableDeclaration.type)
|
switch (variableDeclaration.type)
|
||||||
{
|
{
|
||||||
case VariableDeclaration::Type::UserDefined:
|
case VariableDeclaration::Type::UserDefined:
|
||||||
|
@ -40,8 +40,36 @@ ast::Formula buildCompletedFormulaDisjunction(const ast::Predicate &predicate, c
|
|||||||
|
|
||||||
assert(otherPredicate.arguments.size() == parameters.size());
|
assert(otherPredicate.arguments.size() == parameters.size());
|
||||||
|
|
||||||
|
auto &freeVariables = scopedFormula.freeVariables;
|
||||||
|
|
||||||
// Each formula with the predicate as its consequent currently has its own copy of the predicate’s parameters
|
// Each formula with the predicate as its consequent currently has its own copy of the predicate’s parameters
|
||||||
// These need to be linked to the new, unique set of parameters
|
// These need to be linked to the new, unique set of parameters
|
||||||
|
|
||||||
|
// First, remove the free variables whose occurrences will be relinked, which is why they are no longer needed
|
||||||
|
const auto isFreeVariableUnneeded =
|
||||||
|
[&](const auto &freeVariable)
|
||||||
|
{
|
||||||
|
const auto matchesVariableToBeReplaced = std::find_if(otherPredicate.arguments.cbegin(), otherPredicate.arguments.cend(),
|
||||||
|
[&](const ast::Term &argument)
|
||||||
|
{
|
||||||
|
assert(argument.is<ast::Variable>());
|
||||||
|
const auto &otherVariable = argument.get<ast::Variable>();
|
||||||
|
|
||||||
|
return (freeVariable.get() == otherVariable.declaration);
|
||||||
|
});
|
||||||
|
|
||||||
|
return (matchesVariableToBeReplaced != otherPredicate.arguments.cend());
|
||||||
|
};
|
||||||
|
|
||||||
|
freeVariables.erase(std::remove_if(freeVariables.begin(), freeVariables.end(), isFreeVariableUnneeded), freeVariables.end());
|
||||||
|
|
||||||
|
// Currently, only rules with singleton heads are supported
|
||||||
|
// Rules with multiple elements in the head are not yet handled correctly by the head variable detection mechanism
|
||||||
|
for (const auto &freeVariable : freeVariables)
|
||||||
|
if (freeVariable->type == ast::VariableDeclaration::Type::Head)
|
||||||
|
throw CompletionException("cannot perform completion, only singleton rule heads supported currently");
|
||||||
|
|
||||||
|
// Second, link all occurrences of the deleted free variable to the new, unique parameter
|
||||||
for (size_t i = 0; i < parameters.size(); i++)
|
for (size_t i = 0; i < parameters.size(); i++)
|
||||||
{
|
{
|
||||||
assert(otherPredicate.arguments[i].is<ast::Variable>());
|
assert(otherPredicate.arguments[i].is<ast::Variable>());
|
||||||
@ -50,16 +78,6 @@ ast::Formula buildCompletedFormulaDisjunction(const ast::Predicate &predicate, c
|
|||||||
scopedFormula.formula.accept(ast::ReplaceVariableInFormulaVisitor(), scopedFormula.formula, otherVariable.declaration, parameters[i].get());
|
scopedFormula.formula.accept(ast::ReplaceVariableInFormulaVisitor(), scopedFormula.formula, otherVariable.declaration, parameters[i].get());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove all the head variables, because they are not free variables after completion
|
|
||||||
const auto isHeadVariable =
|
|
||||||
[](const auto &variableDeclaration)
|
|
||||||
{
|
|
||||||
return variableDeclaration->type == ast::VariableDeclaration::Type::Head;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto &freeVariables = scopedFormula.freeVariables;
|
|
||||||
freeVariables.erase(std::remove_if(freeVariables.begin(), freeVariables.end(), isHeadVariable), freeVariables.end());
|
|
||||||
|
|
||||||
if (freeVariables.empty())
|
if (freeVariables.empty())
|
||||||
disjunction.get<ast::Or>().arguments.emplace_back(std::move(implies.antecedent));
|
disjunction.get<ast::Or>().arguments.emplace_back(std::move(implies.antecedent));
|
||||||
else
|
else
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#include <anthem/IntegerVariableDetection.h>
|
#include <anthem/IntegerVariableDetection.h>
|
||||||
|
|
||||||
#include <anthem/Arithmetics.h>
|
|
||||||
#include <anthem/ASTCopy.h>
|
#include <anthem/ASTCopy.h>
|
||||||
#include <anthem/ASTUtils.h>
|
#include <anthem/ASTUtils.h>
|
||||||
#include <anthem/ASTVisitors.h>
|
#include <anthem/ASTVisitors.h>
|
||||||
|
#include <anthem/Evaluation.h>
|
||||||
#include <anthem/Exception.h>
|
#include <anthem/Exception.h>
|
||||||
#include <anthem/Simplification.h>
|
#include <anthem/Simplification.h>
|
||||||
|
#include <anthem/Type.h>
|
||||||
#include <anthem/Utils.h>
|
#include <anthem/Utils.h>
|
||||||
#include <anthem/output/AST.h>
|
#include <anthem/output/AST.h>
|
||||||
|
|
||||||
@ -18,19 +19,19 @@ namespace anthem
|
|||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
using VariableDomainMap = std::map<const ast::VariableDeclaration *, ast::Domain>;
|
using VariableDomainMap = std::map<const ast::VariableDeclaration *, Domain>;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
ast::Domain domain(const ast::Variable &variable, VariableDomainMap &variableDomainMap)
|
Domain domain(const ast::Variable &variable, VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
if (variable.declaration->domain != ast::Domain::Unknown)
|
if (variable.declaration->domain != Domain::Unknown)
|
||||||
return variable.declaration->domain;
|
return variable.declaration->domain;
|
||||||
|
|
||||||
const auto match = variableDomainMap.find(variable.declaration);
|
const auto match = variableDomainMap.find(variable.declaration);
|
||||||
|
|
||||||
if (match == variableDomainMap.end())
|
if (match == variableDomainMap.end())
|
||||||
return ast::Domain::Unknown;
|
return Domain::Unknown;
|
||||||
|
|
||||||
return match->second;
|
return match->second;
|
||||||
}
|
}
|
||||||
@ -40,14 +41,14 @@ ast::Domain domain(const ast::Variable &variable, VariableDomainMap &variableDom
|
|||||||
void clearVariableDomainMap(VariableDomainMap &variableDomainMap)
|
void clearVariableDomainMap(VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
for (auto &variableDeclaration : variableDomainMap)
|
for (auto &variableDeclaration : variableDomainMap)
|
||||||
variableDeclaration.second = ast::Domain::Unknown;
|
variableDeclaration.second = Domain::Unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct VariableDomainMapAccessor
|
struct VariableDomainMapAccessor
|
||||||
{
|
{
|
||||||
ast::Domain operator()(const ast::Variable &variable, VariableDomainMap &variableDomainMap)
|
Domain operator()(const ast::Variable &variable, VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
return domain(variable, variableDomainMap);
|
return domain(variable, variableDomainMap);
|
||||||
}
|
}
|
||||||
@ -55,216 +56,16 @@ struct VariableDomainMapAccessor
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
EvaluationResult isArithmetic(const ast::Term &term, VariableDomainMap &variableDomainMap)
|
Type type(const ast::Term &term, VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
return isArithmetic<VariableDomainMapAccessor>(term, variableDomainMap);
|
return type<VariableDomainMapAccessor>(term, variableDomainMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
EvaluationResult evaluate(const ast::Formula &formula, VariableDomainMap &variableDomainMap);
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct EvaluateFormulaVisitor
|
|
||||||
{
|
|
||||||
static EvaluationResult visit(const ast::And &and_, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
bool someFalse = false;
|
|
||||||
bool someUnknown = false;
|
|
||||||
|
|
||||||
for (const auto &argument : and_.arguments)
|
|
||||||
{
|
|
||||||
const auto result = evaluate(argument, variableDomainMap);
|
|
||||||
|
|
||||||
switch (result)
|
|
||||||
{
|
|
||||||
case EvaluationResult::Error:
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
case EvaluationResult::True:
|
|
||||||
break;
|
|
||||||
case EvaluationResult::False:
|
|
||||||
someFalse = true;
|
|
||||||
break;
|
|
||||||
case EvaluationResult::Unknown:
|
|
||||||
someUnknown = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (someFalse)
|
|
||||||
return EvaluationResult::False;
|
|
||||||
|
|
||||||
if (someUnknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
return EvaluationResult::True;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Biconditional &biconditional, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
const auto leftResult = evaluate(biconditional.left, variableDomainMap);
|
|
||||||
const auto rightResult = evaluate(biconditional.right, variableDomainMap);
|
|
||||||
|
|
||||||
if (leftResult == EvaluationResult::Error || rightResult == EvaluationResult::Error)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (leftResult == EvaluationResult::Unknown || rightResult == EvaluationResult::Unknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
return (leftResult == rightResult ? EvaluationResult::True : EvaluationResult::False);
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Boolean &boolean, VariableDomainMap &)
|
|
||||||
{
|
|
||||||
return (boolean.value == true ? EvaluationResult::True : EvaluationResult::False);
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Comparison &comparison, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
const auto isLeftArithmetic = isArithmetic(comparison.left, variableDomainMap);
|
|
||||||
const auto isRightArithmetic = isArithmetic(comparison.right, variableDomainMap);
|
|
||||||
|
|
||||||
if (isLeftArithmetic == EvaluationResult::Error || isRightArithmetic == EvaluationResult::Error)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (isLeftArithmetic == EvaluationResult::Unknown || isRightArithmetic == EvaluationResult::Unknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
if (isLeftArithmetic == isRightArithmetic)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
// Handle the case where one side is arithmetic but the other one isn’t
|
|
||||||
switch (comparison.operator_)
|
|
||||||
{
|
|
||||||
case ast::Comparison::Operator::Equal:
|
|
||||||
return EvaluationResult::False;
|
|
||||||
case ast::Comparison::Operator::NotEqual:
|
|
||||||
return EvaluationResult::True;
|
|
||||||
default:
|
|
||||||
// TODO: implement more cases
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Exists &exists, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
return evaluate(exists.argument, variableDomainMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::ForAll &forAll, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
return evaluate(forAll.argument, variableDomainMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Implies &implies, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
const auto antecedentResult = evaluate(implies.antecedent, variableDomainMap);
|
|
||||||
const auto consequentResult = evaluate(implies.consequent, variableDomainMap);
|
|
||||||
|
|
||||||
if (antecedentResult == EvaluationResult::Error || consequentResult == EvaluationResult::Error)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (antecedentResult == EvaluationResult::False)
|
|
||||||
return EvaluationResult::True;
|
|
||||||
|
|
||||||
if (consequentResult == EvaluationResult::True)
|
|
||||||
return EvaluationResult::True;
|
|
||||||
|
|
||||||
if (antecedentResult == EvaluationResult::True && consequentResult == EvaluationResult::False)
|
|
||||||
return EvaluationResult::False;
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::In &in, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
const auto isElementArithmetic = isArithmetic(in.element, variableDomainMap);
|
|
||||||
const auto isSetArithmetic = isArithmetic(in.set, variableDomainMap);
|
|
||||||
|
|
||||||
if (isElementArithmetic == EvaluationResult::Error || isSetArithmetic == EvaluationResult::Error)
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
|
|
||||||
if (isElementArithmetic == EvaluationResult::Unknown || isSetArithmetic == EvaluationResult::Unknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
if (isElementArithmetic == isSetArithmetic)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
// If one side is arithmetic, but the other one isn’t, set inclusion is never satisfied
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Not ¬_, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
const auto result = evaluate(not_.argument, variableDomainMap);
|
|
||||||
|
|
||||||
if (result == EvaluationResult::Error || result == EvaluationResult::Unknown)
|
|
||||||
return result;
|
|
||||||
|
|
||||||
return (result == EvaluationResult::True ? EvaluationResult::False : EvaluationResult::True);
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Or &or_, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
bool someTrue = false;
|
|
||||||
bool someUnknown = false;
|
|
||||||
|
|
||||||
for (const auto &argument : or_.arguments)
|
|
||||||
{
|
|
||||||
const auto result = evaluate(argument, variableDomainMap);
|
|
||||||
|
|
||||||
switch (result)
|
|
||||||
{
|
|
||||||
case EvaluationResult::Error:
|
|
||||||
return EvaluationResult::Error;
|
|
||||||
case EvaluationResult::True:
|
|
||||||
someTrue = true;
|
|
||||||
break;
|
|
||||||
case EvaluationResult::False:
|
|
||||||
break;
|
|
||||||
case EvaluationResult::Unknown:
|
|
||||||
someUnknown = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (someTrue)
|
|
||||||
return EvaluationResult::True;
|
|
||||||
|
|
||||||
if (someUnknown)
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
|
|
||||||
return EvaluationResult::False;
|
|
||||||
}
|
|
||||||
|
|
||||||
static EvaluationResult visit(const ast::Predicate &predicate, VariableDomainMap &variableDomainMap)
|
|
||||||
{
|
|
||||||
assert(predicate.arguments.size() == predicate.declaration->arity());
|
|
||||||
|
|
||||||
for (size_t i = 0; i < predicate.arguments.size(); i++)
|
|
||||||
{
|
|
||||||
const auto &argument = predicate.arguments[i];
|
|
||||||
const auto ¶meter = predicate.declaration->parameters[i];
|
|
||||||
|
|
||||||
if (parameter.domain != ast::Domain::Integer)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const auto isArgumentArithmetic = isArithmetic(argument, variableDomainMap);
|
|
||||||
|
|
||||||
if (isArgumentArithmetic == EvaluationResult::Error || isArgumentArithmetic == EvaluationResult::False)
|
|
||||||
return isArgumentArithmetic;
|
|
||||||
}
|
|
||||||
|
|
||||||
return EvaluationResult::Unknown;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
EvaluationResult evaluate(const ast::Formula &formula, VariableDomainMap &variableDomainMap)
|
EvaluationResult evaluate(const ast::Formula &formula, VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
return formula.accept(EvaluateFormulaVisitor(), variableDomainMap);
|
return evaluate<VariableDomainMap>(formula, variableDomainMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -392,25 +193,20 @@ struct CheckIfDefinitionFalseFunctor
|
|||||||
OperationResult operator()(ast::VariableDeclaration &variableDeclaration,
|
OperationResult operator()(ast::VariableDeclaration &variableDeclaration,
|
||||||
ast::Formula &, ast::Formula &definition, VariableDomainMap &variableDomainMap)
|
ast::Formula &, ast::Formula &definition, VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
if (variableDeclaration.domain != ast::Domain::Unknown)
|
if (variableDeclaration.domain != Domain::Unknown)
|
||||||
return OperationResult::Unchanged;
|
return OperationResult::Unchanged;
|
||||||
|
|
||||||
clearVariableDomainMap(variableDomainMap);
|
clearVariableDomainMap(variableDomainMap);
|
||||||
|
|
||||||
auto result = evaluate(definition, variableDomainMap);
|
|
||||||
|
|
||||||
if (result == EvaluationResult::Error || result == EvaluationResult::False)
|
|
||||||
return OperationResult::Unchanged;
|
|
||||||
|
|
||||||
// As a hypothesis, make the parameter’s domain noninteger
|
// As a hypothesis, make the parameter’s domain noninteger
|
||||||
variableDomainMap[&variableDeclaration] = ast::Domain::General;
|
variableDomainMap[&variableDeclaration] = Domain::Noninteger;
|
||||||
|
|
||||||
result = evaluate(definition, variableDomainMap);
|
const auto result = evaluate(definition, variableDomainMap);
|
||||||
|
|
||||||
if (result == EvaluationResult::Error || result == EvaluationResult::False)
|
if (result == EvaluationResult::Error || result == EvaluationResult::False)
|
||||||
{
|
{
|
||||||
// If making the variable noninteger leads to a false or erroneous result, it’s proven to be integer
|
// If making the variable noninteger leads to a false or erroneous result, it’s proven to be integer
|
||||||
variableDeclaration.domain = ast::Domain::Integer;
|
variableDeclaration.domain = Domain::Integer;
|
||||||
return OperationResult::Changed;
|
return OperationResult::Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -425,25 +221,20 @@ struct CheckIfQuantifiedFormulaFalseFunctor
|
|||||||
OperationResult operator()(ast::VariableDeclaration &variableDeclaration,
|
OperationResult operator()(ast::VariableDeclaration &variableDeclaration,
|
||||||
ast::Formula &quantifiedFormula, VariableDomainMap &variableDomainMap)
|
ast::Formula &quantifiedFormula, VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
if (variableDeclaration.domain != ast::Domain::Unknown)
|
if (variableDeclaration.domain != Domain::Unknown)
|
||||||
return OperationResult::Unchanged;
|
return OperationResult::Unchanged;
|
||||||
|
|
||||||
clearVariableDomainMap(variableDomainMap);
|
clearVariableDomainMap(variableDomainMap);
|
||||||
|
|
||||||
auto result = evaluate(quantifiedFormula, variableDomainMap);
|
|
||||||
|
|
||||||
if (result == EvaluationResult::Error || result == EvaluationResult::False)
|
|
||||||
return OperationResult::Unchanged;
|
|
||||||
|
|
||||||
// As a hypothesis, make the parameter’s domain noninteger
|
// As a hypothesis, make the parameter’s domain noninteger
|
||||||
variableDomainMap[&variableDeclaration] = ast::Domain::General;
|
variableDomainMap[&variableDeclaration] = Domain::Noninteger;
|
||||||
|
|
||||||
result = evaluate(quantifiedFormula, variableDomainMap);
|
const auto result = evaluate(quantifiedFormula, variableDomainMap);
|
||||||
|
|
||||||
if (result == EvaluationResult::Error || result == EvaluationResult::False)
|
if (result == EvaluationResult::Error || result == EvaluationResult::False)
|
||||||
{
|
{
|
||||||
// If making the variable noninteger leads to a false or erroneous result, it’s proven to be integer
|
// If making the variable noninteger leads to a false or erroneous result, it’s proven to be integer
|
||||||
variableDeclaration.domain = ast::Domain::Integer;
|
variableDeclaration.domain = Domain::Integer;
|
||||||
return OperationResult::Changed;
|
return OperationResult::Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -458,25 +249,20 @@ struct CheckIfCompletedFormulaTrueFunctor
|
|||||||
OperationResult operator()(ast::VariableDeclaration &variableDeclaration,
|
OperationResult operator()(ast::VariableDeclaration &variableDeclaration,
|
||||||
ast::Formula &, ast::Formula &completedFormula, VariableDomainMap &variableDomainMap)
|
ast::Formula &, ast::Formula &completedFormula, VariableDomainMap &variableDomainMap)
|
||||||
{
|
{
|
||||||
if (variableDeclaration.domain != ast::Domain::Unknown)
|
if (variableDeclaration.domain != Domain::Unknown)
|
||||||
return OperationResult::Unchanged;
|
return OperationResult::Unchanged;
|
||||||
|
|
||||||
clearVariableDomainMap(variableDomainMap);
|
clearVariableDomainMap(variableDomainMap);
|
||||||
|
|
||||||
auto result = evaluate(completedFormula, variableDomainMap);
|
|
||||||
|
|
||||||
if (result == EvaluationResult::Error || result == EvaluationResult::True)
|
|
||||||
return OperationResult::Unchanged;
|
|
||||||
|
|
||||||
// As a hypothesis, make the parameter’s domain noninteger
|
// As a hypothesis, make the parameter’s domain noninteger
|
||||||
variableDomainMap[&variableDeclaration] = ast::Domain::General;
|
variableDomainMap[&variableDeclaration] = Domain::Noninteger;
|
||||||
|
|
||||||
result = evaluate(completedFormula, variableDomainMap);
|
const auto result = evaluate(completedFormula, variableDomainMap);
|
||||||
|
|
||||||
if (result == EvaluationResult::Error || result == EvaluationResult::True)
|
if (result == EvaluationResult::Error || result == EvaluationResult::True)
|
||||||
{
|
{
|
||||||
// If making the variable noninteger leads to a false or erroneous result, it’s proven to be integer
|
// If making the variable noninteger leads to a false or erroneous result, it’s proven to be integer
|
||||||
variableDeclaration.domain = ast::Domain::Integer;
|
variableDeclaration.domain = Domain::Integer;
|
||||||
return OperationResult::Changed;
|
return OperationResult::Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
#include <anthem/Arithmetics.h>
|
|
||||||
#include <anthem/ASTCopy.h>
|
#include <anthem/ASTCopy.h>
|
||||||
#include <anthem/Equality.h>
|
#include <anthem/Equality.h>
|
||||||
#include <anthem/SimplificationVisitors.h>
|
#include <anthem/SimplificationVisitors.h>
|
||||||
|
#include <anthem/Type.h>
|
||||||
#include <anthem/output/AST.h>
|
#include <anthem/output/AST.h>
|
||||||
|
|
||||||
namespace anthem
|
namespace anthem
|
||||||
@ -524,11 +524,14 @@ struct SimplificationRuleIntegerSetInclusion
|
|||||||
|
|
||||||
auto &in = formula.get<ast::In>();
|
auto &in = formula.get<ast::In>();
|
||||||
|
|
||||||
const auto isElementInteger = isInteger(in.element);
|
const auto elementType = type(in.element);
|
||||||
const auto isSetInteger = isInteger(in.set);
|
const auto setType = type(in.set);
|
||||||
|
|
||||||
if (isElementInteger != EvaluationResult::True || isSetInteger != EvaluationResult::True)
|
if (elementType.domain != Domain::Integer || setType.domain != Domain::Integer
|
||||||
|
|| elementType.setSize != SetSize::Unit || setType.setSize != SetSize::Unit)
|
||||||
|
{
|
||||||
return OperationResult::Unchanged;
|
return OperationResult::Unchanged;
|
||||||
|
}
|
||||||
|
|
||||||
formula = ast::Formula::make<ast::Comparison>(ast::Comparison::Operator::Equal, std::move(in.element), std::move(in.set));
|
formula = ast::Formula::make<ast::Comparison>(ast::Comparison::Operator::Equal, std::move(in.element), std::move(in.set));
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ void translate(const char *fileName, std::istream &stream, Context &context)
|
|||||||
{
|
{
|
||||||
auto ¶meter = predicateDeclaration->parameters[i];
|
auto ¶meter = predicateDeclaration->parameters[i];
|
||||||
|
|
||||||
if (parameter.domain != ast::Domain::Integer)
|
if (parameter.domain != Domain::Integer)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
context.logger.outputStream()
|
context.logger.outputStream()
|
||||||
@ -155,7 +155,7 @@ void translate(const char *fileName, std::istream &stream, Context &context)
|
|||||||
<< "(" << predicateDeclaration->name
|
<< "(" << predicateDeclaration->name
|
||||||
<< "/" << output::Number(predicateDeclaration->arity())
|
<< "/" << output::Number(predicateDeclaration->arity())
|
||||||
<< "@" << output::Number(i + 1)
|
<< "@" << output::Number(i + 1)
|
||||||
<< ")." << std::endl;
|
<< ")" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ TEST_CASE("[completion] Rules are completed", "[completion]")
|
|||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() ==
|
CHECK(output.str() ==
|
||||||
"forall V1 (f(V1) <-> (exists U1 (V1 = f(f(f(f(U1)))) and f(U1)) or V1 in 1..5))\n");
|
"forall V1 (f(V1) <-> (exists U1 (V1 = f(f(f(f(U1)))) and f(U1)) or V1 in (1..5)))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("useless implications")
|
SECTION("useless implications")
|
||||||
@ -152,8 +152,8 @@ TEST_CASE("[completion] Rules are completed", "[completion]")
|
|||||||
|
|
||||||
CHECK(output.str() ==
|
CHECK(output.str() ==
|
||||||
"forall V1 (covered(V1) <-> exists U1 in(V1, U1))\n"
|
"forall V1 (covered(V1) <-> exists U1 in(V1, U1))\n"
|
||||||
"forall V2, V3 (in(V2, V3) -> (V2 in 1..n and V3 in 1..r))\n"
|
"forall V2, V3 (in(V2, V3) -> (V2 in (1..n) and V3 in (1..r)))\n"
|
||||||
"forall U2 (U2 in 1..n -> covered(U2))\n"
|
"forall U2 (U2 in (1..n) -> 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");
|
"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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,6 +190,6 @@ TEST_CASE("[completion] Rules are completed", "[completion]")
|
|||||||
input << "adj(X, Y) :- X = 1..n, Y = 1..n, |X - Y| = 1.";
|
input << "adj(X, Y) :- X = 1..n, Y = 1..n, |X - Y| = 1.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "forall V1, V2 (adj(V1, V2) <-> (V1 in 1..n and V2 in 1..n and |V1 - V2| = 1))\n");
|
CHECK(output.str() == "forall V1, V2 (adj(V1, V2) <-> (V1 in (1..n) and V2 in (1..n) and |V1 - V2| = 1))\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,7 +150,7 @@ TEST_CASE("[hidden predicate elimination] Hidden predicates are correctly elimin
|
|||||||
|
|
||||||
// TODO: simplify further
|
// TODO: simplify further
|
||||||
CHECK(output.str() ==
|
CHECK(output.str() ==
|
||||||
"forall V1 (a(V1) <-> exists U1 (c(V1) = c(U1) and U1 in 1..4))\n");
|
"forall V1 (a(V1) <-> exists U1 (c(V1) = c(U1) and U1 in (1..4)))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("simple propositions are hidden correctly")
|
SECTION("simple propositions are hidden correctly")
|
||||||
|
117
tests/TestIntegerDetection.cpp
Normal file
117
tests/TestIntegerDetection.cpp
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <anthem/AST.h>
|
||||||
|
#include <anthem/Context.h>
|
||||||
|
#include <anthem/Translation.h>
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
TEST_CASE("[integer detection] Integer variables are correctly detected", "[integer detection]")
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
context.performIntegerDetection = true;
|
||||||
|
|
||||||
|
SECTION("simple-to-detect integer parameter")
|
||||||
|
{
|
||||||
|
input << "p(X) :- X = 1..5.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall N1 (p(N1) <-> N1 in (1..5))\n"
|
||||||
|
"int(p/1@1)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("simple noninteger parameter")
|
||||||
|
{
|
||||||
|
input <<
|
||||||
|
"p(X) :- X = 1..5.\n"
|
||||||
|
"p(X) :- X = error.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall V1 (p(V1) <-> (V1 in (1..5) or V1 = error))\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("integer parameter with arithmetics")
|
||||||
|
{
|
||||||
|
input << "p(X) :- X = (2 + (1..5)) * 2.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall N1 (p(N1) <-> N1 in ((2 + (1..5)) * 2))\n"
|
||||||
|
"int(p/1@1)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("integer parameter with arithmetics depending on another integer parameter")
|
||||||
|
{
|
||||||
|
input
|
||||||
|
<< "p(X) :- X = 1..5."
|
||||||
|
<< "q(X) :- p(Y), X = (Y + 5) / 3.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall N1 (p(N1) <-> N1 in (1..5))\n"
|
||||||
|
"forall N2 (q(N2) <-> exists N3 (p(N3) and N2 in ((N3 + 5) / 3)))\n"
|
||||||
|
"int(p/1@1)\n"
|
||||||
|
"int(q/1@1)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("multiple mixed parameters")
|
||||||
|
{
|
||||||
|
input
|
||||||
|
<< "p(X) :- X = 1..5."
|
||||||
|
<< "q(X) :- X = error."
|
||||||
|
<< "r(A, B, C) :- p(X), A = X ** 2, q(B), p(C).";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall N1 (p(N1) <-> N1 in (1..5))\n"
|
||||||
|
"forall V1 (q(V1) <-> V1 = error)\n"
|
||||||
|
"forall N2, V2, N3 (r(N2, V2, N3) <-> exists N4 (p(N4) and N2 = (N4 ** 2) and q(V2) and p(N3)))\n"
|
||||||
|
"int(p/1@1)\n"
|
||||||
|
"int(r/3@1)\n"
|
||||||
|
"int(r/3@3)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("integer parameter despite usage of constant symbol")
|
||||||
|
{
|
||||||
|
input
|
||||||
|
<< "p(X) :- X = 2..n.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall N1 (p(N1) <-> N1 in (2..n))\n"
|
||||||
|
"int(p/1@1)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("integer arithmetics are correctly simplified for operators other than division")
|
||||||
|
{
|
||||||
|
input
|
||||||
|
<< "p(X) :- X = 5 + 9 ** 2.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall N1 (p(N1) <-> N1 = (5 + (9 ** 2)))\n"
|
||||||
|
"int(p/1@1)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("integer arithmetics are not simplified with the division operator")
|
||||||
|
{
|
||||||
|
input
|
||||||
|
<< "p(X) :- X = 5 + 9 / 0.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() ==
|
||||||
|
"forall N1 (p(N1) <-> N1 in (5 + (9 / 0)))\n"
|
||||||
|
"int(p/1@1)\n");
|
||||||
|
}
|
||||||
|
}
|
@ -40,7 +40,7 @@ TEST_CASE("[simplification] Rules are simplified correctly", "[simplification]")
|
|||||||
input << ":- not covered(I), I = 1..n.";
|
input << ":- not covered(I), I = 1..n.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((not covered(U1) and U1 in 1..n) -> #false)\n");
|
CHECK(output.str() == "((not covered(U1) and U1 in (1..n)) -> #false)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("comparisons")
|
SECTION("comparisons")
|
||||||
@ -50,4 +50,34 @@ TEST_CASE("[simplification] Rules are simplified correctly", "[simplification]")
|
|||||||
|
|
||||||
CHECK(output.str() == "(U1 > U2 -> #false)\n");
|
CHECK(output.str() == "(U1 > U2 -> #false)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("biconditionals are replaced with implifactions with choice rules")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "{p(a)}.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() == "forall V1 (p(V1) -> V1 = a)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("biconditionals are replaced with implifactions with complicated choice rules")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "{p(n + 5)}.";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() == "forall V1 (p(V1) -> V1 in (n + 5))\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("biconditionals are not replaced with implifactions with nonchoice rules")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "p(a).";
|
||||||
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
|
CHECK(output.str() == "forall V1 (p(V1) <-> V1 = a)\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << "p(1..5).";
|
input << "p(1..5).";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "(V1 in 1..5 -> p(V1))\n");
|
CHECK(output.str() == "(V1 in (1..5) -> p(V1))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("simple example 2")
|
SECTION("simple example 2")
|
||||||
@ -32,7 +32,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << "p(N) :- N = 1..5.";
|
input << "p(N) :- N = 1..5.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((V1 in U1 and exists X1, X2 (X1 in U1 and X2 in 1..5 and X1 = X2)) -> p(V1))\n");
|
CHECK(output.str() == "((V1 in U1 and exists X1, X2 (X1 in U1 and X2 in (1..5) and X1 = X2)) -> p(V1))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("simple example 3")
|
SECTION("simple example 3")
|
||||||
@ -48,7 +48,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << "p(N, 1, 2) :- N = 1..5.";
|
input << "p(N, 1, 2) :- N = 1..5.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((V1 in U1 and V2 in 1 and V3 in 2 and exists X1, X2 (X1 in U1 and X2 in 1..5 and X1 = X2)) -> p(V1, V2, V3))\n");
|
CHECK(output.str() == "((V1 in U1 and V2 in 1 and V3 in 2 and exists X1, X2 (X1 in U1 and X2 in (1..5) and X1 = X2)) -> p(V1, V2, V3))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("disjunctive head")
|
SECTION("disjunctive head")
|
||||||
@ -57,7 +57,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << "q(3, N); p(N, 1, 2) :- N = 1..5.";
|
input << "q(3, N); p(N, 1, 2) :- N = 1..5.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((V1 in U1 and V2 in 1 and V3 in 2 and V4 in 3 and V5 in U1 and exists X1, X2 (X1 in U1 and X2 in 1..5 and X1 = X2)) -> (p(V1, V2, V3) or q(V4, V5)))\n");
|
CHECK(output.str() == "((V1 in U1 and V2 in 1 and V3 in 2 and V4 in 3 and V5 in U1 and exists X1, X2 (X1 in U1 and X2 in (1..5) and X1 = X2)) -> (p(V1, V2, V3) or q(V4, V5)))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("disjunctive head (alternative syntax)")
|
SECTION("disjunctive head (alternative syntax)")
|
||||||
@ -66,7 +66,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << "q(3, N), p(N, 1, 2) :- N = 1..5.";
|
input << "q(3, N), p(N, 1, 2) :- N = 1..5.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((V1 in U1 and V2 in 1 and V3 in 2 and V4 in 3 and V5 in U1 and exists X1, X2 (X1 in U1 and X2 in 1..5 and X1 = X2)) -> (p(V1, V2, V3) or q(V4, V5)))\n");
|
CHECK(output.str() == "((V1 in U1 and V2 in 1 and V3 in 2 and V4 in 3 and V5 in U1 and exists X1, X2 (X1 in U1 and X2 in (1..5) and X1 = X2)) -> (p(V1, V2, V3) or q(V4, V5)))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("escaping conflicting variable names")
|
SECTION("escaping conflicting variable names")
|
||||||
@ -98,7 +98,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << ":- not p(I), I = 1..n.";
|
input << ":- not p(I), I = 1..n.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((exists X1 (X1 in U1 and not p(X1)) and exists X2, X3 (X2 in U1 and X3 in 1..n and X2 = X3)) -> #false)\n");
|
CHECK(output.str() == "((exists X1 (X1 in U1 and not p(X1)) and exists X2, X3 (X2 in U1 and X3 in (1..n) and X2 = X3)) -> #false)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("disjunctive fact (no arguments)")
|
SECTION("disjunctive fact (no arguments)")
|
||||||
@ -178,7 +178,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << "p(X, 1..10) :- q(X, 6..12).";
|
input << "p(X, 1..10) :- q(X, 6..12).";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((V1 in U1 and V2 in 1..10 and exists X1, X2 (X1 in U1 and X2 in 6..12 and q(X1, X2))) -> p(V1, V2))\n");
|
CHECK(output.str() == "((V1 in U1 and V2 in (1..10) and exists X1, X2 (X1 in U1 and X2 in (6..12) and q(X1, X2))) -> p(V1, V2))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("intervals with variable")
|
SECTION("intervals with variable")
|
||||||
@ -186,7 +186,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << ":- q(N), 1 = 1..N.";
|
input << ":- q(N), 1 = 1..N.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((exists X1 (X1 in U1 and q(X1)) and exists X2, X3 (X2 in 1 and X3 in 1..U1 and X2 = X3)) -> #false)\n");
|
CHECK(output.str() == "((exists X1 (X1 in U1 and q(X1)) and exists X2, X3 (X2 in 1 and X3 in (1..U1) and X2 = X3)) -> #false)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("intervals with two variables")
|
SECTION("intervals with two variables")
|
||||||
@ -194,7 +194,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << ":- q(M, N), M = 1..N.";
|
input << ":- q(M, N), M = 1..N.";
|
||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
CHECK(output.str() == "((exists X1, X2 (X1 in U1 and X2 in U2 and q(X1, X2)) and exists X3, X4 (X3 in U1 and X4 in 1..U2 and X3 = X4)) -> #false)\n");
|
CHECK(output.str() == "((exists X1, X2 (X1 in U1 and X2 in U2 and q(X1, X2)) and exists X3, X4 (X3 in U1 and X4 in (1..U2) and X3 = X4)) -> #false)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("comparisons")
|
SECTION("comparisons")
|
||||||
@ -262,7 +262,7 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
anthem::translate("input", input, context);
|
anthem::translate("input", input, context);
|
||||||
|
|
||||||
// TODO: eliminate V5: not needed
|
// TODO: eliminate V5: not needed
|
||||||
CHECK(output.str() == "((V1 in 1..3 and V2 in U1 and V3 in 2..4 and p(V1, V2)) -> p(V1, V2))\n((V4 in 1..3 and V5 in U2 and V6 in 2..4 and q(V6)) -> q(V6))\n");
|
CHECK(output.str() == "((V1 in (1..3) and V2 in U1 and V3 in (2..4) and p(V1, V2)) -> p(V1, V2))\n((V4 in (1..3) and V5 in U2 and V6 in (2..4) and q(V6)) -> q(V6))\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("choice rule with body")
|
SECTION("choice rule with body")
|
||||||
@ -302,6 +302,6 @@ TEST_CASE("[translation] Rules are translated correctly", "[translation]")
|
|||||||
input << "p(N, N ** N) :- N = 1..n.";
|
input << "p(N, N ** N) :- N = 1..n.";
|
||||||
anthem::translate("input", input, context);
|
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");
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
73
tests/TestUnsupported.cpp
Normal file
73
tests/TestUnsupported.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <anthem/AST.h>
|
||||||
|
#include <anthem/Context.h>
|
||||||
|
#include <anthem/Translation.h>
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
TEST_CASE("[unsupported] Errors are correctly issued when using unsupported features", "[unsupported]")
|
||||||
|
{
|
||||||
|
std::stringstream input;
|
||||||
|
std::stringstream output;
|
||||||
|
std::stringstream errors;
|
||||||
|
|
||||||
|
anthem::output::Logger logger(output, errors);
|
||||||
|
anthem::Context context(std::move(logger));
|
||||||
|
|
||||||
|
SECTION("rules with disjunctive head are unsupported")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "a; b.";
|
||||||
|
|
||||||
|
CHECK_THROWS(anthem::translate("input", input, context));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("rules with disjunctive head containing elements with arguments are unsupported")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "p(a); p(b).";
|
||||||
|
|
||||||
|
CHECK_THROWS(anthem::translate("input", input, context));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("singleton choice rules are supported")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "{a}.";
|
||||||
|
|
||||||
|
CHECK_NOTHROW(anthem::translate("input", input, context));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("singleton choice rules containing an element with arguments are supported")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "{p(a)}.";
|
||||||
|
|
||||||
|
CHECK_NOTHROW(anthem::translate("input", input, context));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("choice rules with multiple simple elements are supported")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "{a; b}.";
|
||||||
|
|
||||||
|
CHECK_NOTHROW(anthem::translate("input", input, context));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("choice rules with multiple elements with arguments are unsupported")
|
||||||
|
{
|
||||||
|
context.performCompletion = true;
|
||||||
|
|
||||||
|
input << "{p(a); p(b)}.";
|
||||||
|
|
||||||
|
CHECK_THROWS(anthem::translate("input", input, context));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user