b9db8daced356e52401736428969b212565bb263
[cvc5.git] / src / parser / smt / smt.cpp
1 /********************* */
2 /*! \file smt.cpp
3 ** \verbatim
4 ** Original author: cconway
5 ** Major contributors: mdeters
6 ** Minor contributors (to current version): dejan
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010, 2011 The Analysis of Computer Systems Group (ACSys)
9 ** Courant Institute of Mathematical Sciences
10 ** New York University
11 ** See the file COPYING in the top-level source directory for licensing
12 ** information.\endverbatim
13 **
14 ** Definitions of SMT constants.
15 **/
16
17 #include <ext/hash_map>
18 namespace std {
19 using namespace __gnu_cxx;
20 }
21
22 #include "expr/type.h"
23 #include "expr/command.h"
24 #include "parser/parser.h"
25 #include "parser/smt/smt.h"
26
27 namespace CVC4 {
28 namespace parser {
29
30 std::hash_map<const std::string, Smt::Logic, CVC4::StringHashFunction> Smt::newLogicMap() {
31 std::hash_map<const std::string, Smt::Logic, CVC4::StringHashFunction> logicMap;
32 logicMap["QF_AX"] = QF_AX;
33 logicMap["QF_BV"] = QF_BV;
34 logicMap["QF_IDL"] = QF_IDL;
35 logicMap["QF_LIA"] = QF_LIA;
36 logicMap["QF_LRA"] = QF_LRA;
37 logicMap["QF_NIA"] = QF_NIA;
38 logicMap["QF_NRA"] = QF_NRA;
39 logicMap["QF_RDL"] = QF_RDL;
40 logicMap["QF_SAT"] = QF_SAT;
41 logicMap["QF_UF"] = QF_UF;
42 logicMap["QF_UFIDL"] = QF_UFIDL;
43 logicMap["QF_UFBV"] = QF_UFBV;
44 logicMap["QF_UFLRA"] = QF_UFLRA;
45 logicMap["QF_UFLIA"] = QF_UFLIA;
46 logicMap["QF_UFLIRA"] = QF_UFLIRA;
47 logicMap["QF_UFNIA"] = QF_UFNIA;
48 logicMap["QF_UFNIRA"] = QF_UFNIRA;
49 logicMap["QF_UFNRA"] = QF_UFNRA;
50 logicMap["QF_ABV"] = QF_ABV;
51 logicMap["QF_AUFBV"] = QF_AUFBV;
52 logicMap["QF_AUFBVLIA"] = QF_AUFBVLIA;
53 logicMap["QF_AUFBVLRA"] = QF_AUFBVLRA;
54 logicMap["QF_UFNIRA"] = QF_UFNIRA;
55 logicMap["QF_AUFLIA"] = QF_AUFLIA;
56 logicMap["QF_AUFLIRA"] = QF_AUFLIRA;
57 return logicMap;
58 }
59
60 Smt::Logic Smt::toLogic(const std::string& name) {
61 static std::hash_map<const std::string, Smt::Logic, CVC4::StringHashFunction> logicMap = newLogicMap();
62 return logicMap[name];
63 }
64
65 Smt::Smt(ExprManager* exprManager, Input* input, bool strictMode, bool parseOnly) :
66 Parser(exprManager,input,strictMode,parseOnly),
67 d_logicSet(false) {
68
69 // Boolean symbols are always defined
70 addOperator(kind::AND);
71 addOperator(kind::EQUAL);
72 addOperator(kind::IFF);
73 addOperator(kind::IMPLIES);
74 addOperator(kind::ITE);
75 addOperator(kind::NOT);
76 addOperator(kind::OR);
77 addOperator(kind::XOR);
78
79 }
80
81 void Smt::addArithmeticOperators() {
82 addOperator(kind::PLUS);
83 addOperator(kind::MINUS);
84 addOperator(kind::UMINUS);
85 addOperator(kind::MULT);
86 addOperator(kind::LT);
87 addOperator(kind::LEQ);
88 addOperator(kind::GT);
89 addOperator(kind::GEQ);
90 }
91
92 void Smt::addTheory(Theory theory) {
93 switch(theory) {
94 case THEORY_ARRAYS:
95 case THEORY_ARRAYS_EX: {
96 Type indexType = mkSort("Index");
97 Type elementType = mkSort("Element");
98 DeclarationSequence* seq = new DeclarationSequence();
99 seq->addCommand(new DeclareTypeCommand("Index", 0, indexType));
100 seq->addCommand(new DeclareTypeCommand("Element", 0, elementType));
101 preemptCommand(seq);
102
103 defineType("Array", getExprManager()->mkArrayType(indexType, elementType));
104
105 addOperator(kind::SELECT);
106 addOperator(kind::STORE);
107 break;
108 }
109
110 case THEORY_INT_ARRAYS_EX: {
111 defineType("Array", getExprManager()->mkArrayType(getExprManager()->integerType(), getExprManager()->integerType()));
112
113 addOperator(kind::SELECT);
114 addOperator(kind::STORE);
115 break;
116 }
117
118 case THEORY_EMPTY: {
119 Type sort = mkSort("U");
120 preemptCommand(new DeclareTypeCommand("U", 0, sort));
121 break;
122 }
123
124 case THEORY_REALS_INTS:
125 defineType("Real", getExprManager()->realType());
126 // falling-through on purpose, to add Ints part of RealsInts
127
128 case THEORY_INTS:
129 defineType("Int", getExprManager()->integerType());
130 addArithmeticOperators();
131 break;
132
133 case THEORY_REALS:
134 defineType("Real", getExprManager()->realType());
135 addArithmeticOperators();
136 break;
137
138 case THEORY_BITVECTORS:
139 break;
140
141 default:
142 Unhandled(theory);
143 }
144 }
145
146 bool Smt::logicIsSet() {
147 return d_logicSet;
148 }
149
150 inline void Smt::addUf() {
151 addTheory(Smt::THEORY_EMPTY);
152 addOperator(kind::APPLY_UF);
153 }
154
155 void Smt::setLogic(const std::string& name) {
156 d_logicSet = true;
157 d_logic = toLogic(name);
158
159 switch(d_logic) {
160 case QF_AX:
161 addTheory(THEORY_ARRAYS_EX);
162 break;
163
164 case QF_IDL:
165 case QF_LIA:
166 case QF_NIA:
167 addTheory(THEORY_INTS);
168 break;
169
170 case QF_RDL:
171 case QF_LRA:
172 case QF_NRA:
173 addTheory(THEORY_REALS);
174 break;
175
176 case QF_SAT:
177 /* no extra symbols needed */
178 break;
179
180 case QF_UFIDL:
181 case QF_UFLIA:
182 case QF_UFNIA:// nonstandard logic
183 addTheory(THEORY_INTS);
184 addUf();
185 break;
186
187 case QF_UFLRA:
188 case QF_UFNRA:
189 addTheory(THEORY_REALS);
190 addUf();
191 break;
192
193 case QF_UFLIRA:// nonstandard logic
194 case QF_UFNIRA:// nonstandard logic
195 addTheory(THEORY_INTS);
196 addTheory(THEORY_REALS);
197 addUf();
198 break;
199
200 case QF_UF:
201 addUf();
202 break;
203
204 case QF_BV:
205 addTheory(THEORY_BITVECTORS);
206 break;
207
208 case QF_ABV:
209 addTheory(THEORY_ARRAYS_EX);
210 addTheory(THEORY_BITVECTORS);
211 break;
212
213 case QF_UFBV:
214 addUf();
215 addTheory(THEORY_BITVECTORS);
216 break;
217
218 case QF_AUFBV:
219 addUf();
220 addTheory(THEORY_ARRAYS_EX);
221 addTheory(THEORY_BITVECTORS);
222 break;
223
224 case QF_AUFBVLIA:
225 addUf();
226 addTheory(THEORY_ARRAYS_EX);
227 addTheory(THEORY_BITVECTORS);
228 addTheory(THEORY_INTS);
229 break;
230
231 case QF_AUFBVLRA:
232 addUf();
233 addTheory(THEORY_ARRAYS_EX);
234 addTheory(THEORY_BITVECTORS);
235 addTheory(THEORY_REALS);
236 break;
237
238 case QF_AUFLIA:
239 addTheory(THEORY_INT_ARRAYS_EX);
240 addUf();
241 addTheory(THEORY_INTS);
242 break;
243
244 case QF_AUFLIRA:
245 addTheory(THEORY_ARRAYS_EX);
246 addUf();
247 addTheory(THEORY_INTS);
248 addTheory(THEORY_REALS);
249 break;
250
251 case AUFLIA:
252 case AUFLIRA:
253 case AUFNIRA:
254 case LRA:
255 case UFLRA:
256 case UFNIA:
257 Unhandled(name);
258 }
259 }
260
261 }/* CVC4::parser namespace */
262 }/* CVC4 namespace */