htree = RecFunction('htree', fgsort, fgsetsort) reach_lr = RecFunction('reach_lr', fgsort, fgsort, boolsort) AddRecDefinition( tree, x, If( x == nil, True, And( notInChildren(x), And( SetIntersect(htree(lft(x)), htree(rght(x))) == fgsetsort.lattice_bottom, And(tree(lft(x)), tree(rght(x))))))) AddRecDefinition( htree, x, If(x == nil, fgsetsort.lattice_bottom, SetAdd(SetUnion(htree(lft(x)), htree(rght(x))), x))) AddRecDefinition( reach_lr, (x, y), If(x == y, True, Or(reach_lr(lft(x), y), reach_lr(rght(x), y)))) AddAxiom((), lft(nil) == nil) AddAxiom((), rght(nil) == nil) # vc goal = Implies(tree(x), Implies(reach_lr(x, y), tree(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:
rght = Function('rght', fgsort, fgsort) minr = Function('minr', fgsort, intsort) maxr = Function('maxr', fgsort, intsort) bst = RecFunction('bst', fgsort, boolsort) hbst = RecFunction('hbst', 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(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]
# projections for cons head = Function('head', fgsort, intsort) tail = Function('tail', fgsort, fgsort) # rec defs slst = RecFunction('slst', fgsort, boolsort) elems = RecFunction('elems', fgsort, fgsetsort) member = RecFunction('member', fgsort, boolsort) AddRecDefinition( slst, l, 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))),
from naturalproofs.pfp import make_pfp_formula from lemsynth.lemsynth_engine import solveProblem # 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)
signature = dict() signature['x'] = x signature['nil'] = nil signature['lft'] = lft signature['rght'] = rght signature['parent'] = parent signature['key'] = key signature['tree'] = tree # Additional recursive definitions htree = RecFunction('htree', fgsort, fgsetsort) AddRecDefinition( htree, x, If(x == nil, fgsetsort.lattice_bottom, SetAdd(SetUnion(htree(lft(x)), htree(rght(x))), x))) minr = RecFunction('minr', fgsort, intsort) AddRecDefinition( minr, x, If(x == nil, 100, min_intsort(key(x), minr(lft(x)), minr(rght(x))))) maxr = RecFunction('maxr', fgsort, intsort) AddRecDefinition( maxr, x, If(x == nil, -100, max_intsort(key(x), maxr(lft(x)), maxr(rght(x))))) ########## Variant dimensions ########## ## Variant dimension number of datafields define_datafields = lambda datname, datnum: [ Function(f'{datname}{str(i)}', fgsort, intsort) for i in range(1, datnum + 1)
from naturalproofs.prover import NPSolver from naturalproofs.pfp import make_pfp_formula from lemsynth.lemsynth_engine import solveProblem # 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
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))) 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()
from lemsynth.lemsynth_engine import solveProblem # 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( 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')
key = Function('key', fgsort, intsort) keys = Function('keys', fgsort, intsetsort) lft = Function('lft', fgsort, fgsort) rght = Function('rght', fgsort, fgsort) maxheap = RecFunction('maxheap', fgsort, boolsort) htree = RecFunction('htree', fgsort, fgsetsort) AddRecDefinition(maxheap, x, If(x == nil, True, 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')
# Declarations x, y = Vars('x y', fgsort) c, nil = Consts('c nil', fgsort) key = Function('key', fgsort, intsort) nxt = Function('nxt', fgsort, fgsort) lst = RecFunction('lst', fgsort, boolsort) lseg = RecFunction('lseg', fgsort, fgsort, boolsort) keys = RecFunction('keys', fgsort, intsetsort) lsegkeys = RecFunction('lsegkeys', fgsort, 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, intsetsort.lattice_bottom, SetAdd(keys(nxt(x)), key(x)))) AddRecDefinition( lsegkeys, (x, y), If(x == y, intsetsort.lattice_bottom, SetAdd(lsegkeys(nxt(x), y), key(x)))) AddAxiom((), nxt(nil) == nil) # Problem parameters pre = And(lst(y), x != c) goal = Implies(lseg(x, y), Implies(pre, keys(x) == SetUnion(lsegkeys(x, y), keys(y)))) # parameters representing the grammar for synth-fun and # terms on which finite model is extracted # TODO: extract this automatically from grammar_string
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( 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),