Esempio n. 1
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())
Esempio n. 2
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())
Esempio n. 3
0
    def _get_witnesses(self, concept_name):
        # TODO: maybe this function should be in ConceptDomain? or Concept?
        """
        Return a list of constant that are witnesses for the given unary
        constant, or [] if none are found

        A witness is a constant c s.t. concept(x) implies x=c. Note
        that this does not necessarily mean that concept(c) holds.
        """
        concept = self.domain.concepts[concept_name]
        assert concept.arity == 1
        sort = concept.variables[0].sort
        assert sort != TopSort()

        constants = used_constants(concept.formula)
        x = Const(self._fresh_const_name(constants), sort)
        f = concept(x)

        def is_witness(c):
            try:
                return z3_implies(f, Eq(x, c))
            except SortError:
                return False

        return [c for c in constants if is_witness(c)]
Esempio n. 4
0
def convert_from_sortvars(s):
    """
    Convert sort vars to TopSort
    """
    s = find(s)
    if type(s) is SortVar:
        return TopSort()
    elif type(s) is FunctionSort:
        return FunctionSort(*(convert_from_sortvars(x) for x in s))
    else:
        return s
Esempio n. 5
0
 def _materialize_node(self, concept_name):
     """
     Materialize a node, returns the witness constant
     """
     concept = self.domain.concepts[concept_name]
     assert concept.arity == 1
     sort = concept.variables[0].sort
     assert sort != TopSort()
     witnesses = self._get_witnesses(concept_name)
     if len(witnesses) > 0:
         c = witnesses[0]
     else:
         c = Const(self._fresh_const_name(), sort)
         # TODO: maybe we shouldn't split here, and create the concepts explicitly
         X = Var('X', c.sort)
         name = '={}'.format(c.name)
         self.domain.concepts[name] = Concept(name, [X], Eq(X, c))
         self.domain.split(concept_name, name)
     self.suppose(concept(c))
     return c
Esempio n. 6
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())
Esempio n. 7
0
def get_standard_combiners():
    T = TopSort()
    UnaryRelation = FunctionSort(T, Boolean)
    BinaryRelation = FunctionSort(T, T, Boolean)
    X, Y, Z = (Var(n, T) for n in ['X', 'Y', 'Z'])
    U = Var('U', UnaryRelation)
    U1 = Var('U1', UnaryRelation)
    U2 = Var('U2', UnaryRelation)
    B = Var('B', BinaryRelation)
    B1 = Var('B1', BinaryRelation)
    B2 = Var('B2', BinaryRelation)
    result = OrderedDict()

    result['none'] = ConceptCombiner([U], Not(Exists([X], U(X))))
    result['at_least_one'] = ConceptCombiner([U], Exists([X], U(X)))
    result['at_most_one'] = ConceptCombiner([U], ForAll([X,Y], Implies(And(U(X), U(Y)), Eq(X,Y))))

    result['node_necessarily'] = ConceptCombiner(
        [U1, U2],
        ForAll([X], Implies(U1(X), U2(X))),
    )
    result['node_necessarily_not'] = ConceptCombiner(
        [U1, U2],
        ForAll([X], Implies(U1(X), Not(U2(X)))),
    )


    result['mutually_exclusive'] = ConceptCombiner(
        [U1, U2],
        ForAll([X, Y], Not(And(U1(X), U2(Y))))
    )

    result['all_to_all'] = ConceptCombiner(
        [B, U1, U2],
        ForAll([X,Y], Implies(And(U1(X), U2(Y)), B(X,Y)))
    )
    result['none_to_none'] = ConceptCombiner(
        [B, U1, U2],
        ForAll([X,Y], Implies(And(U1(X), U2(Y)), Not(B(X,Y))))
    )
    result['total'] = ConceptCombiner(
        [B, U1, U2],
        ForAll([X], Implies(U1(X), Exists([Y], And(U2(Y), B(X,Y)))))
    )
    result['functional'] = ConceptCombiner(
        [B, U1, U2],
        ForAll([X, Y, Z], Implies(And(U1(X), U2(Y), U2(Z), B(X,Y), B(X,Z)), Eq(Y,Z)))
    )
    result['surjective'] = ConceptCombiner(
        [B, U1, U2],
        ForAll([Y], Implies(U2(Y), Exists([X], And(U1(X), B(X,Y)))))
    )
    result['injective'] = ConceptCombiner(
        [B, U1, U2],
        ForAll([X, Y, Z], Implies(And(U1(X), U1(Y), U2(Z), B(X,Z), B(Y,Z)), Eq(X,Y)))
    )

    result['node_info'] = ['none', 'at_least_one', 'at_most_one']
    if False:
        # this just slows us down, and it's not clear it's needed
        # later this should be made customizable by the user
        result['edge_info'] = ['all_to_all', 'none_to_none', 'total',
                               'functional', 'surjective', 'injective']
    else:
        result['edge_info'] = ['all_to_all', 'none_to_none']

    result['node_label'] = ['node_necessarily', 'node_necessarily_not']

    return result
Esempio n. 8
0
    result = dict()
    count = defaultdict(int)
    for c in elements:
        prefix = str(c.sort).lower()
        result[c.name] = prefix + str(count[prefix])
        count[prefix] += 1

    return result


if __name__ == '__main__':
    def test(st):
        print st, "=", eval(st)

    S = UninterpretedSort('S')
    T = TopSort()
    UnaryRelationS = FunctionSort(S, Boolean)
    BinaryRelationS = FunctionSort(S, S, Boolean)
    UnaryRelationT = FunctionSort(T, Boolean)
    BinaryRelationT = FunctionSort(T, T, Boolean)


    X, Y, Z = (Var(n, S) for n in ['X', 'Y', 'Z'])

    r = Const('r', BinaryRelationS)
    n = Const('n', BinaryRelationS)
    p = Const('p', UnaryRelationS)
    q = Const('q', UnaryRelationS)
    u = Const('u', UnaryRelationS)

    c11 = Concept([X], And(p(X), q(X)))
Esempio n. 9
0
def infer_sorts(t, env=None):
    """
    Infer the sort of term t in environment env.

    env maps symbol names to sort variables.

    The result is a pair: (s, tt) where s is a sort or sort variable
    with the sort of t in env, and tt is a closure that, when called,
    will concretize t according to inferred sort information at its
    call time.

    If env is not None, it must contain all the free variables and
    constants used in t.
    """

    if env is None:
        names = free_variables(t,
                               by_name=True).union(x.name
                                                   for x in used_constants(t))
        env = dict((name, SortVar()) for name in names)

    if type(t) in (Var, Const):
        if is_polymorphic(t):  # each instance can have different sort
            s = insert_sortvars(t.sort, {})
        else:
            s = env[t.name]
            unify(s, t.sort)
        return s, lambda: type(t)(t.name, convert_from_sortvars(s))

    elif type(t) is Apply:
        func_s, func_t = infer_sorts(t.func, env)
        xys = [infer_sorts(tt, env) for tt in t.terms]
        terms_s = [x for x, y in xys]
        terms_t = [y for x, y in xys]
        sorts = terms_s + [SortVar()]
        unify(func_s, FunctionSort(*sorts))
        return sorts[-1], lambda: Apply(func_t(), *(x() for x in terms_t))

    elif type(t) is Eq:
        s1, t1 = infer_sorts(t.t1, env)
        s2, t2 = infer_sorts(t.t2, env)
        unify(s1, s2)
        return Boolean, lambda: Eq(t1(), t2())

    elif type(t) is Ite:
        s_cond, t_cond = infer_sorts(t.cond, env)
        s_then, t_then = infer_sorts(t.t_then, env)
        s_else, t_else = infer_sorts(t.t_else, env)
        unify(s_cond, Boolean)
        unify(s_then, s_else)
        return s_then, lambda: Ite(t_cond(), t_then(), t_else())

    elif type(t) in (Not, And, Or, Implies, Iff):
        xys = [infer_sorts(tt, env) for tt in t]
        terms_s = [x for x, y in xys]
        terms_t = [y for x, y in xys]
        for s in terms_s:
            unify(s, Boolean)
        return Boolean, lambda: type(t)(*[x() for x in terms_t])

    elif type(t) in (ForAll, Exists):
        # create a copy of the environment and shadow that quantified
        # variables
        env = env.copy()
        env.update((v.name, SortVar()) for v in t.variables)
        vars_t = [infer_sorts(v, env)[1] for v in t.variables]
        body_s, body_t = infer_sorts(t.body, env)
        unify(body_s, Boolean)
        return Boolean, lambda: type(t)(
            [x() for x in vars_t],
            body_t(),
        )

    elif hasattr(t, 'clone'):
        xys = [infer_sorts(tt, env) for tt in t.args]
        terms_t = [y for x, y in xys]
        return TopSort(), lambda: t.clone([x() for x in terms_t])

    else:
        assert False, type(t)