Exemplo n.º 1
0
static int Solver_addDIMACSSeqence(struct lua_State *state) {

  LGL* lgl = get_lgl(state);

  int n = lua_gettop (state);
  int success;
  for (int i = 2; i <= n; i++) {
    int type = lua_type (state, i);
    if (type == LUA_TTABLE) {
      // Iterate over the entries of the table
      lua_len(state, i);
      lua_Integer len = lua_tointeger(state, -1);
      lua_pop(state, 1);
      for (int j = 1; j <= len; j++) {
	lua_geti(state, i, j);
	lua_Integer c = lua_tointegerx (state, -1, &success);
	if (!success) {
	  lua_pushliteral(state, "Illegal argument in DIMACS sequence");
	  lua_error(state);    
	}
	lgladd (lgl, c);
	lua_pop(state, 1);	
      }      
    } else {
      lua_Integer c = lua_tointegerx (state, i, &success);
      if (!success) {
	lua_pushliteral(state, "Illegal argument in DIMACS sequence");
	lua_error(state);    
      }
      lgladd (lgl, c);
    }
  }

  return 0;
}
Exemplo n.º 2
0
void satcheck_lingelingt::lcnf(const bvt &bv)
{
  bvt new_bv;
  
  if(process_clause(bv, new_bv))
    return;

  forall_literals(it, new_bv)
    lgladd(solver, it->dimacs());

  lgladd(solver, 0);

  clause_counter++;
}
Exemplo n.º 3
0
int main (int argc, char ** argv) {
  int i, len, ch, close = 0, res, arg;
  char buffer[80], * tok, * opt;
  FILE * file;
  char * cmd;
  LGL * lgl;
  for (i = 1; i < argc; i++) {
    if (!strcmp (argv[i], "-h")) {
      printf ("usage: lgluntrace [-h][-v][-e][<trace>[.gz]]\n");
      exit (0);
    } else if (!strcmp (argv[i], "-v")) verbose = 1;
    else if (!strcmp (argv[i], "-e")) exitonabort = 1;
    else if (argv[i][0] == '-')
      die ("invalid command line option '%s' (try '-h')", argv[i]);
    else if (name)
      die ("two traces '%s' and '%s' specified (try '-h')", name, argv[i]);
    else name = argv[i];
  }
  if(name) {
    len = strlen (name);
    if (len >= 3 && !strcmp (name + len - 3, ".gz")) {
      cmd = malloc (len + 20);
      sprintf (cmd, "gunzip -c %s", name);
      file = popen (cmd, "r");
      free (cmd);
      if (file) close = 2;
    } else {
      file = fopen (name, "r");
      if (file) close = 1;
    }
    if (!file) die ("can not read '%s'", name);
  } else name = "<stdin>", file = stdin;
  if (exitonabort) {
    msg ("setting signal handlers since '-e' specified");
    signal (SIGINT, exitonsig);
    signal (SIGSEGV, exitonsig);
    signal (SIGABRT, exitonsig);
    signal (SIGTERM, exitonsig);
  }
  msg ("reading %s", name);
  buffer[len = 0] = 0;
  lineno = 1;
  res = 0;
  lgl = 0;
NEXT:
  ch = getc (file);
  if (ch == EOF) goto DONE;
  if (ch == '\r') goto NEXT;
  if (ch != '\n') {
    if (len + 1 >= sizeof (buffer)) perr ("line buffer exceeded");
    buffer[len++] = ch;
    buffer[len] = 0;
    goto NEXT;
  }
  msg ("line %d : %s", lineno, buffer);
  if (!(tok = strtok (buffer, " "))) perr ("empty line");
  else if (!strcmp (tok, "add")) lgladd (lgl, intarg ("add"));
  else if (!strcmp (tok, "return")) {
    arg = intarg ("return");
    if (arg != res) 
      die ("expected return value %d but got %d", arg, res);
  } else if (!strcmp (tok, "deref")) res = lglderef (lgl, intarg ("deref"));
  else if (!strcmp (tok, "failed")) res = lglfailed (lgl, intarg ("failed"));
  else if (!strcmp (tok, "fixed")) res = lglfixed (lgl, intarg ("fixed"));
  else if (!strcmp (tok, "repr")) res = lglrepr (lgl, intarg ("repr"));
  else if (noarg (tok, "incvar")) res = lglincvar (lgl);
  else if (noarg (tok, "maxvar")) res = lglmaxvar (lgl);
  else if (noarg (tok, "changed")) res = lglchanged (lgl);
  else if (noarg (tok, "inconsistent")) res = lglinconsistent (lgl);
  else if (noarg (tok, "lkhd")) res = lglookahead (lgl);
  else if (noarg (tok, "fixate")) lglfixate (lgl);
  else if (noarg (tok, "reduce")) lglreducecache (lgl);
  else if (noarg (tok, "flush")) lglflushcache (lgl);
  else if (noarg (tok, "chkclone")) lglchkclone (lgl);
  else if (!strcmp (tok, "assume")) lglassume (lgl, intarg ("assume"));
  else if (noarg (tok, "init")) lgl = lglinit ();
  else if (noarg (tok, "sat")) res = lglsat (lgl);
  else if (!strcmp (tok, "simp")) res = lglsimp (lgl, intarg ("simp"));
  else if (noarg (tok, "stats")) lglstats (lgl);
  else if (!strcmp (tok, "freeze")) lglfreeze (lgl, intarg ("freeze"));
  else if (!strcmp (tok, "melt")) lglmelt (lgl, intarg ("melt"));
  else if (!strcmp (tok, "reuse")) lglreuse (lgl, intarg ("reuse"));
  else if (!strcmp (tok, "frozen")) res = lglfrozen (lgl, intarg ("frozen"));
  else if (!strcmp (tok, "usable")) res = lglusable (lgl, intarg ("usable"));
  else if (!strcmp (tok, "reusable"))
    res = lglreusable (lgl, intarg ("reusable"));
  else if (noarg (tok, "setphases")) lglsetphases (lgl);
  else if (!strcmp (tok, "setphase")) lglsetphase (lgl, intarg ("setphase"));
  else if (!strcmp (tok, "resetphase"))
    lglresetphase (lgl, intarg ("resetphase"));
  else if (!strcmp (tok, "option")) {
    if (!(opt = strtok (0, " "))) perr ("option name missing");
    lglsetopt (lgl, opt, intarg ("option"));
  } else if (noarg (tok, "release")) lglrelease (lgl);
  else perr ("invalid command '%s'", tok);
  lineno++;
  len = 0;
  goto NEXT;
DONE:
  if(close == 1) fclose (file);
  if(close == 2) pclose (file);
  msg ("done %s", name);
  return 0;
}
Exemplo n.º 4
0
static void
btor_lingeling_add (BtorSATMgr * smgr, int lit)
{
  BtorLGL * blgl = smgr->solver;
  lgladd (blgl->lgl, lit);
}
Exemplo n.º 5
0
static void
process_skeleton_tseitin (Btor * btor, LGL * lgl,
                          BtorNodePtrStack * work_stack,
                          BtorNodePtrStack * unmark_stack,
                          BtorPtrHashTable * ids,
                          BtorNode * root)
{
  assert (btor);

  int i, lhs, rhs[3], fixed;
  BtorNode * exp;

  BTOR_PUSH_STACK (btor->mm, *work_stack, root);

  do {
    exp = BTOR_POP_STACK (*work_stack);
    assert (exp);
    exp = BTOR_REAL_ADDR_NODE (exp);
    if (!exp->mark)
      {
        exp->mark = 1;
        BTOR_PUSH_STACK (btor->mm, *unmark_stack, exp);

        BTOR_PUSH_STACK (btor->mm, *work_stack, exp);
        for (i = exp->arity-1; i >= 0; i--)
          BTOR_PUSH_STACK (btor->mm, *work_stack, exp->e[i]);
      }
    else if (exp->mark == 1)
      {
        exp->mark = 2;
        if (BTOR_IS_FUN_NODE (exp)
            || BTOR_IS_ARGS_NODE (exp)
            || exp->parameterized
            || btor_get_exp_width (btor, exp) != 1)
          continue;

#ifndef NDEBUG
        for (i = 0; i < exp->arity; i++)
          {
            BtorNode * child = exp->e[i];
            child = BTOR_REAL_ADDR_NODE (child);
            assert (child->mark == 2);
            if (!BTOR_IS_FUN_NODE (child)
                && !BTOR_IS_ARGS_NODE (child)
                && !child->parameterized
                && btor_get_exp_width (btor, child) == 1)
              assert (btor_find_in_ptr_hash_table (ids, child));
          }
#endif
        lhs = process_skeleton_tseitin_lit (ids, exp);
        fixed = fixed_exp (btor, exp);
        if (fixed)
          {
            lgladd (lgl, (fixed > 0) ? lhs : -lhs);
            lgladd (lgl, 0);
          }

        switch (exp->kind)
          {
            case BTOR_AND_NODE:
              rhs[0] = process_skeleton_tseitin_lit (ids, exp->e[0]);
              rhs[1] = process_skeleton_tseitin_lit (ids, exp->e[1]);

              lgladd (lgl, -lhs);
              lgladd (lgl, rhs[0]);
              lgladd (lgl, 0);

              lgladd (lgl, -lhs);
              lgladd (lgl, rhs[1]);
              lgladd (lgl, 0);

              lgladd (lgl, lhs);
              lgladd (lgl, -rhs[0]);
              lgladd (lgl, -rhs[1]);
              lgladd (lgl, 0);
              break;

            case BTOR_BEQ_NODE:
              if (btor_get_exp_width (btor, exp->e[0]) != 1)
                break;
              assert (btor_get_exp_width (btor, exp->e[1]) == 1);
              rhs[0] = process_skeleton_tseitin_lit (ids, exp->e[0]);
              rhs[1] = process_skeleton_tseitin_lit (ids, exp->e[1]);

              lgladd (lgl, -lhs);
              lgladd (lgl, -rhs[0]);
              lgladd (lgl, rhs[1]);
              lgladd (lgl, 0);

              lgladd (lgl, -lhs);
              lgladd (lgl, rhs[0]);
              lgladd (lgl, -rhs[1]);
              lgladd (lgl, 0);

              lgladd (lgl, lhs);
              lgladd (lgl, rhs[0]);
              lgladd (lgl, rhs[1]);
              lgladd (lgl, 0);

              lgladd (lgl, lhs);
              lgladd (lgl, -rhs[0]);
              lgladd (lgl, -rhs[1]);
              lgladd (lgl, 0);

              break;

            case BTOR_BCOND_NODE:
              assert (btor_get_exp_width (btor, exp->e[0]) == 1);
              if (btor_get_exp_width (btor, exp->e[1]) != 1)
                break;
              assert (btor_get_exp_width (btor, exp->e[2]) == 1);
              rhs[0] = process_skeleton_tseitin_lit (ids, exp->e[0]);
              rhs[1] = process_skeleton_tseitin_lit (ids, exp->e[1]);
              rhs[2] = process_skeleton_tseitin_lit (ids, exp->e[2]);

              lgladd (lgl, -lhs);
              lgladd (lgl, -rhs[0]);
              lgladd (lgl, rhs[1]);
              lgladd (lgl, 0);

              lgladd (lgl, -lhs);
              lgladd (lgl, rhs[0]);
              lgladd (lgl, rhs[2]);
              lgladd (lgl, 0);

              lgladd (lgl, lhs);
              lgladd (lgl, -rhs[0]);
              lgladd (lgl, -rhs[1]);
              lgladd (lgl, 0);

              lgladd (lgl, lhs);
              lgladd (lgl, rhs[0]);
              lgladd (lgl, -rhs[2]);
              lgladd (lgl, 0);
              break;

            default:
              assert (exp->kind != BTOR_PROXY_NODE);
              break;
          }
      }
  } while (!BTOR_EMPTY_STACK (*work_stack));
}
Exemplo n.º 6
0
void
btor_process_skeleton (Btor * btor)
{
  BtorPtrHashTable * ids;
  BtorNodePtrStack unmark_stack;
  int count, fixed;
  BtorNodePtrStack work_stack;
  BtorMemMgr * mm = btor->mm;
  BtorHashTableIterator it;
  double start, delta;
  int res, lit, val;
  BtorNode * exp;
  LGL * lgl;

  start = btor_time_stamp ();

  ids = btor_new_ptr_hash_table (mm,
          (BtorHashPtr) btor_hash_exp_by_id,
          (BtorCmpPtr) btor_compare_exp_by_id);

  lgl = lglinit ();
  lglsetprefix (lgl, "[lglskel] ");

  if (btor->options.verbosity.val >= 2)
    {
      lglsetopt (lgl, "verbose", btor->options.verbosity.val - 1);
      lglbnr ("Lingeling", "[lglskel] ", stdout);
      fflush (stdout);
    }
  else
    lglsetopt (lgl, "verbose", -1);        

  count = 0;

  BTOR_INIT_STACK (work_stack);
  BTOR_INIT_STACK (unmark_stack);

  btor_init_node_hash_table_iterator (&it, btor->synthesized_constraints);
  btor_queue_node_hash_table_iterator (&it, btor->unsynthesized_constraints);
  while (btor_has_next_node_hash_table_iterator (&it))
    {
      count++;
      exp = btor_next_node_hash_table_iterator (&it);
      assert (btor_get_exp_width (btor, exp) == 1);
      process_skeleton_tseitin (btor, lgl,
        &work_stack, &unmark_stack, ids, exp);
      lgladd (lgl, process_skeleton_tseitin_lit (ids, exp));
      lgladd (lgl, 0);
    }

  BTOR_RELEASE_STACK (mm, work_stack);

  while (!BTOR_EMPTY_STACK (unmark_stack))
    {
      exp = BTOR_POP_STACK (unmark_stack);
      assert (!BTOR_IS_INVERTED_NODE (exp));
      assert (exp->mark);
      exp->mark = 0;
    }

  BTOR_RELEASE_STACK (mm, unmark_stack);

  BTOR_MSG (btor->msg, 1, "found %u skeleton literals in %d constraints",
                ids->count, count);

  res = lglsimp (lgl, 0);

  if (btor->options.verbosity.val)
    {
      BTOR_MSG (btor->msg, 1, "skeleton preprocessing result %d", res);
      lglstats (lgl);
    }

  fixed = 0;

  if (res == 20)
    {
      BTOR_MSG (btor->msg, 1, "skeleton inconsistent");
      btor->inconsistent = 1;
    }
  else
    {
      assert (res == 0 || res == 10);
      btor_init_node_hash_table_iterator (&it, ids);
      while (btor_has_next_node_hash_table_iterator (&it))
        {
          exp = btor_next_node_hash_table_iterator (&it);
          assert (!BTOR_IS_INVERTED_NODE (exp));
          lit = process_skeleton_tseitin_lit (ids, exp);
          val = lglfixed (lgl, lit);
          if (val)
            {
              if (!btor_find_in_ptr_hash_table (
                    btor->synthesized_constraints, exp) &&
                  !btor_find_in_ptr_hash_table (
                    btor->unsynthesized_constraints, exp))
                {
                  if (val < 0)
                    exp = BTOR_INVERT_NODE (exp);
                  btor_assert_exp (btor, exp);
                  btor->stats.skeleton_constraints++;
                  fixed++;
                }
            }
          else
            {
              assert (!btor_find_in_ptr_hash_table (
                         btor->synthesized_constraints, exp));
              assert (!btor_find_in_ptr_hash_table (
                         btor->unsynthesized_constraints, exp));
            }
        }
    }

  btor_delete_ptr_hash_table (ids);
  lglrelease (lgl);

  delta = btor_time_stamp () - start;
  btor->time.skel += delta;
  BTOR_MSG (btor->msg, 1,
      "skeleton preprocessing produced %d new constraints in %.1f seconds",
      fixed, delta);
  assert (btor_check_id_table_mark_unset_dbg (btor));
}