1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* @file FormulaVarIterator.cpp
* Implements a class FormulaVarIterator that iterates
* over free variables in a formula or a term.
*
* @since 06/01/2004, Manchester
* @since 02/09/2009 Redmond, reimplemented to work with non-rectified
* formulas and return each variable only once
* @since 15/05/2015 Gothenburg, FOOL support added
*/
#include "FormulaVarIterator.hpp"
using namespace Lib;
using namespace Kernel;
/**
* Build an iterator over f.
* @since 02/09/2009 Redmond
*/
FormulaVarIterator::FormulaVarIterator(const Formula* f)
: _found(false)
{
_instructions.push(FVI_FORMULA);
_formulas.push(f);
} // FormulaVarIterator::FormulaVarIterator
/**
* Build an iterator over a term.
* @since 15/05/2015 Gothenburg
*/
FormulaVarIterator::FormulaVarIterator(const Term* t)
: _found(false)
{
_instructions.push(FVI_TERM);
_terms.push(t);
} // FormulaVarIterator::FormulaVarIterator
/**
* Build an iterator over a list of terms.
* @since 15/05/2015 Gothenburg
*/
FormulaVarIterator::FormulaVarIterator(const TermList ts)
: _found(false)
{
_instructions.push(FVI_TERM_LIST);
_termLists.push(ts);
} // FormulaVarIterator::FormulaVarIterator
/**
* Return the next free variable.
* @since 06/01/2004 Manchester
*/
unsigned FormulaVarIterator::next()
{
ASS(_found);
_found = false;
return _nextVar;
} // FormulaVarIterator::next
/**
* True if there is the next free variable.
* @since 06/01/2004 Manchester
* @since 11/12/2004 Manchester, true and false added
* @since 15/05/2015 Gothenburg, FOOL support added
*/
bool FormulaVarIterator::hasNext()
{
if (_found) return true;
while (_instructions.isNonEmpty()) {
switch (_instructions.pop()) {
case FVI_FORMULA: {
const Formula* f = _formulas.pop();
switch (f->connective()) {
case LITERAL: {
Literal* lit = const_cast<Literal*>(f->literal());
if(lit->isTwoVarEquality()){
_instructions.push(FVI_TERM_LIST);
_termLists.push(lit->twoVarEqSort());
}
_instructions.push(FVI_TERM);
_terms.push(lit);
break;
}
case AND:
case OR: {
FormulaList::Iterator fs(f->args());
while (fs.hasNext()) {
_instructions.push(FVI_FORMULA);
_formulas.push(fs.next());
}
break;
}
case IMP:
case IFF:
case XOR:
_instructions.push(FVI_FORMULA);
_formulas.push(f->left());
_instructions.push(FVI_FORMULA);
_formulas.push(f->right());
break;
case NOT:
_instructions.push(FVI_FORMULA);
_formulas.push(f->uarg());
break;
case FORALL:
case EXISTS:
_instructions.push(FVI_UNBIND);
_instructions.push(FVI_FORMULA);
_formulas.push(f->qarg());
_instructions.push(FVI_BIND);
_vars.push(f->vars());
break;
case BOOL_TERM:
_instructions.push(FVI_TERM_LIST);
_termLists.push(f->getBooleanTerm());
break;
case TRUE:
case FALSE:
case NAME:
break;
default:
ASSERTION_VIOLATION;
}
break;
}
case FVI_TERM: {
const Term* t = _terms.pop();
// TODO: is there a better iterator over arguments of const Term*?
Term::Iterator ts(t);
while (ts.hasNext()) {
_instructions.push(FVI_TERM_LIST);
_termLists.push(ts.next());
}
if (t->isSpecial()) {
const Term::SpecialTermData* sd = t->getSpecialData();
switch (t->specialFunctor()) {
case SpecialFunctor::ITE:
_instructions.push(FVI_FORMULA);
_formulas.push(sd->getITECondition());
_instructions.push(FVI_TERM_LIST);
_termLists.push(sd->getSort());
break;
case SpecialFunctor::FORMULA:
_instructions.push(FVI_FORMULA);
_formulas.push(sd->getFormula());
break;
case SpecialFunctor::LET: {
_instructions.push(FVI_FORMULA);
_formulas.push(sd->getLetBinding());
_instructions.push(FVI_TERM_LIST);
_termLists.push(sd->getSort());
break;
}
case SpecialFunctor::LAMBDA:{
_instructions.push(FVI_UNBIND);
SList* sorts = sd->getLambdaVarSorts();
while(sorts){
_instructions.push(FVI_TERM_LIST);
_termLists.push(sorts->head());
sorts = sorts->tail();
}
_instructions.push(FVI_TERM_LIST);
_termLists.push(sd->getLambdaExpSort());
_instructions.push(FVI_TERM_LIST);
_termLists.push(sd->getLambdaExp());
_instructions.push(FVI_BIND);
_vars.push(sd->getLambdaVars());
break;
}
case SpecialFunctor::MATCH: {
for (unsigned int i = 0; i < t->arity(); i++) {
_instructions.push(FVI_TERM_LIST);
_termLists.push(*t->nthArgument(i));
}
_instructions.push(FVI_TERM_LIST);
_termLists.push(sd->getMatchedSort());
_instructions.push(FVI_TERM_LIST);
_termLists.push(sd->getSort());
break;
}
}
}
break;
}
case FVI_TERM_LIST: {
TermList ts = _termLists.pop();
if (ts.isVar()) {
unsigned var = ts.var();
if (!_free.get(var) && !_bound.get(var)) {
_nextVar = var;
_found = true;
_free[var] = true;
return true;
}
} else {
_instructions.push(FVI_TERM);
_terms.push(ts.term());
}
break;
}
case FVI_BIND: {
VList::Iterator vs(_vars.top());
while (vs.hasNext()) {
_bound[vs.next()]++;
}
break;
}
case FVI_UNBIND: {
VList::Iterator vs(_vars.pop());
while (vs.hasNext()) {
_bound[vs.next()]--;
}
break;
}
}
}
return false;
} // FormulaVarIterator::hasNext