#include "prover.h"

inline Boolean AddFact (TableProver table, Literal newFact)
{
  /*  if (INVALID_LITERAL(newFact))
	 return FALSE; */
  Atom atom = ATOM(newFact);
  
  if (isKnown(table[atom])){
	 if (table[atom] == (TRUE_VALUE(newFact)))
		return TRUE;
	 else{
		table[atom] = CONTRADICTORY;
		return FALSE;
	 }
  } else
	 table[atom] = TRUE_VALUE(newFact);
  return TRUE;
}

inline Boolean RemoveFact(TableProver table, Literal newFact)
{
  /*  if (INVALID_LITERAL(newFact))
	 return FALSE; */
  table[ATOM(newFact)] = DONT_KNOW;
  return TRUE;
}
  
inline Boolean QueryLiteral(TableProver table, Literal query)
{
  Atom atom = ATOM(query);
  switch(table[atom]) {
  case CONTRADICTORY:
      return CONTRADICTORY;
  case DONT_KNOW:
      return DONT_KNOW;
  case TRUE:
      return TRUE_VALUE(query); /* Is true if literal is not negated */
  case FALSE:
      return !(TRUE_VALUE(query)); /* Is true if literal is negated */
  }
  
  return DONT_KNOW;
}

inline int GetTableSize(TableProver table)
{
  return (int) table[0];
}
  
inline Boolean Prove(TableProver table, Formula form)
{
  int index, numberTrue = 0, numberFalse = 0, value;
  
  if (IS_CONJUNCTION(form)){
     for (index = 1; index <= form[0]; index++){
	if ((value = QueryLiteral(table,form[index])) == TRUE)
	  numberTrue++;
	else if (value == FALSE)
	  return FALSE;
     }
     if (numberTrue == form[0])
	return TRUE;
  } else{
     for (index = 1; index <= form[0]; index++){
	if ((value = QueryLiteral(table,form[index])) == TRUE)
	  return TRUE;
	else if (value == FALSE)
	  numberFalse++;
     }
     if (numberFalse == form[0])
	return FALSE;
  }
  return DONT_KNOW;
}
