1 /*********************                                                        */
2 /*! \file command.h
3  ** \verbatim
4  ** Top contributors (to current version):
5  **   Tim King, Morgan Deters, Haniel Barbosa
6  ** This file is part of the CVC4 project.
7  ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8  ** in the top-level source directory) and their institutional affiliations.
9  ** All rights reserved.  See the file COPYING in the top-level source
10  ** directory for licensing information.\endverbatim
11  **
12  ** \brief Implementation of the command pattern on SmtEngines.
13  **
14  ** Implementation of the command pattern on SmtEngines.  Command
15  ** objects are generated by the parser (typically) to implement the
16  ** commands in parsed input (see Parser::parseNextCommand()), or by
17  ** client code.
18  **/
19 
20 #include "cvc4_public.h"
21 
22 #ifndef __CVC4__COMMAND_H
23 #define __CVC4__COMMAND_H
24 
25 #include <iosfwd>
26 #include <map>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 #include "expr/datatype.h"
32 #include "expr/expr.h"
33 #include "expr/type.h"
34 #include "expr/variable_type_map.h"
35 #include "proof/unsat_core.h"
36 #include "util/proof.h"
37 #include "util/result.h"
38 #include "util/sexpr.h"
39 
40 namespace CVC4 {
41 
42 class SmtEngine;
43 class Command;
44 class CommandStatus;
45 class Model;
46 
47 std::ostream& operator<<(std::ostream&, const Command&) CVC4_PUBLIC;
48 std::ostream& operator<<(std::ostream&, const Command*) CVC4_PUBLIC;
49 std::ostream& operator<<(std::ostream&, const CommandStatus&) CVC4_PUBLIC;
50 std::ostream& operator<<(std::ostream&, const CommandStatus*) CVC4_PUBLIC;
51 
52 /** The status an SMT benchmark can have */
53 enum BenchmarkStatus
54 {
55   /** Benchmark is satisfiable */
56   SMT_SATISFIABLE,
57   /** Benchmark is unsatisfiable */
58   SMT_UNSATISFIABLE,
59   /** The status of the benchmark is unknown */
60   SMT_UNKNOWN
61 }; /* enum BenchmarkStatus */
62 
63 std::ostream& operator<<(std::ostream& out, BenchmarkStatus status) CVC4_PUBLIC;
64 
65 /**
66  * IOStream manipulator to print success messages or not.
67  *
68  *   out << Command::printsuccess(false) << CommandSuccess();
69  *
70  * prints nothing, but
71  *
72  *   out << Command::printsuccess(true) << CommandSuccess();
73  *
74  * prints a success message (in a manner appropriate for the current
75  * output language).
76  */
77 class CVC4_PUBLIC CommandPrintSuccess
78 {
79  public:
80   /** Construct a CommandPrintSuccess with the given setting. */
CommandPrintSuccess(bool printSuccess)81   CommandPrintSuccess(bool printSuccess) : d_printSuccess(printSuccess) {}
82   void applyPrintSuccess(std::ostream& out);
83   static bool getPrintSuccess(std::ostream& out);
84   static void setPrintSuccess(std::ostream& out, bool printSuccess);
85 
86  private:
87   /** The allocated index in ios_base for our depth setting. */
88   static const int s_iosIndex;
89 
90   /**
91    * The default setting, for ostreams that haven't yet had a setdepth()
92    * applied to them.
93    */
94   static const int s_defaultPrintSuccess = false;
95 
96   /** When this manipulator is used, the setting is stored here. */
97   bool d_printSuccess;
98 
99 }; /* class CommandPrintSuccess */
100 
101 /**
102  * Sets the default print-success setting when pretty-printing an Expr
103  * to an ostream.  Use like this:
104  *
105  *   // let out be an ostream, e an Expr
106  *   out << Expr::setdepth(n) << e << endl;
107  *
108  * The depth stays permanently (until set again) with the stream.
109  */
110 std::ostream& operator<<(std::ostream& out,
111                          CommandPrintSuccess cps) CVC4_PUBLIC;
112 
113 class CVC4_PUBLIC CommandStatus
114 {
115  protected:
116   // shouldn't construct a CommandStatus (use a derived class)
CommandStatus()117   CommandStatus() {}
118  public:
~CommandStatus()119   virtual ~CommandStatus() {}
120   void toStream(std::ostream& out,
121                 OutputLanguage language = language::output::LANG_AUTO) const;
122   virtual CommandStatus& clone() const = 0;
123 }; /* class CommandStatus */
124 
125 class CVC4_PUBLIC CommandSuccess : public CommandStatus
126 {
127   static const CommandSuccess* s_instance;
128 
129  public:
instance()130   static const CommandSuccess* instance() { return s_instance; }
clone()131   CommandStatus& clone() const override
132   {
133     return const_cast<CommandSuccess&>(*this);
134   }
135 }; /* class CommandSuccess */
136 
137 class CVC4_PUBLIC CommandInterrupted : public CommandStatus
138 {
139   static const CommandInterrupted* s_instance;
140 
141  public:
instance()142   static const CommandInterrupted* instance() { return s_instance; }
clone()143   CommandStatus& clone() const override
144   {
145     return const_cast<CommandInterrupted&>(*this);
146   }
147 }; /* class CommandInterrupted */
148 
149 class CVC4_PUBLIC CommandUnsupported : public CommandStatus
150 {
151  public:
clone()152   CommandStatus& clone() const override
153   {
154     return *new CommandUnsupported(*this);
155   }
156 }; /* class CommandSuccess */
157 
158 class CVC4_PUBLIC CommandFailure : public CommandStatus
159 {
160   std::string d_message;
161 
162  public:
CommandFailure(std::string message)163   CommandFailure(std::string message) : d_message(message) {}
clone()164   CommandFailure& clone() const override { return *new CommandFailure(*this); }
getMessage()165   std::string getMessage() const { return d_message; }
166 }; /* class CommandFailure */
167 
168 /**
169  * The execution of the command resulted in a non-fatal error and further
170  * commands can be processed. This status is for example used when a user asks
171  * for an unsat core in a place that is not immediately preceded by an
172  * unsat/valid response.
173  */
174 class CVC4_PUBLIC CommandRecoverableFailure : public CommandStatus
175 {
176   std::string d_message;
177 
178  public:
CommandRecoverableFailure(std::string message)179   CommandRecoverableFailure(std::string message) : d_message(message) {}
clone()180   CommandRecoverableFailure& clone() const override
181   {
182     return *new CommandRecoverableFailure(*this);
183   }
getMessage()184   std::string getMessage() const { return d_message; }
185 }; /* class CommandRecoverableFailure */
186 
187 class CVC4_PUBLIC Command
188 {
189  protected:
190   /**
191    * This field contains a command status if the command has been
192    * invoked, or NULL if it has not.  This field is either a
193    * dynamically-allocated pointer, or it's a pointer to the singleton
194    * CommandSuccess instance.  Doing so is somewhat asymmetric, but
195    * it avoids the need to dynamically allocate memory in the common
196    * case of a successful command.
197    */
198   const CommandStatus* d_commandStatus;
199 
200   /**
201    * True if this command is "muted"---i.e., don't print "success" on
202    * successful execution.
203    */
204   bool d_muted;
205 
206  public:
207   typedef CommandPrintSuccess printsuccess;
208 
209   Command();
210   Command(const Command& cmd);
211 
212   virtual ~Command();
213 
214   virtual void invoke(SmtEngine* smtEngine) = 0;
215   virtual void invoke(SmtEngine* smtEngine, std::ostream& out);
216 
217   void toStream(std::ostream& out,
218                 int toDepth = -1,
219                 bool types = false,
220                 size_t dag = 1,
221                 OutputLanguage language = language::output::LANG_AUTO) const;
222 
223   std::string toString() const;
224 
225   virtual std::string getCommandName() const = 0;
226 
227   /**
228    * If false, instruct this Command not to print a success message.
229    */
setMuted(bool muted)230   void setMuted(bool muted) { d_muted = muted; }
231   /**
232    * Determine whether this Command will print a success message.
233    */
isMuted()234   bool isMuted() { return d_muted; }
235   /**
236    * Either the command hasn't run yet, or it completed successfully
237    * (CommandSuccess, not CommandUnsupported or CommandFailure).
238    */
239   bool ok() const;
240 
241   /**
242    * The command completed in a failure state (CommandFailure, not
243    * CommandSuccess or CommandUnsupported).
244    */
245   bool fail() const;
246 
247   /**
248    * The command was ran but was interrupted due to resource limiting.
249    */
250   bool interrupted() const;
251 
252   /** Get the command status (it's NULL if we haven't run yet). */
getCommandStatus()253   const CommandStatus* getCommandStatus() const { return d_commandStatus; }
254   virtual void printResult(std::ostream& out, uint32_t verbosity = 2) const;
255 
256   /**
257    * Maps this Command into one for a different ExprManager, using
258    * variableMap for the translation and extending it with any new
259    * mappings.
260    */
261   virtual Command* exportTo(ExprManager* exprManager,
262                             ExprManagerMapCollection& variableMap) = 0;
263 
264   /**
265    * Clone this Command (make a shallow copy).
266    */
267   virtual Command* clone() const = 0;
268 
269  protected:
270   class ExportTransformer
271   {
272     ExprManager* d_exprManager;
273     ExprManagerMapCollection& d_variableMap;
274 
275    public:
ExportTransformer(ExprManager * exprManager,ExprManagerMapCollection & variableMap)276     ExportTransformer(ExprManager* exprManager,
277                       ExprManagerMapCollection& variableMap)
278         : d_exprManager(exprManager), d_variableMap(variableMap)
279     {
280     }
operator()281     Expr operator()(Expr e) { return e.exportTo(d_exprManager, d_variableMap); }
operator()282     Type operator()(Type t) { return t.exportTo(d_exprManager, d_variableMap); }
283   }; /* class Command::ExportTransformer */
284 };   /* class Command */
285 
286 /**
287  * EmptyCommands are the residue of a command after the parser handles
288  * them (and there's nothing left to do).
289  */
290 class CVC4_PUBLIC EmptyCommand : public Command
291 {
292  public:
293   EmptyCommand(std::string name = "");
294   std::string getName() const;
295   void invoke(SmtEngine* smtEngine) override;
296   Command* exportTo(ExprManager* exprManager,
297                     ExprManagerMapCollection& variableMap) override;
298   Command* clone() const override;
299   std::string getCommandName() const override;
300 
301  protected:
302   std::string d_name;
303 }; /* class EmptyCommand */
304 
305 class CVC4_PUBLIC EchoCommand : public Command
306 {
307  public:
308   EchoCommand(std::string output = "");
309 
310   std::string getOutput() const;
311 
312   void invoke(SmtEngine* smtEngine) override;
313   void invoke(SmtEngine* smtEngine, std::ostream& out) override;
314   Command* exportTo(ExprManager* exprManager,
315                     ExprManagerMapCollection& variableMap) override;
316   Command* clone() const override;
317   std::string getCommandName() const override;
318 
319  protected:
320   std::string d_output;
321 }; /* class EchoCommand */
322 
323 class CVC4_PUBLIC AssertCommand : public Command
324 {
325  protected:
326   Expr d_expr;
327   bool d_inUnsatCore;
328 
329  public:
330   AssertCommand(const Expr& e, bool inUnsatCore = true);
331 
332   Expr getExpr() const;
333 
334   void invoke(SmtEngine* smtEngine) override;
335   Command* exportTo(ExprManager* exprManager,
336                     ExprManagerMapCollection& variableMap) override;
337   Command* clone() const override;
338   std::string getCommandName() const override;
339 }; /* class AssertCommand */
340 
341 class CVC4_PUBLIC PushCommand : public Command
342 {
343  public:
344   void invoke(SmtEngine* smtEngine) override;
345   Command* exportTo(ExprManager* exprManager,
346                     ExprManagerMapCollection& variableMap) override;
347   Command* clone() const override;
348   std::string getCommandName() const override;
349 }; /* class PushCommand */
350 
351 class CVC4_PUBLIC PopCommand : public Command
352 {
353  public:
354   void invoke(SmtEngine* smtEngine) override;
355   Command* exportTo(ExprManager* exprManager,
356                     ExprManagerMapCollection& variableMap) override;
357   Command* clone() const override;
358   std::string getCommandName() const override;
359 }; /* class PopCommand */
360 
361 class CVC4_PUBLIC DeclarationDefinitionCommand : public Command
362 {
363  protected:
364   std::string d_symbol;
365 
366  public:
367   DeclarationDefinitionCommand(const std::string& id);
368 
369   void invoke(SmtEngine* smtEngine) override = 0;
370   std::string getSymbol() const;
371 }; /* class DeclarationDefinitionCommand */
372 
373 class CVC4_PUBLIC DeclareFunctionCommand : public DeclarationDefinitionCommand
374 {
375  protected:
376   Expr d_func;
377   Type d_type;
378   bool d_printInModel;
379   bool d_printInModelSetByUser;
380 
381  public:
382   DeclareFunctionCommand(const std::string& id, Expr func, Type type);
383   Expr getFunction() const;
384   Type getType() const;
385   bool getPrintInModel() const;
386   bool getPrintInModelSetByUser() const;
387   void setPrintInModel(bool p);
388 
389   void invoke(SmtEngine* smtEngine) override;
390   Command* exportTo(ExprManager* exprManager,
391                     ExprManagerMapCollection& variableMap) override;
392   Command* clone() const override;
393   std::string getCommandName() const override;
394 }; /* class DeclareFunctionCommand */
395 
396 class CVC4_PUBLIC DeclareTypeCommand : public DeclarationDefinitionCommand
397 {
398  protected:
399   size_t d_arity;
400   Type d_type;
401 
402  public:
403   DeclareTypeCommand(const std::string& id, size_t arity, Type t);
404 
405   size_t getArity() const;
406   Type getType() const;
407 
408   void invoke(SmtEngine* smtEngine) override;
409   Command* exportTo(ExprManager* exprManager,
410                     ExprManagerMapCollection& variableMap) override;
411   Command* clone() const override;
412   std::string getCommandName() const override;
413 }; /* class DeclareTypeCommand */
414 
415 class CVC4_PUBLIC DefineTypeCommand : public DeclarationDefinitionCommand
416 {
417  protected:
418   std::vector<Type> d_params;
419   Type d_type;
420 
421  public:
422   DefineTypeCommand(const std::string& id, Type t);
423   DefineTypeCommand(const std::string& id,
424                     const std::vector<Type>& params,
425                     Type t);
426 
427   const std::vector<Type>& getParameters() const;
428   Type getType() const;
429 
430   void invoke(SmtEngine* smtEngine) override;
431   Command* exportTo(ExprManager* exprManager,
432                     ExprManagerMapCollection& variableMap) override;
433   Command* clone() const override;
434   std::string getCommandName() const override;
435 }; /* class DefineTypeCommand */
436 
437 class CVC4_PUBLIC DefineFunctionCommand : public DeclarationDefinitionCommand
438 {
439  protected:
440   Expr d_func;
441   std::vector<Expr> d_formals;
442   Expr d_formula;
443 
444  public:
445   DefineFunctionCommand(const std::string& id, Expr func, Expr formula);
446   DefineFunctionCommand(const std::string& id,
447                         Expr func,
448                         const std::vector<Expr>& formals,
449                         Expr formula);
450 
451   Expr getFunction() const;
452   const std::vector<Expr>& getFormals() const;
453   Expr getFormula() const;
454 
455   void invoke(SmtEngine* smtEngine) override;
456   Command* exportTo(ExprManager* exprManager,
457                     ExprManagerMapCollection& variableMap) override;
458   Command* clone() const override;
459   std::string getCommandName() const override;
460 }; /* class DefineFunctionCommand */
461 
462 /**
463  * This differs from DefineFunctionCommand only in that it instructs
464  * the SmtEngine to "remember" this function for later retrieval with
465  * getAssignment().  Used for :named attributes in SMT-LIBv2.
466  */
467 class CVC4_PUBLIC DefineNamedFunctionCommand : public DefineFunctionCommand
468 {
469  public:
470   DefineNamedFunctionCommand(const std::string& id,
471                              Expr func,
472                              const std::vector<Expr>& formals,
473                              Expr formula);
474   void invoke(SmtEngine* smtEngine) override;
475   Command* exportTo(ExprManager* exprManager,
476                     ExprManagerMapCollection& variableMap) override;
477   Command* clone() const override;
478 }; /* class DefineNamedFunctionCommand */
479 
480 /**
481  * The command when parsing define-fun-rec or define-funs-rec.
482  * This command will assert a set of quantified formulas that specify
483  * the (mutually recursive) function definitions provided to it.
484  */
485 class CVC4_PUBLIC DefineFunctionRecCommand : public Command
486 {
487  public:
488   DefineFunctionRecCommand(Expr func,
489                            const std::vector<Expr>& formals,
490                            Expr formula);
491   DefineFunctionRecCommand(const std::vector<Expr>& funcs,
492                            const std::vector<std::vector<Expr> >& formals,
493                            const std::vector<Expr>& formula);
494 
495   const std::vector<Expr>& getFunctions() const;
496   const std::vector<std::vector<Expr> >& getFormals() const;
497   const std::vector<Expr>& getFormulas() const;
498 
499   void invoke(SmtEngine* smtEngine) override;
500   Command* exportTo(ExprManager* exprManager,
501                     ExprManagerMapCollection& variableMap) override;
502   Command* clone() const override;
503   std::string getCommandName() const override;
504 
505  protected:
506   /** functions we are defining */
507   std::vector<Expr> d_funcs;
508   /** formal arguments for each of the functions we are defining */
509   std::vector<std::vector<Expr> > d_formals;
510   /** formulas corresponding to the bodies of the functions we are defining */
511   std::vector<Expr> d_formulas;
512 }; /* class DefineFunctionRecCommand */
513 
514 /**
515  * The command when an attribute is set by a user.  In SMT-LIBv2 this is done
516  *  via the syntax (! expr :attr)
517  */
518 class CVC4_PUBLIC SetUserAttributeCommand : public Command
519 {
520  public:
521   SetUserAttributeCommand(const std::string& attr, Expr expr);
522   SetUserAttributeCommand(const std::string& attr,
523                           Expr expr,
524                           const std::vector<Expr>& values);
525   SetUserAttributeCommand(const std::string& attr,
526                           Expr expr,
527                           const std::string& value);
528 
529   void invoke(SmtEngine* smtEngine) override;
530   Command* exportTo(ExprManager* exprManager,
531                     ExprManagerMapCollection& variableMap) override;
532   Command* clone() const override;
533   std::string getCommandName() const override;
534 
535  private:
536   SetUserAttributeCommand(const std::string& attr,
537                           Expr expr,
538                           const std::vector<Expr>& expr_values,
539                           const std::string& str_value);
540 
541   const std::string d_attr;
542   const Expr d_expr;
543   const std::vector<Expr> d_expr_values;
544   const std::string d_str_value;
545 }; /* class SetUserAttributeCommand */
546 
547 /**
548  * The command when parsing check-sat.
549  * This command will check satisfiability of the input formula.
550  */
551 class CVC4_PUBLIC CheckSatCommand : public Command
552 {
553  public:
554   CheckSatCommand();
555   CheckSatCommand(const Expr& expr);
556 
557   Expr getExpr() const;
558   Result getResult() const;
559   void invoke(SmtEngine* smtEngine) override;
560   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
561   Command* exportTo(ExprManager* exprManager,
562                     ExprManagerMapCollection& variableMap) override;
563   Command* clone() const override;
564   std::string getCommandName() const override;
565 
566  private:
567   Expr d_expr;
568   Result d_result;
569 }; /* class CheckSatCommand */
570 
571 /**
572  * The command when parsing check-sat-assuming.
573  * This command will assume a set of formulas and check satisfiability of the
574  * input formula under these assumptions.
575  */
576 class CVC4_PUBLIC CheckSatAssumingCommand : public Command
577 {
578  public:
579   CheckSatAssumingCommand(Expr term);
580   CheckSatAssumingCommand(const std::vector<Expr>& terms);
581 
582   const std::vector<Expr>& getTerms() const;
583   Result getResult() const;
584   void invoke(SmtEngine* smtEngine) override;
585   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
586   Command* exportTo(ExprManager* exprManager,
587                     ExprManagerMapCollection& variableMap) override;
588   Command* clone() const override;
589   std::string getCommandName() const override;
590 
591  private:
592   std::vector<Expr> d_terms;
593   Result d_result;
594 }; /* class CheckSatAssumingCommand */
595 
596 class CVC4_PUBLIC QueryCommand : public Command
597 {
598  protected:
599   Expr d_expr;
600   Result d_result;
601   bool d_inUnsatCore;
602 
603  public:
604   QueryCommand(const Expr& e, bool inUnsatCore = true);
605 
606   Expr getExpr() const;
607   Result getResult() const;
608   void invoke(SmtEngine* smtEngine) override;
609   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
610   Command* exportTo(ExprManager* exprManager,
611                     ExprManagerMapCollection& variableMap) override;
612   Command* clone() const override;
613   std::string getCommandName() const override;
614 }; /* class QueryCommand */
615 
616 /* ------------------- sygus commands  ------------------ */
617 
618 /** Declares a sygus universal variable */
619 class CVC4_PUBLIC DeclareSygusVarCommand : public DeclarationDefinitionCommand
620 {
621  public:
622   DeclareSygusVarCommand(const std::string& id, Expr var, Type type);
623   /** returns the declared variable */
624   Expr getVar() const;
625   /** returns the declared variable's type */
626   Type getType() const;
627   /** invokes this command
628    *
629    * The declared sygus variable is communicated to the SMT engine in case a
630    * synthesis conjecture is built later on.
631    */
632   void invoke(SmtEngine* smtEngine) override;
633   /** exports command to given expression manager */
634   Command* exportTo(ExprManager* exprManager,
635                     ExprManagerMapCollection& variableMap) override;
636   /** creates a copy of this command */
637   Command* clone() const override;
638   /** returns this command's name */
639   std::string getCommandName() const override;
640 
641  protected:
642   /** the declared variable */
643   Expr d_var;
644   /** the declared variable's type */
645   Type d_type;
646 };
647 
648 /** Declares a sygus primed variable, for invariant problems
649  *
650  * We do not actually build expressions for the declared variables because they
651  * are unnecessary for building SyGuS problems.
652  */
653 class CVC4_PUBLIC DeclareSygusPrimedVarCommand
654     : public DeclarationDefinitionCommand
655 {
656  public:
657   DeclareSygusPrimedVarCommand(const std::string& id, Type type);
658   /** returns the declared primed variable's type */
659   Type getType() const;
660 
661   /** invokes this command
662    *
663    * The type of the primed variable is communicated to the SMT engine for
664    * debugging purposes when a synthesis conjecture is built later on.
665    */
666   void invoke(SmtEngine* smtEngine) override;
667   /** exports command to given expression manager */
668   Command* exportTo(ExprManager* exprManager,
669                     ExprManagerMapCollection& variableMap) override;
670   /** creates a copy of this command */
671   Command* clone() const override;
672   /** returns this command's name */
673   std::string getCommandName() const override;
674 
675  protected:
676   /** the type of the declared primed variable */
677   Type d_type;
678 };
679 
680 /** Declares a sygus universal function variable */
681 class CVC4_PUBLIC DeclareSygusFunctionCommand
682     : public DeclarationDefinitionCommand
683 {
684  public:
685   DeclareSygusFunctionCommand(const std::string& id, Expr func, Type type);
686   /** returns the declared function variable */
687   Expr getFunction() const;
688   /** returns the declared function variable's type */
689   Type getType() const;
690   /** invokes this command
691    *
692    * The declared sygus function variable is communicated to the SMT engine in
693    * case a synthesis conjecture is built later on.
694    */
695   void invoke(SmtEngine* smtEngine) override;
696   /** exports command to given expression manager */
697   Command* exportTo(ExprManager* exprManager,
698                     ExprManagerMapCollection& variableMap) override;
699   /** creates a copy of this command */
700   Command* clone() const override;
701   /** returns this command's name */
702   std::string getCommandName() const override;
703 
704  protected:
705   /** the declared function variable */
706   Expr d_func;
707   /** the declared function variable's type */
708   Type d_type;
709 };
710 
711 /** Declares a sygus function-to-synthesize
712  *
713  * This command is also used for the special case in which we are declaring an
714  * invariant-to-synthesize
715  */
716 class CVC4_PUBLIC SynthFunCommand : public DeclarationDefinitionCommand
717 {
718  public:
719   SynthFunCommand(const std::string& id,
720                   Expr func,
721                   Type sygusType,
722                   bool isInv,
723                   const std::vector<Expr>& vars);
724   SynthFunCommand(const std::string& id, Expr func, Type sygusType, bool isInv);
725   /** returns the function-to-synthesize */
726   Expr getFunction() const;
727   /** returns the input variables of the function-to-synthesize */
728   const std::vector<Expr>& getVars() const;
729   /** returns the sygus type of the function-to-synthesize */
730   Type getSygusType() const;
731   /** returns whether the function-to-synthesize should be an invariant */
732   bool isInv() const;
733 
734   /** invokes this command
735    *
736    * The declared function-to-synthesize is communicated to the SMT engine in
737    * case a synthesis conjecture is built later on.
738    */
739   void invoke(SmtEngine* smtEngine) override;
740   /** exports command to given expression manager */
741   Command* exportTo(ExprManager* exprManager,
742                     ExprManagerMapCollection& variableMap) override;
743   /** creates a copy of this command */
744   Command* clone() const override;
745   /** returns this command's name */
746   std::string getCommandName() const override;
747 
748  protected:
749   /** the function-to-synthesize */
750   Expr d_func;
751   /** sygus type of the function-to-synthesize
752    *
753    * If this type is a "sygus datatype" then it encodes a grammar for the
754    * possible varlues of the function-to-sytnhesize
755    */
756   Type d_sygusType;
757   /** whether the function-to-synthesize should be an invariant */
758   bool d_isInv;
759   /** the input variables of the function-to-synthesize */
760   std::vector<Expr> d_vars;
761 };
762 
763 /** Declares a sygus constraint */
764 class CVC4_PUBLIC SygusConstraintCommand : public Command
765 {
766  public:
767   SygusConstraintCommand(const Expr& e);
768   /** returns the declared constraint */
769   Expr getExpr() const;
770   /** invokes this command
771    *
772    * The declared constraint is communicated to the SMT engine in case a
773    * synthesis conjecture is built later on.
774    */
775   void invoke(SmtEngine* smtEngine) override;
776   /** exports command to given expression manager */
777   Command* exportTo(ExprManager* exprManager,
778                     ExprManagerMapCollection& variableMap) override;
779   /** creates a copy of this command */
780   Command* clone() const override;
781   /** returns this command's name */
782   std::string getCommandName() const override;
783 
784  protected:
785   /** the declared constraint */
786   Expr d_expr;
787 };
788 
789 /** Declares a sygus invariant constraint
790  *
791  * Invarint constraints are declared in a somewhat implicit manner in the SyGuS
792  * language: they are declared in terms of the previously declared
793  * invariant-to-synthesize, precondition, transition relation and condition.
794  *
795  * The actual constraint must be built such that the invariant is not stronger
796  * than the precondition, not weaker than the postcondition and inductive
797  * w.r.t. the transition relation.
798  */
799 class CVC4_PUBLIC SygusInvConstraintCommand : public Command
800 {
801  public:
802   SygusInvConstraintCommand(const std::vector<Expr>& predicates);
803   SygusInvConstraintCommand(const Expr& inv,
804                             const Expr& pre,
805                             const Expr& trans,
806                             const Expr& post);
807   /** returns the place holder predicates */
808   const std::vector<Expr>& getPredicates() const;
809   /** invokes this command
810    *
811    * The place holders are communicated to the SMT engine and the actual
812    * invariant constraint is built, in case an actual synthesis conjecture is
813    * built later on.
814    */
815   void invoke(SmtEngine* smtEngine) override;
816   /** exports command to given expression manager */
817   Command* exportTo(ExprManager* exprManager,
818                     ExprManagerMapCollection& variableMap) override;
819   /** creates a copy of this command */
820   Command* clone() const override;
821   /** returns this command's name */
822   std::string getCommandName() const override;
823 
824  protected:
825   /** the place holder predicates with which to build the actual constraint
826    * (i.e. the invariant, precondition, transition relation and postcondition)
827    */
828   std::vector<Expr> d_predicates;
829 };
830 
831 /** Declares a synthesis conjecture */
832 class CVC4_PUBLIC CheckSynthCommand : public Command
833 {
834  public:
CheckSynthCommand()835   CheckSynthCommand(){};
836   /** returns the result of the check-synth call */
837   Result getResult() const;
838   /** prints the result of the check-synth-call */
839   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
840   /** invokes this command
841    *
842    * This invocation makes the SMT engine build a synthesis conjecture based on
843    * previously declared information (such as universal variables,
844    * functions-to-synthesize and so on), set up attributes to guide the solving,
845    * and then perform a satisfiability check, whose result is stored in
846    * d_result.
847    */
848   void invoke(SmtEngine* smtEngine) override;
849   /** exports command to given expression manager */
850   Command* exportTo(ExprManager* exprManager,
851                     ExprManagerMapCollection& variableMap) override;
852   /** creates a copy of this command */
853   Command* clone() const override;
854   /** returns this command's name */
855   std::string getCommandName() const override;
856 
857  protected:
858   /** result of the check-synth call */
859   Result d_result;
860   /** string stream that stores the output of the solution */
861   std::stringstream d_solution;
862 };
863 
864 /* ------------------- sygus commands  ------------------ */
865 
866 // this is TRANSFORM in the CVC presentation language
867 class CVC4_PUBLIC SimplifyCommand : public Command
868 {
869  protected:
870   Expr d_term;
871   Expr d_result;
872 
873  public:
874   SimplifyCommand(Expr term);
875 
876   Expr getTerm() const;
877   Expr getResult() const;
878   void invoke(SmtEngine* smtEngine) override;
879   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
880   Command* exportTo(ExprManager* exprManager,
881                     ExprManagerMapCollection& variableMap) override;
882   Command* clone() const override;
883   std::string getCommandName() const override;
884 }; /* class SimplifyCommand */
885 
886 class CVC4_PUBLIC ExpandDefinitionsCommand : public Command
887 {
888  protected:
889   Expr d_term;
890   Expr d_result;
891 
892  public:
893   ExpandDefinitionsCommand(Expr term);
894 
895   Expr getTerm() const;
896   Expr getResult() const;
897   void invoke(SmtEngine* smtEngine) override;
898   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
899   Command* exportTo(ExprManager* exprManager,
900                     ExprManagerMapCollection& variableMap) override;
901   Command* clone() const override;
902   std::string getCommandName() const override;
903 }; /* class ExpandDefinitionsCommand */
904 
905 class CVC4_PUBLIC GetValueCommand : public Command
906 {
907  protected:
908   std::vector<Expr> d_terms;
909   Expr d_result;
910 
911  public:
912   GetValueCommand(Expr term);
913   GetValueCommand(const std::vector<Expr>& terms);
914 
915   const std::vector<Expr>& getTerms() const;
916   Expr getResult() const;
917   void invoke(SmtEngine* smtEngine) override;
918   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
919   Command* exportTo(ExprManager* exprManager,
920                     ExprManagerMapCollection& variableMap) override;
921   Command* clone() const override;
922   std::string getCommandName() const override;
923 }; /* class GetValueCommand */
924 
925 class CVC4_PUBLIC GetAssignmentCommand : public Command
926 {
927  protected:
928   SExpr d_result;
929 
930  public:
931   GetAssignmentCommand();
932 
933   SExpr getResult() const;
934   void invoke(SmtEngine* smtEngine) override;
935   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
936   Command* exportTo(ExprManager* exprManager,
937                     ExprManagerMapCollection& variableMap) override;
938   Command* clone() const override;
939   std::string getCommandName() const override;
940 }; /* class GetAssignmentCommand */
941 
942 class CVC4_PUBLIC GetModelCommand : public Command
943 {
944  public:
945   GetModelCommand();
946 
947   // Model is private to the library -- for now
948   // Model* getResult() const ;
949   void invoke(SmtEngine* smtEngine) override;
950   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
951   Command* exportTo(ExprManager* exprManager,
952                     ExprManagerMapCollection& variableMap) override;
953   Command* clone() const override;
954   std::string getCommandName() const override;
955 
956  protected:
957   Model* d_result;
958   SmtEngine* d_smtEngine;
959 }; /* class GetModelCommand */
960 
961 class CVC4_PUBLIC GetProofCommand : public Command
962 {
963  public:
964   GetProofCommand();
965 
966   const Proof& getResult() const;
967   void invoke(SmtEngine* smtEngine) override;
968   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
969   Command* exportTo(ExprManager* exprManager,
970                     ExprManagerMapCollection& variableMap) override;
971   Command* clone() const override;
972   std::string getCommandName() const override;
973 
974  protected:
975   SmtEngine* d_smtEngine;
976   // d_result is owned by d_smtEngine.
977   const Proof* d_result;
978 }; /* class GetProofCommand */
979 
980 class CVC4_PUBLIC GetInstantiationsCommand : public Command
981 {
982  public:
983   GetInstantiationsCommand();
984 
985   void invoke(SmtEngine* smtEngine) override;
986   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
987   Command* exportTo(ExprManager* exprManager,
988                     ExprManagerMapCollection& variableMap) override;
989   Command* clone() const override;
990   std::string getCommandName() const override;
991 
992  protected:
993   SmtEngine* d_smtEngine;
994 }; /* class GetInstantiationsCommand */
995 
996 class CVC4_PUBLIC GetSynthSolutionCommand : public Command
997 {
998  public:
999   GetSynthSolutionCommand();
1000 
1001   void invoke(SmtEngine* smtEngine) override;
1002   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
1003   Command* exportTo(ExprManager* exprManager,
1004                     ExprManagerMapCollection& variableMap) override;
1005   Command* clone() const override;
1006   std::string getCommandName() const override;
1007 
1008  protected:
1009   SmtEngine* d_smtEngine;
1010 }; /* class GetSynthSolutionCommand */
1011 
1012 class CVC4_PUBLIC GetQuantifierEliminationCommand : public Command
1013 {
1014  protected:
1015   Expr d_expr;
1016   bool d_doFull;
1017   Expr d_result;
1018 
1019  public:
1020   GetQuantifierEliminationCommand();
1021   GetQuantifierEliminationCommand(const Expr& expr, bool doFull);
1022 
1023   Expr getExpr() const;
1024   bool getDoFull() const;
1025   void invoke(SmtEngine* smtEngine) override;
1026   Expr getResult() const;
1027   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
1028 
1029   Command* exportTo(ExprManager* exprManager,
1030                     ExprManagerMapCollection& variableMap) override;
1031   Command* clone() const override;
1032   std::string getCommandName() const override;
1033 }; /* class GetQuantifierEliminationCommand */
1034 
1035 class CVC4_PUBLIC GetUnsatAssumptionsCommand : public Command
1036 {
1037  public:
1038   GetUnsatAssumptionsCommand();
1039   void invoke(SmtEngine* smtEngine) override;
1040   std::vector<Expr> getResult() const;
1041   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
1042   Command* exportTo(ExprManager* exprManager,
1043                     ExprManagerMapCollection& variableMap) override;
1044   Command* clone() const override;
1045   std::string getCommandName() const override;
1046  protected:
1047   std::vector<Expr> d_result;
1048 }; /* class GetUnsatAssumptionsCommand */
1049 
1050 class CVC4_PUBLIC GetUnsatCoreCommand : public Command
1051 {
1052  public:
1053   GetUnsatCoreCommand();
1054   const UnsatCore& getUnsatCore() const;
1055 
1056   void invoke(SmtEngine* smtEngine) override;
1057   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
1058 
1059   Command* exportTo(ExprManager* exprManager,
1060                     ExprManagerMapCollection& variableMap) override;
1061   Command* clone() const override;
1062   std::string getCommandName() const override;
1063 
1064  protected:
1065   // the result of the unsat core call
1066   UnsatCore d_result;
1067 }; /* class GetUnsatCoreCommand */
1068 
1069 class CVC4_PUBLIC GetAssertionsCommand : public Command
1070 {
1071  protected:
1072   std::string d_result;
1073 
1074  public:
1075   GetAssertionsCommand();
1076 
1077   void invoke(SmtEngine* smtEngine) override;
1078   std::string getResult() const;
1079   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
1080   Command* exportTo(ExprManager* exprManager,
1081                     ExprManagerMapCollection& variableMap) override;
1082   Command* clone() const override;
1083   std::string getCommandName() const override;
1084 }; /* class GetAssertionsCommand */
1085 
1086 class CVC4_PUBLIC SetBenchmarkStatusCommand : public Command
1087 {
1088  protected:
1089   BenchmarkStatus d_status;
1090 
1091  public:
1092   SetBenchmarkStatusCommand(BenchmarkStatus status);
1093 
1094   BenchmarkStatus getStatus() const;
1095 
1096   void invoke(SmtEngine* smtEngine) override;
1097   Command* exportTo(ExprManager* exprManager,
1098                     ExprManagerMapCollection& variableMap) override;
1099   Command* clone() const override;
1100   std::string getCommandName() const override;
1101 }; /* class SetBenchmarkStatusCommand */
1102 
1103 class CVC4_PUBLIC SetBenchmarkLogicCommand : public Command
1104 {
1105  protected:
1106   std::string d_logic;
1107 
1108  public:
1109   SetBenchmarkLogicCommand(std::string logic);
1110 
1111   std::string getLogic() const;
1112   void invoke(SmtEngine* smtEngine) override;
1113   Command* exportTo(ExprManager* exprManager,
1114                     ExprManagerMapCollection& variableMap) override;
1115   Command* clone() const override;
1116   std::string getCommandName() const override;
1117 }; /* class SetBenchmarkLogicCommand */
1118 
1119 class CVC4_PUBLIC SetInfoCommand : public Command
1120 {
1121  protected:
1122   std::string d_flag;
1123   SExpr d_sexpr;
1124 
1125  public:
1126   SetInfoCommand(std::string flag, const SExpr& sexpr);
1127 
1128   std::string getFlag() const;
1129   SExpr getSExpr() const;
1130 
1131   void invoke(SmtEngine* smtEngine) override;
1132   Command* exportTo(ExprManager* exprManager,
1133                     ExprManagerMapCollection& variableMap) override;
1134   Command* clone() const override;
1135   std::string getCommandName() const override;
1136 }; /* class SetInfoCommand */
1137 
1138 class CVC4_PUBLIC GetInfoCommand : public Command
1139 {
1140  protected:
1141   std::string d_flag;
1142   std::string d_result;
1143 
1144  public:
1145   GetInfoCommand(std::string flag);
1146 
1147   std::string getFlag() const;
1148   std::string getResult() const;
1149 
1150   void invoke(SmtEngine* smtEngine) override;
1151   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
1152   Command* exportTo(ExprManager* exprManager,
1153                     ExprManagerMapCollection& variableMap) override;
1154   Command* clone() const override;
1155   std::string getCommandName() const override;
1156 }; /* class GetInfoCommand */
1157 
1158 class CVC4_PUBLIC SetOptionCommand : public Command
1159 {
1160  protected:
1161   std::string d_flag;
1162   SExpr d_sexpr;
1163 
1164  public:
1165   SetOptionCommand(std::string flag, const SExpr& sexpr);
1166 
1167   std::string getFlag() const;
1168   SExpr getSExpr() const;
1169 
1170   void invoke(SmtEngine* smtEngine) override;
1171   Command* exportTo(ExprManager* exprManager,
1172                     ExprManagerMapCollection& variableMap) override;
1173   Command* clone() const override;
1174   std::string getCommandName() const override;
1175 }; /* class SetOptionCommand */
1176 
1177 class CVC4_PUBLIC GetOptionCommand : public Command
1178 {
1179  protected:
1180   std::string d_flag;
1181   std::string d_result;
1182 
1183  public:
1184   GetOptionCommand(std::string flag);
1185 
1186   std::string getFlag() const;
1187   std::string getResult() const;
1188 
1189   void invoke(SmtEngine* smtEngine) override;
1190   void printResult(std::ostream& out, uint32_t verbosity = 2) const override;
1191   Command* exportTo(ExprManager* exprManager,
1192                     ExprManagerMapCollection& variableMap) override;
1193   Command* clone() const override;
1194   std::string getCommandName() const override;
1195 }; /* class GetOptionCommand */
1196 
1197 // Set expression name command
1198 // Note this is not an official smt2 command
1199 // Conceptually:
1200 //   (assert (! expr :named name))
1201 // is converted to
1202 //   (assert expr)
1203 //   (set-expr-name expr name)
1204 class CVC4_PUBLIC SetExpressionNameCommand : public Command
1205 {
1206  protected:
1207   Expr d_expr;
1208   std::string d_name;
1209 
1210  public:
1211   SetExpressionNameCommand(Expr expr, std::string name);
1212 
1213   void invoke(SmtEngine* smtEngine) override;
1214   Command* exportTo(ExprManager* exprManager,
1215                     ExprManagerMapCollection& variableMap) override;
1216   Command* clone() const override;
1217   std::string getCommandName() const override;
1218 }; /* class SetExpressionNameCommand */
1219 
1220 class CVC4_PUBLIC DatatypeDeclarationCommand : public Command
1221 {
1222  private:
1223   std::vector<DatatypeType> d_datatypes;
1224 
1225  public:
1226   DatatypeDeclarationCommand(const DatatypeType& datatype);
1227 
1228   DatatypeDeclarationCommand(const std::vector<DatatypeType>& datatypes);
1229   const std::vector<DatatypeType>& getDatatypes() const;
1230   void invoke(SmtEngine* smtEngine) override;
1231   Command* exportTo(ExprManager* exprManager,
1232                     ExprManagerMapCollection& variableMap) override;
1233   Command* clone() const override;
1234   std::string getCommandName() const override;
1235 }; /* class DatatypeDeclarationCommand */
1236 
1237 class CVC4_PUBLIC RewriteRuleCommand : public Command
1238 {
1239  public:
1240   typedef std::vector<std::vector<Expr> > Triggers;
1241 
1242  protected:
1243   typedef std::vector<Expr> VExpr;
1244   VExpr d_vars;
1245   VExpr d_guards;
1246   Expr d_head;
1247   Expr d_body;
1248   Triggers d_triggers;
1249 
1250  public:
1251   RewriteRuleCommand(const std::vector<Expr>& vars,
1252                      const std::vector<Expr>& guards,
1253                      Expr head,
1254                      Expr body,
1255                      const Triggers& d_triggers);
1256   RewriteRuleCommand(const std::vector<Expr>& vars, Expr head, Expr body);
1257 
1258   const std::vector<Expr>& getVars() const;
1259   const std::vector<Expr>& getGuards() const;
1260   Expr getHead() const;
1261   Expr getBody() const;
1262   const Triggers& getTriggers() const;
1263 
1264   void invoke(SmtEngine* smtEngine) override;
1265   Command* exportTo(ExprManager* exprManager,
1266                     ExprManagerMapCollection& variableMap) override;
1267   Command* clone() const override;
1268   std::string getCommandName() const override;
1269 }; /* class RewriteRuleCommand */
1270 
1271 class CVC4_PUBLIC PropagateRuleCommand : public Command
1272 {
1273  public:
1274   typedef std::vector<std::vector<Expr> > Triggers;
1275 
1276  protected:
1277   typedef std::vector<Expr> VExpr;
1278   VExpr d_vars;
1279   VExpr d_guards;
1280   VExpr d_heads;
1281   Expr d_body;
1282   Triggers d_triggers;
1283   bool d_deduction;
1284 
1285  public:
1286   PropagateRuleCommand(const std::vector<Expr>& vars,
1287                        const std::vector<Expr>& guards,
1288                        const std::vector<Expr>& heads,
1289                        Expr body,
1290                        const Triggers& d_triggers,
1291                        /* true if we want a deduction rule */
1292                        bool d_deduction = false);
1293   PropagateRuleCommand(const std::vector<Expr>& vars,
1294                        const std::vector<Expr>& heads,
1295                        Expr body,
1296                        bool d_deduction = false);
1297 
1298   const std::vector<Expr>& getVars() const;
1299   const std::vector<Expr>& getGuards() const;
1300   const std::vector<Expr>& getHeads() const;
1301   Expr getBody() const;
1302   const Triggers& getTriggers() const;
1303   bool isDeduction() const;
1304   void invoke(SmtEngine* smtEngine) override;
1305   Command* exportTo(ExprManager* exprManager,
1306                     ExprManagerMapCollection& variableMap) override;
1307   Command* clone() const override;
1308   std::string getCommandName() const override;
1309 }; /* class PropagateRuleCommand */
1310 
1311 class CVC4_PUBLIC ResetCommand : public Command
1312 {
1313  public:
ResetCommand()1314   ResetCommand() {}
1315   void invoke(SmtEngine* smtEngine) override;
1316   Command* exportTo(ExprManager* exprManager,
1317                     ExprManagerMapCollection& variableMap) override;
1318   Command* clone() const override;
1319   std::string getCommandName() const override;
1320 }; /* class ResetCommand */
1321 
1322 class CVC4_PUBLIC ResetAssertionsCommand : public Command
1323 {
1324  public:
ResetAssertionsCommand()1325   ResetAssertionsCommand() {}
1326   void invoke(SmtEngine* smtEngine) override;
1327   Command* exportTo(ExprManager* exprManager,
1328                     ExprManagerMapCollection& variableMap) override;
1329   Command* clone() const override;
1330   std::string getCommandName() const override;
1331 }; /* class ResetAssertionsCommand */
1332 
1333 class CVC4_PUBLIC QuitCommand : public Command
1334 {
1335  public:
QuitCommand()1336   QuitCommand() {}
1337   void invoke(SmtEngine* smtEngine) override;
1338   Command* exportTo(ExprManager* exprManager,
1339                     ExprManagerMapCollection& variableMap) override;
1340   Command* clone() const override;
1341   std::string getCommandName() const override;
1342 }; /* class QuitCommand */
1343 
1344 class CVC4_PUBLIC CommentCommand : public Command
1345 {
1346   std::string d_comment;
1347 
1348  public:
1349   CommentCommand(std::string comment);
1350 
1351   std::string getComment() const;
1352 
1353   void invoke(SmtEngine* smtEngine) override;
1354   Command* exportTo(ExprManager* exprManager,
1355                     ExprManagerMapCollection& variableMap) override;
1356   Command* clone() const override;
1357   std::string getCommandName() const override;
1358 }; /* class CommentCommand */
1359 
1360 class CVC4_PUBLIC CommandSequence : public Command
1361 {
1362  private:
1363   /** All the commands to be executed (in sequence) */
1364   std::vector<Command*> d_commandSequence;
1365   /** Next command to be executed */
1366   unsigned int d_index;
1367 
1368  public:
1369   CommandSequence();
1370   ~CommandSequence();
1371 
1372   void addCommand(Command* cmd);
1373   void clear();
1374 
1375   void invoke(SmtEngine* smtEngine) override;
1376   void invoke(SmtEngine* smtEngine, std::ostream& out) override;
1377 
1378   typedef std::vector<Command*>::iterator iterator;
1379   typedef std::vector<Command*>::const_iterator const_iterator;
1380 
1381   const_iterator begin() const;
1382   const_iterator end() const;
1383 
1384   iterator begin();
1385   iterator end();
1386 
1387   Command* exportTo(ExprManager* exprManager,
1388                     ExprManagerMapCollection& variableMap) override;
1389   Command* clone() const override;
1390   std::string getCommandName() const override;
1391 }; /* class CommandSequence */
1392 
1393 class CVC4_PUBLIC DeclarationSequence : public CommandSequence
1394 {
1395 };
1396 
1397 } /* CVC4 namespace */
1398 
1399 #endif /* __CVC4__COMMAND_H */
1400