--preserve-facts does not preserve conditional literals
Consider the following input file:
p(a).
p(b).
q(a,c).
q(b,d).
r(c).
r(d).
t(Y) :- r(Y), p(X) : q(X,Y).
I expect grounding while preserving facts to output something like
p(a).
p(b).
q(a,c).
q(b,d).
r(c).
r(d).
t(c) :- r(c), p(a):q(a,c).
t(d) :- r(d), p(b):q(b,d).
instead, I get
p(a).
p(b).
q(a,c).
q(b,d).
r(c).
r(d).
t(c) :- r(c).
t(d) :- r(d).
We can contrast the grounding of t/1 in echo "p(a). p(b). q(a,c). q(b,d). r(c). r(d). t(Y) :- r(Y), p(X) : q(X,Y)." | clingo --text --preserve-facts=all - and in echo "1 {p(a)}. 1 {p(b)}. 1 {q(a,c)}. 1 {q(b,d)}. r(c). r(d). t(Y) :- r(Y), p(X) : q(X,Y)." | clingo --text --preserve-facts=all -.
Preserve facts (body) only affects facts directly occurring in rule bodies and does not extend to conditions.
If you want to prevent the grounder from applying any simplifications you might want to consider making facts externals.
#external p(a). [true]
#external p(b). [true]
#external q(a,c). [true]
#external q(b,d). [true]
#external r(c). [true]
#external r(d). [true]
t(c) :- r(c), p(a):q(a,c).
t(d) :- r(d), p(b):q(b,d).
Thank you for the tip with external. It hadn't occurred to me and it is a little bit more elegant than using false-choice rules.
Regarding preserve-facts only affecting facts directly occuring in the body, wouldn't that mean that q/2 would be simplified away but p/1 would be preserved in the example? So that the output could be something like
p(a).
p(b).
q(a,c).
q(b,d).
r(c).
r(d).
t(c) :- r(c), p(a).
t(d) :- r(d), p(b).
Regarding preserve-facts only affecting facts directly occuring in the body, wouldn't that mean that q/2 would be simplified away but p/1 would be preserved in the example? So that the output could be something like
The feature has been implemented on top of the existing system design. What you describe is certainly possible - just not implemented. I am also not sure whether it is worth to spend any time here. Maybe I'll add an option that treats facts as externals for the next major release...
An option to treat facts as externals would be perfect for my use-cases (in teaching and in my own debugging). Both these needs can be met with manually writing #external, so this option is certainly low priority.
I think this will take a completely different route. Preserving facts while grounding falls into the category special use cases. I'll keep the system simple and drop the preserve-facts option in the next major release.