简体   繁体   中英

Z3 using goals and tactics

I am using Z3 with C-API. I am trying to understand how to understand how to use the tactics and goal in C-API. I checked in the examples of Z3, but could not find one that suits me.

I have my C-API psuedo-code as below -

contextId = mk_context();
solverId = Z3_mk_solver(contextId);
Z3_solver_inc_ref(contextId, solverId);
... 
then some assertions like x > 0, y > 0 and so on..
...
// now comes the final goal
mygoal = Z3_mk_less_than(contextId, ...) //  x < 50
Z3_solver_assert (contextId, solverId, mygoal)
...
// finally   check
Z3_solver_check(contextId, solverId)
Z3_reset_memory();
Z3_del_context(contextId);
Z3_solver_dec_ref(contextId, solverId);

Now I want to apply some tactics to the mygoal. However, I am not able to get how exactly the C-API should follow. I checked the documentation, but could not find an example doing this.

What I tried is to use Z3_goal_assert (); API. But it didn't work somehow. Could someone give me a simple example with C-API?

UPDATE : I tried the following C-Code, but it doesn't work. At the function call Z3_tactic_apply() , the solver throws an error like this -

pure virtual method called
terminate called without an active exception

Piece of Code :

goalId = Z3_mk_goal();      
Z3_goal_inc_ref(context, goalId);

assertionVector = Z3_solver_get_assertions (context, solver);
int vectorSize = Z3_ast_vector_size(assertionVector);

for(int i=0;i<vectorSize;i++)       
    Z3_goal_assert(context, goalId, Z3_ast_vector_get(context, assertionVector, i));

Z3_goal_assert(context, goalId, Z3_mk_eq(context, totalProcDecl, Z3_mk_int(context, numProcessors, int_sort)));
Z3_goal_assert(context, goalId, Z3_mk_eq(contextlatencyDecl, Z3_mk_int(context, latencyConstraint, int_sort)));

// This is what I am trying to apply
// (check-sat-using (then (! simplify :arith-lhs true) solve-eqs lia2pb pb2bv bit-blast sat))

tactic0 = Z3_mk_tactic (context, "simplify");
Z3_tactic_inc_ref (context,tactic0);

tactic1 = Z3_mk_tactic (context, "solve-eqs");
Z3_tactic_inc_ref (context, tactic1);

tactic2 = Z3_mk_tactic (context, "lia2pb");
Z3_tactic_inc_ref (context, tactic2);

tactic3 = Z3_mk_tactic (context, "pb2bv");
Z3_tactic_inc_ref (context, tactic3);

tactic4 = Z3_mk_tactic (context, "bit-blast");
Z3_tactic_inc_ref (context, tactic4);

tactic5 = Z3_mk_tactic (context, "sat");
Z3_tactic_inc_ref (context, tactic5);

temp = Z3_tactic_and_then (context, tactic0, tactic1);
temp = Z3_tactic_and_then (context, temp, tactic2);
temp = Z3_tactic_and_then (context, temp, tactic3);
temp = Z3_tactic_and_then (context, temp, tactic4);
temp = Z3_tactic_and_then (context, temp, tactic5);

result = Z3_tactic_apply (context, temp, goalId);
printf("Result : %s\n", Z3_apply_result_to_string (context, result));

// Finished Solving.
Z3_goal_dec_ref (context, goalId);
Z3_tactic_dec_ref (context, tactic0);
Z3_tactic_dec_ref (context, tactic1);
Z3_tactic_dec_ref (context, tactic2);
Z3_tactic_dec_ref (context, tactic3);
Z3_tactic_dec_ref (context, tactic4);
Z3_tactic_dec_ref (context, tactic5);

I also tried one more option to add parameter to simplify tactic.

tactic0_without_param = Z3_mk_tactic (context, "simplify");
Z3_tactic_inc_ref (context,tactic0_without_param);

paramsId = Z3_mk_params(context);
Z3_params_inc_ref(context, paramsId);
Z3_params_set_bool (context, p, paramsId, Z3_mk_string_symbol(context, ":arith-lhs"), true);
tactic0 = Z3_tactic_using_params (context, tactic0, paramsId);

But again doesn't work.

Thanks.

Z3 4.x comes with C++ header files that makes the Z3 C API much easier to use (file include/z3++.h ). It also has an example based on C++ (file examples/c++/example.cpp ). This file contains many examples using tactic objects.

That being said, tactics should be applied to Goals. For convenience, we also provide an API that creates a solver from a tactic Z3_mk_solver_from_tactic . The solver object returned by this API will tries to solve a satisfiability query using the given tactic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM