2013-02-07 17:50:02 +00:00
|
|
|
#ifndef YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_
|
|
|
|
#define YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <string>
|
|
|
|
#include <ostream>
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
#include "TinySet.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class Symbol {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
2013-02-28 19:45:37 +00:00
|
|
|
Symbol() : id_(Util::maxUnsigned()) { }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
Symbol (unsigned id) : id_(id) { }
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
operator unsigned() const { return id_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool valid() const { return id_ != Util::maxUnsigned(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static Symbol invalid() { return Symbol(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
private:
|
2013-02-07 22:37:45 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream&, const Symbol&);
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
unsigned id_;
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class LogVar {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVar() : id_(Util::maxUnsigned()) { }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
LogVar (unsigned id) : id_(id) { }
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
operator unsigned() const { return id_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVar& operator++();
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool valid() const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
private:
|
2013-02-07 22:37:45 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream&, const LogVar&);
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
unsigned id_;
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
inline LogVar&
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVar::operator++()
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
|
|
|
assert (valid());
|
|
|
|
id_ ++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVar::valid() const
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
return id_ != Util::maxUnsigned();
|
2013-02-06 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
namespace std {
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
template <> struct hash<Horus::Symbol> {
|
|
|
|
size_t operator() (const Horus::Symbol& s) const {
|
2012-05-23 14:56:01 +01:00
|
|
|
return std::hash<unsigned>() (s);
|
2013-02-08 00:56:42 +00:00
|
|
|
}};
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
template <> struct hash<Horus::LogVar> {
|
|
|
|
size_t operator() (const Horus::LogVar& X) const {
|
2012-05-23 14:56:01 +01:00
|
|
|
return std::hash<unsigned>() (X);
|
2013-02-08 00:56:42 +00:00
|
|
|
}};
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-07 23:53:13 +00:00
|
|
|
} // namespace std
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
typedef std::vector<Symbol> Symbols;
|
|
|
|
typedef std::vector<Symbol> Tuple;
|
|
|
|
typedef std::vector<Tuple> Tuples;
|
|
|
|
typedef std::vector<LogVar> LogVars;
|
|
|
|
typedef TinySet<Symbol> SymbolSet;
|
|
|
|
typedef TinySet<LogVar> LogVarSet;
|
|
|
|
typedef TinySet<Tuple> TupleSet;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
std::ostream& operator<< (std::ostream&, const Tuple&);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace LiftedUtils {
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
Symbol getSymbol (const std::string&);
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void printSymbolDictionary();
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class Ground {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
|
|
|
Ground (Symbol f) : functor_(f) { }
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
Ground (Symbol f, const Symbols& args)
|
|
|
|
: functor_(f), args_(args) { }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
Symbol functor() const { return functor_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
Symbols args() const { return args_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
size_t arity() const { return args_.size(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool isAtom() const { return args_.empty(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
private:
|
2013-02-07 22:37:45 +00:00
|
|
|
friend std::ostream& operator<< (std::ostream&, const Ground&);
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
Symbol functor_;
|
|
|
|
Symbols args_;
|
|
|
|
};
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
typedef std::vector<Ground> Grounds;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class Substitution {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
2013-02-06 00:24:02 +00:00
|
|
|
void add (LogVar X_old, LogVar X_new);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
void rename (LogVar X_old, LogVar X_new);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
LogVar newNameFor (LogVar X) const;
|
|
|
|
|
|
|
|
bool containsReplacementFor (LogVar X) const;
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
size_t nrReplacements() const;
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
LogVars getDiscardedLogVars() const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
private:
|
2013-02-07 13:37:15 +00:00
|
|
|
friend std::ostream& operator<< (
|
2013-02-07 22:37:45 +00:00
|
|
|
std::ostream&, const Substitution&);
|
2013-02-06 00:24:02 +00:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
std::unordered_map<LogVar, LogVar> subs_;
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
Substitution::add (LogVar X_old, LogVar X_new)
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (Util::contains (subs_, X_old) == false);
|
2013-02-07 13:37:15 +00:00
|
|
|
subs_.insert (std::make_pair (X_old, X_new));
|
2013-02-06 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
Substitution::rename (LogVar X_old, LogVar X_new)
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (Util::contains (subs_, X_old));
|
2013-02-06 00:24:02 +00:00
|
|
|
subs_.find (X_old)->second = X_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline LogVar
|
|
|
|
Substitution::newNameFor (LogVar X) const
|
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::unordered_map<LogVar, LogVar>::const_iterator it;
|
2013-02-06 00:24:02 +00:00
|
|
|
it = subs_.find (X);
|
|
|
|
if (it != subs_.end()) {
|
|
|
|
return subs_.find (X)->second;
|
|
|
|
}
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
Substitution::containsReplacementFor (LogVar X) const
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
return Util::contains (subs_, X);
|
2013-02-06 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline size_t
|
2013-02-28 19:45:37 +00:00
|
|
|
Substitution::nrReplacements() const
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
|
|
|
return subs_.size();
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-08 00:20:01 +00:00
|
|
|
#endif // YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_
|
2012-05-23 14:56:01 +01:00
|
|
|
|