[mathprog] Some changes

  • From: Yonatan Ben-Yaakov <yontanbn@xxxxxxxxxxxxx>
  • To: mathprog@xxxxxxxxxxxxx
  • Date: Thu, 12 Sep 2002 23:21:44 +0300 (IDT)

1. Eylon yogev has joined our team and currently has nothing to do.

2. I changed a few files (Sentence.h, Sentence.cpp, Makefile). I also
changed main.cpp which is not very imporant, and i added a new file called
PointerComparator.h. They're all attached here.

3. Again I must emphesize that due to the growing size and complexity of
this project, bugs are appearing. Especially in the interface, which i
hastily programmed, and now the == operator i implemented on sentences. I
didn't have time to debug any of these. Maybe this is a good job for the
people without jobs, i.e. Yoel and Eylon. Start debugging my code. What
you can fix, fix. What you can't fix, tell me the bug and i'll try to fix
myself.

- Yonatan.


#include "Interface.h"
#include "Sentence.h"
#include "SentenceTree.h"
using namespace std;

list<Sentence*>* makeList() {
  return new list<Sentence*>();
}
list<Sentence*>* makeList(Sentence* s1) {
  list<Sentence*>* s = makeList();
  s->push_back(s1);
  return s;
}
list<Sentence*>* makeList(Sentence* s1, Sentence* s2) {
  list<Sentence*>* s = makeList(s1);
  s->push_back(s2);
  return s;
}

list<Sentence*>* makeList(Sentence* s1, Sentence* s2, Sentence* s3) {
  list<Sentence*>* s = makeList(s1, s2);
  s->push_back(s3);
  return s;
}

int main() {
  Sentence* s1 = new QuantifierSentence(QuantifierSentence::FORALL, new 
VariableSentence("x"), new OperatorSentence(OperatorSentence::IMPLIES, new 
PredicateSentence("P", new VariableSentence("x")), new PredicateSentence("Q", 
new VariableSentence("x"))));
  Sentence* s2 = new PredicateSentence("Q", new FunctionSentence("f", new 
ConstantSentence("a")), new FunctionSentence("f", new ConstantSentence("b")));
  Sentence* s3 = new EqualitySentence(new FunctionSentence("g", new 
ConstantSentence("a")), new VariableSentence("x"));
  Sentence* s4 = new OperatorSentence(OperatorSentence::AND, new 
PredicateSentence("P", new ConstantSentence("b")), new PredicateSentence("P", 
new ConstantSentence("c")));
  
  SentenceTree *t = new SentenceTree(makeList(s1), new 
SentenceTree(makeList(s3, s4)), new SentenceTree(makeList(s2)));
 
  cout << t->toString() << endl;

  //  Sentence* pavel = new PredicateSentence("P", new VariableSentence("x"), 
new ConstantSentence("a"));
  
  // const list<WorldValueSentence*>* l = 
dynamic_cast<PredicateSentence*>(pavel)->getParameters();

  Sentence* s = Interface::iGetSentence(Interface::getNameTable());
  cout << s->toString() << endl;
  delete s;


  Sentence* s1tag = new QuantifierSentence(QuantifierSentence::FORALL, new 
VariableSentence("x"), new OperatorSentence(OperatorSentence::IMPLIES, new 
PredicateSentence("P", new VariableSentence("x")), new PredicateSentence("Q", 
new VariableSentence("x"))));
  cout << "are they equal?" << (*s1tag == *s1) << (*s1 == *s1tag) << endl;
  cout << "are THEY equal?" << (*s1 == *s4) << (*s3 == *s2) << endl;

  return 0;
}






template<class T> class PointerComparator {
public:
  bool operator() (T* t1, T* t2) {
    if (t1 && t2)
      return (*t1) == (*t2);
    else
      return false;
  }
};
#include "Sentence.h"
#include "PointerComparator.h"
#include <sstream>
using namespace::std;

Sentence::Sentence() {}
Sentence::~Sentence() {}

WorldValueSentence::WorldValueSentence(string s): name(s) {}
const string WorldValueSentence::getName() const {
  return name;
  
}
ConstantSentence::ConstantSentence(string s): WorldValueSentence(s) {}
string ConstantSentence::toString() const {
  return name;
}
bool ConstantSentence::equals(const Sentence* s) const {
  const ConstantSentence* cs;
  if ((cs = dynamic_cast<const ConstantSentence*>(s))) {
    return (cs->name == name);
  }
  else
    return false;
}

VariableSentence::VariableSentence(string s): WorldValueSentence(s) {}
string VariableSentence::toString() const {
  return name;
}

bool VariableSentence::equals(const Sentence* s) const {
  const VariableSentence* vs;
  if ((vs = dynamic_cast<const VariableSentence*>(s))) {
    return (vs->name == name);
  }
  else
    return false;
}
FunctionSentence::FunctionSentence(string s, WorldValueSentence* par):
  WorldValueSentence(s), arity(1) {
  parameters = new list<WorldValueSentence*>();
  parameters->push_back(par);
}

FunctionSentence::FunctionSentence(string s, WorldValueSentence* par1,
                                   WorldValueSentence* par2):
  WorldValueSentence(s), arity(2) {
  parameters = new list<WorldValueSentence*>();
  parameters->push_back(par1);
  parameters->push_back(par2);
}

FunctionSentence::FunctionSentence(string s, list<WorldValueSentence*>* l):
  WorldValueSentence(s), parameters(l) {
  if (l != 0) { // should throw something if it IS!
    arity = l->size();
  }
}

FunctionSentence::~FunctionSentence() {
  for (list<WorldValueSentence*>::iterator it = parameters->begin();
       it != parameters->end(); it++) {
    delete(*it);
  }
  delete parameters;
}

string FunctionSentence::toString() const {
  stringstream s;
  s << name << "_" << arity << "(";
  list<WorldValueSentence*>::const_iterator it = parameters->begin();
  if (it == parameters->end()) {
    s << ")";
    return s.str();
  }
  s << (*it)->toString();
  for (it++; it != parameters->end(); it++) {
    s << ", ";
    s << (*it)->toString();
  }
  s << ")";
  return s.str();
}

int FunctionSentence::getArity() const {
  return arity;
}
const std::list<WorldValueSentence*>* FunctionSentence::getParameters() const {
  return parameters;
}

bool FunctionSentence::equals(const Sentence* s) const {
  const FunctionSentence* fs;
  if ((fs = dynamic_cast<const FunctionSentence*>(s))) {
    if ((fs->name == name) && (fs->arity == arity) &&
        (std::equal(fs->parameters->begin(), fs->parameters->end(),
               parameters->begin(), PointerComparator<Sentence>())))
      return true;
  }
  return false;
}

PredicateSentence::PredicateSentence(string s, WorldValueSentence* par): 
name(s),
                                                                   arity(1) {
  parameters = new list<WorldValueSentence*>();
  parameters->push_back(par);
}
PredicateSentence::PredicateSentence(string s, WorldValueSentence* par1, 
WorldValueSentence* par2): name(s), arity(2) {
  parameters = new list<WorldValueSentence*>();
  parameters->push_back(par1);
  parameters->push_back(par2);
}

PredicateSentence::PredicateSentence(string s, list<WorldValueSentence*>* l): 
name(s), parameters(l) {
  if (l != 0) { // should throw something if it IS!
    arity = l->size();
  }
}

PredicateSentence::~PredicateSentence() {
  for (list<WorldValueSentence*>::iterator it = parameters->begin();
       it != parameters->end(); it++) {
    delete(*it);
  }
  delete parameters;
}

string PredicateSentence::toString() const { 
  stringstream s;
  s << name << "_" << arity << "(";
  list<WorldValueSentence*>::const_iterator it = parameters->begin();
  if (it == parameters->end()) {
    s << ")";
    return s.str();
  }
  s << (*it)->toString();
  for (it++; it != parameters->end(); it++) {
    s << ", ";
    s << (*it)->toString();
  }
  s << ")";
  return s.str();
}

const string PredicateSentence::getName() const {
  return name;
}
int PredicateSentence::getArity() const {
  return arity;
}
const std::list<WorldValueSentence*>* PredicateSentence::getParameters() const {
  return parameters;
}


bool PredicateSentence::equals(const Sentence* s) const {
  const PredicateSentence* ps;
  if ((ps = dynamic_cast<const PredicateSentence*>(s))) {
    if ((ps->name == name) && (ps->arity == arity) &&
        (equal(ps->parameters->begin(), ps->parameters->end(),
               parameters->begin(), PointerComparator<Sentence>())))
      return true;
  }
  return false;
}


EqualitySentence::EqualitySentence(WorldValueSentence* f, WorldValueSentence* 
s):
  first(f), second(s) {}
EqualitySentence::~EqualitySentence() {
  delete first;
  delete second;
}

string EqualitySentence::toString() const {
  string s;
  s += "(";
  s += first->toString();
  s += ")";
  s += "=";
  s += "(";
  s += second->toString();
  s += ")";
  return s;
}

const WorldValueSentence* EqualitySentence::getFirst() const {
  return first;
}
const WorldValueSentence* EqualitySentence::getSecond() const {
  return second;
}

bool EqualitySentence::equals(const Sentence* s) const {
  const EqualitySentence* es;
  if ((es = dynamic_cast<const EqualitySentence*>(s))) {
    if ((*first == *es->first) && (*second == *es->second))
      return true;
  }
  return false;
}


OperatorSentence::OperatorSentence(int o, Sentence* f, Sentence* s = 0): op(o), 
firstOperand(f), secondOperand(s) {}
OperatorSentence::~OperatorSentence() {
  delete firstOperand;
  delete secondOperand;
}
string OperatorSentence::toString() const {
  string s;
  if (op == NOT) {
    s += "NOT(";
    s += firstOperand->toString();
    s += ")";
  }
  else {
    s += "(";
    s += firstOperand->toString();
    s += ")";
    switch (op) {
      case AND: s += " AND "; break;
      case OR: s += " OR "; break;
      case IMPLIES: s+= " ==> "; break;
      case IMPLIED: s+= " <== "; break;
      case IFF: s += " <==> "; break;
    }
    s += "(";
    s += secondOperand->toString();
    s += ")";
  }
  return s;
}

int OperatorSentence::getOp() const {
  return op;
}
const Sentence* OperatorSentence::getFirstOperand() const {
  return firstOperand;
}
const Sentence* OperatorSentence::getSecondOperand() const {
  return secondOperand;
}


bool OperatorSentence::equals(const Sentence* s) const {
  const OperatorSentence* os;
  if ((os = dynamic_cast<const OperatorSentence*>(s))) {
    if ((*firstOperand == *os->firstOperand)) {
      if (((!secondOperand) && (!os->secondOperand)) ||
          (secondOperand && os->secondOperand && *secondOperand == 
*os->secondOperand))
        return true;
    }
  }
  return false;
}


QuantifierSentence::QuantifierSentence(int q, VariableSentence* v, Sentence* s):
  quantifier(q), variable(v), sentence(s) {} 

string QuantifierSentence::toString() const {
  string s;
  switch (quantifier) {
    case FORALL: 
      s += "For all ";
      s += variable->toString();
      s += ", (";
      s += sentence->toString();
      s += ")";
      break;
    case EXISTS:
      s += "Exists ";
      s += variable->toString();
      s += ", (";
      s += sentence->toString();
      s += ")";
      break;
  }
  return s;
}

int QuantifierSentence::getQuantifier() const {
  return quantifier;
}
const WorldValueSentence* QuantifierSentence::getVariable() const {
  return variable;
}
const Sentence* QuantifierSentence::getSentence() const {
  return sentence;
}


bool QuantifierSentence::equals(const Sentence* s) const {
  const QuantifierSentence* qs;
  if ((qs = dynamic_cast<const QuantifierSentence*>(s))) {
    if ((*variable == *qs->variable) &&
        (*sentence == *qs->sentence))
      return true;
  }
  return false;
}

#ifndef __SENTENCE_H__
#define __SENTENCE_H__

// notes:
// must have names of predicates and private_names
#include <string>
#include <map>
#include <list>

class Sentence {

protected:
  virtual bool equals(const Sentence* s) const = 0;
public:

  Sentence();
  
  virtual std::string toString() const = 0;
  virtual ~Sentence();

  friend inline bool operator==(const Sentence& s1, const Sentence& s2) {
    return s1.equals(&s2);
  }
  friend inline bool operator!=(const Sentence& s1, const Sentence& s2) {
    return !s1.equals(&s2);
  }
};

class WorldValueSentence: public Sentence {
protected:
  std::string name;
public:
  WorldValueSentence(std::string s);
  const string getName() const;
};

class ConstantSentence : public WorldValueSentence {
private:
  bool equals(const Sentence* s) const;
public:
  ConstantSentence(std::string s);
  std::string toString() const;
};

class VariableSentence: public WorldValueSentence {
private:
  bool equals(const Sentence* s) const;
public:
  VariableSentence(std::string s);
  std::string toString() const;
};

class FunctionSentence: public WorldValueSentence {
private:
  bool equals(const Sentence* s) const;
  int arity;
  std::list<WorldValueSentence*>* parameters;
public:
  FunctionSentence(std::string s, WorldValueSentence* par);
  FunctionSentence(std::string s, WorldValueSentence* par1, WorldValueSentence*\
                   par2);
  FunctionSentence(std::string s, std::list<WorldValueSentence*>* l);
  ~FunctionSentence();
  
  std::string toString() const;
  int getArity() const;
  const std::list<WorldValueSentence*>* getParameters() const;
  
};


class PredicateSentence: public Sentence {
private:
  bool equals(const Sentence* s) const;
  std::string name;
  int arity;
  std::list<WorldValueSentence*>* parameters;
public:
  PredicateSentence(std::string s, WorldValueSentence* par);
  PredicateSentence(std::string s, WorldValueSentence* par1, 
WorldValueSentence* par2);  
  PredicateSentence(std::string s, std::list<WorldValueSentence*>* l);
  ~PredicateSentence();

  std::string toString() const;
  const string getName() const;
  int getArity() const;
  const std::list<WorldValueSentence*>* getParameters() const;
};

class EqualitySentence: public Sentence {
private:
  bool equals(const Sentence* s) const;
  WorldValueSentence* first;
  WorldValueSentence* second;
public:
  EqualitySentence(WorldValueSentence* f, WorldValueSentence* s);
  ~EqualitySentence();

  std::string toString() const;
  const WorldValueSentence* getFirst() const;
  const WorldValueSentence* getSecond() const;
};

class OperatorSentence: public Sentence {
private:
  bool equals(const Sentence* s) const;
  int op;
  Sentence* firstOperand;
  Sentence* secondOperand; // not always used
public:
  static const int AND = 1;
  static const int OR = 2;
  static const int NOT = 3;
  static const int IMPLIES = 4;
  static const int IMPLIED = 5;
  static const int IFF = 6;
  
  OperatorSentence(int o, Sentence* f, Sentence* s = 0);
  ~OperatorSentence();

  std::string toString() const;
  int getOp() const;
  const Sentence* getFirstOperand() const;
  const Sentence* getSecondOperand() const;
};

class QuantifierSentence: public Sentence {
private:
  bool equals(const Sentence* s) const;
  int quantifier;
  VariableSentence* variable;
  Sentence* sentence;
public:
  static const int FORALL = 1;
  static const int EXISTS = 2;
  QuantifierSentence(int q, VariableSentence* v, Sentence* s);

  std::string toString() const;
  int getQuantifier() const;
  const WorldValueSentence* getVariable() const;
  const Sentence* getSentence() const;
};
#endif
CXX = g++
CXXFLAGS = -Wall -g
OFILES = main.o Sentence.o SentenceTree.o Interface.o NameTable.o
EXECUTABLE = atp

all:    $(OFILES)
        $(CXX) $(CXXFLAGS) -o$(EXECUTABLE) $(OFILES)

main.o: main.cpp Sentence.h SentenceTree.h Interface.h

Sentence.o: Sentence.h Sentence.cpp PointerComparator.h

SentenceTree.o: SentenceTree.h SentenceTree.cpp

Interface.o: Interface.h Interface.cpp Sentence.h NameTable.h

NameTable.o: NameTable.h NameTable.cpp

.PHONY: clean
clean:
        rm -f $(OFILES) *~ $(EXECUTABLE)

Other related posts:

  • » [mathprog] Some changes