Skip to content

Commit

Permalink
Showing 19 changed files with 353 additions and 177 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ buildscript {

allprojects {
group = "hu.bme.mit.inf.theta"
version = "2.22.1"
version = "2.22.2"

apply(from = rootDir.resolve("gradle/shared-with-buildSrc/mirrors.gradle.kts"))
}
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ public static <IndexType extends Type, ElemType extends Type> ArrayLitExpr<Index

public List<Tuple2<LitExpr<IndexType>, LitExpr<ElemType>>> getElements() { return ImmutableList.copyOf(elems); }

public Expr<ElemType> getElseElem() { return elseElem; }
public LitExpr<ElemType> getElseElem() { return elseElem; }

@Override
public ArrayType<IndexType, ElemType> getType() {
Original file line number Diff line number Diff line change
@@ -6,10 +6,8 @@
import hu.bme.mit.theta.common.Utils;
import hu.bme.mit.theta.core.decl.VarDecl;
import hu.bme.mit.theta.core.type.LitExpr;
import hu.bme.mit.theta.core.type.inttype.IntLitExpr;
import hu.bme.mit.theta.xsts.XSTS;
import hu.bme.mit.theta.xsts.analysis.XstsState;
import hu.bme.mit.theta.xsts.dsl.XstsTypeDeclSymbol;

import java.util.List;
import java.util.Optional;
@@ -76,18 +74,9 @@ public String toString() {
public String stateToString(ExplState state) {
final LispStringBuilder sb = Utils.lispStringBuilder(ExplState.class.getSimpleName()).body();
for (VarDecl decl : xsts.getVars()) {
Optional<LitExpr<?>> val = state.eval(decl);
Optional<LitExpr> val = state.eval(decl);
if (val.isPresent()) {
if (xsts.getVarToType().containsKey(decl)) {
XstsTypeDeclSymbol type = xsts.getVarToType().get(decl);
IntLitExpr intValue = (IntLitExpr) val.get();
var optSymbol = type.getLiterals().stream()
.filter(symbol -> symbol.getIntValue().equals(intValue.getValue()))
.findFirst();
if(optSymbol.isPresent()) sb.add(String.format("(%s %s)", decl.getName(), optSymbol.get().getName()));
} else {
sb.add(String.format("(%s %s)", decl.getName(), val.get()));
}
sb.add(String.format("(%s %s)", decl.getName(), xsts.getVarToType().get(decl).serializeLiteral(val.get())));
}
}
return sb.toString();
Original file line number Diff line number Diff line change
@@ -7,15 +7,15 @@
import hu.bme.mit.theta.core.type.booltype.BoolType;
import hu.bme.mit.theta.core.utils.ExprUtils;
import hu.bme.mit.theta.core.utils.StmtUtils;
import hu.bme.mit.theta.xsts.dsl.XstsTypeDeclSymbol;
import hu.bme.mit.theta.xsts.type.XstsType;

import java.util.*;

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

public final class XSTS {
private final Collection<VarDecl<?>> vars;
private final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType;
private final Map<VarDecl<?>, XstsType<?>> varToType;
private final Set<VarDecl<?>> ctrlVars;

private final NonDetStmt tran;
@@ -31,7 +31,7 @@ public Collection<VarDecl<?>> getVars() {
return vars;
}

public Map<VarDecl<?>, XstsTypeDeclSymbol> getVarToType() { return varToType; }
public Map<VarDecl<?>, XstsType<?>> getVarToType() { return varToType; }

public Expr<BoolType> getProp() { return prop; }

@@ -47,7 +47,7 @@ public NonDetStmt getEnv() {

public Set<VarDecl<?>> getCtrlVars() { return ctrlVars; }

public XSTS(final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType, final Set<VarDecl<?>> ctrlVars, final NonDetStmt init, final NonDetStmt tran, final NonDetStmt env, final Expr<BoolType> initFormula, final Expr<BoolType> prop) {
public XSTS(final Map<VarDecl<?>, XstsType<?>> varToType, final Set<VarDecl<?>> ctrlVars, final NonDetStmt init, final NonDetStmt tran, final NonDetStmt env, final Expr<BoolType> initFormula, final Expr<BoolType> prop) {
this.tran = checkNotNull(tran);
this.init = checkNotNull(init);
this.env = checkNotNull(env);
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hu.bme.mit.theta.xsts.dsl;

import hu.bme.mit.theta.common.dsl.Symbol;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.xsts.type.XstsCustomType;

import java.math.BigInteger;

import static hu.bme.mit.theta.core.type.inttype.IntExprs.Int;

public class XstsCustomLiteralSymbol implements Symbol {

private final XstsCustomType.XstsCustomLiteral literal;

private static int counter = 0;

public XstsCustomLiteralSymbol(String name) {
this.literal = XstsCustomType.XstsCustomLiteral.of(name, BigInteger.valueOf(counter++));
}

@Override
public String getName() {
return literal.getName();
}

@Override
public String toString() {
return literal.toString();
}

public Expr instantiate(){
return Int(literal.getIntValue());
}

public XstsCustomType.XstsCustomLiteral getLiteral() {
return literal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package hu.bme.mit.theta.xsts.dsl;

import hu.bme.mit.theta.common.dsl.Symbol;
import hu.bme.mit.theta.core.type.Type;
import hu.bme.mit.theta.xsts.type.XstsCustomType;

import java.util.Objects;

public final class XstsCustomTypeSymbol implements Symbol {

private XstsCustomType xstsType;

private XstsCustomTypeSymbol(final XstsCustomType xstsType) {
this.xstsType =xstsType;
}

public static XstsCustomTypeSymbol of(final XstsCustomType xstsType) {
return new XstsCustomTypeSymbol(xstsType);
}

@Override
public int hashCode() {
return Objects.hash(xstsType);
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof XstsCustomTypeSymbol) {
final XstsCustomTypeSymbol that = (XstsCustomTypeSymbol) obj;
return this.xstsType.equals(that.xstsType);
} else {
return false;
}
}

@Override
public String toString() {
return xstsType.toString();
}

public XstsCustomType getXstsType(){
return xstsType;
}

@Override
public String getName() { return xstsType.getName(); }

public Type instantiate() {
return xstsType.getType();
}
}
Original file line number Diff line number Diff line change
@@ -498,7 +498,7 @@ private <T1 extends Type, T2 extends Type> Expr<?> createArrayLitExpr(final ArrL
final T2 valueType;

if(ctx.indexType != null) {
indexType = (T1) new XstsType(typeTable,ctx.indexType).instantiate(env);
indexType = (T1) new XstsType(typeTable,ctx.indexType).instantiate(env).getType();
}
else {
indexType = (T1) ctx.indexExpr.get(0).accept(this).getType();
Original file line number Diff line number Diff line change
@@ -7,8 +7,11 @@
import hu.bme.mit.theta.core.stmt.NonDetStmt;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.core.type.booltype.BoolType;
import hu.bme.mit.theta.core.utils.ExprUtils;
import hu.bme.mit.theta.xsts.XSTS;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslParser.XstsContext;
import hu.bme.mit.theta.xsts.type.XstsCustomType;
import hu.bme.mit.theta.xsts.type.XstsType;

import java.util.*;
import java.util.regex.Pattern;
@@ -47,56 +50,50 @@ public Optional<? extends Symbol> resolve(String name) {
public XSTS instantiate(){
final Env env = new Env();

final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType = Containers.createMap();
final Map<VarDecl<?>, XstsType<?>> varToType = Containers.createMap();
final Set<VarDecl<?>> ctrlVars = Containers.createSet();
final List<Expr<BoolType>> initExprs = new ArrayList<>();

for(var typeDeclContext: context.typeDeclarations){
final List<XstsTypeLiteralSymbol> literals = new ArrayList<>();
final List<XstsCustomLiteralSymbol> literalSymbols = new ArrayList<>();
for(var literalContext: typeDeclContext.literals){
var optSymbol = resolve(literalContext.name.getText());
if(optSymbol.isPresent()){
literals.add((XstsTypeLiteralSymbol) optSymbol.get());
literalSymbols.add((XstsCustomLiteralSymbol) optSymbol.get());
} else {
var symbol = new XstsTypeLiteralSymbol(literalContext.name.getText());
literals.add(symbol);
var symbol = new XstsCustomLiteralSymbol(literalContext.name.getText());
literalSymbols.add(symbol);
declare(symbol);
env.define(symbol,symbol.instantiate());
}
}
final List<XstsCustomType.XstsCustomLiteral> literals = literalSymbols.stream().map(XstsCustomLiteralSymbol::getLiteral).collect(Collectors.toList());
final XstsCustomType xstsCustomType = XstsCustomType.of(typeDeclContext.name.getText(),literals);

final XstsTypeDeclSymbol typeDeclSymbol = XstsTypeDeclSymbol.of(typeDeclContext.name.getText(),literals);
final XstsCustomTypeSymbol typeDeclSymbol = XstsCustomTypeSymbol.of(xstsCustomType);
typeTable.add(typeDeclSymbol);
env.define(typeDeclSymbol,typeDeclSymbol.instantiate());
env.define(typeDeclSymbol,typeDeclSymbol.getXstsType());
}

for(var varDeclContext: context.variableDeclarations){
if (tempVarPattern.matcher(varDeclContext.name.getText()).matches()){
throw new ParseException(varDeclContext, "Variable name '" + varDeclContext.name.getText() + "' is reserved!");
}

final XstsVariableSymbol symbol = new XstsVariableSymbol(typeTable,varDeclContext);
final XstsVariableSymbol symbol = new XstsVariableSymbol(typeTable, varToType, varDeclContext);
declare(symbol);

final VarDecl<?> var = symbol.instantiate(env);
final Optional<? extends Symbol> typeDeclSymbol = typeTable.get(varDeclContext.ttype.getText());
if (typeDeclSymbol.isPresent()) {
varToType.put(var, (XstsTypeDeclSymbol) typeDeclSymbol.get());
}
final VarDecl var = symbol.instantiate(env);
if(varDeclContext.CTRL()!=null) ctrlVars.add(var);
if(varDeclContext.initValue!=null){
initExprs.add(Eq(var.getRef(),new XstsExpression(this,typeTable,varDeclContext.initValue).instantiate(env)));
} else if(varToType.containsKey(var)) {
final var type = varToType.get(var);
final Expr<BoolType> expr = Or(type.getLiterals().stream()
.map(lit -> Eq(var.getRef(),Int(lit.getIntValue())))
.collect(Collectors.toList()));
initExprs.add(expr);
} else {
initExprs.add(varToType.get(var).createBoundExpr(var));
}
env.define(symbol,var);
}

final Expr<BoolType> initFormula = And(initExprs);
final Expr<BoolType> initFormula = ExprUtils.simplify(And(initExprs));

final NonDetStmt tranSet = new XstsTransitionSet(this,typeTable,context.tran.transitionSet(), varToType).instantiate(env);
final NonDetStmt initSet = new XstsTransitionSet(this,typeTable,context.init.transitionSet(), varToType).instantiate(env);
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hu.bme.mit.theta.xsts.dsl;

import com.google.common.collect.ImmutableList;
import hu.bme.mit.theta.common.dsl.*;
import hu.bme.mit.theta.core.decl.Decls;
import hu.bme.mit.theta.core.decl.VarDecl;
@@ -11,18 +12,16 @@
import hu.bme.mit.theta.core.type.arraytype.ArrayType;
import hu.bme.mit.theta.core.type.booltype.BoolType;
import hu.bme.mit.theta.core.type.inttype.IntType;
import hu.bme.mit.theta.core.utils.TypeUtils;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslBaseVisitor;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslParser.*;
import hu.bme.mit.theta.xsts.type.XstsCustomType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.*;
import static hu.bme.mit.theta.core.stmt.Stmts.*;
import static hu.bme.mit.theta.core.type.abstracttype.AbstractExprs.Eq;
import static hu.bme.mit.theta.core.type.arraytype.ArrayExprs.Write;
import static hu.bme.mit.theta.core.type.booltype.BoolExprs.Bool;
import static hu.bme.mit.theta.core.type.booltype.BoolExprs.Or;
@@ -34,9 +33,9 @@ public class XstsStatement {
private final DynamicScope scope;
private final SymbolTable typeTable;
private final StmtContext context;
private final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType;
private final Map<VarDecl<?>, hu.bme.mit.theta.xsts.type.XstsType<?>> varToType;

public XstsStatement(final DynamicScope scope, final SymbolTable typeTable, final StmtContext context, final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType) {
public XstsStatement(final DynamicScope scope, final SymbolTable typeTable, final StmtContext context, final Map<VarDecl<?>, hu.bme.mit.theta.xsts.type.XstsType<?>> varToType) {
this.scope = checkNotNull(scope);
this.typeTable = checkNotNull(typeTable);
this.context = checkNotNull(context);
@@ -57,10 +56,10 @@ private static final class StmtCreatorVisitor extends XstsDslBaseVisitor<Stmt> {

private DynamicScope currentScope;
private final SymbolTable typeTable;
final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType;
final Map<VarDecl<?>, hu.bme.mit.theta.xsts.type.XstsType<?>> varToType;
private final Env env;

public StmtCreatorVisitor(final DynamicScope scope, final SymbolTable typeTable, final Env env, final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType) {
public StmtCreatorVisitor(final DynamicScope scope, final SymbolTable typeTable, final Env env, final Map<VarDecl<?>, hu.bme.mit.theta.xsts.type.XstsType<?>> varToType) {
this.currentScope = checkNotNull(scope);
this.typeTable = checkNotNull(typeTable);
this.env = checkNotNull(env);
@@ -83,14 +82,14 @@ public Stmt visitHavocStmt(final HavocStmtContext ctx) {
final String lhsId = ctx.lhs.getText();
final Symbol lhsSymbol = currentScope.resolve(lhsId).get();
final VarDecl<?> var = (VarDecl<?>) env.eval(lhsSymbol);
if(varToType.containsKey(var)){
final XstsTypeDeclSymbol type = varToType.get(var);
final Expr<BoolType> expr = Or(type.getLiterals().stream()
.map(lit -> Eq(var.getRef(),Int(lit.getIntValue())))
.collect(Collectors.toList()));

final hu.bme.mit.theta.xsts.type.XstsType type = varToType.get(var);
if(type instanceof XstsCustomType){
final Expr<BoolType> expr = type.createBoundExpr(var);
final AssumeStmt assume = Assume(expr);
return SequenceStmt.of(List.of(Havoc(var),assume));
}

return Havoc(var);
}

@@ -191,20 +190,27 @@ public Stmt visitLoopStmt(LoopStmtContext ctx) {
@Override
public Stmt visitLocalVarDeclStmt(LocalVarDeclStmtContext ctx) {
final String name = ctx.name.getText();
final Type type = new XstsType(typeTable,ctx.ttype).instantiate(env);
final hu.bme.mit.theta.xsts.type.XstsType xstsType = new XstsType(typeTable,ctx.ttype).instantiate(env);
final Type type = xstsType.getType();
final var decl = Decls.Var(name,type);
final Symbol symbol = DeclSymbol.of(decl);


final Stmt result;
final Stmt havoc = Havoc(decl);
if(ctx.initValue==null){
result = SkipStmt.getInstance();
if(xstsType instanceof XstsCustomType){
final Expr<BoolType> expr = xstsType.createBoundExpr(decl);
final AssumeStmt assume = Assume(expr);
result = SequenceStmt.of(List.of(havoc,assume));
} else {
result = havoc;
}
} else {
var expr = new XstsExpression(currentScope,typeTable,ctx.initValue).instantiate(env);
if (expr.getType().equals(decl.getType())) {
@SuppressWarnings("unchecked") final VarDecl<Type> tVar = (VarDecl<Type>) decl;
@SuppressWarnings("unchecked") final Expr<Type> tExpr = (Expr<Type>) expr;
result = Assign(tVar, tExpr);
result = SequenceStmt(ImmutableList.of(havoc, Assign(tVar,tExpr)));
} else {
throw new IllegalArgumentException("Type of " + decl + " is incompatilbe with " + expr);
}
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import hu.bme.mit.theta.core.stmt.Stmt;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslBaseVisitor;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslParser.TransitionSetContext;
import hu.bme.mit.theta.xsts.type.XstsType;

import java.util.List;
import java.util.Map;
@@ -21,9 +22,9 @@ public class XstsTransitionSet {
private final DynamicScope scope;
private final SymbolTable typeTable;
private final TransitionSetContext context;
private final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType;
private final Map<VarDecl<?>, XstsType<?>> varToType;

public XstsTransitionSet(final DynamicScope scope, final SymbolTable typeTable, final TransitionSetContext context, final Map<VarDecl<?>,XstsTypeDeclSymbol> varToType) {
public XstsTransitionSet(final DynamicScope scope, final SymbolTable typeTable, final TransitionSetContext context, final Map<VarDecl<?>,XstsType<?>> varToType) {
this.scope = checkNotNull(scope);
this.typeTable = checkNotNull(typeTable);
this.context = checkNotNull(context);
Original file line number Diff line number Diff line change
@@ -4,9 +4,10 @@
import hu.bme.mit.theta.common.dsl.Symbol;
import hu.bme.mit.theta.common.dsl.SymbolTable;
import hu.bme.mit.theta.core.dsl.ParseException;
import hu.bme.mit.theta.core.type.Type;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslBaseVisitor;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslParser.*;
import hu.bme.mit.theta.xsts.type.XstsArrayType;
import hu.bme.mit.theta.xsts.type.XstsPrimitiveType;

import java.util.Optional;

@@ -25,17 +26,17 @@ public XstsType(final SymbolTable typeTable, final TypeContext context) {
this.context = checkNotNull(context);
}

public Type instantiate(final Env env) {
public hu.bme.mit.theta.xsts.type.XstsType instantiate(final Env env) {
final TypeCreatorVisitor typeCreatorVisitor = new TypeCreatorVisitor(typeTable,env);
final Type result = context.accept(typeCreatorVisitor);
final hu.bme.mit.theta.xsts.type.XstsType result = context.accept(typeCreatorVisitor);
if (result == null) {
throw new AssertionError();
} else {
return result;
}
}

private static class TypeCreatorVisitor extends XstsDslBaseVisitor<Type> {
private static class TypeCreatorVisitor extends XstsDslBaseVisitor<hu.bme.mit.theta.xsts.type.XstsType> {

private final SymbolTable typeTable;
private final Env env;
@@ -46,31 +47,31 @@ public TypeCreatorVisitor(final SymbolTable typeTable, final Env env) {
}

@Override
public Type visitCustomType(final CustomTypeContext ctx) {
public hu.bme.mit.theta.xsts.type.XstsType visitCustomType(final CustomTypeContext ctx) {
Optional<? extends Symbol> optSymbol = typeTable.get(ctx.name.getText());
if (optSymbol.isEmpty()) {
throw new ParseException(ctx, "Type '" + ctx.name.getText() + "' cannot be resolved");
}
final Symbol symbol = optSymbol.get();
final Type type = (Type) env.eval(symbol);
return type;
final hu.bme.mit.theta.xsts.type.XstsType xstsType = (hu.bme.mit.theta.xsts.type.XstsType) env.eval(symbol);
return xstsType;
}

@Override
public Type visitBoolType(final BoolTypeContext ctx) {
return Bool();
public hu.bme.mit.theta.xsts.type.XstsType visitBoolType(final BoolTypeContext ctx) {
return XstsPrimitiveType.of(Bool());
}

@Override
public Type visitIntType(final IntTypeContext ctx) {
return Int();
public hu.bme.mit.theta.xsts.type.XstsType visitIntType(final IntTypeContext ctx) {
return XstsPrimitiveType.of(Int());
}

@Override
public Type visitArrayType(final ArrayTypeContext ctx) {
final Type indexType = ctx.indexType.accept(this);
final Type elemType = ctx.elemType.accept(this);
return Array(indexType, elemType);
public hu.bme.mit.theta.xsts.type.XstsType visitArrayType(final ArrayTypeContext ctx) {
final hu.bme.mit.theta.xsts.type.XstsType indexType = ctx.indexType.accept(this);
final hu.bme.mit.theta.xsts.type.XstsType elemType = ctx.elemType.accept(this);
return XstsArrayType.of(indexType, elemType);
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -6,17 +6,21 @@
import hu.bme.mit.theta.core.decl.VarDecl;
import hu.bme.mit.theta.xsts.dsl.gen.XstsDslParser.VariableDeclarationContext;

import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;
import static hu.bme.mit.theta.core.decl.Decls.Var;

public class XstsVariableSymbol implements Symbol {

private final String name;
private final XstsType type;
final Map<VarDecl<?>, hu.bme.mit.theta.xsts.type.XstsType<?>> varToType;

public XstsVariableSymbol(final SymbolTable typeTable, final VariableDeclarationContext context) {
public XstsVariableSymbol(final SymbolTable typeTable, final Map<VarDecl<?>, hu.bme.mit.theta.xsts.type.XstsType<?>> varToType, final VariableDeclarationContext context) {
checkNotNull(context);
name = context.name.getText();
this.varToType = varToType;
type = new XstsType(typeTable,context.ttype);
}

@@ -26,6 +30,9 @@ public String getName() {
}

public VarDecl<?> instantiate(Env env) {
return Var(name, type.instantiate(env));
final hu.bme.mit.theta.xsts.type.XstsType<?> xstsType = type.instantiate(env);
final VarDecl<?> var = Var(name, xstsType.getType());
varToType.put(var, xstsType);
return var;
}
}
Original file line number Diff line number Diff line change
@@ -13,8 +13,8 @@
import hu.bme.mit.theta.core.type.booltype.BoolType;
import hu.bme.mit.theta.core.type.inttype.IntType;
import hu.bme.mit.theta.xsts.XSTS;
import hu.bme.mit.theta.xsts.dsl.XstsTypeDeclSymbol;
import hu.bme.mit.theta.xsts.pnml.elements.*;
import hu.bme.mit.theta.xsts.type.XstsType;

import java.io.InputStream;
import java.util.*;
@@ -84,7 +84,7 @@ public static XSTS createXSTS(final PnmlNet net, final InputStream propStream){
final NonDetStmt init = NonDetStmt.of(ImmutableList.of());
final NonDetStmt env = NonDetStmt.of(ImmutableList.of());

final Map<VarDecl<?>, XstsTypeDeclSymbol> varToType = ImmutableMap.of();
final Map<VarDecl<?>, XstsType<?>> varToType = ImmutableMap.of();
final Set<VarDecl<?>> ctrlVars = ImmutableSet.of();

final Scanner propScanner = new Scanner(propStream).useDelimiter("\\A");
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package hu.bme.mit.theta.xsts.type;

import com.google.common.base.Preconditions;
import hu.bme.mit.theta.common.Utils;
import hu.bme.mit.theta.core.decl.VarDecl;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.core.type.LitExpr;
import hu.bme.mit.theta.core.type.Type;
import hu.bme.mit.theta.core.type.arraytype.ArrayLitExpr;
import hu.bme.mit.theta.core.type.arraytype.ArrayType;
import hu.bme.mit.theta.core.type.booltype.BoolType;

import static hu.bme.mit.theta.core.type.arraytype.ArrayExprs.Array;
import static hu.bme.mit.theta.core.type.booltype.BoolExprs.True;

public final class XstsArrayType<IndexType extends Type, ElemType extends Type> implements XstsType<ArrayType<IndexType, ElemType>> {
private final XstsType<IndexType> indexType;
private final XstsType<ElemType> elemType;
private ArrayType<IndexType,ElemType> type;

private XstsArrayType(XstsType<IndexType> indexType, XstsType<ElemType> elemType) {
this.indexType = indexType;
this.elemType = elemType;
this.type = Array(indexType.getType(), elemType.getType());
}

public static <IndexType extends Type, ElemType extends Type> XstsArrayType<IndexType, ElemType> of(XstsType<IndexType> indexType, XstsType<ElemType> elemType) {
return new XstsArrayType<>(indexType, elemType);
}

public ArrayType<IndexType, ElemType> getType() {
return type;
}

@Override
public Expr<BoolType> createBoundExpr(VarDecl<ArrayType<IndexType, ElemType>> decl) {
return True();
}

@Override
public String serializeLiteral(LitExpr<ArrayType<IndexType, ElemType>> literal) {
Preconditions.checkArgument(literal.getType().equals(type));
final ArrayLitExpr<IndexType,ElemType> arrayLitExpr = (ArrayLitExpr<IndexType,ElemType>) literal;
return Utils.lispStringBuilder("array")
.addAll(arrayLitExpr.getElements().stream().map(elem -> String.format("(%s %s)", indexType.serializeLiteral(elem.get1()), elemType.serializeLiteral(elem.get2()))))
.add((String.format("(default %s)", elemType.serializeLiteral(arrayLitExpr.getElseElem()))))
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package hu.bme.mit.theta.xsts.type;

import com.google.common.base.Preconditions;
import hu.bme.mit.theta.core.decl.VarDecl;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.core.type.LitExpr;
import hu.bme.mit.theta.core.type.booltype.BoolType;
import hu.bme.mit.theta.core.type.inttype.IntLitExpr;
import hu.bme.mit.theta.core.type.inttype.IntType;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

import static hu.bme.mit.theta.core.type.abstracttype.AbstractExprs.Eq;
import static hu.bme.mit.theta.core.type.booltype.BoolExprs.Or;
import static hu.bme.mit.theta.core.type.inttype.IntExprs.Int;

public final class XstsCustomType implements XstsType<IntType> {
private final String name;
private final List<XstsCustomLiteral> literals;

private XstsCustomType(final String name, final List<XstsCustomLiteral> literals) {
this.name = name;
this.literals = literals;
}

public static XstsCustomType of(final String name, final List<XstsCustomLiteral> literals) {
return new XstsCustomType(name, literals);
}

public String getName() {
return name;
}

public List<XstsCustomLiteral> getLiterals() {
return literals;
}

public static final class XstsCustomLiteral {
private final BigInteger intValue;
private final String name;

private XstsCustomLiteral(String name, BigInteger intValue) {
this.name = name;
this.intValue = intValue;
}

public static XstsCustomLiteral of(String name, BigInteger intValue) {
return new XstsCustomLiteral(name, intValue);
}

public BigInteger getIntValue() {
return intValue;
}

public String getName() {
return name;
}
}

public IntType getType() {
return Int();
}

@Override
public Expr<BoolType> createBoundExpr(VarDecl<IntType> decl) {
return Or(literals.stream()
.map(lit -> Eq(decl.getRef(), Int(lit.getIntValue())))
.collect(Collectors.toList()));
}

@Override
public String serializeLiteral(LitExpr<IntType> literal) {
final IntLitExpr intLitExpr = (IntLitExpr) literal;
final var customLiteral = literals.stream().filter(lit -> lit.getIntValue().equals(intLitExpr.getValue())).findFirst();
Preconditions.checkArgument(customLiteral.isPresent(), "Literal %s not found", intLitExpr.getValue());
return customLiteral.get().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hu.bme.mit.theta.xsts.type;

import com.google.common.base.Preconditions;
import hu.bme.mit.theta.core.decl.VarDecl;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.core.type.LitExpr;
import hu.bme.mit.theta.core.type.Type;
import hu.bme.mit.theta.core.type.booltype.BoolType;

import static hu.bme.mit.theta.core.type.booltype.BoolExprs.True;

public final class XstsPrimitiveType<T extends Type> implements XstsType<T> {
private final T type;

private XstsPrimitiveType(T type) {
this.type = type;
}

public static <T extends Type> XstsPrimitiveType<T> of(T type) {
return new XstsPrimitiveType<>(type);
}

@Override
public T getType() {
return type;
}

@Override
public Expr<BoolType> createBoundExpr(VarDecl<T> decl) {
return True();
}

@Override
public String serializeLiteral(LitExpr<T> literal) {
Preconditions.checkArgument(literal.getType().equals(type));
return literal.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hu.bme.mit.theta.xsts.type;

import hu.bme.mit.theta.core.decl.VarDecl;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.core.type.LitExpr;
import hu.bme.mit.theta.core.type.Type;
import hu.bme.mit.theta.core.type.booltype.BoolType;

public interface XstsType<T extends Type> {

T getType();

Expr<BoolType> createBoundExpr(final VarDecl<T> decl);

String serializeLiteral(final LitExpr<T> literal);

}

0 comments on commit 1cbc98f

Please sign in to comment.