Exemplo n.º 1
0
	def decode(self, data, shared = None):
		"""
		Decode a term to its SMT representation.
		"""
		if cc.is_symb(data):
			s = "|{}|".format(cc.get_symb(data))
			if s not in self.vars and s not in self.aux_vars:
				self.aux_vars.append(s)
				self.commands.append(["declare-const", s, "Term"])
			return s
		elif cc.is_int(data):
			return ["int", build_int(cc.get_int(data))]
		elif cc.is_float(data):
			return ["real", build_real(cc.get_float(data))]
		elif cc.is_atom(data):
			return ["atom", build_ilist(cc.get_atom_chars(data))]
		elif cc.is_list(data):
			items = cc.get_list_subterms(data)
			if shared is None:
				shared = cc.get_shared(data)
			return ["list", build_tlist([self.decode(item, shared) for item in items])]
		elif cc.is_tuple(data):
			items = cc.get_tuple_subterms(data)
			if shared is None:
				shared = cc.get_shared(data)
			return ["tuple", build_tlist([self.decode(item, shared) for item in items])]
		elif cc.is_bitstring(data):
			return ["str", build_slist(cc.get_bits(data))]
		elif cc.is_alias(data):
			return self.decode(shared[cc.get_alias(data)], shared)
		clg.debug_info("decoding failed: " + str(data))
		assert False
Exemplo n.º 2
0
def pretty(d):
    global scnt, symbs
    try:
        # Symbolic Variable
        if cc.is_symb(d):
            s = cc.get_symb(d)
            if s in symbs:
                return symbs[s]
            else:
                scnt += 1
                x = "x%s" % scnt
                symbs[s] = x
                return x
        # Type
        if cc.is_type_message(d):
            return pretty_type(d)
        # Int
        if cc.is_int(d):
            return str(cc.get_int(d))
        # Float
        if cc.is_float(d):
            return str(cc.get_float(d))
        # Atom
        if cc.is_atom(d):
            cs = cc.get_atom_chars(d)
            return "".join(map(chr, cs))
        # List
        if cc.is_list(d):
            return "[%s]" % pretty_list(cc.get_list_subterms(d))
        # Bitstring
        if cc.is_bitstring(d):
            bits = map(lambda x: 1 if x else 0, cc.get_bits(d))
            return "<<%s>>" % pretty_list(bits)
    except KeyError:
        pass
    return str(d)
Exemplo n.º 3
0
def pretty(d):
    global scnt, symbs
    try:
        # Symbolic Variable
        if cc.is_symb(d):
            s = cc.get_symb(d)
            if s in symbs:
                return symbs[s] + "(" + s + ")"
            else:
                scnt += 1
                x = "x%s" % scnt
                symbs[s] = x
                return x + "(" + s + ")"
        # Type
        if cc.is_type_message(d):
            return pretty_type(d)
        # Int
        if cc.is_int(d):
            return str(cc.get_int(d))
        # Float
        if cc.is_float(d):
            return str(cc.get_float(d))
        # Atom
        if cc.is_atom(d):
            cs = cc.get_atom_chars(d)
            return "".join(map(chr, cs))
        # List
        if cc.is_list(d):
            return "[%s]" % pretty_list(cc.get_list_subterms(d))
        # Bitstring
        if cc.is_bitstring(d):
            bits = map(lambda x: 1 if x else 0, cc.get_bits(d))
            return "<<%s>>" % pretty_list(bits)
    except KeyError:
        pass
    return str(d)
Exemplo n.º 4
0
 def encode(self, data, funs=[]):
     # TODO description
     if data[0] == "int":
         return cc.mk_int(parse_int(data[1]))
     elif data[0] == "real":
         return cc.mk_float(parse_real(data[1]))
     elif data[0] == "atom":
         node = data[1]
         v = []
         while node != "in":
             v.append(parse_int(node[1]))
             node = node[2]
         return cc.mk_atom(v)
     elif data[0] == "list":
         node = data[1]
         v = []
         while node != "tn":
             v.append(self.encode(node[1], funs))
             node = node[2]
         return cc.mk_list(v)
     elif data[0] == "tuple":
         node = data[1]
         v = []
         while node != "tn":
             v.append(self.encode(node[1], funs))
             node = node[2]
         return cc.mk_tuple(v)
     elif data[0] == "str":
         node = data[1]
         v = []
         while node != "sn":
             v.append(node[1] == "true")
             node = node[2]
         return cc.mk_bitstring(v)
     elif data[0] == "fun":
         # TODO function decoding and encoding
         assert isinstance(data, list) and len(data) == 2
         fv = parse_int(data[1])
         # if a cycle (a function calling itself recursively) is found,
         # it is obvious that the solver has selected an arbitrary term as a value
         if fv in funs:
             return cc.mk_any()
         funs = funs[:]
         funs.append(fv)
         # get function info from solver
         # TODO save function arity and entries to an array
         val = self.solver.get_value(["fa", data[1]], ["fm", data[1]])
         assert isinstance(val, list) and len(val) == 2
         assert isinstance(val[0], list) and len(val[0]) == 2
         arity = parse_int(expand_lets(val[0][1]))
         # if arity is less than or greater than 255, we assume it is an arbitrary value selected by the solver
         # because there is no constraint limiting the function's arity; thus, we set it to zero
         if arity < 0 or arity > 255:
             arity = 0
         assert isinstance(val[1], list) and len(val[1]) == 2
         node = expand_lets(val[1][1])
         entries = []
         otherwise = None
         while node != "fn":
             assert isinstance(node,
                               list) and len(node) == 4 and node[0] == "fc"
             x = cc.get_list_subterms(self.encode(["list", node[1]], funs))
             # keep only entries with argument length equal to arity
             if len(x) == arity:
                 y = self.encode(node[2], funs)
                 if otherwise is None:
                     otherwise = y
                 else:
                     entries.append(cc.mk_fun_entry(x, y))
             node = node[3]
         if otherwise is None:
             otherwise = cc.mk_any()
         return cc.mk_fun(arity, entries, otherwise)
     clg.debug_info("encoding failed: " + str(data))
     assert False