Eliminate more uses of SExpr. (#6149)
authorAbdalrhman Mohamed <32971963+abdoo8080@users.noreply.github.com>
Thu, 18 Mar 2021 18:33:47 +0000 (13:33 -0500)
committerGitHub <noreply@github.com>
Thu, 18 Mar 2021 18:33:47 +0000 (13:33 -0500)
This PR eliminates all remaining uses of SExpr outside of statistics.

13 files changed:
src/api/cvc4cpp.cpp
src/main/driver_unified.cpp
src/parser/cvc/Cvc.g
src/parser/smt2/smt2.h
src/printer/printer.h
src/printer/smt2/smt2_printer.h
src/smt/command.cpp
src/smt/command.h
src/smt/env.h
src/smt/smt_engine.cpp
src/smt/smt_engine.h
test/regress/CMakeLists.txt
test/regress/regress0/options/set-and-get-options.smt2 [new file with mode: 0644]

index 4ccff99303e859ab35bb16fe0d46a09060f20ea1..e87e6176b87a1f8383b8216027fd5b4060bbe8d7 100644 (file)
@@ -6229,7 +6229,7 @@ std::string Solver::getOption(const std::string& option) const
 {
   CVC4_API_TRY_CATCH_BEGIN;
   //////// all checks before this line
-  SExpr res = d_smtEngine->getOption(option);
+  Node res = d_smtEngine->getOption(option);
   return res.toString();
   ////////
   CVC4_API_TRY_CATCH_END;
index 001da12db5153d6a34e7fd8151197fac842144c4..846bef5be6f3019a110f4f239d9de02da258ad82 100644 (file)
@@ -252,7 +252,7 @@ int runCvc4(int argc, char* argv[], Options& opts) {
         //     "--tear-down-incremental incompatible with --incremental");
         // }
 
-        // cmd.reset(new SetOptionCommand("incremental", SExpr(false)));
+        // cmd.reset(new SetOptionCommand("incremental", "false"));
         // cmd->setMuted(true);
         // pExecutor->doCommand(cmd);
       }
index b7997cafa3d11aa0ee965e80b5197c533b978291..c12c1efc62b3f13ba6a7aa7964f607b1a53be778 100644 (file)
@@ -538,7 +538,6 @@ api::Term addNots(api::Solver* s, size_t n, api::Term e) {
 #include "parser/antlr_input.h"
 #include "parser/antlr_tracing.h"
 #include "parser/parser.h"
-#include "util/integer.h"
 
 }/* @lexer::includes */
 
@@ -551,6 +550,7 @@ api::Term addNots(api::Solver* s, size_t n, api::Term e) {
 #include "parser/antlr_tracing.h"
 #include "parser/parser.h"
 #include "smt/command.h"
+#include "util/rational.h"
 
 namespace CVC4 {
   class Expr;
index 0449f13cee74a546b7e9bd7ebf0ac52ad4a7e9b2..a7fe65f673c6d930a3f636823e5618b3503e8298 100644 (file)
@@ -34,7 +34,6 @@
 namespace CVC4 {
 
 class Command;
-class SExpr;
 
 namespace api {
 class Solver;
index 06b263e51d034b07d4362137aff9c812fbc38831..17fd668483c5c6027488202eef3f9d32e352a045 100644 (file)
@@ -25,7 +25,6 @@
 #include "options/language.h"
 #include "smt/model.h"
 #include "util/result.h"
-#include "util/sexpr.h"
 
 namespace CVC4 {
 
index 664bc2bf9dc9867c58fd774c45a7d4da44b9425b..26ccf58217f04c9ecde3260938d373a31a4b4198 100644 (file)
@@ -245,7 +245,6 @@ class Smt2Printer : public CVC4::Printer
                           TNode n,
                           int toDepth,
                           TypeNode tn) const;
-  void toStream(std::ostream& out, const SExpr& sexpr) const;
   void toStream(std::ostream& out, const DType& dt) const;
   /**
    * To stream model sort. This prints the appropriate output for type
index 4fe88ceec1e4d0698ce6c0f765700edde554512d..5d27c2eb8f9011838f3fa55a8c1cc7af3045a094 100644 (file)
@@ -38,7 +38,6 @@
 #include "smt/model.h"
 #include "smt/smt_engine.h"
 #include "smt/smt_engine_scope.h"
-#include "util/sexpr.h"
 #include "util/unsafe_interrupt_exception.h"
 #include "util/utility.h"
 
@@ -1641,15 +1640,15 @@ void GetAssignmentCommand::invoke(api::Solver* solver, SymbolManager* sm)
     // of whether terms is empty.
     std::vector<api::Term> values = solver->getValue(terms);
     Assert(values.size() == names.size());
-    std::vector<SExpr> sexprs;
+    std::vector<api::Term> sexprs;
     for (size_t i = 0, nterms = terms.size(); i < nterms; i++)
     {
-      std::vector<SExpr> ss;
-      ss.emplace_back(SExpr::Keyword(names[i]));
-      ss.emplace_back(SExpr::Keyword(values[i].toString()));
-      sexprs.emplace_back(ss);
+      // Treat the expression name as a variable name as opposed to a string
+      // constant to avoid printing double quotes around the name.
+      api::Term name = solver->mkVar(solver->getBooleanSort(), names[i]);
+      sexprs.push_back(solver->mkTerm(api::SEXPR, name, values[i]));
     }
-    d_result = SExpr(sexprs);
+    d_result = solver->mkTerm(api::SEXPR, sexprs);
     d_commandStatus = CommandSuccess::instance();
   }
   catch (api::CVC4ApiRecoverableException& e)
@@ -1666,7 +1665,7 @@ void GetAssignmentCommand::invoke(api::Solver* solver, SymbolManager* sm)
   }
 }
 
-SExpr GetAssignmentCommand::getResult() const { return d_result; }
+api::Term GetAssignmentCommand::getResult() const { return d_result; }
 void GetAssignmentCommand::printResult(std::ostream& out,
                                        uint32_t verbosity) const
 {
index 78e7c4071455aa9cd9a9ac0bb681d9374d836f9a..1017a74c942bfa94b3f761ff55de04fabd994278 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "api/cvc4cpp.h"
 #include "cvc4_export.h"
-#include "util/sexpr.h"
+#include "options/language.h"
 
 namespace CVC4 {
 
@@ -930,12 +930,12 @@ class CVC4_EXPORT GetValueCommand : public Command
 class CVC4_EXPORT GetAssignmentCommand : public Command
 {
  protected:
-  SExpr d_result;
+  api::Term d_result;
 
  public:
   GetAssignmentCommand();
 
-  SExpr getResult() const;
+  api::Term getResult() const;
   void invoke(api::Solver* solver, SymbolManager* sm) override;
   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
   Command* clone() const override;
index e8f73a4f2a885ce71856b3c62f2abfa46dcec0a9..c777a7755988b3e53df8781fd7c156d0e636e227 100644 (file)
@@ -22,7 +22,6 @@
 
 #include "options/options.h"
 #include "theory/logic_info.h"
-#include "util/sexpr.h"
 #include "util/statistics.h"
 
 namespace CVC4 {
index aac8ceab71d369618d70ae5ea3407f0fc95efad8..3493aae1180d4346f5799c6a90126d1917752c3c 100644 (file)
@@ -1928,12 +1928,14 @@ void SmtEngine::setOption(const std::string& key, const std::string& value)
   NodeManagerScope nms(getNodeManager());
   Trace("smt") << "SMT setOption(" << key << ", " << value << ")" << endl;
 
-  if(Dump.isOn("benchmark")) {
+  if (Dump.isOn("benchmark"))
+  {
     getPrinter().toStreamCmdSetOption(
         getOutputManager().getDumpOut(), key, value);
   }
 
-  if(key == "command-verbosity") {
+  if (key == "command-verbosity")
+  {
     size_t fstIndex = value.find(" ");
     size_t sndIndex = value.find(" ", fstIndex + 1);
     if (sndIndex == std::string::npos)
@@ -1948,7 +1950,8 @@ void SmtEngine::setOption(const std::string& key, const std::string& value)
       d_commandVerbosity[c] = v;
       return;
     }
-    throw OptionException("command-verbosity value must be a tuple (command-name, integer)");
+    throw OptionException(
+        "command-verbosity value must be a tuple (command-name integer)");
   }
 
   if (value.find(" ") != std::string::npos)
@@ -1964,59 +1967,88 @@ void SmtEngine::setIsInternalSubsolver() { d_isInternalSubsolver = true; }
 
 bool SmtEngine::isInternalSubsolver() const { return d_isInternalSubsolver; }
 
-CVC4::SExpr SmtEngine::getOption(const std::string& key) const
+Node SmtEngine::getOption(const std::string& key) const
 {
   NodeManagerScope nms(getNodeManager());
+  NodeManager* nm = d_env->getNodeManager();
 
   Trace("smt") << "SMT getOption(" << key << ")" << endl;
 
-  if(key.length() >= 18 &&
-     key.compare(0, 18, "command-verbosity:") == 0) {
-    map<string, Integer>::const_iterator i = d_commandVerbosity.find(key.c_str() + 18);
-    if(i != d_commandVerbosity.end()) {
-      return SExpr((*i).second);
+  if (key.length() >= 18 && key.compare(0, 18, "command-verbosity:") == 0)
+  {
+    map<string, Integer>::const_iterator i =
+        d_commandVerbosity.find(key.c_str() + 18);
+    if (i != d_commandVerbosity.end())
+    {
+      return nm->mkConst(Rational(i->second));
     }
     i = d_commandVerbosity.find("*");
-    if(i != d_commandVerbosity.end()) {
-      return SExpr((*i).second);
+    if (i != d_commandVerbosity.end())
+    {
+      return nm->mkConst(Rational(i->second));
     }
-    return SExpr(Integer(2));
+    return nm->mkConst(Rational(2));
   }
 
-  if(Dump.isOn("benchmark")) {
+  if (Dump.isOn("benchmark"))
+  {
     getPrinter().toStreamCmdGetOption(d_outMgr.getDumpOut(), key);
   }
 
-  if(key == "command-verbosity") {
-    vector<SExpr> result;
-    SExpr defaultVerbosity;
-    for(map<string, Integer>::const_iterator i = d_commandVerbosity.begin();
-        i != d_commandVerbosity.end();
-        ++i) {
-      vector<SExpr> v;
-      v.push_back(SExpr((*i).first));
-      v.push_back(SExpr((*i).second));
-      if((*i).first == "*") {
+  if (key == "command-verbosity")
+  {
+    vector<Node> result;
+    Node defaultVerbosity;
+    for (map<string, Integer>::const_iterator i = d_commandVerbosity.begin();
+         i != d_commandVerbosity.end();
+         ++i)
+    {
+      // treat the command name as a variable name as opposed to a string
+      // constant to avoid printing double quotes around the name
+      Node name = nm->mkBoundVar(i->first, nm->integerType());
+      Node value = nm->mkConst(Rational(i->second));
+      if ((*i).first == "*")
+      {
         // put the default at the end of the SExpr
-        defaultVerbosity = SExpr(v);
-      } else {
-        result.push_back(SExpr(v));
+        defaultVerbosity = nm->mkNode(Kind::SEXPR, name, value);
+      }
+      else
+      {
+        result.push_back(nm->mkNode(Kind::SEXPR, name, value));
       }
     }
-    // put the default at the end of the SExpr
-    if(!defaultVerbosity.isAtom()) {
-      result.push_back(defaultVerbosity);
-    } else {
-      // ensure the default is always listed
-      vector<SExpr> v;
-      v.push_back(SExpr("*"));
-      v.push_back(SExpr(Integer(2)));
-      result.push_back(SExpr(v));
+    // ensure the default is always listed
+    if (defaultVerbosity.isNull())
+    {
+      defaultVerbosity = nm->mkNode(Kind::SEXPR,
+                                    nm->mkBoundVar("*", nm->integerType()),
+                                    nm->mkConst(Rational(2)));
     }
-    return SExpr(result);
+    result.push_back(defaultVerbosity);
+    return nm->mkNode(Kind::SEXPR, result);
   }
 
-  return SExpr::parseAtom(getOptions().getOption(key));
+  // parse atom string
+  std::string atom = getOptions().getOption(key);
+
+  if (atom == "true")
+  {
+    return nm->mkConst<bool>(true);
+  }
+  else if (atom == "false")
+  {
+    return nm->mkConst<bool>(false);
+  }
+  try
+  {
+    Integer z(atom);
+    return nm->mkConst(Rational(z));
+  }
+  catch (std::invalid_argument&)
+  {
+    // Fall through to the next case
+  }
+  return nm->mkConst(String(atom));
 }
 
 Options& SmtEngine::getOptions() { return d_env->d_options; }
index 744320950e75eb3d4fa7ee2da0b4193bc073c46c..dd0fe3a6e72c040aa3ff34453225e3c5fb29b253 100644 (file)
@@ -310,7 +310,7 @@ class CVC4_EXPORT SmtEngine
    * Get an aspect of the current SMT execution environment.
    * @throw OptionException
    */
-  CVC4::SExpr getOption(const std::string& key) const;
+  Node getOption(const std::string& key) const;
 
   /**
    * Define function func in the current context to be:
index 14752f72639feb890e30be8bb267a4cd66e02c43..964e73fc0aaddafc943c5a5d08bf4ce9302158a5 100644 (file)
@@ -707,6 +707,7 @@ set(regress_0_tests
   regress0/nl/very-simple-unsat.smt2
   regress0/opt-abd-no-use.smt2
   regress0/options/invalid_dump.smt2
+  regress0/options/set-and-get-options.smt2
   regress0/parallel-let.smt2
   regress0/parser/as.smt2
   regress0/parser/bv_arity_smt2.6.smt2
diff --git a/test/regress/regress0/options/set-and-get-options.smt2 b/test/regress/regress0/options/set-and-get-options.smt2
new file mode 100644 (file)
index 0000000..478e3d5
--- /dev/null
@@ -0,0 +1,19 @@
+; EXPECT: ((* 2))
+; EXPECT: ((check-sat 1) (* 1))
+; EXPECT: true
+; EXPECT: false
+; EXPECT: 15
+; EXPECT: "SimplificationMode::NONE"
+
+(get-option :command-verbosity)
+(set-option :command-verbosity (* 1))
+(set-option :command-verbosity (check-sat 1))
+(get-option :command-verbosity)
+(set-option :check-models true)
+(get-option :check-models)
+(set-option :check-models false)
+(get-option :check-models)
+(set-option :dag-thresh 15)
+(get-option :dag-thresh)
+(set-option :simplification none)
+(get-option :simplification)