Commit 4679128e authored by Martin Jonas's avatar Martin Jonas
Browse files

Working version

parents
Loading
Loading
Loading
Loading

CMakeLists.txt

0 → 100644
+31 −0
Original line number Diff line number Diff line
#specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6)
#Name your project here
project(ExprSimplifier)

#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

#Sends the -std=c99 flag to the gcc compiler
add_definitions(-std=c++0x)
#This tells CMake to fib.c and name it fibonacci
file(GLOB Q3B_SRC
    "*.h"
    "*.cpp"
)

add_executable(exprsimplifier ${Q3B_SRC})

#set( CMAKE_CXX_FLAGS "-fstack-protector -fstack-protector-all" )
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")

find_library(LibZ3 z3 PATHS /usr/lib DOC "z3 library")

if(NOT LibZ3)
  message(FATAL_ERROR "Library libz3 required, but not found!")
endif(NOT LibZ3)

include_directories(${LibZ3_INCLUDE_DIRS})
set(LIBS ${LIBS} ${LibZ3})

target_link_libraries(exprsimplifier ${LIBS})
+0 −0

File added.

Preview size limit exceeded, changes collapsed.

+75 −0
Original line number Diff line number Diff line
#ifndef UNCONSTRAINEDVARIABLESIMPLIFIER_H
#define UNCONSTRAINEDVARIABLESIMPLIFIER_H

#include "z3++.h"
#include <map>
#include <vector>
#include <string>
#include <tuple>

enum BoundType { EXISTENTIAL, UNIVERSAL };

class UnconstrainedVariableSimplifier
{
public:
    UnconstrainedVariableSimplifier(z3::context &ctx, z3::expr expr) : expression(expr)
    {
      this->context = &ctx;

      std::vector<BoundVar> boundVars;
      variableCounts = countVariableOccurences(expression, boundVars).first;
    }

    void PrintUnconstrained()
    {
        bool allConstrained = true;

        for (auto &item : variableCounts)
        {
            std::cout << "var " << item.first << " - " << item.second << " times" << std::endl;

            if (item.second == 1)
            {
                allConstrained = false;
                //std::cout << "Unconstrained variable: " << item.first << std::endl;
            }
        }
        if (allConstrained) std::cout << "All variables constrained" << std::endl;
    }

    void SimplifyOnce()
    {
        expression = simplifyOnce(expression, {}, true);
    }

    z3::expr GetExpr() const { return expression; }

    void SimplifyIte();

private:
    z3::context* context;
    z3::expr expression;

    typedef std::tuple<std::string, BoundType, int> BoundVar;

    std::map<const Z3_ast, std::pair<std::map<std::string, int>, std::vector<BoundVar>>> subformulaVariableCounts;
    std::map<const Z3_ast, int> subformulaMaxDeBruijnIndices;
    std::map<std::string, int> variableCounts;

    typedef std::map<const Z3_ast, std::pair<z3::expr, const std::vector<BoundVar>>> cacheMapType;

    cacheMapType trueSimplificationCache;
    cacheMapType falseSimplificationCache;

    std::pair<std::map<std::string, int>, int> countVariableOccurences(z3::expr, std::vector<BoundVar>);

    z3::expr simplifyOnce(z3::expr, std::vector<BoundVar>, bool);
    bool isUnconstrained(z3::expr, const std::vector<BoundVar>&);
    bool isVar(z3::expr);
    bool isBefore(z3::expr, z3::expr);
    BoundType getBoundType(z3::expr, const std::vector<BoundVar>&);

    int getNumberOfLeadingZeroes(const z3::expr&);
};

#endif // UNCONSTRAINEDVARIABLESIMPLIFIER_H

main.cpp

0 → 100644
+0 −0

File added.

Preview size limit exceeded, changes collapsed.