Consider a clause with cut
f(X) :- g(X), !, h(X).
I think it is a common sense that we can refactor cuts as follows:
h1(X) :- !, h(X).
f(X) :- g(X), h1(X).
However, for DCGs, this kind of macro-like refactoring for cuts doesn't seem to work. For instance, I tried to refactor a DCG clause like this
f(X) --> g(X), !, h(X).
into something like below
h1(X) --> !, h(X).
f(X) --> g(X), h1(X).
but it just didn't work. That is, failure in h(X)
in the latter while it didn't backtrack in the former before refactoring the cut.
Is there a general principles when refactoring DCGs with cuts? Or, some additional dark corners to consider when using cuts inside DCGs?