Skip to content

Instantly share code, notes, and snippets.

@mikesol
Last active August 7, 2019 10:23
Show Gist options
  • Save mikesol/ca52a0ebfb9d6893daac684257880c41 to your computer and use it in GitHub Desktop.
Save mikesol/ca52a0ebfb9d6893daac684257880c41 to your computer and use it in GitHub Desktop.
Prove that a list whose sum is greater than its length must have at least one element greater than one
from z3 import *
def test_list_elt_gt_1():
ll = Datatype('ll')
ll.declare('empty')
ll.declare('cons', ('car', IntSort()), ('cdr', ll))
ll = ll.create()
list_sum = Function('list_sum', ll, IntSort(), BoolSort())
list_len = Function('list_len', ll, IntSort(), BoolSort())
a,e = Consts('a e', ll)
b,c,d = Ints('b c d')
fp = Fixedpoint()
fp.declare_var(a,b,c,d,e)
fp.register_relation(list_sum, list_len)
fp.rule(list_sum(a, 0), a == ll.empty)
fp.rule(list_sum(a, d), [a != ll.empty, b < 1, ll.cons(b, e) == a, list_sum(e, c), b + c == d])
fp.rule(list_len(a, 0), a == ll.empty)
fp.rule(list_len(a, d), [a != ll.empty, b < 1, ll.cons(b, e) == a, list_len(e, c), 1 + c == d])
assert fp.query(And(list_sum(a, b), list_len(a, c), a != ll.empty, b >= c)) == unsat
assert fp.query(And(list_sum(a, b), list_len(a, c), a != ll.empty, b < c)) == sat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment