Exemplo n.º 1
0
 def _preprocess_(cls, variables, formula):
     variables = tuple(variables)
     if set(variables) != free_variables(formula):
         raise IvyError("Free variables {} must match formula: {}".format(variables, formula))
     if not all(type(v) is Var and first_order_sort(v.sort) for v in variables):
         raise IvyError("Concept variables must be first-order: {}".format(variables))
     return variables, formula
Exemplo n.º 2
0
def get_diagram_concept_domain(sig, diagram):
    """
    sig is an ivy_logic.Sig object
    diagram is a formula
    """
    concepts = OrderedDict()

    concepts['nodes'] = []
    concepts['node_labels'] = []
    concepts['edges'] = []

    # add equality concept
    X = Var('X', TopSort())
    Y = Var('Y', TopSort())
    concepts['='] = Concept([X, Y], Eq(X, Y))

    # add concepts from relations and constants in the signature and
    # in the diagram
    if sig is not None:
        sig_symbols = frozenset(sig.symbols.values())
    else:
        sig_symbols = frozenset()
    for c in sorted(sig_symbols | used_constants(diagram)):
        assert type(c) is Const

        if first_order_sort(c.sort):
            # first order constant, add unary equality concept
            X = Var('X', c.sort)
            name = '{}:{}'.format(c.name, c.sort)
            concepts[name] = Concept([X], Eq(X,c))
            concepts['nodes'].append(name)

        elif type(c.sort) is FunctionSort and c.sort.arity == 1:
            # add unary concept and label
            X = Var('X', c.sort.domain[0])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X], c(X))

        elif type(c.sort) is FunctionSort and c.sort.arity == 2:
            # add binary concept and edge
            X = Var('X', c.sort.domain[0])
            Y = Var('Y', c.sort.domain[1])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X, Y], c(X, Y))

        elif type(c.sort) is FunctionSort and c.sort.arity == 3:
            # add ternary concept
            X = Var('X', c.sort.domain[0])
            Y = Var('Y', c.sort.domain[1])
            Z = Var('Z', c.sort.domain[2])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X, Y, Z], c(X, Y, Z))

        else:
            # skip other symbols
            pass

    return ConceptDomain(concepts, get_standard_combiners(), get_standard_combinations())
Exemplo n.º 3
0
def get_initial_concept_domain(sig):
    """
    sig is an ivy_logic.Sig object
    """
    concepts = OrderedDict()

    concepts['nodes'] = []
    concepts['node_labels'] = []
    concepts['edges'] = []

    # add sort concepts
    for s in sorted(sig.sorts.values()):
        X = Var('X', s)
        concepts[s.name] = Concept([X], Eq(X,X))
        concepts['nodes'].append(s.name)

    # add equality concept
    X = Var('X', TopSort())
    Y = Var('Y', TopSort())
    concepts['='] = Concept([X, Y], Eq(X, Y))

    # add concepts from relations
    for c in sorted(sig.symbols.values()):
        assert type(c) is Const

        if first_order_sort(c.sort):
            # first order constant, add unary equality concept
            X = Var('X', c.sort)
            name = '={}'.format(c.name)
            concepts[name] = Concept([X], Eq(X,c))

        elif type(c.sort) is FunctionSort and c.sort.arity == 1:
            # add unary concept and label
            X = Var('X', c.sort.domain[0])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X], c(X))

        elif type(c.sort) is FunctionSort and c.sort.arity == 2:
            # add binary concept and edge
            X = Var('X', c.sort.domain[0])
            Y = Var('Y', c.sort.domain[1])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X, Y], c(X, Y))

        elif type(c.sort) is FunctionSort and c.sort.arity == 3:
            # add ternary concept
            X = Var('X', c.sort.domain[0])
            Y = Var('Y', c.sort.domain[1])
            Z = Var('Z', c.sort.domain[2])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X, Y, Z], c(X, Y, Z))

        else:
            # skip other symbols
            pass

    return ConceptDomain(concepts, get_standard_combiners(), get_standard_combinations())
Exemplo n.º 4
0
    def _preprocess_(cls, name, variables, formula):
        if not isinstance(name,str):
            raise IvyError("Concept name {} is not a string".format(name))
        variables = tuple(variables)
#        if set(variables) != free_variables(formula):
#            raise IvyError("Free variables {} must match formula: {}".format(variables, formula))
        if not all(type(v) is Var and first_order_sort(v.sort) for v in variables):
            raise IvyError("Concept variables must be first-order: {}".format(variables))
        return name,variables, formula
Exemplo n.º 5
0
def _to_z3(x):
    """
    Convert a term or a sort to a Z3 object.
    """
    print "<bhavya> _to_z3 called"
    if x in _z3_interpreted:
        return _z3_interpreted[x]

    elif type(x) is UninterpretedSort:
        if x not in _z3_uninterpreted_sorts:
            _z3_uninterpreted_sorts[x] = z3.DeclareSort(x.name)
        return _z3_uninterpreted_sorts[x]

    elif type(x) is FunctionSort:
        assert False, "FunctionSort's aren't converted to Z3"

    elif type(x) in (Var, Const) and first_order_sort(x.sort):
        return z3.Const(x.name + ':' + str(x.sort), to_z3(x.sort))

    elif type(x) in (Var, Const) and type(x.sort) is FunctionSort and len(
            x.sort.sorts) == 1:
        # convert to first order
        s = x.sort.sorts[0]
        return z3.Const(x.name + ':' + str(s), to_z3(s))

    elif type(x) in (Var, Const) and type(x.sort) is FunctionSort:
        assert type(
            x
        ) is Const, "Cannot convert high-order variables to Z3, only constants"
        return z3.Function(x.name, *(to_z3(s) for s in x.sort))

    elif type(x) is Apply and len(x.terms) == 0:
        # convert application to use of first order symbol
        return to_z3(x.func)

    elif type(x) is Apply:
        return to_z3(x.func)(*(to_z3(t) for t in x.terms))

    elif type(x) in _z3_operators:
        return _z3_operators[type(x)](*(to_z3(y) for y in x))

    elif type(x) in _z3_quantifiers:
        if len(x.variables) == 0:
            return to_z3(x.body)
        else:
            return _z3_quantifiers[type(x)](
                [to_z3(v) for v in x.variables],
                to_z3(x.body),
            )

    else:
        assert False, type(x)
Exemplo n.º 6
0
def _to_z3(x):
    """
    Convert a term or a sort to a Z3 object.
    """

    if x in _z3_interpreted:
        return _z3_interpreted[x]

    elif type(x) is UninterpretedSort:
        if x not in _z3_uninterpreted_sorts:
            _z3_uninterpreted_sorts[x] = z3.DeclareSort(x.name)
        return _z3_uninterpreted_sorts[x]

    elif type(x) is FunctionSort:
        assert False, "FunctionSort's aren't converted to Z3"

    elif type(x) in (Var, Const) and first_order_sort(x.sort):
        return z3.Const(x.name + ":" + str(x.sort), to_z3(x.sort))

    elif type(x) in (Var, Const) and type(x.sort) is FunctionSort and len(x.sort.sorts) == 1:
        # convert to first order
        s = x.sort.sorts[0]
        return z3.Const(x.name + ":" + str(s), to_z3(s))

    elif type(x) in (Var, Const) and type(x.sort) is FunctionSort:
        assert type(x) is Const, "Cannot convert high-order variables to Z3, only constants"
        return z3.Function(x.name, *(to_z3(s) for s in x.sort))

    elif type(x) is Apply and len(x.terms) == 0:
        # convert application to use of first order symbol
        return to_z3(x.func)

    elif type(x) is Apply:
        return to_z3(x.func)(*(to_z3(t) for t in x.terms))

    elif type(x) in _z3_operators:
        return _z3_operators[type(x)](*(to_z3(y) for y in x))

    elif type(x) in _z3_quantifiers:
        if len(x.variables) == 0:
            return to_z3(x.body)
        else:
            return _z3_quantifiers[type(x)]([to_z3(v) for v in x.variables], to_z3(x.body))

    else:
        assert False, type(x)
Exemplo n.º 7
0
def get_structure_concept_domain(state, sig=None):
    """
    state is an ivy_interp.State with a .universe
    sig is an ivy_logic.Sig object
    """
    concepts = OrderedDict()

    concepts['nodes'] = []
    concepts['node_labels'] = []

    # add equality concept
    X = Var('X', TopSort())
    Y = Var('Y', TopSort())
    concepts['='] = Concept([X, Y], Eq(X, Y))

    # add nodes for universe elements
    elements = [uc for s in state.universe for uc in state.universe[s]]
    for uc in sorted(elements):
        # add unary equality concept
        X = Var('X', uc.sort)
        name = uc.name
        if str(uc.sort) not in name:
            name += ':{}'.format(uc.sort)
        concepts[name] = Concept([X], Eq(X,uc))
        concepts['nodes'].append(name)

    # # find which symbols are equal to which universe constant
    # equals = dict(
    #     (uc, [c for c in symbols
    #           if c != uc and
    #           c.sort == s and
    #           z3_implies(state_formula, Eq(c, uc))])
    #     for s in state.universe
    #     for uc in state.universe[s]
    # )

    # add concepts for relations and constants
    state_formula = state.clauses.to_formula()
    symbols = used_constants(state_formula)
    if sig is not None:
        symbols = symbols | frozenset(sig.symbols.values())
    symbols = symbols - frozenset(elements)
    symbols = sorted(symbols)
    for c in symbols:
        assert type(c) is Const

        if first_order_sort(c.sort):
            # first order constant, add unary equality concept
            X = Var('X', c.sort)
            name = '={}'.format(c.name)
            concepts[name] = Concept([X], Eq(X,c))

        elif type(c.sort) is FunctionSort and c.sort.arity == 1:
            # add unary concept and label
            X = Var('X', c.sort.domain[0])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X], c(X))

        elif type(c.sort) is FunctionSort and c.sort.arity == 2:
            # add binary concept and edge
            X = Var('X', c.sort.domain[0])
            Y = Var('Y', c.sort.domain[1])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X, Y], c(X, Y))

        elif type(c.sort) is FunctionSort and c.sort.arity == 3:
            # add ternary concept
            X = Var('X', c.sort.domain[0])
            Y = Var('Y', c.sort.domain[1])
            Z = Var('Z', c.sort.domain[2])
            name = '{}'.format(c.name)
            concepts[name] = Concept([X, Y, Z], c(X, Y, Z))

        else:
            # skip other symbols
            pass

    return ConceptDomain(concepts, get_standard_combiners(), get_standard_combinations())
Exemplo n.º 8
0
def universe_element_to_concept_name(uc):
    assert first_order_sort(uc.sort)
    name = uc.name
    if str(uc.sort) not in name:
        name += ':{}'.format(uc.sort)
    return name