-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathBasicProverEnvironment.java
More file actions
204 lines (182 loc) · 8.12 KB
/
BasicProverEnvironment.java
File metadata and controls
204 lines (182 loc) · 8.12 KB
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
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2020 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.java_smt.api;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.sosy_lab.java_smt.api.SolverContext.ProverOptions;
/**
* Super interface for {@link ProverEnvironment} and {@link InterpolatingProverEnvironment} that
* provides only the common operations. In most cases, just use one of the two sub-interfaces
*/
public interface BasicProverEnvironment<T> extends AutoCloseable {
/**
* Push a backtracking point and add a formula to the current stack, asserting it. The return
* value may be used to identify this formula later on in a query (this depends on the subtype of
* the environment).
*/
@Nullable
@CanIgnoreReturnValue
default T push(BooleanFormula f) throws InterruptedException {
push();
return addConstraint(f);
}
/**
* Remove one backtracking point/level from the current stack. This removes the latest level
* including all of its formulas, i.e., all formulas that were added for this backtracking point.
*/
void pop();
/** Add a constraint to the latest backtracking point. */
@Nullable
@CanIgnoreReturnValue
T addConstraint(BooleanFormula constraint) throws InterruptedException;
/**
* Create a new backtracking point, i.e., a new level on the assertion stack. Each level can hold
* several asserted formulas.
*
* <p>If formulas are added before creating the first backtracking point, they can not be removed
* via a POP-operation.
*/
void push() throws InterruptedException;
/**
* Get the number of backtracking points/levels on the current stack.
*
* <p>Caution: This is the number of PUSH-operations, and not necessarily equal to the number of
* asserted formulas. On any level there can be an arbitrary number of asserted formulas. Even
* with size of 0, formulas can already be asserted (at bottom level).
*/
int size();
/** Check whether the conjunction of all formulas on the stack is unsatisfiable. */
boolean isUnsat() throws SolverException, InterruptedException;
/**
* Check whether the conjunction of all formulas on the stack together with the list of
* assumptions is satisfiable.
*
* @param assumptions A list of literals.
*/
boolean isUnsatWithAssumptions(Collection<BooleanFormula> assumptions)
throws SolverException, InterruptedException;
/**
* Get a satisfying assignment. This method should be called only immediately after an {@link
* #isUnsat()} call that returned <code>false</code>. The returned model is guaranteed to stay
* constant and valid as long as the solver context is available, even if constraints are added
* to, pushed or popped from the prover stack.
*
* <p>A model might contain additional symbols with their evaluation, if a solver uses its own
* temporary symbols. There should be at least a value-assignment for each free symbol.
*/
Model getModel() throws SolverException;
/**
* Get a temporary view on the current satisfying assignment. This should be called only
* immediately after an {@link #isUnsat()} call that returned <code>false</code>. The evaluator
* should no longer be used as soon as any constraints are added to, pushed, or popped from the
* prover stack.
*/
default Evaluator getEvaluator() throws SolverException {
return getModel();
}
/**
* Get a list of satisfying assignments. This is equivalent to <code>
* ImmutableList.copyOf(getModel())</code>, but removes the need for calling {@link
* Model#close()}.
*
* <p>Note that if you need to iterate multiple times over the model it may be more efficient to
* use this method instead of {@link #getModel()} (depending on the solver).
*/
default ImmutableList<Model.ValueAssignment> getModelAssignments() throws SolverException {
try (Model model = getModel()) {
return model.asList();
}
}
/**
* Get an unsat core. This should be called only immediately after an {@link #isUnsat()} call that
* returned <code>false</code>.
*/
List<BooleanFormula> getUnsatCore();
/**
* Returns an UNSAT core (if it exists, otherwise {@code Optional.empty()}), over the chosen
* assumptions. Does NOT require the {@link ProverOptions#GENERATE_UNSAT_CORE} option to work, but
* {@link ProverOptions#GENERATE_UNSAT_CORE_OVER_ASSUMPTIONS}.
*
* @param assumptions Selected assumptions
* @return Empty optional if the constraints with assumptions are satisfiable, subset of
* assumptions which is unsatisfiable with the original constraints otherwise.
*/
Optional<List<BooleanFormula>> unsatCoreOverAssumptions(Collection<BooleanFormula> assumptions)
throws SolverException, InterruptedException;
/**
* Get statistics for a concrete ProverEnvironment in a solver. The returned mapping is intended
* to provide solver-internal statistics for only this instance. The keys can differ between
* individual solvers.
*
* <p>Calling the statistics several times for the same {@link ProverEnvironment}s returns
* accumulated number, i.e., we currently do not provide any possibility to reset the statistics.
* Calling the statistics for different {@link ProverEnvironment}s returns independent statistics.
*
* <p>We do not guarantee any specific key to be present, as this depends on the used solver. We
* might even return an empty mapping if the solver does not support calling this method or is in
* an invalid state.
*
* @see SolverContext#getStatistics()
*/
default ImmutableMap<String, String> getStatistics() {
return ImmutableMap.of();
}
/**
* Closes the prover environment. The object should be discarded, and should not be used after
* closing. The first call of this method will close the prover instance, further calls are
* ignored.
*/
@Override
void close();
/**
* Get all satisfying assignments of the current environment with regard to a subset of terms, and
* create a region representing all those models.
*
* @param important A set of (positive) variables appearing in the asserted queries. Only these
* variables will appear in the region.
* @return A region representing all satisfying models of the formula.
*/
<R> R allSat(AllSatCallback<R> callback, List<BooleanFormula> important)
throws InterruptedException, SolverException;
/**
* Interface for the {@link #allSat} callback.
*
* @param <R> The result type of the callback, passed through by {@link #allSat}.
*/
interface AllSatCallback<R> {
/**
* Callback for each possible satisfying assignment to given {@code important} predicates. If
* the predicate is assigned {@code true} in the model, it is returned as-is in the list, and
* otherwise it is negated.
*
* <p>There is no guarantee that the list of model values corresponds to the list in {@link
* BasicProverEnvironment#allSat}. We can reorder the variables or leave out values with a
* freely chosen value.
*/
void apply(List<BooleanFormula> model);
/** Returning the result generated after all the {@link #apply} calls went through. */
R getResult() throws InterruptedException;
}
/**
* Registers a {@link UserPropagator} for this prover environment. Only a single user propagator
* can be registered at a time, and each user propagator shall only be registered once in its
* lifetime (see also {@link UserPropagator#initializeWithBackend}).
*
* @param propagator The (fresh) user propagator to register.
* @return {@code true}, if the user propagator was successfully registered. Most SMT solvers do
* not support user propagators and hence return {@code false}.
*/
default boolean registerUserPropagator(UserPropagator propagator) {
return false;
}
}