Skip to content

Instantly share code, notes, and snippets.

@philzook58
Created April 10, 2022 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philzook58/c55a80223287a51220f3d5c244c03f3e to your computer and use it in GitHub Desktop.
Save philzook58/c55a80223287a51220f3d5c244c03f3e to your computer and use it in GitHub Desktop.
Z3 on uninterpreted commutative
from z3 import *
Num = DeclareSort("Num")
add = Function("add", Num, Num, Num)
num = Function("num", IntSort(), Num)
from functools import reduce
import random
print(reduce(add, [num(n) for n in range(10)]))
x,y,z = Consts("x y z", Num)
axioms = [
ForAll([x,y], add(x,y) == add(y,x)),
ForAll([x,y,z], add(add(x,y),z) == add(x,add(y,z)))
]
import timeit
for N in range(5,17):
s = Solver()
s.add(axioms)
e1 = reduce(add, [num(n) for n in range(N)])
perm = list(range(N))
random.shuffle(perm)
e2 = reduce(add, [num(n) for n in perm])
s.add(e1 != e2)
print(N, timeit.timeit(lambda: s.check(), number = 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment