#ifdef DOC_BUILD_INTERNAL
#endif
#include "state.h"
BOOL state_InferencesCompare(POINTER Inf1, POINTER Inf2) {
  return (clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf1)) > clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf2)) ||
   (clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf1)) == clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf2)) &&
    (unsigned)array_STupleSecond(*(ARRAY*)Inf1) > (unsigned)array_STupleSecond(*(ARRAY*)Inf2)) ||
   (clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf1)) == clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf2)) &&
    (unsigned)array_STupleSecond(*(ARRAY*)Inf1) == (unsigned)array_STupleSecond(*(ARRAY*)Inf2) &&
    clause_GetId((CLAUSE)array_STupleThird(*(ARRAY*)Inf1)) > clause_GetId((CLAUSE)array_STupleThird(*(ARRAY*)Inf2))) ||
   (clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf1)) == clause_GetId((CLAUSE)array_STupleFirst(*(ARRAY*)Inf2)) &&
    (unsigned)array_STupleSecond(*(ARRAY*)Inf1) == (unsigned)array_STupleSecond(*(ARRAY*)Inf2) &&
    clause_GetId((CLAUSE)array_STupleThird(*(ARRAY*)Inf1)) == clause_GetId((CLAUSE)array_STupleThird(*(ARRAY*)Inf2)) &&
    (unsigned)array_STupleFourth(*(ARRAY*)Inf1) > (unsigned)array_STupleFourth(*(ARRAY*)Inf2)));
}
BOOL state_InferencesEqual(ARRAY Inf1, ARRAY Inf2) {
  return (array_STupleFirst(Inf1) == array_STupleFirst(Inf2) &&
   array_STupleSecond(Inf1) == array_STupleSecond(Inf2) &&
   array_STupleThird(Inf1) == array_STupleThird(Inf2) &&
   array_STupleFourth(Inf1) == array_STupleFourth(Inf2));
}
void state_InferencesRemoveDuplicates(SCLSTATE State)
{
  ARRAY inferences;
  MEMORY mem;
  unsigned cnum,i,j;
  inferences = state_GetInferences(State);
  mem = state_GetMemory(State);
  cnum = array_GetSize(inferences);
  srtg_AMergeSortCompare(mem, inferences, inferences, inferences+(cnum-1), (BOOL (*)(POINTER,POINTER))state_InferencesCompare);
  for (i=0,j=1; j<cnum; ++j) {
    if (state_InferencesEqual((ARRAY)array_GetElement(inferences, i), (ARRAY)array_GetElement(inferences, j))) {
      array_STupleDelete(mem, (ARRAY)array_GetElement(inferences, i));
      inferences[i] = inferences[j];
      inferences[j] = (intptr_t)NULL;
    }
    else {
      i = j;
    }
  }
  i = 0;
  for (j=0;j<cnum;j++) {
    if (inferences[j] != (intptr_t)NULL) {
      inferences[i] = inferences[j];
      i++;
    }
  }
  array_SetSize(inferences, i);
}
SCLSTATE state_Create(MEMORY Mem, FOLSTATE Folstate, TERMSTATE Termstate, SYMBSTATE Symbstate, SUBSTSTATE SState, CCBS_STATE Congrstate, CLOCK Clock, SHARER Sharer)
{
  SCLSTATE state;
  state = memory_Malloc(Mem, sizeof(SCLSTATE_NODE));
  state->memory = Mem;
  state->symbstate = Symbstate;
  state->termstate = Termstate;
  state->folstate = Folstate;
  state->sstate = SState;
  state->tptpstate = NULL;
  state->congrstate = Congrstate;
  state->foltype = symbol_CreateTypeConstant(Symbstate, "F");
  state->namehash = ohash_Create(Mem,16,(OHASH_HASH_FCT)ohash_StringHash, (OHASH_ELEM_EQUAL)ohash_StringEq);
  state->predicates = NULL;
  state->functions = NULL;
  state->constants = NULL;
  state->groundatoms = array_Create(Mem,10);
  state->poslits = distrn_Create(Mem);
  state->neglits = distrn_Create(Mem);
  state->id = 1;
  state->inputlits = 0;
  state->inputclauses = 0;
  state->sharer = Sharer;
  state->pconstants = array_Create(Mem, 10);
  state->workedoff = (ARRAY)NULL;
  state->usable = (ARRAY)NULL;
  state->clauses = (ARRAY)NULL;
  state->candidates = (ARRAY)NULL;
  state->inferences = array_Create(Mem, 10);
  state->lclauses = array_Create(Mem, 20);
  state->conclauses = array_Create(Mem,10);
  state->lastclauseid = 0;
  state->redclauses = array_Create(Mem,10);
  state->instance = inst_CreateEmpty(Mem);
  state->empty = (CLAUSE)NULL;
  state->equations = FALSE;
  state->variables = FALSE;
  state->linear = TRUE;
  state->inputconjecture = FALSE;
  state->learned = 0;
  state->decisions = 0;
  state->propagations = 0;
  state->resolutions = 0;
  state->condens = 0;
  state->subres = 0;
  state->overall = Clock;
  state->seconds = 0;
  state->namehash = ohash_Insert(state->namehash, (OHASH_KEY_TYPE)string_StringCopy(Mem,symbol_TypeName(Symbstate, fol_TypeTop(Folstate))), fol_TypeTop(Folstate), NULL, NULL);
  state->namehash = ohash_Insert(state->namehash, (OHASH_KEY_TYPE)string_StringCopy(Mem,symbol_GetName(Symbstate, fol_Equal(Folstate))), fol_Equal(Folstate), NULL, NULL);
  state->abstr = TRUE;
  return state;
}
void state_OhashStringFree(MEMORY Mem, OHASH_KEY_TYPE Key)
{
  string_StringFree(Mem, (char*)Key);
}
void state_Delete(SCLSTATE State)
{
  unsigned i;
  ohash_Delete(State->namehash, (OHASH_DELETE_KEY_FCT)state_OhashStringFree, NULL);
  if (state_GetTptpstate(State)) {
    tptp_Free(state_GetTptpstate(State));
    state_SetTptpstate(State, NULL);
  }
  if (State->clauses) {
    for (i=0; i<array_GetSize(State->clauses);i++)
      clause_SharedDelete(State->memory, State->termstate, (CLAUSE)(State->clauses)[i]);
    array_Delete(State->memory, State->clauses);
  }
  for (i=0; i<array_GetSize(State->redclauses);i++)
    clause_SharedDelete(State->memory, State->termstate, (CLAUSE)(State->redclauses)[i]);
  array_Delete(State->memory, State->redclauses);
  array_Delete(State->memory, State->predicates);
  array_Delete(State->memory, State->constants);
  array_Delete(State->memory, State->functions);
  array_Delete(State->memory, State->conclauses);
  array_Delete(State->memory, State->lclauses);
  for (i=0; i<array_GetSize(State->inferences);i++)
    array_QTupleDelete(State->memory, (ARRAY)State->inferences[i]);
  array_Delete(State->memory, State->inferences);
  if (State->pconstants)
    array_Delete(State->memory, State->pconstants);
  if (State->workedoff)
    array_Delete(State->memory, State->workedoff);
  if (State->usable)
    array_Delete(State->memory, State->usable);
  if (State->candidates)
    array_Delete(State->memory, State->candidates);
  array_Delete(State->memory, State->groundatoms);
  distrn_DeleteWithEntries(State->memory, State->poslits, array_PairDelete);
  distrn_DeleteWithEntries(State->memory, State->neglits, array_PairDelete);
  if (State->overall)
    clock_Delete(State->memory, State->overall);
  sharing_Free(State->sharer);
  inst_Delete(State->memory, State->instance);
  memory_Free(State->memory, State, sizeof(SCLSTATE_NODE));
}
void state_SetLastClauseId(SCLSTATE State, unsigned Id)
{
  State->lastclauseid = Id;
}
unsigned state_GetLastClauseId(SCLSTATE State)
{
  return State->lastclauseid;
}
ARRAY state_GetRedClauses(SCLSTATE State)
{
  return State->redclauses;
}
void state_SetRedClauses(SCLSTATE State, ARRAY Redclauses)
{
  State->redclauses = Redclauses;
}
INST state_GetInstance(SCLSTATE State)
{
  return State->instance;
}
ARRAY state_GetUsables(SCLSTATE State)
{
  return State->usable;
}
void state_SetUsables(SCLSTATE State, ARRAY Usables)
{
  State->usable = Usables;
}
ARRAY state_GetCandidates(SCLSTATE State)
{
  return State->candidates;
}
void state_SetCandidates(SCLSTATE State, ARRAY Candidates)
{
  State->candidates = Candidates;
}
ARRAY state_GetInferences(SCLSTATE State)
{
  return State->inferences;
}
void state_SetInferences(SCLSTATE State, ARRAY Inferences)
{
  State->inferences = Inferences;
}
SHARER state_GetSharer(SCLSTATE State)
{
  return State->sharer;
}
ARRAY state_GetWorkedOff(SCLSTATE State)
{
  return State->workedoff;
}
void state_SetWorkedOff(SCLSTATE State, ARRAY WorkedOff)
{
  State->workedoff = WorkedOff;
}
void state_SetId(SCLSTATE State, unsigned Id)
{
  State->id = Id;
}
unsigned state_GetId(SCLSTATE State)
{
  return State->id;
}
unsigned state_GetNextIncrementId(SCLSTATE State)
{
  return State->id++;
}
void state_SetNumberInputLits(SCLSTATE State, unsigned Lits)
{
  State->inputlits = Lits;
}
unsigned state_GetNumberInputLits(SCLSTATE State)
{
  return State->inputlits;
}
void state_SetPredicates(SCLSTATE State, ARRAY Symbols)
{
  State->predicates = Symbols;
}
ARRAY state_GetPredicates(SCLSTATE State)
{
  return State->predicates;
}
void state_AddPredicate(SCLSTATE State, SYMBOL Symbol)
{
  state_SetPredicates(State,
        array_Add(state_GetMemory(State),
    state_GetPredicates(State),
    (intptr_t)Symbol));
}
void state_SetGroundAtoms(SCLSTATE State, ARRAY Atoms)
{
  State->groundatoms = Atoms;
}
ARRAY state_GetGroundAtoms(SCLSTATE State)
{
  return State->groundatoms;
}
void state_AddGroundAtom(SCLSTATE State, TERM Atom)
{
  State->groundatoms = array_Add(State->memory, State->groundatoms, (intptr_t)Atom);
}
void state_SetConstants(SCLSTATE State, ARRAY Symbols)
{
  State->constants = Symbols;
}
ARRAY state_GetConstants(SCLSTATE State)
{
  return State->constants;
}
void state_AddConstant(SCLSTATE State, SYMBOL Symbol)
{
  state_SetConstants(State,
       array_Add(state_GetMemory(State),
          state_GetConstants(State),
          (intptr_t)Symbol));
}
void state_SetPConstants(SCLSTATE State, ARRAY Symbols)
{
  State->pconstants = Symbols;
}
ARRAY state_GetPConstants(SCLSTATE State)
{
  return State->pconstants;
}
void state_AddPConstant(SCLSTATE State, SYMBOL Symbol)
{
  state_SetPConstants(State,
       array_Add(state_GetMemory(State),
          state_GetPConstants(State),
          (intptr_t)Symbol));
}
void state_SetFunctions(SCLSTATE State, ARRAY Symbols)
{
  State->functions = Symbols;
}
ARRAY state_GetFunctions(SCLSTATE State)
{
  return State->functions;
}
void state_AddFunction(SCLSTATE State, SYMBOL Symbol)
{
  state_SetFunctions(State,
       array_Add(state_GetMemory(State),
          state_GetFunctions(State),
          (intptr_t)Symbol));
}
void state_SetEmptyClause(SCLSTATE State, CLAUSE Clause)
{
  State->empty = Clause;
}
CLAUSE state_GetEmptyClause(SCLSTATE State)
{
  return State->empty;
}
void state_SetInputConjecture(SCLSTATE State, BOOL InputConjecture)
{
  State->inputconjecture = InputConjecture;
}
BOOL state_HasInputConjecture(SCLSTATE State)
{
  return State->inputconjecture;
}
const char* state_GetSZSUnsatStatus(SCLSTATE State)
{
  return state_HasInputConjecture(State) ? "Theorem" : "Unsatisfiable";
}
const char* state_GetSZSSatStatus(SCLSTATE State)
{
  return state_HasInputConjecture(State) ? "CounterSatisfiable" : "Satisfiable";
}
OHASH state_GetNamehash(SCLSTATE State)
{
  return State->namehash;
}
TYPE state_GetFolType(SCLSTATE State)
{
  return State->foltype;
}
MEMORY state_GetMemory(SCLSTATE State)
{
  return State->memory;
}
TERMSTATE state_GetTermstate(SCLSTATE State)
{
  return State->termstate;
}
CCBS_STATE state_GetCongrstate(SCLSTATE State)
{
  return State->congrstate;
}
SYMBSTATE state_GetSymbstate(SCLSTATE State)
{
  return State->symbstate;
}
FOLSTATE state_GetFolstate(SCLSTATE State)
{
  return State->folstate;
}
SUBSTSTATE state_GetSubststate(SCLSTATE State)
{
  return State->sstate;
}
TPTPSTATE state_GetTptpstate(SCLSTATE State)
{
  return State->tptpstate;
}
void state_SetTptpstate(SCLSTATE State, TPTPSTATE Tptpstate)
{
  State->tptpstate = Tptpstate;
}
DISTRN state_GetPoslits(SCLSTATE State)
{
  return State->poslits;
}
DISTRN state_GetNeglits(SCLSTATE State)
{
  return State->neglits;
}
void state_SetClauses(SCLSTATE State, ARRAY Input)
{
  State->clauses = Input;
}
ARRAY state_GetClauses(SCLSTATE State)
{
  return State->clauses;
}
void state_AddConClause(SCLSTATE State, CLAUSE Clause)
{
  State->conclauses = array_Add(State->memory, State->conclauses, (intptr_t)Clause);
}
ARRAY state_GetConClauses(SCLSTATE State)
{
  return State->conclauses;
}
CLAUSE state_GetClauseById(SCLSTATE State, unsigned Id)
{
  unsigned i;
  for (i=0;i<array_GetSize(State->clauses);i++)
    if (clause_GetId((CLAUSE)(State->clauses)[i]) == Id)
 return (CLAUSE)(State->clauses)[i];
  return NULL;
}
void state_AddLearnedClause(SCLSTATE State, CLAUSE Clause)
{
  State->lclauses = array_Add(State->memory, State->lclauses, (intptr_t)Clause);
}
ARRAY state_GetLearnedClauses(SCLSTATE State)
{
  return State->lclauses;
}
void state_SetNumberInputClauses(SCLSTATE State, unsigned Cls)
{
  State->inputclauses = Cls;
}
unsigned state_GetNumberInputClauses(SCLSTATE State)
{
  return State->inputclauses;
}
void state_SetNamehash(SCLSTATE State, OHASH Hash)
{
  State->namehash = Hash;
}
void state_SetFilename(SCLSTATE State, char * Filename)
{
  State->filename = Filename;
}
char * state_GetFilename(SCLSTATE State)
{
  return State->filename;
}
void state_AddClause(SCLSTATE State, CLAUSE clause)
{
  State->clauses = array_Add(State->memory, State->clauses, (intptr_t)clause);
}
void state_SetProblemHasEquations(SCLSTATE State, BOOL Equations)
{
  State->equations = Equations;
}
void state_SetProblemIsLinear(SCLSTATE State, BOOL Linear)
{
  State->linear = Linear;
}
void state_SetProblemHasVariables(SCLSTATE State, BOOL Variables)
{
  State->variables = Variables;
}
BOOL state_IsBSProblem(SCLSTATE State)
{
  return (array_GetSize(State->functions) == 0);
}
BOOL state_IsProblemLinear(SCLSTATE State)
{
  return State->linear;
}
BOOL state_IsBSREqProblem(SCLSTATE State)
{
  return (state_IsBSProblem(State) && State->equations);
}
BOOL state_IsGroundProblem(SCLSTATE State)
{
  return (!State->variables);
}
BOOL state_IsPropGroundProblem(SCLSTATE State)
{
  return (state_IsGroundProblem(State) && !state_IsEqualityProblem(State));
}
BOOL state_IsEqualityProblem(SCLSTATE State)
{
  return (State->equations);
}
BOOL state_AllPredicatesPropVariables(SCLSTATE State)
{
  unsigned i;
  for (i=0; i<array_GetSize(State->predicates); i++)
    if (symbol_GetArity(State->symbstate, (SYMBOL)((State->predicates)[i])) > 0)
      return FALSE;
  return TRUE;
}
void state_PrintProblemProperties(SCLSTATE State)
{
  printf("%% Problem Properties:\n");
  if (state_IsGroundProblem(State)) {
    if (state_AllPredicatesPropVariables(State))
      printf("%% This is a propositional problem.");
    else {
      if (state_IsEqualityProblem(State))
 printf("%% This is a first-order ground problem with equality.");
      else
 printf("%% This is a first-order ground problem without equality.");
    }
  }
  else {
    if (array_GetSize(State->functions) == 0) {
      if (state_IsEqualityProblem(State))
 printf("%% This is a Bernays Schoenfinkel Ramsey problem.");
      else
 printf("%% This is a Bernays Schoenfinkel problem.");
    }
    else {
      if (state_IsEqualityProblem(State))
 printf("%% This is a full first-order problem with equality.");
      else
 printf("%% This is a full first-order problem without equality.");
    }
    if (state_IsProblemLinear(State))
      printf(" The Problem is linear. ");
    else
      printf(" The Problem is non-linear. ");
  }
  printf("\n");
}
void state_PrintStatistics(SCLSTATE State)
{
  printf("%% Problem: %s\n\n", state_GetFilename(State));
  printf("%% SPASS-SCL-FOL Statistics:\n");
  printf("%% Number of learned clauses: %u\n", state_GetNumberOfLearnedClauses(State));
  printf("%% Number of propagations: %u\n", state_GetPropagations(State));
  printf("%% Number of decisions: %u\n", state_GetDecisions(State));
  printf("%% Number of resolutions: %u\n", state_GetResolutions(State));
  printf("%% Number of condensations: %u\n", state_GetCondensations(State));
  printf("%% Number of sub resolutions: %u\n", state_GetSubResolutions(State));
}
void state_SetSeconds(SCLSTATE State, int Seconds)
{
  State->seconds = Seconds;
}
int state_GetSeconds(SCLSTATE State)
{
  return State->seconds;
}
CLOCK state_GetClockOverall(SCLSTATE State)
{
  return State->overall;
}
void state_AnalyzeInputClauses(SCLSTATE State)
{
  unsigned i,j;
  ARRAY clauses;
  CLAUSE clause;
  BOOL vars, equations, linear;
  clauses = state_GetClauses(State);
  vars = FALSE;
  equations = FALSE;
  linear = TRUE;
  for (i=0;i<array_GetSize(clauses) && (!(vars && equations) || linear);i++) {
    clause = (CLAUSE)array_GetElement(clauses, i);
    if (!vars && clause_GetVariables(clause) > 0){
      state_SetProblemHasVariables(State, TRUE);
      vars = TRUE;
    }
    if (!equations || linear) {
      for (j=0;j<clause_GetFolLiterals(clause); j++) {
 if (fol_IsEqual(state_GetFolstate(State), clause_GetAtom(clause, j))) {
   state_SetProblemHasEquations(State, TRUE);
   equations = TRUE;
 }
 if (!term_IsLinear(state_GetTermstate(State), clause_GetAtom(clause, j))) {
   linear = FALSE;
   state_SetProblemIsLinear(State, FALSE);
 }
      }
    }
  }
}
unsigned state_GetNumberOfLearnedClauses(SCLSTATE State)
{
  return State->learned;
}
void state_IncLearnedClauses(SCLSTATE State)
{
  State->learned++;
}
unsigned state_GetDecisions(SCLSTATE State)
{
  return State->decisions;
}
void state_IncDecisions(SCLSTATE State)
{
  State->decisions++;
}
unsigned state_GetPropagations(SCLSTATE State)
{
  return State->propagations;
}
void state_IncPropagations(SCLSTATE State)
{
  State->propagations++;
}
unsigned state_GetResolutions(SCLSTATE State)
{
  return State->resolutions;
}
void state_IncResolutions(SCLSTATE State)
{
  State->resolutions++;
}
unsigned state_GetCondensations(SCLSTATE State)
{
  return State->condens;
}
void state_IncCondensations(SCLSTATE State)
{
  State->condens++;
}
unsigned state_GetSubResolutions(SCLSTATE State)
{
  return State->subres;
}
void state_IncSubResolutions(SCLSTATE State)
{
  State->subres++;
}
BOOL state_GetAbstraction(SCLSTATE State)
{
  return State->abstr;
}
void state_SetAbstraction(SCLSTATE State, BOOL Abstr)
{
  State->abstr = Abstr;
}
