Read func interp of a z3 array from the z3 model

柔情痞子 提交于 2019-12-28 04:20:29

问题


Suppose I have 2 arrays in a formula whose satisfiability I want to check using z3. If z3 returns sat, I want to read in the first array in the z3 model and pretty print it as a key, value pair and a default value. Later I want to convert it to a map and do further analysis on it. Here's the example I run:

void find_model_example_arr() {
  std::cout << "find_model_example_involving_array\n";
  context c;
  sort arr_sort = c.array_sort(c.int_sort(), c.int_sort());
  expr some_array_1 = c.constant("some_array_1", arr_sort);
  expr some_array_2 = c.constant("some_array_2", arr_sort);
  solver s(c);

  s.add(select(some_array_1, 0) > 0);
  s.add(select(some_array_2, 5) < -4);
  std::cout << s.check() << "\n";

  model m = s.get_model();
  std::cout << m << "\n";

  expr some_array_1_eval = m.eval(some_array_1);

  std::cout << "\nsome_array_1_eval = " << some_array_1_eval << "\n";

  func_decl some_array_1_eval_func_decl = some_array_1_eval.decl();
  std::cout << "\nThe Z3 expr(fun_decl) for some_array_1_eval is : " << some_array_1_eval_func_decl << "\n";

  // ERROR here
  func_interp fun_interp = m.get_func_interp(some_array_1_eval_func_decl);
  // This works well 
  //func_interp fun_interp = m.get_func_interp(m.get_func_decl(0)); 

  std::cout << "\nThe Z3 expr(fun_interp) for the array is : " << fun_interp << "\n";

  unsigned num_entries = fun_interp.num_entries();
  for(unsigned i = 0; i < num_entries; i++) 
  {
    z3::func_entry entry = fun_interp.entry(i);
    z3::expr k = entry.arg(0);

    z3::expr v = entry.value();

    std::cout << "\n(key,value): (" << k << "," << v << ")";
  }

  z3::expr default_value = fun_interp.else_value();
  std::cout << "\nDefault value:" << default_value;
}

I get the following output:

find_model_example_involving_array 
sat 
(define-fun some_array_1 () (Array Int Int)   
  (_ as-array k!0)) 
(define-fun some_array_2 () (Array Int Int)  
  (_ as-array k!1))
(define-fun k!0 ((x!1 Int)) Int
  (ite (= x!1 0) 1
    1)) 
(define-fun k!1 ((x!1 Int)) Int
  (ite (= x!1 5) (- 5)
    (- 5)))

some_array_1_eval = (_ as-array k!0)

The Z3 expr(fun_decl) for some_array_1_eval is : 
(declare-fun as-array () (Array  Int Int)) 
unexpected error: invalid argument

Instead if I comment out the first line and use the second, ie use the following code block:

// ERROR here
// func_interp fun_interp = m.get_func_interp(some_array_1_eval_func_decl);
// This works well 
func_interp fun_interp = m.get_func_interp(m.get_func_decl(0)); 

I get the output I am looking for:

(key,value): (0,1)
Default value:1

Here's the problem though? How do I figure out that m.get_func_decl(0) is the one corresponding to some_array_1? For instance, if I use m.get_func_decl(1), I get wrong (key, value) pairs. Is other words how do I get a func_interp of an array (defined as a z3 expr) from a model?


回答1:


The representation for array models is indeed a bit confusing. The meaning of

(define-fun some_array_1 () (Array Int Int)   
  (_ as-array k!0)) 

is that the model for array some_array_1 is the function k!0 which is to be interpreted as an array (signified by the call to as-array. The latter is a parametric function, which has no arguments, therefore, to get at the actual definition of the model function for some_array_1, we have to look up which function as-array calls. In the given example, we can do that as follows, first making sure that we actually have an array model in the expected format by checking a few of assertions:

assert(Z3_get_decl_kind(c, some_array_1_eval_func_decl) == Z3_OP_AS_ARRAY); 
assert(Z3_is_app(c, some_array_1_eval));
assert(Z3_get_decl_num_parameters(c, some_array_1_eval_func_decl) == 1);
assert(Z3_get_decl_parameter_kind(c, some_array_1_eval_func_decl, 0) == 
       Z3_PARAMETER_FUNC_DECL);
func_decl model_fd = func_decl(c, 
                   Z3_get_decl_func_decl_parameter(c, some_array_1_eval_func_decl, 0));

The function declaration model_fd then holds the actual function assigned by the model (k!0) and we can get the function interpretation via

  func_interp fun_interp = m.get_func_interp(model_fd);


来源:https://stackoverflow.com/questions/22885457/read-func-interp-of-a-z3-array-from-the-z3-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!