In scenario : Input table with columns "Name", "Value"
- Each column is represented as Enum sorts for z3 (Valid possible values of a column is known)
- A combination (Name, Value) is represented as a DataType
- Valid combination : if it appears in table
The following sample code always returns unsat. I can model same using uninterpreted sort and functions, however would like to know why the following with DataType isnt working.
import z3 as z3
symbol_dict = {}
def create_enum_sort(name, elts):
global symbol_dict
z3_sort, z3_elts = z3.EnumSort(name, elts)
for e in z3_elts:
symbol_dict[str(e)] = e
return z3_sort
# Universe of Enum values for columns to take
Values = ['1', '2', '3', '4', '5']
Names = ['A1', 'A2', 'B1', 'B2', 'C5']
VTYPE = create_enum_sort('VTYPE', Values)
NTYPE = create_enum_sort('NTYPE', Names)
# Valid combinations (Table relation)
dct = {'A2': '2',
'A1': '1',
'B1': '1',
'B2': '2',
'C5': '5'}
# Record type consisting of Name, Value
comb = z3.Datatype('Comb')
comb.declare('cons', \
('value', VTYPE), \
('name', NTYPE) \
)
COMB = comb.create()
# Constraint to valid combinations
constraints = []
e1 = z3.Const('e1', COMB)
for key, value in dct.items():
constraints.append(z3.ForAll([e1],
z3.Implies(COMB.name(e1) == symbol_dict[key], COMB.value(e1) == symbol_dict[value])))
# Basic check
solver = z3.Solver()
solver.add(constraints)
xvar = z3.Const('xvar', COMB)
solver.set(unsat_core=True)
print(solver.assertions())
res = solver.check()
print(res)
if res == z3.sat:
model = solver.model()
print('Model:', model)