92a89a18ee970cf919b4327595f23322b32f8f0d
1 /********************* */
2 /*! \file sygus_print_callback.cpp
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, 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
12 ** \brief Implementation of sygus print callbacks
15 #include "printer/sygus_print_callback.h"
17 #include "expr/node.h"
18 #include "printer/printer.h"
20 using namespace CVC4::kind
;
26 SygusExprPrintCallback::SygusExprPrintCallback(Expr body
,
27 const std::vector
<Expr
>& args
)
28 : d_body(body
), d_body_argument(-1)
30 d_args
.insert(d_args
.end(), args
.begin(), args
.end());
31 for (unsigned i
= 0, size
= d_args
.size(); i
< size
; i
++)
33 if (d_args
[i
] == d_body
)
35 d_body_argument
= static_cast<int>(i
);
40 void SygusExprPrintCallback::doStrReplace(std::string
& str
,
41 const std::string
& oldStr
,
42 const std::string
& newStr
) const
45 while ((pos
= str
.find(oldStr
, pos
)) != std::string::npos
)
47 str
.replace(pos
, oldStr
.length(), newStr
);
48 pos
+= newStr
.length();
52 void SygusExprPrintCallback::toStreamSygus(const Printer
* p
,
56 // optimization: if body is equal to an argument, then just print that one
57 if (d_body_argument
>= 0)
59 p
->toStreamSygus(out
, Node::fromExpr(e
[d_body_argument
]));
64 std::vector
<Node
> vars
;
65 std::vector
<Node
> subs
;
66 for (unsigned i
= 0, size
= d_args
.size(); i
< size
; i
++)
68 vars
.push_back(Node::fromExpr(d_args
[i
]));
70 ss
<< "_setpc_var_" << i
;
71 Node lv
= NodeManager::currentNM()->mkBoundVar(
72 ss
.str(), TypeNode::fromType(d_args
[i
].getType()));
76 // the substituted body should be a non-sygus term
77 Node sbody
= Node::fromExpr(d_body
);
79 sbody
.substitute(vars
.begin(), vars
.end(), subs
.begin(), subs
.end());
81 // print to stream without letification
82 std::stringstream body_out
;
83 p
->toStream(body_out
, sbody
, -1, false, 0);
85 // do string substitution
86 Assert(e
.getNumChildren() == d_args
.size());
87 std::string str_body
= body_out
.str();
88 for (unsigned i
= 0, size
= d_args
.size(); i
< size
; i
++)
90 std::stringstream old_str
;
92 std::stringstream new_str
;
93 p
->toStreamSygus(new_str
, Node::fromExpr(e
[i
]));
94 doStrReplace(str_body
, old_str
.str().c_str(), new_str
.str().c_str());
100 SygusNamedPrintCallback::SygusNamedPrintCallback(std::string name
)
105 void SygusNamedPrintCallback::toStreamSygus(const Printer
* p
,
109 if (e
.getNumChildren() > 0)
114 if (e
.getNumChildren() > 0)
119 p
->toStreamSygus(out
, ec
);
125 void SygusEmptyPrintCallback::toStreamSygus(const Printer
* p
,
129 if (e
.getNumChildren() == 1)
131 p
->toStreamSygus(out
, e
[0]);
139 std::shared_ptr
<SygusEmptyPrintCallback
> SygusEmptyPrintCallback::d_empty_pc
= nullptr;
141 } /* CVC4::printer namespace */
142 } /* CVC4 namespace */