Constrained the element’s type in set element expressions to primitive terms.

This commit is contained in:
Patrick Lühne 2017-03-29 23:56:58 +02:00
parent dbb106c40b
commit 3c79625685
No known key found for this signature in database
GPG Key ID: 05F3611E97A70ABF
2 changed files with 12 additions and 11 deletions

View File

@ -14,6 +14,12 @@ namespace ast
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Terms are primitive (or arguments) if they are neither operations nor intervals
inline bool isPrimitive(const ast::Term &term)
{
return (!term.is<ast::BinaryOperation>() && !term.is<ast::Interval>());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -112,12 +118,15 @@ struct Function
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: refactor (limit element type to primitive terms)
struct In
{
In(Term &&element, Term &&set)
: element{std::move(element)},
set{std::move(set)}
{
// While the set may be any term, the element must be primitive
assert(isPrimitive(element));
}
Term element;

View File

@ -11,16 +11,6 @@ namespace anthem
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Determins whether a term is primitive
// All terms but binary operations and intervals are primitive
// With primitive terms t, “X in t” and “X = t” are equivalent
bool isPrimitiveTerm(const ast::Term &term)
{
return (!term.is<ast::BinaryOperation>() && !term.is<ast::Interval>());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Determines whether a term is a specific variable
bool matchesVariable(const ast::Term &term, const ast::Variable &variable)
{
@ -189,7 +179,9 @@ struct SimplifyFormulaVisitor : public ast::RecursiveFormulaVisitor<SimplifyForm
// Simplify formulas of type “A in B” to “A = B” if A and B are primitive
static void accept(ast::In &in, ast::Formula &formula)
{
if (!isPrimitiveTerm(in.element) || !isPrimitiveTerm(in.set))
assert(ast::isPrimitive(in.element));
if (!ast::isPrimitive(in.element) || !ast::isPrimitive(in.set))
return;
formula = ast::Comparison(ast::Comparison::Operator::Equal, std::move(in.element), std::move(in.set));