Exemplo n.º 1
0
def wff2z3_update(w):
    if w[0] == 'e' or w[0] == 'i0' or w[0] == 'i1':
        var_cstr_map = {}
        flag_constr = False
        lhs = expr2z3_update(w[-2], var_cstr_map)
        rhs = expr2z3_update(w[-1], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())
        if fun_utiles.isArrayFunction(w[-2][0]) == True:
            if '_x1' in var_cstr_map.keys():
                del var_cstr_map['_x1']
            flag_constr = True
        if '_s1' in var_cstr_map.keys():
            del var_cstr_map['_s1']
            flag_constr = True
        list_cstr_str = cstr_list(var_cstr_map.values())
        if 'Or' not in lhs and 'And' not in lhs and 'If' not in lhs and '/' not in lhs:
            lhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(lhs))
        if 'Or' not in rhs and 'And' not in rhs and 'If' not in rhs and '/' not in rhs:
            rhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(rhs))
        if w[-1][0] in ['==', '<=', '>=', '>', '<', '!=', 'or', 'and']:
            rhs = 'If((' + rhs + ')==0,0,1)'
        if list_var_str is not None and list_cstr_str is not None:
            if w[0] == 'i1':
                return "ForAll([" + list_var_str + "],Implies(" + list_cstr_str + "," + lhs + ' == ' + rhs + "))"
            else:
                if flag_constr == True:
                    return "ForAll([" + list_var_str + "],Implies(" + list_cstr_str + "," + lhs + ' == ' + rhs + "))"
                else:
                    return 'ForAll([' + list_var_str + '],' + lhs + ' == ' + rhs + ")"
        else:
            return lhs + ' == ' + rhs
    elif w[0] == 'd0':  # Bi-implications are represented using equality == in z3py
        var_cstr_map = {}
        lhs = expr2z3_update(w[1], var_cstr_map)
        rhs = expr2z3_update(w[2], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())
        list_cstr_str = cstr_list(var_cstr_map.values())
        if 'Or' not in lhs and 'And' not in lhs and 'If' not in lhs and '/' not in lhs:
            lhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(lhs))
        if 'Or' not in rhs and 'And' not in rhs and 'If' not in rhs and '/' not in rhs:
            rhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(rhs))
        if list_var_str is not None and list_cstr_str is not None:
            return 'ForAll([' + list_var_str + '],' + lhs + '=0 == ' + rhs + ")"
        else:
            return lhs + '=0 == ' + rhs
    elif w[0] == 'd1':  # Bi-implications are represented using equality == in z3py
        var_cstr_map = {}
        lhs = expr2z3_update(w[2], var_cstr_map)
        rhs = expr2z3_update(w[3], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())
        list_cstr_str = cstr_list(var_cstr_map.values())
        lhs = w[1] + '+1'
        if 'Or' not in lhs and 'And' not in lhs and 'If' not in lhs and '/' not in lhs:
            lhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(lhs))
        if 'Or' not in rhs and 'And' not in rhs and 'If' not in rhs and '/' not in rhs:
            rhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(rhs))
        if list_var_str is not None and list_cstr_str is not None:
            return "ForAll([" + list_var_str + "]," + lhs + ' == ' + rhs + ")"
        else:
            return lhs + ' == ' + rhs
    elif w[0] == 'a' or w[0] == 's0':
        var_cstr_map = {}
        expression = expr2z3_update(w[1], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())
        list_cstr_str = cstr_list(var_cstr_map.values())
        if 'Or' not in expression and 'And' not in expression and 'If' not in expression and '/' not in expression:
            expression = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(expression))
        if list_var_str is not None and list_cstr_str is not None:
            if 'Implies' in expression:
                arg_list = fun_utiles.extract_args(expression)
                constr = simplify(arg_list[0])
                axms = str(constr).split('<')
                axms[0] = axms[0].strip()
                axms[1] = axms[1].strip()
                arg_list[1] = 'Or(' + axms[1] + '==0,' + arg_list[1].replace(
                    axms[0], '(' + axms[1] + '-1)') + ')'
                if list_var_str is not None and list_cstr_str is not None:
                    return 'ForAll([' + str(list_var_str) + '],Implies(' + str(
                        list_cstr_str) + ',' + arg_list[1] + '))'
                else:
                    return arg_list[1]
            else:
                return 'ForAll([' + list_var_str + '],Implies(' + list_cstr_str + ',' + expression + '))'

        else:
            return expression
    elif w[0] == 's1':
        var_cstr_map = {}
        equations = []
        expression = expr2z3_update(w[1], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())
        list_cstr_str = cstr_list(var_cstr_map.values())
        if 'Or' not in expression and 'And' not in expression and 'If' not in expression and '/' not in expression:
            expression = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(expression))
        if list_var_str is not None and list_cstr_str is not None:
            if 'Implies' in expression:
                arg_list = fun_utiles.extract_args(expression)
                constr = simplify(arg_list[0])
                axms = str(constr).split('<')
                axms[0] = axms[0].strip()
                axms[1] = axms[1].strip()
                var_cstr_map_mod = {}
                for x in var_cstr_map.keys():
                    if x != axms[0]:
                        var_cstr_map_mod[x] = var_cstr_map[x]
                list_var_str_new = qualifier_list(var_cstr_map_mod.keys())
                list_cstr_str_new = cstr_list(var_cstr_map_mod.values())

                new_w = copy.deepcopy(w)
                for element in var_cstr_map.keys():
                    fun = []
                    fun.append('_f')
                    parameter = []
                    parameter.append(element)
                    fun.append(parameter)
                    sub = []
                    sub.append(element)
                    new_w[1] = fun_utiles.expr_replace(
                        new_w[1], sub,
                        fun)  #expr_replace_const(new_w[1],element,fun)
                new_expression = expr2z3_update(new_w[1], var_cstr_map)
                new_arg_list = fun_utiles.extract_args(new_expression)

                #old_arg_list=arg_list[1]
                old_arg_list = new_arg_list[1]
                arg_list[1] = 'Or(' + axms[
                    1] + '==0,' + fun_utiles.simplify_expand_sympy(
                        arg_list[1].replace(axms[0],
                                            '(' + axms[1] + '-1)')) + ')'
                if list_var_str_new is not None and list_cstr_str_new is not None:
                    return 'ForAll([' + str(
                        list_var_str
                    ) + '],Implies(And(' + arg_list[0] + ',' + str(
                        list_cstr_str) + '),' + old_arg_list + '))'
                else:
                    return 'ForAll([' + str(
                        list_var_str
                    ) + '],Implies(And(' + arg_list[0] + ',' + str(
                        list_cstr_str) + '),' + old_arg_list + '))'
            else:
                return 'ForAll([' + list_var_str + '],Implies(' + list_cstr_str + ',' + expression + '))'

    elif w[0] == 'c1':
        var_cstr_map = {}
        equations = []
        expression = expr2z3_update(w[1], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())
        list_cstr_str = cstr_list(var_cstr_map.values())
        if list_var_str is not None and list_cstr_str is not None:
            return 'ForAll([' + list_var_str + '],Implies(' + list_cstr_str + ',' + expression + '))'
        else:
            return expression
    elif w[0] == 'L1':
        var_cstr_map = {}
        flag_constr = False
        lhs = expr2z3_update(w[-2], var_cstr_map)
        rhs = expr2z3_update(w[-1], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())
        if isArrayFunction(w[-2][0]) == True:
            if '_x1' in var_cstr_map.keys():
                del var_cstr_map['_x1']
            flag_constr = True
        if '_s1' in var_cstr_map.keys():
            del var_cstr_map['_s1']
            flag_constr = True
        list_cstr_str = cstr_list(var_cstr_map.values())
        list_cstr_str2 = cstr_list(var_cstr_map.values())
        if list_cstr_str is not None:
            constant = w[2].replace('n', 'L')
            list_cstr_str = 'And(And(' + list_cstr_str + ',' + w[
                2] + '<' + constant + '),' + constant + '>0' + ')'
            list_cstr_str2 = 'And(And(' + list_cstr_str2 + ',' + w[
                2] + '<' + constant + '+1),' + constant + '>0' + ')'
        if 'Or' not in lhs and 'And' not in lhs and 'If' not in lhs and '/' not in lhs:
            lhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(lhs))
        if 'Or' not in rhs and 'And' not in rhs and 'If' not in rhs and '/' not in rhs:
            rhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(rhs))
        if list_var_str is not None and list_cstr_str is not None:
            if w[0] == 'i1':
                return "Implies(" + "ForAll([" + list_var_str + "],Implies(" + list_cstr_str + "," + lhs + ' == ' + rhs + "))" + "," + "ForAll([" + list_var_str + "],Implies(" + list_cstr_str2 + "," + lhs + ' == ' + rhs + "))" + ")"
            else:
                if flag_constr == True:
                    return "Implies(" + "ForAll([" + list_var_str + "],Implies(" + list_cstr_str + "," + lhs + ' == ' + rhs + "))" + "," + "ForAll([" + list_var_str + "],Implies(" + list_cstr_str2 + "," + lhs + ' == ' + rhs + "))" + ")"
                else:
                    return "Implies(" + 'ForAll([' + list_var_str + '],' + lhs + ' == ' + rhs + ")" + "," + 'ForAll([' + list_var_str + '],' + lhs + ' == ' + rhs + ")" + ")"
        else:
            return "Implies(" + lhs + ' == ' + rhs + "," + lhs + ' == ' + rhs + ")"
    elif w[0] == 'L2':
        var_cstr_map = {}
        flag_constr = False
        lhs = expr2z3_update(w[2], var_cstr_map)
        rhs = expr2z3_update(w[3], var_cstr_map)
        list_var_str = qualifier_list(var_cstr_map.keys())

        list_cstr_str = cstr_list(var_cstr_map.values())
        list_cstr_str2 = cstr_list(var_cstr_map.values())

        if list_cstr_str is not None:
            constant = w[1].replace('n', 'L')
            list_cstr_str = 'And(And(' + list_cstr_str + ',' + w[
                1] + '<' + constant + '),' + constant + '>0' + ')'
            list_cstr_str2 = 'And(And(' + list_cstr_str + ',' + w[
                1] + '<' + constant + '+1),' + constant + '>0' + ')'
        if 'Or' not in lhs and 'And' not in lhs and 'If' not in lhs and '/' not in lhs:
            lhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(lhs))
        if 'Or' not in rhs and 'And' not in rhs and 'If' not in rhs and '/' not in rhs:
            rhs = fun_utiles.convert_pow_op_fun(
                fun_utiles.simplify_expand_sympy(rhs))
        if list_var_str is not None and list_cstr_str is not None:
            return "Implies(" + "ForAll([" + list_var_str + "],Implies(" + list_cstr_str + "," + lhs + "))" + "," + "ForAll([" + list_var_str + "],Implies(" + list_cstr_str2 + "," + rhs + "))" + ")"
        else:
            return "Implies(" + lhs + "," + rhs + ")"
    elif w[0] == 'R':
        var_cstr_map = {}
        lhs = expr2z3_update(w[2], var_cstr_map)
        rhs = expr2z3_update(w[3], var_cstr_map)
        list_var_str = qualifier_list(w[1])
        if list_var_str is not None:
            return 'ForAll([' + list_var_str + '],' + lhs + ' == ' + rhs + ")"
        else:
            return lhs + ' == ' + rhs
    elif w[0] == 'RE':
        var_cstr_map = {}
        if len(w[2]) == 0:
            lhs = None
        else:
            lhs = expr2z3_update(w[2], var_cstr_map)
        rhs = expr2z3_update(w[3], var_cstr_map)
        if len(w[1]) == 0:
            list_var_str = None
        else:
            list_var_str = qualifier_list(w[1])
        if list_var_str is not None:
            if lhs != '' and lhs is not None:
                return 'ForAll([' + list_var_str + '],Implies(' + lhs + ',' + rhs + "))"
            else:
                return 'ForAll([' + list_var_str + '],' + rhs + ")"
        elif lhs != '' and lhs is not None:
            return 'Implies(' + lhs + ',' + rhs + ")"
        else:
            return rhs
    else:
        return expression
Exemplo n.º 2
0
def expr2z3_update(e, var_cstr_map):
    args = expr_args(e)
    op = expr_op(e)
    if len(args) == 0:
        if fun_utiles.isvariable(op) == True:
            var_cstr_map[op] = op + ">=0"
        return op
    else:
        if op == 'and' or op == 'or':
            if len(args) == 1:
                expression = expr2z3_update(args[0], var_cstr_map)
                if '/' not in expression and 'Or' not in expression and '==' not in expression and 'And' not in expression and 'If' not in expression and 'Implies' not in expression:
                    expression = fun_utiles.simplify_expand_sympy(expression)
                return expression
            else:
                e_array = []
                for x in args:
                    parameter1 = expr2z3_update(x, var_cstr_map)
                    if '/' not in parameter1 and 'Or' not in parameter1 and '==' not in parameter1 and 'And' not in parameter1 and 'If' not in parameter1 and 'Implies' not in parameter1:
                        parameter1 = fun_utiles.convert_pow_op_fun(
                            fun_utiles.simplify_expand_sympy(parameter1))

                    elif 'Or' not in parameter1 and 'And' not in parameter1 and 'If' not in parameter1 and 'Implies' not in parameter1:
                        parameter1 = fun_utiles.convert_pow_op_fun(parameter1)
                    e_array.append(parameter1)
                if op == 'or':
                    #return 'Or('+parameter1+','+parameter2+')'
                    return fun_utiles.constructAndOr(e_array, 'Or')
                else:
                    if op == 'and':
                        #return 'And('+parameter1+','+parameter2+')'
                        return fun_utiles.constructAndOr(e_array, 'And')
        elif op == 'not' and len(args) == 1:
            expression = expr2z3_update(args[0], var_cstr_map)
            if '/' not in expression and 'Or' not in expression and '==' not in expression and 'And' not in expression and 'If' not in expression and 'Implies' not in expression:
                expression = fun_utiles.convert_pow_op_fun(
                    fun_utiles.simplify_expand_sympy(expression))
            elif 'Or' not in expression and 'And' not in expression and 'If' not in expression and 'Implies' not in expression:
                expression = fun_utiles.convert_pow_op_fun(expression)
            return 'Not(' + expression + ')'
        elif op == 'implies' and len(args) == 2:
            if len(var_cstr_map) == 0:
                expression1 = expr2z3_update(args[0], var_cstr_map)
                expression2 = expr2z3_update(args[1], var_cstr_map)
                if '/' not in expression1 and 'Or' not in expression1 and '==' not in expression1 and 'And' not in expression1 and 'If' not in expression1 and 'Implies' not in expression1:
                    expression1 = fun_utiles.convert_pow_op_fun(
                        fun_utiles.simplify_expand_sympy(expression1))
                elif 'Or' not in expression1 and 'And' not in expression1 and 'If' not in expression1 and 'Implies' not in expression1:
                    expression1 = fun_utiles.convert_pow_op_fun(expression1)
                if '/' not in expression2 and 'Or' not in expression2 and '==' not in expression2 and 'And' not in expression2 and 'If' not in expression2 and 'Implies' not in expression2:
                    expression2 = fun_utiles.convert_pow_op_fun(
                        fun_utiles.simplify_expand_sympy(expression2))
                elif 'Or' not in expression2 and 'And' not in expression2 and 'If' not in expression2 and 'Implies' not in expression2:
                    expression2 = fun_utiles.convert_pow_op_fun(expression2)
                return 'Implies(' + expression1 + ',' + expression2 + ')'
            else:
                list_constrn = ""
                for x in var_cstr_map:
                    if list_constrn == "":
                        expression1 = expr2z3_update(args[0], var_cstr_map)
                        if '/' not in expression1 and 'Or' not in expression1 and '==' not in expression1 and 'And' not in expression1 and 'If' not in expression1 and 'Implies' not in expression1:
                            expression1 = fun_utiles.convert_pow_op_fun(
                                fun_utiles.simplify_expand_sympy(expression1))
                        elif 'Or' not in expression1 and 'And' not in expression1 and 'If' not in expression1 and 'Implies' not in expression1:
                            expression1 = fun_utiles.convert_pow_op_fun(
                                expression1)
                        list_constrn = "And(" + expression1 + "," + var_cstr_map[
                            x] + ")"
                    else:
                        list_constrn = "And(" + list_constrn + "," + var_cstr_map[
                            x] + ")"
                expression2 = expr2z3_update(args[1], var_cstr_map)
                if '/' not in expression2 and 'Or' not in expression2 and '==' not in expression2 and 'And' not in expression2 and 'If' not in expression2 and 'Implies' not in expression2:
                    expression1 = fun_utiles.simplify_expand_sympy(expression2)
                elif 'Or' not in expression2 and 'And' not in expression2 and 'If' not in expression2 and 'Implies' not in expression2:
                    expression2 = fun_utiles.convert_pow_op_fun(expression2)
                return 'Implies(' + list_constrn + ',' + expression2 + ')'
        elif op in _infix_op and len(args) == 2:
            expression1 = expr2z3_update(args[0], var_cstr_map)
            expression2 = expr2z3_update(args[1], var_cstr_map)
            if '/' not in expression1 and 'Or' not in expression1 and '==' not in expression1 and 'And' not in expression1 and 'If' not in expression1 and 'Implies' not in expression1:
                expression1 = fun_utiles.simplify_expand_sympy(expression1)
            if '/' not in expression2 and 'Or' not in expression2 and '==' not in expression2 and 'And' not in expression2 and 'If' not in expression2 and 'Implies' not in expression2:
                expression2 = fun_utiles.simplify_expand_sympy(expression2)
            if op == '/':
                return '((' + expression1 + ')' + op + '(' + expression2 + '))'
            elif op == '**':
                if expression2 == '2':
                    return expression1 + '*' + expression1
                else:
                    return 'power((' + expression1 + ')' + ',' + '(' + expression2 + '))'
            elif op == '=':

                return '((' + expression1 + ')==(' + expression2 + '))'
            else:
                if op == '*':
                    expression = '((' + expression1 + ')' + op + '(' + expression2 + '))'
                else:
                    expression = '((' + expression1 + ')' + op + '(' + expression2 + '))'
                if '/' not in expression and 'Or' not in expression and '==' not in expression and 'And' not in expression and '.' not in expression:
                    return fun_utiles.simplify_expand_sympy(expression)
                else:
                    return expression
        else:
            if op == 'ite':
                return 'If(' + ','.join(
                    list(
                        fun_utiles.conditionSimplifyPower(
                            expr2z3_update(x, var_cstr_map))
                        for x in args)) + ')'
            else:
                if fun_utiles.isArrayFunction(op) == True:
                    parameter_list = []
                    defineDetailtemp = []
                    defineDetailtemp.append(op)
                    parameter_list.append('array')
                    for x in range(0, len(args)):
                        parameter_list.append('int')
                    defineDetailtemp.append(len(args))
                    defineDetailtemp.append(parameter_list)
                    defineDetaillist.append(defineDetailtemp)
                return op + '(' + ','.join(
                    list(expr2z3_update(x, var_cstr_map) for x in args)) + ')'
Exemplo n.º 3
0
def expr2stringvfacteq(e, var_map):
    args = expr_args(e)
    op = expr_op(e)
    if len(args) == 0:
        if op not in var_map.keys() and fun_utiles.is_number(
                op) == False and fun_utiles.is_hex(
                    op) == None and op not in _base:
            element = []
            element.append(op)
            element.append(0)
            element_para = []
            element.append(element_para)
            element_para.append('int')
            #print '----------'
            #print op
            #print element
            #print '----------'
            var_map[op] = element
    else:
        if op == 'and' or op == 'or':
            if len(args) == 1:
                expr2stringvfacteq(args[0], var_map)
            else:
                for x in args:
                    expr2stringvfacteq(x, var_map)
        elif op == 'not' and len(args) == 1:
            expr2stringvfacteq(args[0], var_map)
        elif op == 'implies' and len(args) == 2:
            expr2stringvfacteq(args[0], var_map)
            expr2stringvfacteq(args[1], var_map)
        elif op in _infix_op and len(args) == 2:
            expr2stringvfacteq(args[0], var_map)
            expr2stringvfacteq(args[1], var_map)
        else:
            if fun_utiles.isArrayFunction(op) == True:
                count = 0
                element = []
                element.append(op)
                element.append(len(args))
                element_para = []
                array_type = None
                for parameter in args:
                    if count == 0:
                        element_para.append('array')
                        element_in = []
                        expr2stringvfacteq(parameter, var_map)
                        para_value = expr2string1(parameter, var_map)
                        element_in.append(para_value)
                        element_in.append(0)
                        element_para_in = []
                        element_para_in.append('array')
                        element_in.append(element_para_in)
                        var_map[para_value] = element_in
                        array_type = 'int'
                    else:
                        expr2stringvfacteq(parameter, var_map, allvariablelist,
                                           constraints)
                        typename = 'int'
                        element_para.append(typename)
                    count = count + 1

                element_para.append(array_type)

                element.append(element_para)

                var_map[op] = element

            else:
                if op not in var_map.keys(
                ) and op is not 'ite' and op not in _base:
                    element = []
                    element.append(op)
                    element.append(len(args))
                    element_para = []
                    if len(args) > 0:
                        for x in args:
                            expr2stringvfacteq(x, var_map)
                            element_para.append('int')
                        element_para.append('int')
                    else:
                        element_para.append('int')

                    element.append(element_para)
                    var_map[op] = element

            for x in args:
                expr2stringvfacteq(x, var_map)