Exemplo n.º 1
0
def model_facts(h, ignore, clauses1, upclose=False):
    # define the universe for each sort:
    uc = [fact for s in h.sorts() for fact in model_universe_facts(h, s, upclose)]
    # values of constants in formula
    temp = [
        [(ivy_logic.Constant(c), get_model_constant(h.model, ivy_logic.Constant(c)))]
        for c in used_constants_clauses(clauses1)
        if not ignore(c) and c not in ivy_logic.sig.constructors
    ]
    #    print "temp = {}".format(temp)
    vc = [
        [ivy_logic._eq_lit(ivy_logic.Constant(c), get_model_constant(h.model, ivy_logic.Constant(c)))]
        for c in used_constants_clauses(clauses1)
        if not ignore(c) and c not in ivy_logic.sig.constructors
    ]
    #    print "model_facts vc = {}".format(vc)
    # values of relations in formula
    #    print "used_relations_clauses = {}".format(used_relations_clauses(clauses1))
    urc = dict((ivy_logic.normalize_symbol(r), n) for r, n in used_relations_clauses(clauses1).iteritems())
    vr = [[l] for (r, n) in urc.iteritems() if not ignore(r) for l in relation_model_to_clauses(h, r, n)]
    # values of functions in formula
    fns = set(f for (f, arity) in functions_clauses(clauses1) if not ignore(f) and arity >= 1)
    vf = [[l] for f in fns for l in function_model_to_clauses(h, f)]
    res = uc + vc + vr + vf
    return Clauses(res)
Exemplo n.º 2
0
def model_facts(h, ignore, clauses1, upclose=False):
    # define the universe for each sort:
    uc = [
        fact for s in h.sorts()
        for fact in model_universe_facts(h, s, upclose)
    ]
    # values of constants in formula
    temp = [[(ivy_logic.Constant(c),
              get_model_constant(h.model, ivy_logic.Constant(c)))]
            for c in used_constants_clauses(clauses1)
            if not ignore(c) and c not in ivy_logic.sig.constructors]
    #    print "temp = {}".format(temp)
    vc = [[
        ivy_logic._eq_lit(ivy_logic.Constant(c),
                          get_model_constant(h.model, ivy_logic.Constant(c)))
    ] for c in used_constants_clauses(clauses1)
          if not ignore(c) and c not in ivy_logic.sig.constructors]
    #    print "model_facts vc = {}".format(vc)
    # values of relations in formula
    #    print "used_relations_clauses = {}".format(used_relations_clauses(clauses1))
    vr = [[l] for (r, n) in used_relations_clauses(clauses1).iteritems()
          if not ignore(r) for l in relation_model_to_clauses(h, r, n)]
    # values of functions in formula
    fns = set(f for (f, arity) in functions_clauses(clauses1)
              if not ignore(f) and arity >= 1)
    vf = [[l] for f in fns for l in function_model_to_clauses(h, f)]
    res = uc + vc + vr + vf
    return Clauses(res)
def interp_from_unsat_core(clauses1, clauses2, core, interpreted):
    used_syms = used_symbols_clauses(core)
    vars = used_variables_clauses(core)
    if vars:
        #        print "interpolant would require skolem constants"
        return None
    core_consts = used_constants_clauses(core)
    clauses2_consts = used_constants_clauses(clauses2)
    #    print "interp_from_unsat_core core_consts = {}".format(map(str,core_consts))
    #    print "interp_from_unsat_core clauses2_consts = {}".format(map(str,clauses2_consts))
    renaming = dict()
    i = 0
    for v in core_consts:
        if v not in clauses2_consts or v.is_skolem(
        ):  # and v not in interpreted:
            renaming[v] = Variable('V' + str(i), Constant(v).get_sort())
            i += 1


#    print "interp_from_unsat_core core = {}".format(core)
#    print "interp_from_unsat_core renaming = {}".format(renaming)
    renamed_core = substitute_constants_clauses(core, renaming)
    #    print "interp_from_unsat_core renamed_core = {}".format(renamed_core)
    res = simplify_clauses(
        Clauses([Or(*[negate(c) for c in renamed_core.fmlas])]))
    #    print "interp_from_unsat_core res = {}".format(res)
    return res
Exemplo n.º 4
0
 def string_to_concept(self,text):
     # set up the signature with symbols in graph so
     # we can parse the formula.
     sig = il.sig.copy()
     with sig:
         for c in ilu.used_constants_clauses(self.state):
             if not isinstance(c.sort,il.EnumeratedSort) and not c.is_numeral():
                 il.add_symbol(str(c),c.sort)
         for c in ilu.used_constants_clauses(self.constraints) and not c.is_numeral():
             if not isinstance(c.sort,il.EnumeratedSort):
                 il.add_symbol(str(c),c.sort)
         return concept_from_formula(ilu.to_formula(text))
Exemplo n.º 5
0
    def string_to_concept(self,text):
        # set up the signature with symbols in graph so
        # we can parse the formula.
        sig = ivy_logic.sig.copy()
        with sig:
            for c in used_constants_clauses(self.state):
                if not isinstance(c.sort,ivy_logic.EnumeratedSort):
                    ivy_logic.add_symbol(str(c),c.sort)
            for c in used_constants_clauses(self.constraints):
                if not isinstance(c.sort,ivy_logic.EnumeratedSort):
                    ivy_logic.add_symbol(str(c),c.sort)

            return to_literal(text)
Exemplo n.º 6
0
 def string_to_concept(self, text):
     # set up the signature with symbols in graph so
     # we can parse the formula.
     sig = il.sig.copy()
     with sig:
         for c in ilu.used_constants_clauses(self.state):
             if not isinstance(c.sort,
                               il.EnumeratedSort) and not c.is_numeral():
                 il.add_symbol(str(c), c.sort)
         for c in ilu.used_constants_clauses(
                 self.constraints) and not c.is_numeral():
             if not isinstance(c.sort, il.EnumeratedSort):
                 il.add_symbol(str(c), c.sort)
         return concept_from_formula(ilu.to_formula(text))
Exemplo n.º 7
0
def numeral_assign(clauses, h):
    num_by_sort = defaultdict(list)
    numerals = [c for c in used_constants_clauses(clauses) if c.is_numeral()]
    for num in numerals:
        num_by_sort[num.sort].append(num)
    #    print "num_by_sort: {}".format(numerals)
    foom = dict()
    used = set()
    #    print "starting: foom = {}".format(foom)
    for s in h.sorts():
        #        print "na sort: {}".format(repr(s))
        if ivy_logic.is_interpreted_sort(s):
            continue
        #        print "sort loop: sort = {}, foom = {}".format(s,foom)
        for num in num_by_sort[s]:
            #            print "foom = {}".format(foom)
            numv = h.eval_constant(num)
            #            print "eval: {}:{} = {}".format(num,num.sort,numv)
            if numv in foom:
                print "two numerals assigned same value!: {} = {}".format(num, foom[numv])
            else:
                #                print "assigning {} to {}".format(num,numv)
                foom[numv] = num
            used.add(num)
        i = 0
        for c in h.sorted_sort_universe(s):
            if c not in foom:
                while True:
                    num = ivy_logic.Constant(c.rep.rename(lambda s: str(i)))
                    i = i + 1
                    if num not in used:
                        foom[c.rep] = num
                        break
    return foom
Exemplo n.º 8
0
def numeral_assign(clauses,h):
    num_by_sort = defaultdict(list)
    numerals = [c for c in used_constants_clauses(clauses) if c.is_numeral()]
    for num in numerals:
        num_by_sort[num.sort].append(num)
#    print "num_by_sort: {}".format(numerals)
    foom = dict()
    used = set()
#    print "starting: foom = {}".format(foom)
    for s in h.sorts():
#        print "na sort: {}".format(repr(s))
        if ivy_logic.is_interpreted_sort(s):
            continue
#        print "sort loop: sort = {}, foom = {}".format(s,foom)
        for num in num_by_sort[s]:
#            print "foom = {}".format(foom)
            numv = h.eval_constant(num)
#            print "eval: {}:{} = {}".format(num,num.sort,numv)
            if numv in foom:
                print "two numerals assigned same value!: {} = {}".format(num,foom[numv])
            else:
#                print "assigning {} to {}".format(num,numv)
                foom[numv] = num
            used.add(num)
        i = 0
        for c in h.sorted_sort_universe(s):
            if c not in foom:
                while True:
                    num = ivy_logic.Constant(c.rep.rename(lambda s:str(i)))
                    i = i + 1
                    if num not in used:
                        foom[c.rep] = num
                        break
    return foom
Exemplo n.º 9
0
 def splatter(self, node, constants=None):
     if constants == None:
         constants = used_constants_clauses(self.constraints)
     eqs = [
         eq_lit(node.variable('X'), Constant(c)) for c in constants
         if c.sort == node.sort
     ]
     #        print "splatter eqs = {}".format(eqs)
     self.split_n_way(node, eqs)
Exemplo n.º 10
0
def clauses_model_to_diagram(clauses1,ignore = None, implied = None,model = None,axioms=None,weaken=True,numerals=True):
    """ Return a diagram of a model of clauses1 or None.  The function "ignore", if
    provided, returns true for symbols that should be ignored in the
    diagram.
    """
#    print "clauses_model_to_diagram clauses1 = {}".format(clauses1)
    if axioms == None:
        axioms = true_clauses()
    h = model_if_none(and_clauses(clauses1,axioms),implied,model)
    ignore = ignore if ignore != None else lambda x: False
    res = model_facts(h,(lambda x: False),clauses1,upclose=True) # why not pass axioms?
#    print "clauses_model_to_diagram res = {}".format(res)
    # find representative elements
    # find representatives of universe elements
    if numerals:
        reps = numeral_assign(res,h)
    else:
        reps = dict()
        for c in used_constants_clauses(clauses1):
    #        print "constant: {}".format(c)
            mc = get_model_constant(h.model,ivy_logic.Constant(c))
    #        print "value: {}".format(mc)
            if mc.rep not in reps or reps[mc.rep].rep.is_skolem() and not c.is_skolem():
                reps[mc.rep] = ivy_logic.Constant(c)
        for s in h.sorts():
            for e in h.sort_universe(s):
                if e.rep not in reps:
                    reps[e.rep] = e.rep.skolem()()
#    print "clauses_model_to_diagram reps = {}".format(reps)
    # filter out clauses using universe elements without reps
#    res = [cls for cls in res if all(c in reps for c in used_constants_clause(cls))]
    # replace universe elements with their reps
#    print "clauses_model_to_diagram res = {}".format(res)
    res = substitute_constants_clauses(res,reps)
    # filter defined skolems
    # this caused a bug in the leader example. the generated diagram did not satisfy clauses1
    res.fmlas = [f for f in res.fmlas if not any((x.is_skolem() and x in clauses1.defidx) for x in used_symbols_ast(f))]
#    print "clauses_model_to_diagram res = {}".format(res)
    uc = Clauses([[ivy_logic._eq_lit(ivy_logic.Variable('X',c.get_sort()),reps[c.rep])
                   for c in h.sort_universe(s)] for s in h.sorts()])
#    print "clauses_model_to_diagram uc = {}".format(uc)

    #    uc = true_clauses()
    if weaken:
        res = unsat_core(res,and_clauses(uc,axioms),clauses1) # implied not used here
#    print "clauses_model_to_diagram res = {}".format(res)

#    print "foo = {}".format(unsat_core(and_clauses(uc,axioms),true_clauses(),clauses1))

    # filter out non-rep skolems
    repset = set(c.rep for e,c in reps.iteritems())
#    print "clauses_model_to_diagram repset = {}".format(repset)
    ign = lambda x,ignore=ignore: (ignore(x) and not x in repset)
    res = Clauses([cl for cl in res.fmlas if not any(ign(c) for c in used_symbols_ast(cl))])
#    print "clauses_model_to_diagram res = {}".format(res)
    return res
Exemplo n.º 11
0
 def splatter(self,node,constants = None):
     if constants == None:
         constants = ilu.used_constants_clauses(self.constraints)
     concepts = self.concept_session.domain.concepts
     name = node.name + '.splatter'
     concepts[name] = enum_concepts(name,None,None)
     for cons in constants:
         c1 = concept_from_formula(Equals(Variable('X',node.sort),cons))
         concepts[c1.name] = c1
         concepts[name].append(c1.name)
     self.concept_session.domain.split(node.name,name)
     self.recompute()
Exemplo n.º 12
0
 def splatter(self, node, constants=None):
     if constants == None:
         constants = ilu.used_constants_clauses(self.constraints)
     concepts = self.concept_session.domain.concepts
     name = node.name + '.splatter'
     concepts[name] = enum_concepts(name, None, None)
     for cons in constants:
         c1 = concept_from_formula(Equals(Variable('X', node.sort), cons))
         concepts[c1.name] = c1
         concepts[name].append(c1.name)
     self.concept_session.domain.split(node.name, name)
     self.recompute()
Exemplo n.º 13
0
def interp_from_unsat_core(clauses1,clauses2,core,interpreted):
    used_syms = used_symbols_clauses(core)
    vars = used_variables_clauses(core)
    if vars:
#        print "interpolant would require skolem constants"
        return None
    core_consts = used_constants_clauses(core)
    clauses2_consts = used_constants_clauses(clauses2)
#    print "interp_from_unsat_core core_consts = {}".format(map(str,core_consts))
#    print "interp_from_unsat_core clauses2_consts = {}".format(map(str,clauses2_consts))
    renaming = dict()
    i = 0
    for v in core_consts:
        if v not in clauses2_consts or v.is_skolem(): # and v not in interpreted:
            renaming[v] = Variable('V' + str(i),Constant(v).get_sort())
            i += 1
#    print "interp_from_unsat_core core = {}".format(core)
#    print "interp_from_unsat_core renaming = {}".format(renaming)
    renamed_core = substitute_constants_clauses(core,renaming)
#    print "interp_from_unsat_core renamed_core = {}".format(renamed_core)
    res = simplify_clauses(Clauses([Or(*[negate(c) for c in renamed_core.fmlas])]))
#    print "interp_from_unsat_core res = {}".format(res)
    return res
Exemplo n.º 14
0
    def get_predicates(self,clauses):
#        print "get_predicates: {}".format(clauses)
        d = self.parent_state.domain
        sig = d.sig
        urs = [x for x in used_unary_relations_clauses(clauses) if not is_skolem(x)]
        cs = [x for x in used_constants_clauses(clauses)
              if not is_skolem(x) and not has_enumerated_sort(sig,x) and not x.is_numeral()]
        ufs = [x for x in used_unary_functions_clauses(clauses)
               if not is_skolem(x) and  has_enumerated_sort(sig,x)]
        nrs = [x for x,arity in d.relations.iteritems() if arity == 0]
        union_to_list(urs,[x for x,arity in d.relations.iteritems() if arity == 1])
        union_to_list(cs,[x for x,arity in d.functions.iteritems()
                          if arity == 0 and not has_enumerated_sort(sig,x)])
        union_to_list(ufs,[x for x,arity in d.functions.iteritems()
                           if arity == 1 and has_enumerated_sort(sig,x)])
#        print "ufs: {}".format(ufs)
        ccs = [Constant(c) for c in cs]
#        print "sorts: {}".format([(c,c.get_sort()) for c in ccs])
        return ([Literal(1,Atom(c,[])) for c in nrs] +
                [Literal(1,Atom(equals,[Variable("X",c.get_sort()),c])) for c in ccs] +
                [Literal(1,Atom(r,[Variable("X",r.sort.dom[0])])) for r in urs] +
                [(App(f,Variable('X',f.sort.dom[0])),[Constant(Symbol(x,f.sort.rng)) for x in f.sort.rng.defines()]) for f in ufs])
Exemplo n.º 15
0
 def get_predicates(self, clauses):
     #        print "get_predicates: {}".format(clauses)
     d = self.parent_state.domain
     sig = d.sig
     urs = [
         x for x in used_unary_relations_clauses(clauses)
         if not is_skolem(x)
     ]
     cs = [
         x for x in used_constants_clauses(clauses) if not is_skolem(x)
         and not has_enumerated_sort(sig, x) and not x.is_numeral()
     ]
     ufs = [
         x for x in used_unary_functions_clauses(clauses)
         if not is_skolem(x) and has_enumerated_sort(sig, x)
     ]
     nrs = [x for x, arity in d.relations.iteritems() if arity == 0]
     union_to_list(
         urs, [x for x, arity in d.relations.iteritems() if arity == 1])
     union_to_list(cs, [
         x for x, arity in d.functions.iteritems()
         if arity == 0 and not has_enumerated_sort(sig, x)
     ])
     union_to_list(ufs, [
         x for x, arity in d.functions.iteritems()
         if arity == 1 and has_enumerated_sort(sig, x)
     ])
     #        print "ufs: {}".format(ufs)
     ccs = [Constant(c) for c in cs]
     #        print "sorts: {}".format([(c,c.get_sort()) for c in ccs])
     return ([Literal(1, Atom(c, [])) for c in nrs] + [
         Literal(1, Atom(equals, [Variable("X", c.get_sort()), c]))
         for c in ccs
     ] + [Literal(1, Atom(r, [Variable("X", r.sort.dom[0])]))
          for r in urs] + [(App(f, Variable('X', f.sort.dom[0])), [
              Constant(Symbol(x, f.sort.rng)) for x in f.sort.rng.defines()
          ]) for f in ufs])
Exemplo n.º 16
0
    def splatter(self,node,constants = None):
        if constants == None:
            constants = used_constants_clauses(self.constraints)
        eqs = [eq_lit(node.variable('X'),Constant(c)) for c in constants if c.sort == node.sort]
#        print "splatter eqs = {}".format(eqs)
        self.split_n_way(node,eqs)
Exemplo n.º 17
0
def clauses_model_to_diagram(clauses1,
                             ignore=None,
                             implied=None,
                             model=None,
                             axioms=None,
                             weaken=True):
    """ Return a diagram of a model of clauses1 or None.  The function "ignore", if
    provided, returns true for symbols that should be ignored in the
    diagram.
    """
    print "clauses_model_to_diagram clauses1 = {}".format(clauses1)
    if axioms == None:
        axioms = true_clauses
    h = model_if_none(and_clauses(clauses1, axioms), implied, model)
    ignore = ignore if ignore != None else lambda x: False
    res = model_facts(h, (lambda x: False), clauses1,
                      upclose=True)  # why not pass axioms?
    print "clauses_model_to_diagram res = {}".format(res)
    # find representative elements
    # find representatives of universe elements
    reps = dict()
    for c in used_constants_clauses(clauses1):
        #        print "constant: {}".format(c)
        mc = get_model_constant(h.model, ivy_logic.Constant(c))
        #        print "value: {}".format(mc)
        if mc.rep not in reps or reps[
                mc.rep].rep.is_skolem() and not c.is_skolem():
            reps[mc.rep] = ivy_logic.Constant(c)
    for s in h.sorts():
        for e in h.sort_universe(s):
            if e.rep not in reps:
                reps[e.rep] = e.rep.skolem()()
    print "clauses_model_to_diagram reps = {}".format(reps)
    # filter out clauses using universe elements without reps
    #    res = [cls for cls in res if all(c in reps for c in used_constants_clause(cls))]
    # replace universe elements with their reps
    print "clauses_model_to_diagram res = {}".format(res)
    res = substitute_constants_clauses(res, reps)
    # filter defined skolems
    # this caused a bug in the leader example. the generated diagram did not satisfy clauses1
    res.fmlas = [
        f for f in res.fmlas
        if not any((x.is_skolem() and x in clauses1.defidx)
                   for x in used_symbols_ast(f))
    ]
    print "clauses_model_to_diagram res = {}".format(res)
    uc = Clauses([[
        ivy_logic._eq_lit(ivy_logic.Variable('X', c.get_sort()), reps[c.rep])
        for c in h.sort_universe(s)
    ] for s in h.sorts()])
    print "clauses_model_to_diagram uc = {}".format(uc)

    #    uc = true_clauses()
    if weaken:
        res = unsat_core(res, and_clauses(uc, axioms),
                         clauses1)  # implied not used here
    print "clauses_model_to_diagram res = {}".format(res)

    #    print "foo = {}".format(unsat_core(and_clauses(uc,axioms),true_clauses(),clauses1))

    # filter out non-rep skolems
    repset = set(c.rep for e, c in reps.iteritems())
    print "clauses_model_to_diagram repset = {}".format(repset)
    ign = lambda x, ignore=ignore: (ignore(x) and not x in repset)
    res = Clauses([
        cl for cl in res.fmlas
        if not any(ign(c) for c in used_symbols_ast(cl))
    ])
    print "clauses_model_to_diagram res = {}".format(res)
    return res