#ifdef DOC_BUILD_INTERNAL
#endif
#include "trail.h"
MEMORY trail_GetMemory(TRAIL Trail)
{
  return Trail->memory;
}
SYMBSTATE trail_GetSymbstate(TRAIL Trail)
{
  return Trail->symbstate;
}
TERMSTATE trail_GetTermstate(TRAIL Trail)
{
  return Trail->termstate;
}
FOLSTATE trail_GetFolstate(TRAIL Trail)
{
  return Trail->folstate;
}
SUBSTSTATE trail_GetSubststate(TRAIL Trail)
{
  return Trail->sstate;
}
TRAIL trail_Create(MEMORY Mem, SYMBSTATE Symbstate, TERMSTATE Termstate, FOLSTATE Folstate, SUBSTSTATE SState, SHARER Sharer)
{
  TRAIL trail;
  unsigned i;
  trail = memory_Malloc(Mem, sizeof(TRAIL_NODE));
  trail->memory = Mem;
  trail->symbstate = Symbstate;
  trail->termstate = Termstate;
  trail->folstate = Folstate;
  trail->sstate = SState;
  trail->sharer = Sharer;
  trail->tsize = 0;
  trail->tcap = 32;
  trail->trail = memory_Malloc(Mem, sizeof(TRAILDATA_NODE)*trail->tcap);
  trail->dl = 0U;
  trail->mapcap = array_GetSize(Sharer->shared) > 2 ? array_GetSize(Sharer->shared) : 2;
  trail->sharedmap = memory_Malloc(Mem, sizeof(unsigned)*trail->mapcap);
  for(i = 0; i < trail->mapcap; i++)
    trail->sharedmap[i] = UINT_MAX;
  trail->posidx = pathidx_Create(Termstate,SState,Folstate,32);
  trail->negidx = pathidx_Create(Termstate,SState,Folstate,32);
  trail->cstack = array_Create(trail_GetMemory(trail), 5);
  trail->iter = NULL;
  return trail;
}
void trail_Delete(TRAIL Trail)
{
  TRAILDATA data;
  while (Trail->tsize > 0) {
    Trail->tsize--;
    data = Trail->trail + Trail->tsize;
    if (data->subst) {
      subst_DeleteWithTerms(trail_GetTermstate(Trail), trail_GetMemory(Trail), data->subst);
      data->subst = (SUBST)NULL;
    }
  }
  pathidx_Delete(Trail->posidx);
  pathidx_Delete(Trail->negidx);
  array_Delete(Trail->memory,Trail->cstack);
  memory_Free(Trail->memory,Trail->trail,sizeof(TRAILDATA_NODE)*Trail->tcap);
  memory_Free(Trail->memory,Trail->sharedmap,sizeof(unsigned)*Trail->mapcap);
  memory_Free(Trail->memory,Trail,sizeof(TRAIL_NODE));
}
void trail_SetDl(TRAIL Trail, unsigned Dl)
{
  Trail->dl = Dl;
}
void trail_IncreaseDl(TRAIL Trail)
{
  Trail->dl++;
}
unsigned trail_GetDl(TRAIL Trail)
{
  return Trail->dl;
}
TRAILDATA trail_GetDecisionAtDl(TRAIL Trail, unsigned Dl)
{
  return trail_GetDataFromTrailPos(Trail,array_GetElement(Trail->cstack, Dl));
}
unsigned trail_GetNumberLitUpToCurrDl(TRAIL Trail)
{
#ifdef SPASS_CHECK
  if (array_GetSize(Trail->cstack) == 0) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s:", __func__);
    misc_ErrorReport("\n There should be at least one decision.");
    misc_FinishErrorReport();
  }
#endif
  return array_GetElement(Trail->cstack, trail_GetDl(Trail));
}
void trail_NewDl(TRAIL Trail)
{
  Trail->cstack = array_Add(trail_GetMemory(Trail), Trail->cstack, trail_GetSize(Trail));
  trail_IncreaseDl(Trail);
}
 void trail_CStackAdjustSize(TRAIL Trail)
{
  array_SetSize(Trail->cstack, trail_GetDl(Trail));
}
unsigned trail_GetSize(TRAIL Trail)
{
  return Trail->tsize;
}
unsigned trail_AtomTrailPositionFromData(TRAIL Trail, TRAILDATA Data)
{
  return Data->tpos;
}
void trail_SetSize(TRAIL Trail, unsigned NewSize)
{
  Trail->tsize = NewSize;
}
unsigned trail_GetCapacity(TRAIL Trail)
{
  return Trail->tcap;
}
void trail_SetCapacity(TRAIL Trail, unsigned NewCap)
{
  Trail->tcap = NewCap;
}
void trail_AddDataToIdx(TRAIL Trail, unsigned TrailPos);
TRAILDATA trail_Add(TRAIL Trail, TERM Atom, CLAUSE Origin, SUBST Subst, unsigned ClausePos, BOOL IsPositive, BOOL Propagation)
{
  if (trail_GetSize(Trail) + 1 > trail_GetCapacity(Trail)) {
    unsigned oldcap;
    oldcap = trail_GetCapacity(Trail);
    trail_SetCapacity(Trail, oldcap*2);
    Trail->trail = memory_Realloc(trail_GetMemory(Trail), Trail->trail,
      (oldcap)*sizeof(TRAILDATA_NODE),
      trail_GetCapacity(Trail)*sizeof(TRAILDATA_NODE));
  }
  while (sharing_SharedIdx(Atom) >= Trail->mapcap) {
    unsigned oldcap;
    oldcap = Trail->mapcap;
    Trail->mapcap = Trail->mapcap*2;
    Trail->sharedmap = memory_Realloc(trail_GetMemory(Trail), Trail->sharedmap,
          (oldcap)*sizeof(unsigned),
          Trail->mapcap *sizeof(unsigned));
    for(; oldcap < Trail->mapcap; oldcap++)
      Trail->sharedmap[oldcap] = UINT_MAX;
  }
  TRAILDATA data;
  data = Trail->trail + trail_GetSize(Trail);
  data->atom = Atom;
  data->reason = Origin;
  data->subst = Subst;
  data->dinfo = trail_GetDl(Trail) << 1;
  if(Propagation){
    data->dinfo = data->dinfo | 1U;
  }
  data->cinfo = ClausePos << 1;
  data->tpos = trail_GetSize(Trail);
  if(IsPositive){
    data->cinfo = data->cinfo | 1U;
    data->ipos = pathidx_ForceInsert(Trail->posidx,Atom,(TYPING)NULL,(intptr_t)data->tpos);
  }else{
    data->ipos = pathidx_ForceInsert(Trail->negidx,Atom,(TYPING)NULL,(intptr_t)data->tpos);
  }
  trail_SetSize(Trail, trail_GetSize(Trail) + 1);
  Trail->sharedmap[sharing_SharedIdx(Atom)] = data->tpos;
  return data;
}
BOOL trail_IsPositiveFromData(TRAIL Trail, TRAILDATA Data)
{
  return (Data->cinfo & 1U) ? TRUE : FALSE;
}
unsigned trail_AtomIdFromData(TRAIL Trail, TRAILDATA Data)
{
  return (Data->cinfo)>>1;
}
unsigned trail_ClausePositionFromData(TRAIL Trail, TRAILDATA Data)
{
  return Data->cinfo >> 1;
}
TERM trail_AtomFromData(TRAIL Trail, TRAILDATA Data)
{
  return Data->atom;
}
unsigned trail_LitDlFromData(TRAIL Trail, TRAILDATA Data)
{
  return Data->dinfo >> 1;
}
BOOL trail_LitIsDecision(TRAIL Trail, TRAILDATA Data)
{
  return (Data->dinfo & 1U) ? FALSE : TRUE;
}
CLAUSE trail_LitReasonFromData(TRAIL Trail, TRAILDATA Data)
{
  return Data->reason;
}
SUBST trail_LitSubstFromData(TRAIL Trail, TRAILDATA Data)
{
    return Data->subst;
}
void trail_PrintData(TRAIL Trail, TRAILDATA Data)
{
  if (!trail_IsPositiveFromData(Trail,Data))
    printf("~");
  term_PrintPrefix(trail_GetTermstate(Trail),
     trail_AtomFromData(Trail,Data));
  if (trail_LitIsDecision(Trail,Data))
    printf("[%s,%u]", "D" ,trail_LitDlFromData(Trail,Data));
  else
    printf("[%s,%u,%u,%u]", "P" ,trail_LitDlFromData(Trail,Data), clause_GetId(trail_LitReasonFromData(Trail, Data)),trail_AtomIdFromData(Trail, Data));
}
void trail_PrintStatus(VAR_STATUS Status)
{
  switch(Status){
  case SET_TRUE:
    printf("T");
    break;
  case SET_FALSE:
    printf("F");
    break;
  default:
    printf("U");
  }
}
void trail_Print(TRAIL Trail)
{
  unsigned i;
  TRAILDATA data;
  for (i = 0; i < trail_GetSize(Trail); i++) {
    printf("\n %u ", i);
    data = trail_GetDataFromTrailPos(Trail,i);
    if (!trail_IsPositiveFromData(Trail,data))
      printf("~");
    term_PrintPrefix(trail_GetTermstate(Trail),
       trail_AtomFromData(Trail,data));
    if (trail_LitReasonFromData(Trail,data)) {
      printf(":%d*", clause_GetId(trail_LitReasonFromData(Trail,data)));
      subst_Print(trail_GetTermstate(Trail),trail_GetSymbstate(Trail),
    trail_LitSubstFromData(Trail,data));
    }
    else
      printf(":D:%d",trail_LitDlFromData(Trail,data));
    printf(" ");
  }
}
void trail_CheckWithinBounds(TRAIL Trail, unsigned Pos)
{
#ifdef SPASS_CHECK
  if(Trail->tsize <= Pos){
    misc_StartErrorReport();
    misc_ErrorReport("\n %s: ");
    misc_ErrorReport("tried to access trail position (=%u) that is out of bounds (>= %u). ",Pos,Trail->tsize);
    misc_FinishErrorReport();
  }
#endif
}
BOOL trail_IsPositiveFromTrailPos(TRAIL Trail, unsigned Pos)
{
  return trail_IsPositiveFromData(Trail,trail_GetDataFromTrailPos(Trail,Pos));
}
unsigned trail_ClausePositionFromTrailPos(TRAIL Trail, unsigned Pos)
{
  return trail_ClausePositionFromData(Trail,trail_GetDataFromTrailPos(Trail,Pos));
}
TERM trail_GetAtomFromTrailPos(TRAIL Trail, unsigned Pos)
{
  return trail_AtomFromData(Trail,trail_GetDataFromTrailPos(Trail,Pos));
}
CLAUSE trail_GetReasonFromTrailPos(TRAIL Trail, unsigned Pos)
{
  return trail_LitReasonFromData(Trail,trail_GetDataFromTrailPos(Trail,Pos));
}
unsigned trail_GetDlFromTrailPos(TRAIL Trail, unsigned Pos)
{
  return trail_LitDlFromData(Trail,trail_GetDataFromTrailPos(Trail,Pos));
}
BOOL trail_IsDecisionFromTrailPos(TRAIL Trail, unsigned Pos)
{
  return trail_LitIsDecision(Trail,trail_GetDataFromTrailPos(Trail,Pos));
}
TRAILDATA trail_GetDataFromTrailPos(TRAIL Trail, unsigned Pos)
{
  trail_CheckWithinBounds(Trail, Pos);
  return Trail->trail + Pos;
}
TRAILDATA trail_LitData(TRAIL Trail, TERM Lit, SUBST Subst)
{
  TERM atom;
  if(symbol_Equal(term_TopSymbol(Lit),fol_Not(Trail->folstate))){
    atom = term_FirstArgument(Lit);
  }else{
    atom = Lit;
  }
  return trail_AtomData(Trail,atom,Subst);
}
TRAILDATA trail_AtomData(TRAIL Trail, TERM Atom, SUBST Subst)
{
  TERM iterm;
  intptr_t idata;
  TRAILDATA data;
  iterm = (TERM)NULL;
  idata = (intptr_t)NULL;
  if(!pathidx_FindExactMatch(Trail->posidx,Atom,Subst,NULL,&iterm,&idata) &&
     !pathidx_FindExactMatch(Trail->negidx,Atom,Subst,NULL,&iterm,&idata)){
    return NULL;
  }else{
    data = trail_GetDataFromTrailPos(Trail,idata);
    return data;
  }
}
TRAILDATA trail_AtomDataPos(TRAIL Trail, TERM Atom, SUBST Subst, unsigned Pos)
{
  TERM iterm;
  intptr_t idata;
  TRAILDATA data;
  iterm = (TERM)NULL;
  idata = (intptr_t)NULL;
  data = (TRAILDATA)NULL;
  if(!pathidx_FindExactMatch(Trail->posidx,Atom,Subst,NULL,&iterm,&idata) &&
     !pathidx_FindExactMatch(Trail->negidx,Atom,Subst,NULL,&iterm,&idata)){
    return NULL;
  }else{
    if (idata <= Pos)
      data = trail_GetDataFromTrailPos(Trail,idata);
    return data;
  }
}
BOOL trail_IsLitSet(TRAIL Trail, TERM Lit, SUBST Subst)
{
  return trail_LitData(Trail,Lit,Subst) != NULL;
}
VAR_STATUS trail_LitStatusFromLit(TRAIL Trail, TERM Lit, SUBST Subst)
{
  TRAILDATA data;
  data = trail_LitData(Trail,Lit,Subst);
  if(data == NULL){
    return NOT_SET;
  }
  if(trail_LitIsPositive(Trail,Lit)){
    return trail_IsPositiveFromData(Trail,data) ? SET_TRUE : SET_FALSE;
  }else{
    return trail_IsPositiveFromData(Trail,data) ? SET_FALSE : SET_TRUE;
  }
}
VAR_STATUS trail_LitStatusFromAtom(TRAIL Trail, TERM Atom, SUBST Subst, BOOL Positive)
{
  TRAILDATA data;
  data = trail_AtomData(Trail,Atom,Subst);
  if(data == NULL){
    return NOT_SET;
  }
  if(Positive){
    return trail_IsPositiveFromData(Trail,data) ? SET_TRUE : SET_FALSE;
  }else{
    return trail_IsPositiveFromData(Trail,data) ? SET_FALSE : SET_TRUE;
  }
}
VAR_STATUS trail_LitStatusFromAtomPos(TRAIL Trail, TERM Atom, SUBST Subst, BOOL Positive, unsigned Pos)
{
  TRAILDATA data;
  data = trail_AtomData(Trail,Atom,Subst);
  if(data == NULL || trail_AtomTrailPositionFromData(Trail, data) > Pos) {
    return NOT_SET;
  }
  if(Positive){
    return trail_IsPositiveFromData(Trail,data) ? SET_TRUE : SET_FALSE;
  }else{
    return trail_IsPositiveFromData(Trail,data) ? SET_FALSE : SET_TRUE;
  }
}
CLAUSE trail_LitReason(TRAIL Trail, TERM Lit, SUBST Subst)
{
  TRAILDATA data;
  data = trail_LitData(Trail,Lit,Subst);
  if(data == NULL){
    return NULL;
  }else{
    return trail_LitReasonFromData(Trail,data);
  }
}
unsigned trail_LitDl(TRAIL Trail, TERM Lit, SUBST Subst)
{
  TRAILDATA data;
  data = trail_LitData(Trail,Lit,Subst);
  if(data == NULL){
    return UINT_MAX;
  }else{
    return trail_LitDlFromData(Trail,data);
  }
}
ARRAY trail_FalseInstancesOfLit(TRAIL Trail, TERM Lit, SUBST Subst, TYPING Types, unsigned Vars)
{
  PATHIDX idx;
  TERM atom;
  if(symbol_Equal(term_TopSymbol(Lit),fol_Not(Trail->folstate))){
    atom = term_FirstArgument(Lit);
    idx = Trail->posidx;
  }else{
    atom = Lit;
    idx = Trail->negidx;
  }
  return pathidx_FindInstances(idx,atom,Subst,Types,Vars);
}
ARRAY trail_FalseInstancesOfAtom(TRAIL Trail, TERM Atom, SUBST Subst, BOOL Positive, TYPING Types, unsigned Vars)
{
  PATHIDX idx;
  if(!Positive){
    idx = Trail->posidx;
  }else{
    idx = Trail->negidx;
  }
  return pathidx_FindInstances(idx,Atom,Subst,Types,Vars);
}
ARRAY trail_FalseInstancesOfAtomPure(TRAIL Trail, TERM Atom, SUBST Subst, BOOL Positive, TYPING Types, ARRAY *Result)
{
  PATHIDX idx;
  if(!Positive){
    idx = Trail->posidx;
  }else{
    idx = Trail->negidx;
  }
  return pathidx_FindInstancesPure(idx,Atom,Subst,Types, Result);
}
BOOL trail_StartFalseInstancesIterator(TRAIL Trail, TERM Atom, SUBST Subst, BOOL Positive, TYPING Types)
{
#ifdef SPASS_CHECK
  if(trail_FalseInstancesIteratorIsRunning(Trail)){
    misc_StartUserErrorReport();
    misc_UserErrorReport("%s: there is already a running iterator.",__func__);
    misc_FinishUserErrorReport();
  }
#endif
  PATHIDX idx;
  if(!Positive){
    idx = Trail->posidx;
  }else{
    idx = Trail->negidx;
  }
  Trail->iter = idx;
  return pathidx_StartInstancesIterator(idx,Atom,Subst,Types);
}
BOOL trail_NextFalseInstanceFromIterator(TRAIL Trail, TERM Atom, SUBST Subst, BOOL Positive, TYPING Types)
{
#ifdef SPASS_CHECK
  if(!trail_FalseInstancesIteratorIsRunning(Trail)){
    misc_StartUserErrorReport();
    misc_UserErrorReport("%s: there is no running iterator.",__func__);
    misc_FinishUserErrorReport();
  }
#endif
  return pathidx_NextInstanceFromIterator(Trail->iter,Atom,Subst,Types);
}
BOOL trail_NextFalseInstanceFromIteratorPos(TRAIL Trail, TERM Atom, SUBST Subst, BOOL Positive, TYPING Types, unsigned Pos)
{
#ifdef SPASS_CHECK
  if(!trail_FalseInstancesIteratorIsRunning(Trail)){
    misc_StartUserErrorReport();
    misc_UserErrorReport("%s: there is no running iterator.",__func__);
    misc_FinishUserErrorReport();
  }
#endif
  unsigned pos, i, size;
  ARRAY instances;
  PATHIDX index;
  index = Trail->iter;
  instances = index->tempar1;
  i = index->iter_pos;
  if (i == UINT_MAX){
    i = 0;
  }else{
    i++;
  }
  size = array_GetSize(instances);
  for(; i < size; i++){
    pos = array_GetElement(instances,i);
    subst_ContextStart(pathidx_GetSubststate(index), pathidx_GetSymbstate(index));
    if (subst_FolTermMatchTermsSubsts(pathidx_GetSubststate(index), pathidx_GetSymbstate(index), pathidx_GetTermstate(index), pathidx_GetFolstate(index), Atom, Types, Subst, index->terms[pos], (TYPING)NULL, (SUBST)NULL) &&
 index->data[pos] <= Pos) {
      index->iter_pos = i;
      index->marked[pos] = 0U;
      return TRUE;
    }
    subst_ContextStop(pathidx_GetSubststate(index));
    index->marked[pos] = 0U;
  }
  return FALSE;
}
unsigned trail_CurrTrailPosForFalseInstanceIterator(TRAIL Trail)
{
#ifdef SPASS_CHECK
  if(!trail_FalseInstancesIteratorIsRunning(Trail)){
    misc_StartUserErrorReport();
    misc_UserErrorReport("%s: there is no running iterator.",__func__);
    misc_FinishUserErrorReport();
  }
#endif
  return (unsigned)pathidx_DataFromCurrentIteratorInstance(Trail->iter);
}
BOOL trail_FalseInstancesIteratorIsRunning(TRAIL Trail)
{
  return Trail->iter != NULL;
}
void trail_StopInstancesIterator(TRAIL Trail)
{
#ifdef SPASS_CHECK
  if(!trail_FalseInstancesIteratorIsRunning(Trail)){
    misc_StartUserErrorReport();
    misc_UserErrorReport("%s: there is no running iterator.",__func__);
    misc_FinishUserErrorReport();
  }
#endif
  pathidx_StopInstancesIterator(Trail->iter);
  Trail->iter = NULL;
}
BOOL trail_LitIsPositive(TRAIL Trail, TERM Lit)
{
  return !symbol_Equal(term_TopSymbol(Lit),fol_Not(Trail->folstate));
}
TRAILDATA trail_GLitData(TRAIL Trail, TERM Lit)
{
  TERM atom;
  if(symbol_Equal(term_TopSymbol(Lit),fol_Not(Trail->folstate))){
    atom = term_FirstArgument(Lit);
  }else{
    atom = Lit;
  }
  return trail_GAtomData(Trail,atom);
}
TRAILDATA trail_GAtomData(TRAIL Trail, TERM Atom)
{
  unsigned pos;
  if(sharing_SharedIdx(Atom) >= Trail->mapcap){
    return NULL;
  }
  pos = Trail->sharedmap[sharing_SharedIdx(Atom)];
  if(pos == UINT_MAX){
    return NULL;
  }
  return trail_GetDataFromTrailPos(Trail,pos);
}
BOOL trail_IsGLitSet(TRAIL Trail, TERM Lit)
{
  return trail_GLitData(Trail,Lit) != NULL;
}
VAR_STATUS trail_LitStatusFromGLit(TRAIL Trail, TERM Lit)
{
  TRAILDATA data;
  data = trail_GLitData(Trail,Lit);
  if(data == NULL){
    return NOT_SET;
  }
  if(trail_LitIsPositive(Trail,Lit)){
    return trail_IsPositiveFromData(Trail,data) ? SET_TRUE : SET_FALSE;
  }else{
    return trail_IsPositiveFromData(Trail,data) ? SET_FALSE : SET_TRUE;
  }
}
VAR_STATUS trail_LitStatusFromGAtom(TRAIL Trail, TERM Atom, BOOL Positive)
{
  TRAILDATA data;
  data = trail_GAtomData(Trail,Atom);
  if(data == NULL){
    return NOT_SET;
  }
  if(Positive){
    return trail_IsPositiveFromData(Trail,data) ? SET_TRUE : SET_FALSE;
  }else{
    return trail_IsPositiveFromData(Trail,data) ? SET_FALSE : SET_TRUE;
  }
}
CLAUSE trail_GLitReason(TRAIL Trail, TERM Lit)
{
  TRAILDATA data;
  data = trail_GLitData(Trail,Lit);
  if(data == NULL){
    return NULL;
  }else{
    return trail_LitReasonFromData(Trail,data);
  }
}
unsigned trail_GLitDl(TRAIL Trail, TERM Lit)
{
  TRAILDATA data;
  data = trail_GLitData(Trail,Lit);
  if(data == NULL){
    return UINT_MAX;
  }else{
    return trail_LitDlFromData(Trail,data);
  }
}
TRAILDATA trail_Pop(TRAIL Trail)
{
  TRAILDATA data;
  if(Trail->tsize == 0){
    return NULL;
  }
  Trail->tsize--;
  data = Trail->trail + Trail->tsize;
  if(trail_IsPositiveFromData(Trail,data)){
    pathidx_RemoveByPosition(Trail->posidx,data->ipos);
  }else{
    pathidx_RemoveByPosition(Trail->negidx,data->ipos);
  }
  data->ipos = UINT_MAX;
  if (data->subst) {
    subst_DeleteWithTerms(trail_GetTermstate(Trail), trail_GetMemory(Trail), data->subst);
    data->subst = (SUBST)NULL;
  }
  Trail->sharedmap[sharing_SharedIdx(data->atom)] = UINT_MAX;
  return data;
}
void trail_MakeBacktrack(TRAIL Trail)
{
  int i;
  for (i = trail_GetSize(Trail) - trail_GetNumberLitUpToCurrDl(Trail); i != 0; i--) {
    trail_Pop(Trail);
  }
  trail_CStackAdjustSize(Trail);
}
void trail_IsConsistent(TRAIL Trail)
{
  unsigned i, j, pos;
  TRAILDATA data, pdata;
  CLAUSE clause;
  SUBST subst;
  for (i=0; i < trail_GetSize(Trail); i++) {
    data = trail_GetDataFromTrailPos(Trail, i);
    if (!trail_IsDecisionFromTrailPos(Trail, i)) {
      clause = trail_LitReasonFromData(Trail, data);
      subst = trail_LitSubstFromData(Trail, data);
      pos = trail_ClausePositionFromData(Trail, data);
      if (i == 0) {
 if (clause_GetFolLiterals(clause) > 1 && !subst_TermEqualModuloSubsts(trail_GetSymbstate(Trail), clause_GetAtom(clause,0), subst, clause_GetAtom(clause,1), subst)) {
   misc_StartErrorReport();
   misc_ErrorReport("\n In %s:", __func__);
   misc_ErrorReport("\n Non-Unit clause propagating at level zero.");
   printf("\n "); clause_Print(trail_GetTermstate(Trail), clause);
   printf("\n");
   misc_FinishErrorReport();
 }
      }
      else {
 for (j=0; j < clause_GetFolLiterals(clause); j++) {
   if (j == pos || subst_TermEqualModuloSubsts(trail_GetSymbstate(Trail), clause_GetAtom(clause,j), subst, clause_GetAtom(clause,pos), subst)) {
     if (trail_LitStatusFromAtomPos(Trail, clause_GetAtom(clause,j), subst, (j<clause_GetPremiseLiterals(clause) ? FALSE : TRUE), i-1) != NOT_SET) {
       misc_StartErrorReport();
       misc_ErrorReport("\n In %s:", __func__);
       misc_ErrorReport("\n Propagating literal already set.");
       printf("\n "); clause_Print(trail_GetTermstate(Trail), clause);
       printf("\n");
       misc_FinishErrorReport();
     }
   }
   else {
     if (trail_LitStatusFromAtomPos(Trail, clause_GetAtom(clause,j), subst, (j<clause_GetPremiseLiterals(clause) ? FALSE : TRUE), i-1) != SET_FALSE) {
       misc_StartErrorReport();
       misc_ErrorReport("\n In %s:", __func__);
       misc_ErrorReport("\n Not all Literals of propagating clause false. Propagating %u Not False %u Trail Pos %u", pos, j, i);
       printf("\n"); trail_Print(Trail);
       printf("\n\n "); clause_Print(trail_GetTermstate(Trail), clause); printf(" "); subst_Print(trail_GetTermstate(Trail),trail_GetSymbstate(Trail),subst);
       printf("\n");
       misc_FinishErrorReport();
     }
   }
 }
      }
    }
    for (j=0; j < i; j++) {
      pdata = trail_GetDataFromTrailPos(Trail, j);
      if (trail_AtomFromData(Trail, pdata) == trail_AtomFromData(Trail, data) && trail_IsPositiveFromData(Trail, pdata) != trail_IsPositiveFromData(Trail, data)) {
 misc_StartErrorReport();
 misc_ErrorReport("\n In %s:", __func__);
 misc_ErrorReport("\n Complemenatry literals at %u and %u:",i,j); term_PrintPrefix(trail_GetTermstate(Trail), trail_AtomFromData(Trail, pdata));
 printf("\n");
 misc_FinishErrorReport();
      }
    }
  }
}
void trail_ClausePrintSubst(TRAIL Trail, TERMSTATE Termstate, CLAUSE Clause, SUBST Subst)
{
  unsigned i;
  ARRAY clits;
  VAR_STATUS status;
  printf("%u:%u:%u:", clause_GetId(Clause), clause_GetFolLiterals(Clause),clause_GetVariables(Clause));
  if (clause_GetParents(Clause)) {
    printf("[");
    clits = (ARRAY)clause_GetParents(Clause);
    i = 0;
    do {
      printf("%ld.%ld", clits[i], clits[i+1]);
      i = i + 2;
      if (i<array_GetSize(clits))
 printf(",");
    } while (i<array_GetSize(clits));
    printf("]:");
  }
  else
    printf("INP:");
  for (i=0;i<clause_GetVariables(Clause);i++) {
    symbol_PrintVarTypingType(term_GetSymbstate(Termstate), clause_GetVarType(Clause,i));
  }
  printf(": ");
  clits = (ARRAY)clause_GetConstraintLiterals(Clause);
  if (clits != NULL) {
    for (i=0; i < array_GetSize(clits);) {
      subst_TermPrintPrefix(Termstate, (TERM)clits[i], Subst);
      i++;
      if (i < array_GetSize(clits))
 printf(",");
    }
    i = 0;
    printf(" || ");
  }
  for (i=0; i < clause_GetPremiseLiterals(Clause);) {
    subst_TermPrintPrefix(Termstate, (TERM)Clause[i], Subst);
    status = trail_LitStatusFromAtom(Trail, (TERM)Clause[i], Subst, FALSE);
    if (status == SET_TRUE)
      printf(":T:%u", trail_AtomTrailPositionFromData(Trail, trail_AtomData(Trail, (TERM)Clause[i], Subst)));
    else {
      if (status == SET_FALSE)
 printf(":F:%u", trail_AtomTrailPositionFromData(Trail, trail_AtomData(Trail, (TERM)Clause[i], Subst)));
      else
 printf(":U");
    }
    i++;
    if (i < clause_GetPremiseLiterals(Clause))
      printf(",");
  }
  printf(" -> ");
  for (; i < clause_GetFolLiterals(Clause);) {
    subst_TermPrintPrefix(Termstate, (TERM)Clause[i], Subst);status = trail_LitStatusFromAtom(Trail, (TERM)Clause[i], Subst, TRUE);
    if (status == SET_TRUE)
      printf(":T:%u", trail_AtomTrailPositionFromData(Trail, trail_AtomData(Trail, (TERM)Clause[i], Subst)));
    else {
      if (status == SET_FALSE)
 printf(":F:%u", trail_AtomTrailPositionFromData(Trail, trail_AtomData(Trail, (TERM)Clause[i], Subst)));
      else
 printf(":U");
    }
    i++;
    if (i < clause_GetFolLiterals(Clause))
      printf(",");
  }
}
