Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python str() output of certain clingo AST modules does not match valid Gringo input format #171

Closed
madingess opened this issue Oct 16, 2019 · 2 comments
Assignees
Milestone

Comments

@madingess
Copy link

My use case is to parse a program with the python Clingo module using clingo.parse_program(). Then, using the abstract syntax tree returned by the parser, I make observations on the program and possibly perform transformations to specific rules within the program. Finally, the result is output to file using the python 2.7 str() function on each rule of the (possibly transformed) program.

The problem is observed regardless of whether I make transformations to the program. Even if the program makes no transformations, I may still find that the output file contains syntax which is not valid as input to Gringo or Clingo.

For example, with pools, the following code

import clingo

inputFile = "q(1;2;3)."
program_rules = []

clingo.parse_program(inputFile, lambda rule: program_rules.append(rule))

for rule in program_rules:

print "%s" % str(rule)

will reliably output

#program base.
(q(1);q(2);q(3)).

The #program base. statement is valid Gringo input; however, the output of the pool statement (q(1);q(2);q(3)). is not valid input.

Similarly, with head disjunction. If we instead use

inputFile = "q(1) | q(2)."

we reliably see the output

#program base.
q(2) : ; q(1) : .

Again, the second line is not valid Gringo input. I suspect a similar outcome with body disjoints.

These could be handled manually, by checking whether the rule uses pools or disjoints, and outputting an appropriate Gringo syntax form. But is there perhaps a built-in output method of which I am not aware? Or some better way of handling the issue?

Thank you in advance.

@rkaminsk
Copy link
Member

The string representation of the AST has been written with the idea to ease debugging not returning a parsable format. The AST for a statement is meant to be passed to a ProgramBuilder object.

At the moment the AST implementation is still marked as experimental. So far I did not have the time to give it the attention it needs. Unfortunately, I also won't have the time anytime soon because improving its implementation is actually a huge time sink.

I could address your two issues for the next release but I do not give any guarantees that the string representation of any AST can be parsed again.

@rkaminsk rkaminsk self-assigned this Oct 28, 2019
@rkaminsk rkaminsk added this to the v5.4.1 milestone Oct 28, 2019
rkaminsk added a commit that referenced this issue Dec 8, 2019
The parser now accepts disjunctions with empty conditions - e.g., `a:.`
Note that for technical reasons the `|` symbol cannot be used after an
empty condition - e.g., `a:;b.` is accepted while `a:|b.` is not.

This also slightly changes the way disjunctions are ordered - e.g.,
`a | b | c.` is no longer grounded as `b | c | a.` but as `a | b | c.`.

This commit partly addresses #171.
rkaminsk added a commit that referenced this issue Dec 8, 2019
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.
@rkaminsk
Copy link
Member

rkaminsk commented Dec 8, 2019

I addressed your two issues. I have not checked thoroughly if every AST can be parsed again. If you find more cases, feel free to open a new issue.

@rkaminsk rkaminsk closed this as completed Dec 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants