use of org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier in project java-smt by sosy-lab.
the class VariableNamesTest method testNameInQuantification.
@Test
public void testNameInQuantification() {
requireQuantifiers();
requireIntegers();
IntegerFormula var = createVariableWith(imgr::makeVariable, getVarname());
IntegerFormula zero = imgr.makeNumber(0);
BooleanFormula eq = imgr.equal(var, zero);
BooleanFormula exists = qmgr.exists(var, eq);
BooleanFormula query = bmgr.and(bmgr.not(eq), exists);
// (var != 0) & (EX var: (var == 0))
assertThat(mgr.extractVariablesAndUFs(eq)).hasSize(1);
assertThat(mgr.extractVariablesAndUFs(eq)).containsEntry(getVarname(), var);
assertThat(mgr.extractVariablesAndUFs(query)).hasSize(1);
assertThat(mgr.extractVariablesAndUFs(query)).containsEntry(getVarname(), var);
assertThat(mgr.extractVariablesAndUFs(exists)).isEmpty();
mgr.visit(query, new DefaultFormulaVisitor<Void>() {
@Override
public Void visitQuantifier(BooleanFormula pF, Quantifier pQuantifier, List<Formula> pBoundVariables, BooleanFormula pBody) {
if (solverToUse() != Solvers.PRINCESS) {
// TODO Princess does not (yet) return quantified variables.
assertThat(pBoundVariables).hasSize(1);
}
for (Formula f : pBoundVariables) {
Map<String, Formula> map = mgr.extractVariables(f);
assertThat(map).hasSize(1);
assertThat(map).containsEntry(getVarname(), f);
}
return null;
}
@Override
protected Void visitDefault(Formula pF) {
return null;
}
});
}
use of org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier in project java-smt by sosy-lab.
the class VariableNamesTest method testNameInNestedQuantification.
@Test
public void testNameInNestedQuantification() {
requireQuantifiers();
requireIntegers();
IntegerFormula var1 = createVariableWith(imgr::makeVariable, getVarname() + 1);
IntegerFormula var2 = createVariableWith(imgr::makeVariable, getVarname() + 2);
IntegerFormula var3 = createVariableWith(imgr::makeVariable, getVarname() + 3);
IntegerFormula var4 = createVariableWith(imgr::makeVariable, getVarname() + 4);
IntegerFormula zero = imgr.makeNumber(0);
// (v1 == 0) & (EX v2: ((v2 == v1) & (EX v3: ((v3 == v2) & (EX v4: (v4 == v3))))
BooleanFormula eq01 = imgr.equal(zero, var1);
BooleanFormula eq12 = imgr.equal(var1, var2);
BooleanFormula eq23 = imgr.equal(var2, var3);
BooleanFormula eq34 = imgr.equal(var3, var4);
BooleanFormula exists4 = qmgr.exists(var4, eq34);
BooleanFormula exists3 = qmgr.exists(var3, bmgr.and(eq23, exists4));
BooleanFormula exists2 = qmgr.exists(var2, bmgr.and(eq12, exists3));
BooleanFormula query = bmgr.and(eq01, exists2);
assertThat(mgr.extractVariablesAndUFs(eq01)).hasSize(1);
assertThat(mgr.extractVariablesAndUFs(eq01)).containsEntry(getVarname() + 1, var1);
assertThat(mgr.extractVariablesAndUFs(eq12)).hasSize(2);
assertThat(mgr.extractVariablesAndUFs(eq12)).containsEntry(getVarname() + 1, var1);
assertThat(mgr.extractVariablesAndUFs(eq12)).containsEntry(getVarname() + 2, var2);
assertThat(mgr.extractVariablesAndUFs(eq23)).hasSize(2);
assertThat(mgr.extractVariablesAndUFs(eq23)).containsEntry(getVarname() + 2, var2);
assertThat(mgr.extractVariablesAndUFs(eq23)).containsEntry(getVarname() + 3, var3);
assertThat(mgr.extractVariablesAndUFs(eq34)).hasSize(2);
assertThat(mgr.extractVariablesAndUFs(eq34)).containsEntry(getVarname() + 3, var3);
assertThat(mgr.extractVariablesAndUFs(eq34)).containsEntry(getVarname() + 4, var4);
assertThat(mgr.extractVariablesAndUFs(query)).hasSize(1);
assertThat(mgr.extractVariablesAndUFs(query)).containsEntry(getVarname() + 1, var1);
assertThat(mgr.extractVariablesAndUFs(exists2)).hasSize(1);
assertThat(mgr.extractVariablesAndUFs(exists2)).containsEntry(getVarname() + 1, var1);
assertThat(mgr.extractVariablesAndUFs(exists3)).hasSize(1);
assertThat(mgr.extractVariablesAndUFs(exists3)).containsEntry(getVarname() + 2, var2);
assertThat(mgr.extractVariablesAndUFs(exists4)).hasSize(1);
assertThat(mgr.extractVariablesAndUFs(exists4)).containsEntry(getVarname() + 3, var3);
mgr.visit(query, new DefaultFormulaVisitor<Void>() {
int depth = 1;
@Override
public Void visitQuantifier(BooleanFormula pF, Quantifier pQuantifier, List<Formula> pBoundVariables, BooleanFormula pBody) {
if (solverToUse() != Solvers.PRINCESS) {
// TODO Princess does not return quantified variables.
assertThat(pBoundVariables).hasSize(1);
}
for (Formula f : pBoundVariables) {
Map<String, Formula> map = mgr.extractVariables(f);
assertThat(map).hasSize(1);
assertThat(map).containsEntry(getVarname() + depth, f);
}
depth++;
return null;
}
@Override
protected Void visitDefault(Formula pF) {
return null;
}
});
}
use of org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier in project java-smt by sosy-lab.
the class UfElimination method isQuantified.
private boolean isQuantified(Formula f) {
AtomicBoolean result = new AtomicBoolean();
fmgr.visitRecursively(f, new DefaultFormulaVisitor<>() {
@Override
protected TraversalProcess visitDefault(Formula pF) {
return TraversalProcess.CONTINUE;
}
@Override
public TraversalProcess visitQuantifier(BooleanFormula pF, Quantifier pQ, List<Formula> pBoundVariables, BooleanFormula pBody) {
result.set(true);
return TraversalProcess.ABORT;
}
});
return result.get();
}
use of org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier in project java-smt by sosy-lab.
the class QuantifierManagerTest method testIntrospectionForall.
@Test
public void testIntrospectionForall() {
requireIntegers();
BooleanFormula forall = qmgr.forall(ImmutableList.of(x), a_at_x_eq_0);
final AtomicBoolean isQuantifier = new AtomicBoolean(false);
final AtomicBoolean isForall = new AtomicBoolean(false);
final AtomicInteger numBound = new AtomicInteger(0);
// Test introspection with visitors.
mgr.visit(forall, new DefaultFormulaVisitor<Void>() {
@Override
protected Void visitDefault(Formula f) {
return null;
}
@Override
public Void visitQuantifier(BooleanFormula f, Quantifier quantifier, List<Formula> boundVariables, BooleanFormula body) {
isForall.set(quantifier == Quantifier.FORALL);
isQuantifier.set(true);
numBound.set(boundVariables.size());
return null;
}
});
}
use of org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier in project java-smt by sosy-lab.
the class Z3FormulaCreator method visit.
@Override
public <R> R visit(FormulaVisitor<R> visitor, final Formula formula, final Long f) {
switch(Z3_ast_kind.fromInt(Native.getAstKind(environment, f))) {
case Z3_NUMERAL_AST:
return visitor.visitConstant(formula, convertValue(f));
case Z3_APP_AST:
int arity = Native.getAppNumArgs(environment, f);
int declKind = Native.getDeclKind(environment, Native.getAppDecl(environment, f));
if (arity == 0) {
// constants
Object value = Z3_CONSTANTS.get(declKind);
if (value != null) {
return visitor.visitConstant(formula, value);
} else if (declKind == Z3_decl_kind.Z3_OP_FPA_NUM.toInt() || Native.getSortKind(environment, Native.getSort(environment, f)) == Z3_sort_kind.Z3_ROUNDING_MODE_SORT.toInt()) {
return visitor.visitConstant(formula, convertValue(f));
} else {
// TODO: assert that.
return visitor.visitFreeVariable(formula, getAppName(f));
}
}
ImmutableList.Builder<Formula> args = ImmutableList.builder();
ImmutableList.Builder<FormulaType<?>> argTypes = ImmutableList.builder();
for (int i = 0; i < arity; i++) {
long arg = Native.getAppArg(environment, f, i);
FormulaType<?> argumentType = getFormulaType(arg);
args.add(encapsulate(argumentType, arg));
argTypes.add(argumentType);
}
return visitor.visitFunction(formula, args.build(), FunctionDeclarationImpl.of(getAppName(f), getDeclarationKind(f), argTypes.build(), getFormulaType(f), Native.getAppDecl(environment, f)));
case Z3_VAR_AST:
int deBruijnIdx = Native.getIndexValue(environment, f);
return visitor.visitBoundVariable(formula, deBruijnIdx);
case Z3_QUANTIFIER_AST:
BooleanFormula body = encapsulateBoolean(Native.getQuantifierBody(environment, f));
Quantifier q = Native.isQuantifierForall(environment, f) ? Quantifier.FORALL : Quantifier.EXISTS;
return visitor.visitQuantifier((BooleanFormula) formula, q, getBoundVars(f), body);
case Z3_SORT_AST:
case Z3_FUNC_DECL_AST:
case Z3_UNKNOWN_AST:
default:
throw new UnsupportedOperationException("Input should be a formula AST, " + "got unexpected type instead");
}
}
Aggregations