Skip to content

Commit

Permalink
improve AST to string conversion
Browse files Browse the repository at this point in the history
Since there is no direct AST representation for terms of form `f(a;b)`,
they are represented as `f(a);f(b)`. This commit changes printing, so
that such pools are printed in the shorter form. This way, the string
representation of rules containing pools in arguments of predicates is
parsable.

Addresses #171.
  • Loading branch information
rkaminsk committed Dec 8, 2019
1 parent a799a85 commit 9a51241
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/clingo/tests/python/ast2.lp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ def trm():
("p((a^b)).", ["p((a^b))."]),
("p(a..b).", ["p((a..b))."]),
("p((),(1,),f(),f(1,2)).", ["p((),(1,),f,f(1,2))."]),
("p(a;b).", ["(p(a);p(b))."]),
("p(a;b).", ["p(a;b)."]),
("p(@f(a;b)).", ["p(@f(a;b))."]),
("p((a,;b)).", ["p(((a,);b))."]),
("p(((a,);b)).", ["p(((a,);b))."]),
("1 $+ 3 $* $x $+ 7 $< 2 $< 3.", ["1$+3$*$x$+7$<2$<3."]),
])

Expand Down
2 changes: 1 addition & 1 deletion app/clingo/tests/python/ast2.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Step: 1
bdl(0,ok) bdl(1,ok) bdl(2,ok) bdl(3,ok) bdl(4,ok) bdl(5,ok) bdl(6,ok) bdl(7,ok) bdl(8,ok) bdl(9,ok) hdl(0,ok) hdl(1,ok) hdl(2,ok) hdl(3,ok) hdl(4,ok) hdl(5,ok) hdl(6,ok) hdl(7,ok) lit(0,ok) lit(1,ok) lit(2,ok) lit(3,ok) lit(4,ok) lit(5,ok) lit(6,ok) lit(7,ok) stm(0,ok) stm(1,ok) stm(10,ok) stm(11,ok) stm(12,ok) stm(13,ok) stm(14,ok) stm(15,ok) stm(16,ok) stm(17,ok) stm(18,ok) stm(2,ok) stm(3,ok) stm(4,ok) stm(5,ok) stm(6,ok) stm(7,ok) stm(8,ok) stm(9,ok) thd(0,ok) thd(1,ok) thd(2,ok) tht(0,ok) tht(1,ok) tht(10,ok) tht(11,ok) tht(2,ok) tht(3,ok) tht(4,ok) tht(5,ok) tht(6,ok) tht(7,ok) tht(8,ok) tht(9,ok) trm(0,ok) trm(1,ok) trm(10,ok) trm(11,ok) trm(12,ok) trm(13,ok) trm(14,ok) trm(15,ok) trm(16,ok) trm(2,ok) trm(3,ok) trm(4,ok) trm(5,ok) trm(6,ok) trm(7,ok) trm(8,ok) trm(9,ok)
bdl(0,ok) bdl(1,ok) bdl(2,ok) bdl(3,ok) bdl(4,ok) bdl(5,ok) bdl(6,ok) bdl(7,ok) bdl(8,ok) bdl(9,ok) hdl(0,ok) hdl(1,ok) hdl(2,ok) hdl(3,ok) hdl(4,ok) hdl(5,ok) hdl(6,ok) hdl(7,ok) lit(0,ok) lit(1,ok) lit(2,ok) lit(3,ok) lit(4,ok) lit(5,ok) lit(6,ok) lit(7,ok) stm(0,ok) stm(1,ok) stm(10,ok) stm(11,ok) stm(12,ok) stm(13,ok) stm(14,ok) stm(15,ok) stm(16,ok) stm(17,ok) stm(18,ok) stm(2,ok) stm(3,ok) stm(4,ok) stm(5,ok) stm(6,ok) stm(7,ok) stm(8,ok) stm(9,ok) thd(0,ok) thd(1,ok) thd(2,ok) tht(0,ok) tht(1,ok) tht(10,ok) tht(11,ok) tht(2,ok) tht(3,ok) tht(4,ok) tht(5,ok) tht(6,ok) tht(7,ok) tht(8,ok) tht(9,ok) trm(0,ok) trm(1,ok) trm(10,ok) trm(11,ok) trm(12,ok) trm(13,ok) trm(14,ok) trm(15,ok) trm(16,ok) trm(17,ok) trm(18,ok) trm(2,ok) trm(3,ok) trm(4,ok) trm(5,ok) trm(6,ok) trm(7,ok) trm(8,ok) trm(9,ok)
SAT
38 changes: 36 additions & 2 deletions libclingo/clingo.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5058,8 +5058,42 @@ inline std::ostream &operator<<(std::ostream &out, CSPProduct const &x) {

inline std::ostream &operator<<(std::ostream &out, Pool const &x) {
// NOTE: there is no representation for an empty pool
if (x.arguments.empty()) { out << "(1/0)"; }
else { out << Detail::print(x.arguments, "(", ";", ")", true); }
auto &args = x.arguments;
if (args.empty()) { out << "(1/0)"; }
if (args.size() == 1) { out << args[0]; }
else {
bool equal{true};
Function const *old{nullptr};
for (auto &arg : args) {
if (arg.data.is<Function>()) {
auto &fun = arg.data.get<Function>();
if (!old) { old = &fun; }
else if (strcmp(fun.name, old->name) != 0 || fun.external != old->external) {
equal = false;
break;
}
}
else {
equal = false;
break;
}
}
if (equal) {
out << (old->external ? "@" : "") << old->name << "(";
bool sem = false;
for (auto &arg : args) {
if (sem) { out << ";"; }
else { sem = true; }
auto &pargs = arg.data.get<Function>().arguments;
bool tc = old->name[0] == '\0' && pargs.size() == 1;
out << Detail::print(pargs, "", ",", tc ? "," : "", true);
}
out << ")";
}
else {
out << Detail::print(x.arguments, "(", ";", ")", true);
}
}
return out;
}

Expand Down
4 changes: 3 additions & 1 deletion libclingo/tests/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ TEST_CASE("parse-ast", "[clingo]") {
REQUIRE(parse("p((a^b)).") == "p((a^b)).");
REQUIRE(parse("p(a..b).") == "p((a..b)).");
REQUIRE(parse("p((),(1,),f(),f(1,2)).") == "p((),(1,),f,f(1,2)).");
REQUIRE(parse("p(a;b).") == "(p(a);p(b)).");
REQUIRE(parse("p(@f(a;b)).") == "p(@f(a;b)).");
REQUIRE(parse("p(a;b).") == "p(a;b).");
REQUIRE(parse("p((a,;b)).") == "p(((a,);b)).");
REQUIRE(parse("p(((a,);b)).") == "p(((a,);b)).");
REQUIRE(parse("1 $+ 3 $* $x $+ 7 $< 2 $< 3.") == "1$+3$*$x$+7$<2$<3.");
}
SECTION("theory terms") {
Expand Down
39 changes: 38 additions & 1 deletion libpyclingo/pyclingo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5372,7 +5372,44 @@ given in the [grammar](.) above.
case ASTType::Pool: {
Object args = fields_.getItem("arguments");
if (args.empty()) { out << "(1/0)"; }
else { out << printList(args, "(", ";", ")", true); }
if (args.size() == 1) { out << args.getItem(0); }
else {
bool equal = true, old_ext = false;
Object old_name;
for (auto arg : args.iter()) {
if (arg.getAttr("type") == ASTType::getAttr(ASTType::Function)) {
auto name = arg.getAttr("name");
auto ext = pyToCpp<bool>(arg.getAttr("external"));
if (!old_name.valid()) {
old_name = name;
old_ext = ext;
}
else if (name != old_name || ext != old_ext) {
equal = false;
break;
}
}
else {
equal = false;
break;
}
}
if (equal) {
out << (old_ext ? "@" : "") << old_name << "(";
bool sem = false;
for (auto arg : args.iter()) {
if (sem) { out << ";"; }
else { sem = true; }
auto pargs = arg.getAttr("arguments");
bool tc = old_name.size() == 0 && pargs.size() == 1;
out << printList(pargs, "", ",", tc ? "," : "", true);
}
out << ")";
}
else {
out << printList(args, "(", ";", ")", true);
}
}
break;
}
case ASTType::CSPProduct: {
Expand Down

0 comments on commit 9a51241

Please sign in to comment.