示例#1
0
int
main (void)
{
  Btor *btor;
  BoolectorNode *array1, *array2, *zero, *one, *val1, *val2;
  BoolectorNode *write1, *write2, *formula;
  char **indices, **values;
  int result, size, i;

  btor = boolector_new ();
  boolector_set_opt (btor, "model_gen", 1);

  zero = boolector_zero (btor, ARRAY2_EXAMPLE_INDEX_BW);
  one = boolector_one (btor, ARRAY2_EXAMPLE_INDEX_BW);
  val1 = boolector_int (btor, 3, ARRAY2_EXAMPLE_VALUE_BW);
  val2 = boolector_int (btor, 5, ARRAY2_EXAMPLE_VALUE_BW);
  array1 =
    boolector_array (btor, ARRAY2_EXAMPLE_VALUE_BW, ARRAY2_EXAMPLE_INDEX_BW,
                     NULL);
  array2 =
    boolector_array (btor, ARRAY2_EXAMPLE_VALUE_BW, ARRAY2_EXAMPLE_INDEX_BW,
                     NULL);
  write1 = boolector_write (btor, array1, zero, val1);
  write2 = boolector_write (btor, array2, one, val2);
  /* Note: we compare two arrays for equality ---> needs extensional theory */
  formula = boolector_eq (btor, write1, write2);
  boolector_assert (btor, formula);
  result = boolector_sat (btor);
  if (result == BOOLECTOR_SAT)
    printf ("Formula is satisfiable\n");
  else
    abort ();

  /* Formula is satisfiable, we can obtain array models: */
  boolector_array_assignment (btor, array1, &indices, &values, &size);
  if (size > 0)
    {
      printf ("Array1:\n");
      for (i = 0; i < size; i++)
        printf ("Array1[%s] = %s\n", indices[i], values[i]);
      boolector_free_array_assignment (btor, indices, values, size);
    }

  boolector_array_assignment (btor, array2, &indices, &values, &size);
  if (size > 0)
    {
      printf ("\nArray2:\n");
      for (i = 0; i < size; i++)
        printf ("Array2[%s] = %s\n", indices[i], values[i]);
      boolector_free_array_assignment (btor, indices, values, size);
    }

  boolector_array_assignment (btor, write1, &indices, &values, &size);
  if (size > 0)
    {
      printf ("\nWrite1:\n");
      for (i = 0; i < size; i++)
        printf ("Write1[%s] = %s\n", indices[i], values[i]);
      boolector_free_array_assignment (btor, indices, values, size);
    }

  boolector_array_assignment (btor, write2, &indices, &values, &size);
  if (size > 0)
    {
      printf ("\nWrite2:\n");
      for (i = 0; i < size; i++)
        printf ("Write2[%s] = %s\n", indices[i], values[i]);
      boolector_free_array_assignment (btor, indices, values, size);
    }

  /* clean up */
  boolector_release (btor, formula);
  boolector_release (btor, write1);
  boolector_release (btor, write2);
  boolector_release (btor, array1);
  boolector_release (btor, array2);
  boolector_release (btor, val1);
  boolector_release (btor, val2);
  boolector_release (btor, zero);
  boolector_release (btor, one);
  assert (boolector_get_refs (btor) == 0);
  boolector_delete (btor);
  return 0;
}
示例#2
0
文件: bv2.c 项目: hellok/kint
int
main (void)
{
  Btor *btor;
  BtorNode *v1, *v2, *add, *zero, *vars_sgt_zero, *impl;
  BtorNode *v1_sgt_zero, *v2_sgt_zero, *add_sgt_zero, *formula;
  char *assignments[10];
  int result, i;

  btor = boolector_new ();
  boolector_enable_model_gen (btor);

  v1 = boolector_var (btor, BV2_EXAMPLE_NUM_BITS, NULL);
  v2 = boolector_var (btor, BV2_EXAMPLE_NUM_BITS, NULL);
  zero = boolector_zero (btor, BV2_EXAMPLE_NUM_BITS);

  v1_sgt_zero = boolector_sgt (btor, v1, zero);
  v2_sgt_zero = boolector_sgt (btor, v2, zero);
  vars_sgt_zero = boolector_and (btor, v1_sgt_zero, v2_sgt_zero);

  add = boolector_add (btor, v1, v2);
  add_sgt_zero = boolector_sgt (btor, add, zero);

  impl = boolector_implies (btor, vars_sgt_zero, add_sgt_zero);

  /* We negate the formula and try to show that the negation is unsatisfiable */
  formula = boolector_not (btor, impl);

  /* We assert the formula and call Boolector */
  boolector_assert (btor, formula);
  result = boolector_sat (btor);
  if (result == BOOLECTOR_SAT)
    printf ("Instance is satisfiable");
  else
    abort ();

  /* The formula is not valid, we have found a counter-example.
   * Now, we are able to obtain assignments to arbitrary expressions */
  i = 0;
  assignments[i++] = boolector_bv_assignment (btor, zero);
  assignments[i++] = boolector_bv_assignment (btor, v1);
  assignments[i++] = boolector_bv_assignment (btor, v2);
  assignments[i++] = boolector_bv_assignment (btor, add);
  assignments[i++] = boolector_bv_assignment (btor, v1_sgt_zero);
  assignments[i++] = boolector_bv_assignment (btor, v2_sgt_zero);
  assignments[i++] = boolector_bv_assignment (btor, vars_sgt_zero);
  assignments[i++] = boolector_bv_assignment (btor, add_sgt_zero);
  assignments[i++] = boolector_bv_assignment (btor, impl);
  assignments[i++] = boolector_bv_assignment (btor, formula);

  i = 0;
  printf ("Assignment to 0: %s\n", assignments[i++]);
  printf ("Assignment to v1: %s\n", assignments[i++]);
  printf ("Assignment to v2: %s\n", assignments[i++]);
  printf ("Assignment to v1 + v2: %s\n", assignments[i++]);
  printf ("Assignment to v1 > 0: %s\n", assignments[i++]);
  printf ("Assignment to v2 > 0: %s\n", assignments[i++]);
  printf ("Assignment to v1 > 0 & v2 > 0: %s\n", assignments[i++]);
  printf ("Assignment to v1 + v2 > 0: %s\n", assignments[i++]);
  printf ("Assignment to v1 > 0 & v2 > 0  => v1 + v2 > 0: %s\n",
          assignments[i++]);
  printf ("Assignment to !(v1 > 0 & v2 > 0  => v1 + v2 > 0): %s\n",
          assignments[i++]);
  for (i = 0; i < 10; i++)
    boolector_free_bv_assignment (btor, assignments[i]);

  /* cleanup */
  boolector_release (btor, zero);
  boolector_release (btor, v1);
  boolector_release (btor, v2);
  boolector_release (btor, add);
  boolector_release (btor, impl);
  boolector_release (btor, formula);
  boolector_release (btor, v1_sgt_zero);
  boolector_release (btor, v2_sgt_zero);
  boolector_release (btor, vars_sgt_zero);
  boolector_release (btor, add_sgt_zero);
  assert (boolector_get_refs (btor) == 0);
  boolector_delete (btor);
  return 0;
}