Skip to content

Commit

Permalink
Matching fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Nov 2, 2023
1 parent 5fe9189 commit 4e23f93
Show file tree
Hide file tree
Showing 30 changed files with 1,087 additions and 747 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ifeq ("$(wildcard $(3RD_PARTY_DIR)/gtest/usr/lib)","")
cd $(3RD_PARTY_DIR)/gtest; tar xvzf $(GTEST_CODE)
mkdir -p $(3RD_PARTY_DIR)/gtest/build
cd $(3RD_PARTY_DIR)/gtest/build; cmake ../$(GTEST_DIR) -DCMAKE_INSTALL_PREFIX=../usr
cd $(3RD_PARTY_DIR)/gtest/build; sudo make install
cd $(3RD_PARTY_DIR)/gtest/build; make install
rm -rf $(3RD_PARTY_DIR)/gtest/$(GTEST_DIR)
rm -rf $(3RD_PARTY_DIR)/gtest/build
endif
Expand Down
18 changes: 18 additions & 0 deletions ast/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ std::ostream &operator<<(std::ostream &out, const ExprList &el)
return out;
}

Rational::Rational() : num_(), den_() {}
Rational::Rational(Expr num, Expr den) : num_(num), den_(den) {}

member_imp(Rational, Expr, num);
member_imp(Rational, Expr, den);

bool Rational::operator==(const Rational &other) const
{
return num() == other.num() && den() == other.den();
}

std::ostream &operator<<(std::ostream &out, const Rational &r)
{
out << "(" << r.num() << "/" << r.den() << ")";

return out;
}

UnaryOp::UnaryOp() : op_(), expr_() {}
UnaryOp::UnaryOp(UnOp op, Expr expr) : op_(op), expr_(expr) {}

Expand Down
16 changes: 14 additions & 2 deletions ast/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ namespace AST {
typedef std::string Name;
typedef Util::NAT Natural;
typedef Util::MD_NAT MDNatural;
typedef Util::RATIONAL Rational;
typedef bool Boolean;
struct Rational;
struct UnaryOp;
struct BinOp;
struct Call;
Expand All @@ -62,7 +62,8 @@ struct PWLMap;
struct SBG;
struct DSBG;

typedef boost::variant<Natural, Rational, MDNatural, Boolean, Util::VariableName,
typedef boost::variant<Natural, MDNatural, Boolean, Util::VariableName,
boost::recursive_wrapper<Rational>,
boost::recursive_wrapper<UnaryOp>,
boost::recursive_wrapper<BinOp>,
boost::recursive_wrapper<Call>,
Expand All @@ -89,6 +90,17 @@ std::ostream &operator<<(std::ostream &out, const ExprList &el);
template <typename T>
inline bool is(Expr e) { return e.type() == typeid(T); }

struct Rational {
member_class(Expr, num);
member_class(Expr, den);

Rational();
Rational(Expr num, Expr den);

eq_class(Rational);
};
std::ostream &operator<<(std::ostream &out, const Rational &op);

enum class UnOp { neg };
std::ostream &operator<<(std::ostream &out, const UnOp &op);

Expand Down
31 changes: 1 addition & 30 deletions eval/visitors/eval_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,6 @@ auto connected_visitor_ = Util::Overload {
}
};

/*
auto min_reach_visitor_ = Util::Overload {
[](LIB::BaseDSBG a) {
LIB::BaseMR mr(a);
return ExprBaseType(mr.calculate().reps());
},
[](LIB::CanonDSBG a) {
LIB::CanonMR mr(a);
return ExprBaseType(mr.calculate().reps());
},
[](auto a) {
Util::ERROR("Wrong arguments for minReach");
return ExprBaseType();
}
};
*/

auto matching_visitor_ = Util::Overload {
[](LIB::BaseSBG a) {
LIB::BaseMatch match(a);
Expand Down Expand Up @@ -311,7 +294,7 @@ ExprBaseType EvalExpression::operator()(AST::Natural v) const { return v; }

ExprBaseType EvalExpression::operator()(AST::MDNatural v) const { return v; }

ExprBaseType EvalExpression::operator()(AST::Rational v) const { return v; }
ExprBaseType EvalExpression::operator()(AST::Rational v) const { return Apply(EvalRat(env_), AST::Expr(v)); }

ExprBaseType EvalExpression::operator()(AST::Boolean v) const { return v; }

Expand Down Expand Up @@ -580,18 +563,6 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const
}
break;

/*
case Eval::Func::min_reach:
if (eval_args.size() == 1) {
arity_ok = true;
SBGBaseType g = Apply(EvalGraph{}, eval_args[0]);
ExprBaseType result = std::visit(min_reach_visitor_, g);
return result;
}
break;
*/

case Eval::Func::matching:
if (eval_args.size() == 1) {
arity_ok = true;
Expand Down
4 changes: 2 additions & 2 deletions eval/visitors/eval_nat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Util::NAT EvalNat::operator()(AST::MDNatural v) const

Util::NAT EvalNat::operator()(AST::Rational v) const
{
if (v.denominator() == 1)
return v.numerator();
if (ApplyThis(v.den()) == 1)
return ApplyThis(v.num());

Util::ERROR("EvalNat: trying to evaluate a Rational");
return 0;
Expand Down
6 changes: 5 additions & 1 deletion eval/visitors/eval_rat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ Util::RATIONAL EvalRat::operator()(AST::MDNatural v) const
return Util::RATIONAL(0, 1);
}

Util::RATIONAL EvalRat::operator()(AST::Rational v) const { return v; }
Util::RATIONAL EvalRat::operator()(AST::Rational v) const
{
EvalNat visit_nat(env_);
return Util::RATIONAL(Apply(visit_nat, v.num()), Apply(visit_nat, v.den()));
}

Util::RATIONAL EvalRat::operator()(AST::Boolean v) const
{
Expand Down
1 change: 1 addition & 0 deletions eval/visitors/eval_rat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define AST_VISITOR_EVALRAT

#include "eval/defs.hpp"
#include "eval/visitors/eval_nat.hpp"

namespace SBG {

Expand Down
4 changes: 2 additions & 2 deletions parser/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
md_nat = OPAREN >> qi::lexeme[qi::ulong_long][phx::push_back(qi::_val, qi::_1)]
>> *(COMA >> qi::lexeme[qi::ulong_long])[phx::push_back(qi::_val, qi::_1)] >> CPAREN;

rational = (RAT >> OPAREN >> qi::lexeme[qi::long_long]
>> COMA >> qi::lexeme[qi::long_long] >> CPAREN)[qi::_val = phx::construct<Util::RATIONAL>(qi::_1, qi::_2)];
rational = (RAT >> OPAREN >> primary
>> COMA >> primary >> CPAREN)[qi::_val = phx::construct<AST::Rational>(qi::_1, qi::_2)];

call_exp = (ident >> function_call_args)[qi::_val = phx::construct<AST::Call>(qi::_1, qi::_2)];

Expand Down
2 changes: 1 addition & 1 deletion parser/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct ExprRule : qi::grammar<Iterator, Skipper<Iterator>, AST::ExprList()> {

// Other rules
qi::rule<Iterator, Skipper<Iterator>, Util::MD_NAT()> md_nat;
qi::rule<Iterator, Skipper<Iterator>, Util::RATIONAL()> rational;
qi::rule<Iterator, Skipper<Iterator>, AST::Rational> rational;
qi::rule<Iterator, Skipper<Iterator>, AST::Call()> call_exp;
qi::rule<Iterator, Skipper<Iterator>, AST::ExprList()> function_call_args;
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> primary;
Expand Down
7 changes: 7 additions & 0 deletions sbg/interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ Interval intersection(Interval i1, Interval i2)

// Extra operations ------------------------------------------------------------

Interval offset(Util::NAT off, Interval i)
{
Util::NAT new_b = i.begin() + off, new_e = i.end() + off;

return Interval(new_b, i.step(), new_e);
}

Interval least(Interval i1, Interval i2) { return std::min(i1, i2); }

MaybeInterval canonize(Interval i1, Interval i2)
Expand Down
1 change: 1 addition & 0 deletions sbg/interval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Interval intersection(Interval i1, Interval i2);
* @brief Extra operations.
*/

Interval offset(Util::NAT off, Interval i);
Interval least(Interval i1, Interval i2);
typedef std::optional<Interval> MaybeInterval;
MaybeInterval canonize(Interval i1, Interval i2);
Expand Down
16 changes: 16 additions & 0 deletions sbg/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ SBGMap<Set> minInv(Set d, SBGMap<Set> sbgmap)
template<typename Set>
SBGMap<Set> minInv(SBGMap<Set> sbgmap) { return minInv(sbgmap.dom(), sbgmap); }

template<typename Set>
bool isId(SBGMap<Set> sbgmap)
{
if (cardinal(sbgmap.dom()) == 1) return sbgmap.dom() == image(sbgmap);

return isId(sbgmap.exp());
}

// Function should be called on a non-empty sbgmap
template<typename Set>
unsigned int nmbrDims(SBGMap<Set> sbgmap) { return sbgmap.dom().size(); }

template<typename Set>
std::size_t hash_value(const SBGMap<Set> &sbgmap)
{
Expand All @@ -235,6 +247,8 @@ template UnordSet preImage<UnordSet>(UnordSet subdom, BaseMap sbgmap);
template BaseMap composition<UnordSet>(BaseMap sbgmap1, BaseMap sbgmap2);
template BaseMap minInv<UnordSet>(UnordSet im, BaseMap sbgmap);
template BaseMap minInv<UnordSet>(BaseMap sbgmap);
template bool isId<UnordSet>(BaseMap sbgmap);
template unsigned int nmbrDims<UnordSet>(BaseMap sbgmap);
template std::size_t hash_value<UnordSet>(const BaseMap &sbgmap);

template struct SBGMap<OrdSet>;
Expand All @@ -247,6 +261,8 @@ template OrdSet preImage<OrdSet>(OrdSet subdom, CanonMap sbgmap);
template CanonMap composition<OrdSet>(CanonMap sbgmap1, CanonMap sbgmap2);
template CanonMap minInv<OrdSet>(OrdSet im, CanonMap sbgmap);
template CanonMap minInv<OrdSet>(CanonMap sbgmap);
template bool isId<OrdSet>(CanonMap sbgmap);
template unsigned int nmbrDims<OrdSet>(CanonMap sbgmap);
template std::size_t hash_value<OrdSet>(const CanonMap &sbgmap);

} // namespace LIB
Expand Down
6 changes: 6 additions & 0 deletions sbg/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ SBGMap<Set> minInv(Set im, SBGMap<Set> sbgmap);
template<typename Set>
SBGMap<Set> minInv(SBGMap<Set> sbgmap);

template<typename Set>
bool isId(SBGMap<Set> sbgmap);

template<typename Set>
unsigned int nmbrDims(SBGMap<Set> sbgmap);

template <typename Set>
std::size_t hash_value(const SBGMap<Set> &sbgmap);

Expand Down
13 changes: 13 additions & 0 deletions sbg/multidim_inter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ MultiDimInter intersection(MultiDimInter mdi1, MultiDimInter mdi2)

// Extra operations ------------------------------------------------------------

MultiDimInter offset(Util::MD_NAT off, MultiDimInter mdi)
{
MultiDimInter res;

parallel_foreach2 (off.value_ref(), mdi.intervals_ref()) {
Util::NAT o = boost::get<0>(items);
Interval i = boost::get<1>(items);
res.emplaceBack(offset(o, i));
}

return res;
}

MultiDimInter least(MultiDimInter mdi1, MultiDimInter mdi2) { return std::min(mdi1, mdi2); }

bool isUnidim(MultiDimInter mdi) { return mdi.size() <= 1; }
Expand Down
1 change: 1 addition & 0 deletions sbg/multidim_inter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ MultiDimInter intersection(MultiDimInter mdi1, MultiDimInter mdi2);
* @brief Extra operations.
*/

MultiDimInter offset(Util::MD_NAT off, MultiDimInter mdi);
MultiDimInter least(MultiDimInter mdi1, MultiDimInter mdi2);
bool isUnidim(MultiDimInter mdi);

Expand Down
32 changes: 21 additions & 11 deletions sbg/ord_pw_mdinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ std::size_t OrdPWMDInter::size() { return pieces().size(); }

void OrdPWMDInter::emplace(SetPiece mdi) { pieces_ref().emplace(mdi); }
void OrdPWMDInter::emplace_hint(MDInterOrdSetIt it, SetPiece mdi) { pieces_ref().emplace_hint(it, mdi); }
void OrdPWMDInter::emplaceBack(SetPiece mdi) { pieces_ref().emplace_hint(pieces_ref().cend(), mdi); }

OrdPWMDInter::iterator OrdPWMDInter::begin() { return pieces_ref().begin(); }
OrdPWMDInter::iterator OrdPWMDInter::end() { return pieces_ref().end(); }
Expand Down Expand Up @@ -179,20 +180,20 @@ OrdPWMDInter cup(OrdPWMDInter pwi1, OrdPWMDInter pwi2)

if (maxElem(pwi1) <= minElem(pwi2)) {
BOOST_FOREACH (SetPiece mdi1, pwi1)
un.emplace_hint(un.end(), mdi1);
un.emplaceBack(mdi1);

BOOST_FOREACH (SetPiece mdi2, pwi2)
un.emplace_hint(un.end(), mdi2);
un.emplaceBack(mdi2);

return OrdPWMDInter(un);
}

if (maxElem(pwi2) <= minElem(pwi1)) {
BOOST_FOREACH (SetPiece mdi2, pwi2)
un.emplace_hint(un.end(), mdi2);
un.emplaceBack(mdi2);

BOOST_FOREACH (SetPiece mdi1, pwi1)
un.emplace_hint(un.end(), mdi1);
un.emplaceBack(mdi1);

return OrdPWMDInter(un);
}
Expand Down Expand Up @@ -244,24 +245,24 @@ OrdPWMDInter complementAtom(OrdPWMDInter pwi)
if (i.begin() != 0) {
Interval i_res(0, 1, i.begin() - 1);
if (!isEmpty(i_res))
c.emplace_hint(c.end(), SetPiece(i_res));
c.emplaceBack(SetPiece(i_res));
}

// "During" interval
if (i.begin() < Util::Inf) {
for (Util::NAT j = 1; j < i.step(); j++) {
Interval i_res(i.begin() + j, i.step(), i.end());
if (!isEmpty(i_res))
c.emplace_hint(c.end(), SetPiece(i_res));
c.emplaceBack(SetPiece(i_res));
}
}

// After interval
if (maxElem(i) < Util::Inf)
c.emplace_hint(c.end(), SetPiece(Interval(maxElem(i) + 1, 1, Util::Inf)));
c.emplaceBack(SetPiece(Interval(maxElem(i) + 1, 1, Util::Inf)));

else
c.emplace_hint(c.end(), SetPiece(Interval(Util::Inf)));
c.emplaceBack(SetPiece(Interval(Util::Inf)));
}

else Util::ERROR("LIB::OrdPWMDInter:complementAtom: argument should have only one element");
Expand Down Expand Up @@ -358,7 +359,16 @@ OrdPWMDInter filterSet(bool (*f)(SetPiece), OrdPWMDInter pwi)
OrdPWMDInter res;

BOOST_FOREACH (SetPiece mdi, pwi)
if (f(mdi)) res.emplace_hint(res.end(), mdi);
if (f(mdi)) res.emplaceBack(mdi);

return res;
}

OrdPWMDInter offset(Util::MD_NAT off, OrdPWMDInter pwi)
{
OrdPWMDInter res;

BOOST_FOREACH (SetPiece mdi, pwi) res.emplaceBack(offset(off, mdi));

return res;
}
Expand Down Expand Up @@ -392,7 +402,7 @@ MDInterOrdSet boundedTraverse(OrdPWMDInter pwi1, OrdPWMDInter pwi2, SetPiece (*f
BOOST_FOREACH (SetPiece mdi2, pwi2) {
SetPiece funci = func(mdi1, mdi2);
if (!isEmpty(funci))
result.emplace(funci);
result.emplace_hint(result.cend(), funci);
}
}

Expand Down Expand Up @@ -440,7 +450,7 @@ MDInterOrdSet traverse(OrdPWMDInter pwi1, OrdPWMDInter pwi2, SetPiece (*func)(Se
BOOST_FOREACH (SetPiece mdi2, pwi2) {
SetPiece funci = func(mdi1, mdi2);
if (!isEmpty(funci))
result.emplace(funci);
result.emplace_hint(result.cend(), funci);
}
}

Expand Down
2 changes: 2 additions & 0 deletions sbg/ord_pw_mdinter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct OrdPWMDInter {
std::size_t size();
void emplace(SetPiece mdi);
void emplace_hint(MDInterOrdSetIt it, SetPiece mdi);
void emplaceBack(SetPiece mdi);
iterator begin();
iterator end();
SetPiece operator[](std::size_t n);
Expand Down Expand Up @@ -146,6 +147,7 @@ MDInterOrdSet canonize(MDInterOrdSet ii);
OrdPWMDInter concatenation(OrdPWMDInter pwi1, OrdPWMDInter pwi2);

OrdPWMDInter filterSet(bool (*f)(SetPiece), OrdPWMDInter pwi);
OrdPWMDInter offset(Util::MD_NAT off, OrdPWMDInter pwi);

/** @function boundedTraverse
*
Expand Down
Loading

0 comments on commit 4e23f93

Please sign in to comment.