Skip to content

Commit 037ac56

Browse files
SmtInterpol: Check for collisions before declaring new function symbols
1 parent 676bb9d commit 037ac56

2 files changed

Lines changed: 35 additions & 39 deletions

File tree

src/org/sosy_lab/java_smt/solvers/smtinterpol/FormulaCollectionScript.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import static com.google.common.base.Preconditions.checkNotNull;
1212

13+
import com.google.common.collect.FluentIterable;
1314
import de.uni_freiburg.informatik.ultimate.logic.Annotation;
1415
import de.uni_freiburg.informatik.ultimate.logic.ApplicationTerm;
1516
import de.uni_freiburg.informatik.ultimate.logic.Assignments;
@@ -28,6 +29,7 @@
2829
import java.math.BigDecimal;
2930
import java.math.BigInteger;
3031
import java.util.ArrayList;
32+
import java.util.Arrays;
3133
import java.util.Collections;
3234
import java.util.List;
3335
import java.util.Map;
@@ -70,41 +72,40 @@ public LBool assertTerm(Term pTerm) throws SMTLIBException {
7072

7173
@Override
7274
public void declareFun(String fun, Sort[] paramSorts, Sort resultSort) throws SMTLIBException {
73-
FunctionSymbol fsym = null;
74-
try {
75-
fsym = theory.getFunction(fun, paramSorts);
76-
} catch (SMTLIBException e) {
77-
// fsym = null
78-
}
79-
if (fsym == null) {
75+
var functions = theory.getDeclaredFunctions();
76+
if (!functions.containsKey(fun)) {
77+
// There is no function with that name yet: create a new symbol
8078
script.declareFun(fun, paramSorts, resultSort);
8179
} else {
82-
if (!fsym.getReturnSort().equals(resultSort)) {
80+
// A symbol with the same name already exists: Check if the signature matches and throw an
81+
// exception otherwise
82+
var decl = functions.get(fun);
83+
if (!Arrays.equals(decl.getParameterSorts(), paramSorts)
84+
|| !decl.getReturnSort().equals(resultSort)) {
8385
throw new SMTLIBException(
84-
"Function " + fun + " is already declared with different definition");
86+
"Function '%s' already declared with a different sort".formatted(fun));
8587
}
8688
}
8789
}
8890

8991
@Override
9092
public void defineFun(String fun, TermVariable[] params, Sort resultSort, Term definition)
9193
throws SMTLIBException {
92-
Sort[] paramSorts = new Sort[params.length];
93-
for (int i = 0; i < paramSorts.length; i++) {
94-
paramSorts[i] = params[i].getSort();
95-
}
96-
FunctionSymbol fsym = null;
97-
try {
98-
fsym = theory.getFunction(fun, paramSorts);
99-
} catch (SMTLIBException e) {
100-
// fsym = null
101-
}
102-
if (fsym == null) {
94+
var functions = theory.getDeclaredFunctions();
95+
if (!functions.containsKey(fun)) {
96+
// There is no function with that name yet: create a new symbol
10397
script.defineFun(fun, params, resultSort, definition);
10498
} else {
105-
if (!fsym.getDefinition().equals(definition) || !fsym.getReturnSort().equals(resultSort)) {
99+
// A symbol with the same name already exists: Check if the signature and the definition
100+
// match and throw an exception otherwise
101+
var decl = functions.get(fun);
102+
if (!Arrays.equals(
103+
decl.getParameterSorts(),
104+
FluentIterable.from(params).transform(TermVariable::getSort).toArray(Sort.class))
105+
|| !decl.getReturnSort().equals(resultSort)
106+
|| !decl.getDefinition().equals(definition)) {
106107
throw new SMTLIBException(
107-
"Function " + fun + " is already defined with different definition");
108+
"Function '%s' already declared with a different sort".formatted(fun));
108109
}
109110
}
110111
}

src/org/sosy_lab/java_smt/solvers/smtinterpol/SmtInterpolFormulaCreator.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.math.BigDecimal;
3535
import java.math.BigInteger;
3636
import java.util.ArrayList;
37+
import java.util.Arrays;
3738
import java.util.List;
3839
import java.util.Map;
3940
import org.sosy_lab.java_smt.api.ArrayFormula;
@@ -112,27 +113,21 @@ public Term makeVariable(final Sort type, final String varName) {
112113
@CanIgnoreReturnValue
113114
private FunctionSymbol declareFun(String fun, Sort[] paramSorts, Sort resultSort) {
114115
checkSymbol(fun);
115-
FunctionSymbol fsym = null;
116-
try {
117-
fsym = environment.getTheory().getFunction(fun, paramSorts);
118-
} catch (SMTLIBException e) {
119-
// fsym = null
120-
}
121-
if (fsym == null) {
122-
try {
123-
environment.declareFun(fun, paramSorts, resultSort);
124-
} catch (SMTLIBException e) {
125-
// can fail, if function is already declared with a different sort
126-
throw new IllegalArgumentException("Cannot declare function '" + fun + "'", e);
127-
}
128-
return environment.getTheory().getFunction(fun, paramSorts);
116+
var functions = environment.getTheory().getDeclaredFunctions();
117+
if (!functions.containsKey(fun)) {
118+
// There is no function with that name yet: create a new symbol
119+
environment.declareFun(fun, paramSorts, resultSort);
129120
} else {
130-
if (!fsym.getReturnSort().equals(resultSort)) {
121+
// A symbol with the same name already exists: Check if the signature matches and throw an
122+
// exception otherwise
123+
var decl = functions.get(fun);
124+
if (!Arrays.equals(decl.getParameterSorts(), paramSorts)
125+
|| !decl.getReturnSort().equals(resultSort)) {
131126
throw new IllegalArgumentException(
132-
"Function " + fun + " is already declared with different definition");
127+
"Function '%s' already declared with a different sort".formatted(fun));
133128
}
134-
return fsym;
135129
}
130+
return functions.get(fun);
136131
}
137132

138133
/**

0 commit comments

Comments
 (0)