Пример #1
0
def define_a(
        max_solutions=15,
        max_memory=pomagma.analyst.synthesize.MAX_MEMORY,
        verbose=1,
        address=pomagma.analyst.ADDRESS):
    '''
    Search for definition of A = Join {<r, s> | r o s [= I}.
    Tip: use pyprofile and snakeviz to profile this function:
    $ pyprofile -o define_a.pstats -s time src/examples/synthesize.py define_a
    $ snakeviz define_a.pstats
    '''
    assert max_solutions > 0, max_solutions
    assert 0 < max_memory and max_memory < 1, max_memory
    facts = parse_facts(A_THEORY)
    hole = Expression.make('hole')
    initial_sketch = parse_expr('HOLE')
    language = {
        'APP': 1.0,
        # 'COMP': 1.6,
        'JOIN': 3.0,
        'B': 1.0,
        'C': 1.3,
        'A': 2.0,
        'BOT': 2.0,
        'TOP': 2.0,
        'I': 2.2,
        # 'Y': 2.3,
        'K': 2.6,
        'S': 2.7,
        'J': 2.8,
        'DIV': 3.0,
    }
    with pomagma.analyst.connect(address) as db:
        results = synthesize_from_facts(
            db=db,
            facts=facts,
            var=hole,
            initial_sketch=initial_sketch,
            language=language,
            max_solutions=max_solutions,
            max_memory=max_memory,
            verbose=verbose)
    print 'Possible Fillings:'
    APP = Expression_2('APP')
    f = Expression.make('f')
    for complexity, term, filling in results:
        print simplify_expr(APP(filling, f))
    return results
Пример #2
0
def filter_normal_sketches(sketches):
    """Filter out sketches whose normal forms have already been seen."""
    normal_forms = set()
    for sketch, steps in sketches:
        normal = simplify_expr(sketch)
        if normal not in normal_forms:
            normal_forms.add(normal)
            yield sketch, steps
Пример #3
0
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
Пример #4
0
def substitute_bodies(terms, var, bodies):
    for term in list(terms):
        if var in term.vars:
            terms.remove(term)
            for body in bodies:
                terms.add(simplify_expr(term.substitute(var, body)))
Пример #5
0
def simplify_filling(db, term):
    term = simplify_expr(term)
    term = _db_simplify_expr(db, term)
    return term