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-08 21:01:53 +00:00
|
|
|
Symbol (void) : id_(Util::maxUnsigned()) { }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
Symbol (unsigned id) : id_(id) { }
|
|
|
|
|
|
|
|
operator unsigned (void) const { return id_; }
|
|
|
|
|
2013-02-08 21:01:53 +00:00
|
|
|
bool valid (void) const { return id_ != Util::maxUnsigned(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
static Symbol invalid (void) { return Symbol(); }
|
|
|
|
|
|
|
|
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-08 21:01:53 +00:00
|
|
|
LogVar (void) : id_(Util::maxUnsigned()) { }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
LogVar (unsigned id) : id_(id) { }
|
|
|
|
|
|
|
|
operator unsigned (void) const { return id_; }
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
LogVar& operator++ (void);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
bool valid (void) 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&
|
|
|
|
LogVar::operator++ (void)
|
|
|
|
{
|
|
|
|
assert (valid());
|
|
|
|
id_ ++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
LogVar::valid (void) const
|
|
|
|
{
|
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
|
|
|
|
|
|
|
void printSymbolDictionary (void);
|
|
|
|
|
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
|
|
|
|
|
|
|
Symbol functor (void) const { return functor_; }
|
|
|
|
|
|
|
|
Symbols args (void) const { return args_; }
|
|
|
|
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t arity (void) const { return args_.size(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2012-12-27 12:54:58 +00:00
|
|
|
bool isAtom (void) 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;
|
|
|
|
|
|
|
|
size_t nrReplacements (void) const;
|
|
|
|
|
|
|
|
LogVars getDiscardedLogVars (void) 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
|
|
|
|
Substitution::nrReplacements (void) const
|
|
|
|
{
|
|
|
|
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
|
|
|
|