datatype.h
Go to the documentation of this file.
/********************* */
#include "cvc4_public.h"
#ifndef __CVC4__DATATYPE_H
#define __CVC4__DATATYPE_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
namespace CVC4 {
// messy; Expr needs Datatype (because it's the payload of a
// CONSTANT-kinded expression), and Datatype needs Expr.
class CVC4_PUBLIC Datatype;
}/* CVC4 namespace */
#include "expr/expr.h"
#include "expr/type.h"
#include "util/hash.h"
#include "util/exception.h"
namespace CVC4 {
class CVC4_PUBLIC ExprManager;
class CVC4_PUBLIC DatatypeConstructor;
class CVC4_PUBLIC DatatypeConstructorArg;
class CVC4_PUBLIC DatatypeConstructorIterator {
const std::vector<DatatypeConstructor>* d_v;
size_t d_i;
friend class Datatype;
DatatypeConstructorIterator(const std::vector<DatatypeConstructor>& v, bool start) : d_v(&v), d_i(start ? 0 : v.size()) {
}
public:
typedef const DatatypeConstructor& value_type;
const DatatypeConstructor& operator*() const { return (*d_v)[d_i]; }
const DatatypeConstructor* operator->() const { return &(*d_v)[d_i]; }
DatatypeConstructorIterator& operator++() { ++d_i; return *this; }
DatatypeConstructorIterator operator++(int) { DatatypeConstructorIterator i(*this); ++d_i; return i; }
bool operator==(const DatatypeConstructorIterator& other) const { return d_v == other.d_v && d_i == other.d_i; }
bool operator!=(const DatatypeConstructorIterator& other) const { return d_v != other.d_v || d_i != other.d_i; }
};/* class DatatypeConstructorIterator */
class CVC4_PUBLIC DatatypeConstructorArgIterator {
const std::vector<DatatypeConstructorArg>* d_v;
size_t d_i;
friend class DatatypeConstructor;
DatatypeConstructorArgIterator(const std::vector<DatatypeConstructorArg>& v, bool start) : d_v(&v), d_i(start ? 0 : v.size()) {
}
public:
typedef const DatatypeConstructorArg& value_type;
const DatatypeConstructorArg& operator*() const { return (*d_v)[d_i]; }
const DatatypeConstructorArg* operator->() const { return &(*d_v)[d_i]; }
DatatypeConstructorArgIterator& operator++() { ++d_i; return *this; }
DatatypeConstructorArgIterator operator++(int) { DatatypeConstructorArgIterator i(*this); ++d_i; return i; }
bool operator==(const DatatypeConstructorArgIterator& other) const { return d_v == other.d_v && d_i == other.d_i; }
bool operator!=(const DatatypeConstructorArgIterator& other) const { return d_v != other.d_v || d_i != other.d_i; }
};/* class DatatypeConstructorArgIterator */
class CVC4_PUBLIC DatatypeResolutionException : public Exception {
public:
inline DatatypeResolutionException(std::string msg);
};/* class DatatypeResolutionException */
class CVC4_PUBLIC DatatypeSelfType {
};/* class DatatypeSelfType */
class CVC4_PUBLIC DatatypeUnresolvedType {
std::string d_name;
public:
inline DatatypeUnresolvedType(std::string name);
inline std::string getName() const throw();
};/* class DatatypeUnresolvedType */
class CVC4_PUBLIC DatatypeConstructorArg {
std::string d_name;
Expr d_selector;
Expr d_constructor;
bool d_resolved;
DatatypeConstructorArg(std::string name, Expr selector);
friend class DatatypeConstructor;
friend class Datatype;
bool isUnresolvedSelf() const throw();
public:
std::string getName() const throw();
Expr getSelector() const;
Expr getConstructor() const;
SelectorType getType() const;
std::string getTypeName() const;
bool isResolved() const throw();
};/* class DatatypeConstructorArg */
class CVC4_PUBLIC DatatypeConstructor {
public:
typedef DatatypeConstructorArgIterator iterator;
typedef DatatypeConstructorArgIterator const_iterator;
private:
std::string d_name;
Expr d_constructor;
Expr d_tester;
std::vector<DatatypeConstructorArg> d_args;
void resolve(ExprManager* em, DatatypeType self,
const std::map<std::string, DatatypeType>& resolutions,
const std::vector<Type>& placeholders,
const std::vector<Type>& replacements,
const std::vector< SortConstructorType >& paramTypes,
const std::vector< DatatypeType >& paramReplacements, size_t cindex)
throw(IllegalArgumentException, DatatypeResolutionException);
friend class Datatype;
Type doParametricSubstitution(Type range,
const std::vector< SortConstructorType >& paramTypes,
const std::vector< DatatypeType >& paramReplacements);
public:
explicit DatatypeConstructor(std::string name);
DatatypeConstructor(std::string name, std::string tester);
void addArg(std::string selectorName, Type selectorType);
void addArg(std::string selectorName, DatatypeUnresolvedType selectorType);
void addArg(std::string selectorName, DatatypeSelfType);
std::string getName() const throw();
Expr getConstructor() const;
Expr getTester() const;
std::string getTesterName() const throw();
inline size_t getNumArgs() const throw();
Type getSpecializedConstructorType(Type returnType) const;
Cardinality getCardinality() const throw(IllegalArgumentException);
bool isFinite() const throw(IllegalArgumentException);
bool isWellFounded() const throw(IllegalArgumentException);
Expr mkGroundTerm( Type t ) const throw(IllegalArgumentException);
inline bool isResolved() const throw();
inline iterator begin() throw();
inline iterator end() throw();
inline const_iterator begin() const throw();
inline const_iterator end() const throw();
const DatatypeConstructorArg& operator[](size_t index) const;
const DatatypeConstructorArg& operator[](std::string name) const;
Expr getSelector(std::string name) const;
bool involvesExternalType() const;
};/* class DatatypeConstructor */
class CVC4_PUBLIC Datatype {
public:
static const Datatype& datatypeOf(Expr item) CVC4_PUBLIC;
static size_t indexOf(Expr item) CVC4_PUBLIC;
static size_t cindexOf(Expr item) CVC4_PUBLIC;
typedef DatatypeConstructorIterator iterator;
typedef DatatypeConstructorIterator const_iterator;
private:
std::string d_name;
std::vector<Type> d_params;
bool d_isCo;
std::vector<DatatypeConstructor> d_constructors;
bool d_resolved;
Type d_self;
bool d_involvesExt;
// "mutable" because computing the cardinality can be expensive,
// and so it's computed just once, on demand---this is the cache
mutable Cardinality d_card;
void resolve(ExprManager* em,
const std::map<std::string, DatatypeType>& resolutions,
const std::vector<Type>& placeholders,
const std::vector<Type>& replacements,
const std::vector< SortConstructorType >& paramTypes,
const std::vector< DatatypeType >& paramReplacements)
throw(IllegalArgumentException, DatatypeResolutionException);
friend class ExprManager;// for access to resolve()
public:
inline explicit Datatype(std::string name, bool isCo = false);
inline Datatype(std::string name, const std::vector<Type>& params, bool isCo = false);
void addConstructor(const DatatypeConstructor& c);
inline std::string getName() const throw();
inline size_t getNumConstructors() const throw();
inline bool isParametric() const throw();
inline size_t getNumParameters() const throw();
inline Type getParameter( unsigned int i ) const;
inline std::vector<Type> getParameters() const;
inline bool isCodatatype() const;
Cardinality getCardinality() const throw(IllegalArgumentException);
bool isFinite() const throw(IllegalArgumentException);
bool isWellFounded() const throw(IllegalArgumentException);
Expr mkGroundTerm( Type t ) const throw(IllegalArgumentException);
DatatypeType getDatatypeType() const throw(IllegalArgumentException);
DatatypeType getDatatypeType(const std::vector<Type>& params) const throw(IllegalArgumentException);
bool operator==(const Datatype& other) const throw();
inline bool operator!=(const Datatype& other) const throw();
inline bool isResolved() const throw();
inline iterator begin() throw();
inline iterator end() throw();
inline const_iterator begin() const throw();
inline const_iterator end() const throw();
const DatatypeConstructor& operator[](size_t index) const;
const DatatypeConstructor& operator[](std::string name) const;
Expr getConstructor(std::string name) const;
bool involvesExternalType() const;
};/* class Datatype */
struct CVC4_PUBLIC DatatypeHashFunction {
inline size_t operator()(const Datatype& dt) const {
return StringHashFunction()(dt.getName());
}
inline size_t operator()(const Datatype* dt) const {
return StringHashFunction()(dt->getName());
}
inline size_t operator()(const DatatypeConstructor& dtc) const {
return StringHashFunction()(dtc.getName());
}
inline size_t operator()(const DatatypeConstructor* dtc) const {
return StringHashFunction()(dtc->getName());
}
};/* struct DatatypeHashFunction */
// FUNCTION DECLARATIONS FOR OUTPUT STREAMS
std::ostream& operator<<(std::ostream& os, const Datatype& dt) CVC4_PUBLIC;
std::ostream& operator<<(std::ostream& os, const DatatypeConstructor& ctor) CVC4_PUBLIC;
std::ostream& operator<<(std::ostream& os, const DatatypeConstructorArg& arg) CVC4_PUBLIC;
// INLINE FUNCTIONS
inline DatatypeResolutionException::DatatypeResolutionException(std::string msg) :
Exception(msg) {
}
inline DatatypeUnresolvedType::DatatypeUnresolvedType(std::string name) :
d_name(name) {
}
inline std::string DatatypeUnresolvedType::getName() const throw() {
return d_name;
}
inline Datatype::Datatype(std::string name, bool isCo) :
d_name(name),
d_params(),
d_isCo(isCo),
d_constructors(),
d_resolved(false),
d_self(),
d_involvesExt(false),
d_card(CardinalityUnknown()) {
}
inline Datatype::Datatype(std::string name, const std::vector<Type>& params, bool isCo) :
d_name(name),
d_params(params),
d_isCo(isCo),
d_constructors(),
d_resolved(false),
d_self(),
d_involvesExt(false),
d_card(CardinalityUnknown()) {
}
inline std::string Datatype::getName() const throw() {
return d_name;
}
inline size_t Datatype::getNumConstructors() const throw() {
return d_constructors.size();
}
inline bool Datatype::isParametric() const throw() {
return d_params.size() > 0;
}
inline size_t Datatype::getNumParameters() const throw() {
return d_params.size();
}
inline Type Datatype::getParameter( unsigned int i ) const {
CheckArgument(isParametric(), this, "cannot get type parameter of a non-parametric datatype");
CheckArgument(i < d_params.size(), i, "type parameter index out of range for datatype");
return d_params[i];
}
inline std::vector<Type> Datatype::getParameters() const {
CheckArgument(isParametric(), this, "cannot get type parameters of a non-parametric datatype");
return d_params;
}
inline bool Datatype::isCodatatype() const {
return d_isCo;
}
inline bool Datatype::operator!=(const Datatype& other) const throw() {
return !(*this == other);
}
inline bool Datatype::isResolved() const throw() {
return d_resolved;
}
inline Datatype::iterator Datatype::begin() throw() {
return iterator(d_constructors, true);
}
inline Datatype::iterator Datatype::end() throw() {
return iterator(d_constructors, false);
}
inline Datatype::const_iterator Datatype::begin() const throw() {
return const_iterator(d_constructors, true);
}
inline Datatype::const_iterator Datatype::end() const throw() {
return const_iterator(d_constructors, false);
}
inline bool DatatypeConstructor::isResolved() const throw() {
return !d_tester.isNull();
}
inline size_t DatatypeConstructor::getNumArgs() const throw() {
return d_args.size();
}
inline bool DatatypeConstructorArg::isResolved() const throw() {
// We could just write:
//
// return !d_selector.isNull() && d_selector.getType().isSelector();
//
// HOWEVER, this causes problems in ExprManager tear-down, because
// the attributes are removed before the pool is purged. When the
// pool is purged, this triggers an equality test between Datatypes,
// and this triggers a call to isResolved(), which breaks because
// d_selector has no type after attributes are stripped.
//
// This problem, coupled with the fact that this function is called
// _often_, means we should just use a boolean flag.
//
return d_resolved;
}
inline DatatypeConstructor::iterator DatatypeConstructor::begin() throw() {
return iterator(d_args, true);
}
inline DatatypeConstructor::iterator DatatypeConstructor::end() throw() {
return iterator(d_args, false);
}
inline DatatypeConstructor::const_iterator DatatypeConstructor::begin() const throw() {
return const_iterator(d_args, true);
}
inline DatatypeConstructor::const_iterator DatatypeConstructor::end() const throw() {
return const_iterator(d_args, false);
}
}/* CVC4 namespace */
#endif /* __CVC4__DATATYPE_H */
ExprManager
void * ExprManager
Definition c_interface_defs.h:43
CVC4::CardinalityUnknown
Representation for an unknown cardinality.
Definition cardinality.h:54
CVC4::Cardinality
A simple representation of a cardinality.
Definition cardinality.h:65
CVC4::DatatypeConstructorArgIterator
Definition datatype.h:65
CVC4::DatatypeConstructorArgIterator::value_type
const DatatypeConstructorArg & value_type
Definition datatype.h:75
CVC4::DatatypeConstructorArgIterator::operator*
const DatatypeConstructorArg & operator*() const
Definition datatype.h:76
CVC4::DatatypeConstructorArgIterator::operator++
DatatypeConstructorArgIterator & operator++()
Definition datatype.h:78
CVC4::DatatypeConstructorArgIterator::operator==
bool operator==(const DatatypeConstructorArgIterator &other) const
Definition datatype.h:80
CVC4::DatatypeConstructorArgIterator::operator!=
bool operator!=(const DatatypeConstructorArgIterator &other) const
Definition datatype.h:81
CVC4::DatatypeConstructorArgIterator::operator++
DatatypeConstructorArgIterator operator++(int)
Definition datatype.h:79
CVC4::DatatypeConstructorArgIterator::operator->
const DatatypeConstructorArg * operator->() const
Definition datatype.h:77
CVC4::DatatypeConstructorArg
A Datatype constructor argument (i.e., a Datatype field).
Definition datatype.h:119
CVC4::DatatypeConstructorArg::isResolved
bool isResolved() const
Returns true iff this constructor argument has been resolved.
Definition datatype.h:755
CVC4::DatatypeConstructorIterator
Definition datatype.h:46
CVC4::DatatypeConstructorIterator::operator++
DatatypeConstructorIterator & operator++()
Definition datatype.h:59
CVC4::DatatypeConstructorIterator::value_type
const DatatypeConstructor & value_type
Definition datatype.h:56
CVC4::DatatypeConstructorIterator::operator!=
bool operator!=(const DatatypeConstructorIterator &other) const
Definition datatype.h:62
CVC4::DatatypeConstructorIterator::operator*
const DatatypeConstructor & operator*() const
Definition datatype.h:57
CVC4::DatatypeConstructorIterator::operator==
bool operator==(const DatatypeConstructorIterator &other) const
Definition datatype.h:61
CVC4::DatatypeConstructorIterator::operator++
DatatypeConstructorIterator operator++(int)
Definition datatype.h:60
CVC4::DatatypeConstructorIterator::operator->
const DatatypeConstructor * operator->() const
Definition datatype.h:58
CVC4::DatatypeConstructor
A constructor for a Datatype.
Definition datatype.h:174
CVC4::DatatypeConstructor::addArg
void addArg(std::string selectorName, Type selectorType)
Add an argument (i.e., a data field) of the given name and type to this Datatype constructor.
CVC4::DatatypeConstructor::getNumArgs
size_t getNumArgs() const
Get the number of arguments (so far) of this Datatype constructor.
Definition datatype.h:751
CVC4::DatatypeConstructor::getName
std::string getName() const
Get the name of this Datatype constructor.
CVC4::DatatypeConstructor::begin
iterator begin()
Get the beginning iterator over DatatypeConstructor args.
Definition datatype.h:772
CVC4::DatatypeConstructor::end
iterator end()
Get the ending iterator over DatatypeConstructor args.
Definition datatype.h:776
CVC4::DatatypeConstructor::DatatypeConstructor
DatatypeConstructor(std::string name, std::string tester)
Create a new Datatype constructor with the given name for the constructor and the given name for the ...
CVC4::DatatypeConstructor::const_iterator
DatatypeConstructorArgIterator const_iterator
The (const) type for iterators over constructor arguments.
Definition datatype.h:180
CVC4::DatatypeConstructor::DatatypeConstructor
DatatypeConstructor(std::string name)
Create a new Datatype constructor with the given name for the constructor and the same name (prefixed...
CVC4::DatatypeConstructor::addArg
void addArg(std::string selectorName, DatatypeSelfType)
Add a self-referential (i.e., a data field) of the given name to this Datatype constructor that refer...
CVC4::DatatypeConstructor::iterator
DatatypeConstructorArgIterator iterator
The type for iterators over constructor arguments.
Definition datatype.h:178
CVC4::DatatypeConstructor::addArg
void addArg(std::string selectorName, DatatypeUnresolvedType selectorType)
Add an argument (i.e., a data field) of the given name to this Datatype constructor that refers to an...
CVC4::DatatypeConstructor::isResolved
bool isResolved() const
Returns true iff this Datatype constructor has already been resolved.
Definition datatype.h:747
CVC4::DatatypeResolutionException
An exception that is thrown when a datatype resolution fails.
Definition datatype.h:87
CVC4::DatatypeSelfType
A holder type (used in calls to DatatypeConstructor::addArg()) to allow a Datatype to refer to itself...
Definition datatype.h:98
CVC4::DatatypeType
Class encapsulating the datatype type.
Definition type.h:615
CVC4::DatatypeUnresolvedType
An unresolved type (used in calls to DatatypeConstructor::addArg()) to allow a Datatype to refer to i...
Definition datatype.h:109
CVC4::DatatypeUnresolvedType::DatatypeUnresolvedType
DatatypeUnresolvedType(std::string name)
Definition datatype.h:662
CVC4::DatatypeUnresolvedType::getName
std::string getName() const
Definition datatype.h:666
CVC4::Datatype
The representation of an inductive datatype.
Definition datatype.h:423
CVC4::Datatype::isParametric
bool isParametric() const
Is this datatype parametric?
Definition datatype.h:700
CVC4::Datatype::operator!=
bool operator!=(const Datatype &other) const
Return true iff the two Datatypes are not the same.
Definition datatype.h:723
CVC4::Datatype::addConstructor
void addConstructor(const DatatypeConstructor &c)
Add a constructor to this Datatype.
CVC4::Datatype::end
iterator end()
Get the ending iterator over DatatypeConstructors.
Definition datatype.h:735
CVC4::Datatype::indexOf
static size_t indexOf(Expr item)
Get the index of a constructor or tester in its datatype, or the index of a selector in its construct...
CVC4::Datatype::getParameters
std::vector< Type > getParameters() const
Get parameters.
Definition datatype.h:714
CVC4::Datatype::getNumParameters
size_t getNumParameters() const
Get the nubmer of type parameters.
Definition datatype.h:704
CVC4::Datatype::isCodatatype
bool isCodatatype() const
is this a co-datatype?
Definition datatype.h:719
CVC4::Datatype::datatypeOf
static const Datatype & datatypeOf(Expr item)
Get the datatype of a constructor, selector, or tester operator.
CVC4::Datatype::getNumConstructors
size_t getNumConstructors() const
Get the number of constructors (so far) for this Datatype.
Definition datatype.h:696
CVC4::Datatype::const_iterator
DatatypeConstructorIterator const_iterator
The (const) type for iterators over constructors.
Definition datatype.h:446
CVC4::Datatype::iterator
DatatypeConstructorIterator iterator
The type for iterators over constructors.
Definition datatype.h:444
CVC4::Datatype::Datatype
Datatype(std::string name, bool isCo=false)
Create a new Datatype of the given name.
Definition datatype.h:670
CVC4::Datatype::cindexOf
static size_t cindexOf(Expr item)
Get the index of constructor corresponding to selector.
CVC4::Datatype::getParameter
Type getParameter(unsigned int i) const
Get parameter.
Definition datatype.h:708
CVC4::Datatype::getName
std::string getName() const
Get the name of this Datatype.
Definition datatype.h:692
CVC4::Datatype::begin
iterator begin()
Get the beginning iterator over DatatypeConstructors.
Definition datatype.h:731
CVC4::Datatype::isResolved
bool isResolved() const
Return true iff this Datatype has already been resolved.
Definition datatype.h:727
CVC4::Exception
Definition exception.h:32
CVC4::ExprManager
Definition expr_manager.h:124
CVC4::Expr
Class encapsulating CVC4 expressions and methods for constructing new expressions.
Definition expr.h:227
CVC4::Expr::isNull
bool isNull() const
Check if this is a null expression.
CVC4::IllegalArgumentException
Definition exception.h:79
CVC4::SelectorType
Class encapsulating the Selector type.
Definition type.h:683
CVC4::Type
Class encapsulating CVC4 expression types.
Definition type.h:89
cvc4_public.h
Macros that should be defined everywhere during the building of the libraries and driver binary,...
CVC4_PUBLIC
#define CVC4_PUBLIC
Definition cvc4_public.h:30
exception.h
CVC4's exception base class and some associated utilities.
expr.h
hash.h
[[ Add one-line brief description here ]]
CVC4
Definition expr.h:106
CVC4::operator<<
std::ostream & operator<<(std::ostream &out, const TypeCheckingException &e)
CVC4::CheckArgument
void CheckArgument(bool cond, const T &arg, const char *fmt,...)
Definition exception.h:140
std
STL namespace.
CVC4::DatatypeHashFunction
A hash function for Datatypes.
Definition datatype.h:635
CVC4::DatatypeHashFunction::operator()
size_t operator()(const DatatypeConstructor *dtc) const
Definition datatype.h:645
CVC4::DatatypeHashFunction::operator()
size_t operator()(const Datatype &dt) const
Definition datatype.h:636
CVC4::DatatypeHashFunction::operator()
size_t operator()(const Datatype *dt) const
Definition datatype.h:639
CVC4::DatatypeHashFunction::operator()
size_t operator()(const DatatypeConstructor &dtc) const
Definition datatype.h:642
CVC4::StringHashFunction
Definition hash.h:49
type.h
Interface for expression types.