#ifdef DOC_BUILD_INTERNAL
#endif
#include "inst.h"
void inst_DestrPrint(TERMSTATE Termstate, INST_QUERY Data)
{
  printf("\nDate Query: First: %c ", (inst_GetQueryFirstWatched(Data) ? 't' : 'f'));
  inst_Print(Termstate, inst_GetQueryInst(Data));
}
INST inst_Create(MEMORY Mem, CLAUSE Clause, unsigned FirstW, unsigned SecondW, SUBST Subst, BOOL EmptyTrail)
{
#ifdef SPASS_CHECK
  if (FirstW >= clause_GetFolLiterals(Clause)) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Index of first literal to watch is larger than largest index in clause.", __func__);
    misc_FinishErrorReport();
  }
  if (SecondW >= clause_GetFolLiterals(Clause)) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Index of second literal to watch is larger than largest index in clause.", __func__);
    misc_FinishErrorReport();
  }
#endif
  INST new;
  new = (INST)memory_Malloc(Mem, sizeof(struct INST_HELP));
  new->origin = Clause;
  new->watched1 = FirstW;
  new->watched2 = SecondW;
  new->subst = Subst;
  new->emptyTrail = EmptyTrail || (Subst == NULL);
  new->firstQuery = inst_QueryCreate(Mem, new, TRUE);
  new->secondQuery = inst_QueryCreate(Mem, new, FALSE);
  return new;
}
INST inst_CreateEmpty(MEMORY Mem)
{
  INST new;
  new = (INST)memory_Malloc(Mem, sizeof(struct INST_HELP));
  new->origin = (CLAUSE)NULL;
  new->watched1 = 0;
  new->watched2 = 0;
  new->subst = (SUBST)NULL;
  new->emptyTrail = TRUE;
  new->firstQuery = (INST_QUERY)NULL;
  new->secondQuery = (INST_QUERY)NULL;
  return new;
}
INST inst_CreateFromInstAndSubst(MEMORY Mem, SHARER Sharer, TERMSTATE Termstate, INST Inst, SUBST Subst, BOOL EmptyTrail)
{
#ifdef SPASS_CHECK
  if (inst_GetSubst(Inst) == NULL && subst_NumberOfVariables(Subst) != clause_GetVariables(inst_GetOrigin(Inst))) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Given substitution should be as large as the number of variables.", __func__);
    misc_FinishErrorReport();
  }
#endif
  INST new;
  SUBST new_subst;
  if (inst_GetSubst(Inst) == NULL)
    new_subst = Subst;
  else
    new_subst = subst_ApplyToSubst(Termstate, inst_GetSubst(Inst), Subst, TRUE);
  new = inst_Create(Mem, inst_GetOrigin(Inst), inst_GetFirstWatched(Inst), inst_GetSecondWatched(Inst), new_subst, EmptyTrail);
  inst_ShareSubstOfNew(Sharer, new);
  return new;
}
INST inst_CreateFromInstAndContext(MEMORY Mem, SHARER Sharer, TERMSTATE Termstate, INST Inst, SUBSTSTATE Subststate, BOOL EmptyTrail, BOOL Matching)
{
  INST new;
  SUBST new_subst;
  if (inst_GetSubst(Inst) == NULL) {
    if (Matching)
      new_subst = subst_ContextExtractSubstShallow(Subststate, Termstate, term_GetSymbstate(Termstate), (SYMBOL)clause_GetVariables(inst_GetOrigin(Inst)));
    else
      new_subst = subst_ContextFlattenExtractSubst(Subststate, Termstate, term_GetSymbstate(Termstate), (SYMBOL)clause_GetVariables(inst_GetOrigin(Inst)));
  }
  else
    new_subst = subst_ContextFlattenApplyToSubst(Subststate, Termstate, inst_GetSubst(Inst), TRUE);
  new = inst_Create(Mem, inst_GetOrigin(Inst), inst_GetFirstWatched(Inst), inst_GetSecondWatched(Inst), new_subst, EmptyTrail);
  inst_ShareSubstOfNew(Sharer, new);
  return new;
}
void inst_ShareSubstOfNew(SHARER Sharer, INST Inst)
{
#ifdef SPASS_CHECK
  if (inst_GetSubst(Inst) == NULL) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Substitution of Inst is NULL", __func__);
    misc_FinishErrorReport();
  }
#endif
  unsigned it;
  SUBST subst;
  subst = inst_GetSubst(Inst);
  for (it = 0; it < subst_NumberOfVariables(subst); it++)
    if (subst_VarDefined(subst, (SYMBOL)it) && !sharing_IsShared(subst_GetValue(subst, (SYMBOL)it)))
      subst_SetValue(subst, (SYMBOL)it, sharing_GetSharedTerm(Sharer, subst_GetValue(subst, (SYMBOL)it)));
}
void inst_Delete(MEMORY Mem, INST Inst)
{
  if (inst_GetSubst(Inst) != NULL)
    subst_Delete(Mem, inst_GetSubst(Inst));
  if (inst_GetFirstQuery(Inst))
    inst_QueryDelete(Mem, inst_GetFirstQuery(Inst));
  if (inst_GetSecondQuery(Inst))
    inst_QueryDelete(Mem, inst_GetSecondQuery(Inst));
  memory_Free(Mem, Inst, sizeof(struct INST_HELP));
}
BOOL inst_Equal(SYMBSTATE Symbstate, INST Inst1, INST Inst2)
{
  CLAUSE clause;
  MEMORY mem;
  if (inst_GetOrigin(Inst1) != inst_GetOrigin(Inst2))
    return FALSE;
  mem = symbol_GetStateMemory(Symbstate);
  clause = inst_GetOrigin(Inst1);
  if (clause_EqualModuloSubst(mem, Symbstate, clause, inst_GetSubst(Inst1), inst_GetSubst(Inst2)))
    return TRUE;
  return FALSE;
}
unsigned inst_GetFirstWatched(INST Inst)
{
  return Inst->watched1;
}
void inst_SetFirstWatched(INST Inst, unsigned NewWatched)
{
#ifdef SPASS_CHECK
  if (NewWatched >= clause_GetFolLiterals(inst_GetOrigin(Inst))) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Index of new literal to watch is larger than largest index in clause.", __func__);
    misc_FinishErrorReport();
  }
#endif
  Inst->watched1 = NewWatched;
}
void inst_SetFirstWatchedTheoryConflict(INST Inst, unsigned NewWatched)
{
  Inst->watched1 = NewWatched;
}
unsigned inst_GetSecondWatched(INST Inst)
{
  return Inst->watched2;
}
void inst_SetSecondWatched(INST Inst, unsigned NewWatched)
{
#ifdef SPASS_CHECK
  if (NewWatched >= clause_GetFolLiterals(inst_GetOrigin(Inst))) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Index of new literal to watch is larger than largest index in clause.", __func__);
    misc_FinishErrorReport();
  }
#endif
  Inst->watched2 = NewWatched;
}
CLAUSE inst_GetOrigin(INST Inst)
{
  return Inst->origin;
}
void inst_SetOrigin(INST Inst, CLAUSE Clause)
{
  Inst->origin = Clause;
}
SUBST inst_GetSubst(INST Inst)
{
  return Inst->subst;
}
BOOL inst_GetEmptyTrail(INST Inst)
{
  return Inst->emptyTrail;
}
INST_QUERY inst_GetFirstQuery(INST Inst)
{
  return Inst->firstQuery;
}
INST_QUERY inst_GetSecondQuery(INST Inst)
{
  return Inst->secondQuery;
}
void inst_Print(TERMSTATE Termstate, INST Inst)
{
  clause_Print(Termstate, inst_GetOrigin(Inst));
  if (inst_GetSubst(Inst) != NULL) {
    printf(" ");
    subst_Print(Termstate, term_GetSymbstate(Termstate), inst_GetSubst(Inst));
  }
  else
    printf(" S:NULL");
}
void inst_InitiallyPickWatched(INST Inst, TERMSTATE Termstate, TRAIL Trail, unsigned Pos)
{
#ifdef SPASS_CHECK
  if (inst_GetSubst(Inst) != NULL) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Substitution of the given instance is not NULL.", __func__);
    misc_FinishErrorReport();
  }
#endif
  unsigned it, clause_size;
  int w1, w2, w1falsepos, w2falsepos;
  CLAUSE clause;
  TRAILDATA t_data;
  clause = inst_GetOrigin(Inst);
  clause_size = clause_GetFolLiterals(clause);
  w1 = -1;
  w2 = -1;
  if (clause_GetFolLiterals(clause) == 1) {
    inst_SetFirstWatched(Inst, 0);
    inst_SetSecondWatched(Inst, 0);
    return;
  }
  w1falsepos = -1;
  w2falsepos = -1;
  for (it = 0; it < clause_size; it++) {
    if (!term_IsGround(Termstate, clause_GetAtom(clause, it))) {
      if (w1 == -1 || (w1falsepos > -1 && w2 != -1)) {
        w1 = it;
 w1falsepos = -1;
        continue;
      } else {
 if (w2 == -1 || w2falsepos > -1) {
   w2falsepos = -1;
   w2 = it;
 }
      }
    }
    t_data = trail_AtomDataPos(Trail, clause_GetAtom(clause, it), NULL, Pos);
    if (t_data == NULL) {
      if (w1 == -1) {
        w1 = it;
      } else {
 if (w2 == -1)
   w2 = it;
      }
    }
    else {
      if ((int)trail_AtomTrailPositionFromData(Trail, t_data) > w1falsepos && w1 == -1) {
 w1falsepos = trail_AtomTrailPositionFromData(Trail, t_data);
 w1 = it;
      }
      else {if ((int)trail_AtomTrailPositionFromData(Trail, t_data) > w2falsepos && w2 == -1) {
   w2falsepos = trail_AtomTrailPositionFromData(Trail, t_data);
   w2 = it;
 }
      }
    }
  }
  inst_SetFirstWatched(Inst, w1);
  inst_SetSecondWatched(Inst, w2);
}
void inst_NewCreatedPickNewWatched(INST Inst, TERMSTATE Termstate, unsigned FalseWatched, TRAIL Trail, unsigned Pos)
{
#ifdef SPASS_CHECK
  if (FalseWatched > 2) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: FalseWatched is not 0, 1 or 2.", __func__);
    misc_FinishErrorReport();
  }
  if (inst_GetSubst(Inst) == NULL) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Substitution is empty.", __func__);
    misc_FinishErrorReport();
  }
#endif
  unsigned it, w1, w2, to_watch, other_watched, clause_size;
  int unassigned, last_false_pos_on_trail, last_false_trail_position;
  CLAUSE clause;
  SUBST subst;
  TRAILDATA t_data;
  BOOL found_true, loop_once;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  clause_size = clause_GetFolLiterals(clause);
  if (clause_GetFolLiterals(clause) == 1)
    return;
  unassigned = -1;
  last_false_trail_position = -1;
  found_true = FALSE;
  if (FalseWatched == 1) {
    to_watch = w1;
    other_watched = w2;
  } else {
    to_watch = w2;
    other_watched = w1;
  }
  last_false_trail_position = to_watch;
  for (it = (to_watch + 1) % clause_size; it != to_watch; it = (it + 1) % clause_size) {
    if (it == other_watched ||
        (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, other_watched) &&
         subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), clause_GetAtom(clause, it), subst, clause_GetAtom(clause, other_watched), subst)))
      continue;
    if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, it), subst)) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    t_data = trail_AtomDataPos(Trail, clause_GetAtom(clause, it), subst, Pos);
    if (t_data == NULL) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    if (trail_IsPositiveFromData(Trail, t_data) == clause_LiteralIsPositive(clause, it)) {
      to_watch = it;
      found_true = TRUE;
      break;
    }
    if (trail_AtomTrailPositionFromData(Trail, t_data) == (Pos == trail_GetSize(Trail) ? Pos-1 : Pos))
      last_false_trail_position = it;
  }
  if (!found_true) {
    if (unassigned != -1)
      to_watch = unassigned;
    else
      to_watch = last_false_trail_position;
  }
  other_watched = to_watch;
  if (FalseWatched == 1) {
    w1 = to_watch;
    to_watch = w2;
  } else {
    w2 = to_watch;
    to_watch = w1;
  }
  if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, to_watch), subst) ||
       trail_LitStatusFromAtomPos(Trail, clause_GetAtom(clause, to_watch), subst, clause_LiteralIsPositive(clause, to_watch), Pos) != SET_FALSE) {
    inst_SetFirstWatched(Inst, w1);
    inst_SetSecondWatched(Inst, w2);
    return;
  }
  unassigned = -1;
  last_false_trail_position = -1;
  last_false_pos_on_trail = -1;
  found_true = FALSE;
  loop_once = FALSE;
  for (it = to_watch; (it != to_watch) || !loop_once; it = (it + 1) % clause_size) {
    loop_once = TRUE;
    if (it == other_watched ||
        (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, other_watched) &&
         subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), clause_GetAtom(clause, it), subst, clause_GetAtom(clause, other_watched), subst)))
      continue;
    if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, it), subst)) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    t_data = trail_AtomDataPos(Trail, clause_GetAtom(clause, it), subst, Pos);
    if (t_data == NULL) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    if (trail_IsPositiveFromData(Trail, t_data) == clause_LiteralIsPositive(clause, it)) {
      to_watch = it;
      found_true = TRUE;
      break;
    }
    if ((int)trail_AtomTrailPositionFromData(Trail, t_data) > last_false_pos_on_trail) {
      last_false_trail_position = it;
      last_false_pos_on_trail = trail_AtomTrailPositionFromData(Trail, t_data);
    }
  }
  if (!found_true) {
    if (unassigned != -1)
      to_watch = unassigned;
    else if (last_false_trail_position != -1)
      to_watch = last_false_trail_position;
  }
  if (FalseWatched == 1)
    w2 = to_watch;
  else
    w1 = to_watch;
  inst_SetFirstWatched(Inst, w1);
  inst_SetSecondWatched(Inst, w2);
}
void inst_CheckPotentialFalseLiteralsOfNewInst(INST Inst, TERMSTATE Termstate, TRAIL Trail, unsigned Pos)
{
  unsigned it, w1, w2, nw1, nw2, clause_size;
  unsigned lfp1, lfp2;
  CLAUSE clause;
  SUBST subst;
  TRAILDATA data1, data2, datal;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  nw1 = w1;
  nw2 = w2;
  clause_size = clause_GetFolLiterals(clause);
  if (clause_GetFolLiterals(clause) == 1 || !subst_TermIsGrounded(Termstate, clause_GetAtom(clause, w1), subst) || !subst_TermIsGrounded(Termstate, clause_GetAtom(clause, w2), subst))
    return;
  data1 = trail_AtomDataPos(Trail, clause_GetAtom(clause, w1), subst, Pos);
  data2 = trail_AtomDataPos(Trail, clause_GetAtom(clause, w2), subst, Pos);
  if (data1 == NULL || data2 == NULL)
    return;
  else {
    lfp1 = trail_AtomTrailPositionFromData(Trail,data1);
    lfp2 = trail_AtomTrailPositionFromData(Trail,data2);
    if (trail_IsPositiveFromData(Trail, data1) == clause_LiteralIsPositive(clause, w1) || trail_IsPositiveFromData(Trail, data2) == clause_LiteralIsPositive(clause, w2))
      return;
  }
  for (it = 0; it < clause_size; it++) {
    if (it == w1 || it == w2)
      continue;
#ifdef SPASS_CHECK
    if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, it), subst)) {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: Both watched literals false but %u not grounded.\n ", __func__,it);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_FinishErrorReport();
    }
#endif
    datal = trail_AtomDataPos(Trail, clause_GetAtom(clause, it), subst, Pos);
#ifdef SPASS_CHECK
    if (datal == NULL) {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: Both watched literals false but %u undefined.\n ", __func__,it);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_FinishErrorReport();
    }
    if (trail_IsPositiveFromData(Trail, datal) == clause_LiteralIsPositive(clause, it)) {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: Both watched literals false but %u true.\n ", __func__,it);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_FinishErrorReport();
    }
#endif
    if (lfp1 > lfp2) {
      if (trail_AtomTrailPositionFromData(Trail, datal) > lfp2 && !subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), clause_GetAtom(clause, nw1),subst,clause_GetAtom(clause, it), subst)) {
 nw2 = it;
 lfp2 = trail_AtomTrailPositionFromData(Trail, datal);
      }
    }
    else {
      if (trail_AtomTrailPositionFromData(Trail, datal) > lfp1 && !subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), clause_GetAtom(clause, nw2),subst,clause_GetAtom(clause, it), subst)) {
 nw1 = it;
 lfp1 = trail_AtomTrailPositionFromData(Trail, datal);
      }
    }
  }
  inst_SetFirstWatched(Inst, nw1);
  inst_SetSecondWatched(Inst, nw2);
}
BOOL inst_PickNewWatched(INST Inst, TERMSTATE Termstate, BOOL FirstFalse, TRAIL Trail)
{
  unsigned it, w1, w2, other_watched, clause_size;
  int unassigned, to_watch;
  CLAUSE clause;
  SUBST subst;
  TRAILDATA t_data;
  BOOL found_true;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  to_watch = -1;
  if (clause_GetFolLiterals(clause) <= 2)
    return FALSE;
  unassigned = -1;
  found_true = FALSE;
  if (FirstFalse) {
    to_watch = w1;
    other_watched = w2;
  } else {
    to_watch = w2;
    other_watched = w1;
  }
#ifdef SPASS_CHECK
  if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, to_watch), subst)) {
    misc_StartErrorReport();
    if (FirstFalse)
      misc_ErrorReport("\n In %s: First watched literal that should be picked new is not ground in the instance\n ", __func__);
    else
      misc_ErrorReport("\n In %s: Second watched literal that should be picked new is not ground in the instance\n ", __func__);
    inst_Print(Termstate, Inst);
    printf("\n");
    misc_FinishErrorReport();
  }
  if (trail_LitStatusFromAtom(Trail, clause_GetAtom(clause, to_watch), subst, clause_LiteralIsPositive(clause, to_watch)) != SET_FALSE) {
    misc_StartErrorReport();
    if (FirstFalse)
      misc_ErrorReport("\n In %s: First watched literal that should be picked new is not false in the instance\n ", __func__);
    else
      misc_ErrorReport("\n In %s: Second watched literal that should be picked new is not false in the instance\n ", __func__);
    inst_Print(Termstate, Inst);
    printf("\n Trail: ");
    trail_Print(Trail);
    printf("\n");
    misc_FinishErrorReport();
  }
#endif
  clause_size = clause_GetFolLiterals(clause);
  for (it = (to_watch + 1) % clause_size; (int)it != to_watch; it = (it + 1) % clause_size) {
    if (it == other_watched ||
        (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, other_watched) &&
         subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), clause_GetAtom(clause, it), subst, clause_GetAtom(clause, other_watched), subst)))
      continue;
    if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, it), subst)) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    t_data = trail_AtomData(Trail, clause_GetAtom(clause, it), subst);
    if (t_data == NULL) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    if (trail_IsPositiveFromData(Trail, t_data) == clause_LiteralIsPositive(clause, it)) {
      to_watch = it;
      found_true = TRUE;
      break;
    }
  }
  if (!found_true) {
    if (unassigned != -1)
      to_watch = unassigned;
    else
      return FALSE;
  }
  if (FirstFalse)
    inst_SetFirstWatched(Inst, to_watch);
  else
    inst_SetSecondWatched(Inst, to_watch);
  return TRUE;
}
BOOL inst_PickNewWatchedPos(INST Inst, TERMSTATE Termstate, BOOL FirstFalse, TRAIL Trail, unsigned Pos)
{
  unsigned it, w1, w2, other_watched, clause_size;
  int unassigned, to_watch;
  CLAUSE clause;
  SUBST subst;
  TRAILDATA t_data;
  BOOL found_true;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  to_watch = -1;
#ifdef SPASS_CHECK
  if (clause_GetFolLiterals(clause) >1 && w1 == w2) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Identical Watched Literals \n ", __func__);
    inst_Print(Termstate, Inst);
    misc_FinishErrorReport();
  }
#endif
  if (clause_GetFolLiterals(clause) <= 2)
    return FALSE;
  unassigned = -1;
  found_true = FALSE;
  if (FirstFalse) {
    to_watch = w1;
    other_watched = w2;
  } else {
    to_watch = w2;
    other_watched = w1;
  }
#ifdef SPASS_CHECK
  if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, to_watch), subst)) {
    misc_StartErrorReport();
    if (FirstFalse)
      misc_ErrorReport("\n In %s: First watched literal that should be picked new is not ground in the instance\n ", __func__);
    else
      misc_ErrorReport("\n In %s: Second watched literal that should be picked new is not ground in the instance\n ", __func__);
    inst_Print(Termstate, Inst);
    printf("\n");
    misc_FinishErrorReport();
  }
  if (trail_LitStatusFromAtomPos(Trail, clause_GetAtom(clause, to_watch), subst, clause_LiteralIsPositive(clause, to_watch), Pos) != SET_FALSE) {
    misc_StartErrorReport();
    if (FirstFalse)
      misc_ErrorReport("\n In %s: First watched literal that should be picked new is not false in the instance\n ", __func__);
    else
      misc_ErrorReport("\n In %s: Second watched literal that should be picked new is not false in the instance\n ", __func__);
    inst_Print(Termstate, Inst);
    printf("\n Trail: ");
    trail_Print(Trail);
    printf("\n");
    misc_FinishErrorReport();
  }
#endif
  clause_size = clause_GetFolLiterals(clause);
  for (it = (to_watch + 1) % clause_size; (int)it != to_watch; it = (it + 1) % clause_size) {
    if (it == other_watched ||
        (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, other_watched) &&
         subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), clause_GetAtom(clause, it), subst, clause_GetAtom(clause, other_watched), subst)))
      continue;
    if (!subst_TermIsGrounded(Termstate, clause_GetAtom(clause, it), subst)) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    t_data = trail_AtomDataPos(Trail, clause_GetAtom(clause, it), subst, Pos);
    if (t_data == NULL) {
      if (unassigned == -1)
        unassigned = it;
      continue;
    }
    if (trail_IsPositiveFromData(Trail, t_data) == clause_LiteralIsPositive(clause, it)) {
      to_watch = it;
      found_true = TRUE;
      break;
    }
  }
  if (!found_true) {
    if (unassigned != -1)
      to_watch = unassigned;
    else
      return FALSE;
  }
  if (FirstFalse)
    inst_SetFirstWatched(Inst, to_watch);
  else
    inst_SetSecondWatched(Inst, to_watch);
  return TRUE;
}
void inst_CheckForDifferentWatched(INST Inst, TERMSTATE Termstate)
{
  unsigned it, w1, w2;
  CLAUSE clause;
  SUBST subst;
  TERM atom1, atom2;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  if (clause_GetFolLiterals(clause) <= 1)
    return;
  if (subst == NULL) {
    if (w1 != w2)
      return;
    else {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: Instance \n ", __func__);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_ErrorReport(" has equal watched literals.");
      misc_FinishErrorReport();
    }
  }
  if (clause_LiteralIsPositive(clause, w1) != clause_LiteralIsPositive(clause, w2))
    return;
  atom1 = clause_GetAtom(clause, w1);
  atom2 = clause_GetAtom(clause, w2);
  if (!subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), atom1, subst, atom2, subst))
    return;
  if (clause_GetPremiseLiterals(clause) != 0 && clause_GetConclusionLiterals(clause) != 0) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Instance \n ", __func__);
    inst_Print(Termstate, Inst);
    printf("\n");
    misc_ErrorReport(" has equal watched literals in a non-unit instance.");
    misc_FinishErrorReport();
  }
  for (it = 0; it < clause_GetFolLiterals(clause); it++) {
    if (!subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), atom1, subst, clause_GetAtom(clause, it), subst)) {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: Instance \n ", __func__);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_ErrorReport(" has equal watched literals in a non-unit instance.");
      misc_FinishErrorReport();
    }
  }
}
BOOL inst_CheckAllNotWatchedFalse(INST Inst, TERMSTATE Termstate, TRAIL Trail, unsigned Pos)
{
  unsigned it, w1, w2;
  CLAUSE clause;
  SUBST subst;
  TERM atom1, atom2, atom;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  atom1 = clause_GetAtom(clause, w1);
  atom2 = clause_GetAtom(clause, w2);
  for (it = 0; it < clause_GetFolLiterals(clause); it++) {
    if (it == w1 || it == w2)
      continue;
    atom = clause_GetAtom(clause, it);
    if (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, w1) && subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), atom, subst, atom1, subst))
      continue;
    if (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, w2) && subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), atom, subst, atom2, subst))
      continue;
    if (!subst_TermIsGrounded(Termstate, atom, subst))
      return FALSE;
    if (trail_LitStatusFromAtomPos(Trail, atom, subst, clause_LiteralIsPositive(clause, it), Pos) != SET_FALSE)
      return FALSE;
  }
  return TRUE;
}
void inst_PrintLiteralStatus(INST Inst, TERMSTATE Termstate, TRAIL Trail, unsigned Pos)
{
  unsigned it;
  TERM atom;
  CLAUSE clause;
  SUBST subst;
  TRAILDATA data;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  for (it = 0; it < clause_GetFolLiterals(clause); it++) {
    atom = clause_GetAtom(clause, it);
    term_PrintPrefix(Termstate, atom);
    if (!subst_TermIsGrounded(Termstate, atom, subst)) {
      printf(":NONGROUND ");
    }
    else {
      data = trail_AtomDataPos(Trail, atom, subst, Pos);
      if (data) {
 printf("%u:", trail_AtomTrailPositionFromData(Trail, data));
 if (trail_IsPositiveFromData(Trail, data) != clause_LiteralIsPositive(clause,it))
   printf("FALSE ");
 else
   printf("TRUE ");
      }
      else
 printf("UNDEFINED ");
    }
  }
}
unsigned inst_LastTrailpositionOfNotWatchedLiteral(INST Inst, TERMSTATE Termstate, TRAIL Trail)
{
  unsigned it, w1, w2, last_pos, cur_pos;
  CLAUSE clause;
  SUBST subst;
  TERM atom1, atom2, atom;
  TRAILDATA t_data;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  atom1 = clause_GetAtom(clause, w1);
  atom2 = clause_GetAtom(clause, w2);
  last_pos = 0;
  for (it = 0; it < clause_GetFolLiterals(clause); it++) {
    if (it == w1 || it == w2)
      continue;
    atom = clause_GetAtom(clause, it);
    if (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, w1) && subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), atom, subst, atom1, subst))
      continue;
    if (clause_LiteralIsPositive(clause, it) == clause_LiteralIsPositive(clause, w2) && subst_TermEqualModuloSubsts(term_GetSymbstate(Termstate), atom, subst, atom2, subst))
      continue;
    t_data = trail_AtomData(Trail, atom, subst);
    cur_pos = trail_AtomTrailPositionFromData(Trail, t_data);
    if (cur_pos > last_pos)
      last_pos = cur_pos;
  }
  return last_pos;
}
void inst_CheckWatchedCorrect(INST Inst, TERMSTATE Termstate, TRAIL Trail, unsigned Pos)
{
  unsigned w1, w2, t_pos1, t_pos2, all_other_false_last_trail_pos;
  CLAUSE clause;
  SUBST subst;
  TERM atom1, atom2;
  BOOL ground1, ground2, value1, value2, is_false1, is_false2, is_true1, is_true2, all_other_false;
  TRAILDATA t_data1, t_data2;
  t_data1 = NULL;
  t_data2 = NULL;
  value1 = TRUE;
  value2 = TRUE;
  t_pos1 = 0;
  t_pos2 = 0;
  all_other_false_last_trail_pos = 0;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  atom1 = clause_GetAtom(clause, w1);
  atom2 = clause_GetAtom(clause, w2);
  ground1 = subst_TermIsGrounded(Termstate, atom1, subst);
  ground2 = subst_TermIsGrounded(Termstate, atom2, subst);
  inst_CheckForDifferentWatched(Inst, Termstate);
  if (ground1) {
    t_data1 = trail_AtomDataPos(Trail, atom1, subst, Pos);
    if (t_data1 != NULL) {
      value1 = (trail_IsPositiveFromData(Trail, t_data1) == clause_LiteralIsPositive(clause, w1));
      t_pos1 = trail_AtomTrailPositionFromData(Trail, t_data1);
    }
  }
  if (ground2) {
    t_data2 = trail_AtomDataPos(Trail, atom2, subst, Pos);
    if (t_data2 != NULL) {
      value2 = (trail_IsPositiveFromData(Trail, t_data2) == clause_LiteralIsPositive(clause, w2));
      t_pos2 = trail_AtomTrailPositionFromData(Trail, t_data2);
    }
  }
  is_false1 = (ground1 && t_data1 != NULL && !value1);
  is_false2 = (ground2 && t_data2 != NULL && !value2);
  is_true1 = (ground1 && t_data1 != NULL && value1);
  is_true2 = (ground2 && t_data2 != NULL && value2);
  if (!is_false1 && !is_false2)
    return;
  all_other_false = inst_CheckAllNotWatchedFalse(Inst, Termstate, Trail, Pos);
  if (is_true1 || is_true2)
    return;
  if (!is_false1 && !is_false2 && !is_true1 && !is_true2)
    return;
  if (!all_other_false && (is_false1 || is_false2)) {
    misc_StartErrorReport();
    printf("\n");
    trail_Print(Trail);
    printf("\nPos: %u ", Pos);
    misc_ErrorReport("\n In %s: Instance \n ", __func__);
    inst_Print(Termstate, Inst);
    printf("\n");
    misc_ErrorReport(" the watched literals are not correct, because there are false literals watched and there is still at least one non-false literal.");
    inst_PrintLiteralStatus(Inst, Termstate, Trail, Pos);
    misc_FinishErrorReport();
  }
  if (all_other_false)
    all_other_false_last_trail_pos = inst_LastTrailpositionOfNotWatchedLiteral(Inst, Termstate, Trail);
  if (is_false1 && is_false2) {
    if (t_pos1 < all_other_false_last_trail_pos || t_pos2 < all_other_false_last_trail_pos) {
      misc_StartErrorReport();
      printf("\n");
      trail_Print(Trail);
      printf("\nPos: %u ", Pos);
      misc_ErrorReport("\n In %s: Instance \n ", __func__);
      inst_Print(Termstate, Inst);
      printf("\n"); trail_ClausePrintSubst(Trail, Termstate, clause, subst);
      printf("\n");
      misc_ErrorReport(" the watched literals are not correct, because there is a non-watched literal false that is made false from a literal earlier on the trail.");
      misc_FinishErrorReport();
    } else
      return;
  }
  if (is_false1) {
    if (!all_other_false) {
      if (value2 && t_pos2 < t_pos1)
        return;
      if (trail_GetSize(Trail) - 1 == t_pos1)
        return;
      else {
        misc_StartErrorReport();
 printf("\n");
 trail_Print(Trail);
 printf("\nPos: %u ", Pos);
        misc_ErrorReport("\n In %s: Instance \n ", __func__);
        inst_Print(Termstate, Inst);
 printf("\n"); trail_ClausePrintSubst(Trail, Termstate, clause, subst);
        printf("\n");
        misc_ErrorReport(" the watched literals are not correct, because the first watched literal is false but there is another non-watched non-false literal that should be watched.");
        misc_FinishErrorReport();
      }
    } else {
      if (t_pos1 >= all_other_false_last_trail_pos)
        return;
      else {
        misc_StartErrorReport();
 printf("\n");
 trail_Print(Trail);
 printf("\nPos: %u ", Pos);
        misc_ErrorReport("\n In %s: Instance \n ", __func__);
        inst_Print(Termstate, Inst);
        printf("\n");
        misc_ErrorReport(" the watched literals are not correct, because the first watched is false but there is a non-watched false that is made false by a literal earlier on the trail.");
        misc_FinishErrorReport();
      }
    }
  }
  if (is_false2) {
    if (!all_other_false) {
      if (value1 && t_pos1 < t_pos2)
        return;
      if ((Pos == trail_GetSize(Trail) ? Pos - 1 : Pos) == t_pos2)
        return;
      else {
        misc_StartErrorReport();
 printf("\n");
 trail_Print(Trail);
 printf("\nPos: %u \n", Pos);
        misc_ErrorReport("\n In %s: Instance \n ", __func__);
        inst_Print(Termstate, Inst);
 printf("\n");
 inst_PrintLiteralStatus(Inst, Termstate, Trail, Pos);
        printf("\n");
        misc_ErrorReport(" the watched literals are not correct, because the second watched literal is false but there is another non-watched non-false literal that should be watched.");
        misc_FinishErrorReport();
      }
    } else {
      if (t_pos2 >= all_other_false_last_trail_pos)
        return;
      else {
        misc_StartErrorReport();
 printf("\n");
 trail_Print(Trail);
 printf("\nPos: %u \n", Pos);
        misc_ErrorReport("\n In %s: Instance \n ", __func__);
        inst_Print(Termstate, Inst);
 printf("\n");
 inst_PrintLiteralStatus(Inst, Termstate, Trail, Pos);
        printf("\n");
        misc_ErrorReport(" the watched literals are not correct, because the second watched is false but there is a non-watched false that is made false by a literal earlier on the trail.");
        misc_FinishErrorReport();
      }
    }
  }
}
void inst_CheckWatchedCorrectAfterExhaustiveApplication(INST Inst, TERMSTATE Termstate, TRAIL Trail)
{
  unsigned w1, w2, t_pos1, t_pos2, all_other_false_last_trail_pos;
  CLAUSE clause;
  SUBST subst;
  TERM atom1, atom2;
  BOOL ground1, ground2, value1, value2, is_false1, is_false2, all_other_false;
  TRAILDATA t_data1, t_data2;
  t_data1 = NULL;
  t_data2 = NULL;
  value1 = TRUE;
  value2 = TRUE;
  t_pos1 = 0;
  t_pos2 = 0;
  all_other_false_last_trail_pos = 0;
  clause = inst_GetOrigin(Inst);
  subst = inst_GetSubst(Inst);
  w1 = inst_GetFirstWatched(Inst);
  w2 = inst_GetSecondWatched(Inst);
  atom1 = clause_GetAtom(clause, w1);
  atom2 = clause_GetAtom(clause, w2);
  ground1 = subst_TermIsGrounded(Termstate, atom1, subst);
  ground2 = subst_TermIsGrounded(Termstate, atom2, subst);
  if (ground1) {
    t_data1 = trail_AtomData(Trail, atom1, subst);
    if (t_data1 != NULL) {
      value1 = (trail_IsPositiveFromData(Trail, t_data1) == clause_LiteralIsPositive(clause, w1));
      t_pos1 = trail_AtomTrailPositionFromData(Trail, t_data1);
    }
  }
  if (ground2) {
    t_data2 = trail_AtomData(Trail, atom2, subst);
    if (t_data2 != NULL) {
      value2 = (trail_IsPositiveFromData(Trail, t_data2) == clause_LiteralIsPositive(clause, w2));
      t_pos2 = trail_AtomTrailPositionFromData(Trail, t_data2);
    }
  }
  is_false1 = (ground1 && t_data1 != NULL && !value1);
  is_false2 = (ground1 && t_data2 != NULL && !value2);
  if (!is_false1 && !is_false2)
    return;
  all_other_false = inst_CheckAllNotWatchedFalse(Inst, Termstate, Trail, trail_GetSize(Trail)-1);
  if (is_false1 && is_false2 && !all_other_false) {
    misc_StartErrorReport();
    misc_ErrorReport("\n In %s: Instance \n ", __func__);
    inst_Print(Termstate, Inst);
    printf("\n");
    misc_ErrorReport(" the watched literals are not correct, because there are two false literals watched and there is still at least one non-false literal.");
    misc_FinishErrorReport();
  }
  if (all_other_false)
    all_other_false_last_trail_pos = inst_LastTrailpositionOfNotWatchedLiteral(Inst, Termstate, Trail);
  if (is_false1 && is_false2) {
    if (t_pos1 < all_other_false_last_trail_pos || t_pos2 < all_other_false_last_trail_pos) {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: Instance \n ", __func__);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_ErrorReport(" the watched literals are not correct, because there is a non-watched literal false that is made false from a literal earlier on the trail.");
      misc_FinishErrorReport();
    } else
      return;
  }
  if (is_false1) {
    if (all_other_false) {
      if (t_pos1 >= all_other_false_last_trail_pos)
        return;
      else {
        misc_StartErrorReport();
        misc_ErrorReport("\n In %s: Instance \n ", __func__);
        inst_Print(Termstate, Inst);
        printf("\n");
        misc_ErrorReport(" the watched literals are not correct, because the first watched is false but there is a non-watched false that is made false by a literal earlier on the trail.");
        misc_FinishErrorReport();
      }
    } else {
      if (value2 && t_pos2 < t_pos1)
        return;
      else {
        misc_StartErrorReport();
        misc_ErrorReport("\n In %s: Instance \n ", __func__);
        inst_Print(Termstate, Inst);
        printf("\n");
        misc_ErrorReport(" the watched literals are not correct, because the first watched literal is false but there is another non-watched non-false literal that should be watched.");
        misc_FinishErrorReport();
      }
    }
  }
  if (is_false2) {
    if (all_other_false) {
      if (t_pos2 >= all_other_false_last_trail_pos)
        return;
      else {
        misc_StartErrorReport();
        misc_ErrorReport("\n In %s: Instance \n ", __func__);
        inst_Print(Termstate, Inst);
        printf("\n");
        misc_ErrorReport(" the watched literals are not correct, because the second watched is false but there is a non-watched false that is made false by a literal earlier on the trail.");
        misc_FinishErrorReport();
      }
    } else if (value1 && t_pos1 < t_pos2)
      return;
    else {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: Instance \n ", __func__);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_ErrorReport(" the watched literals are not correct, because the second watched literal is false but there is another non-watched non-false literal that should be watched.");
      misc_FinishErrorReport();
    }
  }
}
void inst_CheckSubstShared(INST Inst, TERMSTATE Termstate)
{
  unsigned it;
  SUBST subst;
  TERM value;
  subst = inst_GetSubst(Inst);
  for (it = 0; it < subst_NumberOfVariables(subst); it++) {
    if (!subst_VarDefined(subst, (SYMBOL)it))
      continue;
    value = subst_GetValue(subst, (SYMBOL)it);
    if (!sharing_IsShared(value)) {
      misc_StartErrorReport();
      misc_ErrorReport("\n In %s: The Substitution of instance \n ", __func__);
      inst_Print(Termstate, Inst);
      printf("\n");
      misc_ErrorReport(" is not (fully) shared.");
      misc_FinishErrorReport();
    }
  }
}
INST_QUERY inst_QueryCreate(MEMORY Mem, INST Inst, BOOL FirstWatched)
{
  INST_QUERY new;
  new = memory_Malloc(Mem, sizeof(struct INST_QUERY_HELP));
  new->inst = Inst;
  new->firstWatched = FirstWatched;
  return new;
}
void inst_QueryDelete(MEMORY Mem, INST_QUERY InstsQuery)
{
  memory_Free(Mem, InstsQuery, sizeof(struct INST_QUERY_HELP));
}
INST inst_GetQueryInst(INST_QUERY Query)
{
  return Query->inst;
}
BOOL inst_GetQueryFirstWatched(INST_QUERY Query)
{
  return Query->firstWatched;
}
unsigned inst_TestSpecialConstantValue(SYMBSTATE Symbstate, SYMBOL Constant)
{
  char * Name;
  Name = symbol_GetName(Symbstate, Constant);
  if (Name[1] == '1') {
    if (string_Length(Name) == 2)
      return 1;
    else
      return 10;
  }
  if (Name[1] == '2')
    return 2;
  if (Name[1] == '3')
    return 3;
  if (Name[1] == '4')
    return 4;
  if (Name[1] == '5')
    return 5;
  if (Name[1] == '6')
    return 6;
  if (Name[1] == '7')
    return 7;
  if (Name[1] == '8')
    return 8;
  if (Name[1] == '9')
    return 9;
  if (Name[1] == '0')
    return 0;
  return 42;
}
void inst_Test(MEMORY Mem, ARRAY Clauses, SHARER Sharer, TERMSTATE Termstate, SUBSTSTATE Subststate, SYMBSTATE Symbstate, FOLSTATE Folstate, SYMBOL Control)
{
  INST old, new;
  CLAUSE origin, clause;
  TERM op, atom;
  int clause_it;
  unsigned it, n, vars;
  SUBST subst;
  TRAIL trail;
  TYPING typing;
  origin = (CLAUSE)array_GetElement(Clauses, 0);
  vars = clause_GetVariables(origin);
  old = NULL;
  new = NULL;
  trail = trail_Create(Mem, Symbstate, Termstate, Folstate, Subststate, Sharer);
  for (clause_it = 1; clause_it < array_GetSize(Clauses); clause_it++) {
    clause = (CLAUSE)array_GetElement(Clauses, clause_it);
    printf("%d:\n", clause_it + 1);
    clause_Print(Termstate, clause);
    printf("\n");
    op = clause_GetAtom(clause,0);
    clause_RemoveLiteral(clause,0);
    n = inst_TestSpecialConstantValue(Symbstate, term_TopSymbol(term_SecondArgument(op)));
    switch (inst_TestSpecialConstantValue(Symbstate, term_TopSymbol(term_FirstArgument(op)))) {
    case 1:
      for (it = 0; it < clause_GetPremiseLiterals(clause); it++)
        trail_Add(trail, clause_GetAtom(clause, it), clause, NULL, it, FALSE, FALSE);
      for (; it < clause_GetFolLiterals(clause); it++)
        trail_Add(trail, clause_GetAtom(clause, it), clause, NULL, it, TRUE, FALSE);
      break;
    case 2:
      for (it = 0; it < n; it++)
        trail_Pop(trail);
      break;
    case 3:
      old = new;
      typing = clause_GetTypes(clause);
      switch (n) {
      case 1:
        subst = subst_Create(Mem, vars);
        for (it = clause_GetPremiseLiterals(clause); it < clause_GetFolLiterals(clause); it++) {
          atom = clause_GetAtom(clause, it);
          subst_SetValue(subst, term_TopSymbol(term_FirstArgument(atom)), term_SecondArgument(atom));
        }
        new = inst_CreateFromInstAndSubst(Mem, Sharer, Termstate, old, subst, trail_GetSize(trail) == 0);
        if (subst != inst_GetSubst(new))
          subst_Delete(Mem, subst);
        inst_NewCreatedPickNewWatched(new, Termstate, 1, trail, trail_GetSize(trail));
        break;
      case 2:
        subst_ContextStart(Subststate, Symbstate);
        for (it = clause_GetPremiseLiterals(clause); it < clause_GetFolLiterals(clause); it++) {
          atom = clause_GetAtom(clause, it);
          if (!subst_FolTermMatchTerms(Subststate, Symbstate, Termstate, Folstate, term_FirstArgument(atom), typing, term_SecondArgument(atom), typing))
            printf("Terms do not match.\n");
        }
        new = inst_CreateFromInstAndContext(Mem, Sharer, Termstate, old, Subststate, trail_GetSize(trail) == 0, TRUE);
        subst_ContextStop(Subststate);
        inst_NewCreatedPickNewWatched(new, Termstate, 1, trail, trail_GetSize(trail));
        break;
      case 3:
        subst_ContextStart(Subststate, Symbstate);
        for (it = clause_GetPremiseLiterals(clause); it < clause_GetFolLiterals(clause); it++) {
          atom = clause_GetAtom(clause, it);
          if (!subst_FolTermUnifyIdenticalVars(Subststate, Folstate, Termstate, Symbstate, typing, term_FirstArgument(atom), term_SecondArgument(atom), TRUE))
            printf("Terms not unifiable.\n");
        }
        new = inst_CreateFromInstAndContext(Mem, Sharer, Termstate, old, Subststate, trail_GetSize(trail) == 0, FALSE);
        subst_ContextStop(Subststate);
        inst_NewCreatedPickNewWatched(new, Termstate, 1, trail, trail_GetSize(trail));
        break;
      }
      inst_Delete(Mem, old);
      break;
    case 4:
      if (new != NULL)
        inst_Delete(Mem, new);
      new = inst_Create(Mem, origin, 1, 1, NULL, trail_GetSize(trail) == 0);
      inst_InitiallyPickWatched(new, Termstate, trail, trail_GetSize(trail));
      break;
    case 5:
      if (!inst_PickNewWatched(new, Termstate, n == 1, trail))
        printf("Found no unassigned literal.\n");
      break;
    }
    trail_Print(trail);
    if (new != NULL) {
      printf("\n");
      inst_Print(Termstate, new);
    }
    printf("\n\n");
  }
  if (new != NULL)
    inst_Delete(Mem, new);
  trail_Delete(trail);
}
