given z3 formula in cnf, possible convert list-of-lists representation using z3py? i'd easier access , manipulation of literals. if python functional language, might like
def cnf2list(fm) : match fm | and(p,q) -> cnf2list(p) + cnf2list(q) | p -> clause2list(p) def clause2list(fm) : match fm | or(p,q) -> clause2list(p) + clause2list(q) | p -> [p]
but i'm not sure can in python. possible perform pattern matching above (or use entirely different method) obtain list-of-lists representation of z3 cnf formula?
there no pattern matching, z3py allows inspect z3 expressions:
def clause2list(expr): if z3.is_const(expr): return [str(expr)] elif z3.is_or(expr): return [atom disjunct in expr.children() atom in clause2list(disjunct)] else: assert false, ('not supported', expr) x, y, z = z3.bools('x y z') print(clause2list(z3.or(x, y, z))) # ['x', 'y', 'z']
support negations, conjunctions, , true , false literals left exercise :) see z3.py, ctrl-f "def is_".
note implementation returns lists of variable names instead of z3 variables themselves. that's because of christoph wintersteiger's warning. if intend processing on these lists, symbolic __eq__
not want.
i don't know problem trying solve, if generating cnfs yourself, consider producing them in list-of-lists form start. it's easier convert list of lists z3 expression other way around.