def axiom_to_function(self, axiom: Axiom): """ Take an axiom and give back a Python function that gives the truth value when called with actual instances of the variable (preserve order of the quantifiers) :param axiom: axiom to transform :return: Callable Python function """ parser = Parser() rpn_left = parser.expr_to_rpn(self, axiom.left) rpn_right = parser.expr_to_rpn(self, axiom.right) left_fun = self.arithmetic_function_generator.make(rpn_left) right_fun = self.arithmetic_function_generator.make(rpn_right) return lambda **kwargs: self.equal(left_fun(**kwargs), right_fun(**kwargs))
def make_table(self, expr_, check_cache=False, cache_sub=False): """ WARNING : if the Expr is `Ax Ay Az x*y` this method will return a table of dim 2, avoiding unnecessary z dimension. :param expr_: Expr, expression to make a table with :param check_cache: optional use od model's cache_manager. If True, this method will check for every sub-expression if it's not already cached :param cache_sub: if true, will cache every sub_expression not yet cached :return: the table of expr """ parser = Parser() rpn = parser.expr_to_rpn(self.model, expr_) table_function = self.model.arithmetic_function_generator.make(rpn) ndim = 0 rpn_repr = [s.repr for s in rpn] expr_variables = [] for v in expr_.variables: if v.repr in rpn_repr: ndim += 1 expr_variables.append(v) # we need to preserve the order table_array = empty([self.model.cardinal] * ndim) table = Table(table_array, self.model.elements, ndim=ndim, special_char=self.model.special_char) if check_cache: warn("Warning : check_cache not supported yet") if cache_sub: warn("Warning : cache_sub not supported yet") for variable_instances in product(self.model.elements, repeat=ndim): val = table_function(**{ expr_variables[i].repr: variable_instances[i] for i in range(ndim) }) table.update(*variable_instances, new=val) return table
class AxiomVerifierSAT: def __init__(self, axiom: Axiom, model): self.model = model self.axiom = axiom self.parser = Parser() self.left_rpn = [ tok.repr for tok in self.parser.expr_to_rpn(axiom.left, self.model) ] self.right_rpn = [ tok.repr for tok in self.parser.expr_to_rpn(axiom.right, self.model) ] self.literals_list = [] self.term_dictionary = TermDictionary() var_value_map = { "*": self.model.mul, "/": self.model.rdiv, "\\": self.model.ldiv_lexical_order } for i, instance in enumerate( product(self.model.elements, repeat=self.axiom.ndim)): for var, val in zip(self.axiom.variables, instance): var_value_map[var.repr] = val left = [] right = [] for tok in self.left_rpn: left.append(var_value_map[tok]) for tok in self.right_rpn: right.append(var_value_map[tok]) self.literals_list.append( Literal(left, right, id_=i, term_dict=self.term_dictionary))
def compute_rpn(self, model): parser = Parser() self.rpn = parser.expr_to_rpn(model, self)