ilasynth  1.0
ILASynth: Template-based ILA Synthesis Engine
bool.hpp
Go to the documentation of this file.
1 #ifndef __AST_BOOL_HPP_DEFINED__
2 #define __AST_BOOL_HPP_DEFINED__
3 
4 #include <boost/multiprecision/cpp_int.hpp>
5 #include <boost/python.hpp>
6 #include <boost/shared_ptr.hpp>
7 #include <iostream>
8 #include <string>
9 #include <vector>
10 
11 #include <assert.h>
12 #include <ilasynth/ast/node.hpp>
13 #include <ilasynth/type.hpp>
14 #include <z3++.h>
15 
16 #include <ilasynth/ast.hpp>
17 
18 namespace ilasynth {
19 class Abstraction;
20 
21 // ---------------------------------------------------------------------- //
22 // Boolean expressions are derived from this class.
23 class BoolExpr : public Node {
24 public:
25  // constructor.
26  BoolExpr();
27  // constructor needed for ChoiceExpr
28  BoolExpr(NodeType t);
29  // destructor.
30  virtual ~BoolExpr();
31 };
32 
33 // ---------------------------------------------------------------------- //
34 // Boolean variables.
35 class BoolVar : public BoolExpr {
36 public:
37  BoolVar(const std::string& name);
38  virtual ~BoolVar();
39  virtual Node* clone() const;
40  virtual bool equal(const Node* that) const;
41  virtual std::ostream& write(std::ostream& out) const;
42 };
43 
44 // ---------------------------------------------------------------------- //
45 // Boolean constants.
46 class BoolConst : public BoolExpr {
47 protected:
48  static nptr_t true_node;
50 
51  bool value;
52 
53 private:
54  BoolConst(bool value);
55  BoolConst(int value);
56  BoolConst(const mp_int_t& l);
57 
58 public:
59  virtual ~BoolConst();
60  virtual Node* clone() const;
61  virtual bool equal(const Node* that) const;
62  virtual boost::python::object getValue() const;
63  virtual std::ostream& write(std::ostream& out) const;
64 
65  static nptr_t get(bool v) {
66  if (v)
67  return true_node;
68  else
69  return false_node;
70  }
71 
72  // helper functions.
73  bool val() const { return value; }
74 };
75 
76 // ---------------------------------------------------------------------- //
77 // Boolean operators.
78 class BoolOp : public BoolExpr {
79 public:
80  // Number of operands.
82 
83  // What is the operation?
84  enum Op {
85  // invalid
87  // unary
88  NOT,
89  // binary.
90  AND,
91  OR,
92  XOR,
95  NOR,
97  SLT,
98  SGT,
99  SLE,
107  // ternary
109  } op;
110 
111  static const std::string operatorNames[];
112 
113  // the operands themselves.
115 
116 private:
117  // Don't forget to update these helper functions below.
118  static bool isUnary(Op op) { return op >= NOT && op <= NOT; }
119  static bool isBinary(Op op) { return op >= AND && op <= DISTINCT; }
120  static bool isTernary(Op op) { return op >= IF && op <= IF; }
121  // helper functions to check argument types.
122  static bool checkUnaryOpTypes(Op op, const nptr_t& n);
123  static bool checkBinaryOpTypes(Op op, const nptr_t& n1, const nptr_t& n2);
124  static int checkTernaryOpTypes(Op op, nptr_vec_t args_);
125 
126 public:
127  // constructors.
128  BoolOp(Op op, const nptr_t& n1);
129  BoolOp(Op op, const nptr_t& n1, const nptr_t& n2);
130  BoolOp(Op op, nptr_vec_t& args_);
131  // kind of like a copy constructor, but use a fresh set of args.
132  BoolOp(const BoolOp* other, nptr_vec_t& args_);
133  // destructors.
134  virtual ~BoolOp();
135 
136  // clone.
137  virtual Node* clone() const;
138 
139  // equal.
140  virtual bool equal(const Node* that) const;
141 
142  // stream output.
143  virtual std::ostream& write(std::ostream& out) const;
144 
145  // number of operands.
146  virtual unsigned nArgs() const;
147 
148  // operand i.
149  virtual nptr_t arg(unsigned i) const;
150 
151  // operation.
152  Op getOp() const { return op; }
153 
154  // negate this boolean expression.
155  static nptr_t& negate(const nptr_t& n, rwmap_t& cache);
156 };
157 } // namespace ilasynth
158 #endif
Definition: bool.hpp:94
Definition: bool.hpp:100
static const std::string operatorNames[]
Definition: bool.hpp:111
static bool isUnary(Op op)
Definition: bool.hpp:118
Definition: bool.hpp:105
Definition: bool.hpp:96
Definition: bool.hpp:99
static nptr_t true_node
Definition: bool.hpp:48
Definition: bool.hpp:97
Definition: bool.hpp:95
Definition: bool.hpp:106
Definition: bool.hpp:90
static nptr_t get(bool v)
Definition: bool.hpp:65
virtual bool equal(const Node *that) const
virtual Node * clone() const
Definition: bool.hpp:93
Arity
Definition: bool.hpp:81
static int checkTernaryOpTypes(Op op, nptr_vec_t args_)
virtual bool equal(const Node *that) const
static nptr_t & negate(const nptr_t &n, rwmap_t &cache)
static bool checkUnaryOpTypes(Op op, const nptr_t &n)
Definition: type.hpp:11
Definition: bool.hpp:104
Definition: bool.hpp:81
std::vector< nptr_t > nptr_vec_t
Definition: node.hpp:27
Definition: bool.hpp:81
std::string name
Definition: node.hpp:67
bool val() const
Definition: bool.hpp:73
boost::multiprecision::cpp_int mp_int_t
Definition: common.hpp:9
Definition: bool.hpp:92
Op
Definition: bool.hpp:84
Definition: bool.hpp:103
virtual Node * clone() const
Definition: bool.hpp:78
Definition: bool.hpp:86
BoolVar(const std::string &name)
virtual nptr_t arg(unsigned i) const
Definition: bool.hpp:91
Definition: bool.hpp:81
virtual std::ostream & write(std::ostream &out) const
boost::shared_ptr< Node > nptr_t
Definition: node.hpp:24
Definition: bool.hpp:46
Definition: bool.hpp:102
bool value
Definition: bool.hpp:51
Definition: bool.hpp:98
Definition: bool.hpp:23
enum ilasynth::BoolOp::Op op
virtual bool equal(const Node *that) const
Definition: abstraction.hpp:21
Definition: node.hpp:55
Definition: bool.hpp:108
virtual std::ostream & write(std::ostream &out) const
static nptr_t false_node
Definition: bool.hpp:49
virtual unsigned nArgs() const
virtual std::ostream & write(std::ostream &out) const
Definition: bool.hpp:35
BoolConst(bool value)
virtual Node * clone() const
nptr_vec_t args
Definition: bool.hpp:114
Definition: bool.hpp:101
virtual ~BoolOp()
Definition: bool.hpp:88
static bool checkBinaryOpTypes(Op op, const nptr_t &n1, const nptr_t &n2)
static bool isBinary(Op op)
Definition: bool.hpp:119
static bool isTernary(Op op)
Definition: bool.hpp:120
Op getOp() const
Definition: bool.hpp:152
virtual boost::python::object getValue() const
virtual ~BoolVar()
BoolOp(Op op, const nptr_t &n1)
enum ilasynth::BoolOp::Arity arity
std::unordered_map< const Node *, nptr_t, decltype(&nodeHash), decltype(&nodeEqual)> rwmap_t
Definition: node.hpp:177