def simplify_facts(db, facts, vars_to_keep): assert isinstance(facts, list), facts assert all(isinstance(f, Expression) for f in facts), facts assert isinstance(vars_to_keep, set), vars_to_keep assert all(isinstance(v, Expression) for v in vars_to_keep), vars_to_keep assert all(v.is_var() for v in vars_to_keep), vars_to_keep facts = set(simplify_expr(f) for f in facts) facts = simplify_defs(facts, vars_to_keep) strings = db.simplify([f.polish for f in facts]) facts = set(parse_string_to_expr(s) for s in strings) facts = map(unguard_vars, facts) return facts
def compile_solver(expr, theory): ''' Produces a set of programs that finds values of term satisfying a theory. Inputs: expr - string, an expression with free variables theory - string representing a theory (in .theory format) Outputs: a program to be consumed by the virtual machine Example: expr = 's' theory = """ # 6 constraints = 4 facts + 2 rules LESS APP V s s NLESS x BOT NLESS x I LESS APP s BOT BOT -------------- ---------------- EQUAL APP s I I LESS I APP s x LESS TOP APP s x LESS TOP APP s TOP """ ''' assert isinstance(expr, basestring), expr assert isinstance(theory, basestring), theory expr = desugar_expr(parse_string_to_expr(expr)) assert expr.vars, expr theory = parse_theory_string(theory) facts = map(desugar_expr, theory['facts']) rules = map(desugar_sequent, theory['rules']) sequents = [] can_infer_necessary = not rules and all(f.vars <= expr.vars for f in facts) can_infer_possible = expr.is_var() # TODO generalize to injective exprs if can_infer_necessary: sequents.append(Sequent(facts, [NONEGATE(RETURN(expr))])) if can_infer_possible: fail = NONEGATE(NRETURN(expr)) sequents += [Sequent([], [f, fail]) for f in facts] sequents += [ Sequent(r.antecedents, set_with(r.succedents, fail)) for r in rules ] assert sequents, 'Cannot infer anything' programs = [] write_full_programs(programs, sequents, can_parallelize=False) program = '\n'.join(programs) assert not re.search('FOR_BLOCK', program), 'cannot parallelize' return program
def parse_expr(string): return desugar_expr(parse_string_to_expr(string))
def pythonize_expr(proto_expr): return parse_string_to_expr(proto_expr.polish)
def desugar(string): expr = parse_string_to_expr(string) expr = desugar_expr(expr) expr = guard_vars(expr) return str(expr)
def test_hole_filler_increases_complexity(example): term = parse_string_to_expr(example[0]) fillings = list(fill_holes(term)) for f in fillings: assert evaluate_complexity(term) < evaluate_complexity(f)
def test_hole_filler(example): term = parse_string_to_expr(example[0]) expected = map(parse_string_to_expr, example[1]) actual = list(fill_holes(term)) assert actual == expected
def test_complexity_evaluator(example): term = parse_string_to_expr(example) assert 0 < evaluate_complexity(term)
def _db_simplify_expr(db, expr): string = db.simplify([expr.polish])[0] expr = parse_string_to_expr(string) expr = unguard_vars(expr) return expr