Exemplo n.º 1
0
AddRecDefinition(maxr, x, If(x == nil, -1, max_intsort(key(x), maxr(lft(x)), maxr(rght(x)))))
AddRecDefinition(bst, x, If(x == nil, True,
                            And(0 < key(x),
                                And(key(x) < 100,
                                    And(bst(lft(x)),
                                        And(bst(rght(x)),
                                            And(maxr(lft(x)) <= key(x),
                                                key(x) <= minr(rght(x)))))))))
AddRecDefinition(hbst, x, If(x == nil, fgsetsort.lattice_bottom,
                             SetAdd(SetUnion(hbst(lft(x)), hbst(rght(x))), x)))
AddAxiom((), lft(nil) == nil)
AddAxiom((), rght(nil) == nil)

# Problem parameters
goal = Implies(bst(x), Implies(And(x != nil,
                                   And(IsMember(y, hbst(x)),
                                       And(k == minr(x),
                                           And(k == minr(y), y != nil)))),
                               k == minr(lft(y))))

# parameters representing the grammar for synth-fun and
# terms on which finite model is extracted
# TODO: extract this automatically from grammar_string
v = Var('v', fgsort)
lemma_grammar_args = [v, y, k, nil]
lemma_grammar_terms = {v, lft(v), rght(v), nil, y}

name = 'bst-minimal'
grammar_string = importlib_resources.read_text('experiments', 'grammar_{}.sy'.format(name))

solveProblem(lemma_grammar_args, lemma_grammar_terms, goal, name, grammar_string)
Exemplo n.º 2
0
                                And(key(x) < 100,
                                    And(bst(lft(x)),
                                        And(bst(rght(x)),
                                            And(maxr(lft(x)) <= key(x),
                                                And(key(x) <= minr(rght(x)),
                                                    And(notInChildren(x),
                                                        SetIntersect(hbst(lft(x)), hbst(rght(x)))
                                                        == fgsetsort.lattice_bottom)))))))))
AddRecDefinition(hbst, x, If(x == nil, fgsetsort.lattice_bottom,
                             SetAdd(SetUnion(hbst(lft(x)), hbst(rght(x))), x)))
AddAxiom((), lft(nil) == nil)
AddAxiom((), rght(nil) == nil)

# vc
goal = Implies(bst(x), Implies(And(x != nil, And(rght(y) != nil,
                                   And(IsMember(y, hbst(x)),
                                       And(k == maxr(x),
                                           And(k == maxr(y), y != nil))))),
                               k == maxr(rght(y))))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemmas
lemma_params = (x,y)
lemma_body = Implies(bst(x), Implies(IsMember(y, hbst(x)), bst(y)))
Exemplo n.º 3
0
def notInChildren(x):
    return And(Not(IsMember(x, htree(lft(x)))),
               Not(IsMember(x, htree(rght(x)))))
Exemplo n.º 4
0
# declarations
x, y = Vars('x y', fgsort)
nil = Const('nil', fgsort)
k = Const('k', intsort)
nxt = Function('nxt', fgsort, fgsort)
lst = RecFunction('lst', fgsort, boolsort)
lseg = RecFunction('lseg', fgsort, fgsort, boolsort)
key = Function('key', fgsort, intsort)
keys = RecFunction('keys', fgsort, intsetsort)
AddRecDefinition(lst, x, If(x == nil, True, lst(nxt(x))))
AddRecDefinition(lseg, (x, y) , If(x == y, True, lseg(nxt(x), y)))
AddRecDefinition(keys, x, If(x == nil, fgsetsort.lattice_bottom, SetAdd(keys(nxt(x)), key(x))))
AddAxiom((), nxt(nil) == nil)

# vc
goal = Implies(lseg(x, y), Implies(And(And(And(x != nil, y != nil), lst(x)), key(y) == k), IsMember(k, keys(x))))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemma
lemma_params = (x,y)
lemma_body = Implies(lseg(x, y), Implies(And(And(y != nil, lst(x)), key(y) == k), IsMember(k, keys(x))))
lemmas = {(lemma_params, lemma_body)}

# check validity of lemmas
Exemplo n.º 5
0
                                And(key(x) < 100,
                                    And(bst(lft(x)),
                                        And(bst(rght(x)),
                                            And(maxr(lft(x)) <= key(x),
                                                And(key(x) <= minr(rght(x)),
                                                    And(notInChildren(x),
                                                        SetIntersect(hbst(lft(x)), hbst(rght(x)))
                                                        == fgsetsort.lattice_bottom)))))))))
AddRecDefinition(hbst, x, If(x == nil, fgsetsort.lattice_bottom,
                             SetAdd(SetUnion(hbst(lft(x)), hbst(rght(x))), x)))
AddAxiom((), lft(nil) == nil)
AddAxiom((), rght(nil) == nil)

# vc
goal = Implies(bst(x), Implies(And(x != nil,
                                   And(IsMember(y, hbst(lft(x))),
                                       IsMember(z, hbst(rght(x))))),
                               key(y) <= key(z)))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemmas
lemma1_params = (x,y)
lemma1_body = Implies(bst(x), Implies(IsMember(y, hbst(x)), key(y) <= maxr(x)))
lemma2_params = (x,y)
Exemplo n.º 6
0
# declarations
x, y = Vars('x y', fgsort)
nil = Const('nil', fgsort)
k = Const('k', intsort)
nxt = Function('nxt', fgsort, fgsort)
key = Function('key', fgsort, intsort)
lst = RecFunction('lst', fgsort, boolsort)
hlst = RecFunction('hlst', fgsort, fgsetsort)
lseg = RecFunction('lseg', fgsort, fgsort, boolsort)
AddRecDefinition(lst, x, If(x == nil, True, lst(nxt(x))))
AddRecDefinition(hlst, x, If(x == nil, fgsetsort.lattice_bottom, SetAdd(hlst(nxt(x)), x)))
AddRecDefinition(lseg, (x, y), If(x == y, True, lseg(nxt(x), y)))
AddAxiom((), nxt(nil) == nil)

# vc
goal = Implies(lst(x), Implies(key(x) != k, Implies(IsMember(y, hlst(x)), lseg(x,y))))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemma
lemma_params = (x,y)
lemma_body = Implies(lst(x), Implies(IsMember(y, hlst(x)), lseg(x,y)))
lemmas = {(lemma_params, lemma_body)}

# check validity of lemmas
Exemplo n.º 7
0
    If(l == nil, True,
       If(tail(l) == nil, True, And(head(l) <= head(tail(l)), slst(tail(l))))))
AddRecDefinition(
    elems, l,
    If(l == nil, fgsetsort.lattice_bottom, SetAdd(elems(tail(l)), head(l))))
AddRecDefinition(
    member, l,
    If(l == nil, False,
       If(k == head(l), True, If(k < head(l), False, member(tail(l))))))

# axioms
AddAxiom(l, head(cons(h, l)) == h)
AddAxiom(l, tail(cons(h, l)) == l)
AddAxiom(l, cons(h, l) != nil)

goal = Implies(And(slst(l), IsMember(k, elems(l))), member(l))

# adt pfp of goal
base_case = Implies(And(slst(nil), IsMember(k, elems(nil))), member(nil))
induction_hypothesis = Implies(And(slst(tl), IsMember(k, elems(tl))),
                               member(tl))
induction_step = Implies(
    And(slst(cons(h, tl)), IsMember(k, elems(cons(h, tl)))),
    member(cons(h, tl)))

pfp_goal = And(base_case, Implies(induction_hypothesis, induction_step))

np_solver = NPSolver()
solution = np_solver.solve(pfp_goal)
print('No lemma: ' + str(solution.if_sat))
Exemplo n.º 8
0
lst = RecFunction('lst', fgsort, boolsort)
lseg = RecFunction('lseg', fgsort, fgsort, boolsort)
key = Function('key', fgsort, intsort)
keys = RecFunction('keys', fgsort, intsetsort)
AddRecDefinition(lst, x, If(x == nil, True, lst(nxt(x))))
AddRecDefinition(lseg, (x, y), If(x == y, True, lseg(nxt(x), y)))
AddRecDefinition(
    keys, x,
    If(x == nil, fgsetsort.lattice_bottom, SetAdd(keys(nxt(x)), key(x))))
AddAxiom((), nxt(nil) == nil)

# vc
goal = Implies(
    lst(x),
    Implies(And(And(y != nil, lseg(x, y)),
                key(y) == k), IsMember(k, keys(x))))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemma
lemma_params = (x, y)
lemma_body = Implies(
    lseg(x, y),
    Implies(And(And(y != nil, lst(x)),
                key(y) == k), IsMember(k, keys(x))))
Exemplo n.º 9
0
                                    == fgsetsort.lattice_bottom)))))))))
AddRecDefinition(
    hbst, x,
    If(x == nil, fgsetsort.lattice_bottom,
       SetAdd(SetUnion(hbst(lft(x)), hbst(rght(x))), x)))
AddRecDefinition(
    keys, x,
    If(x == nil, fgsetsort.lattice_bottom,
       SetAdd(SetUnion(keys(lft(x)), keys(rght(x))), key(x))))
AddAxiom((), lft(nil) == nil)
AddAxiom((), rght(nil) == nil)

# vc
goal = Implies(
    bst(x),
    Implies(And(IsMember(k, keys(x)), k < key(x), x != nil),
            IsMember(k, keys(lft(x)))))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemmas
lemma_params = (x, )
lemma1_body = Implies(
    bst(x), Implies(And(IsMember(k, keys(x)), x != nil),
                    minr(x) <= k))
Exemplo n.º 10
0
from lemsynth.lemsynth_engine import solveProblem

x = Var('x', fgsort)
nil = Const('nil', fgsort)
k = Const('k', intsort)
nxt = Function('nxt', fgsort, fgsort)
key = Function('key', fgsort, intsort)
slst = RecFunction('slst', fgsort, boolsort)
keys = RecFunction('keys', fgsort, intsetsort)
AddRecDefinition(
    slst, x,
    If(x == nil, True,
       If(nxt(x) == nil, True, And(key(x) <= key(nxt(x)), slst(nxt(x))))))
AddRecDefinition(
    keys, x,
    If(x == nil, intsetsort.lattice_bottom, SetAdd(keys(nxt(x)), key(x))))
AddAxiom((), nxt(nil) == nil)

goal = Implies(slst(x), Implies(k < key(x), Not(IsMember(k, keys(x)))))

name = 'lem-member'

# leave all things related to grammar as empty, just using natural proof engine
lemma_grammar_args = []
lemma_grammar_terms = {}

grammar_string = ''

solveProblem(lemma_grammar_args, lemma_grammar_terms, goal, name,
             grammar_string)
Exemplo n.º 11
0
                                And(notInChildren(x),
                                    And(SetIntersect(htree(lft(x)), htree(rght(x)))
                                        == fgsetsort.lattice_bottom,
                                        And(maxheap(lft(x)),
                                            And(maxheap(rght(x)),
                                                And(key(lft(x)) <= key(x),
                                                    key(rght(x)) <= key(x))))))))
AddRecDefinition(htree, x, If(x == nil, fgsetsort.lattice_bottom,
                              SetAdd(SetUnion(htree(lft(x)), htree(rght(x))), x)))
AddRecDefinition(keys, x, If(x == nil, fgsetsort.lattice_bottom,
                             SetAdd(SetUnion(keys(lft(x)), keys(rght(x))), key(x))))
AddAxiom((), lft(nil) == nil)
AddAxiom((), rght(nil) == nil)

# vc
goal = Implies(maxheap(x), Implies(lft(x) != nil, Implies(IsMember(k, keys(x)), k <= key(x))))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemmas
lemma_params = (x,)
lemma_body = Implies(maxheap(x), Implies(IsMember(k, keys(x)), k <= key(x)))
lemmas = {(lemma_params, lemma_body)}

# check validity of lemmas
Exemplo n.º 12
0
    Implies(
        And(x != nil,
            And(y != nil, And(reach_lr(lft(x), y), reach_lr(rght(x), z)))),
        y != z))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemmas
lemma_params = (x, y)
lemma_body = Implies(reach_lr(x, y), Implies(y != nil, IsMember(y, htree(x))))
lemmas = {(lemma_params, lemma_body)}

# check validity of lemmas
solution = np_solver.solve(make_pfp_formula(lemma_body))
if not solution.if_sat:
    print('lemma is valid')
else:
    print('lemma is invalid')

# check validity with natural proof solver and hardcoded lemmas
solution = np_solver.solve(goal, lemmas)
if not solution.if_sat:
    print('goal (with lemmas) is valid')
else:
    print('goal (with lemmas) is invalid')
Exemplo n.º 13
0
import naturalproofs.proveroptions as proveroptions

from lemsynth.lemsynth_engine import solveProblem

# Declarations
x, y = Vars('x y', fgsort)
nil = Const('nil', fgsort)
nxt = Function('nxt', fgsort, fgsort)
lst = RecFunction('lst', fgsort, boolsort)
hlst = RecFunction('hlst', fgsort, fgsetsort)
lseg = RecFunction('lseg', fgsort, fgsort, boolsort)
AddRecDefinition(lst, x, If(x == nil, True, lst(nxt(x))))
AddRecDefinition(hlst, x, If(x == nil, fgsetsort.lattice_bottom, SetAdd(hlst(nxt(x)), x)))
AddRecDefinition(lseg, (x, y), If(x == y, True, lseg(nxt(x), y)))
AddAxiom((), nxt(nil) == nil)

# Problem parameters
goal = Implies(lst(x), Implies(IsMember(y, hlst(x)), lseg(x, y)))

# parameters representing the grammar for synth-fun and
# terms on which finite model is extracted
# TODO: extract this automatically from grammar_string
v1, v2 = Vars('v1 v2', fgsort)
lemma_grammar_args = [v1, v2, nil]
lemma_grammar_terms = {v1, nil, nxt(nil), v2, nxt(v2), nxt(v1), nxt(nxt(v1))}

name = 'list-hlist-lseg'
grammar_string = importlib_resources.read_text('experiments', 'grammar_{}.sy'.format(name))

solveProblem(lemma_grammar_args, lemma_grammar_terms, goal, name, grammar_string)
Exemplo n.º 14
0
bst = RecFunction('bst', fgsort, boolsort)
htree = RecFunction('htree', fgsort, fgsetsort)
AddRecDefinition(
    minr, x, If(x == nil, 100, min_intsort(key(x), minr(lft(x)),
                                           minr(rght(x)))))
AddRecDefinition(
    maxr, x, If(x == nil, -1, max_intsort(key(x), maxr(lft(x)),
                                          maxr(rght(x)))))
AddRecDefinition(tree, x, If(x == nil, True, And(tree(lft(x)), tree(rght(x)))))
AddRecDefinition(
    tree, x,
    If(
        x == nil, True,
        And(
            And(
                And(Not(IsMember(x, htree(lft(x)))),
                    Not(IsMember(x, htree(rght(x)))))),
            SetIntersect(htree(lft(x)),
                         htree(rght(x))) == fgsetsort.lattice_bottom,
            And(tree(lft(x)), tree(rght(x))))))
AddRecDefinition(
    bst, x,
    If(
        x == nil, True,
        And(
            0 < key(x),
            And(
                key(x) < 100,
                And(
                    bst(lft(x)),
                    And(
Exemplo n.º 15
0
                And(
                    maxheap(lft(x)),
                    And(maxheap(rght(x)),
                        And(key(lft(x)) <= key(x),
                            key(rght(x)) <= key(x))))))))
AddRecDefinition(
    htree, x,
    If(x == nil, fgsetsort.lattice_bottom,
       SetAdd(SetUnion(htree(lft(x)), htree(rght(x))), x)))
AddAxiom((), lft(nil) == nil)
AddAxiom((), rght(nil) == nil)

# vc
goal = Implies(
    maxheap(x),
    Implies(key(x) != k, Implies(IsMember(y, htree(x)),
                                 key(y) <= key(x))))

# check validity with natural proof solver and no hardcoded lemmas
np_solver = NPSolver()
solution = np_solver.solve(make_pfp_formula(goal))
if not solution.if_sat:
    print('goal (no lemmas) is valid')
else:
    print('goal (no lemmas) is invalid')

# hardcoded lemmas
lemma_params = (x, y)
lemma_body = Implies(maxheap(x),
                     Implies(IsMember(y, htree(x)),
                             key(y) <= key(x)))
Exemplo n.º 16
0
                And(
                    bst(lft(x)),
                    And(bst(rght(x)),
                        And(maxr(lft(x)) <= key(x),
                            key(x) <= minr(rght(x)))))))))
AddRecDefinition(
    keys, x,
    If(x == nil, fgsetsort.lattice_bottom,
       SetAdd(SetUnion(keys(lft(x)), keys(rght(x))), key(x))))
AddAxiom((), lft(nil) == nil)
AddAxiom((), rght(nil) == nil)

# Problem parameters
goal = Implies(
    bst(x),
    Implies(And(IsMember(k, keys(x)), k < key(x)), IsMember(k, keys(lft(x)))))

# parameters representing the grammar for synth-fun and
# terms on which finite model is extracted
# TODO: extract this automatically from grammar_string
v = Var('v', fgsort)
lemma_grammar_args = [v, k, nil]
lemma_grammar_terms = {
    lft(v),
    rght(v), nil,
    rght(rght(nil)),
    rght(nil), v,
    rght(rght(v)),
    rght(lft(v)),
    lft(lft(v)),
    rght(rght(v)),