All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.bme.mit.theta.solver.smtlib.impl.princess.PrincessSmtLibItpSolver Maven / Gradle / Ivy

There is a newer version: 6.5.2
Show newest version
/*
 *  Copyright 2024 Budapest University of Technology and Economics
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package hu.bme.mit.theta.solver.smtlib.impl.princess;

import hu.bme.mit.theta.core.decl.ConstDecl;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.core.type.booltype.BoolExprs;
import hu.bme.mit.theta.core.type.booltype.BoolType;
import hu.bme.mit.theta.solver.*;
import hu.bme.mit.theta.solver.smtlib.dsl.gen.SMTLIBv2Lexer;
import hu.bme.mit.theta.solver.smtlib.dsl.gen.SMTLIBv2Parser;
import hu.bme.mit.theta.solver.smtlib.solver.SmtLibEnumStrategy;
import hu.bme.mit.theta.solver.smtlib.solver.SmtLibItpSolver;
import hu.bme.mit.theta.solver.smtlib.solver.SmtLibSolverException;
import hu.bme.mit.theta.solver.smtlib.solver.binary.SmtLibSolverBinary;
import hu.bme.mit.theta.solver.smtlib.solver.interpolation.SmtLibInterpolant;
import hu.bme.mit.theta.solver.smtlib.solver.interpolation.SmtLibItpPattern;
import hu.bme.mit.theta.solver.smtlib.solver.model.SmtLibModel;
import hu.bme.mit.theta.solver.smtlib.solver.parser.ThrowExceptionErrorListener;
import hu.bme.mit.theta.solver.smtlib.solver.transformer.SmtLibSymbolTable;
import hu.bme.mit.theta.solver.smtlib.solver.transformer.SmtLibTermTransformer;
import hu.bme.mit.theta.solver.smtlib.solver.transformer.SmtLibTransformationManager;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.Interval;

import java.util.*;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.*;
import static hu.bme.mit.theta.core.type.booltype.BoolExprs.False;

public final class PrincessSmtLibItpSolver extends SmtLibItpSolver {

    private final Map, String> assertionNames = new IdentityHashMap<>();
    private static final String assertionNamePattern = "_princess_assertion_%d";
    private static long assertionCount = 0;

    public PrincessSmtLibItpSolver(
            final SmtLibSymbolTable symbolTable,
            final SmtLibTransformationManager transformationManager,
            final SmtLibTermTransformer termTransformer, final SmtLibSolverBinary solverBinary,
            final SmtLibEnumStrategy enumStrategy
    ) {
        super(symbolTable, transformationManager, termTransformer, solverBinary, enumStrategy);
    }

    @Override
    public ItpPattern createTreePattern(final ItpMarkerTree root) {
        checkNotNull(root);
        return SmtLibItpPattern.of(root);
    }

    @Override
    public PrincessSmtLibItpMarker createMarker() {
        final var marker = new PrincessSmtLibItpMarker();
        markers.add(marker);
        return marker;
    }

    @Override
    protected void add(final PrincessSmtLibItpMarker marker, final Expr assertion,
                       final Set> consts, final String term) {
        consts.stream().map(symbolTable::getDeclaration).forEach(this::issueGeneralCommand);

        final var name = String.format(assertionNamePattern, assertionCount++);
        assertionNames.put(assertion, name);
        issueGeneralCommand(String.format("(assert (! %s :named %s))", term, name));
    }

    @Override
    public Interpolant getInterpolant(final ItpPattern pattern) {
        checkState(getStatus() == SolverStatus.UNSAT,
                "Cannot get interpolant if status is not UNSAT.");
        checkArgument(pattern instanceof SmtLibItpPattern);
        @SuppressWarnings("unchecked") final var princessItpPattern = (SmtLibItpPattern) pattern;

        final var term = patternToTerm(princessItpPattern.getRoot());

        final List> itpList = new LinkedList<>();

        solverBinary.issueCommand(String.format("(get-interpolants %s)", term));
        for (final var itp : parseItpResponse(solverBinary.readResponse())) {
            itpList.add(termTransformer.toExpr(itp, BoolExprs.Bool(),
                    new SmtLibModel(Collections.emptyMap())));
        }
        itpList.add(False());

        final Map> itpMap = new HashMap<>();
        buildItpMapFormList(princessItpPattern.getRoot(), itpList, itpMap);

        return new SmtLibInterpolant(itpMap);
    }

    private String patternToTerm(final ItpMarkerTree markerTree) {
        String term;

        final var marker = markerTree.getMarker();
        final var terms = marker.getTerms();
        if (terms.size() == 1) {
            term = assertionNames.get(terms.iterator().next().get1());
        } else {
            term = String.format("(and %s)", terms.stream().map(t -> assertionNames.get(t.get1()))
                    .collect(Collectors.joining(" ")));
        }

        final var children = markerTree.getChildren();
        for (var i = children.size() - 1; i >= 0; i--) {
            if (i == 0) {
                term = String.format("%s %s", patternToTerm(children.get(i)), term);
            } else {
                term = String.format("(%s) %s", patternToTerm(children.get(i)), term);
            }
        }

        return term;
    }

    private void buildItpMapFormList(final ItpMarkerTree pattern,
                                     final List> itpList, final Map> itpMap) {
        for (final ItpMarkerTree child : pattern.getChildren()) {
            buildItpMapFormList(child, itpList, itpMap);
        }
        final ItpMarker marker = pattern.getMarker();
        final Expr itpExpr = itpList.get(0);
        itpMap.put(marker, itpExpr);
        itpList.remove(0);
    }

    @Override
    protected void init() {
        issueGeneralCommand("(set-option :print-success true)");
        issueGeneralCommand("(set-option :produce-models true)");
        issueGeneralCommand("(set-option :produce-interpolants true)");
        issueGeneralCommand("(set-logic ALL)");
    }

    private List parseItpResponse(final String response) {
        final var lexer = new SMTLIBv2Lexer(CharStreams.fromString(response));
        final var parser = new SMTLIBv2Parser(new CommonTokenStream(lexer));
        try {
            final var interpolants = new ArrayList();

            lexer.removeErrorListeners();
            lexer.addErrorListener(new ThrowExceptionErrorListener());
            parser.removeErrorListeners();
            parser.addErrorListener(new ThrowExceptionErrorListener());

            for (final var term : parser.get_interpolants_response_smtinterpol().term()) {
                interpolants.add(extractString(term));
            }

            return interpolants;
        } catch (Exception e) {
            try {
                throw new SmtLibSolverException(
                        parser.response().general_response_error().reason.getText());
            } catch (Exception ex) {
                throw new SmtLibSolverException("Could not parse solver output: " + response, e);
            }
        }
    }

    private static String extractString(final ParserRuleContext ctx) {
        return ctx.start.getInputStream()
                .getText(new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy