Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String formulae #250

Merged
merged 59 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
0520718
Initial support for String formulae
serras Aug 17, 2021
e66962a
Run formatting
serras Aug 17, 2021
5ff9e5b
Return BooleanFormula on equals
serras Aug 17, 2021
0c37762
Do not use the string theory until Features.S is enabled
serras Aug 17, 2021
5eae506
Fix style problem
serras Aug 17, 2021
abfba6c
SMTInterpol doesn't really support strings
serras Aug 24, 2021
991b737
Update UFElimination procedure
serras Aug 24, 2021
64896de
Finish removal of SMTInterpol string support
serras Aug 24, 2021
4509941
Another reversal
serras Aug 24, 2021
5808d2c
Merge remote-tracking branch 'sosy-lab/master'
serras Oct 29, 2021
75cc067
Add regular expression support + more tests
serras Oct 29, 2021
e5fd0be
fix: add forgotten method for encapsulating Regex formulas.
kfriedberger Nov 13, 2021
70936a6
formatting.
kfriedberger Nov 13, 2021
94c04ea
fix type of empty and full regex.
kfriedberger Nov 13, 2021
4220e30
add more operations for String theory: prefix and suffix.
kfriedberger Nov 13, 2021
238ce26
add more operations for String theory: allow concatenation of several…
kfriedberger Nov 13, 2021
b1a622e
update Readme with upcoming support for String and Regex theory.
kfriedberger Nov 13, 2021
1be548c
improve assertion message.
kfriedberger Nov 13, 2021
587cdf0
move JUnit tests for Strings into a separate class.
kfriedberger Nov 13, 2021
49a52ef
formatting Junit test
kfriedberger Nov 13, 2021
95ab89f
replace merged imports by single-class imports, and ignore too many p…
kfriedberger Nov 13, 2021
88fa084
cleanup
kfriedberger Nov 14, 2021
a184697
add support for String theory with CVC4.
kfriedberger Nov 14, 2021
00fc6ee
add more operations for String theory: indexOf, contains, charAt, sub…
kfriedberger Nov 14, 2021
cce8309
Remove allChar + translation from Java regex
serras Nov 17, 2021
bab867b
fixing GEQ,LEQ,LT for String theory, and shortening some code.
kfriedberger Nov 17, 2021
1281b86
remove unused test for removed method "allChar()".
kfriedberger Nov 17, 2021
1d4c133
add more operations for String theory: IntToString and StringToInt.
kfriedberger Nov 17, 2021
685bf4c
more junit tests for String theory.
kfriedberger Nov 18, 2021
a562121
fix Checkstyle warning.
kfriedberger Nov 18, 2021
a89caac
More tests for String equality constraints + negative length
baierd Nov 19, 2021
92357b0
Add String length inequality tests for < <= > >=
baierd Nov 19, 2021
67de78a
Enable CVC4 to use all String methods by setting its options
baierd Nov 19, 2021
d0d7d71
Enable Strings in CVC4 to use unicode by default
baierd Nov 19, 2021
6c71f93
More tests for charAt, unicode and lexicographic ordering + fix a test
baierd Nov 19, 2021
9e27bdd
fix String comparison in Z3.
kfriedberger Nov 19, 2021
90f8b38
more JUnit tests
kfriedberger Nov 19, 2021
2728ed5
introduce variable to apply a common operation only once.
kfriedberger Nov 19, 2021
d849dfb
fix model retrieval for Strings in CVC4.
kfriedberger Nov 19, 2021
ab3f235
remove unused variables.
kfriedberger Nov 19, 2021
48f7357
Add startIndex to indexOf in Strings according to SMTLIB2 standard
baierd Nov 20, 2021
6722500
More tests for concat, substring, indexOf, contains
baierd Nov 20, 2021
64da1c3
Add more subString tests
baierd Nov 21, 2021
f0867f4
Add more tests for substring based on variables
baierd Nov 21, 2021
dcf86e0
Add tests for String constants for replace() and replaceAll()
baierd Nov 21, 2021
1cb177d
Add more tests for replace() and replaceAll() for String theories
baierd Nov 26, 2021
820c90f
Fix a bug that StringFormulas used the BooleanFormula type
baierd Nov 26, 2021
b2c623d
Add StringFormulas in the type selection of the
baierd Nov 26, 2021
241a91a
Add a basic String Array test
baierd Nov 26, 2021
425632f
Add tests to check whether or not a solver supports all possible Array
baierd Nov 26, 2021
f885645
Add basic tests for quantifiers with strings and array int strings
baierd Nov 26, 2021
0659d05
Add more general array type combination tests
baierd Nov 26, 2021
f8c351e
remove API methods for building a regex from Java-based regex or patt…
kfriedberger Nov 26, 2021
93e53b8
formatting.
kfriedberger Nov 26, 2021
81167e1
adding basic visitor support for String and Regex theory.
kfriedberger Nov 27, 2021
499275d
improve JUnit test for model evaluation.
kfriedberger Nov 27, 2021
ff4c5b1
improve JUnit test for String theory.
kfriedberger Nov 27, 2021
2e46c9d
improve JUnit test for String theory, fix warnings.
kfriedberger Nov 27, 2021
284eb40
CVC4: disable some JUnit tests for theory combination of Arrays and Q…
kfriedberger Nov 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ivy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ SPDX-License-Identifier: Apache-2.0
<dependency org="com.puppycrawl.tools" name="checkstyle" rev="8.43" conf="checkstyle->default"/>

<!-- SmtInterpol -->
<dependency org="de.uni-freiburg.informatik.ultimate" name="smtinterpol" rev="2.5-732-gd208e931" conf="runtime-smtinterpol->master; contrib->sources"/>
<dependency org="de.uni-freiburg.informatik.ultimate" name="smtinterpol" rev="2.5-842-gfcd46532" conf="runtime-smtinterpol->master; contrib->sources"/>

<!-- Princess for our Maven release -->
<dependency org="io.github.uuverifiers" name="princess_2.13" rev="2021-08-12" conf="runtime-princess-with-javacup->default; contrib->sources"/>
Expand Down
7 changes: 7 additions & 0 deletions src/org/sosy_lab/java_smt/api/FormulaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public interface FormulaManager {
*/
QuantifiedFormulaManager getQuantifiedFormulaManager();

/**
* Returns the String Theory.
*
* @throws UnsupportedOperationException If the theory is not supported by the solver.
*/
StringFormulaManager getStringFormulaManager();

/**
* Create variable of the type equal to {@code formulaType}.
*
Expand Down
25 changes: 25 additions & 0 deletions src/org/sosy_lab/java_smt/api/FormulaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public boolean isSLType() {
return false;
}

public boolean isStringType() {
return false;
}

@Override
public abstract String toString();

Expand Down Expand Up @@ -344,6 +348,25 @@ public String toSMTLIBString() {
}
}

public static final FormulaType<BooleanFormula> StringType =
new FormulaType<>() {

@Override
public boolean isStringType() {
return true;
}

@Override
public String toString() {
return "String";
}

@Override
public String toSMTLIBString() {
return "String";
}
};

/**
* Parse a string and return the corresponding type. This method is the counterpart of {@link
* #toString()}.
Expand All @@ -355,6 +378,8 @@ public static FormulaType<?> fromString(String t) {
return IntegerType;
} else if (RationalType.toString().equals(t)) {
return RationalType;
} else if (StringType.toString().equals(t)) {
return StringType;
} else if (FloatingPointRoundingModeType.toString().equals(t)) {
return FloatingPointRoundingModeType;
} else if (t.startsWith("FloatingPoint<")) {
Expand Down
8 changes: 8 additions & 0 deletions src/org/sosy_lab/java_smt/api/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ public interface Model extends Iterable<ValueAssignment>, AutoCloseable {
@Nullable
BigInteger evaluate(BitvectorFormula f);

/**
* Type-safe evaluation for string formulas.
*
* <p>The formula does not need to be a variable, we also allow complex expression.
*/
@Nullable
String evaluate(StringFormula f);

/**
* Iterate over all values present in the model. Note that iterating multiple times may be
* inefficient for some solvers, it is recommended to use {@link
Expand Down
15 changes: 15 additions & 0 deletions src/org/sosy_lab/java_smt/api/StringFormula.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2021 Alejandro Serrano Mena
//
// SPDX-License-Identifier: Apache-2.0

package org.sosy_lab.java_smt.api;

import com.google.errorprone.annotations.Immutable;

/** A formula of the string sort. */
@Immutable
public interface StringFormula extends Formula {}
34 changes: 34 additions & 0 deletions src/org/sosy_lab/java_smt/api/StringFormulaManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2021 Alejandro Serrano Mena
//
// SPDX-License-Identifier: Apache-2.0

package org.sosy_lab.java_smt.api;

/** Manager for dealing with string formulas. */
public interface StringFormulaManager {

/**
* Returns a {@link StringFormula} representing the given constant.
*
* @param value the boolean value the returned <code>Formula</code> should represent
* @return a Formula representing the given value
*/
StringFormula makeString(String value);

/**
* Creates a variable with exactly the given name.
*
* <p>Please make sure that the given name is valid in SMT-LIB2. Take a look at {@link
* FormulaManager#isValidName} for further information.
*
* <p>This method does not quote or unquote the given name, but uses the plain name "AS IS".
* {@link Formula#toString} can return a different String than the given one.
*/
StringFormula makeVariable(String pVar);

BooleanFormula equal(StringFormula str1, StringFormula str2);
}
kfriedberger marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 9 additions & 0 deletions src/org/sosy_lab/java_smt/basicimpl/AbstractFormula.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.sosy_lab.java_smt.api.FormulaType;
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
import org.sosy_lab.java_smt.api.StringFormula;

/**
* A Formula represented as a TFormulaInfo object.
Expand Down Expand Up @@ -132,4 +133,12 @@ static final class RationalFormulaImpl<TFormulaInfo> extends AbstractFormula<TFo
super(pTerm);
}
}

/** Simple StringFormula implementation. */
static final class StringFormulaImpl<TFormulaInfo> extends AbstractFormula<TFormulaInfo>
implements StringFormula {
StringFormulaImpl(TFormulaInfo pT) {
super(pT);
}
}
}
18 changes: 17 additions & 1 deletion src/org/sosy_lab/java_smt/basicimpl/AbstractFormulaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.sosy_lab.java_smt.api.IntegerFormulaManager;
import org.sosy_lab.java_smt.api.RationalFormulaManager;
import org.sosy_lab.java_smt.api.SLFormulaManager;
import org.sosy_lab.java_smt.api.StringFormulaManager;
import org.sosy_lab.java_smt.api.Tactic;
import org.sosy_lab.java_smt.api.visitors.FormulaTransformationVisitor;
import org.sosy_lab.java_smt.api.visitors.FormulaVisitor;
Expand Down Expand Up @@ -117,6 +118,8 @@ public abstract class AbstractFormulaManager<TFormulaInfo, TType, TEnv, TFuncDec

private final @Nullable AbstractSLFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl> slManager;

private final @Nullable StringFormulaManager strManager;

private final FormulaCreator<TFormulaInfo, TType, TEnv, TFuncDecl> formulaCreator;

/** Builds a solver from the given theory implementations. */
Expand All @@ -135,7 +138,8 @@ protected AbstractFormulaManager(
@Nullable
AbstractQuantifiedFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl> quantifiedManager,
@Nullable AbstractArrayFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl> arrayManager,
@Nullable AbstractSLFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl> slManager) {
@Nullable AbstractSLFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl> slManager,
@Nullable StringFormulaManager strManager) {
kfriedberger marked this conversation as resolved.
Show resolved Hide resolved

this.arrayManager = arrayManager;
this.quantifiedManager = quantifiedManager;
Expand All @@ -146,6 +150,7 @@ protected AbstractFormulaManager(
this.bitvectorManager = bitvectorManager;
this.floatingPointManager = floatingPointManager;
this.slManager = slManager;
this.strManager = strManager;
this.formulaCreator = pFormulaCreator;

checkArgument(
Expand Down Expand Up @@ -230,6 +235,14 @@ public ArrayFormulaManager getArrayFormulaManager() {
return arrayManager;
}

@Override
public StringFormulaManager getStringFormulaManager() {
if (strManager == null) {
throw new UnsupportedOperationException("Solver does not support string theory");
}
return strManager;
}

public abstract Appender dumpFormula(TFormulaInfo t);

@Override
Expand Down Expand Up @@ -391,6 +404,9 @@ public <T extends Formula> T makeVariable(FormulaType<T> formulaType, String nam
} else if (formulaType.isRationalType()) {
assert rationalManager != null;
t = rationalManager.makeVariable(name);
} else if (formulaType.isStringType()) {
assert strManager != null;
t = strManager.makeVariable(name);
} else if (formulaType.isBitvectorType()) {
assert bitvectorManager != null;
t = bitvectorManager.makeVariable((BitvectorType) formulaType, name);
Expand Down
7 changes: 7 additions & 0 deletions src/org/sosy_lab/java_smt/basicimpl/AbstractModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.sosy_lab.java_smt.api.Model;
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
import org.sosy_lab.java_smt.api.StringFormula;

@SuppressWarnings("ClassTypeParameterName")
public abstract class AbstractModel<TFormulaInfo, TType, TEnv> implements Model {
Expand Down Expand Up @@ -56,6 +57,12 @@ public Boolean evaluate(BooleanFormula f) {
return (Boolean) evaluateImpl(creator.extractInfo(f));
}

@Nullable
@Override
public String evaluate(StringFormula f) {
return (String) evaluateImpl(creator.extractInfo(f));
}

@Nullable
@Override
public BigInteger evaluate(BitvectorFormula f) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2021 Alejandro SerranoMena
//
// SPDX-License-Identifier: Apache-2.0

package org.sosy_lab.java_smt.basicimpl;

import static org.sosy_lab.java_smt.basicimpl.AbstractFormulaManager.checkVariableName;

import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.StringFormula;
import org.sosy_lab.java_smt.api.StringFormulaManager;

@SuppressWarnings("ClassTypeParameterName")
public abstract class AbstractStringFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl>
extends AbstractBaseFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl>
implements StringFormulaManager {

protected AbstractStringFormulaManager(
FormulaCreator<TFormulaInfo, TType, TEnv, TFuncDecl> pCreator) {
super(pCreator);
}

private StringFormula wrap(TFormulaInfo formulaInfo) {
return getFormulaCreator().encapsulateString(formulaInfo);
}

@Override
public StringFormula makeString(String value) {
return wrap(makeStringImpl(value));
}

protected abstract TFormulaInfo makeStringImpl(String value);

@Override
public StringFormula makeVariable(String pVar) {
checkVariableName(pVar);
return wrap(makeVariableImpl(pVar));
}

protected abstract TFormulaInfo makeVariableImpl(String pVar);

@Override
public BooleanFormula equal(StringFormula str1, StringFormula str2) {
TFormulaInfo param1 = extractInfo(str1);
TFormulaInfo param2 = extractInfo(str2);

return wrapBool(equal(param1, param2));
}

protected abstract TFormulaInfo equal(TFormulaInfo pParam1, TFormulaInfo pParam2);
}
26 changes: 25 additions & 1 deletion src/org/sosy_lab/java_smt/basicimpl/FormulaCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
import org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier;
import org.sosy_lab.java_smt.api.StringFormula;
import org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor;
import org.sosy_lab.java_smt.api.visitors.FormulaVisitor;
import org.sosy_lab.java_smt.api.visitors.TraversalProcess;
Expand All @@ -49,6 +50,7 @@
import org.sosy_lab.java_smt.basicimpl.AbstractFormula.FloatingPointRoundingModeFormulaImpl;
import org.sosy_lab.java_smt.basicimpl.AbstractFormula.IntegerFormulaImpl;
import org.sosy_lab.java_smt.basicimpl.AbstractFormula.RationalFormulaImpl;
import org.sosy_lab.java_smt.basicimpl.AbstractFormula.StringFormulaImpl;

/**
* This is a helper class with several methods that are commonly used throughout the basicimpl
Expand All @@ -66,14 +68,20 @@ public abstract class FormulaCreator<TFormulaInfo, TType, TEnv, TFuncDecl> {
private final TType boolType;
private final @Nullable TType integerType;
private final @Nullable TType rationalType;
private final @Nullable TType stringType;
protected final TEnv environment;

protected FormulaCreator(
TEnv env, TType boolType, @Nullable TType pIntegerType, @Nullable TType pRationalType) {
TEnv env,
TType boolType,
@Nullable TType pIntegerType,
@Nullable TType pRationalType,
@Nullable TType stringType) {
this.environment = env;
this.boolType = boolType;
this.integerType = pIntegerType;
this.rationalType = pRationalType;
this.stringType = stringType;
}

public final TEnv getEnv() {
Expand Down Expand Up @@ -104,6 +112,13 @@ public final TType getRationalType() {

public abstract TType getArrayType(TType indexType, TType elementType);

public final TType getStringType() {
if (stringType == null) {
throw new UnsupportedOperationException("String theory is not supported by this solver.");
}
return stringType;
}

public abstract TFormulaInfo makeVariable(TType type, String varName);

public BooleanFormula encapsulateBoolean(TFormulaInfo pTerm) {
Expand Down Expand Up @@ -132,6 +147,11 @@ assert getFormulaType(pTerm).equals(FormulaType.getArrayType(pIndexType, pElemen
return new ArrayFormulaImpl<>(pTerm, pIndexType, pElementType);
}

public StringFormula encapsulateString(TFormulaInfo pTerm) {
assert getFormulaType(pTerm).isStringType();
return new StringFormulaImpl<>(pTerm);
}

public Formula encapsulateWithTypeOf(TFormulaInfo pTerm) {
return encapsulate(getFormulaType(pTerm), pTerm);
}
Expand All @@ -148,6 +168,8 @@ public <T extends Formula> T encapsulate(FormulaType<T> pType, TFormulaInfo pTer
return (T) new IntegerFormulaImpl<>(pTerm);
} else if (pType.isRationalType()) {
return (T) new RationalFormulaImpl<>(pTerm);
} else if (pType.isStringType()) {
return (T) new StringFormulaImpl<>(pTerm);
} else if (pType.isBitvectorType()) {
return (T) new BitvectorFormulaImpl<>(pTerm);
} else if (pType.isFloatingPointType()) {
Expand Down Expand Up @@ -194,6 +216,8 @@ protected <T extends Formula> FormulaType<T> getFormulaType(T formula) {
t = FormulaType.IntegerType;
} else if (formula instanceof RationalFormula) {
t = FormulaType.RationalType;
} else if (formula instanceof StringFormula) {
t = FormulaType.StringType;
} else if (formula instanceof FloatingPointRoundingModeFormula) {
t = FormulaType.FloatingPointRoundingModeType;
} else if (formula instanceof ArrayFormula) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class SolverStatistics {
final AtomicInteger bvOperations = new AtomicInteger();
final AtomicInteger fpOperations = new AtomicInteger();
final AtomicInteger typeOperations = new AtomicInteger();
final AtomicInteger stringOperations = new AtomicInteger();

// model operations
final AtomicInteger modelEvaluations = new AtomicInteger();
Expand Down Expand Up @@ -140,6 +141,10 @@ public int getNumberOfFPOperations() {
return fpOperations.get();
}

public int getNumberOfStringOperations() {
return stringOperations.get();
}

public int getNumberOfModelEvaluationQueries() {
return modelEvaluations.get();
}
Expand Down
Loading